feat: export reproducible retrieval history snapshots
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import { apiDownload } from './session'
|
||||
import type { RetrievalRunResult } from '../api'
|
||||
import type { RagContext } from './rag'
|
||||
|
||||
export type ExportSelection = { run_ids: string[]; include_contexts?: boolean }
|
||||
export type ExportManifest = {
|
||||
record_type: 'manifest'; schema_version: 'nalati-retrieval-export-v1'; export_id: string; exported_at: string;
|
||||
selection: Required<ExportSelection>; counts: { retrieval_runs: number; rag_contexts: number }; records_sha256: string;
|
||||
snapshot_semantics: 'stored_history_not_current_corpus';
|
||||
generation_measurements: { status: 'not_called'; usage: null; cost: null; currency: null }
|
||||
}
|
||||
export type ExportRecord = ExportManifest
|
||||
| { record_type: 'retrieval_run'; id: string; ranking_unit: 'knowledge_item' | 'chunk'; payload: RetrievalRunResult }
|
||||
| { record_type: 'rag_context'; id: string; payload: RagContext }
|
||||
|
||||
// Returns bytes/metadata; callers explicitly choose whether and where to save the file.
|
||||
export const exportRetrievalHistory = (payload: ExportSelection) => apiDownload(
|
||||
'/api/retrieval/exports', { method: 'POST', body: JSON.stringify(payload) }, true,
|
||||
)
|
||||
@@ -6,10 +6,10 @@ export class ApiError extends Error {
|
||||
|
||||
export function clearSessionToken() { csrfToken = null }
|
||||
|
||||
export async function apiRequest<T>(path: string, init: RequestInit = {}, protect = false): Promise<T> {
|
||||
async function request(path: string, init: RequestInit, protect: boolean, accept: string): Promise<Response> {
|
||||
if (protect && !csrfToken) await apiRequest('/api/auth/me')
|
||||
const headers = new Headers(init.headers)
|
||||
headers.set('Accept', 'application/json')
|
||||
headers.set('Accept', accept)
|
||||
if (init.body) headers.set('Content-Type', 'application/json')
|
||||
if (protect && csrfToken) headers.set('X-CSRF-Token', csrfToken)
|
||||
const response = await fetch(path, { ...init, credentials: 'include', headers })
|
||||
@@ -19,6 +19,11 @@ export async function apiRequest<T>(path: string, init: RequestInit = {}, protec
|
||||
const detail = body && typeof body === 'object' && 'detail' in body ? body.detail : null
|
||||
throw new ApiError(response.status, typeof detail === 'string' ? detail : `请求失败(${response.status}),请检查登录状态或填写内容。`)
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
export async function apiRequest<T>(path: string, init: RequestInit = {}, protect = false): Promise<T> {
|
||||
const response = await request(path, init, protect, 'application/json')
|
||||
if (response.status === 204) return undefined as T
|
||||
const data = await response.json()
|
||||
if (path === '/api/auth/login' || path === '/api/auth/me') {
|
||||
@@ -26,3 +31,12 @@ export async function apiRequest<T>(path: string, init: RequestInit = {}, protec
|
||||
}
|
||||
return data as T
|
||||
}
|
||||
|
||||
export type ApiDownload = { blob: Blob; filename: string; recordsSha256: string | null }
|
||||
|
||||
export async function apiDownload(path: string, init: RequestInit = {}, protect = false): Promise<ApiDownload> {
|
||||
const response = await request(path, init, protect, 'application/x-ndjson')
|
||||
const disposition = response.headers.get('Content-Disposition') ?? ''
|
||||
const filename = /^attachment; filename="([A-Za-z0-9._-]+)"$/.exec(disposition)?.[1] ?? 'download.jsonl'
|
||||
return { blob: await response.blob(), filename, recordsSha256: response.headers.get('X-Records-SHA256') }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user