@rudra-js/core 0.9.0 → 0.10.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.
Files changed (63) hide show
  1. package/README.md +425 -375
  2. package/dist/claim-screening.d.ts.map +1 -1
  3. package/dist/claim-screening.js +3 -9
  4. package/dist/claim-screening.js.map +1 -1
  5. package/dist/component-generator.d.ts +1 -1
  6. package/dist/component-generator.d.ts.map +1 -1
  7. package/dist/component-generator.js +28 -61
  8. package/dist/component-generator.js.map +1 -1
  9. package/dist/component-spec.d.ts +100 -11
  10. package/dist/component-spec.d.ts.map +1 -1
  11. package/dist/component-spec.js +3 -6
  12. package/dist/component-spec.js.map +1 -1
  13. package/dist/fallback-component.d.ts.map +1 -1
  14. package/dist/fallback-component.js +3 -3
  15. package/dist/fallback-component.js.map +1 -1
  16. package/dist/fit-to-shopper.d.ts +4 -1
  17. package/dist/fit-to-shopper.d.ts.map +1 -1
  18. package/dist/fit-to-shopper.js +8 -0
  19. package/dist/fit-to-shopper.js.map +1 -1
  20. package/dist/index.d.ts +2 -2
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +1 -1
  23. package/dist/index.js.map +1 -1
  24. package/dist/model-prompt.d.ts.map +1 -1
  25. package/dist/model-prompt.js +16 -3
  26. package/dist/model-prompt.js.map +1 -1
  27. package/dist/product-display.d.ts +6 -0
  28. package/dist/product-display.d.ts.map +1 -0
  29. package/dist/product-display.js +32 -0
  30. package/dist/product-display.js.map +1 -0
  31. package/dist/reconciliation.d.ts +2 -4
  32. package/dist/reconciliation.d.ts.map +1 -1
  33. package/dist/reconciliation.js +15 -21
  34. package/dist/reconciliation.js.map +1 -1
  35. package/dist/schemas.d.ts +1 -0
  36. package/dist/schemas.d.ts.map +1 -1
  37. package/dist/schemas.js +1 -0
  38. package/dist/schemas.js.map +1 -1
  39. package/dist/signal-digest.d.ts.map +1 -1
  40. package/dist/signal-digest.js +2 -10
  41. package/dist/signal-digest.js.map +1 -1
  42. package/dist/spec-cache.d.ts +86 -7
  43. package/dist/spec-cache.d.ts.map +1 -1
  44. package/dist/spec-cache.js +14 -32
  45. package/dist/spec-cache.js.map +1 -1
  46. package/dist/tracking-input.d.ts.map +1 -1
  47. package/dist/tracking-input.js +8 -2
  48. package/dist/tracking-input.js.map +1 -1
  49. package/package.json +5 -4
  50. package/schemas/package.json +4 -0
  51. package/src/claim-screening.ts +3 -9
  52. package/src/component-generator.ts +33 -82
  53. package/src/component-spec.ts +5 -18
  54. package/src/fallback-component.ts +4 -3
  55. package/src/fit-to-shopper.ts +16 -1
  56. package/src/index.ts +1 -2
  57. package/src/model-prompt.ts +24 -3
  58. package/src/product-display.ts +43 -0
  59. package/src/reconciliation.ts +16 -23
  60. package/src/schemas.ts +6 -0
  61. package/src/signal-digest.ts +2 -8
  62. package/src/spec-cache.ts +17 -41
  63. package/src/tracking-input.ts +11 -2
package/src/spec-cache.ts CHANGED
@@ -1,12 +1,14 @@
1
1
  import { createHash } from 'node:crypto';
2
- import { SPEC_VERSION, type GeneratedSpec } from './component-spec.js';
3
- import { SYSTEM_PROMPT } from './model-prompt.js';
4
- import type { SignalDigest } from './signal-digest.js';
2
+ import { z } from 'zod';
3
+ import { SPEC_VERSION, generatedSpecSchema } from './component-spec.js';
4
+ import type { PromptPair } from './model-prompt.js';
5
5
 
6
- export interface CachedSpec {
7
- spec: GeneratedSpec;
8
- generatedAt: number;
9
- }
6
+ export const cachedSpecSchema = z.object({
7
+ spec: generatedSpecSchema,
8
+ generatedAt: z.number(),
9
+ });
10
+
11
+ export type CachedSpec = z.infer<typeof cachedSpecSchema>;
10
12
 
11
13
  export interface SpecCache {
12
14
  get(key: string): Promise<CachedSpec | undefined>;
@@ -79,43 +81,17 @@ export function createNullSpecCache(): SpecCache {
79
81
  return { get: async () => undefined, set: async () => {} };
80
82
  }
81
83
 
82
- function canonicalise(value: unknown): string {
83
- if (value === null || typeof value !== 'object') return JSON.stringify(value) ?? 'null';
84
- if (Array.isArray(value)) return `[${value.map(canonicalise).join(',')}]`;
85
-
86
- const sorted = Object.entries(value as Record<string, unknown>).toSorted(([left], [right]) =>
87
- left < right ? -1 : 1,
88
- );
89
- const fields: string[] = [];
90
- for (const [name, fieldValue] of sorted) {
91
- if (fieldValue === undefined) continue;
92
- fields.push(`${JSON.stringify(name)}:${canonicalise(fieldValue)}`);
93
- }
94
- return `{${fields.join(',')}}`;
95
- }
96
-
97
- const PROMPT_FINGERPRINT = createHash('sha256').update(SYSTEM_PROMPT).digest('hex').slice(0, 16);
98
-
99
- function withoutDwellAndScores(digest: SignalDigest) {
100
- return {
101
- ...digest,
102
- topViewed: digest.topViewed.map(({ dwellMs: _dwellMs, ...view }) => view),
103
- categoryAffinity: digest.categoryAffinity.map(({ score: _score, ...affinity }) => affinity),
104
- };
105
- }
106
-
107
84
  export function specCacheKey(
108
- digest: SignalDigest,
109
- candidateSkus: readonly string[],
85
+ prompt: PromptPair,
110
86
  provider: { name: string; model: string },
111
87
  ): string {
112
- const material = canonicalise({
113
- specVersion: SPEC_VERSION,
114
- prompt: PROMPT_FINGERPRINT,
115
- provider,
116
- digest: withoutDwellAndScores(digest),
117
- candidates: candidateSkus.toSorted(),
118
- });
88
+ const material = JSON.stringify([
89
+ SPEC_VERSION,
90
+ provider.name,
91
+ provider.model,
92
+ prompt.system,
93
+ prompt.user,
94
+ ]);
119
95
 
120
96
  return createHash('sha256').update(material).digest('hex').slice(0, 32);
121
97
  }
@@ -23,6 +23,15 @@ const MAX_EPOCH_MS = Date.UTC(2100, 0, 1);
23
23
  const identifier = () => z.string().min(1).max(FIELD_LIMITS.identifier);
24
24
  const optionalIdentifier = () => identifier().optional();
25
25
 
26
+ // In u mode a surrogate pair reads as one code point, so this matches only an unpaired half.
27
+ const hasLoneSurrogate = (value: string) => /\p{Surrogate}/u.test(value);
28
+
29
+ // Product links run the SKU through encodeURIComponent, which throws on a lone surrogate.
30
+ const skuIdentifier = () =>
31
+ identifier().refine((value) => !hasLoneSurrogate(value), {
32
+ message: 'expected well-formed Unicode, with no unpaired surrogate',
33
+ });
34
+
26
35
  const currencyCode = () =>
27
36
  z
28
37
  .string()
@@ -48,7 +57,7 @@ const imageReference = () =>
48
57
  );
49
58
 
50
59
  export const productSchema = z.strictObject({
51
- sku: identifier(),
60
+ sku: skuIdentifier(),
52
61
  title: z.string().min(1).max(FIELD_LIMITS.shortText),
53
62
  category: identifier(),
54
63
  price: z.number().nonnegative(),
@@ -143,7 +152,7 @@ export type TrackingSignals = z.infer<typeof trackingSignalsSchema>;
143
152
  export const bundleSchema = z.strictObject({
144
153
  id: identifier(),
145
154
  skus: z
146
- .array(identifier())
155
+ .array(skuIdentifier())
147
156
  .min(2)
148
157
  .max(FIELD_LIMITS.productsPerBundle)
149
158
  .refine(isUnique, { message: 'a bundle must not list the same product twice' }),