@anthusai/papyrus 5.118.0 → 5.120.0-next.1

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.
@@ -2152,6 +2152,8 @@ const schema = a.schema({
2152
2152
  categoryKey: a.string(),
2153
2153
  displayName: a.string(),
2154
2154
  description: a.string(),
2155
+ // The language this node's own text (name, description, aliases) is written in: the site's primary language, written on every row.
2156
+ language: a.string(),
2155
2157
  aliases: a.string().array(),
2156
2158
  authorityScore: a.float(),
2157
2159
  authorityRank: a.integer(),
@@ -2178,6 +2180,44 @@ const schema = a.schema({
2178
2180
  .authorization((allow) => [
2179
2181
  allow.groups(categoryWriteGroups).to(["read"]),
2180
2182
  ]),
2183
+ /** One name of one concept in one language (docs/multilingual-labels.md). The Concept (SemanticNode) keeps its identity and its edges; each language's
2184
+ * preferred name, other names and short description are rows here. entityKey `{entityKind}#{entityId}`; id `{entityKey}#{language}` (preferred) or `...#alias#{folded text}`; `nameKey`
2185
+ * is `{language}#{folded text}` and finds a concept by name with one query; `workKey` (`{language}#{syncState}`) is present only while a label needs work. */
2186
+ EntityLabel: a
2187
+ .model({
2188
+ id: a.id().required(),
2189
+ tenantId: a.string(),
2190
+ entityKind: a.string().required(),
2191
+ entityId: a.id().required(),
2192
+ entityKey: a.string().required(),
2193
+ language: a.string().required(),
2194
+ role: a.string().required(),
2195
+ text: a.string().required(),
2196
+ textLanguage: a.string(),
2197
+ nameKey: a.string().required(),
2198
+ sortKey: a.string().required(),
2199
+ description: a.string(),
2200
+ equivalence: a.string(),
2201
+ origin: a.string(),
2202
+ pinned: a.boolean(),
2203
+ reviewedBy: a.string(),
2204
+ reviewedAt: a.datetime(),
2205
+ sourceContentHash: a.string(),
2206
+ syncState: a.string(),
2207
+ workKey: a.string(),
2208
+ slug: a.string(),
2209
+ provenanceId: a.string(),
2210
+ createdAt: a.datetime(),
2211
+ updatedAt: a.datetime(),
2212
+ })
2213
+ .secondaryIndexes((index) => [
2214
+ index("entityKey").sortKeys(["sortKey"]).queryField("listEntityLabelsByEntityAndSortKey"),
2215
+ index("nameKey").queryField("listEntityLabelsByNameKey"),
2216
+ index("workKey").sortKeys(["updatedAt"]).queryField("listEntityLabelsByWorkKeyAndUpdatedAt"),
2217
+ ])
2218
+ .authorization((allow) => [
2219
+ allow.groups(contentWriteGroups),
2220
+ ]),
2181
2221
  Message: a
2182
2222
  .model({
2183
2223
  id: a.id().required(),
@@ -2304,6 +2304,8 @@ const schema = a.schema({
2304
2304
  categoryKey: a.string(),
2305
2305
  displayName: a.string(),
2306
2306
  description: a.string(),
2307
+ // The language this node's own text (name, description, aliases) is written in: the site's primary language, written on every row.
2308
+ language: a.string(),
2307
2309
  aliases: a.string().array(),
2308
2310
  authorityScore: a.float(),
2309
2311
  authorityRank: a.integer(),
@@ -2331,6 +2333,45 @@ const schema = a.schema({
2331
2333
  allow.groups(categoryWriteGroups).to(["read"]),
2332
2334
  ]),
2333
2335
 
2336
+ /** One name of one concept in one language (docs/multilingual-labels.md). The Concept (SemanticNode) keeps its identity and its edges; each language's
2337
+ * preferred name, other names and short description are rows here. entityKey `{entityKind}#{entityId}`; id `{entityKey}#{language}` (preferred) or `...#alias#{folded text}`; `nameKey`
2338
+ * is `{language}#{folded text}` and finds a concept by name with one query; `workKey` (`{language}#{syncState}`) is present only while a label needs work. */
2339
+ EntityLabel: a
2340
+ .model({
2341
+ id: a.id().required(),
2342
+ tenantId: a.string(),
2343
+ entityKind: a.string().required(),
2344
+ entityId: a.id().required(),
2345
+ entityKey: a.string().required(),
2346
+ language: a.string().required(),
2347
+ role: a.string().required(),
2348
+ text: a.string().required(),
2349
+ textLanguage: a.string(),
2350
+ nameKey: a.string().required(),
2351
+ sortKey: a.string().required(),
2352
+ description: a.string(),
2353
+ equivalence: a.string(),
2354
+ origin: a.string(),
2355
+ pinned: a.boolean(),
2356
+ reviewedBy: a.string(),
2357
+ reviewedAt: a.datetime(),
2358
+ sourceContentHash: a.string(),
2359
+ syncState: a.string(),
2360
+ workKey: a.string(),
2361
+ slug: a.string(),
2362
+ provenanceId: a.string(),
2363
+ createdAt: a.datetime(),
2364
+ updatedAt: a.datetime(),
2365
+ })
2366
+ .secondaryIndexes((index) => [
2367
+ index("entityKey").sortKeys(["sortKey"]).queryField("listEntityLabelsByEntityAndSortKey"),
2368
+ index("nameKey").queryField("listEntityLabelsByNameKey"),
2369
+ index("workKey").sortKeys(["updatedAt"]).queryField("listEntityLabelsByWorkKeyAndUpdatedAt"),
2370
+ ])
2371
+ .authorization((allow) => [
2372
+ allow.groups(contentWriteGroups),
2373
+ ]),
2374
+
2334
2375
  Message: a
2335
2376
  .model({
2336
2377
  id: a.id().required(),
@@ -44,7 +44,7 @@ const realtimeModel = (fields) => a.model(fields);
44
44
  export const OPTIONAL_MODEL_GROUPS = {
45
45
  editions: ["Edition", "EditionItem", "PublishedEdition", "PublishedEditionItem"],
46
46
  knowledge: ["KnowledgeArtifact", "KnowledgeCorpus", "KnowledgeImportRun", "KnowledgeRawPayload"],
47
- semantic: ["SemanticNode", "SemanticRelation", "SemanticRelationType"],
47
+ semantic: ["SemanticNode", "SemanticRelation", "SemanticRelationType", "EntityLabel"],
48
48
  review: ["ReviewRequest", "ReviewRecipientRule", "ReviewReadMark"],
49
49
  procedures: ["ProcedureDefinition", "ProcedureRun", "ProcedureVersion"],
50
50
  editorial: ["EditorialCheckRun", "EditorialFeedback", "EditorialPolicy", "EditorialCheckSettings"],
@@ -2000,6 +2000,8 @@ function buildBaseDefinitions(excludedGroups = []) {
2000
2000
  categoryKey: a.string(),
2001
2001
  displayName: a.string(),
2002
2002
  description: a.string(),
2003
+ // The language this node's own text (name, description, aliases) is written in: the site's primary language, written on every row.
2004
+ language: a.string(),
2003
2005
  aliases: a.string().array(),
2004
2006
  authorityScore: a.float(),
2005
2007
  authorityRank: a.integer(),
@@ -2026,6 +2028,43 @@ function buildBaseDefinitions(excludedGroups = []) {
2026
2028
  .authorization((allow) => [
2027
2029
  allow.groups(categoryWriteGroups).to(["read"]),
2028
2030
  ]),
2031
+ /** One name of one concept in one language (docs/multilingual-labels.md). The Concept (SemanticNode) keeps its identity and its edges; each language's
2032
+ * preferred name, other names and short description are rows here. entityKey `{entityKind}#{entityId}`; id `{entityKey}#{language}` (preferred) or `...#alias#{folded text}`; `nameKey`
2033
+ * is `{language}#{folded text}` and finds a concept by name with one query; `workKey` (`{language}#{syncState}`) is present only while a label needs work. */
2034
+ EntityLabel: modelWithoutSubscriptions({
2035
+ id: a.id().required(),
2036
+ tenantId: a.string(),
2037
+ entityKind: a.string().required(),
2038
+ entityId: a.id().required(),
2039
+ entityKey: a.string().required(),
2040
+ language: a.string().required(),
2041
+ role: a.string().required(),
2042
+ text: a.string().required(),
2043
+ textLanguage: a.string(),
2044
+ nameKey: a.string().required(),
2045
+ sortKey: a.string().required(),
2046
+ description: a.string(),
2047
+ equivalence: a.string(),
2048
+ origin: a.string(),
2049
+ pinned: a.boolean(),
2050
+ reviewedBy: a.string(),
2051
+ reviewedAt: a.datetime(),
2052
+ sourceContentHash: a.string(),
2053
+ syncState: a.string(),
2054
+ workKey: a.string(),
2055
+ slug: a.string(),
2056
+ provenanceId: a.string(),
2057
+ createdAt: a.datetime(),
2058
+ updatedAt: a.datetime(),
2059
+ })
2060
+ .secondaryIndexes((index) => [
2061
+ index("entityKey").sortKeys(["sortKey"]).queryField("listEntityLabelsByEntityAndSortKey"),
2062
+ index("nameKey").queryField("listEntityLabelsByNameKey"),
2063
+ index("workKey").sortKeys(["updatedAt"]).queryField("listEntityLabelsByWorkKeyAndUpdatedAt"),
2064
+ ])
2065
+ .authorization((allow) => [
2066
+ allow.groups(contentWriteGroups),
2067
+ ]),
2029
2068
  Message: realtimeModel({
2030
2069
  id: a.id().required(),
2031
2070
  tenantId: a.string(),
@@ -47,7 +47,7 @@ const realtimeModel = <Fields extends Parameters<typeof a.model>[0]>(fields: Fie
47
47
  export const OPTIONAL_MODEL_GROUPS = {
48
48
  editions: ["Edition", "EditionItem", "PublishedEdition", "PublishedEditionItem"],
49
49
  knowledge: ["KnowledgeArtifact", "KnowledgeCorpus", "KnowledgeImportRun", "KnowledgeRawPayload"],
50
- semantic: ["SemanticNode", "SemanticRelation", "SemanticRelationType"],
50
+ semantic: ["SemanticNode", "SemanticRelation", "SemanticRelationType", "EntityLabel"],
51
51
  review: ["ReviewRequest", "ReviewRecipientRule", "ReviewReadMark"],
52
52
  procedures: ["ProcedureDefinition", "ProcedureRun", "ProcedureVersion"],
53
53
  editorial: ["EditorialCheckRun", "EditorialFeedback", "EditorialPolicy", "EditorialCheckSettings"],
@@ -2148,6 +2148,8 @@ const definitions = {
2148
2148
  categoryKey: a.string(),
2149
2149
  displayName: a.string(),
2150
2150
  description: a.string(),
2151
+ // The language this node's own text (name, description, aliases) is written in: the site's primary language, written on every row.
2152
+ language: a.string(),
2151
2153
  aliases: a.string().array(),
2152
2154
  authorityScore: a.float(),
2153
2155
  authorityRank: a.integer(),
@@ -2175,6 +2177,44 @@ const definitions = {
2175
2177
  allow.groups(categoryWriteGroups).to(["read"]),
2176
2178
  ]),
2177
2179
 
2180
+ /** One name of one concept in one language (docs/multilingual-labels.md). The Concept (SemanticNode) keeps its identity and its edges; each language's
2181
+ * preferred name, other names and short description are rows here. entityKey `{entityKind}#{entityId}`; id `{entityKey}#{language}` (preferred) or `...#alias#{folded text}`; `nameKey`
2182
+ * is `{language}#{folded text}` and finds a concept by name with one query; `workKey` (`{language}#{syncState}`) is present only while a label needs work. */
2183
+ EntityLabel: modelWithoutSubscriptions({
2184
+ id: a.id().required(),
2185
+ tenantId: a.string(),
2186
+ entityKind: a.string().required(),
2187
+ entityId: a.id().required(),
2188
+ entityKey: a.string().required(),
2189
+ language: a.string().required(),
2190
+ role: a.string().required(),
2191
+ text: a.string().required(),
2192
+ textLanguage: a.string(),
2193
+ nameKey: a.string().required(),
2194
+ sortKey: a.string().required(),
2195
+ description: a.string(),
2196
+ equivalence: a.string(),
2197
+ origin: a.string(),
2198
+ pinned: a.boolean(),
2199
+ reviewedBy: a.string(),
2200
+ reviewedAt: a.datetime(),
2201
+ sourceContentHash: a.string(),
2202
+ syncState: a.string(),
2203
+ workKey: a.string(),
2204
+ slug: a.string(),
2205
+ provenanceId: a.string(),
2206
+ createdAt: a.datetime(),
2207
+ updatedAt: a.datetime(),
2208
+ })
2209
+ .secondaryIndexes((index) => [
2210
+ index("entityKey").sortKeys(["sortKey"]).queryField("listEntityLabelsByEntityAndSortKey"),
2211
+ index("nameKey").queryField("listEntityLabelsByNameKey"),
2212
+ index("workKey").sortKeys(["updatedAt"]).queryField("listEntityLabelsByWorkKeyAndUpdatedAt"),
2213
+ ])
2214
+ .authorization((allow) => [
2215
+ allow.groups(contentWriteGroups),
2216
+ ]),
2217
+
2178
2218
  Message: realtimeModel({
2179
2219
  id: a.id().required(),
2180
2220
  tenantId: a.string(),
@@ -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.38.2,<0.39
7
+ humanus>=0.38.3,<0.39
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.38.2,<0.39", "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.38.3,<0.39", "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.38.2,<0.39", "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.38.3,<0.39", "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.38.2,<0.39
1
+ humanus[jev]>=0.38.3,<0.39
@@ -0,0 +1,30 @@
1
+ """The concept name writer: a DynamoDB stream consumer on the SemanticNode table (docs/multilingual-labels.md).
2
+
3
+ All the logic is ``papyrus_content.entity_labels.stream``; this file decodes the stream records and builds the site's authoring client with the function's
4
+ own role. A batch whose handling fails reports every record as failed, so Lambda retries it; the work is idempotent (a sync of an unchanged concept writes
5
+ nothing).
6
+ """
7
+ from __future__ import annotations
8
+
9
+ import json
10
+ import os
11
+ from typing import Any
12
+
13
+ from papyrus_content.entity_labels.stream import events_from_stream, handle_changes
14
+
15
+
16
+ def handler(event: dict[str, Any], context: Any = None, *, client=None) -> dict[str, Any]:
17
+ from papyrus_content.graphql_authoring import PapyrusGraphQLAuthoringClient
18
+
19
+ events = events_from_stream(event.get("Records", []))
20
+ if not events:
21
+ return {"batchItemFailures": []}
22
+ os.environ.setdefault("PAPYRUS_GRAPHQL_AUTH", "iam") # the function's own role signs the calls
23
+ try:
24
+ totals = handle_changes(events, client or PapyrusGraphQLAuthoringClient(endpoint=os.environ.get("PAPYRUS_GRAPHQL_ENDPOINT")))
25
+ except Exception as error:
26
+ print(json.dumps({"event": "papyrus_concept_label_sync_failed", "error": f"{type(error).__name__}: {error}"[:500]}))
27
+ return {"batchItemFailures": [{"itemIdentifier": record["dynamodb"]["SequenceNumber"]} for record in event["Records"]]}
28
+ if totals["nodes"] or totals["deleted"]:
29
+ print(json.dumps({"event": "papyrus_concept_label_sync", **totals}))
30
+ return {"batchItemFailures": []}
@@ -0,0 +1,82 @@
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
+ /**
8
+ * The concept name writer (docs/multilingual-labels.md): a DynamoDB Streams consumer on the SemanticNode table. Every change of a concept, whoever made it,
9
+ * brings that concept's primary-language EntityLabel rows (its name, other names and hashtag) to what its text says, and gives a concept with no language
10
+ * the site's primary language. It calls no model.
11
+ *
12
+ * What it may do, and nothing else: read and write EntityLabel rows, read the label's concept and set its `language`, and read the editorial settings (the
13
+ * primary language) through its own site's API. A shared stack is not wired: its rows carry a tenant this function does not act for.
14
+ */
15
+ export const ENTITY_LABEL_SYNC_API_FIELDS = [
16
+ "Query/fields/listEntityLabelsByConceptAndSortKey",
17
+ "Query/fields/getEditorialCheckSettings",
18
+ "Mutation/fields/createEntityLabel",
19
+ "Mutation/fields/updateEntityLabel",
20
+ "Mutation/fields/deleteEntityLabel",
21
+ "Mutation/fields/updateSemanticNode",
22
+ ];
23
+ const STREAM_READ_ACTIONS = ["dynamodb:DescribeStream", "dynamodb:GetRecords", "dynamodb:GetShardIterator", "dynamodb:ListStreams"];
24
+ /** The SemanticNode table's stream, with both images (the consumer compares them so that its own writes start no further work). */
25
+ function semanticNodeStream(backend) {
26
+ const cfnResources = backend.data.resources.cfnResources;
27
+ const wrapper = cfnResources.amplifyDynamoDbTables?.SemanticNode;
28
+ if (wrapper !== undefined) {
29
+ wrapper.streamSpecification = { streamViewType: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES };
30
+ return wrapper.resource.getAtt("TableStreamArn").toString();
31
+ }
32
+ const cfnTables = cfnResources.cfnTables ?? {};
33
+ const found = cfnTables.SemanticNode ??
34
+ cfnTables.SemanticNodeTable ??
35
+ Object.entries(cfnTables).find(([key]) => key.toLowerCase().replace(/table$/, "") === "semanticnode")?.[1];
36
+ if (!found)
37
+ throw new Error(`The concept name writer needs the SemanticNode table. cfnTables=${Object.keys(cfnTables).join(",")}`);
38
+ found.streamSpecification = { streamViewType: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES };
39
+ return found.attrStreamArn;
40
+ }
41
+ /** Wires the writer, or nothing when the site's schema leaves out the semantic models (`excludeModelGroups`: no table, no names). */
42
+ export function wireEntityLabelSync(backend) {
43
+ if (!backend.data.resources.tables.SemanticNode || !backend.data.resources.tables.EntityLabel)
44
+ return undefined;
45
+ const streamArn = semanticNodeStream(backend);
46
+ const stack = backend.createStack("entity-label-sync");
47
+ const writer = new LambdaFunction(stack, "EntityLabelSync", {
48
+ runtime: Runtime.PYTHON_3_12,
49
+ architecture: Architecture.ARM_64,
50
+ handler: "handler.handler",
51
+ timeout: Duration.seconds(60),
52
+ memorySize: 256,
53
+ code: pythonLambdaCode({ functionDir: "entity-label-sync", modules: ["papyrus_content"], noDeps: true }, [
54
+ "bash",
55
+ "-c",
56
+ [
57
+ "set -euo pipefail",
58
+ "mkdir -p /asset-output/papyrus_content",
59
+ "cp amplify/functions/entity-label-sync/handler.py /asset-output/handler.py",
60
+ "cp -R src/papyrus_content/. /asset-output/papyrus_content/",
61
+ ].join(" && "),
62
+ ]),
63
+ });
64
+ connectToSiteApi(writer, backend.data.resources.cfnResources.cfnGraphqlApi.attrGraphQlUrl);
65
+ const apiId = backend.data.resources.graphqlApi.apiId;
66
+ writer.addToRolePolicy(new PolicyStatement({
67
+ effect: Effect.ALLOW,
68
+ actions: ["appsync:GraphQL"],
69
+ resources: ENTITY_LABEL_SYNC_API_FIELDS.map((field) => `arn:aws:appsync:${stack.region}:${stack.account}:apis/${apiId}/types/${field}`),
70
+ }));
71
+ writer.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: STREAM_READ_ACTIONS, resources: [streamArn] }));
72
+ new CfnEventSourceMapping(stack, "EntityLabelSyncStreamMapping", {
73
+ eventSourceArn: streamArn,
74
+ functionName: writer.functionArn,
75
+ startingPosition: "LATEST",
76
+ batchSize: 10,
77
+ maximumBatchingWindowInSeconds: 2,
78
+ maximumRetryAttempts: 5,
79
+ functionResponseTypes: ["ReportBatchItemFailures"],
80
+ });
81
+ return writer;
82
+ }
@@ -0,0 +1,89 @@
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
+
8
+ /**
9
+ * The concept name writer (docs/multilingual-labels.md): a DynamoDB Streams consumer on the SemanticNode table. Every change of a concept, whoever made it,
10
+ * brings that concept's primary-language EntityLabel rows (its name, other names and hashtag) to what its text says, and gives a concept with no language
11
+ * the site's primary language. It calls no model.
12
+ *
13
+ * What it may do, and nothing else: read and write EntityLabel rows, read the label's concept and set its `language`, and read the editorial settings (the
14
+ * primary language) through its own site's API. A shared stack is not wired: its rows carry a tenant this function does not act for.
15
+ */
16
+ export const ENTITY_LABEL_SYNC_API_FIELDS = [
17
+ "Query/fields/listEntityLabelsByConceptAndSortKey",
18
+ "Query/fields/getEditorialCheckSettings",
19
+ "Mutation/fields/createEntityLabel",
20
+ "Mutation/fields/updateEntityLabel",
21
+ "Mutation/fields/deleteEntityLabel",
22
+ "Mutation/fields/updateSemanticNode",
23
+ ];
24
+ const STREAM_READ_ACTIONS = ["dynamodb:DescribeStream", "dynamodb:GetRecords", "dynamodb:GetShardIterator", "dynamodb:ListStreams"];
25
+
26
+ /** The SemanticNode table's stream, with both images (the consumer compares them so that its own writes start no further work). */
27
+ function semanticNodeStream(backend: any): string {
28
+ const cfnResources = backend.data.resources.cfnResources;
29
+ const wrapper = cfnResources.amplifyDynamoDbTables?.SemanticNode;
30
+ if (wrapper !== undefined) {
31
+ wrapper.streamSpecification = { streamViewType: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES };
32
+ return wrapper.resource.getAtt("TableStreamArn").toString();
33
+ }
34
+ const cfnTables = cfnResources.cfnTables ?? {};
35
+ const found =
36
+ cfnTables.SemanticNode ??
37
+ cfnTables.SemanticNodeTable ??
38
+ Object.entries(cfnTables).find(([key]) => key.toLowerCase().replace(/table$/, "") === "semanticnode")?.[1];
39
+ if (!found) throw new Error(`The concept name writer needs the SemanticNode table. cfnTables=${Object.keys(cfnTables).join(",")}`);
40
+ found.streamSpecification = { streamViewType: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES };
41
+ return found.attrStreamArn;
42
+ }
43
+
44
+ /** Wires the writer, or nothing when the site's schema leaves out the semantic models (`excludeModelGroups`: no table, no names). */
45
+ export function wireEntityLabelSync(backend: any): LambdaFunction | undefined {
46
+ if (!backend.data.resources.tables.SemanticNode || !backend.data.resources.tables.EntityLabel) return undefined;
47
+ const streamArn = semanticNodeStream(backend);
48
+ const stack = backend.createStack("entity-label-sync");
49
+ const writer = new LambdaFunction(stack, "EntityLabelSync", {
50
+ runtime: Runtime.PYTHON_3_12,
51
+ architecture: Architecture.ARM_64,
52
+ handler: "handler.handler",
53
+ timeout: Duration.seconds(60),
54
+ memorySize: 256,
55
+ code: pythonLambdaCode(
56
+ { functionDir: "entity-label-sync", modules: ["papyrus_content"], noDeps: true },
57
+ [
58
+ "bash",
59
+ "-c",
60
+ [
61
+ "set -euo pipefail",
62
+ "mkdir -p /asset-output/papyrus_content",
63
+ "cp amplify/functions/entity-label-sync/handler.py /asset-output/handler.py",
64
+ "cp -R src/papyrus_content/. /asset-output/papyrus_content/",
65
+ ].join(" && "),
66
+ ],
67
+ ),
68
+ });
69
+ connectToSiteApi(writer, backend.data.resources.cfnResources.cfnGraphqlApi.attrGraphQlUrl as string);
70
+ const apiId = backend.data.resources.graphqlApi.apiId as string;
71
+ writer.addToRolePolicy(
72
+ new PolicyStatement({
73
+ effect: Effect.ALLOW,
74
+ actions: ["appsync:GraphQL"],
75
+ resources: ENTITY_LABEL_SYNC_API_FIELDS.map((field) => `arn:aws:appsync:${stack.region}:${stack.account}:apis/${apiId}/types/${field}`),
76
+ }),
77
+ );
78
+ writer.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: STREAM_READ_ACTIONS, resources: [streamArn] }));
79
+ new CfnEventSourceMapping(stack, "EntityLabelSyncStreamMapping", {
80
+ eventSourceArn: streamArn,
81
+ functionName: writer.functionArn,
82
+ startingPosition: "LATEST",
83
+ batchSize: 10,
84
+ maximumBatchingWindowInSeconds: 2,
85
+ maximumRetryAttempts: 5,
86
+ functionResponseTypes: ["ReportBatchItemFailures"],
87
+ });
88
+ return writer;
89
+ }
@@ -107,8 +107,44 @@ function directoryTwin(uri) {
107
107
  return uri.substring(0, uri.length - 3) + "/index.md";
108
108
  }
109
109
 
110
+ // A distribution that serves both an apex host and its www twin sends www to the apex. The flag is spliced in by
111
+ // redirect-rules.ts at deploy time from publication.distribution.wwwRedirect.
112
+ const redirectWww = /*PAPYRUS_WWW_REDIRECT*/false/*END_PAPYRUS_WWW_REDIRECT*/;
113
+
114
+ function queryStringOf(request) {
115
+ const parts = [];
116
+ const query = request.querystring || {};
117
+ Object.keys(query).forEach(function (name) {
118
+ const entry = query[name];
119
+ const values = entry.multiValue ? entry.multiValue.map(function (item) { return item.value; }) : [entry.value];
120
+ values.forEach(function (value) {
121
+ parts.push(value === "" || value === undefined ? name : name + "=" + value);
122
+ });
123
+ });
124
+ return parts.length === 0 ? "" : "?" + parts.join("&");
125
+ }
126
+
127
+ function wwwRedirectResponse(request) {
128
+ const header = request.headers && request.headers.host;
129
+ const host = header && header.value ? header.value.toLowerCase() : "";
130
+ if (host.indexOf("www.") !== 0) {
131
+ return null;
132
+ }
133
+ return {
134
+ statusCode: 301,
135
+ statusDescription: "Moved Permanently",
136
+ headers: { location: { value: "https://" + host.substring(4) + request.uri + queryStringOf(request) } },
137
+ };
138
+ }
139
+
110
140
  async function handler(event) {
111
141
  const request = event.request;
142
+ if (redirectWww) {
143
+ const wwwResponse = wwwRedirectResponse(request);
144
+ if (wwwResponse !== null) {
145
+ return wwwResponse;
146
+ }
147
+ }
112
148
  const redirectTarget = await lookUpRedirectTarget(request.uri);
113
149
  if (redirectTarget !== null) {
114
150
  return {
@@ -47,6 +47,8 @@ const NEGOTIATE_START = "/*PAPYRUS_NEGOTIATE_MARKDOWN*/";
47
47
  const NEGOTIATE_END = "/*END_PAPYRUS_NEGOTIATE_MARKDOWN*/";
48
48
  const TWINS_START = "/*PAPYRUS_DIRECTORY_TWINS*/";
49
49
  const TWINS_END = "/*END_PAPYRUS_DIRECTORY_TWINS*/";
50
+ const WWW_START = "/*PAPYRUS_WWW_REDIRECT*/";
51
+ const WWW_END = "/*END_PAPYRUS_WWW_REDIRECT*/";
50
52
  function spliceFlag(source, start, end, label) {
51
53
  const from = source.indexOf(start);
52
54
  const to = source.indexOf(end);
@@ -69,5 +71,8 @@ export function compileRedirectFunctionSource(source, rules, options = {}) {
69
71
  if (options.directoryTwins === true) {
70
72
  compiled = spliceFlag(compiled, TWINS_START, TWINS_END, "directory twin");
71
73
  }
74
+ if (options.wwwRedirect === true) {
75
+ compiled = spliceFlag(compiled, WWW_START, WWW_END, "www redirect");
76
+ }
72
77
  return compiled;
73
78
  }
@@ -61,6 +61,8 @@ const NEGOTIATE_START = "/*PAPYRUS_NEGOTIATE_MARKDOWN*/";
61
61
  const NEGOTIATE_END = "/*END_PAPYRUS_NEGOTIATE_MARKDOWN*/";
62
62
  const TWINS_START = "/*PAPYRUS_DIRECTORY_TWINS*/";
63
63
  const TWINS_END = "/*END_PAPYRUS_DIRECTORY_TWINS*/";
64
+ const WWW_START = "/*PAPYRUS_WWW_REDIRECT*/";
65
+ const WWW_END = "/*END_PAPYRUS_WWW_REDIRECT*/";
64
66
 
65
67
  function spliceFlag(source: string, start: string, end: string, label: string): string {
66
68
  const from = source.indexOf(start);
@@ -75,7 +77,7 @@ function spliceFlag(source: string, start: string, end: string, label: string):
75
77
  export function compileRedirectFunctionSource(
76
78
  source: string,
77
79
  rules: EdgeRedirectRule[],
78
- options: { negotiateMarkdown?: boolean; directoryTwins?: boolean } = {},
80
+ options: { negotiateMarkdown?: boolean; directoryTwins?: boolean; wwwRedirect?: boolean } = {},
79
81
  ): string {
80
82
  const start = source.indexOf(START_MARKER);
81
83
  const end = source.indexOf(END_MARKER);
@@ -89,5 +91,8 @@ export function compileRedirectFunctionSource(
89
91
  if (options.directoryTwins === true) {
90
92
  compiled = spliceFlag(compiled, TWINS_START, TWINS_END, "directory twin");
91
93
  }
94
+ if (options.wwwRedirect === true) {
95
+ compiled = spliceFlag(compiled, WWW_START, WWW_END, "www redirect");
96
+ }
92
97
  return compiled;
93
98
  }
@@ -5,6 +5,7 @@ import * as origins from "aws-cdk-lib/aws-cloudfront-origins";
5
5
  import { Effect, Policy, PolicyStatement, ServicePrincipal } from "aws-cdk-lib/aws-iam";
6
6
  import { SqsDestination } from "aws-cdk-lib/aws-lambda-destinations";
7
7
  import * as route53 from "aws-cdk-lib/aws-route53";
8
+ import * as route53Targets from "aws-cdk-lib/aws-route53-targets";
8
9
  import * as s3 from "aws-cdk-lib/aws-s3";
9
10
  import * as sqs from "aws-cdk-lib/aws-sqs";
10
11
  import { readFileSync } from "node:fs";
@@ -41,6 +42,17 @@ function createDnsValidatedCertificate(scope, aliases, hostedZone) {
41
42
  region: "us-east-1",
42
43
  });
43
44
  }
45
+ function addAliasRecords(scope, aliases, zone, distribution) {
46
+ const hostedZone = route53.HostedZone.fromHostedZoneAttributes(scope, "SiteAliasHostedZone", {
47
+ hostedZoneId: zone.hostedZoneId,
48
+ zoneName: zone.hostedZoneName,
49
+ });
50
+ const target = route53.RecordTarget.fromAlias(new route53Targets.CloudFrontTarget(distribution));
51
+ aliases.forEach((alias, index) => {
52
+ new route53.ARecord(scope, `SiteAliasA${index}`, { zone: hostedZone, recordName: alias, target });
53
+ new route53.AaaaRecord(scope, `SiteAliasAaaa${index}`, { zone: hostedZone, recordName: alias, target });
54
+ });
55
+ }
44
56
  function createDistribution(scope, props, bucket, redirectFunction, markdownContentTypeFunction) {
45
57
  const aliases = props.distribution?.aliases ?? [];
46
58
  let certificate;
@@ -80,6 +92,7 @@ export function addS3ProjectionPublication(scope, props, media) {
80
92
  code: cloudfront.FunctionCode.fromInline(compileRedirectFunctionSource(readFileSync(redirectFunctionSourcePath, "utf8"), edgeConfig.rules, {
81
93
  negotiateMarkdown: edgeConfig.negotiateMarkdown,
82
94
  directoryTwins: edgeConfig.directoryTwins,
95
+ wwwRedirect: props.distribution?.wwwRedirect === true,
83
96
  })),
84
97
  keyValueStore,
85
98
  });
@@ -99,6 +112,9 @@ export function addS3ProjectionPublication(scope, props, media) {
99
112
  const distribution = markdownContentTypeFunction !== undefined
100
113
  ? createDistribution(scope, props, bucket, redirectFunction, markdownContentTypeFunction)
101
114
  : undefined;
115
+ if (distribution !== undefined && props.distribution?.dnsRecords === true && props.distribution.acm !== undefined) {
116
+ addAliasRecords(scope, props.distribution.aliases ?? [], props.distribution.acm, distribution);
117
+ }
102
118
  let distributionArn;
103
119
  if (distribution !== undefined) {
104
120
  distributionArn = distribution.distributionArn;
@@ -7,6 +7,7 @@ import { Effect, Policy, PolicyStatement, ServicePrincipal } from "aws-cdk-lib/a
7
7
  import type { Function as LambdaFunction } from "aws-cdk-lib/aws-lambda";
8
8
  import { SqsDestination } from "aws-cdk-lib/aws-lambda-destinations";
9
9
  import * as route53 from "aws-cdk-lib/aws-route53";
10
+ import * as route53Targets from "aws-cdk-lib/aws-route53-targets";
10
11
  import * as s3 from "aws-cdk-lib/aws-s3";
11
12
  import * as sqs from "aws-cdk-lib/aws-sqs";
12
13
  import type { Construct } from "constructs";
@@ -74,6 +75,18 @@ function createDnsValidatedCertificate(
74
75
  });
75
76
  }
76
77
 
78
+ function addAliasRecords(scope: Construct, aliases: string[], zone: { hostedZoneId: string; hostedZoneName: string }, distribution: cloudfront.Distribution): void {
79
+ const hostedZone = route53.HostedZone.fromHostedZoneAttributes(scope, "SiteAliasHostedZone", {
80
+ hostedZoneId: zone.hostedZoneId,
81
+ zoneName: zone.hostedZoneName,
82
+ });
83
+ const target = route53.RecordTarget.fromAlias(new route53Targets.CloudFrontTarget(distribution));
84
+ aliases.forEach((alias, index) => {
85
+ new route53.ARecord(scope, `SiteAliasA${index}`, { zone: hostedZone, recordName: alias, target });
86
+ new route53.AaaaRecord(scope, `SiteAliasAaaa${index}`, { zone: hostedZone, recordName: alias, target });
87
+ });
88
+ }
89
+
77
90
  function createDistribution(
78
91
  scope: Construct,
79
92
  props: S3ProjectionPublicationProps,
@@ -126,6 +139,7 @@ export function addS3ProjectionPublication(
126
139
  compileRedirectFunctionSource(readFileSync(redirectFunctionSourcePath, "utf8"), edgeConfig.rules, {
127
140
  negotiateMarkdown: edgeConfig.negotiateMarkdown,
128
141
  directoryTwins: edgeConfig.directoryTwins,
142
+ wwwRedirect: props.distribution?.wwwRedirect === true,
129
143
  }),
130
144
  ),
131
145
  keyValueStore,
@@ -148,6 +162,10 @@ export function addS3ProjectionPublication(
148
162
  ? createDistribution(scope, props, bucket, redirectFunction, markdownContentTypeFunction)
149
163
  : undefined;
150
164
 
165
+ if (distribution !== undefined && props.distribution?.dnsRecords === true && props.distribution.acm !== undefined) {
166
+ addAliasRecords(scope, props.distribution.aliases ?? [], props.distribution.acm, distribution);
167
+ }
168
+
151
169
  let distributionArn: string | undefined;
152
170
  if (distribution !== undefined) {
153
171
  distributionArn = distribution.distributionArn;
@@ -113,6 +113,20 @@ export function validatePublicationConfig(publication) {
113
113
  if (distribution.acm !== undefined && (!distribution.acm.hostedZoneId || !distribution.acm.hostedZoneName)) {
114
114
  problems.push("publication.distribution.acm requires hostedZoneId and hostedZoneName");
115
115
  }
116
+ if (distribution.dnsRecords !== undefined && typeof distribution.dnsRecords !== "boolean") {
117
+ problems.push("publication.distribution.dnsRecords must be a boolean");
118
+ }
119
+ if (distribution.dnsRecords === true && (distribution.acm === undefined || aliases.length === 0)) {
120
+ problems.push("publication.distribution.dnsRecords requires acm (the hosted zone) and aliases");
121
+ }
122
+ if (distribution.wwwRedirect !== undefined && typeof distribution.wwwRedirect !== "boolean") {
123
+ problems.push("publication.distribution.wwwRedirect must be a boolean");
124
+ }
125
+ if (distribution.wwwRedirect === true) {
126
+ const hasPair = aliases.some((alias) => alias.startsWith("www.") && aliases.includes(alias.slice(4)));
127
+ if (!hasPair)
128
+ problems.push("publication.distribution.wwwRedirect requires aliases that list both <host> and www.<host>");
129
+ }
116
130
  }
117
131
  return problems;
118
132
  }
@@ -30,6 +30,15 @@ export type S3ProjectionDistributionConfig = {
30
30
  aliases?: string[];
31
31
  certificateArn?: string;
32
32
  acm?: { hostedZoneId: string; hostedZoneName: string };
33
+ /**
34
+ * Write an A and an AAAA alias record for every alias, in the `acm` hosted zone, pointing at this distribution. Needs `acm` and aliases.
35
+ * An alias whose record already exists in the zone (an Amplify domain association still holds it) makes the deploy fail: release the host first.
36
+ */
37
+ dnsRecords?: boolean;
38
+ /**
39
+ * Answer a request for `www.<host>` with a 301 to `https://<host>` (path and query kept), at the edge. Needs aliases that list both `<host>` and `www.<host>`.
40
+ */
41
+ wwwRedirect?: boolean;
33
42
  };
34
43
 
35
44
  export type PapyrusPublicationCacheConfig = {
@@ -195,6 +204,19 @@ export function validatePublicationConfig(publication: unknown): string[] {
195
204
  if (distribution.acm !== undefined && (!distribution.acm.hostedZoneId || !distribution.acm.hostedZoneName)) {
196
205
  problems.push("publication.distribution.acm requires hostedZoneId and hostedZoneName");
197
206
  }
207
+ if (distribution.dnsRecords !== undefined && typeof distribution.dnsRecords !== "boolean") {
208
+ problems.push("publication.distribution.dnsRecords must be a boolean");
209
+ }
210
+ if (distribution.dnsRecords === true && (distribution.acm === undefined || aliases.length === 0)) {
211
+ problems.push("publication.distribution.dnsRecords requires acm (the hosted zone) and aliases");
212
+ }
213
+ if (distribution.wwwRedirect !== undefined && typeof distribution.wwwRedirect !== "boolean") {
214
+ problems.push("publication.distribution.wwwRedirect must be a boolean");
215
+ }
216
+ if (distribution.wwwRedirect === true) {
217
+ const hasPair = aliases.some((alias) => alias.startsWith("www.") && aliases.includes(alias.slice(4)));
218
+ if (!hasPair) problems.push("publication.distribution.wwwRedirect requires aliases that list both <host> and www.<host>");
219
+ }
198
220
  }
199
221
  return problems;
200
222
  }
@@ -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 { wireEntityLabelSync } from "./functions/entity-label-sync/wire.js";
42
43
  import { wireLocalizationDispatch } from "./functions/localization-dispatch/wire.js";
43
44
  import { announcements } from "./functions/announcements/resource.js";
44
45
  import { wireAnnouncements } from "./functions/announcements/wire.js";
@@ -773,6 +774,9 @@ export function defineSiteBackend(papyrusSite, options = {}) {
773
774
  // 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
775
  if (tenancyMode !== "shared")
775
776
  wireLocalizationDispatch(backend, contentActionsLambda);
777
+ // The names of concepts in each language: a stream consumer on the SemanticNode records writes every concept's primary-language label rows.
778
+ if (tenancyMode !== "shared")
779
+ wireEntityLabelSync(backend);
776
780
  wireImageService(backend);
777
781
  wireProposalActions(backend);
778
782
  // 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 { wireEntityLabelSync } from "./functions/entity-label-sync/wire";
49
50
  import { wireLocalizationDispatch } from "./functions/localization-dispatch/wire";
50
51
  import { announcements } from "./functions/announcements/resource";
51
52
  import { wireAnnouncements } from "./functions/announcements/wire";
@@ -932,6 +933,8 @@ export function defineSiteBackend(papyrusSite: PapyrusSite, options: DefineSiteB
932
933
  wireAutomationView(backend);
933
934
  // 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
935
  if (tenancyMode !== "shared") wireLocalizationDispatch(backend, contentActionsLambda);
936
+ // The names of concepts in each language: a stream consumer on the SemanticNode records writes every concept's primary-language label rows.
937
+ if (tenancyMode !== "shared") wireEntityLabelSync(backend);
935
938
  wireImageService(backend);
936
939
  wireProposalActions(backend);
937
940
  // Announcements are off until the owner turns them on; the parameters and the lease are per site (the brand id names the site).
@@ -18,7 +18,7 @@ export const TENANT_FIELD_MODELS = new Set([
18
18
  "Edition", "EditionItem", "PublishedEdition", "PublishedEditionItem", "PublishedCategorySet", "PublishedCategory",
19
19
  "EditorialPolicy", "EditorialCheckSettings", "EditorialFeedback", "EditorialCheckRun", "Proposal", "ReaderProfile",
20
20
  "ReaderHandle", "ReaderVote", "ReaderFlag", "ReaderComment", "ReaderSubmission", "ReaderReport", "ModerationAction",
21
- "SignupEntry", "LocalizationRequest",
21
+ "SignupEntry", "LocalizationRequest", "EntityLabel",
22
22
  ]);
23
23
  const TENANT_KEY_SEPARATOR = "#";
24
24
  const TENANT_ID_PATTERN = /^[a-z0-9][a-z0-9-]{0,62}$/;
@@ -22,7 +22,7 @@ export const TENANT_FIELD_MODELS: ReadonlySet<string> = new Set([
22
22
  "Edition", "EditionItem", "PublishedEdition", "PublishedEditionItem", "PublishedCategorySet", "PublishedCategory",
23
23
  "EditorialPolicy", "EditorialCheckSettings", "EditorialFeedback", "EditorialCheckRun", "Proposal", "ReaderProfile",
24
24
  "ReaderHandle", "ReaderVote", "ReaderFlag", "ReaderComment", "ReaderSubmission", "ReaderReport", "ModerationAction",
25
- "SignupEntry", "LocalizationRequest",
25
+ "SignupEntry", "LocalizationRequest", "EntityLabel",
26
26
  ]);
27
27
 
28
28
  const TENANT_KEY_SEPARATOR = "#";
package/package.json CHANGED
@@ -640,7 +640,7 @@
640
640
  "optional": true
641
641
  }
642
642
  },
643
- "version": "5.118.0",
643
+ "version": "5.120.0-next.1",
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.118.0",
2
+ "version": "5.120.0-next.1",
3
3
  "routes": [
4
4
  {
5
5
  "file": "app/%5F_preview/[[...path]]/route.ts",