Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : SP Wallet8 * File : services/research-runner/src/api-client.ts9 * Module : services10 * Repository : https://git.spboucher.ai/sp-wallet11 * Project Website : https://wallet.spboucher.ai12 * Created : 2026-11-1113 * Last Modified : 2026-11-1114 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243// The runner's side of the narrow endpoint pair of the wallet API. The runner PULLS a job and PUSHES log lines and one44// result; it holds one shared secret that opens these three routes and nothing else, plus — per job — the token the API45// handed out with that job. It has no owner session, no cookie and no access to any other route.46import { RunnerJob, type RunnerResult } from '@spw/core/research-job';4748export interface LogLine { s: 'out' | 'err' | 'sys'; t: string }49export interface RunnerApi {50 claim(): Promise<RunnerJob | null>;51 log(job: RunnerJob, lines: LogLine[], state?: 'running'): Promise<{ cancel: boolean }>;52 result(job: RunnerJob, result: RunnerResult): Promise<void>;53}5455export function httpApi(o: { apiUrl: string; secret: string; runnerId: string; fetch?: typeof fetch }): RunnerApi {56 const f = o.fetch ?? fetch, base = `${o.apiUrl}/api/v1/research/runner`;57 const post = async (path: string, body: unknown, token?: string): Promise<{ status: number; json: any }> => {58 const r = await f(`${base}${path}`, { method: 'POST', redirect: 'error', signal: AbortSignal.timeout(120_000),59 headers: { 'content-type': 'application/json', authorization: `Bearer ${o.secret}`, ...(token ? { 'x-spw-job-token': token } : {}) }, body: JSON.stringify(body) });60 let json: unknown = null; try { json = await r.json(); } catch { /* empty */ }61 return { status: r.status, json };62 };63 return {64 async claim() {65 const r = await post('/claim', { runnerId: o.runnerId });66 if (r.status === 204 || (r.status === 200 && !r.json?.job)) return null;67 if (r.status !== 200) throw new Error(`claim refused: HTTP ${r.status} ${String(r.json?.error ?? '')}`);68 return RunnerJob.parse(r.json.job);69 },70 async log(job, lines, state) {71 const r = await post(`/jobs/${job.id}/log`, { lines, ...(state ? { state } : {}) }, job.token);72 if (r.status !== 200) throw new Error(`log refused: HTTP ${r.status}`);73 return { cancel: r.json?.cancel === true };74 },75 async result(job, result) {76 const r = await post(`/jobs/${job.id}/result`, result, job.token);77 if (r.status !== 200 && r.status !== 202) throw new Error(`result refused: HTTP ${r.status} ${String(r.json?.message ?? '').slice(0, 200)}`);78 },79 };80}81