Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/backend/src/account/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
21 changes: 21 additions & 0 deletions packages/backend/src/agent/links.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
37 changes: 34 additions & 3 deletions packages/backend/src/agent/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,42 @@ async function moreOptionsTool(state: ToolState): Promise<ToolResult> {
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<string, unknown>, state: ToolState): Promise<ToolResult> {
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 }
Expand Down
4 changes: 4 additions & 0 deletions packages/backend/src/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
Loading