feat(assistant): collection list and saved place cards

This commit is contained in:
Maurice
2026-10-22 15:50:52 +02:00
parent 6e92c2777c
commit a440aefd43
68 changed files with 6052 additions and 92 deletions
@@ -1,5 +1,44 @@
import { COLLECTION_STATUSES } from '../../collection/collection.schema';
import { cardCountSchema, cardHexColorSchema, cardPersonSchema, cardText } from './card-parts';
import { collectionCardListSchema } from './collection-places.card';
import { z } from 'zod';
/** The collections a user keeps. Only its kind so far; the collections cards give it its fields. */
export const collectionListsCardSchema = z.object({ kind: z.literal('collectionLists') });
/**
* The lists a user keeps, as the chat shows them: each with its colour, size
* and people, an incoming invite with whom it came from, the lists a place is
* saved in with its status in each, and what a held change does to a list.
*/
/** What a change does to a list; `none` on an answer. */
export const COLLECTION_LIST_CHANGES = [
'none',
'add',
'update',
'remove',
'join',
'decline',
'withdraw',
'order',
] as const;
export const collectionListRowSchema = collectionCardListSchema.extend({
/** The place's status in this list (a lookup of where a place is saved). */
status: z.enum(COLLECTION_STATUSES).nullable(),
/** Who sent an incoming invite, or whom a withdrawn one was for. */
person: cardPersonSchema.nullable(),
/** An invite the user has not answered yet. */
invite: z.boolean(),
change: z.enum(COLLECTION_LIST_CHANGES),
before: z.object({ name: cardText(120), color: cardHexColorSchema.nullable() }).nullable(),
});
export type CollectionListRow = z.infer<typeof collectionListRowSchema>;
export const collectionListsCardSchema = z.object({
kind: z.literal('collectionLists'),
lists: z.array(collectionListRowSchema).max(12),
more: cardCountSchema,
/** The place a lookup asked about, as it was asked. */
place: cardText(120).nullable(),
});
export type CollectionListsCard = z.infer<typeof collectionListsCardSchema>;
@@ -1,5 +1,132 @@
import { COLLECTION_STATUSES } from '../../collection/collection.schema';
import { idSchema } from '../../common/primitives.schema';
import {
cardCountSchema,
cardFileKeySchema,
cardHexColorSchema,
cardLatSchema,
cardLngSchema,
cardPhotoKeySchema,
cardText,
} from './card-parts';
import { z } from 'zod';
/** Places of a collection. Only its kind so far; the collections cards give it its fields. */
export const collectionPlacesCardSchema = z.object({ kind: z.literal('collectionPlaces') });
/**
* Saved places of the collections as the chat shows them: a list's head with
* its colour, size and people, its places as its rows (status, labels, rating,
* whether the trip has the place already), and what a held change does to
* them. A saved place's id is its own and never a trip place's: the one link
* to the trip is `inTripPlaceId`.
*/
const statusSchema = z.enum(COLLECTION_STATUSES);
/** The fields of a saved place a change can touch, each named on the card by its label. */
export const COLLECTION_CARD_FIELDS = [
'name',
'address',
'coordinates',
'description',
'notes',
'category',
'link',
'photo',
] as const;
/** What a change does to a label of a list; `none` on an answer. */
export const COLLECTION_LABEL_CHANGES = ['none', 'add', 'rename', 'remove'] as const;
/** A list as a card names it: its colour dot, its size and how many share it. `id` null: the list a change creates. */
export const collectionCardListSchema = z.object({
id: idSchema.nullable(),
name: cardText(120),
color: cardHexColorSchema.nullable(),
placeCount: cardCountSchema.nullable(),
/** Accepted members besides the owner. */
memberCount: cardCountSchema,
/** Places per status, only where the whole list was read. */
counts: z.object({ idea: cardCountSchema, want: cardCountSchema, visited: cardCountSchema }).nullable(),
});
export type CollectionCardList = z.infer<typeof collectionCardListSchema>;
export const collectionCardLabelSchema = z.object({
id: idSchema.nullable(),
name: cardText(60),
color: cardHexColorSchema.nullable(),
/** How many places of its list carry it, where the whole list was read. */
places: cardCountSchema.nullable(),
change: z.enum(COLLECTION_LABEL_CHANGES),
before: z.object({ name: cardText(60), color: cardHexColorSchema.nullable() }).nullable(),
});
export type CollectionCardLabel = z.infer<typeof collectionCardLabelSchema>;
/** What a held change does to one saved place. */
export const collectionCardDeltaSchema = z.object({
status: z.object({ from: statusSchema.nullable(), to: statusSchema }).nullable(),
/** The user's own vote; `to: null` takes it away. */
rating: z.object({ to: z.number().int().min(1).max(5).nullable() }).nullable(),
labelsAdded: z.array(idSchema).max(10),
labelsRemoved: z.array(idSchema).max(10),
fields: z.array(z.enum(COLLECTION_CARD_FIELDS)).max(8),
movedTo: collectionCardListSchema.nullable(),
removed: z.boolean(),
});
export type CollectionCardDelta = z.infer<typeof collectionCardDeltaSchema>;
export const collectionCardPlaceSchema = z.object({
/** The saved place's own id; null before a save is applied. */
id: idSchema.nullable(),
listId: idSchema.nullable(),
name: cardText(120),
address: cardText(200).nullable(),
lat: cardLatSchema.nullable(),
lng: cardLngSchema.nullable(),
category: z
.object({ name: cardText(60), icon: cardText(40).nullable(), color: cardHexColorSchema.nullable() })
.nullable(),
status: statusSchema,
labelIds: z.array(idSchema).max(10),
rating: z.number().min(1).max(5).nullable(),
ratingCount: cardCountSchema,
/** The Google place id the photo proxy finds its picture by. */
googlePlaceId: cardPhotoKeySchema.nullable(),
osmId: cardText(40).nullable(),
/** The file name of its own uploaded photo; the client builds `/uploads/places/<key>`. */
imageKey: cardFileKeySchema.nullable(),
/** The same place in the chat's trip, by its provider id or the trip place it was saved from. */
inTripPlaceId: idSchema.nullable(),
/** The lists that hold the place already (a save's warning). */
savedIn: z.array(z.object({ listId: idSchema, name: cardText(120), status: statusSchema })).max(3),
delta: collectionCardDeltaSchema.nullable(),
});
export type CollectionCardPlace = z.infer<typeof collectionCardPlaceSchema>;
/** What an applied change of several places did, from the tool's answer. */
export const collectionCardOutcomeSchema = z.discriminatedUnion('type', [
z.object({ type: z.literal('duplicate'), name: cardText(120) }),
z.object({
type: z.literal('copied'),
copied: cardCountSchema,
skipped: cardCountSchema,
skippedNames: z.array(cardText(120)).max(5),
}),
z.object({ type: z.literal('statusSet'), updated: cardCountSchema, found: cardCountSchema }),
]);
export type CollectionCardOutcome = z.infer<typeof collectionCardOutcomeSchema>;
export const collectionPlacesCardSchema = z.object({
kind: z.literal('collectionPlaces'),
/** Null when the places sit in several lists. */
list: collectionCardListSchema.nullable(),
places: z.array(collectionCardPlaceSchema).max(8),
more: cardCountSchema,
/** Only the labels the shown places carry or the change names. */
labels: z.array(collectionCardLabelSchema).max(24),
outcome: collectionCardOutcomeSchema.nullable(),
/** A status set on a trip place in every list that holds it. */
everyList: z.boolean(),
/** A copy into a trip other than the chat's. */
otherTrip: z.boolean(),
});
export type CollectionPlacesCard = z.infer<typeof collectionPlacesCardSchema>;
@@ -0,0 +1,138 @@
import { assistantAppliedSchema } from '../assistant.schema';
import { MAX_CARD_CHARS } from './card-parts';
import { collectionListsCardSchema } from './collection-lists.card';
import { collectionPlacesCardSchema } from './collection-places.card';
import { COLLECTION_APPLIED_ENTITIES } from './collection.contract';
import { storedTrekCardSchema, trekCardSchema } from './index';
import { describe, expect, it } from 'vitest';
const list = { id: 12, name: 'Paris food', color: '#e11d48', placeCount: 12, memberCount: 2, counts: null };
const place = {
id: 34,
listId: 12,
name: 'Septime',
address: '80 Rue de Charonne',
lat: 48.853,
lng: 2.381,
category: { name: 'Restaurant', icon: 'Utensils', color: '#f97316' },
status: 'want',
labelIds: [5],
rating: 4.5,
ratingCount: 2,
googlePlaceId: 'ChIJseptime',
osmId: null,
imageKey: 'septime.jpg',
inTripPlaceId: null,
savedIn: [],
delta: null,
};
const placesCard = {
kind: 'collectionPlaces',
list,
places: [place],
more: 0,
labels: [{ id: 5, name: 'Dinner', color: '#2563eb', places: 3, change: 'none', before: null }],
outcome: null,
everyList: false,
otherTrip: false,
};
const row = {
...list,
status: null,
person: null,
invite: false,
change: 'none',
before: null,
};
describe('collection cards', () => {
it('CARDS-COLL-001: a list with its saved places and labels reads as itself through the union', () => {
expect(trekCardSchema.parse(placesCard)).toEqual(placesCard);
expect(storedTrekCardSchema.parse(placesCard)).toEqual(placesCard);
expect(JSON.stringify(placesCard).length).toBeLessThan(MAX_CARD_CHARS);
});
it('CARDS-COLL-002: a place a change has not saved yet and the list a change creates carry no id', () => {
const pending = {
...placesCard,
list: { ...list, id: null, placeCount: 0, memberCount: 0 },
places: [{ ...place, id: null, listId: null, imageKey: null, delta: deltaOf({ removed: false }) }],
};
expect(collectionPlacesCardSchema.safeParse(pending).success).toBe(true);
});
it('CARDS-COLL-003: a photo is a stored file name, never an address or a path', () => {
const refused = ['https://example.com/a.jpg', '/uploads/places/a.jpg', '../a.jpg', 'data:image/png;base64,x'];
for (const imageKey of refused) {
const card = { ...placesCard, places: [{ ...place, imageKey }] };
expect(collectionPlacesCardSchema.safeParse(card).success, imageKey).toBe(false);
}
});
it('CARDS-COLL-004: rows, labels and names stay inside their bounds; a card past them reads as null', () => {
const nine = Array.from({ length: 9 }, (_, i) => ({ ...place, id: i + 1 }));
expect(collectionPlacesCardSchema.safeParse({ ...placesCard, places: nine }).success).toBe(false);
expect(collectionPlacesCardSchema.safeParse({ ...placesCard, list: { ...list, name: '' } }).success).toBe(false);
const tooHigh = { ...placesCard, places: [{ ...place, rating: 6 }] };
expect(collectionPlacesCardSchema.safeParse(tooHigh).success).toBe(false);
expect(storedTrekCardSchema.parse({ ...placesCard, places: nine })).toBeNull();
});
it('CARDS-COLL-005: a change names the status it leaves, the vote it casts and the fields it touches', () => {
const delta = deltaOf({ status: { from: 'idea', to: 'visited' }, rating: { to: null }, fields: ['photo'] });
const card = { ...placesCard, places: [{ ...place, delta }] };
expect(collectionPlacesCardSchema.parse(card).places[0]?.delta).toEqual(delta);
const unknownField = { ...placesCard, places: [{ ...place, delta: deltaOf({ fields: ['phone'] }) }] };
expect(collectionPlacesCardSchema.safeParse(unknownField).success).toBe(false);
});
it('CARDS-COLL-006: an applied copy, a duplicate and a status set across lists each say what happened', () => {
for (const outcome of [
{ type: 'duplicate', name: 'Septime' },
{ type: 'copied', copied: 2, skipped: 1, skippedNames: ['Le Comptoir'] },
{ type: 'statusSet', updated: 3, found: 2 },
]) {
expect(collectionPlacesCardSchema.safeParse({ ...placesCard, outcome }).success, outcome.type).toBe(true);
}
});
it('CARDS-COLL-007: the lists card holds lists, invites and a lookup of where a place is saved', () => {
const card = {
kind: 'collectionLists',
lists: [
{ ...row, status: 'visited' },
{ ...row, id: 13, invite: true, person: { id: 4, name: 'Maya', avatarKey: null }, placeCount: null },
],
more: 0,
place: 'Septime',
};
expect(trekCardSchema.parse(card)).toEqual(card);
const thirteen = Array.from({ length: 13 }, (_, i) => ({ ...row, id: i + 1 }));
expect(collectionListsCardSchema.safeParse({ ...card, lists: thirteen }).success).toBe(false);
expect(collectionListsCardSchema.safeParse({ ...card, lists: [{ ...row, change: 'rename' }] }).success).toBe(false);
});
it('CARDS-COLL-008: an applied change names a list or a saved place as what it touched', () => {
for (const entity of COLLECTION_APPLIED_ENTITIES) {
const applied = { toolCallId: 'c1', entity, ids: [12], dayId: null, undoUntil: null };
expect(assistantAppliedSchema.parse(applied).entity).toBe(entity);
}
});
});
function deltaOf(over: Record<string, unknown>) {
return {
status: null,
rating: null,
labelsAdded: [],
labelsRemoved: [],
fields: [],
movedTo: null,
removed: false,
...over,
};
}
@@ -12,5 +12,7 @@ export * from './collection-places.card';
* or entity is added here and nowhere else.
*/
export const COLLECTION_CARD_SCHEMAS = [collectionPlacesCardSchema, collectionListsCardSchema] as const;
/** Their changes read as the plain add, change and remove, named by each tool's own label. */
export const COLLECTION_VERB_KEYS = [] as const;
export const COLLECTION_APPLIED_ENTITIES = [] as const;
/** A list (`collection`) and a saved place of one (`collection_place`), both opened in Collections. */
export const COLLECTION_APPLIED_ENTITIES = ['collection', 'collection_place'] as const;