fix(mobile-templates): discard the generation idempotency key once the server gives a final answer
This commit is contained in:
parent
65320288d5
commit
7a8e1e3e25
5 changed files with 393 additions and 19 deletions
|
|
@ -9,6 +9,14 @@ import type {
|
|||
} from '@d3ro/api-client'
|
||||
import { SUPABASE_URL } from '@d3ro/core/supabase-config'
|
||||
import { supabase } from '../../lib/supabase'
|
||||
import {
|
||||
asyncStorageGenerationKeyStore,
|
||||
type GenerationKeyStore,
|
||||
} from './generation-idempotency'
|
||||
import {
|
||||
generationKeyDisposition,
|
||||
type GenerationAttemptOutcome,
|
||||
} from './generation-key-policy'
|
||||
import type {
|
||||
GenerateMeetingDocumentOptions,
|
||||
GeneratedMeetingDocument,
|
||||
|
|
@ -29,11 +37,18 @@ export type TemplateServiceErrorCode =
|
|||
| 'server'
|
||||
| 'validation'
|
||||
|
||||
/** The HTTP answer the server gave, when an error came from a server response. */
|
||||
export interface TemplateServiceResponseInfo {
|
||||
status: number
|
||||
errorCode: string | null
|
||||
}
|
||||
|
||||
export class TemplateServiceError extends Error {
|
||||
constructor(
|
||||
public readonly code: TemplateServiceErrorCode,
|
||||
message: string,
|
||||
public readonly retryable = false,
|
||||
public readonly response: TemplateServiceResponseInfo | null = null,
|
||||
) {
|
||||
super(message)
|
||||
this.name = 'TemplateServiceError'
|
||||
|
|
@ -357,14 +372,21 @@ export function renderDictationTemplate(
|
|||
|
||||
function generationError(status: number, body: unknown): TemplateServiceError {
|
||||
const code = isRecord(body) && typeof body.error === 'string' ? body.error : ''
|
||||
if (status === 401) return new TemplateServiceError('auth', code)
|
||||
if (status === 403) return new TemplateServiceError('forbidden', code)
|
||||
if (status === 404) return new TemplateServiceError('not-found', code)
|
||||
if (status === 409) return new TemplateServiceError('in-progress', code, true)
|
||||
if (status === 429) return new TemplateServiceError('quota', code, true)
|
||||
if ([502, 503, 504].includes(status)) return new TemplateServiceError('provider', code, true)
|
||||
if (status === 400) return new TemplateServiceError('validation', code)
|
||||
return new TemplateServiceError('server', code || 'Document generation failed', true)
|
||||
const response: TemplateServiceResponseInfo = { status, errorCode: code || null }
|
||||
if (status === 401) return new TemplateServiceError('auth', code, false, response)
|
||||
if (status === 403) return new TemplateServiceError('forbidden', code, false, response)
|
||||
if (status === 404) return new TemplateServiceError('not-found', code, false, response)
|
||||
if (status === 409 && code === 'generation_failed') {
|
||||
// The previous attempt with this key failed for good; a new attempt can succeed.
|
||||
return new TemplateServiceError('server', code, true, response)
|
||||
}
|
||||
if (status === 409) return new TemplateServiceError('in-progress', code, true, response)
|
||||
if (status === 429) return new TemplateServiceError('quota', code, true, response)
|
||||
if ([502, 503, 504].includes(status)) {
|
||||
return new TemplateServiceError('provider', code, true, response)
|
||||
}
|
||||
if (status === 400) return new TemplateServiceError('validation', code, false, response)
|
||||
return new TemplateServiceError('server', code || 'Document generation failed', true, response)
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
|
|
@ -416,6 +438,71 @@ export async function generateMeetingDocument(
|
|||
}
|
||||
}
|
||||
|
||||
export interface GenerateMeetingDocumentOnceOptions
|
||||
extends Omit<GenerateMeetingDocumentOptions, 'idempotencyKey'> {
|
||||
userId: string
|
||||
}
|
||||
|
||||
export interface GenerateMeetingDocumentOnceDeps {
|
||||
keyStore: GenerationKeyStore
|
||||
generate: (options: GenerateMeetingDocumentOptions) => Promise<GeneratedMeetingDocument>
|
||||
}
|
||||
|
||||
const DEFAULT_GENERATE_ONCE_DEPS: GenerateMeetingDocumentOnceDeps = {
|
||||
keyStore: asyncStorageGenerationKeyStore,
|
||||
generate: generateMeetingDocument,
|
||||
}
|
||||
|
||||
/** Maps a failed attempt to what the client actually learned from the server. */
|
||||
export function generationAttemptOutcome(error: unknown): GenerationAttemptOutcome {
|
||||
if (error instanceof TemplateServiceError && error.response !== null) {
|
||||
return {
|
||||
kind: 'server-response',
|
||||
status: error.response.status,
|
||||
errorCode: error.response.errorCode,
|
||||
}
|
||||
}
|
||||
return { kind: 'no-response' }
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates one meeting document and owns its idempotency key's lifecycle:
|
||||
* the key for (user, meeting, template) is reused across ambiguous retries and
|
||||
* discarded once the server has given a final answer for it, success or not.
|
||||
*/
|
||||
export async function generateMeetingDocumentOnce(
|
||||
options: GenerateMeetingDocumentOnceOptions,
|
||||
deps: GenerateMeetingDocumentOnceDeps = DEFAULT_GENERATE_ONCE_DEPS,
|
||||
): Promise<GeneratedMeetingDocument> {
|
||||
const { userId, ...request } = options
|
||||
let idempotencyKey: string
|
||||
try {
|
||||
idempotencyKey = await deps.keyStore.getOrCreate(userId, request.meetingId, request.templateId)
|
||||
} catch (error) {
|
||||
throw toServiceError(error)
|
||||
}
|
||||
|
||||
let outcome: GenerationAttemptOutcome
|
||||
let generated: GeneratedMeetingDocument | null = null
|
||||
let failure: unknown = null
|
||||
try {
|
||||
generated = await deps.generate({ ...request, idempotencyKey })
|
||||
outcome = { kind: 'succeeded' }
|
||||
} catch (error) {
|
||||
failure = error
|
||||
outcome = generationAttemptOutcome(error)
|
||||
}
|
||||
|
||||
if (generationKeyDisposition(outcome) === 'discard') {
|
||||
// A failed cleanup only costs one extra replay of the final answer.
|
||||
await deps.keyStore
|
||||
.clear(userId, request.meetingId, request.templateId)
|
||||
.catch(() => undefined)
|
||||
}
|
||||
if (generated === null) throw toServiceError(failure)
|
||||
return generated
|
||||
}
|
||||
|
||||
export function subscribeToTemplates(
|
||||
userId: string,
|
||||
onRemoteChange: () => void,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue