Coach auf dem Handy: ein lokaler Endpunkt bekommt 25 Minuten, und der native Transport schneidet nie zuerst ab

This commit is contained in:
Duarte Santos committed 2026-08-29 21:26:54 +02:00
1 parent 0d38bc07f0
commit b61f1b6f15
3 files changed
+24 -2

No files matched your search

+4 -1
View File
@@ -22,7 +22,10 @@ export async function nativeFetch(url, init = {}) {
const request = cap.CapacitorHttp.request({
url, method: init.method || 'GET', headers, data,
responseType: 'text', connectTimeout: 30000, readTimeout: 6 * 60000
// The read timeout is not the job timeout: that is the AbortSignal's job (see abortOf),
// and coach-local.js derives it from the provider. This only has to be longer than any
// job may legitimately run, so a local model on a laptop is never cut off by the transport.
responseType: 'text', connectTimeout: 30000, readTimeout: 40 * 60000
})
const res = init.signal ? await Promise.race([request, abortOf(init.signal)]) : await request
const text = typeof res.data === 'string' ? res.data : res.data == null ? '' : JSON.stringify(res.data)
+5 -1
View File
@@ -31,7 +31,11 @@ import { t } from './i18n.js'
export const ADAPTERS = { anthropic, openai, gemini, compatible }
export const LOCAL_DAILY_CAP = 10
// Five minutes is right for a cloud API and wrong for a model on somebody's laptop; the
// OpenAI-compatible endpoint is the one that may be local, so it gets the long budget.
export const TIMEOUT_MS = 5 * 60000
export const LOCAL_ENDPOINT_TIMEOUT_MS = 25 * 60000
export const timeoutFor = provider => (provider === 'compatible' ? LOCAL_ENDPOINT_TIMEOUT_MS : TIMEOUT_MS)
const PENDING_DAYS = 14
const HANDLE_LENGTH = 16
@@ -141,7 +145,7 @@ async function run(S, kind, opts, d, adapter) {
})
const attempt = await runPipeline({
adapter, cfg: cfgOf(d), kind, payload,
model: d.model || HTTP_PROVIDERS[d.provider].defaultModel, timeoutMs: TIMEOUT_MS,
model: d.model || HTTP_PROVIDERS[d.provider].defaultModel, timeoutMs: timeoutFor(d.provider),
invokeOpts: { env: envOf(d, key), fetch: nativeFetch }
})
if (!attempt.ok) {
+15
View File
@@ -179,3 +179,18 @@ describe('the Coach on a phone with its own key', () => {
expect(d.categories).toEqual(['plan', 'training', 'bodyweight', 'profile', 'prefs'])
})
})
describe('timeouts on the phone', () => {
it('gives a local endpoint the long budget and cloud providers the short one', async () => {
const { timeoutFor, TIMEOUT_MS, LOCAL_ENDPOINT_TIMEOUT_MS } = await import('./coach-local.js')
expect(timeoutFor('compatible')).toBe(LOCAL_ENDPOINT_TIMEOUT_MS)
expect(LOCAL_ENDPOINT_TIMEOUT_MS).toBeGreaterThanOrEqual(20 * 60000)
for (const p of ['anthropic', 'openai', 'gemini']) expect(timeoutFor(p)).toBe(TIMEOUT_MS)
})
it('the native transport read timeout outlasts the longest job', async () => {
const src = (await import('node:fs')).readFileSync(new URL('./capacitor-fetch.js', import.meta.url), 'utf8')
const m = src.match(/readTimeout:\s*(\d+)\s*\*\s*60000/)
expect(m).toBeTruthy()
expect(+m[1] * 60000).toBeGreaterThan(25 * 60000)
})
})