@rudra-js/core 0.1.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/LICENSE +21 -0
  2. package/README.md +189 -0
  3. package/dist/component-generator.d.ts +84 -0
  4. package/dist/component-generator.d.ts.map +1 -0
  5. package/dist/component-generator.js +331 -0
  6. package/dist/component-generator.js.map +1 -0
  7. package/dist/component-spec.d.ts +426 -0
  8. package/dist/component-spec.d.ts.map +1 -0
  9. package/dist/component-spec.js +170 -0
  10. package/dist/component-spec.js.map +1 -0
  11. package/dist/fallback-component.d.ts +11 -0
  12. package/dist/fallback-component.d.ts.map +1 -0
  13. package/dist/fallback-component.js +69 -0
  14. package/dist/fallback-component.js.map +1 -0
  15. package/dist/fit-to-shopper.d.ts +4 -0
  16. package/dist/fit-to-shopper.d.ts.map +1 -0
  17. package/dist/fit-to-shopper.js +36 -0
  18. package/dist/fit-to-shopper.js.map +1 -0
  19. package/dist/index.d.ts +19 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +19 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/model-prompt.d.ts +29 -0
  24. package/dist/model-prompt.d.ts.map +1 -0
  25. package/dist/model-prompt.js +227 -0
  26. package/dist/model-prompt.js.map +1 -0
  27. package/dist/product-selection.d.ts +33 -0
  28. package/dist/product-selection.d.ts.map +1 -0
  29. package/dist/product-selection.js +102 -0
  30. package/dist/product-selection.js.map +1 -0
  31. package/dist/provider.d.ts +80 -0
  32. package/dist/provider.d.ts.map +1 -0
  33. package/dist/provider.js +23 -0
  34. package/dist/provider.js.map +1 -0
  35. package/dist/reconciliation.d.ts +49 -0
  36. package/dist/reconciliation.d.ts.map +1 -0
  37. package/dist/reconciliation.js +564 -0
  38. package/dist/reconciliation.js.map +1 -0
  39. package/dist/signal-digest.d.ts +66 -0
  40. package/dist/signal-digest.d.ts.map +1 -0
  41. package/dist/signal-digest.js +224 -0
  42. package/dist/signal-digest.js.map +1 -0
  43. package/dist/spec-cache.d.ts +88 -0
  44. package/dist/spec-cache.d.ts.map +1 -0
  45. package/dist/spec-cache.js +152 -0
  46. package/dist/spec-cache.js.map +1 -0
  47. package/dist/tracking-input.d.ts +258 -0
  48. package/dist/tracking-input.d.ts.map +1 -0
  49. package/dist/tracking-input.js +241 -0
  50. package/dist/tracking-input.js.map +1 -0
  51. package/package.json +60 -0
  52. package/src/component-generator.ts +521 -0
  53. package/src/component-spec.ts +243 -0
  54. package/src/fallback-component.ts +77 -0
  55. package/src/fit-to-shopper.ts +45 -0
  56. package/src/index.ts +102 -0
  57. package/src/model-prompt.ts +258 -0
  58. package/src/product-selection.ts +153 -0
  59. package/src/provider.ts +98 -0
  60. package/src/reconciliation.ts +675 -0
  61. package/src/signal-digest.ts +335 -0
  62. package/src/spec-cache.ts +223 -0
  63. package/src/tracking-input.ts +300 -0
@@ -0,0 +1,80 @@
1
+ import type { z } from 'zod';
2
+ import type { GeneratedSpec } from './component-spec.js';
3
+ /**
4
+ * The language-model port.
5
+ *
6
+ * `@rudra-js/core` depends on no vendor SDK. Adapters live in their own packages,
7
+ * so a host installs exactly one and the rest never reach its dependency tree.
8
+ * Anything satisfying this interface works — a hosted API, a self-hosted model,
9
+ * an in-tenancy deployment, or a recorded fixture.
10
+ */
11
+ /** Reported by an adapter for cost accounting. Never used for control flow. */
12
+ export interface TokenUsage {
13
+ inputTokens?: number;
14
+ outputTokens?: number;
15
+ cacheReadTokens?: number;
16
+ cacheWriteTokens?: number;
17
+ }
18
+ export interface ProviderRequest {
19
+ /**
20
+ * Stable across every request in a deployment. Adapters that support prompt
21
+ * caching should mark this as the cached prefix; interpolating anything
22
+ * per-shopper into it would silently destroy the cache hit rate.
23
+ */
24
+ system: string;
25
+ /** Per-request content. Must not be merged into the cached prefix. */
26
+ user: string;
27
+ /**
28
+ * The schema the response must satisfy. Adapters convert it with their own
29
+ * SDK helper, so there is no hand-maintained JSON Schema to drift out of
30
+ * sync with the one `component-spec` defines.
31
+ */
32
+ schema: z.ZodType<GeneratedSpec>;
33
+ /** Fires when the caller's budget elapses. An adapter must stop work. */
34
+ signal: AbortSignal;
35
+ }
36
+ export interface ProviderResult {
37
+ spec: GeneratedSpec;
38
+ usage?: TokenUsage;
39
+ }
40
+ /**
41
+ * What an adapter promises.
42
+ *
43
+ * Three obligations, none of which the caller can verify from the outside, so
44
+ * they are stated here rather than assumed:
45
+ *
46
+ * 1. Return a parsed object, not a string. Turning model output into a
47
+ * `GeneratedSpec` is the adapter's job, because only it knows what its
48
+ * structured-output mode returns.
49
+ * 2. Throw on failure — a refusal, a transport error, an unparseable
50
+ * response. Never return a partial or invented spec. The caller treats a
51
+ * throw as "use the deterministic component", which is always safe; a
52
+ * fabricated spec is not.
53
+ * 3. Respect `signal`, in both directions: do not start work when it is
54
+ * already aborted, and stop when it aborts mid-flight. The caller races
55
+ * the call against its own timeout regardless, so ignoring it does not
56
+ * hold a page open — but it does leave work running and billing after
57
+ * nobody is waiting, and it answers a caller that has already given up.
58
+ *
59
+ * `AbortSignal.throwIfAborted()` is the whole of the first half.
60
+ *
61
+ * The caller re-validates whatever comes back. An adapter that returns
62
+ * something malformed is a bug, not a security hole.
63
+ */
64
+ export interface ComponentProvider {
65
+ /** Short identifier recorded on every generated spec, e.g. 'anthropic'. */
66
+ readonly name: string;
67
+ /** Concrete model identifier, e.g. 'claude-opus-5'. */
68
+ readonly model: string;
69
+ generate(request: ProviderRequest): Promise<ProviderResult>;
70
+ }
71
+ /**
72
+ * A provider that returns a fixed spec and never touches the network.
73
+ *
74
+ * This is the benchmark's control: it isolates the cost of the framework from
75
+ * the latency of a model, which is the only way to report what server-side
76
+ * rendering itself costs. It is also what the generator's tests run against,
77
+ * since a test that needs an API key is a test nobody runs.
78
+ */
79
+ export declare function createFixedSpecProvider(spec: GeneratedSpec): ComponentProvider;
80
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;;;;;GAOG;AAEH,+EAA+E;AAC/E,MAAM,WAAW,UAAU;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,MAAM,WAAW,eAAe;IAC9B;;;;OAIG;IACH,MAAM,EAAE,MAAM,CAAC;IACf,sEAAsE;IACtE,IAAI,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;IACjC,yEAAyE;IACzE,MAAM,EAAE,WAAW,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,aAAa,CAAC;IACpB,KAAK,CAAC,EAAE,UAAU,CAAC;CACpB;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,WAAW,iBAAiB;IAChC,2EAA2E;IAC3E,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,uDAAuD;IACvD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;CAC7D;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,aAAa,GAAG,iBAAiB,CAa9E"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * A provider that returns a fixed spec and never touches the network.
3
+ *
4
+ * This is the benchmark's control: it isolates the cost of the framework from
5
+ * the latency of a model, which is the only way to report what server-side
6
+ * rendering itself costs. It is also what the generator's tests run against,
7
+ * since a test that needs an API key is a test nobody runs.
8
+ */
9
+ export function createFixedSpecProvider(spec) {
10
+ return {
11
+ name: 'fixed',
12
+ model: 'none',
13
+ generate: async ({ signal }) => {
14
+ // Nothing here is slow enough to need cancelling, which is exactly why it
15
+ // is worth checking: the obligation is not "cancel your work", it is "do
16
+ // not answer a caller that has already given up". A reference
17
+ // implementation that skips the cheap half teaches the wrong lesson.
18
+ signal.throwIfAborted();
19
+ return { spec };
20
+ },
21
+ };
22
+ }
23
+ //# sourceMappingURL=provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AA4EA;;;;;;;GAOG;AACH,MAAM,UAAU,uBAAuB,CAAC,IAAmB;IACzD,OAAO;QACL,IAAI,EAAE,OAAO;QACb,KAAK,EAAE,MAAM;QACb,QAAQ,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE;YAC7B,0EAA0E;YAC1E,yEAAyE;YACzE,8DAA8D;YAC9D,qEAAqE;YACrE,MAAM,CAAC,cAAc,EAAE,CAAC;YACxB,OAAO,EAAE,IAAI,EAAE,CAAC;QAClB,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,49 @@
1
+ import type { Block, GeneratedSpec } from './component-spec.js';
2
+ import type { SignalDigest } from './signal-digest.js';
3
+ import type { Bundle, TrackingInput } from './tracking-input.js';
4
+ /**
5
+ * More than this and the component stops being a component.
6
+ *
7
+ * Exported because the fill pass has to read the same blocks this module will:
8
+ * one past the cap never renders, so nothing is worth reserving for it.
9
+ */
10
+ export declare const MAX_BLOCKS = 4;
11
+ export interface ReconcileResult {
12
+ spec: GeneratedSpec;
13
+ /** True when something survived that is worth rendering. */
14
+ isUsable: boolean;
15
+ /** Machine-readable notes on what was removed or changed, for evaluation. */
16
+ violations: string[];
17
+ }
18
+ /**
19
+ * SKUs that must never be recommended, whatever chose them.
20
+ *
21
+ * Exported because the deterministic selector applies the same rule when it
22
+ * picks. Two copies of "never recommend these" would drift, and the pair that
23
+ * drifted would be the model path and the fallback path — the two whose
24
+ * comparability the whole evaluation depends on.
25
+ */
26
+ export declare function neverRecommend(digest: SignalDigest): Set<string>;
27
+ /**
28
+ * The set this shopper should get, decided before anything is placed.
29
+ *
30
+ * The generator needs the answer early, so it can keep the set's products out
31
+ * of the grid and keep room for them. `chooseBundle` stays private: this hands
32
+ * out the choice, not the machinery behind it.
33
+ *
34
+ * `spokenFor` is what the blocks above the bundle block will have placed by the
35
+ * time it is reached. Placing it here first is what makes the two choices agree:
36
+ * a set is only pre-chosen if reconciliation could still reach for it.
37
+ */
38
+ export declare function bundleForShopper(input: TrackingInput, digest: SignalDigest, spokenFor: readonly string[]): Bundle | undefined;
39
+ /**
40
+ * The hero products these blocks will really place.
41
+ *
42
+ * A hero keeps the product the model named — its headline was written about
43
+ * that product — so it spends a slot of the item budget the grid cannot have.
44
+ * One this shopper cannot see is dropped below and spends nothing, so it is not
45
+ * counted here either.
46
+ */
47
+ export declare function placeableHeroSkus(blocks: readonly Block[], input: TrackingInput, digest: SignalDigest): string[];
48
+ export declare function reconcileSpec(generated: GeneratedSpec, input: TrackingInput, digest: SignalDigest): ReconcileResult;
49
+ //# sourceMappingURL=reconciliation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reconciliation.d.ts","sourceRoot":"","sources":["../src/reconciliation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,KAAK,EACL,aAAa,EAGd,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EAAE,MAAM,EAAW,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAqC1E;;;;;GAKG;AACH,eAAO,MAAM,UAAU,IAAI,CAAC;AAE5B,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,aAAa,CAAC;IACpB,4DAA4D;IAC5D,QAAQ,EAAE,OAAO,CAAC;IAClB,6EAA6E;IAC7E,UAAU,EAAE,MAAM,EAAE,CAAC;CACtB;AA2BD;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,YAAY,GAAG,GAAG,CAAC,MAAM,CAAC,CAQhE;AAwVD;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,aAAa,EACpB,MAAM,EAAE,YAAY,EACpB,SAAS,EAAE,SAAS,MAAM,EAAE,GAC3B,MAAM,GAAG,SAAS,CAQpB;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,SAAS,KAAK,EAAE,EACxB,KAAK,EAAE,aAAa,EACpB,MAAM,EAAE,YAAY,GACnB,MAAM,EAAE,CAcV;AAoID,wBAAgB,aAAa,CAC3B,SAAS,EAAE,aAAa,EACxB,KAAK,EAAE,aAAa,EACpB,MAAM,EAAE,YAAY,GACnB,eAAe,CA0CjB"}