feat(journey): the assistant designs the whole book as one proposal

design_book, revise_book_design and apply_book_design work on a draft, the
apply keeps the old book as a revision for undo, skips pages changed since,
and broadcasts once after the commit. Book level tools set the page setup,
duplicate pages and save layouts.
This commit is contained in:
Maurice
2026-10-22 21:25:46 +00:00
parent 09ffc7d946
commit bda07d6a3e
55 changed files with 4327 additions and 37 deletions
@@ -0,0 +1,27 @@
import { EntityRepositoryType, type Opt, type Ref, defineEntity, p } from '@mikro-orm/core';
import { JourneyBookRevisionsRepository } from '../repositories/JourneyBookRevisions.repository';
import { DbTimestampType } from '../types';
import { Journeys } from './Journeys.entity';
export class JourneyBookRevisions {
[EntityRepositoryType]?: JourneyBookRevisionsRepository;
id!: number & Opt;
journey!: Ref<Journeys>;
journey_id!: number;
version!: number;
document!: string;
created_at!: string & Opt;
}
export const JourneyBookRevisionsSchema = defineEntity({
class: JourneyBookRevisions,
repository: () => JourneyBookRevisionsRepository,
properties: {
id: p.integer().primary(),
journey: () => p.manyToOne(Journeys).ref().deleteRule('cascade').hidden().index('idx_journey_book_revisions_journey'),
journey_id: p.integer().persist(false).index('idx_journey_book_revisions_journey'),
version: p.integer(),
document: p.text(),
created_at: p.type(DbTimestampType).defaultRaw(`CURRENT_TIMESTAMP`),
},
});
@@ -1,5 +1,7 @@
import { Collection, EntityRepositoryType, type Opt, type Ref, defineEntity, p } from '@mikro-orm/core';
import { JourneysRepository } from '../repositories/Journeys.repository';
import { AssistantThreads } from './AssistantThreads.entity';
import { JourneyBookRevisions } from './JourneyBookRevisions.entity';
import { JourneyBooks } from './JourneyBooks.entity';
import { JourneyContributors } from './JourneyContributors.entity';
import { JourneyEntries } from './JourneyEntries.entity';
@@ -31,6 +33,8 @@ export class Journeys {
status_override?: string | null;
photo_location: number & Opt = 0;
auto_prepare_recordings: number & Opt = 0;
assistant_threads_collection = new Collection<AssistantThreads>(this);
journey_book_revisions_collection = new Collection<JourneyBookRevisions>(this);
journey_books_collection = new Collection<JourneyBooks>(this);
journey_contributors_collection = new Collection<JourneyContributors>(this);
journey_entries_collection = new Collection<JourneyEntries>(this);
@@ -64,6 +68,8 @@ export const JourneysSchema = defineEntity({
status_override: p.text().nullable(),
photo_location: p.integer().default(0),
auto_prepare_recordings: p.integer().default(0),
assistant_threads_collection: () => p.oneToMany(AssistantThreads).mappedBy('journey').hidden(),
journey_book_revisions_collection: () => p.oneToMany(JourneyBookRevisions).mappedBy('journey').hidden(),
journey_books_collection: () => p.oneToMany(JourneyBooks).mappedBy('journey').hidden(),
journey_contributors_collection: () => p.oneToMany(JourneyContributors).mappedBy('journey').hidden(),
journey_entries_collection: () => p.oneToMany(JourneyEntries).mappedBy('journey').hidden(),
+2
View File
@@ -45,6 +45,7 @@ import { HiddenCountriesSchema } from './HiddenCountries.entity';
import { HiddenRegionsSchema } from './HiddenRegions.entity';
import { IdempotencyKeysSchema } from './IdempotencyKeys.entity';
import { InviteTokensSchema } from './InviteTokens.entity';
import { JourneyBookRevisionsSchema } from './JourneyBookRevisions.entity';
import { JourneyBooksSchema } from './JourneyBooks.entity';
import { JourneyContributorsSchema } from './JourneyContributors.entity';
import { JourneyEntriesSchema } from './JourneyEntries.entity';
@@ -206,6 +207,7 @@ export const ALL_ENTITIES: readonly EntitySchema[] = [
HiddenRegionsSchema,
IdempotencyKeysSchema,
InviteTokensSchema,
JourneyBookRevisionsSchema,
JourneyBooksSchema,
JourneyContributorsSchema,
JourneyEntriesSchema,
+19
View File
@@ -422,3 +422,22 @@ export const AI_CONNECTION_ACCOUNT: JsonColumn<AiConnectionAccount> = {
schema: aiConnectionAccountSchema,
fallback: () => ({}),
};
/**
* A book as an applied design found it, kept for its undo: the same document
* `journey_books.document` holds. Read as a JSON object; the book contract
* normalizes it on the way back. A broken row reads as none and is refused,
* never written back as an empty book.
*/
export const JOURNEY_BOOK_REVISION_DOCUMENT: JsonColumn<Record<string, unknown> | null> = {
column: 'journey_book_revisions.document',
schema: z.record(z.string(), z.unknown()),
fallback: () => null,
};
/** A photo's main colours, up to five `#rrggbb:share`, most of the picture first. A broken row reads as none. */
export const TREK_PHOTO_SIGNAL_PALETTE: JsonColumn<string[]> = {
column: 'trek_photo_signals.palette',
schema: z.array(z.string().regex(/^#[0-9a-f]{6}(?::(?:0|1)(?:\.\d{1,2})?)?$/)).max(5),
fallback: () => [],
};
@@ -0,0 +1,11 @@
// Generated by scripts/generate-db-types.ts from the migrated schema. Do not edit by hand:
// change the migration, then run `npm run gen:db-types`.
import type { InsertOptional } from './columns';
export interface JourneyBookRevisionsTable {
id: InsertOptional<number>;
journey_id: number;
version: number;
document: string;
created_at: InsertOptional<string>;
}
+2
View File
@@ -48,6 +48,7 @@ import type { HiddenCountriesTable } from './HiddenCountries.table';
import type { HiddenRegionsTable } from './HiddenRegions.table';
import type { IdempotencyKeysTable } from './IdempotencyKeys.table';
import type { InviteTokensTable } from './InviteTokens.table';
import type { JourneyBookRevisionsTable } from './JourneyBookRevisions.table';
import type { JourneyBooksTable } from './JourneyBooks.table';
import type { JourneyContributorsTable } from './JourneyContributors.table';
import type { JourneyEntriesTable } from './JourneyEntries.table';
@@ -215,6 +216,7 @@ export interface DB {
hidden_regions: HiddenRegionsTable;
idempotency_keys: IdempotencyKeysTable;
invite_tokens: InviteTokensTable;
journey_book_revisions: JourneyBookRevisionsTable;
journey_books: JourneyBooksTable;
journey_contributors: JourneyContributorsTable;
journey_entries: JourneyEntriesTable;
@@ -0,0 +1,26 @@
import { Migration } from '@mikro-orm/migrations';
/**
* A TREK Studio book keeps the last few versions a whole-book change replaced,
* so the assistant's `apply_book_design` can be taken back in one step
* (`restore_book_revision`). The row holds the document as it was (`document`,
* the same JSON `journey_books.document` holds) and the version it stood at
* (`version`); the apply writes it in the same transaction as the new book,
* and keeps the newest five per journey. A revision goes with its journey.
*/
export class Migration20200101046260_a_book_keeps_its_last_revisions extends Migration {
override name = 'Migration20200101046260_a_book_keeps_its_last_revisions';
override up(): void {
this.addSql(`
CREATE TABLE IF NOT EXISTS journey_book_revisions (
id INTEGER PRIMARY KEY,
journey_id INTEGER NOT NULL REFERENCES journeys(id) ON DELETE CASCADE,
version INTEGER NOT NULL,
document TEXT NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
)
`);
this.addSql('CREATE INDEX IF NOT EXISTS idx_journey_book_revisions_journey ON journey_book_revisions(journey_id)');
}
}
@@ -0,0 +1,63 @@
import type { JourneyBookRevisions } from '../entities/JourneyBookRevisions.entity';
import type { DB } from '../kysely/db';
import { dbNow } from '../types';
import { TrekRepository } from './_shared/trek-repository';
type RevisionsDB = Pick<DB, 'journey_book_revisions'>;
/** A book as a whole-book change found it, kept so the change can be taken back. */
interface JourneyBookRevisionRow {
id: number;
journey_id: number;
version: number;
document: string;
created_at: string;
}
/**
* `journey_book_revisions`: the book documents an `apply_book_design`
* replaced, written in the transaction of that apply and read back by
* `restore_book_revision`. A journey keeps its newest few; the rest go when
* the next one is kept.
*/
export class JourneyBookRevisionsRepository extends TrekRepository<JourneyBookRevisions> {
private db() {
return this.kysely<RevisionsDB>();
}
/** Keeps the book as it stood at `version`; answers the revision's id. */
async insertRevision(journeyId: number, version: number, document: string): Promise<number> {
const row = await this.db()
.insertInto('journey_book_revisions')
.values({ journey_id: journeyId, version, document, created_at: dbNow() })
.returning('id')
.executeTakeFirstOrThrow();
return row.id;
}
/** Drops the journey's revisions past its newest `keep`; answers how many went. */
async pruneRevisions(journeyId: number, keep: number): Promise<number> {
const newest = this.db()
.selectFrom('journey_book_revisions')
.select('id')
.where('journey_id', '=', journeyId)
.orderBy('id', 'desc')
.limit(keep);
const result = await this.db()
.deleteFrom('journey_book_revisions')
.where('journey_id', '=', journeyId)
.where('id', 'not in', newest)
.executeTakeFirst();
return Number(result.numDeletedRows);
}
/** One revision of this journey's book; undefined for another journey's or one that is gone. */
async findForJourney(id: number, journeyId: number): Promise<JourneyBookRevisionRow | undefined> {
return await this.db()
.selectFrom('journey_book_revisions')
.select(['id', 'journey_id', 'version', 'document', 'created_at'])
.where('id', '=', id)
.where('journey_id', '=', journeyId)
.executeTakeFirst();
}
}