feat: ship Wave 5-6 multi-surface hygiene, optimization, and bug fixes
This commit is contained in:
parent
01ef3293cf
commit
66b6b45ba9
399 changed files with 17828 additions and 11330 deletions
|
|
@ -1,12 +1,11 @@
|
|||
import { isUuid } from '@d3ro/core/guards'
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage'
|
||||
import { createUuidV4 } from '../../lib/random-id'
|
||||
import { createUuidAssert } from '../shared/service-guards'
|
||||
|
||||
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
||||
const PREFIX = 'd3ro:meeting-document-generation:v1:'
|
||||
|
||||
function requireUuid(value: string): void {
|
||||
if (!UUID_PATTERN.test(value)) throw new Error('invalid_generation_identity')
|
||||
}
|
||||
const requireUuid = createUuidAssert(() => new Error('invalid_generation_identity'))
|
||||
|
||||
function storageKey(userId: string, meetingId: string, templateId: string): string {
|
||||
requireUuid(userId)
|
||||
|
|
@ -22,7 +21,7 @@ export async function getOrCreateGenerationIdempotencyKey(
|
|||
): Promise<string> {
|
||||
const key = storageKey(userId, meetingId, templateId)
|
||||
const existing = await AsyncStorage.getItem(key)
|
||||
if (existing !== null && UUID_PATTERN.test(existing)) return existing
|
||||
if (isUuid(existing)) return existing
|
||||
if (existing !== null) await AsyncStorage.removeItem(key)
|
||||
const created = createUuidV4()
|
||||
await AsyncStorage.setItem(key, created)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import type { RealtimeChannel, SupabaseClient } from '@supabase/supabase-js'
|
||||
import { isRecord, isUuid } from '@d3ro/core/guards'
|
||||
import type { RealtimeChannel } from '@supabase/supabase-js'
|
||||
import type {
|
||||
Database,
|
||||
DictationTemplateField,
|
||||
MeetingDocument,
|
||||
TemplateKind,
|
||||
|
|
@ -9,6 +9,13 @@ import type {
|
|||
} from '@d3ro/api-client'
|
||||
import { SUPABASE_URL } from '@d3ro/core/supabase-config'
|
||||
import { supabase } from '../../lib/supabase'
|
||||
import {
|
||||
createLabeledUuidAssert,
|
||||
extractSupabaseErrorInfo,
|
||||
getTypedSupabaseClient,
|
||||
isSupabaseAuthFailure,
|
||||
isSupabaseNetworkFailure,
|
||||
} from '../shared/service-guards'
|
||||
import {
|
||||
asyncStorageGenerationKeyStore,
|
||||
type GenerationKeyStore,
|
||||
|
|
@ -59,20 +66,13 @@ export interface TemplateSubscription {
|
|||
unsubscribe: () => Promise<void>
|
||||
}
|
||||
|
||||
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
||||
const FIELD_ID_PATTERN = /^[A-Za-z][A-Za-z0-9_-]*$/
|
||||
const TEMPLATE_COLUMNS = 'id,user_id,template_kind,builtin_key,name,description,fields,output_format,template_type,system_prompt,is_builtin,revision,created_at,updated_at'
|
||||
const SELECTION_COLUMNS = 'user_id,template_kind,template_id,revision,updated_at'
|
||||
|
||||
function db(): SupabaseClient<Database> {
|
||||
return supabase as unknown as SupabaseClient<Database>
|
||||
}
|
||||
|
||||
function requireUuid(value: string, label: string): void {
|
||||
if (!UUID_PATTERN.test(value)) {
|
||||
throw new TemplateServiceError('auth', `A valid ${label} is required`)
|
||||
}
|
||||
}
|
||||
const requireUuid = createLabeledUuidAssert(
|
||||
(label) => new TemplateServiceError('auth', `A valid ${label} is required`),
|
||||
)
|
||||
|
||||
function normalizeSingleLine(value: string): string {
|
||||
return value.trim().replace(/\s+/g, ' ')
|
||||
|
|
@ -160,38 +160,29 @@ export function normalizeTemplateDraft(
|
|||
|
||||
function toServiceError(error: unknown): TemplateServiceError {
|
||||
if (error instanceof TemplateServiceError) return error
|
||||
const candidate = error as { code?: unknown; message?: unknown }
|
||||
const code = typeof candidate?.code === 'string' ? candidate.code : ''
|
||||
const message = typeof candidate?.message === 'string'
|
||||
? candidate.message
|
||||
: 'Template request failed'
|
||||
const lower = message.toLowerCase()
|
||||
if (
|
||||
error instanceof TypeError
|
||||
|| lower.includes('network request failed')
|
||||
|| lower.includes('failed to fetch')
|
||||
|| lower.includes('networkerror')
|
||||
|| lower.includes('abort')
|
||||
) return new TemplateServiceError('network', message, true)
|
||||
if (code === 'PT409' || lower.includes('revision_conflict')) {
|
||||
return new TemplateServiceError('conflict', message, true)
|
||||
const info = extractSupabaseErrorInfo(error, 'Template request failed')
|
||||
if (isSupabaseNetworkFailure(error, info) || info.lower.includes('abort')) {
|
||||
return new TemplateServiceError('network', info.message, true)
|
||||
}
|
||||
if (lower.includes('builtin_template_immutable')) {
|
||||
return new TemplateServiceError('builtin', message)
|
||||
if (info.code === 'PT409' || info.lower.includes('revision_conflict')) {
|
||||
return new TemplateServiceError('conflict', info.message, true)
|
||||
}
|
||||
if (code === '42501') return new TemplateServiceError('forbidden', message)
|
||||
if (code === 'P0002') return new TemplateServiceError('not-found', message)
|
||||
if (code === 'PGRST301' || lower.includes('jwt')) {
|
||||
return new TemplateServiceError('auth', message)
|
||||
if (info.lower.includes('builtin_template_immutable')) {
|
||||
return new TemplateServiceError('builtin', info.message)
|
||||
}
|
||||
if (code.startsWith('22')) return new TemplateServiceError('validation', message)
|
||||
return new TemplateServiceError('server', message, true)
|
||||
if (info.code === '42501') return new TemplateServiceError('forbidden', info.message)
|
||||
if (info.code === 'P0002') return new TemplateServiceError('not-found', info.message)
|
||||
if (isSupabaseAuthFailure(info)) {
|
||||
return new TemplateServiceError('auth', info.message)
|
||||
}
|
||||
if (info.code.startsWith('22')) return new TemplateServiceError('validation', info.message)
|
||||
return new TemplateServiceError('server', info.message, true)
|
||||
}
|
||||
|
||||
function assertTemplate(value: UserTemplate | null, userId: string): UserTemplate {
|
||||
if (
|
||||
value === null
|
||||
|| !UUID_PATTERN.test(value.id)
|
||||
|| !isUuid(value.id)
|
||||
|| value.user_id !== userId
|
||||
|| !['dictation', 'meeting_document'].includes(value.template_kind)
|
||||
|| !Number.isSafeInteger(value.revision)
|
||||
|
|
@ -216,7 +207,7 @@ export async function loadTemplateLibrary(
|
|||
const search = sanitizeTemplateSearch(searchValue)
|
||||
try {
|
||||
await rpc<UserTemplate[]>('bootstrap_user_templates_v1')
|
||||
let templateQuery = db()
|
||||
let templateQuery = getTypedSupabaseClient()
|
||||
.from('user_templates')
|
||||
.select(TEMPLATE_COLUMNS)
|
||||
.eq('user_id', userId)
|
||||
|
|
@ -230,7 +221,7 @@ export async function loadTemplateLibrary(
|
|||
|
||||
const [templateResult, selectionResult] = await Promise.all([
|
||||
templateQuery,
|
||||
db()
|
||||
getTypedSupabaseClient()
|
||||
.from('user_template_selections')
|
||||
.select(SELECTION_COLUMNS)
|
||||
.eq('user_id', userId),
|
||||
|
|
@ -242,7 +233,7 @@ export async function loadTemplateLibrary(
|
|||
.map((row) => assertTemplate(row as UserTemplate, userId))
|
||||
const selections: TemplateLibrary['selections'] = {}
|
||||
for (const selection of selectionResult.data ?? []) {
|
||||
if (selection.user_id !== userId || !UUID_PATTERN.test(selection.template_id)) {
|
||||
if (selection.user_id !== userId || !isUuid(selection.template_id)) {
|
||||
throw new TemplateServiceError('server', 'Server returned an invalid template selection')
|
||||
}
|
||||
selections[selection.template_kind] = selection
|
||||
|
|
@ -389,9 +380,6 @@ function generationError(status: number, body: unknown): TemplateServiceError {
|
|||
return new TemplateServiceError('server', code || 'Document generation failed', true, response)
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
||||
}
|
||||
|
||||
export async function generateMeetingDocument(
|
||||
options: GenerateMeetingDocumentOptions,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue