@rudra-js/core 0.9.1 → 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.
- package/README.md +58 -42
- package/dist/claim-screening.d.ts.map +1 -1
- package/dist/claim-screening.js +3 -9
- package/dist/claim-screening.js.map +1 -1
- package/dist/component-generator.d.ts +1 -1
- package/dist/component-generator.d.ts.map +1 -1
- package/dist/component-generator.js +28 -61
- package/dist/component-generator.js.map +1 -1
- package/dist/component-spec.d.ts +100 -11
- package/dist/component-spec.d.ts.map +1 -1
- package/dist/component-spec.js +3 -6
- package/dist/component-spec.js.map +1 -1
- package/dist/fallback-component.d.ts.map +1 -1
- package/dist/fallback-component.js +3 -3
- package/dist/fallback-component.js.map +1 -1
- package/dist/fit-to-shopper.d.ts +4 -1
- package/dist/fit-to-shopper.d.ts.map +1 -1
- package/dist/fit-to-shopper.js +8 -0
- package/dist/fit-to-shopper.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/model-prompt.d.ts.map +1 -1
- package/dist/model-prompt.js +16 -3
- package/dist/model-prompt.js.map +1 -1
- package/dist/product-display.d.ts +6 -0
- package/dist/product-display.d.ts.map +1 -0
- package/dist/product-display.js +32 -0
- package/dist/product-display.js.map +1 -0
- package/dist/reconciliation.d.ts +2 -4
- package/dist/reconciliation.d.ts.map +1 -1
- package/dist/reconciliation.js +15 -21
- package/dist/reconciliation.js.map +1 -1
- package/dist/schemas.d.ts +1 -0
- package/dist/schemas.d.ts.map +1 -1
- package/dist/schemas.js +1 -0
- package/dist/schemas.js.map +1 -1
- package/dist/signal-digest.d.ts.map +1 -1
- package/dist/signal-digest.js +2 -10
- package/dist/signal-digest.js.map +1 -1
- package/dist/spec-cache.d.ts +86 -7
- package/dist/spec-cache.d.ts.map +1 -1
- package/dist/spec-cache.js +14 -32
- package/dist/spec-cache.js.map +1 -1
- package/dist/tracking-input.d.ts.map +1 -1
- package/dist/tracking-input.js +8 -2
- package/dist/tracking-input.js.map +1 -1
- package/package.json +5 -4
- package/schemas/package.json +4 -0
- package/src/claim-screening.ts +3 -9
- package/src/component-generator.ts +33 -82
- package/src/component-spec.ts +5 -18
- package/src/fallback-component.ts +4 -3
- package/src/fit-to-shopper.ts +16 -1
- package/src/index.ts +1 -2
- package/src/model-prompt.ts +24 -3
- package/src/product-display.ts +43 -0
- package/src/reconciliation.ts +16 -23
- package/src/schemas.ts +6 -0
- package/src/signal-digest.ts +2 -8
- package/src/spec-cache.ts +17 -41
- package/src/tracking-input.ts +11 -2
package/dist/spec-cache.d.ts
CHANGED
|
@@ -1,9 +1,88 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
3
|
-
export
|
|
4
|
-
spec:
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { PromptPair } from './model-prompt.js';
|
|
3
|
+
export declare const cachedSpecSchema: z.ZodObject<{
|
|
4
|
+
spec: z.ZodObject<{
|
|
5
|
+
tone: z.ZodEnum<{
|
|
6
|
+
editorial: "editorial";
|
|
7
|
+
enthusiastic: "enthusiastic";
|
|
8
|
+
neutral: "neutral";
|
|
9
|
+
urgent: "urgent";
|
|
10
|
+
}>;
|
|
11
|
+
headline: z.ZodString;
|
|
12
|
+
subheadline: z.ZodNullable<z.ZodString>;
|
|
13
|
+
blocks: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
14
|
+
kind: z.ZodLiteral<"hero">;
|
|
15
|
+
headline: z.ZodString;
|
|
16
|
+
body: z.ZodNullable<z.ZodString>;
|
|
17
|
+
sku: z.ZodNullable<z.ZodString>;
|
|
18
|
+
ctaLabel: z.ZodNullable<z.ZodString>;
|
|
19
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
20
|
+
kind: z.ZodLiteral<"grid">;
|
|
21
|
+
title: z.ZodNullable<z.ZodString>;
|
|
22
|
+
columns: z.ZodUnion<readonly [z.ZodLiteral<2>, z.ZodLiteral<3>, z.ZodLiteral<4>]>;
|
|
23
|
+
items: z.ZodArray<z.ZodObject<{
|
|
24
|
+
sku: z.ZodString;
|
|
25
|
+
basis: z.ZodEnum<{
|
|
26
|
+
complements_cart: "complements_cart";
|
|
27
|
+
complements_purchase: "complements_purchase";
|
|
28
|
+
liked_category: "liked_category";
|
|
29
|
+
most_viewed: "most_viewed";
|
|
30
|
+
popular: "popular";
|
|
31
|
+
similar_to_current: "similar_to_current";
|
|
32
|
+
}>;
|
|
33
|
+
reason: z.ZodNullable<z.ZodString>;
|
|
34
|
+
badge: z.ZodNullable<z.ZodString>;
|
|
35
|
+
emphasis: z.ZodEnum<{
|
|
36
|
+
featured: "featured";
|
|
37
|
+
normal: "normal";
|
|
38
|
+
}>;
|
|
39
|
+
}, z.core.$strip>>;
|
|
40
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
41
|
+
kind: z.ZodLiteral<"carousel">;
|
|
42
|
+
title: z.ZodNullable<z.ZodString>;
|
|
43
|
+
items: z.ZodArray<z.ZodObject<{
|
|
44
|
+
sku: z.ZodString;
|
|
45
|
+
basis: z.ZodEnum<{
|
|
46
|
+
complements_cart: "complements_cart";
|
|
47
|
+
complements_purchase: "complements_purchase";
|
|
48
|
+
liked_category: "liked_category";
|
|
49
|
+
most_viewed: "most_viewed";
|
|
50
|
+
popular: "popular";
|
|
51
|
+
similar_to_current: "similar_to_current";
|
|
52
|
+
}>;
|
|
53
|
+
reason: z.ZodNullable<z.ZodString>;
|
|
54
|
+
badge: z.ZodNullable<z.ZodString>;
|
|
55
|
+
emphasis: z.ZodEnum<{
|
|
56
|
+
featured: "featured";
|
|
57
|
+
normal: "normal";
|
|
58
|
+
}>;
|
|
59
|
+
}, z.core.$strip>>;
|
|
60
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
61
|
+
kind: z.ZodLiteral<"banner">;
|
|
62
|
+
tone: z.ZodEnum<{
|
|
63
|
+
info: "info";
|
|
64
|
+
promo: "promo";
|
|
65
|
+
restock: "restock";
|
|
66
|
+
urgency: "urgency";
|
|
67
|
+
}>;
|
|
68
|
+
text: z.ZodString;
|
|
69
|
+
ctaLabel: z.ZodNullable<z.ZodString>;
|
|
70
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
71
|
+
kind: z.ZodLiteral<"copy">;
|
|
72
|
+
title: z.ZodNullable<z.ZodString>;
|
|
73
|
+
body: z.ZodString;
|
|
74
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
75
|
+
kind: z.ZodLiteral<"bundle">;
|
|
76
|
+
title: z.ZodNullable<z.ZodString>;
|
|
77
|
+
body: z.ZodNullable<z.ZodString>;
|
|
78
|
+
ctaLabel: z.ZodNullable<z.ZodString>;
|
|
79
|
+
bundleId: z.ZodNullable<z.ZodString>;
|
|
80
|
+
}, z.core.$strip>], "kind">>;
|
|
81
|
+
rationale: z.ZodString;
|
|
82
|
+
}, z.core.$strip>;
|
|
83
|
+
generatedAt: z.ZodNumber;
|
|
84
|
+
}, z.core.$strip>;
|
|
85
|
+
export type CachedSpec = z.infer<typeof cachedSpecSchema>;
|
|
7
86
|
export interface SpecCache {
|
|
8
87
|
get(key: string): Promise<CachedSpec | undefined>;
|
|
9
88
|
set(key: string, cached: CachedSpec): Promise<void>;
|
|
@@ -16,7 +95,7 @@ export interface MemorySpecCacheOptions {
|
|
|
16
95
|
}
|
|
17
96
|
export declare function createMemorySpecCache(options?: MemorySpecCacheOptions): SpecCache;
|
|
18
97
|
export declare function createNullSpecCache(): SpecCache;
|
|
19
|
-
export declare function specCacheKey(
|
|
98
|
+
export declare function specCacheKey(prompt: PromptPair, provider: {
|
|
20
99
|
name: string;
|
|
21
100
|
model: string;
|
|
22
101
|
}): string;
|
package/dist/spec-cache.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spec-cache.d.ts","sourceRoot":"","sources":["../src/spec-cache.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"spec-cache.d.ts","sourceRoot":"","sources":["../src/spec-cache.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAEpD,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAG3B,CAAC;AAEH,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAE1D,MAAM,WAAW,SAAS;IACxB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC,CAAC;IAClD,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACpD,MAAM,CAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACrC;AAED,MAAM,WAAW,sBAAsB;IACrC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AAgBD,wBAAgB,qBAAqB,CAAC,OAAO,GAAE,sBAA2B,GAAG,SAAS,CAuCrF;AAED,wBAAgB,mBAAmB,IAAI,SAAS,CAE/C;AAED,wBAAgB,YAAY,CAC1B,MAAM,EAAE,UAAU,EAClB,QAAQ,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACxC,MAAM,CAUR"}
|
package/dist/spec-cache.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { SPEC_VERSION, generatedSpecSchema } from './component-spec.js';
|
|
4
|
+
export const cachedSpecSchema = z.object({
|
|
5
|
+
spec: generatedSpecSchema,
|
|
6
|
+
generatedAt: z.number(),
|
|
7
|
+
});
|
|
4
8
|
function assertFiniteAtLeastZero(name, value) {
|
|
5
9
|
if (Number.isFinite(value) && value >= 0)
|
|
6
10
|
return;
|
|
@@ -48,36 +52,14 @@ export function createMemorySpecCache(options = {}) {
|
|
|
48
52
|
export function createNullSpecCache() {
|
|
49
53
|
return { get: async () => undefined, set: async () => { } };
|
|
50
54
|
}
|
|
51
|
-
function
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (fieldValue === undefined)
|
|
60
|
-
continue;
|
|
61
|
-
fields.push(`${JSON.stringify(name)}:${canonicalise(fieldValue)}`);
|
|
62
|
-
}
|
|
63
|
-
return `{${fields.join(',')}}`;
|
|
64
|
-
}
|
|
65
|
-
const PROMPT_FINGERPRINT = createHash('sha256').update(SYSTEM_PROMPT).digest('hex').slice(0, 16);
|
|
66
|
-
function withoutDwellAndScores(digest) {
|
|
67
|
-
return {
|
|
68
|
-
...digest,
|
|
69
|
-
topViewed: digest.topViewed.map(({ dwellMs: _dwellMs, ...view }) => view),
|
|
70
|
-
categoryAffinity: digest.categoryAffinity.map(({ score: _score, ...affinity }) => affinity),
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
export function specCacheKey(digest, candidateSkus, provider) {
|
|
74
|
-
const material = canonicalise({
|
|
75
|
-
specVersion: SPEC_VERSION,
|
|
76
|
-
prompt: PROMPT_FINGERPRINT,
|
|
77
|
-
provider,
|
|
78
|
-
digest: withoutDwellAndScores(digest),
|
|
79
|
-
candidates: candidateSkus.toSorted(),
|
|
80
|
-
});
|
|
55
|
+
export function specCacheKey(prompt, provider) {
|
|
56
|
+
const material = JSON.stringify([
|
|
57
|
+
SPEC_VERSION,
|
|
58
|
+
provider.name,
|
|
59
|
+
provider.model,
|
|
60
|
+
prompt.system,
|
|
61
|
+
prompt.user,
|
|
62
|
+
]);
|
|
81
63
|
return createHash('sha256').update(material).digest('hex').slice(0, 32);
|
|
82
64
|
}
|
|
83
65
|
//# sourceMappingURL=spec-cache.js.map
|
package/dist/spec-cache.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spec-cache.js","sourceRoot":"","sources":["../src/spec-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"spec-cache.js","sourceRoot":"","sources":["../src/spec-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAGxE,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC,MAAM,CAAC;IACvC,IAAI,EAAE,mBAAmB;IACzB,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE;CACxB,CAAC,CAAC;AAgBH,SAAS,uBAAuB,CAAC,IAAY,EAAE,KAAa;IAC1D,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC;QAAE,OAAO;IAEjD,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC;QAC9B,CAAC,CAAC,0EAA0E;QAC5E,CAAC,CAAC,EAAE,CAAC;IACP,MAAM,IAAI,UAAU,CAAC,GAAG,IAAI,oDAAoD,KAAK,GAAG,IAAI,EAAE,CAAC,CAAC;AAClG,CAAC;AAOD,MAAM,UAAU,qBAAqB,CAAC,OAAO,GAA2B,EAAE;IACxE,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK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O,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAQhC,CAAC;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAC;AAEpE,eAAO,MAAM,YAAY;;;;;;kBAUvB,CAAC;AACH,MAAM,MAAM,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,YAAY,CAAC,CAAC;AAElD,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAoC7B,CAAC;AAEJ,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAEhE,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAErE,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,kBAAkB,CAAC,aAAa,CAAC,CAAC;AAEtE,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,CAEhE;AAED,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,mBAAmB,CAE1E"}
|
package/dist/tracking-input.js
CHANGED
|
@@ -18,6 +18,12 @@ const RESERVED_META_KEY = '__proto__';
|
|
|
18
18
|
const MAX_EPOCH_MS = Date.UTC(2100, 0, 1);
|
|
19
19
|
const identifier = () => z.string().min(1).max(FIELD_LIMITS.identifier);
|
|
20
20
|
const optionalIdentifier = () => identifier().optional();
|
|
21
|
+
// In u mode a surrogate pair reads as one code point, so this matches only an unpaired half.
|
|
22
|
+
const hasLoneSurrogate = (value) => /\p{Surrogate}/u.test(value);
|
|
23
|
+
// Product links run the SKU through encodeURIComponent, which throws on a lone surrogate.
|
|
24
|
+
const skuIdentifier = () => identifier().refine((value) => !hasLoneSurrogate(value), {
|
|
25
|
+
message: 'expected well-formed Unicode, with no unpaired surrogate',
|
|
26
|
+
});
|
|
21
27
|
const currencyCode = () => z
|
|
22
28
|
.string()
|
|
23
29
|
.regex(/^[A-Z]{3}$/, 'expected a three-letter ISO 4217 code')
|
|
@@ -33,7 +39,7 @@ const imageReference = () => z
|
|
|
33
39
|
.max(FIELD_LIMITS.shortText)
|
|
34
40
|
.refine((value) => isRootRelative(value) || z.url({ protocol: /^https?$/ }).safeParse(value).success, { message: 'expected an http(s) URL or a root-relative path' });
|
|
35
41
|
export const productSchema = z.strictObject({
|
|
36
|
-
sku:
|
|
42
|
+
sku: skuIdentifier(),
|
|
37
43
|
title: z.string().min(1).max(FIELD_LIMITS.shortText),
|
|
38
44
|
category: identifier(),
|
|
39
45
|
price: z.number().nonnegative(),
|
|
@@ -101,7 +107,7 @@ export const trackingSignalsSchema = z.strictObject({
|
|
|
101
107
|
export const bundleSchema = z.strictObject({
|
|
102
108
|
id: identifier(),
|
|
103
109
|
skus: z
|
|
104
|
-
.array(
|
|
110
|
+
.array(skuIdentifier())
|
|
105
111
|
.min(2)
|
|
106
112
|
.max(FIELD_LIMITS.productsPerBundle)
|
|
107
113
|
.refine(isUnique, { message: 'a bundle must not list the same product twice' }),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tracking-input.js","sourceRoot":"","sources":["../src/tracking-input.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,UAAU,EAAE,GAAG;IACf,SAAS,EAAE,GAAG;IACd,WAAW,EAAE,GAAG;IAChB,GAAG,EAAE,EAAE;IACP,cAAc,EAAE,EAAE;IAClB,WAAW,EAAE,EAAE;IACf,kBAAkB,EAAE,GAAG;IACvB,UAAU,EAAE,GAAG;IACf,iBAAiB,EAAE,CAAC;IACpB,OAAO,EAAE,EAAE;IACX,SAAS,EAAE,EAAE;IACb,QAAQ,EAAE,EAAE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,MAAM,CAAC,IAAI,EAAE,CAAC;YAC9B,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC;gBAAE,OAAO,KAAK,CAAC;QAC5C,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC,EACD,EAAE,OAAO,EAAE,oDAAoD,EAAE,CAClE,CAAC;AAQJ,MAAM,UAAU,kBAAkB,CAAC,KAAc;IAC/C,OAAO,mBAAmB,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AAC1C,CAAC;AAED,MAAM,UAAU,sBAAsB,CAAC,KAAc;IACnD,OAAO,mBAAmB,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;AAC9C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rudra-js/core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.10.0",
|
|
4
|
+
"description": "The core of rudra-js. Lets a language model design product recommendations for a page and checks them against your own products.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"rudra-js",
|
|
7
7
|
"product-recommendations",
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
},
|
|
44
44
|
"files": [
|
|
45
45
|
"dist",
|
|
46
|
+
"schemas",
|
|
46
47
|
"src/**/*.ts",
|
|
47
48
|
"src/**/*.tsx",
|
|
48
49
|
"!src/**/*.test.ts",
|
|
@@ -60,10 +61,10 @@
|
|
|
60
61
|
"zod": "^4.6.7"
|
|
61
62
|
},
|
|
62
63
|
"peerDependencies": {
|
|
63
|
-
"@rudra-js/attested": "^0.
|
|
64
|
+
"@rudra-js/attested": "^0.10.0"
|
|
64
65
|
},
|
|
65
66
|
"devDependencies": {
|
|
66
|
-
"@rudra-js/attested": "0.
|
|
67
|
+
"@rudra-js/attested": "0.10.0",
|
|
67
68
|
"@types/node": "^26.4.1"
|
|
68
69
|
}
|
|
69
70
|
}
|
package/src/claim-screening.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { listLookalikeReadings, verify } from '@rudra-js/attested';
|
|
2
2
|
import { offeredCandidates } from './model-prompt.js';
|
|
3
3
|
import type { TrackingInput } from './tracking-input.js';
|
|
4
4
|
|
|
@@ -84,7 +84,6 @@ export const ALLOWED_PHRASES = [
|
|
|
84
84
|
];
|
|
85
85
|
|
|
86
86
|
const FACT_DIGIT = /\p{Nd}/u;
|
|
87
|
-
const NUMERAL = /[\p{Nd}\p{No}\p{Nl}]/u;
|
|
88
87
|
|
|
89
88
|
export const NO_FACTS: readonly string[] = [];
|
|
90
89
|
|
|
@@ -114,11 +113,7 @@ function kindIn(reading: string): string | null {
|
|
|
114
113
|
}
|
|
115
114
|
|
|
116
115
|
function claimIn(text: string): string | null {
|
|
117
|
-
const
|
|
118
|
-
if (text.includes('\u04C0')) {
|
|
119
|
-
readings.add(foldLookalikes(text.replaceAll('\u04C0', '\u04CF'), true));
|
|
120
|
-
}
|
|
121
|
-
for (const reading of readings) {
|
|
116
|
+
for (const reading of listLookalikeReadings(text)) {
|
|
122
117
|
const kind = kindIn(reading);
|
|
123
118
|
if (kind !== null) return kind;
|
|
124
119
|
}
|
|
@@ -133,8 +128,7 @@ export function unbackedClaim(
|
|
|
133
128
|
const kind = claimIn(raw) ?? claimIn(clamped);
|
|
134
129
|
if (kind !== null) return kind;
|
|
135
130
|
|
|
136
|
-
const
|
|
137
|
-
const result = verify(clamped, { values, allowedPhrases: ALLOWED_PHRASES });
|
|
131
|
+
const result = verify(clamped, { values: facts, allowedPhrases: ALLOWED_PHRASES });
|
|
138
132
|
if (!result.quantity.supported) return 'quantity';
|
|
139
133
|
if (!result.wording.supported) return 'wording';
|
|
140
134
|
return null;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
1
|
import {
|
|
3
2
|
SPEC_VERSION,
|
|
4
3
|
generatedSpecSchema,
|
|
@@ -8,13 +7,14 @@ import {
|
|
|
8
7
|
type SpecSource,
|
|
9
8
|
} from './component-spec.js';
|
|
10
9
|
import { buildFallbackSpec } from './fallback-component.js';
|
|
11
|
-
import { buildPrompt } from './model-prompt.js';
|
|
10
|
+
import { buildPrompt, type PromptPair } from './model-prompt.js';
|
|
12
11
|
import type { ComponentProvider, TokenUsage } from './provider.js';
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
12
|
+
import { reconcileSpec } from './reconciliation.js';
|
|
13
|
+
import type { RankOrder } from './product-selection.js';
|
|
14
|
+
import { fitCohortSpec } from './fit-to-shopper.js';
|
|
16
15
|
import { buildDigest, toCohortDigest, type SignalDigest } from './signal-digest.js';
|
|
17
16
|
import {
|
|
17
|
+
cachedSpecSchema,
|
|
18
18
|
createMemorySpecCache,
|
|
19
19
|
specCacheKey,
|
|
20
20
|
type CachedSpec,
|
|
@@ -86,11 +86,6 @@ async function withinBudget<T>(
|
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
const cachedSpecSchema = z.object({
|
|
90
|
-
spec: generatedSpecSchema,
|
|
91
|
-
generatedAt: z.number(),
|
|
92
|
-
});
|
|
93
|
-
|
|
94
89
|
interface CacheRead {
|
|
95
90
|
outcome: NonNullable<GenerationEvent['cache']>;
|
|
96
91
|
entry?: CachedSpec;
|
|
@@ -102,13 +97,13 @@ interface ModelCall {
|
|
|
102
97
|
}
|
|
103
98
|
|
|
104
99
|
interface Settings {
|
|
105
|
-
options: ComponentGeneratorOptions;
|
|
106
100
|
provider: ComponentProvider | null;
|
|
107
101
|
cache: SpecCache;
|
|
108
|
-
generation: '
|
|
102
|
+
generation: NonNullable<ComponentGeneratorOptions['generation']>;
|
|
109
103
|
rank: RankOrder;
|
|
110
104
|
modelTimeoutMs: number;
|
|
111
105
|
cacheTimeoutMs: number;
|
|
106
|
+
onEvent: ComponentGeneratorOptions['onEvent'];
|
|
112
107
|
inFlight: Map<string, Promise<ModelCall>>;
|
|
113
108
|
}
|
|
114
109
|
|
|
@@ -119,59 +114,26 @@ interface Run {
|
|
|
119
114
|
startedAt: number;
|
|
120
115
|
}
|
|
121
116
|
|
|
122
|
-
interface Trace {
|
|
123
|
-
calledModel: boolean;
|
|
124
|
-
cache: CacheRead['outcome'];
|
|
125
|
-
usage: TokenUsage | undefined;
|
|
126
|
-
error?: unknown;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
117
|
type Found =
|
|
130
|
-
| { spec: GeneratedSpec; generatedAt: number; source: 'cache' | 'llm';
|
|
131
|
-
| { failed: DegradedReason;
|
|
118
|
+
| { spec: GeneratedSpec; generatedAt: number; source: 'cache' | 'llm'; details: EventDetails }
|
|
119
|
+
| { failed: DegradedReason; details: EventDetails };
|
|
132
120
|
|
|
133
121
|
type EventDetails = Pick<
|
|
134
122
|
GenerationEvent,
|
|
135
123
|
'calledModel' | 'usage' | 'violations' | 'cache' | 'error'
|
|
136
124
|
>;
|
|
137
125
|
|
|
138
|
-
function fitCohortSpec(
|
|
139
|
-
spec: GeneratedSpec,
|
|
140
|
-
input: TrackingInput,
|
|
141
|
-
digest: SignalDigest,
|
|
142
|
-
rank: RankOrder,
|
|
143
|
-
): FittedSpec {
|
|
144
|
-
const picks = selectProducts(input, digest, { rank });
|
|
145
|
-
const blocks = capBlocks(spec.blocks);
|
|
146
|
-
|
|
147
|
-
const bundleAt = blocks.findIndex((block) => block.kind === 'bundle');
|
|
148
|
-
if (bundleAt === -1) return fitToShopper(spec, picks, digest.maxItems);
|
|
149
|
-
|
|
150
|
-
const heroesAbove = placeableHeroSkus(blocks.slice(0, bundleAt), input);
|
|
151
|
-
const bundle = bundleForShopper(input, digest, heroesAbove);
|
|
152
|
-
if (!bundle) return fitToShopper(spec, picks, digest.maxItems);
|
|
153
|
-
|
|
154
|
-
const spokenFor = new Set<string>(bundle.skus);
|
|
155
|
-
for (const sku of placeableHeroSkus(blocks, input)) spokenFor.add(sku);
|
|
156
|
-
|
|
157
|
-
const roomLeft = digest.maxItems - spokenFor.size;
|
|
158
|
-
if (roomLeft <= 0) return fitToShopper(spec, picks, digest.maxItems);
|
|
159
|
-
|
|
160
|
-
const forGrid = picks.filter((pick) => !spokenFor.has(pick.product.sku));
|
|
161
|
-
return fitToShopper(spec, forGrid, roomLeft);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
126
|
export function createComponentGenerator(
|
|
165
127
|
options: ComponentGeneratorOptions = {},
|
|
166
128
|
): ComponentGenerator {
|
|
167
129
|
const settings: Settings = {
|
|
168
|
-
options,
|
|
169
130
|
provider: options.provider ?? null,
|
|
170
131
|
cache: options.cache ?? createMemorySpecCache(),
|
|
171
132
|
generation: options.generation ?? 'cohort',
|
|
172
133
|
rank: options.rank ?? 'signals',
|
|
173
134
|
modelTimeoutMs: options.modelTimeoutMs ?? 1_500,
|
|
174
135
|
cacheTimeoutMs: options.cacheTimeoutMs ?? 50,
|
|
136
|
+
onEvent: options.onEvent,
|
|
175
137
|
inFlight: new Map(),
|
|
176
138
|
};
|
|
177
139
|
|
|
@@ -186,13 +148,14 @@ async function generate(settings: Settings, draft: TrackingInputDraft): Promise<
|
|
|
186
148
|
const provider = settings.provider;
|
|
187
149
|
if (!provider) return fallback(settings, run, null, 'no-provider');
|
|
188
150
|
|
|
189
|
-
const
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
151
|
+
const prompt = buildPrompt(run.input, run.promptDigest);
|
|
152
|
+
const key = specCacheKey(prompt, provider);
|
|
153
|
+
const found = await findSpec(settings, provider, prompt, key);
|
|
154
|
+
if ('failed' in found) return fallback(settings, run, key, found.failed, found.details);
|
|
193
155
|
|
|
194
156
|
const reconciled = shapeForShopper(settings, run, found.spec);
|
|
195
|
-
const
|
|
157
|
+
const { usage, ...seen } = found.details;
|
|
158
|
+
const details = { ...seen, violations: reconciled.violations, ...(usage ? { usage } : {}) };
|
|
196
159
|
if (!reconciled.isUsable) return fallback(settings, run, key, 'unusable-on-serve', details);
|
|
197
160
|
|
|
198
161
|
const finishedAt = Date.now();
|
|
@@ -227,39 +190,34 @@ function startRun(settings: Settings, draft: TrackingInputDraft): Run {
|
|
|
227
190
|
return { input, digest, promptDigest, startedAt };
|
|
228
191
|
}
|
|
229
192
|
|
|
230
|
-
function cacheKeyFor(provider: ComponentProvider, run: Run): string {
|
|
231
|
-
const identity = { name: provider.name, model: provider.model };
|
|
232
|
-
const skus = run.input.candidates.map((product) => product.sku);
|
|
233
|
-
return specCacheKey(run.promptDigest, skus, identity);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
193
|
async function findSpec(
|
|
237
194
|
settings: Settings,
|
|
238
195
|
provider: ComponentProvider,
|
|
239
|
-
|
|
196
|
+
prompt: PromptPair,
|
|
240
197
|
key: string,
|
|
241
198
|
): Promise<Found> {
|
|
242
199
|
const read = await readCache(settings, key);
|
|
243
200
|
if (read.entry) {
|
|
244
|
-
const
|
|
245
|
-
return { spec: read.entry.spec, generatedAt: read.entry.generatedAt, source: 'cache',
|
|
201
|
+
const details = { calledModel: false, cache: read.outcome };
|
|
202
|
+
return { spec: read.entry.spec, generatedAt: read.entry.generatedAt, source: 'cache', details };
|
|
246
203
|
}
|
|
247
204
|
|
|
248
205
|
const calledModel = !settings.inFlight.has(key);
|
|
249
206
|
let call: ModelCall;
|
|
250
207
|
try {
|
|
251
|
-
call = await sharedCall(settings, key, () => askModel(settings, provider,
|
|
208
|
+
call = await sharedCall(settings, key, () => askModel(settings, provider, prompt));
|
|
252
209
|
} catch (error) {
|
|
253
210
|
const failed = error instanceof TimeoutError ? 'timeout' : 'provider-error';
|
|
254
|
-
return { failed,
|
|
211
|
+
return { failed, details: { calledModel, cache: read.outcome, error } };
|
|
255
212
|
}
|
|
256
213
|
|
|
257
|
-
const
|
|
258
|
-
if (
|
|
214
|
+
const details: EventDetails = { calledModel, cache: read.outcome };
|
|
215
|
+
if (call.usage) details.usage = call.usage;
|
|
216
|
+
if (!call.spec) return { failed: 'invalid-generation', details };
|
|
259
217
|
|
|
260
218
|
const generatedAt = Date.now();
|
|
261
219
|
if (calledModel) storeInBackground(settings, key, { spec: call.spec, generatedAt });
|
|
262
|
-
return { spec: call.spec, generatedAt, source: 'llm',
|
|
220
|
+
return { spec: call.spec, generatedAt, source: 'llm', details };
|
|
263
221
|
}
|
|
264
222
|
|
|
265
223
|
async function readCache(settings: Settings, key: string): Promise<CacheRead> {
|
|
@@ -277,11 +235,15 @@ async function readCache(settings: Settings, key: string): Promise<CacheRead> {
|
|
|
277
235
|
async function askModel(
|
|
278
236
|
settings: Settings,
|
|
279
237
|
provider: ComponentProvider,
|
|
280
|
-
|
|
238
|
+
prompt: PromptPair,
|
|
281
239
|
): Promise<ModelCall> {
|
|
282
|
-
const { system, user } = buildPrompt(run.input, run.promptDigest);
|
|
283
240
|
const result = await withinBudget('generation', settings.modelTimeoutMs, (signal) =>
|
|
284
|
-
provider.generate({
|
|
241
|
+
provider.generate({
|
|
242
|
+
system: prompt.system,
|
|
243
|
+
user: prompt.user,
|
|
244
|
+
schema: generatedSpecSchema,
|
|
245
|
+
signal,
|
|
246
|
+
}),
|
|
285
247
|
);
|
|
286
248
|
const parsed = generatedSpecSchema.safeParse(result.spec);
|
|
287
249
|
return {
|
|
@@ -345,19 +307,8 @@ function fallback(
|
|
|
345
307
|
};
|
|
346
308
|
}
|
|
347
309
|
|
|
348
|
-
function eventDetails(trace: Trace, violations?: string[]): EventDetails {
|
|
349
|
-
return {
|
|
350
|
-
calledModel: trace.calledModel,
|
|
351
|
-
cache: trace.cache,
|
|
352
|
-
...(violations ? { violations } : {}),
|
|
353
|
-
...(trace.usage ? { usage: trace.usage } : {}),
|
|
354
|
-
...('error' in trace ? { error: trace.error } : {}),
|
|
355
|
-
};
|
|
356
|
-
}
|
|
357
|
-
|
|
358
310
|
function report(settings: Settings, event: GenerationEvent): void {
|
|
359
|
-
if (!settings.options.onEvent) return;
|
|
360
311
|
try {
|
|
361
|
-
settings.
|
|
312
|
+
settings.onEvent?.(event);
|
|
362
313
|
} catch {}
|
|
363
314
|
}
|
package/src/component-spec.ts
CHANGED
|
@@ -40,6 +40,8 @@ const gridBlockSchema = z.object({
|
|
|
40
40
|
items: z.array(productReferenceSchema),
|
|
41
41
|
});
|
|
42
42
|
|
|
43
|
+
export const GRID_COLUMNS = gridBlockSchema.shape.columns.options.map((option) => option.value);
|
|
44
|
+
|
|
43
45
|
export function columnsFor(count: number, widest: 2 | 3 | 4): 2 | 3 | 4 {
|
|
44
46
|
return Math.min(widest, Math.max(2, count)) as 2 | 3 | 4;
|
|
45
47
|
}
|
|
@@ -114,17 +116,6 @@ export type DegradedReason = (typeof DEGRADED_REASONS)[number];
|
|
|
114
116
|
|
|
115
117
|
export const SPEC_VERSION = '1' as const;
|
|
116
118
|
|
|
117
|
-
export interface ComponentSpec extends GeneratedSpec {
|
|
118
|
-
specVersion: typeof SPEC_VERSION;
|
|
119
|
-
slot: string;
|
|
120
|
-
source: SpecSource;
|
|
121
|
-
generatedAt: number;
|
|
122
|
-
latencyMs: number;
|
|
123
|
-
provider: string | null;
|
|
124
|
-
model: string | null;
|
|
125
|
-
degradedReason?: DegradedReason;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
119
|
const componentSpecSchema = generatedSpecSchema.extend({
|
|
129
120
|
specVersion: z.literal(SPEC_VERSION),
|
|
130
121
|
slot: z.string(),
|
|
@@ -133,16 +124,12 @@ const componentSpecSchema = generatedSpecSchema.extend({
|
|
|
133
124
|
latencyMs: z.number(),
|
|
134
125
|
provider: z.string().nullable(),
|
|
135
126
|
model: z.string().nullable(),
|
|
136
|
-
degradedReason: z.enum(DEGRADED_REASONS).
|
|
127
|
+
degradedReason: z.enum(DEGRADED_REASONS).exactOptional(),
|
|
137
128
|
});
|
|
129
|
+
export type ComponentSpec = z.infer<typeof componentSpecSchema>;
|
|
138
130
|
|
|
139
131
|
export function parseComponentSpec(value: unknown): ComponentSpec {
|
|
140
|
-
|
|
141
|
-
return degradedReason === undefined ? spec : { ...spec, degradedReason };
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
export function parseGeneratedSpec(value: unknown): GeneratedSpec {
|
|
145
|
-
return generatedSpecSchema.parse(value);
|
|
132
|
+
return componentSpecSchema.parse(value);
|
|
146
133
|
}
|
|
147
134
|
|
|
148
135
|
export function safeParseGeneratedSpec(value: unknown): GeneratedSpecResult {
|
|
@@ -43,8 +43,9 @@ export function buildFallbackSpec(
|
|
|
43
43
|
items.length === 0
|
|
44
44
|
? []
|
|
45
45
|
: [{ kind: 'grid', title: null, columns: columnsFor(items.length, 4), items }],
|
|
46
|
-
rationale:
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
rationale:
|
|
47
|
+
options.rank === 'given'
|
|
48
|
+
? 'Deterministic: kept in the order the candidates were sent.'
|
|
49
|
+
: 'Deterministic: ranked by category interest (the browsed category counts too), views, rating and shared tags.',
|
|
49
50
|
};
|
|
50
51
|
}
|
package/src/fit-to-shopper.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { Block, GeneratedSpec, ProductReference } from './component-spec.js';
|
|
2
|
-
import type
|
|
2
|
+
import { selectProducts, type ProductPick, type RankOrder } from './product-selection.js';
|
|
3
|
+
import { heroAndSetSkus } from './reconciliation.js';
|
|
4
|
+
import type { SignalDigest } from './signal-digest.js';
|
|
5
|
+
import type { TrackingInput } from './tracking-input.js';
|
|
3
6
|
|
|
4
7
|
export interface FittedSpec {
|
|
5
8
|
spec: GeneratedSpec;
|
|
@@ -41,3 +44,15 @@ export function fitToShopper(
|
|
|
41
44
|
|
|
42
45
|
return { spec: { ...spec, blocks }, ourReasons };
|
|
43
46
|
}
|
|
47
|
+
|
|
48
|
+
export function fitCohortSpec(
|
|
49
|
+
spec: GeneratedSpec,
|
|
50
|
+
input: TrackingInput,
|
|
51
|
+
digest: SignalDigest,
|
|
52
|
+
rank: RankOrder,
|
|
53
|
+
): FittedSpec {
|
|
54
|
+
const spokenFor = heroAndSetSkus(spec.blocks, input, digest);
|
|
55
|
+
const picks = selectProducts(input, digest, { rank });
|
|
56
|
+
const forGrid = picks.filter((pick) => !spokenFor.has(pick.product.sku));
|
|
57
|
+
return fitToShopper(spec, forGrid, digest.maxItems - spokenFor.size);
|
|
58
|
+
}
|