@focus-reactive/payload-plugin-translator 0.8.0 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/dist/client/entities/translation/api/queries/useDocumentTranslation.d.ts +15 -15
  2. package/dist/client/entities/translation/api/queries/useDocumentTranslation.js +27 -11
  3. package/dist/client/entities/translation/index.d.ts +1 -1
  4. package/dist/client/entities/translation/index.js +1 -1
  5. package/dist/client/entities/translation/model/panelStatus.d.ts +8 -2
  6. package/dist/client/entities/translation/model/panelStatus.js +16 -0
  7. package/dist/client/entities/translation/model/statusRows.d.ts +9 -6
  8. package/dist/client/entities/translation/model/statusRows.js +18 -16
  9. package/dist/client/entities/translation/model/types.d.ts +6 -1
  10. package/dist/client/widgets/translate-document/ui/TranslateDocument.js +4 -4
  11. package/dist/composition/levels/useDocTranslationApi.d.ts +2 -2
  12. package/dist/composition/levels/useDocTranslationApi.js +3 -4
  13. package/dist/plugin.js +21 -74
  14. package/dist/server/features/createTranslationRoutes.d.ts +4 -13
  15. package/dist/server/features/createTranslationRoutes.js +3 -4
  16. package/dist/server/features/get-document-status/handler.js +4 -2
  17. package/dist/server/features/get-document-status/model.d.ts +10 -0
  18. package/dist/server/features/get-document-status/model.js +21 -0
  19. package/dist/server/features/staleness/dismissStaleness.handler.js +6 -2
  20. package/dist/server/features/staleness/getDocumentStaleness.handler.js +2 -2
  21. package/dist/server/features/staleness/index.d.ts +1 -1
  22. package/dist/server/features/staleness/model.d.ts +4 -16
  23. package/dist/server/features/translate-document/handler.d.ts +6 -8
  24. package/dist/server/features/translate-document/handler.js +20 -46
  25. package/dist/server/features/translate-document/index.d.ts +1 -0
  26. package/dist/server/features/translate-document/index.js +1 -0
  27. package/dist/server/features/translate-document/wireTranslateRunner.d.ts +28 -0
  28. package/dist/server/features/translate-document/wireTranslateRunner.js +49 -0
  29. package/dist/server/modules/provenance/{provenanceCollection.d.ts → Provenance.collection.d.ts} +17 -9
  30. package/dist/server/modules/provenance/{provenanceCollection.js → Provenance.collection.js} +23 -9
  31. package/dist/server/modules/provenance/Provenance.service.d.ts +56 -0
  32. package/dist/server/modules/provenance/Provenance.service.js +124 -0
  33. package/dist/server/modules/provenance/Provenance.shapes.d.ts +26 -0
  34. package/dist/server/modules/provenance/Provenance.shapes.js +7 -0
  35. package/dist/server/modules/provenance/{PayloadProvenanceStore.js → Provenance.store.js} +1 -1
  36. package/dist/server/modules/provenance/Provenance.wiring.d.ts +23 -0
  37. package/dist/server/modules/provenance/Provenance.wiring.js +43 -0
  38. package/dist/server/modules/provenance/{provenanceCleanupHook.d.ts → ProvenanceCleanup.hook.d.ts} +4 -3
  39. package/dist/server/modules/provenance/{provenanceCleanupHook.js → ProvenanceCleanup.hook.js} +1 -1
  40. package/dist/server/modules/provenance/index.d.ts +8 -4
  41. package/dist/server/modules/provenance/index.js +6 -4
  42. package/dist/server/modules/task-runner/payload-jobs-runner/PayloadJobsTaskRunner.js +11 -2
  43. package/dist/server/modules/task-runner/sync-runner/SyncTaskRunner.js +5 -3
  44. package/dist/server/modules/translation-levels/PluginConfigBuilder.d.ts +6 -14
  45. package/dist/server/modules/translation-levels/PluginConfigBuilder.js +2 -2
  46. package/dist/server/modules/translation-levels/index.d.ts +1 -1
  47. package/dist/server/modules/translation-levels/types.d.ts +20 -12
  48. package/dist/server/shared/payload/sourceDocument.d.ts +8 -0
  49. package/dist/server/shared/payload/sourceDocument.js +15 -0
  50. package/dist/types/ConfigModifier.d.ts +10 -0
  51. package/dist/types/ConfigModifier.js +10 -0
  52. package/package.json +1 -1
  53. package/dist/server/features/_lib/sourceDocument.d.ts +0 -9
  54. package/dist/server/features/_lib/sourceDocument.js +0 -16
  55. package/dist/server/features/staleness/service.d.ts +0 -15
  56. package/dist/server/features/staleness/service.js +0 -78
  57. /package/dist/server/modules/provenance/{PayloadProvenanceStore.d.ts → Provenance.store.d.ts} +0 -0
@@ -10,6 +10,27 @@ import { JobIdSchema, toClientErrorMessage } from "../../shared";
10
10
  collection_id: JobIdSchema,
11
11
  collection_slug: z.string().nonempty()
12
12
  });
13
+ // A job is "newer" by creation time, tie-broken by last update — both ISO-8601, so a lexicographic
14
+ // string compare is a correct chronological compare (no Date parsing needed).
15
+ const isNewerTask = (candidate, current)=>candidate.createdAt !== current.createdAt ? candidate.createdAt > current.createdAt : candidate.updatedAt > current.updatedAt;
16
+ /**
17
+ * Reduce a document's jobs to the latest one per target locale.
18
+ *
19
+ * `findByCollection` returns every job for the document across all target locales (plus any superseded
20
+ * jobs not yet cancelled). The status panel shows one row per target locale, so we keep — per
21
+ * `targetLng` — the most recently created job (tie-break: most recently updated). Without this the
22
+ * caller sees a single arbitrary job and every other in-flight locale looks idle, which is the
23
+ * concurrent re-translate bug (a second re-translate appearing to overwrite the first's status).
24
+ */ export function latestTaskPerTargetLocale(tasks) {
25
+ const byLocale = new Map();
26
+ for (const task of tasks){
27
+ const current = byLocale.get(task.input.targetLng);
28
+ if (!current || isNewerTask(task, current)) byLocale.set(task.input.targetLng, task);
29
+ }
30
+ return [
31
+ ...byLocale.values()
32
+ ];
33
+ }
13
34
  /**
14
35
  * Transforms a Task to API output format (snake_case for client compatibility)
15
36
  */ export function taskToJobStatusOutput(task) {
@@ -1,7 +1,6 @@
1
1
  import { ServerResponse } from "../../shared";
2
2
  import { isCollectionAvailable } from "../_lib/collection-utils";
3
3
  import { DismissStalenessInputSchema } from "./model";
4
- import { dismissLocaleStaleness } from "./service";
5
4
  /** Dismisses (acknowledges) staleness of one target locale for a document. */ export class DismissStalenessHandler {
6
5
  config;
7
6
  constructor(config){
@@ -17,7 +16,12 @@ import { dismissLocaleStaleness } from "./service";
17
16
  if (!collectionSlug) {
18
17
  return ServerResponse.badRequest("Collection not available for translation");
19
18
  }
20
- await dismissLocaleStaleness(req.payload, this.config, collectionSlug, collection_id, target_lng);
19
+ const service = this.config.provenanceServiceFactory?.(req.payload);
20
+ await service?.dismiss({
21
+ collectionSlug: collectionSlug,
22
+ documentId: collection_id,
23
+ targetLocale: target_lng
24
+ });
21
25
  return ServerResponse.success({
22
26
  success: true
23
27
  });
@@ -1,7 +1,6 @@
1
1
  import { ServerResponse } from "../../shared";
2
2
  import { isCollectionAvailable } from "../_lib/collection-utils";
3
3
  import { GetDocumentStalenessInputSchema } from "./model";
4
- import { computeDocumentStaleness } from "./service";
5
4
  /**
6
5
  * Reads per-locale staleness for a single document. Best-effort: a recompute failure (e.g. the
7
6
  * consumer enabled provenance but has not run the SQL migration, so the sidecar table is missing)
@@ -22,7 +21,8 @@ import { computeDocumentStaleness } from "./service";
22
21
  return ServerResponse.badRequest("Collection not available for translation");
23
22
  }
24
23
  try {
25
- const locales = await computeDocumentStaleness(req.payload, this.config, collectionSlug, collection_id);
24
+ const service = this.config.provenanceServiceFactory?.(req.payload);
25
+ const locales = service ? await service.getStaleness(collectionSlug, collection_id) : [];
26
26
  return ServerResponse.success({
27
27
  locales
28
28
  });
@@ -1,2 +1,2 @@
1
1
  export { createGetDocumentStalenessRoute, createDismissStalenessRoute } from "./route";
2
- export type { StalenessConfig, StalenessLocaleOutput } from "./model";
2
+ export type { StalenessConfig } from "./model";
@@ -1,7 +1,6 @@
1
1
  import { z } from "zod";
2
2
  import type { CollectionSlug } from "payload";
3
- import type { CollectionSchemaMap } from "../../../types/CollectionSchemaMap";
4
- import type { ProvenanceStoreFactory } from "../../modules/provenance";
3
+ import type { TranslationContext } from "../../modules/translation-levels";
5
4
  /**
6
5
  * Route params for reading a document's per-locale staleness.
7
6
  *
@@ -32,21 +31,10 @@ export declare const DismissStalenessInputSchema: z.ZodObject<{
32
31
  target_lng: string;
33
32
  }>;
34
33
  /**
35
- * Per-locale staleness for one document (snake_case for client compatibility, matching the other
36
- * translation endpoints). One entry per target locale that has a provenance record.
37
- */
38
- export type StalenessLocaleOutput = {
39
- target_lng: string;
40
- source_lng: string;
41
- is_stale: boolean;
42
- translated_at: string;
43
- };
44
- /**
45
- * Handler configuration. `provenanceStoreFactory` is absent when provenance is disabled — the
34
+ * Handler configuration. `provenanceServiceFactory` is absent when provenance is disabled the
46
35
  * handlers then report no staleness (empty), so the endpoint contract stays stable either way.
36
+ * The fingerprint policy + schema live inside {@link ProvenanceService}, so no `schemaMap` here.
47
37
  */
48
- export type StalenessConfig = {
38
+ export type StalenessConfig = Pick<TranslationContext, "provenanceServiceFactory"> & {
49
39
  availableCollections: Set<CollectionSlug>;
50
- schemaMap: CollectionSchemaMap;
51
- provenanceStoreFactory?: ProvenanceStoreFactory;
52
40
  };
@@ -1,21 +1,19 @@
1
1
  import type { Payload } from "payload";
2
2
  import type { Handler } from "../../shared";
3
3
  import type { TranslationProvider } from "../../../core/translation-providers";
4
- import type { ProvenanceStoreFactory } from "../../modules/provenance";
4
+ import type { ProvenanceServiceFactory } from "../../modules/provenance";
5
5
  import type { CollectionSchemaMap } from "../../../types/CollectionSchemaMap";
6
6
  import type { TranslateDocumentInput, TranslateDocumentOutput } from "./model";
7
- export type TranslateDocumentDependencies = {
8
- translationProvider: TranslationProvider;
9
- schemaMap: CollectionSchemaMap;
10
- };
11
7
  /**
12
- * Translates a single document from source language to target language
8
+ * Translates a single document from source language to target language. Provenance is delegated to
9
+ * {@link ProvenanceService}: this handler only decides *when* to capture the source fingerprint
10
+ * (before the pipeline mutates the source in place) and *when* to record it (after the save).
13
11
  */
14
12
  export declare class TranslateDocumentHandler implements Handler<TranslateDocumentInput, TranslateDocumentOutput> {
15
13
  private readonly translationProvider;
16
14
  private readonly schemaMap;
17
- private readonly provenanceStoreFactory?;
18
- constructor(translationProvider: TranslationProvider, schemaMap: CollectionSchemaMap, provenanceStoreFactory?: ProvenanceStoreFactory);
15
+ private readonly provenanceServiceFactory?;
16
+ constructor(translationProvider: TranslationProvider, schemaMap: CollectionSchemaMap, provenanceServiceFactory?: ProvenanceServiceFactory);
19
17
  handle(payload: Payload, input: TranslateDocumentInput): Promise<TranslateDocumentOutput>;
20
18
  private saveTranslatedDocument;
21
19
  }
@@ -1,17 +1,18 @@
1
1
  import { APIError } from "payload";
2
2
  import { translateContent } from "../../../core/translation-pipeline";
3
- import { computeSourceFingerprint } from "../../../core/content-projection/computeSourceFingerprint";
4
- import { fetchSourceDocument } from "../_lib/sourceDocument";
3
+ import { fetchSourceDocument } from "../../shared/payload/sourceDocument";
5
4
  /**
6
- * Translates a single document from source language to target language
5
+ * Translates a single document from source language to target language. Provenance is delegated to
6
+ * {@link ProvenanceService}: this handler only decides *when* to capture the source fingerprint
7
+ * (before the pipeline mutates the source in place) and *when* to record it (after the save).
7
8
  */ export class TranslateDocumentHandler {
8
9
  translationProvider;
9
10
  schemaMap;
10
- provenanceStoreFactory;
11
- constructor(translationProvider, schemaMap, provenanceStoreFactory){
11
+ provenanceServiceFactory;
12
+ constructor(translationProvider, schemaMap, provenanceServiceFactory){
12
13
  this.translationProvider = translationProvider;
13
14
  this.schemaMap = schemaMap;
14
- this.provenanceStoreFactory = provenanceStoreFactory;
15
+ this.provenanceServiceFactory = provenanceServiceFactory;
15
16
  }
16
17
  async handle(payload, input) {
17
18
  const { collection, collectionId, sourceLng, targetLng, strategy, publishOnTranslation } = input;
@@ -19,24 +20,12 @@ import { fetchSourceDocument } from "../_lib/sourceDocument";
19
20
  const schema = this.schemaMap.get(collection);
20
21
  if (!schema) throw new APIError(`Collection "${collection}" not found in schemaMap`, 400);
21
22
  const sourceData = await fetchSourceDocument(payload, collection, collectionId, sourceLng);
22
- // Capture the staleness baseline from the PRISTINE source NOW, before the pipeline runs. The
23
- // pipeline translates in place and shares object-valued source leaves (e.g. richText nodes) by
24
- // reference with `sourceData`, so fingerprinting after `translateContent` would hash the target
25
- // translation — making every fresh translation look immediately stale. Best-effort: a fingerprint
26
- // failure logs and skips provenance rather than breaking the translation.
27
- let sourceFingerprint = null;
28
- if (this.provenanceStoreFactory) {
29
- try {
30
- sourceFingerprint = computeSourceFingerprint(sourceData, schema);
31
- } catch (error) {
32
- payload.logger.error({
33
- err: error,
34
- collection,
35
- documentId: String(collectionId),
36
- msg: "translator: failed to fingerprint source for provenance"
37
- });
38
- }
39
- }
23
+ // Capture the staleness baseline from the PRISTINE source NOW, before the pipeline runs — it
24
+ // translates in place and shares object-valued source leaves (e.g. richText nodes) by reference,
25
+ // so fingerprinting after `translateContent` would hash the target translation and make every
26
+ // fresh translation look instantly stale. The service is best-effort (a failure returns null).
27
+ const provenance = this.provenanceServiceFactory?.(payload);
28
+ const sourceFingerprint = provenance?.captureFingerprint(collection, sourceData) ?? null;
40
29
  const targetData = await payload.findByID({
41
30
  collection,
42
31
  id: collectionId,
@@ -58,28 +47,13 @@ import { fetchSourceDocument } from "../_lib/sourceDocument";
58
47
  };
59
48
  const collectionConfig = payload.collections[collection].config;
60
49
  await this.saveTranslatedDocument(payload, collection, collectionId, translatedData, targetLng, sourceLng, collectionConfig, publishOnTranslation);
61
- if (this.provenanceStoreFactory && sourceFingerprint !== null) {
62
- const store = this.provenanceStoreFactory(payload);
63
- try {
64
- await store.upsert({
65
- collectionSlug: collection,
66
- documentId: String(collectionId),
67
- targetLocale: targetLng,
68
- sourceLocale: sourceLng,
69
- sourceFingerprint,
70
- translatedAt: new Date().toISOString(),
71
- dismissedFingerprint: null
72
- });
73
- } catch (error) {
74
- payload.logger.error({
75
- err: error,
76
- collection,
77
- documentId: String(collectionId),
78
- targetLocale: targetLng,
79
- sourceLocale: sourceLng,
80
- msg: "translator: failed to record translation provenance"
81
- });
82
- }
50
+ if (provenance && sourceFingerprint !== null) {
51
+ await provenance.record({
52
+ collectionSlug: collection,
53
+ documentId: String(collectionId),
54
+ targetLocale: targetLng,
55
+ sourceLocale: sourceLng
56
+ }, sourceFingerprint);
83
57
  }
84
58
  return {
85
59
  success: true
@@ -1,2 +1,3 @@
1
1
  export { TranslateDocumentHandler } from "./handler";
2
+ export { wireTranslateRunner } from "./wireTranslateRunner";
2
3
  export type { CollectionSchemaMap } from "../../../types/CollectionSchemaMap";
@@ -1,3 +1,4 @@
1
1
  export { TranslateDocumentHandler } from "./handler";
2
+ export { wireTranslateRunner } from "./wireTranslateRunner";
2
3
 
3
4
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,28 @@
1
+ import type { CollectionSlug } from "payload";
2
+ import type { TranslationProvider } from "../../../core/translation-providers";
3
+ import type { CollectionSchemaMap } from "../../../types/CollectionSchemaMap";
4
+ import type { ConfigModifier } from "../../../types/ConfigModifier";
5
+ import type { ProvenanceServiceFactory } from "../../modules/provenance";
6
+ import type { TranslationLifecycleCallbacks } from "../../modules/lifecycle";
7
+ import type { TaskRunnerFactory, TaskRunnerProvider } from "../../modules/task-runner";
8
+ type WireTranslateRunnerParams = {
9
+ translationProvider: TranslationProvider;
10
+ schemaMap: CollectionSchemaMap;
11
+ provenanceServiceFactory?: ProvenanceServiceFactory;
12
+ runner: TaskRunnerProvider;
13
+ lifecycle: TranslationLifecycleCallbacks;
14
+ collections: CollectionSlug[];
15
+ };
16
+ /**
17
+ * Assemble the document-translation task pipeline: the {@link TranslateDocumentHandler}, the runner
18
+ * context that wraps each task with lifecycle notifications, the runner's config modifier, and the
19
+ * per-request {@link TaskRunnerFactory} (decorated with `onQueued` notification when configured).
20
+ *
21
+ * Extracted from the plugin's `init()` so the composition root stays a flat list — `plugin.ts` calls
22
+ * this once and registers the returned `configModifier` through the shared builder.
23
+ */
24
+ export declare function wireTranslateRunner({ translationProvider, schemaMap, provenanceServiceFactory, runner, lifecycle, collections, }: WireTranslateRunnerParams): {
25
+ taskRunnerFactory: TaskRunnerFactory;
26
+ configModifier: ConfigModifier;
27
+ };
28
+ export {};
@@ -0,0 +1,49 @@
1
+ import { LifecycleNotifier, taskFromHandlerInput, withQueuedNotification } from "../../modules/lifecycle";
2
+ import { TranslateDocumentHandler } from "./handler";
3
+ /**
4
+ * Assemble the document-translation task pipeline: the {@link TranslateDocumentHandler}, the runner
5
+ * context that wraps each task with lifecycle notifications, the runner's config modifier, and the
6
+ * per-request {@link TaskRunnerFactory} (decorated with `onQueued` notification when configured).
7
+ *
8
+ * Extracted from the plugin's `init()` so the composition root stays a flat list — `plugin.ts` calls
9
+ * this once and registers the returned `configModifier` through the shared builder.
10
+ */ export function wireTranslateRunner({ translationProvider, schemaMap, provenanceServiceFactory, runner, lifecycle, collections }) {
11
+ const translateHandler = new TranslateDocumentHandler(translationProvider, schemaMap, provenanceServiceFactory);
12
+ const runnerContext = {
13
+ handler: async (payload, input)=>{
14
+ const notifier = new LifecycleNotifier(lifecycle, payload.logger);
15
+ const task = taskFromHandlerInput(input);
16
+ try {
17
+ await translateHandler.handle(payload, {
18
+ collection: input.collection,
19
+ collectionId: input.collectionId,
20
+ sourceLng: input.sourceLng,
21
+ targetLng: input.targetLng,
22
+ strategy: input.strategy,
23
+ publishOnTranslation: input.publishOnTranslation
24
+ });
25
+ } catch (error) {
26
+ await notifier.failed(task, error);
27
+ throw error; // rethrow so the runner marks the job failed
28
+ }
29
+ await notifier.completed(task);
30
+ },
31
+ collections
32
+ };
33
+ // Bind the context once so routes receive a self-sufficient factory: the runner needs no mutable
34
+ // per-instance handler state, and create() has no "configure() must run first" ordering coupling.
35
+ // When an `onQueued` callback is set, decorate the runner so `enqueue` fires it.
36
+ const taskRunnerFactory = {
37
+ create: (payload)=>{
38
+ const taskRunner = runner.create(payload, runnerContext.handler);
39
+ if (!lifecycle.onQueued) return taskRunner;
40
+ return withQueuedNotification(taskRunner, new LifecycleNotifier(lifecycle, payload.logger));
41
+ }
42
+ };
43
+ return {
44
+ taskRunnerFactory,
45
+ configModifier: runner.configure(runnerContext)
46
+ };
47
+ }
48
+
49
+ //# sourceMappingURL=wireTranslateRunner.js.map
@@ -1,14 +1,7 @@
1
1
  import type { CollectionConfig } from "payload";
2
+ import type { ManagedCollectionsConfig } from "./Provenance.shapes";
2
3
  /** Default slug for the provenance sidecar collection. Overridable via `provenance.slug`. */
3
4
  export declare const DEFAULT_PROVENANCE_SLUG = "translator-provenance";
4
- /**
5
- * True for the plugin's own provenance sidecar collection. Recognised by the {@link PROVENANCE_MARKER}
6
- * on `custom` (not the slug, which is consumer-configurable), so plugin wiring can stay idempotent on
7
- * a repeated run and the slug-collision guard can ignore an already-added sidecar.
8
- */
9
- export declare function isProvenanceCollection(collection: {
10
- custom?: unknown;
11
- }): boolean;
12
5
  /**
13
6
  * Build the Payload config for the provenance sidecar collection.
14
7
  *
@@ -18,6 +11,21 @@ export declare function isProvenanceCollection(collection: {
18
11
  * content. The composite index on `(collectionSlug, documentId, targetLocale)` is the upsert key.
19
12
  *
20
13
  * The plugin ships only this config; the consumer's Payload creates the table (see the design doc's
21
- * SQL-migration note). Enabled only when the consumer opts in via `provenance` (wired in slice B).
14
+ * SQL-migration note). Enabled only when the consumer opts in via `provenance`.
22
15
  */
23
16
  export declare function makeProvenanceCollection(slug?: string): CollectionConfig;
17
+ /**
18
+ * True for the plugin's own provenance sidecar collection. Recognised by the {@link PROVENANCE_MARKER}
19
+ * on `custom` (not the slug, which is consumer-configurable), so plugin wiring can stay idempotent on
20
+ * a repeated run and the slug-collision guard can ignore an already-added sidecar.
21
+ */
22
+ export declare function isProvenanceCollection(collection: {
23
+ custom?: unknown;
24
+ }): boolean;
25
+ /**
26
+ * Idempotently register the provenance sidecar collection on `host`: add it once, skipping when a
27
+ * sidecar with this slug is already present, so a repeated `init()` never stacks a duplicate. Takes
28
+ * only the narrow {@link ManagedCollectionsConfig} slice — a real Payload `Config` is structurally
29
+ * assignable, and a test passes a plain `{ collections: [...] }` literal.
30
+ */
31
+ export declare function ensureProvenanceCollectionRegistered(host: ManagedCollectionsConfig, slug: string): void;
@@ -1,12 +1,5 @@
1
1
  /** Default slug for the provenance sidecar collection. Overridable via `provenance.slug`. */ export const DEFAULT_PROVENANCE_SLUG = "translator-provenance";
2
2
  /** The `custom` marker tagging the plugin's own provenance sidecar (set + read in this module). */ const PROVENANCE_MARKER = "translatorProvenance";
3
- /**
4
- * True for the plugin's own provenance sidecar collection. Recognised by the {@link PROVENANCE_MARKER}
5
- * on `custom` (not the slug, which is consumer-configurable), so plugin wiring can stay idempotent on
6
- * a repeated run and the slug-collision guard can ignore an already-added sidecar.
7
- */ export function isProvenanceCollection(collection) {
8
- return collection.custom?.[PROVENANCE_MARKER] === true;
9
- }
10
3
  /**
11
4
  * Build the Payload config for the provenance sidecar collection.
12
5
  *
@@ -16,7 +9,7 @@
16
9
  * content. The composite index on `(collectionSlug, documentId, targetLocale)` is the upsert key.
17
10
  *
18
11
  * The plugin ships only this config; the consumer's Payload creates the table (see the design doc's
19
- * SQL-migration note). Enabled only when the consumer opts in via `provenance` (wired in slice B).
12
+ * SQL-migration note). Enabled only when the consumer opts in via `provenance`.
20
13
  */ export function makeProvenanceCollection(slug = DEFAULT_PROVENANCE_SLUG) {
21
14
  return {
22
15
  slug,
@@ -78,5 +71,26 @@
78
71
  ]
79
72
  };
80
73
  }
74
+ /**
75
+ * True for the plugin's own provenance sidecar collection. Recognised by the {@link PROVENANCE_MARKER}
76
+ * on `custom` (not the slug, which is consumer-configurable), so plugin wiring can stay idempotent on
77
+ * a repeated run and the slug-collision guard can ignore an already-added sidecar.
78
+ */ export function isProvenanceCollection(collection) {
79
+ return collection.custom?.[PROVENANCE_MARKER] === true;
80
+ }
81
+ /**
82
+ * Idempotently register the provenance sidecar collection on `host`: add it once, skipping when a
83
+ * sidecar with this slug is already present, so a repeated `init()` never stacks a duplicate. Takes
84
+ * only the narrow {@link ManagedCollectionsConfig} slice — a real Payload `Config` is structurally
85
+ * assignable, and a test passes a plain `{ collections: [...] }` literal.
86
+ */ export function ensureProvenanceCollectionRegistered(host, slug) {
87
+ const alreadyAdded = host.collections?.some((collection)=>collection.slug === slug && isProvenanceCollection(collection));
88
+ if (!alreadyAdded) {
89
+ host.collections = [
90
+ ...host.collections ?? [],
91
+ makeProvenanceCollection(slug)
92
+ ];
93
+ }
94
+ }
81
95
 
82
- //# sourceMappingURL=provenanceCollection.js.map
96
+ //# sourceMappingURL=Provenance.collection.js.map
@@ -0,0 +1,56 @@
1
+ import type { CollectionSlug, Payload } from "payload";
2
+ import type { ProvenanceKey, ProvenanceStore } from "../../../core/provenance";
3
+ import type { CollectionSchemaMap } from "../../../types/CollectionSchemaMap";
4
+ /** Per-locale staleness for one document (snake_case, matching the other translation endpoints). */
5
+ export type StalenessLocale = {
6
+ target_lng: string;
7
+ source_lng: string;
8
+ is_stale: boolean;
9
+ translated_at: string;
10
+ };
11
+ /** Builds a {@link ProvenanceService} bound to a Payload instance; absent when provenance is disabled. */
12
+ export type ProvenanceServiceFactory = (payload: Payload) => ProvenanceService;
13
+ /**
14
+ * The single owner of provenance fingerprint policy — how the source is hashed on write, re-hashed on
15
+ * read, and compared for staleness. The write path and the read path go through this one class, so
16
+ * they can never drift (the biggest correctness trap in staleness detection). Sits above the CRUD
17
+ * {@link ProvenanceStore} port; the port + `computeSourceFingerprint` + `isRecordStale` stay
18
+ * framework-agnostic in the core.
19
+ *
20
+ * Best-effort by contract: fingerprint/record failures log and no-op rather than failing a translation.
21
+ */
22
+ export declare class ProvenanceService {
23
+ private readonly payload;
24
+ private readonly store;
25
+ private readonly schemaMap;
26
+ constructor(payload: Payload, store: ProvenanceStore, schemaMap: CollectionSchemaMap);
27
+ /**
28
+ * Hash the PRISTINE source. The caller MUST pass source fetched **before** the translation pipeline
29
+ * runs — the pipeline mutates object-valued leaves (e.g. richText nodes) in place, so hashing after
30
+ * it would capture the target translation and make every fresh translation look instantly stale.
31
+ * Returns `null` on any failure (no schema, hashing error) so provenance is skipped, not the translation.
32
+ */
33
+ captureFingerprint(collection: CollectionSlug, sourceData: Record<string, unknown>): string | null;
34
+ /** Persist a translation receipt (best-effort; a store failure logs and no-ops). */
35
+ record(key: ProvenanceKey & {
36
+ sourceLocale: string;
37
+ }, sourceFingerprint: string): Promise<void>;
38
+ /**
39
+ * Per-locale staleness for one document: read every receipt, recompute the current source
40
+ * fingerprint (write-path-identical), and mark each locale stale on undismissed drift. Returns `[]`
41
+ * when the collection has no schema. Isolates per-locale failures so one bad record can't blank the rest.
42
+ */
43
+ getStaleness(collection: CollectionSlug, documentId: string): Promise<StalenessLocale[]>;
44
+ /**
45
+ * Acknowledge the current source drift for one target locale: persist the current fingerprint as the
46
+ * dismissed one, so the indicator hides until the source changes again. No-op when the collection has
47
+ * no schema or the locale has no record.
48
+ */
49
+ dismiss(key: ProvenanceKey): Promise<void>;
50
+ /**
51
+ * Recompute the current source fingerprint the same way the write path does (shared fetch shape +
52
+ * hash). Cached per source locale so a document translated from one source into N locales fetches
53
+ * the source once.
54
+ */
55
+ private makeCurrentFingerprint;
56
+ }
@@ -0,0 +1,124 @@
1
+ import { computeSourceFingerprint } from "../../../core/content-projection/computeSourceFingerprint";
2
+ import { isRecordStale } from "../../../core/provenance";
3
+ import { fetchSourceDocument } from "../../shared/payload/sourceDocument";
4
+ /**
5
+ * The single owner of provenance fingerprint policy — how the source is hashed on write, re-hashed on
6
+ * read, and compared for staleness. The write path and the read path go through this one class, so
7
+ * they can never drift (the biggest correctness trap in staleness detection). Sits above the CRUD
8
+ * {@link ProvenanceStore} port; the port + `computeSourceFingerprint` + `isRecordStale` stay
9
+ * framework-agnostic in the core.
10
+ *
11
+ * Best-effort by contract: fingerprint/record failures log and no-op rather than failing a translation.
12
+ */ export class ProvenanceService {
13
+ payload;
14
+ store;
15
+ schemaMap;
16
+ constructor(payload, store, schemaMap){
17
+ this.payload = payload;
18
+ this.store = store;
19
+ this.schemaMap = schemaMap;
20
+ }
21
+ /**
22
+ * Hash the PRISTINE source. The caller MUST pass source fetched **before** the translation pipeline
23
+ * runs — the pipeline mutates object-valued leaves (e.g. richText nodes) in place, so hashing after
24
+ * it would capture the target translation and make every fresh translation look instantly stale.
25
+ * Returns `null` on any failure (no schema, hashing error) so provenance is skipped, not the translation.
26
+ */ captureFingerprint(collection, sourceData) {
27
+ const schema = this.schemaMap.get(collection);
28
+ if (!schema) return null;
29
+ try {
30
+ return computeSourceFingerprint(sourceData, schema);
31
+ } catch (error) {
32
+ this.payload.logger.error({
33
+ err: error,
34
+ collection,
35
+ msg: "translator: failed to fingerprint source for provenance"
36
+ });
37
+ return null;
38
+ }
39
+ }
40
+ /** Persist a translation receipt (best-effort; a store failure logs and no-ops). */ async record(key, sourceFingerprint) {
41
+ try {
42
+ await this.store.upsert({
43
+ collectionSlug: key.collectionSlug,
44
+ documentId: key.documentId,
45
+ targetLocale: key.targetLocale,
46
+ sourceLocale: key.sourceLocale,
47
+ sourceFingerprint,
48
+ translatedAt: new Date().toISOString(),
49
+ dismissedFingerprint: null
50
+ });
51
+ } catch (error) {
52
+ this.payload.logger.error({
53
+ err: error,
54
+ collection: key.collectionSlug,
55
+ documentId: key.documentId,
56
+ targetLocale: key.targetLocale,
57
+ sourceLocale: key.sourceLocale,
58
+ msg: "translator: failed to record translation provenance"
59
+ });
60
+ }
61
+ }
62
+ /**
63
+ * Per-locale staleness for one document: read every receipt, recompute the current source
64
+ * fingerprint (write-path-identical), and mark each locale stale on undismissed drift. Returns `[]`
65
+ * when the collection has no schema. Isolates per-locale failures so one bad record can't blank the rest.
66
+ */ async getStaleness(collection, documentId) {
67
+ const schema = this.schemaMap.get(collection);
68
+ if (!schema) return [];
69
+ const records = await this.store.findByDocument(collection, documentId);
70
+ if (records.length === 0) return [];
71
+ const currentFingerprint = this.makeCurrentFingerprint(collection, documentId, schema);
72
+ const locales = [];
73
+ for (const record of records){
74
+ try {
75
+ const current = await currentFingerprint(record.sourceLocale);
76
+ locales.push({
77
+ target_lng: record.targetLocale,
78
+ source_lng: record.sourceLocale,
79
+ is_stale: isRecordStale(record, current),
80
+ translated_at: record.translatedAt
81
+ });
82
+ } catch (error) {
83
+ this.payload.logger.error({
84
+ err: error,
85
+ collection,
86
+ documentId,
87
+ targetLocale: record.targetLocale,
88
+ sourceLocale: record.sourceLocale,
89
+ msg: "translator: failed to compute staleness for locale"
90
+ });
91
+ }
92
+ }
93
+ return locales;
94
+ }
95
+ /**
96
+ * Acknowledge the current source drift for one target locale: persist the current fingerprint as the
97
+ * dismissed one, so the indicator hides until the source changes again. No-op when the collection has
98
+ * no schema or the locale has no record.
99
+ */ async dismiss(key) {
100
+ const schema = this.schemaMap.get(key.collectionSlug);
101
+ if (!schema) return;
102
+ const record = await this.store.find(key);
103
+ if (!record) return;
104
+ const currentFingerprint = this.makeCurrentFingerprint(key.collectionSlug, key.documentId, schema);
105
+ await this.store.dismiss(key, await currentFingerprint(record.sourceLocale));
106
+ }
107
+ /**
108
+ * Recompute the current source fingerprint the same way the write path does (shared fetch shape +
109
+ * hash). Cached per source locale so a document translated from one source into N locales fetches
110
+ * the source once.
111
+ */ makeCurrentFingerprint(collection, documentId, schema) {
112
+ const cache = new Map();
113
+ return async (sourceLocale)=>{
114
+ const cached = cache.get(sourceLocale);
115
+ if (cached !== undefined) return cached;
116
+ const sourceData = await fetchSourceDocument(this.payload, collection, documentId, sourceLocale);
117
+ const fingerprint = computeSourceFingerprint(sourceData, schema);
118
+ cache.set(sourceLocale, fingerprint);
119
+ return fingerprint;
120
+ };
121
+ }
122
+ }
123
+
124
+ //# sourceMappingURL=Provenance.service.js.map
@@ -0,0 +1,26 @@
1
+ import type { CollectionAfterDeleteHook } from "payload";
2
+ /**
3
+ * The minimal slice of a Payload collection that provenance's config-time wiring reads and mutates:
4
+ * its `slug`, the sidecar `custom` marker, and the `afterDelete` hook slot. A real `CollectionConfig`
5
+ * is **structurally assignable** to this — call sites pass the live collection with no adapter, and a
6
+ * test passes a plain `{ slug: "posts" }` literal. Keeps `injectProvenanceCleanup` /
7
+ * `ensureProvenanceCollectionRegistered` off the god-`Config`/`CollectionConfig` types.
8
+ *
9
+ * The only Payload type imported here is `CollectionAfterDeleteHook` — a framework callback contract
10
+ * that legitimately stays framework-typed.
11
+ */
12
+ export type ManagedCollectionEntry = {
13
+ slug: string;
14
+ custom?: unknown;
15
+ hooks?: {
16
+ afterDelete?: CollectionAfterDeleteHook[];
17
+ };
18
+ };
19
+ /**
20
+ * The minimal config host provenance's config-time wiring touches: just a mutable `collections`
21
+ * array. A real Payload `Config` plugs straight in (its `collections?: CollectionConfig[]` satisfies
22
+ * `ManagedCollectionEntry[]`).
23
+ */
24
+ export type ManagedCollectionsConfig = {
25
+ collections?: ManagedCollectionEntry[];
26
+ };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * The minimal config host provenance's config-time wiring touches: just a mutable `collections`
3
+ * array. A real Payload `Config` plugs straight in (its `collections?: CollectionConfig[]` satisfies
4
+ * `ManagedCollectionEntry[]`).
5
+ */ export { };
6
+
7
+ //# sourceMappingURL=Provenance.shapes.js.map
@@ -129,4 +129,4 @@ function toRecord(doc) {
129
129
  }
130
130
  }
131
131
 
132
- //# sourceMappingURL=PayloadProvenanceStore.js.map
132
+ //# sourceMappingURL=Provenance.store.js.map