@anthusai/papyrus 5.114.0 → 5.115.0-next.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.
@@ -2493,8 +2493,13 @@ const schema = a.schema({
2493
2493
  requestedAt: a.datetime(),
2494
2494
  runKey: a.string(),
2495
2495
  reason: a.string(),
2496
+ automationId: a.string(),
2497
+ publish: a.boolean(),
2496
2498
  updatedAt: a.datetime(),
2497
2499
  })
2500
+ .secondaryIndexes((index) => [
2501
+ index("state").sortKeys(["requestedAt"]).queryField("listLocalizationRequestsByStateAndRequestedAt"),
2502
+ ])
2498
2503
  .authorization((allow) => [
2499
2504
  allow.groups(contentWriteGroups),
2500
2505
  ]),
@@ -2652,8 +2652,13 @@ const schema = a.schema({
2652
2652
  requestedAt: a.datetime(),
2653
2653
  runKey: a.string(),
2654
2654
  reason: a.string(),
2655
+ automationId: a.string(),
2656
+ publish: a.boolean(),
2655
2657
  updatedAt: a.datetime(),
2656
2658
  })
2659
+ .secondaryIndexes((index) => [
2660
+ index("state").sortKeys(["requestedAt"]).queryField("listLocalizationRequestsByStateAndRequestedAt"),
2661
+ ])
2657
2662
  .authorization((allow) => [
2658
2663
  allow.groups(contentWriteGroups),
2659
2664
  ]),
@@ -2334,8 +2334,13 @@ function buildBaseDefinitions(excludedGroups = []) {
2334
2334
  requestedAt: a.datetime(),
2335
2335
  runKey: a.string(),
2336
2336
  reason: a.string(),
2337
+ automationId: a.string(),
2338
+ publish: a.boolean(),
2337
2339
  updatedAt: a.datetime(),
2338
2340
  })
2341
+ .secondaryIndexes((index) => [
2342
+ index("state").sortKeys(["requestedAt"]).queryField("listLocalizationRequestsByStateAndRequestedAt"),
2343
+ ])
2339
2344
  .authorization((allow) => [
2340
2345
  allow.groups(contentWriteGroups),
2341
2346
  ]),
@@ -2489,8 +2489,13 @@ const definitions = {
2489
2489
  requestedAt: a.datetime(),
2490
2490
  runKey: a.string(),
2491
2491
  reason: a.string(),
2492
+ automationId: a.string(),
2493
+ publish: a.boolean(),
2492
2494
  updatedAt: a.datetime(),
2493
2495
  })
2496
+ .secondaryIndexes((index) => [
2497
+ index("state").sortKeys(["requestedAt"]).queryField("listLocalizationRequestsByStateAndRequestedAt"),
2498
+ ])
2494
2499
  .authorization((allow) => [
2495
2500
  allow.groups(contentWriteGroups),
2496
2501
  ]),
@@ -48,6 +48,7 @@ from papyrus_content.item_deck_backfill import ensure_item_decks
48
48
  from papyrus_content.item_language_backfill import ensure_item_languages
49
49
  from papyrus_content.reference_intake import IntakeError, intake_email_submission_references, intake_references
50
50
  from papyrus_content.publish_alerts import PUBLISHED_TASK_KEY, announce_published
51
+ from papyrus_content.localize.dispatch import PUBLISH_TASK as PUBLISH_LOCALIZATION_TASK
51
52
  from papyrus_content.publishing import (
52
53
  ItemFields,
53
54
  PublishError,
@@ -201,7 +202,7 @@ def project_item_action(client, item_id: str, *, unpublished: bool, published_it
201
202
  is live, here when the projection succeeds, or by the scheduled retry (which carries the same marker) when it did not."""
202
203
  if not projection_configured():
203
204
  if published_item_id:
204
- announce_published(item_id, published_item_id)
205
+ announce_published_story(client, item_id, published_item_id)
205
206
  return {}
206
207
  liveness = {} if unpublished else {"live": False}
207
208
  task = {"task": "item", "itemId": item_id, "unpublished": unpublished}
@@ -238,7 +239,7 @@ def project_item_action(client, item_id: str, *, unpublished: bool, published_it
238
239
  if report.follow_up_item_pages:
239
240
  schedule_item_pages(item_id)
240
241
  if report.ok and published_item_id:
241
- announce_published(item_id, published_item_id)
242
+ announce_published_story(client, item_id, published_item_id)
242
243
  return response
243
244
 
244
245
 
@@ -477,7 +478,7 @@ def build_item_job(client, context, jobs, item_id: str, job_id: str | None, *, u
477
478
  jobs.finish(item_id, job_id, ok=report.ok, outcome=outcome)
478
479
  if report.ok and not unpublished and isinstance(published, dict):
479
480
  # A real publish (the CLI marks it, as does the newsroom's retry): the page is live, so tell the newsletter.
480
- announce_published(item_id, published.get("publishedItemId"))
481
+ announce_published_story(client, item_id, published.get("publishedItemId"))
481
482
  return report
482
483
 
483
484
 
@@ -644,6 +645,8 @@ def _run_projection_task(task: dict, lambda_context=None) -> dict:
644
645
  return run_page_refresh_rerender_task(client, task)
645
646
  elif task.get("task") == PUBLISH_SCHEDULED_TASK:
646
647
  return publish_scheduled_action(client, task)
648
+ elif task.get("task") == PUBLISH_LOCALIZATION_TASK:
649
+ return publish_localization_action(client, task)
647
650
  elif task.get("task") == "invalidate":
648
651
  invalidate = projection_context(client).invalidate
649
652
  if invalidate is None:
@@ -918,6 +921,49 @@ def schedule_item_action(arguments: dict, event: dict) -> dict:
918
921
  return run_schedule_operation(str(arguments.get("operation") or ""), arguments.get("arguments") or {}, service, caller_from_event(event))
919
922
 
920
923
 
924
+ def announce_published_story(client, item_id: str, published_item_id: str | None) -> None:
925
+ """A Story is live: tell the newsletter and the notifications function, and ask for its translations when the site translates on publish
926
+ (``languages.auto``; docs/story-translation-procedure.md). Neither can fail the publish, which is already done. A translation request that could
927
+ not be written is logged and left for ``papyrus stories localize --all``, which finds every Story with no up-to-date translation."""
928
+ announce_published(item_id, published_item_id)
929
+ try:
930
+ from papyrus_content.localize.auto import request_translations
931
+
932
+ requested = request_translations(client, item_id)
933
+ if requested["requested"]:
934
+ print(json.dumps({"event": "papyrus_translations_requested", "itemId": item_id, "requests": requested["requested"]}))
935
+ except Exception as error: # noqa: BLE001 - the publish stands; the log names the cause and the backfill command repairs it
936
+ print(json.dumps({"event": "papyrus_translation_request_failed", "itemId": item_id, "error": f"{type(error).__name__}: {error}"[:500]}))
937
+
938
+
939
+ def publish_localization_action(client, task: dict) -> dict:
940
+ """The dispatcher's ``publish-localization`` task: publish the translation a run saved cleanly, through the ordinary publish path (``publish_item_action``).
941
+
942
+ ``localize.publish.publishable`` decides (language enabled, the original still published at this very text, the run saved it cleanly, not live
943
+ already). A refusal of the publish itself (the site's approval gate, an unrendered page) is recorded on the request and is not retried: retrying
944
+ cannot change a decision a person has to make. The language's public gate is the projection's: a language that is not public gets no page."""
945
+ from papyrus_content.editorial_checks import settings_from
946
+ from papyrus_content.localize import desired_state
947
+ from papyrus_content.localize.publish import publishable
948
+
949
+ request = client.get_record("LocalizationRequest", str(task["requestId"]))
950
+ if request is None or request.get("publish") is not True:
951
+ return {"ok": True, "published": False, "reason": "no request asks for this translation to be published"}
952
+ decision = publishable(client, request, settings_from(GraphQLEditorialStore(execute=client.graphql, tenant_id=getattr(client, "tenant_id", None))))
953
+ if decision.version is None:
954
+ return {"ok": True, "published": False, "reason": decision.reason}
955
+ if decision.done:
956
+ desired_state.record_outcome(client, request, state="published", run_key=request.get("runKey"))
957
+ return {"ok": True, "published": True, "alreadyLive": True}
958
+ try:
959
+ result = publish_item_action({"id": decision.version["id"], "async": True}, "AI (translation on publish)")
960
+ except PublishError as error:
961
+ client.update_record("LocalizationRequest", {"id": request["id"], "reason": f"not published [{error.code}]: {error.message}"[:1000]})
962
+ return {"ok": True, "published": False, "reason": error.message, "code": error.code}
963
+ desired_state.record_outcome(client, request, state="published", run_key=request.get("runKey"))
964
+ return {"ok": True, "published": bool(result.get("ok")), "versionId": decision.version["id"]}
965
+
966
+
921
967
  def publish_scheduled_action(client, task: dict) -> dict:
922
968
  """The Scheduler's event at the scheduled moment: claim the schedule once, then publish its pinned version by the normal publish path (see story_schedule.py).
923
969
  A refusal of the publish (an open checklist, an unrendered page) fails the run so Lambda retries it and then sends it to the dead letter queue; the version stays
@@ -4,5 +4,5 @@ anthus-markus==0.7.0
4
4
  latex2mathml>=3.78,<4
5
5
  Pillow>=11.3,<12
6
6
  awscrt>=0.23,<1
7
- humanus>=0.36,<0.37
7
+ humanus>=0.37,<0.38
8
8
  pypdf>=6.12.1,<7
@@ -7,7 +7,7 @@ import { fileURLToPath } from "node:url";
7
7
  import { pythonLambdaCode } from "../shared/python-bundle.js";
8
8
  const dirname = path.dirname(fileURLToPath(import.meta.url));
9
9
  const projectRoot = path.resolve(dirname, "../../..");
10
- export const contentActionsRequirements = ["PyYAML>=6.0.2,<7", "citeproc-py>=0.8.2,<1", "anthus-markus==0.7.0", "latex2mathml>=3.78,<4", "Pillow>=11.3,<12", "awscrt>=0.23,<1", "humanus>=0.36,<0.37", "pypdf>=6.12.1,<7"];
10
+ export const contentActionsRequirements = ["PyYAML>=6.0.2,<7", "citeproc-py>=0.8.2,<1", "anthus-markus==0.7.0", "latex2mathml>=3.78,<4", "Pillow>=11.3,<12", "awscrt>=0.23,<1", "humanus>=0.37,<0.38", "pypdf>=6.12.1,<7"];
11
11
  function installCheckoutRequirements(outputDir) {
12
12
  const requirementsPath = path.join(projectRoot, "amplify/functions/content-actions/requirements.txt");
13
13
  execSync(`python3 -m pip install -r "${requirementsPath}" -t "${outputDir}" --no-cache-dir --platform manylinux2014_aarch64 --implementation cp --python-version 3.12 --only-binary=:all: --ignore-requires-python`, { cwd: projectRoot, stdio: "inherit" });
@@ -10,7 +10,7 @@ import { pythonLambdaCode, type SiteBundleFile } from "../shared/python-bundle";
10
10
  const dirname = path.dirname(fileURLToPath(import.meta.url));
11
11
  const projectRoot = path.resolve(dirname, "../../..");
12
12
 
13
- export const contentActionsRequirements = ["PyYAML>=6.0.2,<7", "citeproc-py>=0.8.2,<1", "anthus-markus==0.7.0", "latex2mathml>=3.78,<4", "Pillow>=11.3,<12", "awscrt>=0.23,<1", "humanus>=0.36,<0.37", "pypdf>=6.12.1,<7"];
13
+ export const contentActionsRequirements = ["PyYAML>=6.0.2,<7", "citeproc-py>=0.8.2,<1", "anthus-markus==0.7.0", "latex2mathml>=3.78,<4", "Pillow>=11.3,<12", "awscrt>=0.23,<1", "humanus>=0.37,<0.38", "pypdf>=6.12.1,<7"];
14
14
 
15
15
  function installCheckoutRequirements(outputDir: string): void {
16
16
  const requirementsPath = path.join(projectRoot, "amplify/functions/content-actions/requirements.txt");
@@ -1 +1 @@
1
- humanus[jev]>=0.36,<0.37
1
+ humanus[jev]>=0.37,<0.38
@@ -9,7 +9,7 @@ const functionDirectory = path.dirname(fileURLToPath(import.meta.url));
9
9
  /** AppSync gives a resolver 30 seconds; Humanus's classifier deadline (settings, default 20 s) keeps a check inside this. */
10
10
  export const EDITORIAL_CHECKS_TIMEOUT_SECONDS = 28;
11
11
  /** Humanus with the Jev classifier lane; papyrus_content.editorial_checks does the work. */
12
- export const editorialChecksRequirements = ["humanus[jev]>=0.36,<0.37"];
12
+ export const editorialChecksRequirements = ["humanus[jev]>=0.37,<0.38"];
13
13
  function installRequirements(outputDir) {
14
14
  execFileSync("python3", [
15
15
  "-m", "pip", "install", "-r", path.join(functionDirectory, "requirements.txt"), "-t", outputDir, "--no-cache-dir",
@@ -13,7 +13,7 @@ const functionDirectory = path.dirname(fileURLToPath(import.meta.url));
13
13
  export const EDITORIAL_CHECKS_TIMEOUT_SECONDS = 28;
14
14
 
15
15
  /** Humanus with the Jev classifier lane; papyrus_content.editorial_checks does the work. */
16
- export const editorialChecksRequirements = ["humanus[jev]>=0.36,<0.37"];
16
+ export const editorialChecksRequirements = ["humanus[jev]>=0.37,<0.38"];
17
17
 
18
18
  function installRequirements(outputDir: string): void {
19
19
  execFileSync(
@@ -0,0 +1,45 @@
1
+ """The translate-on-publish dispatcher: a DynamoDB stream consumer on the LocalizationRequest table (docs/story-translation-procedure.md).
2
+
3
+ All the logic is ``papyrus_content.localize.dispatch``; this file decodes the stream records and builds the collaborators with the function's own role:
4
+ the site's authoring client, the monitoring table (as the automation-view function reads and writes it), the procedure runner and the content-actions
5
+ function. A batch whose handling fails reports every record as failed, so Lambda retries it (the stream's retry limit and the function's dead letter queue
6
+ follow); the work itself is idempotent (a claim per record, a publish that finds the translation live).
7
+ """
8
+ from __future__ import annotations
9
+
10
+ import json
11
+ import os
12
+ from typing import Any
13
+
14
+ from papyrus_content.localize.dispatch import decode_image, handle_changes
15
+
16
+ CONTENT_ACTIONS_ENV = "PAPYRUS_CONTENT_ACTIONS_FUNCTION"
17
+
18
+
19
+ def invoke_content_actions(task: dict[str, Any]) -> None:
20
+ import boto3
21
+
22
+ boto3.client("lambda").invoke(FunctionName=os.environ[CONTENT_ACTIONS_ENV], InvocationType="Event",
23
+ Payload=json.dumps({"projectionTask": task}).encode("utf-8"))
24
+
25
+
26
+ def handler(event: dict[str, Any], context: Any = None, *, client=None, table=None, runner=None, publisher=None) -> dict[str, Any]:
27
+ from papyrus_content.automations import MonitoringReader, MonitoringWriter, lambda_invoker, monitoring_table_from_environment
28
+ from papyrus_content.graphql_authoring import PapyrusGraphQLAuthoringClient
29
+
30
+ images = [{"old": decode_image(record["dynamodb"].get("OldImage")), "new": decode_image(record["dynamodb"].get("NewImage"))}
31
+ for record in event.get("Records", []) if record.get("eventName") in ("INSERT", "MODIFY")]
32
+ if not images:
33
+ return {"batchItemFailures": []}
34
+ os.environ.setdefault("PAPYRUS_GRAPHQL_AUTH", "iam") # the function's own role signs the calls
35
+ endpoint = os.environ.get("PAPYRUS_GRAPHQL_ENDPOINT")
36
+ table = table if table is not None else monitoring_table_from_environment(os.environ)
37
+ try:
38
+ handle_changes(images, client or PapyrusGraphQLAuthoringClient(endpoint=endpoint),
39
+ reader=MonitoringReader(table) if table is not None else None, writer=MonitoringWriter(table) if table is not None else None,
40
+ invoke=runner or lambda_invoker(os.environ.get("PROCEDURE_RUNNER_FUNCTION")), invoke_publish=publisher or invoke_content_actions,
41
+ site_endpoint=endpoint)
42
+ except Exception as error:
43
+ print(json.dumps({"event": "papyrus_localization_dispatch_failed", "error": f"{type(error).__name__}: {error}"[:500]}))
44
+ return {"batchItemFailures": [{"itemIdentifier": record["dynamodb"]["SequenceNumber"]} for record in event["Records"]]}
45
+ return {"batchItemFailures": []}
@@ -0,0 +1,103 @@
1
+ import { Duration } from "aws-cdk-lib";
2
+ import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
3
+ import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
4
+ import { Architecture, CfnEventSourceMapping, Function as LambdaFunction, Runtime } from "aws-cdk-lib/aws-lambda";
5
+ import { pythonLambdaCode } from "../shared/python-bundle.js";
6
+ import { connectToSiteApi } from "../../site-api-access.js";
7
+ import { AUTOMATION_RUN_NOW_LEADING_KEYS, AUTOMATION_RUN_NOW_TABLE_ACTIONS, PROCEDURE_RUNNER_FUNCTION_NAME } from "../automation-view/wire.js";
8
+ import { MONITORING_TABLE_PARAMETER, MONITORING_VIEW_TABLE_ACTIONS, monitoringViewParameterArn, monitoringViewTableArns } from "../monitoring-view/wire.js";
9
+ /**
10
+ * Translate on publish (docs/story-translation-procedure.md, "Translating on publish"): a DynamoDB Streams consumer on the LocalizationRequest table.
11
+ * The publish path writes one desired-state record per enabled language (`languages.auto`); this function reacts to each change of those records: it
12
+ * starts the waiting runs through the Automation they name (the same code as Run now) and asks the content-actions function to publish a translation a
13
+ * run saved cleanly. It calls no model and publishes nothing itself.
14
+ *
15
+ * What it may do, and nothing else: read and update LocalizationRequest rows and read Automation records of its own site's API; read the monitoring
16
+ * table and write one Run now row in a monitor's partition (the automation-view grant); start the procedure runner; invoke the content-actions function.
17
+ * A shared stack (one backend for several tenants) is not wired: its rows carry a tenant the dispatcher does not act for (docs/multi-tenancy.md).
18
+ */
19
+ export const DISPATCH_API_FIELDS = [
20
+ "Query/fields/getAutomation",
21
+ "Query/fields/getLocalizationRequest",
22
+ "Query/fields/listLocalizationRequestsByStateAndRequestedAt",
23
+ "Mutation/fields/updateLocalizationRequest",
24
+ ];
25
+ const STREAM_READ_ACTIONS = ["dynamodb:DescribeStream", "dynamodb:GetRecords", "dynamodb:GetShardIterator", "dynamodb:ListStreams"];
26
+ export const CONTENT_ACTIONS_ENV = "PAPYRUS_CONTENT_ACTIONS_FUNCTION";
27
+ /** The LocalizationRequest table's stream, with both images (the dispatcher compares them to tell a fresh request from its own put-back). */
28
+ function localizationRequestStream(backend) {
29
+ const cfnResources = backend.data.resources.cfnResources;
30
+ const wrapper = cfnResources.amplifyDynamoDbTables?.LocalizationRequest;
31
+ if (wrapper !== undefined) {
32
+ wrapper.streamSpecification = { streamViewType: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES };
33
+ return wrapper.resource.getAtt("TableStreamArn").toString();
34
+ }
35
+ const cfnTables = cfnResources.cfnTables ?? {};
36
+ const found = cfnTables.LocalizationRequest ??
37
+ cfnTables.LocalizationRequestTable ??
38
+ Object.entries(cfnTables).find(([key]) => key.toLowerCase().replace(/table$/, "") === "localizationrequest")?.[1];
39
+ if (!found)
40
+ throw new Error(`Translating on publish needs the LocalizationRequest table. cfnTables=${Object.keys(cfnTables).join(",")}`);
41
+ found.streamSpecification = { streamViewType: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES };
42
+ return found.attrStreamArn;
43
+ }
44
+ /** Wires the dispatcher, or nothing when the site's schema leaves out the localization models (`excludeModelGroups`: no table, nothing to dispatch). */
45
+ export function wireLocalizationDispatch(backend, contentActionsLambda) {
46
+ if (!backend.data.resources.tables.LocalizationRequest)
47
+ return undefined;
48
+ const streamArn = localizationRequestStream(backend);
49
+ const stack = backend.createStack("localization-dispatch");
50
+ const dispatcher = new LambdaFunction(stack, "LocalizationDispatcher", {
51
+ runtime: Runtime.PYTHON_3_12,
52
+ architecture: Architecture.ARM_64,
53
+ handler: "handler.handler",
54
+ timeout: Duration.seconds(60),
55
+ memorySize: 256,
56
+ code: pythonLambdaCode({ functionDir: "localization-dispatch", modules: ["papyrus_content"], noDeps: true }, [
57
+ "bash",
58
+ "-c",
59
+ [
60
+ "set -euo pipefail",
61
+ "mkdir -p /asset-output/papyrus_content",
62
+ "cp amplify/functions/localization-dispatch/handler.py /asset-output/handler.py",
63
+ "cp -R src/papyrus_content/. /asset-output/papyrus_content/",
64
+ ].join(" && "),
65
+ ]),
66
+ });
67
+ connectToSiteApi(dispatcher, backend.data.resources.cfnResources.cfnGraphqlApi.attrGraphQlUrl);
68
+ dispatcher.addEnvironment("PAPYRUS_MONITORING_TABLE_PARAMETER", MONITORING_TABLE_PARAMETER);
69
+ dispatcher.addEnvironment("PROCEDURE_RUNNER_FUNCTION", PROCEDURE_RUNNER_FUNCTION_NAME);
70
+ dispatcher.addEnvironment(CONTENT_ACTIONS_ENV, contentActionsLambda.functionName);
71
+ const apiId = backend.data.resources.graphqlApi.apiId;
72
+ const region = stack.region;
73
+ const account = stack.account;
74
+ dispatcher.addToRolePolicy(new PolicyStatement({
75
+ effect: Effect.ALLOW,
76
+ actions: ["appsync:GraphQL"],
77
+ resources: DISPATCH_API_FIELDS.map((field) => `arn:aws:appsync:${region}:${account}:apis/${apiId}/types/${field}`),
78
+ }));
79
+ dispatcher.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: ["ssm:GetParameter"], resources: [monitoringViewParameterArn(region, account)] }));
80
+ dispatcher.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: MONITORING_VIEW_TABLE_ACTIONS, resources: monitoringViewTableArns(region, account) }));
81
+ dispatcher.addToRolePolicy(new PolicyStatement({
82
+ effect: Effect.ALLOW,
83
+ actions: AUTOMATION_RUN_NOW_TABLE_ACTIONS,
84
+ resources: monitoringViewTableArns(region, account).slice(0, 1),
85
+ conditions: { "ForAllValues:StringLike": { "dynamodb:LeadingKeys": AUTOMATION_RUN_NOW_LEADING_KEYS } },
86
+ }));
87
+ dispatcher.addToRolePolicy(new PolicyStatement({
88
+ effect: Effect.ALLOW,
89
+ actions: ["lambda:InvokeFunction"],
90
+ resources: [`arn:aws:lambda:${region}:${account}:function:${PROCEDURE_RUNNER_FUNCTION_NAME}`, contentActionsLambda.functionArn],
91
+ }));
92
+ dispatcher.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: STREAM_READ_ACTIONS, resources: [streamArn] }));
93
+ new CfnEventSourceMapping(stack, "LocalizationDispatcherStreamMapping", {
94
+ eventSourceArn: streamArn,
95
+ functionName: dispatcher.functionArn,
96
+ startingPosition: "LATEST",
97
+ batchSize: 10,
98
+ maximumBatchingWindowInSeconds: 2,
99
+ maximumRetryAttempts: 5,
100
+ functionResponseTypes: ["ReportBatchItemFailures"],
101
+ });
102
+ return dispatcher;
103
+ }
@@ -0,0 +1,114 @@
1
+ import { Duration } from "aws-cdk-lib";
2
+ import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
3
+ import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
4
+ import { Architecture, CfnEventSourceMapping, Function as LambdaFunction, Runtime } from "aws-cdk-lib/aws-lambda";
5
+ import { pythonLambdaCode } from "../shared/python-bundle";
6
+ import { connectToSiteApi } from "../../site-api-access";
7
+ import { AUTOMATION_RUN_NOW_LEADING_KEYS, AUTOMATION_RUN_NOW_TABLE_ACTIONS, PROCEDURE_RUNNER_FUNCTION_NAME } from "../automation-view/wire";
8
+ import { MONITORING_TABLE_PARAMETER, MONITORING_VIEW_TABLE_ACTIONS, monitoringViewParameterArn, monitoringViewTableArns } from "../monitoring-view/wire";
9
+
10
+ /**
11
+ * Translate on publish (docs/story-translation-procedure.md, "Translating on publish"): a DynamoDB Streams consumer on the LocalizationRequest table.
12
+ * The publish path writes one desired-state record per enabled language (`languages.auto`); this function reacts to each change of those records: it
13
+ * starts the waiting runs through the Automation they name (the same code as Run now) and asks the content-actions function to publish a translation a
14
+ * run saved cleanly. It calls no model and publishes nothing itself.
15
+ *
16
+ * What it may do, and nothing else: read and update LocalizationRequest rows and read Automation records of its own site's API; read the monitoring
17
+ * table and write one Run now row in a monitor's partition (the automation-view grant); start the procedure runner; invoke the content-actions function.
18
+ * A shared stack (one backend for several tenants) is not wired: its rows carry a tenant the dispatcher does not act for (docs/multi-tenancy.md).
19
+ */
20
+ export const DISPATCH_API_FIELDS = [
21
+ "Query/fields/getAutomation",
22
+ "Query/fields/getLocalizationRequest",
23
+ "Query/fields/listLocalizationRequestsByStateAndRequestedAt",
24
+ "Mutation/fields/updateLocalizationRequest",
25
+ ];
26
+ const STREAM_READ_ACTIONS = ["dynamodb:DescribeStream", "dynamodb:GetRecords", "dynamodb:GetShardIterator", "dynamodb:ListStreams"];
27
+ export const CONTENT_ACTIONS_ENV = "PAPYRUS_CONTENT_ACTIONS_FUNCTION";
28
+
29
+ /** The LocalizationRequest table's stream, with both images (the dispatcher compares them to tell a fresh request from its own put-back). */
30
+ function localizationRequestStream(backend: any): string {
31
+ const cfnResources = backend.data.resources.cfnResources;
32
+ const wrapper = cfnResources.amplifyDynamoDbTables?.LocalizationRequest;
33
+ if (wrapper !== undefined) {
34
+ wrapper.streamSpecification = { streamViewType: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES };
35
+ return wrapper.resource.getAtt("TableStreamArn").toString();
36
+ }
37
+ const cfnTables = cfnResources.cfnTables ?? {};
38
+ const found =
39
+ cfnTables.LocalizationRequest ??
40
+ cfnTables.LocalizationRequestTable ??
41
+ Object.entries(cfnTables).find(([key]) => key.toLowerCase().replace(/table$/, "") === "localizationrequest")?.[1];
42
+ if (!found) throw new Error(`Translating on publish needs the LocalizationRequest table. cfnTables=${Object.keys(cfnTables).join(",")}`);
43
+ found.streamSpecification = { streamViewType: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES };
44
+ return found.attrStreamArn;
45
+ }
46
+
47
+ /** Wires the dispatcher, or nothing when the site's schema leaves out the localization models (`excludeModelGroups`: no table, nothing to dispatch). */
48
+ export function wireLocalizationDispatch(backend: any, contentActionsLambda: LambdaFunction): LambdaFunction | undefined {
49
+ if (!backend.data.resources.tables.LocalizationRequest) return undefined;
50
+ const streamArn = localizationRequestStream(backend);
51
+ const stack = backend.createStack("localization-dispatch");
52
+ const dispatcher = new LambdaFunction(stack, "LocalizationDispatcher", {
53
+ runtime: Runtime.PYTHON_3_12,
54
+ architecture: Architecture.ARM_64,
55
+ handler: "handler.handler",
56
+ timeout: Duration.seconds(60),
57
+ memorySize: 256,
58
+ code: pythonLambdaCode(
59
+ { functionDir: "localization-dispatch", modules: ["papyrus_content"], noDeps: true },
60
+ [
61
+ "bash",
62
+ "-c",
63
+ [
64
+ "set -euo pipefail",
65
+ "mkdir -p /asset-output/papyrus_content",
66
+ "cp amplify/functions/localization-dispatch/handler.py /asset-output/handler.py",
67
+ "cp -R src/papyrus_content/. /asset-output/papyrus_content/",
68
+ ].join(" && "),
69
+ ],
70
+ ),
71
+ });
72
+ connectToSiteApi(dispatcher, backend.data.resources.cfnResources.cfnGraphqlApi.attrGraphQlUrl as string);
73
+ dispatcher.addEnvironment("PAPYRUS_MONITORING_TABLE_PARAMETER", MONITORING_TABLE_PARAMETER);
74
+ dispatcher.addEnvironment("PROCEDURE_RUNNER_FUNCTION", PROCEDURE_RUNNER_FUNCTION_NAME);
75
+ dispatcher.addEnvironment(CONTENT_ACTIONS_ENV, contentActionsLambda.functionName);
76
+ const apiId = backend.data.resources.graphqlApi.apiId as string;
77
+ const region = stack.region;
78
+ const account = stack.account;
79
+ dispatcher.addToRolePolicy(
80
+ new PolicyStatement({
81
+ effect: Effect.ALLOW,
82
+ actions: ["appsync:GraphQL"],
83
+ resources: DISPATCH_API_FIELDS.map((field) => `arn:aws:appsync:${region}:${account}:apis/${apiId}/types/${field}`),
84
+ }),
85
+ );
86
+ dispatcher.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: ["ssm:GetParameter"], resources: [monitoringViewParameterArn(region, account)] }));
87
+ dispatcher.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: MONITORING_VIEW_TABLE_ACTIONS, resources: monitoringViewTableArns(region, account) }));
88
+ dispatcher.addToRolePolicy(
89
+ new PolicyStatement({
90
+ effect: Effect.ALLOW,
91
+ actions: AUTOMATION_RUN_NOW_TABLE_ACTIONS,
92
+ resources: monitoringViewTableArns(region, account).slice(0, 1),
93
+ conditions: { "ForAllValues:StringLike": { "dynamodb:LeadingKeys": AUTOMATION_RUN_NOW_LEADING_KEYS } },
94
+ }),
95
+ );
96
+ dispatcher.addToRolePolicy(
97
+ new PolicyStatement({
98
+ effect: Effect.ALLOW,
99
+ actions: ["lambda:InvokeFunction"],
100
+ resources: [`arn:aws:lambda:${region}:${account}:function:${PROCEDURE_RUNNER_FUNCTION_NAME}`, contentActionsLambda.functionArn],
101
+ }),
102
+ );
103
+ dispatcher.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: STREAM_READ_ACTIONS, resources: [streamArn] }));
104
+ new CfnEventSourceMapping(stack, "LocalizationDispatcherStreamMapping", {
105
+ eventSourceArn: streamArn,
106
+ functionName: dispatcher.functionArn,
107
+ startingPosition: "LATEST",
108
+ batchSize: 10,
109
+ maximumBatchingWindowInSeconds: 2,
110
+ maximumRetryAttempts: 5,
111
+ functionResponseTypes: ["ReportBatchItemFailures"],
112
+ });
113
+ return dispatcher;
114
+ }
@@ -39,6 +39,7 @@ import { automationView } from "./functions/automation-view/resource.js";
39
39
  import { imageService } from "./functions/image-service/resource.js";
40
40
  import { wireImageService } from "./functions/image-service/wire.js";
41
41
  import { wireAutomationView } from "./functions/automation-view/wire.js";
42
+ import { wireLocalizationDispatch } from "./functions/localization-dispatch/wire.js";
42
43
  import { announcements } from "./functions/announcements/resource.js";
43
44
  import { wireAnnouncements } from "./functions/announcements/wire.js";
44
45
  import { proposalActions } from "./functions/proposal-actions/resource.js";
@@ -769,6 +770,9 @@ export function defineSiteBackend(papyrusSite, options = {}) {
769
770
  applySiteBackendCostTags(backend.stack, costAttribution);
770
771
  wireMonitoringView(backend, emailMonitorId);
771
772
  wireAutomationView(backend);
773
+ // Translate on publish (languages.auto): a stream consumer on the LocalizationRequest records. A shared stack's rows carry a tenant it does not act for.
774
+ if (tenancyMode !== "shared")
775
+ wireLocalizationDispatch(backend, contentActionsLambda);
772
776
  wireImageService(backend);
773
777
  wireProposalActions(backend);
774
778
  // Announcements are off until the owner turns them on; the parameters and the lease are per site (the brand id names the site).
@@ -46,6 +46,7 @@ import { automationView } from "./functions/automation-view/resource";
46
46
  import { imageService } from "./functions/image-service/resource";
47
47
  import { wireImageService } from "./functions/image-service/wire";
48
48
  import { wireAutomationView } from "./functions/automation-view/wire";
49
+ import { wireLocalizationDispatch } from "./functions/localization-dispatch/wire";
49
50
  import { announcements } from "./functions/announcements/resource";
50
51
  import { wireAnnouncements } from "./functions/announcements/wire";
51
52
  import { proposalActions } from "./functions/proposal-actions/resource";
@@ -929,6 +930,8 @@ export function defineSiteBackend(papyrusSite: PapyrusSite, options: DefineSiteB
929
930
  if (costAttribution && sharedTier) applySiteBackendCostTags(backend.stack, costAttribution);
930
931
  wireMonitoringView(backend, emailMonitorId);
931
932
  wireAutomationView(backend);
933
+ // Translate on publish (languages.auto): a stream consumer on the LocalizationRequest records. A shared stack's rows carry a tenant it does not act for.
934
+ if (tenancyMode !== "shared") wireLocalizationDispatch(backend, contentActionsLambda);
932
935
  wireImageService(backend);
933
936
  wireProposalActions(backend);
934
937
  // Announcements are off until the owner turns them on; the parameters and the lease are per site (the brand id names the site).
package/package.json CHANGED
@@ -640,7 +640,7 @@
640
640
  "optional": true
641
641
  }
642
642
  },
643
- "version": "5.114.0",
643
+ "version": "5.115.0-next.2",
644
644
  "dependencies": {
645
645
  "@aws-amplify/adapter-nextjs": "^1.7.3",
646
646
  "@aws-crypto/sha256-js": "5.2.0",
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "5.114.0",
2
+ "version": "5.115.0-next.2",
3
3
  "routes": [
4
4
  {
5
5
  "file": "app/%5F_preview/[[...path]]/route.ts",