e71846aa Andy McCall 1 month ago ↶
1
2
3
4
5
6
7
8
9
10
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
21
pub const APIS : [ & str ; 2 ] = [ " chat-completions" , " anthropic-messages" ] ;
22
23
24
pub const DEFAULT_BUDGET : i64 = 120_000 ;
25
26
pub const CALL_TIMEOUT : Duration = Duration:: from_secs( 60 ) ;
27
28
const MAX_ANSWER : usize = 64 * 1024 ;
29
30
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
63
pub api : String,
64
pub model : String,
65
66
pub headers : String,
67
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
89
pub fn header_list ( & self ) -> Vec< ( String, String) > {
90
parse_headers ( & self .headers) .unwrap_or_default ( )
91
}
92
}
93
94
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
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
122
123
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 = [ 0 u8 ; 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
152
153
pub fn seal ( & self , plain : & str ) -> Vec< u8 > {
154
if plain.is_empty ( ) {
155
return vec! [ ] ;
156
}
157
let mut nonce = [ 0 u8 ; 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
167
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
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
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
219
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
277
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
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
317
318
319
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
336
337
# [ derive ( Clone, Debug ) ]
338
pub struct Available {
339
pub assistant : Assistant,
340
341
pub from : String,
342
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
353
354
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
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
379
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
392
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
404
405
406
407
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
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
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
}