feat(items): voice dictation with Gemma 4 and a reworked list UI
Dictation: a mic button in the add field uses the Web Speech API (es-AR) and
shows the partial transcript in the input. POST /api/voice/split asks Gemma 4
(Gemini API, plain fetch) to split the sentence into one string per item,
written the way the existing parser reads typed text; each string then goes
through the usual add flow. The endpoint never fails because of the model:
missing key, timeout, 429 or invalid JSON fall back to splitting on commas,
semicolons, " y " and " e ". A toast offers "Deshacer" for the whole dictation;
if the request fails, the transcript is added as a single item.
UI: "+" instead of the search icon, quantity tags ("2 L", "×12", "1 kg"), the
list name with a list switcher and the pending/bought count in the header,
"Comprados (N)" folded by default, and a bottom sheet to edit name, quantity
(− / + and unit), note and delete. PATCH items now accepts a name.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
47
apps/web/src/lib/item-quantity.test.ts
Normal file
47
apps/web/src/lib/item-quantity.test.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { fromEditableQuantity, stepAmount, toEditableQuantity } from './item-quantity'
|
||||
|
||||
describe('toEditableQuantity', () => {
|
||||
it('splits a stored quantity into amount and unit', () => {
|
||||
expect(toEditableQuantity(null)).toEqual({ amount: '1', unit: '' })
|
||||
expect(toEditableQuantity('2')).toEqual({ amount: '2', unit: '' })
|
||||
expect(toEditableQuantity('1,5 l')).toEqual({ amount: '1,5', unit: 'l' })
|
||||
expect(toEditableQuantity('½ kg')).toEqual({ amount: '½', unit: 'kg' })
|
||||
expect(toEditableQuantity('2 paquetes')).toEqual({ amount: '2', unit: 'paquete' })
|
||||
})
|
||||
|
||||
it('keeps free text as the amount', () => {
|
||||
expect(toEditableQuantity('un par')).toEqual({ amount: 'un par', unit: '' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('stepAmount', () => {
|
||||
it('steps by one for counts and by bigger steps for measures', () => {
|
||||
expect(stepAmount('2', '', 1)).toBe('3')
|
||||
expect(stepAmount('2', '', -1)).toBe('1')
|
||||
expect(stepAmount('1', '', -1)).toBe('1')
|
||||
expect(stepAmount('400', 'g', 1)).toBe('500')
|
||||
expect(stepAmount('½', 'kg', 1)).toBe('1')
|
||||
expect(stepAmount('1', 'l', -1)).toBe('½')
|
||||
})
|
||||
|
||||
it('starts from one step when the amount is not a number', () => {
|
||||
expect(stepAmount('un par', '', 1)).toBe('1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('fromEditableQuantity', () => {
|
||||
it('stores the formatted quantity, and nothing for a plain one', () => {
|
||||
expect(fromEditableQuantity({ amount: '1', unit: '' })).toBeNull()
|
||||
expect(fromEditableQuantity({ amount: '', unit: 'kg' })).toBeNull()
|
||||
expect(fromEditableQuantity({ amount: '2', unit: '' })).toBe('2')
|
||||
expect(fromEditableQuantity({ amount: '1', unit: 'kg' })).toBe('1 kg')
|
||||
expect(fromEditableQuantity({ amount: '0.5', unit: 'kg' })).toBe('½ kg')
|
||||
expect(fromEditableQuantity({ amount: '2', unit: 'paquete' })).toBe('2 paquetes')
|
||||
})
|
||||
|
||||
it('keeps free text as typed', () => {
|
||||
expect(fromEditableQuantity({ amount: 'un par', unit: '' })).toBe('un par')
|
||||
})
|
||||
})
|
||||
64
apps/web/src/lib/item-quantity.ts
Normal file
64
apps/web/src/lib/item-quantity.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { formatQuantity, parseNumber, parseQuantity } from './natural-input/quantity'
|
||||
|
||||
/** Units offered by the item editor; `''` is a plain count ("un."). */
|
||||
export const EDITOR_UNITS = [
|
||||
{ value: '', label: 'un.' },
|
||||
{ value: 'g', label: 'g' },
|
||||
{ value: 'kg', label: 'kg' },
|
||||
{ value: 'ml', label: 'ml' },
|
||||
{ value: 'l', label: 'L' },
|
||||
] as const
|
||||
|
||||
/**
|
||||
* A quantity split into what the editor shows: the amount as text and the unit. A unit
|
||||
* the selector does not offer (`paquete`) is kept, so editing the amount does not lose it.
|
||||
* Free text that is not a quantity ("un par") goes into `amount` untouched.
|
||||
*/
|
||||
export type EditableQuantity = { amount: string; unit: string }
|
||||
|
||||
export function toEditableQuantity(quantity: string | null): EditableQuantity {
|
||||
if (!quantity?.trim()) return { amount: '1', unit: '' }
|
||||
|
||||
const parsed = parseQuantity(quantity)
|
||||
if (!parsed) return { amount: quantity.trim(), unit: '' }
|
||||
|
||||
return { amount: formatQuantity({ amount: parsed.amount, unit: null }), unit: parsed.unit ?? '' }
|
||||
}
|
||||
|
||||
/** The numeric value of the amount text, or `null` when it is free text. */
|
||||
export function amountValue(amount: string): number | null {
|
||||
return parseNumber(amount.trim())
|
||||
}
|
||||
|
||||
/** How much the − / + buttons change the amount for each unit. */
|
||||
export function amountStep(unit: string): number {
|
||||
if (unit === 'g' || unit === 'ml') return 100
|
||||
if (unit === 'kg' || unit === 'l') return 0.5
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
/** The amount after pressing − (`direction` −1) or + (1); never below one step. */
|
||||
export function stepAmount(amount: string, unit: string, direction: 1 | -1): string {
|
||||
const value = amountValue(amount) ?? 0
|
||||
const step = amountStep(unit)
|
||||
const next = Math.max(step, Math.round((value + direction * step) / step) * step)
|
||||
|
||||
return formatQuantity({ amount: next, unit: null })
|
||||
}
|
||||
|
||||
/**
|
||||
* The quantity to store for what the editor shows: `null` for a plain "1" (the same as no
|
||||
* quantity), the formatted quantity when the amount is a number ("2 paquetes", "½ kg") and
|
||||
* the text as typed otherwise.
|
||||
*/
|
||||
export function fromEditableQuantity({ amount, unit }: EditableQuantity): string | null {
|
||||
const text = amount.trim()
|
||||
if (!text) return null
|
||||
|
||||
const value = amountValue(text)
|
||||
if (value === null) return unit ? `${text} ${unit}` : text
|
||||
if (value === 1 && !unit) return null
|
||||
|
||||
return formatQuantity({ amount: value, unit: unit || null })
|
||||
}
|
||||
@@ -87,24 +87,36 @@ describe('splitItems', () => {
|
||||
})
|
||||
|
||||
describe('quantityLabel', () => {
|
||||
it('shows numeric quantities, with their unit when present', () => {
|
||||
expect(quantityLabel('2')).toBe('2')
|
||||
expect(quantityLabel('2 kg')).toBe('2 kg')
|
||||
expect(quantityLabel('1,5')).toBe('1,5')
|
||||
expect(quantityLabel(' 3 ')).toBe('3')
|
||||
it('shows quantities with a unit, litres in capitals', () => {
|
||||
expect(quantityLabel('2 l')).toBe('2 L')
|
||||
expect(quantityLabel('1,5 l')).toBe('1,5 L')
|
||||
expect(quantityLabel('1 kg')).toBe('1 kg')
|
||||
expect(quantityLabel('0,5')).toBe('0,5')
|
||||
expect(quantityLabel('½ kg')).toBe('½ kg')
|
||||
expect(quantityLabel('400 g')).toBe('400 g')
|
||||
expect(quantityLabel('1 lata')).toBe('1 lata')
|
||||
expect(quantityLabel('2 paquetes')).toBe('2 paquetes')
|
||||
})
|
||||
|
||||
it('hides empty, absent and plain-one quantities', () => {
|
||||
it('shows counts above one with a "×"', () => {
|
||||
expect(quantityLabel('2')).toBe('×2')
|
||||
expect(quantityLabel(' 12 ')).toBe('×12')
|
||||
expect(quantityLabel('1,5')).toBe('×1,5')
|
||||
})
|
||||
|
||||
it('shows a large bare amount as a weight, without "×"', () => {
|
||||
expect(quantityLabel('400')).toBe('400')
|
||||
})
|
||||
|
||||
it('hides empty quantities and counts up to one', () => {
|
||||
expect(quantityLabel(null)).toBeNull()
|
||||
expect(quantityLabel('')).toBeNull()
|
||||
expect(quantityLabel('1')).toBeNull()
|
||||
expect(quantityLabel(' 1 ')).toBeNull()
|
||||
expect(quantityLabel('0,5')).toBeNull()
|
||||
})
|
||||
|
||||
it('hides quantities whose text does not start with a number', () => {
|
||||
it('shows other free text only when it starts with a number', () => {
|
||||
expect(quantityLabel('2 + 200 g')).toBe('2 + 200 g')
|
||||
expect(quantityLabel('un par')).toBeNull()
|
||||
expect(quantityLabel('kg')).toBeNull()
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { ListItem } from '@superlist/shared'
|
||||
|
||||
import { isSameLocalDay } from './dates'
|
||||
import { formatQuantity, parseQuantity } from './natural-input/quantity'
|
||||
|
||||
export type SplitItems = {
|
||||
/** Items still to buy, in the API's order (by `order`). */
|
||||
@@ -39,17 +40,32 @@ export function splitItems(items: ListItem[], now: Date): SplitItems {
|
||||
return { pending, boughtToday }
|
||||
}
|
||||
|
||||
/** Bare amounts from here up are weights ("400 de jamón"), not counts: no "×". */
|
||||
const BARE_WEIGHT_FROM = 50
|
||||
|
||||
/**
|
||||
* The quantity to show at the right of an item's name, or `null` when it must
|
||||
* stay hidden. A plain "1" adds no information, so it is hidden, as is anything
|
||||
* that does not start with a number ("un par"); every other quantity is shown,
|
||||
* including fractions and quantities with a unit ("½ kg", "1 kg", "0,5").
|
||||
* The quantity tag shown at the right of an item's name ("2 L", "×12", "1 kg"), or
|
||||
* `null` when it must stay hidden. It is shown when the quantity has a unit or is a
|
||||
* count above one; a plain "1" (or any count up to one) adds nothing. Free text that
|
||||
* does not start with a number ("un par") stays hidden, other free text ("2 + 200 g")
|
||||
* is shown as typed.
|
||||
*/
|
||||
export function quantityLabel(quantity: string | null): string | null {
|
||||
if (!quantity) return null
|
||||
|
||||
const trimmed = quantity.trim()
|
||||
if (!/^(\d|[½¼¾])/.test(trimmed)) return null
|
||||
const parsed = parseQuantity(trimmed)
|
||||
|
||||
return trimmed === '1' ? null : trimmed
|
||||
if (parsed) {
|
||||
if (parsed.unit) {
|
||||
const label = formatQuantity(parsed)
|
||||
return parsed.unit === 'l' ? label.replace(/ l$/, ' L') : label
|
||||
}
|
||||
if (parsed.amount <= 1) return null
|
||||
|
||||
const amount = formatQuantity(parsed)
|
||||
return parsed.amount >= BARE_WEIGHT_FROM ? amount : `×${amount}`
|
||||
}
|
||||
|
||||
return /^(\d|[½¼¾])/.test(trimmed) ? trimmed : null
|
||||
}
|
||||
|
||||
53
apps/web/src/lib/natural-input/voice-examples.test.ts
Normal file
53
apps/web/src/lib/natural-input/voice-examples.test.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { VOICE_SPLIT_EXAMPLES } from '@superlist/shared'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { parseItemInput } from './parse-item'
|
||||
import { parseQuantity } from './quantity'
|
||||
|
||||
/**
|
||||
* The dictation examples the API shows the model (`@superlist/shared` → voice.ts) must be
|
||||
* written exactly the way this parser reads them: each string is one item with the
|
||||
* expected quantity, unit and note.
|
||||
*/
|
||||
const EXPECTED: Record<
|
||||
string,
|
||||
{ name: string; amount: number | null; unit: string | null; notes: string | null }
|
||||
> = {
|
||||
'2 pascualinas': { name: 'Pascualinas', amount: 2, unit: null, notes: null },
|
||||
'1,5 kg de papas': { name: 'Papas', amount: 1.5, unit: 'kg', notes: null },
|
||||
'400 de jamon. Feteado fino': { name: 'Jamón', amount: 400, unit: null, notes: 'Feteado fino' },
|
||||
'1 lata de atun': { name: 'Atún', amount: 1, unit: 'lata', notes: null },
|
||||
'Queso crema. Solo si hay marca Tregar': {
|
||||
name: 'Queso crema',
|
||||
amount: null,
|
||||
unit: null,
|
||||
notes: 'Solo si hay marca Tregar',
|
||||
},
|
||||
'6 unidades de yogur': { name: 'Yogur', amount: 6, unit: null, notes: null },
|
||||
'400g de queso': { name: 'Queso', amount: 400, unit: 'g', notes: null },
|
||||
'Leche (descremada)': { name: 'Leche', amount: null, unit: null, notes: 'Descremada' },
|
||||
}
|
||||
|
||||
describe('dictation few-shot examples', () => {
|
||||
const items = VOICE_SPLIT_EXAMPLES.flatMap((example) => example.items)
|
||||
|
||||
it('has an expectation for every example item', () => {
|
||||
expect([...items].sort()).toEqual(Object.keys(EXPECTED).sort())
|
||||
})
|
||||
|
||||
it.each(items)('parses "%s" as one item with the expected quantity and note', (text) => {
|
||||
const parsed = parseItemInput(text)
|
||||
expect(parsed).toHaveLength(1)
|
||||
|
||||
const [item] = parsed
|
||||
const quantity = item.quantity ? parseQuantity(item.quantity) : null
|
||||
const expected = EXPECTED[text]
|
||||
|
||||
expect({
|
||||
name: item.name,
|
||||
amount: quantity?.amount ?? null,
|
||||
unit: quantity?.unit ?? null,
|
||||
notes: item.notes,
|
||||
}).toEqual(expected)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user