hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! Payload contract for the `rag.chat` capability. |
| 2 | //! |
| 3 | //! Same transport as every other capability: this is JSON riding inside a |
| 4 | //! [`Request::payload`](crate::ai::protocol::Request::payload) and coming back |
| 5 | //! inside [`Response::Ok`](crate::ai::protocol::Response::Ok)`::payload`. One |
| 6 | //! request, one answer — retrieval-then-answer is unary here even though it is |
| 7 | //! two steps inside the service. |
| 8 | //! |
| 9 | //! The service answers *about* a school's course-note corpus, so the request |
| 10 | //! carries both who is asking (so the service can read that person's own data |
| 11 | //! back through the bridge's api reads, as `on_behalf_of`) and the |
| 12 | //! `(class, course)` scope the corpus is routed by. The reply returns the answer |
| 13 | //! text plus the citations behind it, so the backend can resolve each cited |
| 14 | //! passage to the course-note file it came from. |
| 15 | |
| 16 | use ; |
| 17 | |
| 18 | use crateChatRole; |
| 19 | use crate |
| 20 | |
| 21 | ; |
| 22 | |
| 23 | /// One `(class, course)` pair the question is scoped to. |
| 24 | /// |
| 25 | /// The corpus is routed by the **pair**: a grade and a subject list sent |
| 26 | /// separately would cross-product into combinations the asker never named, so |
| 27 | /// each pair is one scope the service may retrieve from. The class is optional — |
| 28 | /// a question about a subject across every grade names the course alone — and |
| 29 | /// the course is required, since a subject-less scope retrieves nothing. |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | /// One previous message in the RAG conversation. |
| 42 | /// |
| 43 | /// The same `{role, content}` shape the chatbot's history uses — a model's |
| 44 | /// `messages` array takes it with no remapping — so the two histories are |
| 45 | /// interchangeable. |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | /// What the backend asks a RAG service to answer. |
| 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 | /// One passage the answer drew on, inside the reply's `citations`. |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | /// The service's answer, inside `Response::Ok { payload }`. |
| 106 | /// |
| 107 | /// Handled failures do *not* come back here — they use the existing |
| 108 | /// [`Response::Err`](crate::ai::protocol::Response::Err) `{ code, message }`, |
| 109 | /// so there is exactly one failure shape on the bridge for every capability. |
| 110 | /// A refusal that is part of the answer (the service chose not to answer, or |
| 111 | /// could not) rides here instead, because it is still an answer: the turn |
| 112 | /// completes, no error surfaces. |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | /// Ask a RAG service one question and resolve what came back. |
| 134 | /// |
| 135 | /// The request is built from what the session already proves — `asker` and |
| 136 | /// `asker_role` are read from the live session by the caller, never taken from |
| 137 | /// a body, and `scope` from [`crate::service::rag_scope`] — and the reply is |
| 138 | /// mapped into what a message row can hold: the answer text, the abstention, |
| 139 | /// and the citations whose corpus `doc_id` resolves to a course-note file the |
| 140 | /// asker may view. |
| 141 | /// |
| 142 | /// `Err` is a short, stable code for the turn's `error_code` — a transport |
| 143 | /// failure, the service's own `Response::Err` verdict, or `bad_reply` when the |
| 144 | /// reply was unreadable or exceeded a storage cap. Two codes are special: |
| 145 | /// `role_required` and `scope_mismatch` mean the **backend** built a request |
| 146 | /// the service may not answer. The turn still fails — an answer is not safe to |
| 147 | /// invent — but this side of the bridge is the bug, so it is logged as one. |
| 148 | // the whole turn context, spelled once |
| 149 | pub async |
| 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 | /// Resolve each citation's corpus `doc_id` to the course-note file that owns |
| 250 | /// it, keeping only a file whose course the asker may view. |
| 251 | /// |
| 252 | /// One read per *distinct* `doc_id`: identical PDF bytes resolve to the same |
| 253 | /// document, so one document may be claimed by several files — across courses, |
| 254 | /// which is exactly why the visibility check is per candidate and not per |
| 255 | /// document. The first visible candidate wins, in the store's own order, so two |
| 256 | /// identical turns resolve a citation the same way. A citation whose document |
| 257 | /// no visible file claims keeps every other field and stores `file: null` — |
| 258 | /// the passage stays citable, only not openable. |
| 259 | async |
| 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 | /// The first candidate file whose owning course `user` may view, in the |
| 318 | /// store's order; `None` when the document is claimed by no file they can |
| 319 | /// open. A read that fails mid-walk is treated like a file they cannot see — |
| 320 | /// the citation still lands, only unresolved. |
| 321 | async |
| 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 | /// A dispatch failure as a short, stable code the frontend can branch on. The |
| 350 | /// same mapping the chatbot nest applies to its own round trip. |
| 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 |