Repository navigation
[Prototype, do not merge] feat(samples): add the inbox prototype's synthetic mailbox - #4381
josemontespg wants to merge 6 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds a new MCP app sample, a2ui-embed-inbox, containing a synthetic mailbox dataset, query utilities, formatting helpers, and unit tests. The review feedback focuses on enhancing the robustness of date and string manipulation, specifically suggesting a simpler implementation for formatWhen, defensive handling of invalid dates in ago, case-insensitive domain matching in messagesFromDomain, a more environment-independent date formatting approach in localDay, and a more flexible timezone offset regex.
| const parts = new Intl.DateTimeFormat('en-US', { | ||
| timeZone, | ||
| weekday: 'short', | ||
| month: 'short', | ||
| day: 'numeric', | ||
| ...(withTime ? {hour: 'numeric', minute: '2-digit'} : {}), | ||
| }).formatToParts(new Date(date)); | ||
| const value = (type: string) => parts.find(part => part.type === type)?.value ?? ''; | ||
| const day = `${value('weekday')} ${value('month')} ${value('day')}`; | ||
| return withTime ? `${day}, ${value('hour')}:${value('minute')} ${value('dayPeriod')}` : day; |
There was a problem hiding this comment.
The current implementation of formatWhen manually reconstructs the formatted date string by extracting parts from formatToParts. This is complex and can be fragile if the order of parts or the exact spacing (like non-breaking spaces) varies across different Node.js/ICU versions.
Since the target format is exactly the standard en-US format but without the first comma after the weekday, we can achieve this much more simply and robustly by formatting the date directly and removing the first comma using .replace(',', '').
| const parts = new Intl.DateTimeFormat('en-US', { | |
| timeZone, | |
| weekday: 'short', | |
| month: 'short', | |
| day: 'numeric', | |
| ...(withTime ? {hour: 'numeric', minute: '2-digit'} : {}), | |
| }).formatToParts(new Date(date)); | |
| const value = (type: string) => parts.find(part => part.type === type)?.value ?? ''; | |
| const day = `${value('weekday')} ${value('month')} ${value('day')}`; | |
| return withTime ? `${day}, ${value('hour')}:${value('minute')} ${value('dayPeriod')}` : day; | |
| const formatted = new Intl.DateTimeFormat('en-US', { | |
| timeZone, | |
| weekday: 'short', | |
| month: 'short', | |
| day: 'numeric', | |
| ...(withTime ? {hour: 'numeric', minute: '2-digit'} : {}), | |
| }).format(new Date(date)); | |
| return formatted.replace(',', ''); |
| export function ago(date: string, now: Date): string { | ||
| const hours = Math.max(0, (now.getTime() - Date.parse(date)) / 3_600_000); | ||
| if (hours < 1) return 'just now'; | ||
| if (hours < 24) return `${Math.floor(hours)} ${Math.floor(hours) === 1 ? 'hour' : 'hours'} ago`; | ||
| const days = Math.floor(hours / 24); | ||
| return days === 1 ? 'yesterday' : `${days} days ago`; | ||
| } |
There was a problem hiding this comment.
If an invalid date string is passed to ago, Date.parse(date) will return NaN. This propagates through the math and results in returning "NaN days ago". We should add a defensive check to handle invalid dates gracefully.
| export function ago(date: string, now: Date): string { | |
| const hours = Math.max(0, (now.getTime() - Date.parse(date)) / 3_600_000); | |
| if (hours < 1) return 'just now'; | |
| if (hours < 24) return `${Math.floor(hours)} ${Math.floor(hours) === 1 ? 'hour' : 'hours'} ago`; | |
| const days = Math.floor(hours / 24); | |
| return days === 1 ? 'yesterday' : `${days} days ago`; | |
| } | |
| export function ago(date: string, now: Date): string { | |
| const parsed = Date.parse(date); | |
| if (isNaN(parsed)) return ''; | |
| const hours = Math.max(0, (now.getTime() - parsed) / 3_600_000); | |
| if (hours < 1) return 'just now'; | |
| if (hours < 24) return Math.floor(hours) + ' ' + (Math.floor(hours) === 1 ? 'hour' : 'hours') + ' ago'; | |
| const days = Math.floor(hours / 24); | |
| return days === 1 ? 'yesterday' : days + ' days ago'; | |
| } |
| messagesFromDomain(domain: string): Message[] { | ||
| return this.mailbox.threads.flatMap(thread => | ||
| thread.messages.filter(message => domainOf(message.from.email) === domain), | ||
| ); | ||
| } |
There was a problem hiding this comment.
The domainOf helper returns a lowercased domain, but the domain parameter passed to messagesFromDomain is compared directly without lowercasing. If a caller passes a mixed-case domain (e.g., "Example.com"), the comparison will fail. We should lowercase the input domain parameter to ensure case-insensitive matching.
| messagesFromDomain(domain: string): Message[] { | |
| return this.mailbox.threads.flatMap(thread => | |
| thread.messages.filter(message => domainOf(message.from.email) === domain), | |
| ); | |
| } | |
| messagesFromDomain(domain: string): Message[] { | |
| const lowerDomain = domain.toLowerCase(); | |
| return this.mailbox.threads.flatMap(thread => | |
| thread.messages.filter(message => domainOf(message.from.email) === lowerDomain), | |
| ); | |
| } |
| export function localDay(date: string | Date, timeZone: string): string { | ||
| return new Intl.DateTimeFormat('en-CA', { | ||
| timeZone, | ||
| year: 'numeric', | ||
| month: '2-digit', | ||
| day: '2-digit', | ||
| }).format(new Date(date)); | ||
| } |
There was a problem hiding this comment.
Relying on the en-CA locale to always format dates as YYYY-MM-DD is risky. In environments with limited ICU support (such as some minimal Docker containers or older Node runtimes), the locale formatting might fallback to a default like MM/DD/YYYY, which would cause localDay to return an unexpected format and break downstream date parsing (e.g., in nextMonday where it splits by -).
Using formatToParts with a standard locale and explicitly extracting the year, month, and day parts is much more robust and environment-independent.
export function localDay(date: string | Date, timeZone: string): string {
const parts = new Intl.DateTimeFormat('en-US', {
timeZone,
year: 'numeric',
month: '2-digit',
day: '2-digit',
}).formatToParts(new Date(date));
const year = parts.find(p => p.type === 'year')?.value ?? '';
const month = parts.find(p => p.type === 'month')?.value ?? '';
const day = parts.find(p => p.type === 'day')?.value ?? '';
return year + '-' + month + '-' + day;
}| const parts = new Intl.DateTimeFormat('en-US', {timeZone, timeZoneName: 'longOffset'}) | ||
| .formatToParts(date) | ||
| .find(part => part.type === 'timeZoneName')?.value; | ||
| const match = /GMT([+-])(\d{2}):?(\d{2})?/.exec(parts ?? ''); |
There was a problem hiding this comment.
The regex /GMT([+-])(\d{2}):?(\d{2})?/ expects exactly two digits for the hour (\d{2}). While standard, some environments or older runtimes might format the timezone offset with a single digit for the hour (e.g., GMT-8 or GMT-8:00). Using \d{1,2} makes the parser more robust against these variations.
| const match = /GMT([+-])(\d{2}):?(\d{2})?/.exec(parts ?? ''); | |
| const match = /GMT([+-])(\d{1,2}):?(\d{2})?/.exec(parts ?? ''); |
4faaddf to
454e474
Compare
Internal M1 proof-of-concept sample for @a2ui/embed (A2UI in MCP Apps), not meant to merge. Builds on #4334.
Summary
First of four PRs adding
samples/community/mcp/a2ui-embed-inbox, an inbox assistant as an MCP App. The agent prepares every email decision that needs the person in one card, with its pick pre-selected and the reason shown, and the person confirms once.This PR adds the package and the demo data: a synthetic mailbox shaped like Gmail and Calendar, so the sample could read the real APIs later.
Changes
package.json,tsconfig.json, vitest and eslint configs) and the community workspace resolutions for the local@a2ui/*packages.fixtures/mailbox.json: one week for a persona, with 30 threads (labels, headers such asList-Unsubscribe, markup for bills, trips, money requests and signature requests, calendar invites), a calendar, contacts, payees with bill history and per-sender open rates. 12 threads need a decision; the rest are noise.src/mail/: zod types for the mailbox and read-only queries (conflicts, free slots next week, events on a day, engagement).src/format.tsandsrc/config.ts: plain-language formatting and paths.Verification
yarn install --immutableinsamples/community, thenyarn buildandyarn lintpass.yarn test: 3 tests pass.