@adobe/spacecat-shared-tokowaka-client 1.1.0 → 1.2.0

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [@adobe/spacecat-shared-tokowaka-client-v1.2.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-tokowaka-client-v1.1.0...@adobe/spacecat-shared-tokowaka-client-v1.2.0) (2025-11-27)
2
+
3
+
4
+ ### Features
5
+
6
+ * url path-wise edge deployment ([#1178](https://github.com/adobe/spacecat-shared/issues/1178)) ([37b0d1a](https://github.com/adobe/spacecat-shared/commit/37b0d1a388f842a62cb95eb5272feb49d1dfb3e8))
7
+
1
8
  # [@adobe/spacecat-shared-tokowaka-client-v1.1.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-tokowaka-client-v1.0.6...@adobe/spacecat-shared-tokowaka-client-v1.1.0) (2025-11-25)
2
9
 
3
10
 
package/README.md CHANGED
@@ -26,34 +26,44 @@ Creates a client instance from a context object.
26
26
  **Required context properties:**
27
27
  - `context.s3.s3Client` (S3Client): AWS S3 client instance
28
28
  - `context.log` (Object, optional): Logger instance
29
- - `context.env.TOKOWAKA_SITE_CONFIG_BUCKET` (string): S3 bucket name for configurations
29
+ - `context.env.TOKOWAKA_SITE_CONFIG_BUCKET` (string): S3 bucket name for deployed configurations
30
+ - `context.env.TOKOWAKA_PREVIEW_BUCKET` (string): S3 bucket name for preview configurations
30
31
  - `context.env.TOKOWAKA_CDN_PROVIDER` (string): CDN provider for cache invalidation
31
32
  - `context.env.TOKOWAKA_CDN_CONFIG` (string): JSON configuration for CDN client
33
+ - `context.env.TOKOWAKA_EDGE_URL` (string): Tokowaka edge URL for preview HTML fetching
32
34
 
33
35
  ## Environment Variables
34
36
 
35
37
  **Required:**
36
- - `TOKOWAKA_SITE_CONFIG_BUCKET` - S3 bucket name for storing configurations
38
+ - `TOKOWAKA_SITE_CONFIG_BUCKET` - S3 bucket name for storing deployed configurations
39
+ - `TOKOWAKA_PREVIEW_BUCKET` - S3 bucket name for storing preview configurations
37
40
 
38
41
  **Optional (for CDN invalidation):**
39
42
  - `TOKOWAKA_CDN_PROVIDER` - CDN provider name (e.g., "cloudfront")
40
43
  - `TOKOWAKA_CDN_CONFIG` - JSON string with CDN-specific configuration. (e.g., { "cloudfront": { "distributionId": <distribution-id>, "region": "us-east-1" }})
41
44
 
45
+ **Optional (for preview functionality):**
46
+ - `TOKOWAKA_EDGE_URL` - Tokowaka edge URL for fetching HTML content during preview
47
+
42
48
  ### Main Methods
43
49
 
44
50
  #### `deploySuggestions(site, opportunity, suggestions)`
45
51
 
46
- Generates configuration and uploads to S3. **Automatically fetches existing configuration and merges** new suggestions with it. Invalidates CDN cache after upload.
52
+ Generates configuration and uploads to S3 **per URL**. **Automatically fetches existing configuration for each URL and merges** new suggestions with it. Invalidates CDN cache after upload.
53
+
54
+ **Architecture Change:** Creates one S3 file per URL instead of a single file with all URLs. This prevents files from growing too large over time.
47
55
 
48
56
  **Returns:** `Promise<DeploymentResult>` with:
49
- - `s3Path` - S3 key where config was uploaded
50
- - `cdnInvalidation` - CDN invalidation result (or error)
57
+ - `s3Paths` - Array of S3 keys where configs were uploaded (one per URL)
58
+ - `cdnInvalidations` - Array of CDN invalidation results (one per URL)
51
59
  - `succeededSuggestions` - Array of deployed suggestions
52
60
  - `failedSuggestions` - Array of `{suggestion, reason}` objects for ineligible suggestions
53
61
 
54
62
  #### `rollbackSuggestions(site, opportunity, suggestions)`
55
63
 
56
- Rolls back previously deployed suggestions by removing their patches from the configuration. **Automatically fetches existing configuration** and removes patches matching the provided suggestions. Invalidates CDN cache after upload.
64
+ Rolls back previously deployed suggestions by removing their patches from the configuration. **Automatically fetches existing configuration for each URL** and removes patches matching the provided suggestions. Invalidates CDN cache after upload.
65
+
66
+ **Architecture Change:** Updates one S3 file per URL instead of a single file with all URLs.
57
67
 
58
68
  **Mapper-Specific Rollback Behavior:**
59
69
  - Each opportunity mapper handles its own rollback logic via `rollbackPatches()` method
@@ -61,15 +71,31 @@ Rolls back previously deployed suggestions by removing their patches from the co
61
71
  - **Headings/Summarization:** Simple removal by suggestion ID (default behavior)
62
72
 
63
73
  **Returns:** `Promise<RollbackResult>` with:
64
- - `s3Path` - S3 key where config was uploaded
65
- - `cdnInvalidation` - CDN invalidation result (or error)
74
+ - `s3Paths` - Array of S3 keys where configs were uploaded (one per URL)
75
+ - `cdnInvalidations` - Array of CDN invalidation results (one per URL)
66
76
  - `succeededSuggestions` - Array of rolled back suggestions
67
77
  - `failedSuggestions` - Array of `{suggestion, reason}` objects for ineligible suggestions
68
- - `removedPatchesCount` - Number of patches removed from the configuration
78
+ - `removedPatchesCount` - Total number of patches removed across all URLs
79
+
80
+ #### `previewSuggestions(site, opportunity, suggestions, options)`
81
+
82
+ Previews suggestions by uploading to preview S3 path and fetching HTML comparison. **All suggestions must belong to the same URL.**
83
+
84
+ **Returns:** `Promise<PreviewResult>` with:
85
+ - `s3Path` - S3 key where preview config was uploaded
86
+ - `config` - Preview configuration object
87
+ - `cdnInvalidation` - CDN invalidation result
88
+ - `succeededSuggestions` - Array of previewed suggestions
89
+ - `failedSuggestions` - Array of `{suggestion, reason}` objects for ineligible suggestions
90
+ - `html` - Object with `url`, `originalHtml`, and `optimizedHtml`
69
91
 
70
- #### `fetchConfig(apiKey)`
92
+ #### `fetchConfig(url, isPreview)`
71
93
 
72
- Fetches existing Tokowaka configuration from S3.
94
+ Fetches existing Tokowaka configuration from S3 for a specific URL.
95
+
96
+ **Parameters:**
97
+ - `url` - Full URL (e.g., 'https://www.example.com/products/item')
98
+ - `isPreview` - Whether to fetch from preview path (default: false)
73
99
 
74
100
  **Returns:** `Promise<TokowakaConfig | null>` - Configuration object or null if not found
75
101
 
@@ -79,36 +105,135 @@ Merges existing configuration with new configuration. For each URL path, checks
79
105
 
80
106
  **Returns:** `TokowakaConfig` - Merged configuration
81
107
 
82
- #### `generateConfig(site, opportunity, suggestions)`
108
+ #### `generateConfig(url, opportunity, suggestions)`
109
+
110
+ Generates Tokowaka configuration from opportunity suggestions for a specific URL without uploading.
111
+
112
+ **Parameters:**
113
+ - `url` - Full URL for which to generate config
114
+ - `opportunity` - Opportunity entity
115
+ - `suggestions` - Array of suggestion entities
116
+
117
+ #### `uploadConfig(url, config, isPreview)`
83
118
 
84
- Generates Tokowaka configuration from opportunity suggestions without uploading.
119
+ Uploads configuration to S3 for a specific URL.
85
120
 
86
- #### `uploadConfig(apiKey, config)`
121
+ **Parameters:**
122
+ - `url` - Full URL (e.g., 'https://www.example.com/products/item')
123
+ - `config` - Tokowaka configuration object
124
+ - `isPreview` - Whether to upload to preview path (default: false)
87
125
 
88
- Uploads configuration to S3. Returns S3 key of uploaded configuration.
126
+ **Returns:** `Promise<string>` - S3 key of uploaded configuration
89
127
 
90
128
  ## CDN Cache Invalidation
91
129
 
92
130
  The client invalidates CDN cache after uploading configurations. Failures are logged but don't block deployment.
93
131
 
132
+ ## Site Configuration
133
+
134
+ Sites must have the following configuration in their `tokowakaConfig`:
135
+
136
+ ```javascript
137
+ {
138
+ "tokowakaConfig": {
139
+ "apiKey": "legacy-key-kept-for-backward-compatibility", // Optional, kept for backward compatibility
140
+ "forwardedHost": "www.example.com" // Required for preview functionality
141
+ }
142
+ }
143
+ ```
144
+
145
+ **Note:**
146
+ - `apiKey` is optional and **not used** for S3 paths or HTTP headers (kept in schema for potential future use)
147
+ - `forwardedHost` is **required** for preview functionality to fetch HTML from Tokowaka edge
148
+
94
149
  ## Supported Opportunity Types
95
150
 
96
151
  ### Headings
97
152
 
98
153
  **Deployment Eligibility:** Only suggestions with `checkType: 'heading-empty'`, `checkType: 'heading-missing-h1'` and `checkType: 'heading-h1-length'` can be deployed currently.
99
154
 
155
+ ### FAQ
156
+
157
+ **Deployment Eligibility:** Suggestions must have `shouldOptimize: true` flag and valid FAQ item structure.
158
+
159
+ **Special Behavior:** Automatically manages heading patch - adds heading when first FAQ is deployed, removes heading when last FAQ is rolled back.
160
+
100
161
  ### Content Summarization
101
162
 
102
- **Deployment Eligibility:** Currently all suggestions for `summarization` opportunity can be deployed.
163
+ **Deployment Eligibility:** Currently all suggestions for `summarization` opportunity can be deployed.
103
164
 
104
165
  ## S3 Storage
105
166
 
106
- Configurations are stored at:
167
+ Configurations are now stored **per URL** with domain-level metadata:
168
+
169
+ ### Structure
107
170
  ```
108
- s3://{TOKOWAKA_SITE_CONFIG_BUCKET}/opportunities/{tokowakaApiKey}
171
+ s3://{TOKOWAKA_SITE_CONFIG_BUCKET}/opportunities/{normalized-domain}/
172
+ ├── config (domain-level metaconfig: siteId, prerender)
173
+ ├── {base64-encoded-path-1} (URL-specific patches)
174
+ ├── {base64-encoded-path-2} (URL-specific patches)
175
+ └── ...
176
+ ```
177
+
178
+ For preview configurations:
179
+ ```
180
+ s3://{TOKOWAKA_PREVIEW_BUCKET}/preview/opportunities/{normalized-domain}/
181
+ ├── config
182
+ ├── {base64-encoded-path-1}
183
+ └── ...
184
+ ```
185
+
186
+ **Architecture Change:** Each URL has its own configuration file instead of one file per site. Domain-level metaconfig is stored separately to avoid duplication.
187
+
188
+ **URL Normalization:**
189
+ - Domain: Strips `www.` prefix (e.g., `www.example.com` → `example.com`)
190
+ - Path: Removes trailing slash (except for root `/`), ensures starts with `/`, then base64 URL encodes
191
+
192
+ **Example:**
193
+ - URL: `https://www.example.com/products/item`
194
+ - Metaconfig Path: `opportunities/example.com/config`
195
+ - Patch Config Path: `opportunities/example.com/L3Byb2R1Y3RzL2l0ZW0`
196
+ - Where `L3Byb2R1Y3RzL2l0ZW0` is base64 URL encoding of `/products/item`
197
+
198
+ ### Metaconfig File Structure
199
+ Domain-level metaconfig (created once per domain, shared by all URLs):
200
+ ```json
201
+ {
202
+ "siteId": "abc-123",
203
+ "prerender": true
204
+ }
205
+ ```
206
+
207
+ ### Configuration File Structure
208
+ Per-URL configuration (flat structure):
209
+ ```json
210
+ {
211
+ "url": "https://example.com/products/item",
212
+ "version": "1.0",
213
+ "forceFail": false,
214
+ "prerender": true,
215
+ "patches": [
216
+ {
217
+ "opportunityId": "abc-123",
218
+ "suggestionId": "xyz-789",
219
+ "prerenderRequired": true,
220
+ "lastUpdated": 1234567890,
221
+ "op": "insertAfter",
222
+ "selector": "main",
223
+ "value": { ... },
224
+ "valueFormat": "hast",
225
+ "target": "ai-bots"
226
+ }
227
+ ]
228
+ }
109
229
  ```
110
230
 
111
- **Note:** The configuration is stored as a JSON file containing the complete Tokowaka optimization config for the site.
231
+ **Note:**
232
+ - `siteId` is stored only in domain-level `config` (metaconfig)
233
+ - `prerender` is stored in both metaconfig (domain-level) and patch files (URL-level)
234
+ - The `baseURL` field has been renamed to `url`
235
+ - The `tokowakaOptimizations` nested structure has been removed
236
+ - The `tokowakaForceFail` field has been renamed to `forceFail`
112
237
 
113
238
  ## Reference Material
114
239
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-tokowaka-client",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Tokowaka Client for SpaceCat - Edge optimization config management",
5
5
  "type": "module",
6
6
  "engines": {
package/src/index.d.ts CHANGED
@@ -32,17 +32,17 @@ export const TARGET_USER_AGENTS_CATEGORIES: {
32
32
  ALL: 'all';
33
33
  };
34
34
 
35
- export interface TokowakaUrlOptimization {
35
+ export interface TokowakaMetaconfig {
36
+ siteId: string;
36
37
  prerender: boolean;
37
- patches: TokawakaPatch[];
38
38
  }
39
39
 
40
40
  export interface TokowakaConfig {
41
- siteId: string;
42
- baseURL: string;
41
+ url: string;
43
42
  version: string;
44
- tokowakaForceFail: boolean;
45
- tokowakaOptimizations: Record<string, TokowakaUrlOptimization>;
43
+ forceFail: boolean;
44
+ prerender: boolean;
45
+ patches: TokawakaPatch[];
46
46
  }
47
47
 
48
48
  export interface CdnInvalidationResult {
@@ -55,16 +55,37 @@ export interface CdnInvalidationResult {
55
55
  }
56
56
 
57
57
  export interface DeploymentResult {
58
+ s3Paths: string[];
59
+ cdnInvalidations: (CdnInvalidationResult | null)[];
60
+ succeededSuggestions: Array<any>;
61
+ failedSuggestions: Array<{ suggestion: any; reason: string }>;
62
+ }
63
+
64
+ export interface RollbackResult {
65
+ s3Paths: string[];
66
+ cdnInvalidations: (CdnInvalidationResult | null)[];
67
+ succeededSuggestions: Array<any>;
68
+ failedSuggestions: Array<{ suggestion: any; reason: string }>;
69
+ removedPatchesCount: number;
70
+ }
71
+
72
+ export interface PreviewResult {
58
73
  s3Path: string;
74
+ config: TokowakaConfig;
59
75
  cdnInvalidation: CdnInvalidationResult | null;
60
76
  succeededSuggestions: Array<any>;
61
77
  failedSuggestions: Array<{ suggestion: any; reason: string }>;
78
+ html: {
79
+ url: string;
80
+ originalHtml: string;
81
+ optimizedHtml: string;
82
+ };
62
83
  }
63
84
 
64
85
  export interface SiteConfig {
65
86
  getTokowakaConfig(): {
66
- apiKey: string;
67
- cdnProvider?: string;
87
+ apiKey?: string;
88
+ forwardedHost?: string;
68
89
  };
69
90
  getFetchConfig?(): {
70
91
  overrideBaseURL?: string;
@@ -261,6 +282,7 @@ export class CdnClientRegistry {
261
282
  export default class TokowakaClient {
262
283
  constructor(config: {
263
284
  bucketName: string;
285
+ previewBucketName?: string;
264
286
  s3Client: S3Client;
265
287
  env?: Record<string, any>;
266
288
  }, log: any);
@@ -268,26 +290,44 @@ export default class TokowakaClient {
268
290
  static createFrom(context: {
269
291
  env: {
270
292
  TOKOWAKA_SITE_CONFIG_BUCKET: string;
293
+ TOKOWAKA_PREVIEW_BUCKET?: string;
271
294
  TOKOWAKA_CDN_PROVIDER?: string;
272
295
  TOKOWAKA_CDN_CONFIG?: string;
296
+ TOKOWAKA_EDGE_URL?: string;
273
297
  };
274
298
  log?: any;
275
299
  s3: { s3Client: S3Client };
276
300
  tokowakaClient?: TokowakaClient;
277
301
  }): TokowakaClient;
278
302
 
303
+ /**
304
+ * Generates Tokowaka configuration from suggestions for a specific URL
305
+ */
279
306
  generateConfig(
280
- site: Site,
307
+ url: string,
281
308
  opportunity: Opportunity,
282
309
  suggestions: Suggestion[]
283
- ): TokowakaConfig;
310
+ ): TokowakaConfig | null;
284
311
 
285
- uploadConfig(apiKey: string, config: TokowakaConfig): Promise<string>;
312
+ /**
313
+ * Uploads configuration to S3 for a specific URL
314
+ */
315
+ uploadConfig(url: string, config: TokowakaConfig, isPreview?: boolean): Promise<string>;
286
316
 
287
317
  /**
288
- * Fetches existing Tokowaka configuration from S3
318
+ * Fetches existing Tokowaka configuration from S3 for a specific URL
289
319
  */
290
- fetchConfig(apiKey: string): Promise<TokowakaConfig | null>;
320
+ fetchConfig(url: string, isPreview?: boolean): Promise<TokowakaConfig | null>;
321
+
322
+ /**
323
+ * Fetches domain-level metaconfig from S3
324
+ */
325
+ fetchMetaconfig(url: string, isPreview?: boolean): Promise<TokowakaMetaconfig | null>;
326
+
327
+ /**
328
+ * Uploads domain-level metaconfig to S3
329
+ */
330
+ uploadMetaconfig(url: string, metaconfig: TokowakaMetaconfig, isPreview?: boolean): Promise<string>;
291
331
 
292
332
  /**
293
333
  * Merges existing configuration with new configuration
@@ -295,16 +335,42 @@ export default class TokowakaClient {
295
335
  mergeConfigs(existingConfig: TokowakaConfig, newConfig: TokowakaConfig): TokowakaConfig;
296
336
 
297
337
  /**
298
- * Invalidates CDN cache
338
+ * Invalidates CDN cache for a specific URL
299
339
  */
300
- invalidateCdnCache(apiKey: string, cdnProvider?: string): Promise<CdnInvalidationResult | null>;
340
+ invalidateCdnCache(url: string, provider?: string, isPreview?: boolean): Promise<CdnInvalidationResult | null>;
301
341
 
342
+ /**
343
+ * Deploys suggestions to Tokowaka edge
344
+ */
302
345
  deploySuggestions(
303
346
  site: Site,
304
347
  opportunity: Opportunity,
305
348
  suggestions: Suggestion[]
306
349
  ): Promise<DeploymentResult>;
307
350
 
351
+ /**
352
+ * Rolls back deployed suggestions
353
+ */
354
+ rollbackSuggestions(
355
+ site: Site,
356
+ opportunity: Opportunity,
357
+ suggestions: Suggestion[]
358
+ ): Promise<RollbackResult>;
359
+
360
+ /**
361
+ * Previews suggestions (all must belong to same URL)
362
+ */
363
+ previewSuggestions(
364
+ site: Site,
365
+ opportunity: Opportunity,
366
+ suggestions: Suggestion[],
367
+ options?: {
368
+ warmupDelayMs?: number;
369
+ maxRetries?: number;
370
+ retryDelayMs?: number;
371
+ }
372
+ ): Promise<PreviewResult>;
373
+
308
374
  /**
309
375
  * Registers a custom mapper for an opportunity type
310
376
  */