OXYGENHUB
# 7d2eb590051f oxygenhub / crates / hub / src / assistants.rs
29.8 KiB · Rust · last changed AI assistants review proposals and draft their descriptions 1 month ago source blame history raw
1
//! AI assistants (hub spec §13.4, #97): chat-completion endpoints the hub
2
//! may ask to review a proposal or draft its description. Assistants are
3
//! kept at three levels, site, organisation and repository, and combined
4
//! downwards: a repository sees its organisation's and the site's beside
5
//! its own, the innermost of a name wins, and one disabled at an outer
6
//! level is off for everything inside it. Each level adds a prompt; the
7
//! prompt in force for a repository is the site's, the organisation's and
8
//! the repository's own, each under a heading. Tokens are sealed with
9
//! ChaCha20-Poly1305 under `keys/assistants.key`, shown masked, and never
10
//! leave the server.
11
12
use crate::db::{now_ms, Pool};
13
use crate::Shared;
14
use rand::RngCore;
15
use ring::aead;
16
use sqlx::FromRow;
17
use std::path::Path;
18
use std::time::Duration;
19
20
/// The request shapes an assistant may speak.
21
pub const APIS: [&str; 2] = ["chat-completions", "anthropic-messages"];
22
/// Characters of context (prompt, description, diffs) a call may carry
23
/// unless the assistant says otherwise.
24
pub const DEFAULT_BUDGET: i64 = 120_000;
25
/// How long one call may take.
26
pub const CALL_TIMEOUT: Duration = Duration::from_secs(60);
27
/// The most an answer is kept.
28
const MAX_ANSWER: usize = 64 * 1024;
29
30
/// Which level an assistant or a prompt belongs to.
31
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32
pub enum Scope {
33
    Site,
34
    Org(i64),
35
    Repo(i64),
36
}
37
38
impl Scope {
39
    pub fn kind(self) -> &'static str {
40
        match self {
41
            Scope::Site => "site",
42
            Scope::Org(_) => "org",
43
            Scope::Repo(_) => "repo",
44
        }
45
    }
46
47
    pub fn id(self) -> i64 {
48
        match self {
49
            Scope::Site => 0,
50
            Scope::Org(id) | Scope::Repo(id) => id,
51
        }
52
    }
53
}
54
55
#[derive(Clone, Debug, FromRow)]
56
pub struct Assistant {
57
    pub id: i64,
58
    pub scope: String,
59
    pub scope_id: i64,
60
    pub name: String,
61
    pub endpoint: String,
62
    /// One of [`APIS`].
63
    pub api: String,
64
    pub model: String,
65
    /// Extra request headers, one `Name: value` per line.
66
    pub headers: String,
67
    /// The sealed token; empty when the endpoint needs none.
68
    pub token: Vec<u8>,
69
    pub enabled: bool,
70
    pub budget: i64,
71
    pub created_ms: i64,
72
    pub updated_ms: i64,
73
}
74
75
impl Assistant {
76
    pub fn scope(&self) -> Scope {
77
        match self.scope.as_str() {
78
            "org" => Scope::Org(self.scope_id),
79
            "repo" => Scope::Repo(self.scope_id),
80
            _ => Scope::Site,
81
        }
82
    }
83
84
    pub fn has_token(&self) -> bool {
85
        !self.token.is_empty()
86
    }
87
88
    /// The extra headers as pairs; malformed lines are skipped.
89
    pub fn header_list(&self) -> Vec<(String, String)> {
90
        parse_headers(&self.headers).unwrap_or_default()
91
    }
92
}
93
94
/// What a settings form supplies for an assistant.
95
#[derive(Clone, Debug, Default)]
96
pub struct Fields {
97
    pub name: String,
98
    pub endpoint: String,
99
    pub api: String,
100
    pub model: String,
101
    pub headers: String,
102
    pub enabled: bool,
103
    pub budget: i64,
104
}
105
106
/// `Name: value` lines to pairs: `Err(line)` names the first bad one.
107
pub fn parse_headers(text: &str) -> Result<Vec<(String, String)>, String> {
108
    let mut out = vec![];
109
    for line in text.lines().map(str::trim).filter(|l| !l.is_empty()) {
110
        let Some((name, value)) = line.split_once(':') else { return Err(line.to_string()) };
111
        let (name, value) = (name.trim(), value.trim());
112
        let name_ok = !name.is_empty() && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_');
113
        if !name_ok || value.chars().any(|c| c.is_control()) {
114
            return Err(line.to_string());
115
        }
116
        out.push((name.to_string(), value.to_string()));
117
    }
118
    Ok(out)
119
}
120
121
// -------------------------------------------------------------- sealing
122
123
/// The key that seals tokens, kept in `<data>/keys/assistants.key`.
124
#[derive(Clone)]
125
pub struct SecretKey([u8; 32]);
126
127
impl SecretKey {
128
    pub fn load_or_create(keys: &Path) -> anyhow::Result<SecretKey> {
129
        std::fs::create_dir_all(keys)?;
130
        let path = keys.join("assistants.key");
131
        if let Ok(bytes) = std::fs::read(&path) {
132
            if bytes.len() == 32 {
133
                return Ok(SecretKey(bytes.try_into().unwrap()));
134
            }
135
        }
136
        let mut k = [0u8; 32];
137
        rand::rngs::OsRng.fill_bytes(&mut k);
138
        std::fs::write(&path, k)?;
139
        #[cfg(unix)]
140
        {
141
            use std::os::unix::fs::PermissionsExt;
142
            std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600))?;
143
        }
144
        Ok(SecretKey(k))
145
    }
146
147
    fn key(&self) -> aead::LessSafeKey {
148
        aead::LessSafeKey::new(aead::UnboundKey::new(&aead::CHACHA20_POLY1305, &self.0).expect("key length"))
149
    }
150
151
    /// Seal a token: a random nonce, then the ciphertext and tag. An
152
    /// empty token seals to nothing, meaning "no token".
153
    pub fn seal(&self, plain: &str) -> Vec<u8> {
154
        if plain.is_empty() {
155
            return vec![];
156
        }
157
        let mut nonce = [0u8; 12];
158
        rand::rngs::OsRng.fill_bytes(&mut nonce);
159
        let mut out = plain.as_bytes().to_vec();
160
        self.key().seal_in_place_append_tag(aead::Nonce::assume_unique_for_key(nonce), aead::Aad::empty(), &mut out).expect("seal");
161
        let mut sealed = nonce.to_vec();
162
        sealed.extend(out);
163
        sealed
164
    }
165
166
    /// The token back, or `None` when the bytes were sealed under another
167
    /// key or damaged. Nothing opens to an empty token.
168
    pub fn open(&self, sealed: &[u8]) -> Option<String> {
169
        if sealed.is_empty() {
170
            return Some(String::new());
171
        }
172
        let (nonce, rest) = sealed.split_at_checked(12)?;
173
        let mut buf = rest.to_vec();
174
        let nonce = aead::Nonce::try_assume_unique_for_key(nonce).ok()?;
175
        let plain = self.key().open_in_place(nonce, aead::Aad::empty(), &mut buf).ok()?;
176
        String::from_utf8(plain.to_vec()).ok()
177
    }
178
}
179
180
// ------------------------------------------------------------- storage
181
182
pub async fn list(pool: &Pool, scope: Scope) -> sqlx::Result<Vec<Assistant>> {
183
    sqlx::query_as("SELECT * FROM assistants WHERE scope = ? AND scope_id = ? ORDER BY name")
184
        .bind(scope.kind())
185
        .bind(scope.id())
186
        .fetch_all(pool)
187
        .await
188
}
189
190
pub async fn get(pool: &Pool, id: i64) -> sqlx::Result<Option<Assistant>> {
191
    sqlx::query_as("SELECT * FROM assistants WHERE id = ?").bind(id).fetch_optional(pool).await
192
}
193
194
/// Add an assistant; `None` when the level already has one of that name.
195
pub async fn create(pool: &Pool, scope: Scope, f: &Fields, token: &[u8]) -> sqlx::Result<Option<Assistant>> {
196
    let now = now_ms();
197
    let r = sqlx::query_as(
198
        "INSERT INTO assistants (scope, scope_id, name, endpoint, api, model, headers, token, enabled, budget, created_ms, updated_ms) \
199
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT DO NOTHING RETURNING *",
200
    )
201
    .bind(scope.kind())
202
    .bind(scope.id())
203
    .bind(&f.name)
204
    .bind(&f.endpoint)
205
    .bind(&f.api)
206
    .bind(&f.model)
207
    .bind(&f.headers)
208
    .bind(token)
209
    .bind(f.enabled)
210
    .bind(f.budget)
211
    .bind(now)
212
    .bind(now)
213
    .fetch_optional(pool)
214
    .await?;
215
    Ok(r)
216
}
217
218
/// Change an assistant's details; `token` is `None` to keep the stored
219
/// one. False when the name is taken at that level.
220
pub async fn update(pool: &Pool, id: i64, f: &Fields, token: Option<&[u8]>) -> sqlx::Result<bool> {
221
    let r = match token {
222
        Some(t) => sqlx::query(
223
            "UPDATE assistants SET name = ?, endpoint = ?, api = ?, model = ?, headers = ?, enabled = ?, budget = ?, token = ?, updated_ms = ? \
224
             WHERE id = ? AND NOT EXISTS (SELECT 1 FROM assistants o WHERE o.scope = assistants.scope AND o.scope_id = assistants.scope_id AND o.name = ? AND o.id <> ?)",
225
        )
226
        .bind(&f.name)
227
        .bind(&f.endpoint)
228
        .bind(&f.api)
229
        .bind(&f.model)
230
        .bind(&f.headers)
231
        .bind(f.enabled)
232
        .bind(f.budget)
233
        .bind(t)
234
        .bind(now_ms())
235
        .bind(id)
236
        .bind(&f.name)
237
        .bind(id)
238
        .execute(pool)
239
        .await?,
240
        None => sqlx::query(
241
            "UPDATE assistants SET name = ?, endpoint = ?, api = ?, model = ?, headers = ?, enabled = ?, budget = ?, updated_ms = ? \
242
             WHERE id = ? AND NOT EXISTS (SELECT 1 FROM assistants o WHERE o.scope = assistants.scope AND o.scope_id = assistants.scope_id AND o.name = ? AND o.id <> ?)",
243
        )
244
        .bind(&f.name)
245
        .bind(&f.endpoint)
246
        .bind(&f.api)
247
        .bind(&f.model)
248
        .bind(&f.headers)
249
        .bind(f.enabled)
250
        .bind(f.budget)
251
        .bind(now_ms())
252
        .bind(id)
253
        .bind(&f.name)
254
        .bind(id)
255
        .execute(pool)
256
        .await?,
257
    };
258
    Ok(r.rows_affected() == 1)
259
}
260
261
pub async fn set_enabled(pool: &Pool, id: i64, enabled: bool) -> sqlx::Result<()> {
262
    sqlx::query("UPDATE assistants SET enabled = ?, updated_ms = ? WHERE id = ?")
263
        .bind(enabled)
264
        .bind(now_ms())
265
        .bind(id)
266
        .execute(pool)
267
        .await?;
268
    Ok(())
269
}
270
271
pub async fn delete(pool: &Pool, id: i64) -> sqlx::Result<()> {
272
    sqlx::query("DELETE FROM assistants WHERE id = ?").bind(id).execute(pool).await?;
273
    Ok(())
274
}
275
276
/// Forget a level's assistants and prompt, when the organisation or
277
/// repository goes.
278
pub async fn delete_scope(pool: &Pool, scope: Scope) -> sqlx::Result<()> {
279
    sqlx::query("DELETE FROM assistants WHERE scope = ? AND scope_id = ?").bind(scope.kind()).bind(scope.id()).execute(pool).await?;
280
    sqlx::query("DELETE FROM assistant_prompts WHERE scope = ? AND scope_id = ?").bind(scope.kind()).bind(scope.id()).execute(pool).await?;
281
    Ok(())
282
}
283
284
/// The prompt a level adds, empty when none.
285
pub async fn prompt(pool: &Pool, scope: Scope) -> sqlx::Result<String> {
286
    Ok(sqlx::query_scalar("SELECT prompt FROM assistant_prompts WHERE scope = ? AND scope_id = ?")
287
        .bind(scope.kind())
288
        .bind(scope.id())
289
        .fetch_optional(pool)
290
        .await?
291
        .unwrap_or_default())
292
}
293
294
pub async fn set_prompt(pool: &Pool, scope: Scope, text: &str) -> sqlx::Result<()> {
295
    if text.trim().is_empty() {
296
        sqlx::query("DELETE FROM assistant_prompts WHERE scope = ? AND scope_id = ?")
297
            .bind(scope.kind())
298
            .bind(scope.id())
299
            .execute(pool)
300
            .await?;
301
    } else {
302
        sqlx::query(
303
            "INSERT INTO assistant_prompts (scope, scope_id, prompt, updated_ms) VALUES (?, ?, ?, ?) \
304
             ON CONFLICT (scope, scope_id) DO UPDATE SET prompt = excluded.prompt, updated_ms = excluded.updated_ms",
305
        )
306
        .bind(scope.kind())
307
        .bind(scope.id())
308
        .bind(text.trim())
309
        .bind(now_ms())
310
        .execute(pool)
311
        .await?;
312
    }
313
    Ok(())
314
}
315
316
// ----------------------------------------------------- what applies where
317
318
/// The levels above a scope, outermost first: the site, then the
319
/// organisation when the owner is one.
320
pub async fn levels_above(pool: &Pool, scope: Scope, owner: &str) -> sqlx::Result<Vec<(Scope, String)>> {
321
    let mut out = vec![];
322
    match scope {
323
        Scope::Site => {}
324
        Scope::Org(_) => out.push((Scope::Site, "the site".to_string())),
325
        Scope::Repo(_) => {
326
            out.push((Scope::Site, "the site".to_string()));
327
            if let Some(org) = crate::db::accounts::org_by_name(pool, owner).await? {
328
                out.push((Scope::Org(org.id), format!("the {} organisation", org.name)));
329
            }
330
        }
331
    }
332
    Ok(out)
333
}
334
335
/// An assistant seen from a level: where it comes from, and whether it is
336
/// on once outer levels have had their say.
337
#[derive(Clone, Debug)]
338
pub struct Available {
339
    pub assistant: Assistant,
340
    /// `the site`, `the acme organisation`, or empty for the level's own.
341
    pub from: String,
342
    /// Off because a level above disabled an assistant of this name.
343
    pub off_above: bool,
344
}
345
346
impl Available {
347
    pub fn on(&self) -> bool {
348
        self.assistant.enabled && !self.off_above
349
    }
350
}
351
352
/// Every assistant a level sees, outermost first, one per name: the
353
/// innermost definition of a name wins, and a name disabled at an outer
354
/// level is off inside it whatever the inner level says.
355
pub async fn available(pool: &Pool, scope: Scope, owner: &str) -> sqlx::Result<Vec<Available>> {
356
    let mut levels = levels_above(pool, scope, owner).await?;
357
    levels.push((scope, String::new()));
358
    let mut out: Vec<Available> = vec![];
359
    let mut off: Vec<String> = vec![];
360
    for (level, from) in levels {
361
        for a in list(pool, level).await? {
362
            let off_above = off.iter().any(|n| n.eq_ignore_ascii_case(&a.name));
363
            if !a.enabled && !off.iter().any(|n| n.eq_ignore_ascii_case(&a.name)) {
364
                off.push(a.name.clone());
365
            }
366
            out.retain(|o| !o.assistant.name.eq_ignore_ascii_case(&a.name));
367
            out.push(Available { assistant: a, from: from.clone(), off_above });
368
        }
369
    }
370
    Ok(out)
371
}
372
373
/// The assistants a repository may ask, by name.
374
pub async fn enabled_for_repo(pool: &Pool, repo: &crate::db::repos::RepoRow) -> sqlx::Result<Vec<Assistant>> {
375
    Ok(available(pool, Scope::Repo(repo.id), &repo.owner).await?.into_iter().filter(Available::on).map(|a| a.assistant).collect())
376
}
377
378
/// The prompts a level inherits, outermost first: (where from, text),
379
/// only the levels that set one.
380
pub async fn inherited_prompts(pool: &Pool, scope: Scope, owner: &str) -> sqlx::Result<Vec<(String, String)>> {
381
    let mut out = vec![];
382
    for (level, from) in levels_above(pool, scope, owner).await? {
383
        let text = prompt(pool, level).await?;
384
        if !text.is_empty() {
385
            out.push((from, text));
386
        }
387
    }
388
    Ok(out)
389
}
390
391
/// The prompt in force for a repository: each level's text under a
392
/// heading, outermost first, so a project adds to what the site says.
393
pub async fn prompt_for_repo(pool: &Pool, repo: &crate::db::repos::RepoRow) -> sqlx::Result<String> {
394
    let scope = Scope::Repo(repo.id);
395
    let mut parts = inherited_prompts(pool, scope, &repo.owner).await?;
396
    let own = prompt(pool, scope).await?;
397
    if !own.is_empty() {
398
        parts.push(("this repository".to_string(), own));
399
    }
400
    Ok(parts.into_iter().map(|(from, text)| format!("## Instructions from {from}\n\n{text}\n")).collect::<Vec<_>>().join("\n"))
401
}
402
403
// -------------------------------------------------------------- calling
404
405
/// Ask an assistant: the system prompt and the request, the answer's
406
/// text. Errors are for the person who asked: a status and the first
407
/// line of what the endpoint said, or why no answer came.
408
pub async fn ask(app: &Shared, a: &Assistant, system: &str, user: &str) -> Result<String, String> {
409
    let Some(token) = app.assistant_key.open(&a.token) else {
410
        return Err("its token cannot be read: set it again".into());
411
    };
412
    let client = reqwest::Client::builder()
413
        .timeout(CALL_TIMEOUT)
414
        .user_agent(format!("oxygenhub/{}", env!("CARGO_PKG_VERSION")))
415
        .build()
416
        .map_err(|e| e.to_string())?;
417
    let mut req = client.post(&a.endpoint).header("content-type", "application/json");
418
    let body = match a.api.as_str() {
419
        "anthropic-messages" => {
420
            if !token.is_empty() {
421
                req = req.header("x-api-key", &token);
422
            }
423
            req = req.header("anthropic-version", "2023-06-01");
424
            serde_json::json!({
425
                "model": a.model,
426
                "max_tokens": 4096,
427
                "system": system,
428
                "messages": [{ "role": "user", "content": user }],
429
            })
430
        }
431
        _ => {
432
            if !token.is_empty() {
433
                req = req.header("authorization", format!("Bearer {token}"));
434
            }
435
            serde_json::json!({
436
                "model": a.model,
437
                "messages": [
438
                    { "role": "system", "content": system },
439
                    { "role": "user", "content": user },
440
                ],
441
            })
442
        }
443
    };
444
    for (name, value) in a.header_list() {
445
        req = req.header(name, value);
446
    }
447
    let r = req.json(&body).send().await.map_err(|e| {
448
        if e.is_timeout() {
449
            "no answer within 60 seconds".to_string()
450
        } else {
451
            scrub(&e.to_string(), &token)
452
        }
453
    })?;
454
    let status = r.status();
455
    let text: String = r.text().await.unwrap_or_default().chars().take(MAX_ANSWER).collect();
456
    if !status.is_success() {
457
        let why = serde_json::from_str::<serde_json::Value>(&text)
458
            .ok()
459
            .and_then(|v| v["error"]["message"].as_str().or(v["error"].as_str()).or(v["message"].as_str()).map(str::to_string))
460
            .unwrap_or_else(|| text.lines().next().unwrap_or("").chars().take(160).collect());
461
        return Err(scrub(&format!("{} {}", status.as_u16(), why.trim()), &token));
462
    }
463
    let v: serde_json::Value = serde_json::from_str(&text).map_err(|_| "the answer was not JSON".to_string())?;
464
    let answer = match a.api.as_str() {
465
        "anthropic-messages" => v["content"]
466
            .as_array()
467
            .map(|parts| parts.iter().filter_map(|p| p["text"].as_str()).collect::<Vec<_>>().join(""))
468
            .unwrap_or_default(),
469
        _ => match &v["choices"][0]["message"]["content"] {
470
            serde_json::Value::String(s) => s.clone(),
471
            serde_json::Value::Array(parts) => parts.iter().filter_map(|p| p["text"].as_str()).collect::<Vec<_>>().join(""),
472
            _ => String::new(),
473
        },
474
    };
475
    if answer.trim().is_empty() {
476
        return Err("the answer carried no text".into());
477
    }
478
    Ok(answer)
479
}
480
481
/// A tiny exchange, for the settings page's TEST: the start of the answer.
482
pub async fn test(app: &Shared, a: &Assistant) -> Result<String, String> {
483
    let answer = ask(app, a, "You are being connected to an OxygenHub site. Answer briefly.", "Reply with the single word OK.").await?;
484
    Ok(answer.trim().chars().take(80).collect())
485
}
486
487
/// Never let the token through in an error message.
488
fn scrub(text: &str, token: &str) -> String {
489
    if token.is_empty() {
490
        text.to_string()
491
    } else {
492
        text.replace(token, "***")
493
    }
494
}
495
496
#[cfg(test)]
497
mod tests {
498
    use super::*;
499
500
    #[test]
501
    fn tokens_seal_and_open_only_under_their_key() {
502
        let dir = tempfile::tempdir().unwrap();
503
        let k = SecretKey::load_or_create(dir.path()).unwrap();
504
        let sealed = k.seal("sk-very-secret");
505
        assert_ne!(sealed, b"sk-very-secret");
506
        assert!(!String::from_utf8_lossy(&sealed).contains("secret"));
507
        assert_eq!(k.open(&sealed).as_deref(), Some("sk-very-secret"));
508
        assert_eq!(SecretKey::load_or_create(dir.path()).unwrap().open(&sealed).as_deref(), Some("sk-very-secret"), "the key is kept");
509
        let other = SecretKey::load_or_create(tempfile::tempdir().unwrap().path()).unwrap();
510
        assert_eq!(other.open(&sealed), None);
511
        assert_eq!(k.open(&[1, 2, 3]), None);
512
        assert_eq!(k.seal(""), Vec::<u8>::new());
513
        assert_eq!(k.open(&[]).as_deref(), Some(""));
514
    }
515
516
    #[test]
517
    fn headers_parse_or_name_the_bad_line() {
518
        assert_eq!(
519
            parse_headers("X-Org: acme\n\n OpenAI-Beta : v1 ").unwrap(),
520
            vec![("X-Org".into(), "acme".into()), ("OpenAI-Beta".into(), "v1".into())]
521
        );
522
        assert_eq!(parse_headers("no colon").unwrap_err(), "no colon");
523
        assert_eq!(parse_headers("bad name: x").unwrap_err(), "bad name: x");
524
    }
525
}
526
527
// ------------------------------------------------------- reviewing
528
529
/// At most this many assistant calls per repository per hour.
530
pub const CALLS_PER_HOUR: usize = 30;
531
532
/// Who is answering what, and recent calls per repository.
533
#[derive(Default)]
534
pub struct Gate {
535
    busy: std::collections::HashSet<(i64, String)>,
536
    recent: std::collections::HashMap<i64, Vec<i64>>,
537
}
538
539
impl Gate {
540
    /// Take the slot for (proposal, assistant), or say why not.
541
    fn take(&mut self, repo_id: i64, proposal_id: i64, name: &str) -> Result<(), String> {
542
        let key = (proposal_id, name.to_lowercase());
543
        if self.busy.contains(&key) {
544
            return Err(format!("{name} is already answering this proposal"));
545
        }
546
        let now = now_ms();
547
        let calls = self.recent.entry(repo_id).or_default();
548
        calls.retain(|t| now - *t < 3_600_000);
549
        if calls.len() >= CALLS_PER_HOUR {
550
            return Err(format!("this repository has asked assistants {CALLS_PER_HOUR} times in the last hour; try later"));
551
        }
552
        calls.push(now);
553
        self.busy.insert(key);
554
        Ok(())
555
    }
556
557
    fn release(&mut self, proposal_id: i64, name: &str) {
558
        self.busy.remove(&(proposal_id, name.to_lowercase()));
559
    }
560
}
561
562
/// What a reviewer is told about a proposal: its title, description,
563
/// changes and diff, cut to `budget` characters.
564
pub fn review_context(
565
    row: &crate::db::repos::RepoRow,
566
    p: &crate::db::proposals::ProposalRow,
567
    loaded: &crate::web::proposals::Loaded,
568
    budget: usize,
569
) -> String {
570
    let mut head =
571
        format!("# Proposal #{}: {}\n\nRepository {}/{} · into {} · by {}\n\n", p.number, p.title, row.owner, row.name, p.target, p.author);
572
    if !p.body.trim().is_empty() {
573
        head.push_str("## Description\n\n");
574
        head.push_str(p.body.trim());
575
        head.push_str("\n\n");
576
    }
577
    context_text(head, &loaded.changes, &loaded.diffs, budget)
578
}
579
580
/// What a drafter is told: the title so far, the changes and the diff.
581
pub fn draft_context(
582
    row: &crate::db::repos::RepoRow,
583
    title: &str,
584
    target: &str,
585
    changes: &[crate::repos::view::ChangeView],
586
    diffs: &[oxy_core::repo::FileDiff],
587
    budget: usize,
588
) -> String {
589
    let head = format!(
590
        "# Proposal: {}\n\nRepository {}/{} · into {}\n\n",
591
        if title.trim().is_empty() { "(untitled)" } else { title.trim() },
592
        row.owner,
593
        row.name,
594
        target
595
    );
596
    context_text(head, changes, diffs, budget)
597
}
598
599
/// `head`, the list of changes, then the diff as `-`/`+` lines per hunk,
600
/// binary files noted, the diff cut so the whole fits `budget`.
601
fn context_text(mut text: String, changes: &[crate::repos::view::ChangeView], diffs: &[oxy_core::repo::FileDiff], budget: usize) -> String {
602
    text.push_str(&format!("## Changes ({})\n\n", changes.len()));
603
    for c in changes {
604
        text.push_str(&format!("- {} {}\n", c.short, c.title));
605
        let body = c.body.trim();
606
        if !body.is_empty() {
607
            for line in body.lines().take(20) {
608
                text.push_str("  ");
609
                text.push_str(line);
610
                text.push('\n');
611
            }
612
        }
613
    }
614
    text.push_str("\n## Diff\n\n");
615
    let head = text.chars().count();
616
    let mut diff = String::new();
617
    for f in diffs {
618
        diff.push_str(&format!("### {} ({})\n", f.path, f.kind));
619
        if let Some(from) = &f.from {
620
            diff.push_str(&format!("renamed from {from}\n"));
621
        }
622
        let binary = f.hunks.iter().any(|h| h.added.iter().chain(h.removed.iter().map(|(_, l)| l)).any(|l| l.contains(&0)));
623
        if binary {
624
            diff.push_str("(binary)\n\n");
625
            continue;
626
        }
627
        diff.push_str("```\n");
628
        for h in &f.hunks {
629
            diff.push_str(&format!("@@ after line {} @@\n", h.base_line));
630
            for (_, l) in &h.removed {
631
                diff.push('-');
632
                diff.push_str(String::from_utf8_lossy(l).trim_end_matches('\n'));
633
                diff.push('\n');
634
            }
635
            for l in &h.added {
636
                diff.push('+');
637
                diff.push_str(String::from_utf8_lossy(l).trim_end_matches('\n'));
638
                diff.push('\n');
639
            }
640
        }
641
        diff.push_str("```\n\n");
642
    }
643
    let room = budget.saturating_sub(head + 80);
644
    if diff.chars().count() > room {
645
        let cut: String = diff.chars().take(room).collect();
646
        text.push_str(&cut);
647
        text.push_str("\n\n(the diff was cut here to fit the context budget)\n");
648
    } else {
649
        text.push_str(&diff);
650
    }
651
    text
652
}
653
654
/// The system prompt for a review: who the assistant is, what a review
655
/// here is, then the prompts the levels set.
656
pub fn review_system_prompt(a: &Assistant, site: &str, prompts: &str) -> String {
657
    let mut s = format!(
658
        "You are {name}, an AI assistant reviewing a proposal on {site}, an OxygenHub site. \
659
         A person asked for your review; it is recorded as a review comment made via you, under their name. \
660
         You cannot approve: only people approve. Answer in Markdown: start with a one-paragraph summary, \
661
         then findings (correctness first, then clarity), each naming the file and quoting the lines it is about; \
662
         keep praise out unless something is notably well done; say so plainly when you find nothing wrong.",
663
        name = a.name
664
    );
665
    if !prompts.trim().is_empty() {
666
        s.push_str("\n\n");
667
        s.push_str(prompts.trim());
668
    }
669
    s
670
}
671
672
/// Ask `a` to review proposal `p` for `by`: records the request, calls the
673
/// endpoint in the background, and stores the answer as a `comment` review
674
/// via the assistant (or an event saying why none came). `Err` is for the
675
/// person who asked, when nothing was started.
676
pub async fn request_review(
677
    app: &Shared,
678
    row: &crate::db::repos::RepoRow,
679
    p: &crate::db::proposals::ProposalRow,
680
    by: &crate::db::accounts::Account,
681
    a: Assistant,
682
) -> Result<(), String> {
683
    app.assistant_gate.lock().unwrap_or_else(|e| e.into_inner()).take(row.id, p.id, &a.name)?;
684
    let pool = &app.db;
685
    crate::db::proposals::add_event(pool, p.id, &by.username, &format!("asked {} to review", a.name), "").await.ok();
686
    crate::db::accounts::audit(
687
        pool,
688
        Some(by),
689
        "assistant.asked",
690
        &format!("{}/{}", row.owner, row.name),
691
        &format!("{} #{}", a.name, p.number),
692
    )
693
    .await
694
    .ok();
695
    let (app, row, p, by) = (app.clone(), row.clone(), p.clone(), by.clone());
696
    tokio::spawn(async move {
697
        let outcome = answer_review(&app, &row, &p, &a).await;
698
        app.assistant_gate.lock().unwrap_or_else(|e| e.into_inner()).release(p.id, &a.name);
699
        match outcome {
700
            Ok(body) => {
701
                if crate::db::proposals::add_review_via(&app.db, p.id, by.id, &by.username, "comment", &body, &a.name).await.is_ok() {
702
                    crate::db::proposals::add_event(&app.db, p.id, &by.username, &format!("review.comment via {}", a.name), "").await.ok();
703
                    crate::web::proposals::reviewed(&app, &row, &p, &by, "comment").await;
704
                    crate::hooks::emit(
705
                        &app,
706
                        &row,
707
                        "review",
708
                        &by.username,
709
                        serde_json::json!({ "number": p.number, "verdict": "comment", "via": a.name }),
710
                    )
711
                    .await;
712
                }
713
            }
714
            Err(why) => {
715
                crate::db::proposals::add_event(&app.db, p.id, &by.username, &format!("{} did not answer", a.name), &why).await.ok();
716
            }
717
        }
718
    });
719
    Ok(())
720
}
721
722
async fn answer_review(
723
    app: &Shared,
724
    row: &crate::db::repos::RepoRow,
725
    p: &crate::db::proposals::ProposalRow,
726
    a: &Assistant,
727
) -> Result<String, String> {
728
    let repo = crate::repos::names::RepoName::new(&row.owner, &row.name).ok_or("bad repository name")?;
729
    let loaded = crate::web::proposals::load(app, &repo, p.clone()).await.map_err(|e| format!("the proposal could not be loaded: {e}"))?;
730
    let budget = usize::try_from(a.budget).unwrap_or(DEFAULT_BUDGET as usize).max(1_000);
731
    let prompts = prompt_for_repo(&app.db, row).await.map_err(|e| e.to_string())?;
732
    let system = review_system_prompt(a, &app.config.site.name, &prompts);
733
    let user = review_context(row, p, &loaded, budget.saturating_sub(system.chars().count()));
734
    ask(app, a, &system, &user).await
735
}
736
737
// -------------------------------------------------------- drafting
738
739
/// The system prompt for drafting a proposal's description.
740
pub fn draft_system_prompt(a: &Assistant, site: &str, prompts: &str) -> String {
741
    let mut s = format!(
742
        "You are {name}, an AI assistant drafting the description of a proposal on {site}, an OxygenHub site, \
743
         for the person who will open or edit it; they read and change your draft before saving, and the draft is theirs. \
744
         Write Markdown for the description field only: no title line, no greeting, no sign-off. \
745
         Say what the changes do and why, what a reviewer should look at, and how to verify them; \
746
         keep to what the diff shows, and keep it short.",
747
        name = a.name
748
    );
749
    if !prompts.trim().is_empty() {
750
        s.push_str("\n\n");
751
        s.push_str(prompts.trim());
752
    }
753
    s
754
}
755
756
/// What a description is drafted from.
757
pub struct Draft<'a> {
758
    /// The proposal's id, or minus the repository's for one not yet opened:
759
    /// one draft per assistant at a time for it.
760
    pub slot: i64,
761
    pub title: &'a str,
762
    pub target: &'a str,
763
    pub changes: &'a [crate::repos::view::ChangeView],
764
    pub diffs: &'a [oxy_core::repo::FileDiff],
765
}
766
767
/// Ask `a` for a description, now; the answer goes into the editor,
768
/// nothing is saved. `Err` is for the person.
769
pub async fn draft_description(
770
    app: &Shared,
771
    row: &crate::db::repos::RepoRow,
772
    by: &crate::db::accounts::Account,
773
    a: &Assistant,
774
    d: Draft<'_>,
775
) -> Result<String, String> {
776
    let Draft { slot, title, target, changes, diffs } = d;
777
    app.assistant_gate.lock().unwrap_or_else(|e| e.into_inner()).take(row.id, slot, &a.name)?;
778
    crate::db::accounts::audit(&app.db, Some(by), "assistant.drafted", &format!("{}/{}", row.owner, row.name), &a.name).await.ok();
779
    let budget = usize::try_from(a.budget).unwrap_or(DEFAULT_BUDGET as usize).max(1_000);
780
    let prompts = prompt_for_repo(&app.db, row).await.map_err(|e| e.to_string());
781
    let outcome = match prompts {
782
        Ok(prompts) => {
783
            let system = draft_system_prompt(a, &app.config.site.name, &prompts);
784
            let user = draft_context(row, title, target, changes, diffs, budget.saturating_sub(system.chars().count()));
785
            ask(app, a, &system, &user).await
786
        }
787
        Err(e) => Err(e),
788
    };
789
    app.assistant_gate.lock().unwrap_or_else(|e| e.into_inner()).release(slot, &a.name);
790
    outcome.map(|t| t.trim().to_string())
791
}