diff --git a/packages/backend/src/account/routes.ts b/packages/backend/src/account/routes.ts index f08f4ab..bfd7ec0 100644 --- a/packages/backend/src/account/routes.ts +++ b/packages/backend/src/account/routes.ts @@ -30,7 +30,7 @@ accountRoutes.get('/me', requireAccount, async (c) => const PER_PURCHASE = { min: 1_000, max: 50_000 } const WEEKLY = { min: 1_000, max: 200_000 } -const SPEECH_RATES = new Set([0.75, 1, 1.25]) +const SPEECH_RATES = new Set([0.75, 1, 1.25, 1.5, 2]) // PATCH /api/me Settings. See UpdateAccountRequest. accountRoutes.patch('/me', requireAccount, async (c) => { diff --git a/packages/backend/src/agent/links.test.ts b/packages/backend/src/agent/links.test.ts new file mode 100644 index 0000000..00e3aaa --- /dev/null +++ b/packages/backend/src/agent/links.test.ts @@ -0,0 +1,21 @@ +import { describe, expect, it } from 'vitest' +import { wordsFromLink } from './tools' + +describe('wordsFromLink', () => { + it('takes the words of a store address', () => { + expect(wordsFromLink('https://www.walmart.com/ip/Stainless-Steel-Whistling-Tea-Kettle/123456789')).toBe('stainless steel whistling tea kettle') + }) + it('drops ids, codes and path labels', () => { + expect(wordsFromLink('https://www.target.com/p/enamel-stovetop-kettle/-/A-12345678?ref=x')).toBe('enamel stovetop kettle') + }) + it('gives nothing when the address says nothing', () => { + expect(wordsFromLink('https://www.amazon.com/dp/B07CQ6YWL1')).toBeNull() + expect(wordsFromLink('not a link')).toBeNull() + }) +}) + +describe('wordsFromLink with a broken address', () => { + it('does not throw on a bad percent sequence', () => { + expect(wordsFromLink('https://store.com/ip/bad%zz-whistling-kettle/1')).toBe('bad whistling kettle') + }) +}) diff --git a/packages/backend/src/agent/tools.ts b/packages/backend/src/agent/tools.ts index e7dfb44..d7b05dc 100644 --- a/packages/backend/src/agent/tools.ts +++ b/packages/backend/src/agent/tools.ts @@ -373,11 +373,42 @@ async function moreOptionsTool(state: ToolState): Promise { return done({ options: next.options ? optionsForModel(next.options) : [] }) } +// "walmart.com/ip/Stainless-Steel-Whistling-Tea-Kettle/123456" -> "stainless steel whistling tea kettle". +// Only the words of the address: ids, codes and path labels are dropped. Null when too little is left. +export function wordsFromLink(rawUrl: string): string | null { + let url: URL + try { + url = new URL(rawUrl.trim()) + } catch { + return null + } + const skip = new Set(['ip', 'dp', 'gp', 'p', 'pd', 'product', 'products', 'item', 'items', 'itm', 'html', 'htm', 'www', 'detail', 'details', 'buy', 'shop']) + let path = url.pathname + try { + path = decodeURIComponent(path) + } catch { + // a broken %-sequence: the raw words are still readable + } + const words = path + .toLowerCase() + .split(/[^a-z]+/) + .filter((w) => w.length > 2 && !skip.has(w)) + const unique = [...new Set(words)].slice(0, 6) + return unique.length >= 2 ? unique.join(' ') : null +} + async function lookupLinkTool(args: Record, state: ToolState): Promise { - const result = await lookupLink(textArg(args.url) ?? '') + const link = textArg(args.url) ?? '' + const result = await lookupLink(link) if ('error' in result) { - say(state, result.error === 'LINK_NOT_PRODUCT' ? templates.linkNotProduct() : templates.linkFailed()) - return done({ error: result.error }) + // Not a dead end: the address usually says what the product is, so look for that. + const query = wordsFromLink(link) + if (!query || RESTRICTED_QUERY.test(query)) { + say(state, query ? templates.restricted() : result.error === 'LINK_NOT_PRODUCT' ? templates.linkNotProduct() : templates.linkUnclear()) + return done({ error: result.error }) + } + say(state, templates.linkSimilar(query)) + return runSearch(state, { query, budgetCents: null, store: null, size: null }, await getLastSearch(state.conversationId)) } await dropWaiting(state) const params = { query: result.option.title, budgetCents: null, store: null, size: null } diff --git a/packages/backend/src/templates.ts b/packages/backend/src/templates.ts index 98b0e6c..a0c29ff 100644 --- a/packages/backend/src/templates.ts +++ b/packages/backend/src/templates.ts @@ -45,6 +45,10 @@ export const templates = { linkFound: (option: OptionCard) => `Found it: ${option.title} from ${option.merchant}, ${formatUSD(option.total_cents)} with shipping. Say "buy it" for the total, or ask me about it.`, linkFailed: () => "I couldn't read that link. Try again, or tell me what you're looking for.", + // The page can't be opened, but its address says what it is: search for that instead. + linkSimilar: (query: string) => `I couldn't open that exact page, so here is what I found for ${query}.`, + // Nothing to go on: ask for words instead of a dead end. + linkUnclear: () => "I couldn't open that page, and I can't tell what it is. Tell me what you're looking for, and I'll find it.", linkNotProduct: () => "That link isn't a product page. Open the product itself and copy its link.", askAbout: (title: string) => `Ask me anything about the ${title}, or say "buy it".`, noOptions: () => 'I found nothing that fits. Try different words or a higher budget.',