@kaleabdenbel/llmweb 1.0.4 → 1.0.6

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.
@@ -0,0 +1,56 @@
1
+ type SourceType = "fetch" | "fn";
2
+ interface SourceDefinition {
3
+ type: SourceType;
4
+ url?: string;
5
+ call?: (...args: any[]) => Promise<any> | any;
6
+ }
7
+ interface MapSchema {
8
+ [targetKey: string]: string | ((sourceData: any) => any) | MapSchema;
9
+ }
10
+ interface DynamicSource {
11
+ from: SourceDefinition;
12
+ map?: MapSchema;
13
+ }
14
+ interface LLMConfig {
15
+ identity?: {
16
+ name: string;
17
+ description?: string;
18
+ brand?: string;
19
+ locale?: string;
20
+ };
21
+ navigation?: {
22
+ pages: Array<{
23
+ label: string;
24
+ path: string;
25
+ }>;
26
+ };
27
+ business?: {
28
+ address?: string;
29
+ email?: string;
30
+ twitter?: string;
31
+ linkedin?: string;
32
+ };
33
+ seo?: {
34
+ titlePattern?: string;
35
+ descriptionPattern?: string;
36
+ multilingual?: boolean;
37
+ };
38
+ contentTypes?: string[];
39
+ type?: string;
40
+ static?: string | Record<string, any>;
41
+ dynamic?: {
42
+ [key: string]: DynamicSource;
43
+ };
44
+ }
45
+ interface TransformerOptions {
46
+ failLoudly?: boolean;
47
+ timeout?: number;
48
+ }
49
+ interface LLMJsonProps {
50
+ config: LLMConfig;
51
+ mode?: "script" | "window" | "pre" | "ldjson";
52
+ targetKey?: string;
53
+ className?: string;
54
+ }
55
+
56
+ export type { DynamicSource as D, LLMConfig as L, MapSchema as M, SourceDefinition as S, TransformerOptions as T, LLMJsonProps as a, SourceType as b };
@@ -0,0 +1,10 @@
1
+ import { L as LLMConfig, T as TransformerOptions } from './index-C5RUlsf2.mjs';
2
+ export { D as DynamicSource, a as LLMJsonProps, M as MapSchema, S as SourceDefinition, b as SourceType } from './index-C5RUlsf2.mjs';
3
+ export { t as toLDJSONString, a as toScriptString, b as toWindowString, c as transform } from './injector-DD7MSHW4.mjs';
4
+
5
+ /**
6
+ * The main "Compiler" for LLM truth (Browser version).
7
+ */
8
+ declare function createLLMSource(config: LLMConfig, options?: TransformerOptions): Promise<any>;
9
+
10
+ export { LLMConfig, TransformerOptions, createLLMSource };
@@ -0,0 +1,10 @@
1
+ import { L as LLMConfig, T as TransformerOptions } from './index-C5RUlsf2.js';
2
+ export { D as DynamicSource, a as LLMJsonProps, M as MapSchema, S as SourceDefinition, b as SourceType } from './index-C5RUlsf2.js';
3
+ export { t as toLDJSONString, a as toScriptString, b as toWindowString, c as transform } from './injector-Gh9wJgRZ.js';
4
+
5
+ /**
6
+ * The main "Compiler" for LLM truth (Browser version).
7
+ */
8
+ declare function createLLMSource(config: LLMConfig, options?: TransformerOptions): Promise<any>;
9
+
10
+ export { LLMConfig, TransformerOptions, createLLMSource };
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.browser.ts
21
+ var index_browser_exports = {};
22
+ __export(index_browser_exports, {
23
+ createLLMSource: () => createLLMSource,
24
+ toLDJSONString: () => toLDJSONString,
25
+ toScriptString: () => toScriptString,
26
+ toWindowString: () => toWindowString,
27
+ transform: () => transform
28
+ });
29
+ module.exports = __toCommonJS(index_browser_exports);
30
+
31
+ // src/core/resolver.ts
32
+ async function resolveAll(dynamicSources, options = {}) {
33
+ const keys = Object.keys(dynamicSources);
34
+ const results = await Promise.allSettled(
35
+ keys.map((key) => resolveSource(dynamicSources[key], options.timeout))
36
+ );
37
+ const data = {};
38
+ results.forEach((result, index) => {
39
+ const key = keys[index];
40
+ if (result.status === "fulfilled") {
41
+ data[key] = result.value;
42
+ } else {
43
+ console.error(`[llmweb] Failed to resolve source "${key}":`, result.reason);
44
+ data[key] = null;
45
+ }
46
+ });
47
+ return data;
48
+ }
49
+ async function resolveSource(source, timeoutMs) {
50
+ const { from } = source;
51
+ const controller = timeoutMs ? new AbortController() : null;
52
+ const signal = controller?.signal;
53
+ const timeoutPromise = timeoutMs ? new Promise(
54
+ (_, reject) => setTimeout(() => {
55
+ controller?.abort();
56
+ reject(new Error(`Timeout of ${timeoutMs}ms exceeded`));
57
+ }, timeoutMs)
58
+ ) : null;
59
+ const resolvePromise = (async () => {
60
+ if (from.type === "fetch") {
61
+ if (!from.url) throw new Error("Fetch source requires a URL");
62
+ const response = await fetch(from.url, { signal });
63
+ if (!response.ok) throw new Error(`HTTP error ${response.status} for ${from.url}`);
64
+ return response.json();
65
+ }
66
+ if (from.type === "fn") {
67
+ if (!from.call) throw new Error('Function source requires a "call" property');
68
+ return from.call();
69
+ }
70
+ throw new Error(`Unsupported source type: ${from.type}`);
71
+ })();
72
+ if (timeoutPromise) {
73
+ return Promise.race([resolvePromise, timeoutPromise]);
74
+ }
75
+ return resolvePromise;
76
+ }
77
+
78
+ // src/core/transformer.ts
79
+ function transform(sourceData, schema) {
80
+ if (!sourceData) return sourceData;
81
+ if (Array.isArray(sourceData)) {
82
+ return sourceData.map((item) => transform(item, schema));
83
+ }
84
+ if (typeof sourceData !== "object") return sourceData;
85
+ const result = {};
86
+ for (const [targetKey, mapping] of Object.entries(schema)) {
87
+ if (typeof mapping === "string") {
88
+ result[targetKey] = getValueByPath(sourceData, mapping);
89
+ } else if (typeof mapping === "function") {
90
+ result[targetKey] = mapping(sourceData);
91
+ } else if (typeof mapping === "object" && mapping !== null) {
92
+ result[targetKey] = transform(sourceData, mapping);
93
+ }
94
+ }
95
+ return result;
96
+ }
97
+ function getValueByPath(obj, path) {
98
+ return path.split(".").reduce((acc, part) => acc && acc[part], obj);
99
+ }
100
+
101
+ // src/core/merger.ts
102
+ function merge(staticData, dynamicResults) {
103
+ if (!staticData) return dynamicResults;
104
+ return {
105
+ ...staticData,
106
+ ...dynamicResults
107
+ };
108
+ }
109
+
110
+ // src/core/compiler.ts
111
+ async function compileLLM(config, loader, options = {}) {
112
+ const { static: staticRef, dynamic, ...topLevelMetadata } = config;
113
+ let staticTruth = { ...topLevelMetadata };
114
+ if (staticRef) {
115
+ try {
116
+ if (typeof staticRef === "object") {
117
+ staticTruth = { ...staticTruth, ...staticRef };
118
+ } else {
119
+ const content = await loader(staticRef);
120
+ if (content) {
121
+ staticTruth = { ...staticTruth, ...JSON.parse(content) };
122
+ }
123
+ }
124
+ } catch (error) {
125
+ if (options.failLoudly) {
126
+ throw new Error(`[llmweb] Static Truth Error: Failed to load/parse JSON at ${staticRef}. ${error}`);
127
+ }
128
+ console.warn(`[llmweb] Warning: Could not load static JSON at ${staticRef}. Proceeding with dynamic data only.`);
129
+ }
130
+ }
131
+ const dynamicTruth = {};
132
+ if (config.dynamic) {
133
+ const rawResults = await resolveAll(config.dynamic, { timeout: options.timeout });
134
+ for (const [key, source] of Object.entries(config.dynamic)) {
135
+ const rawData = rawResults[key];
136
+ if (rawData === null && options.failLoudly) {
137
+ throw new Error(`[llmweb] Dynamic Truth Error: Source "${key}" failed to resolve.`);
138
+ }
139
+ if (rawData && source.map) {
140
+ try {
141
+ dynamicTruth[key] = transform(rawData, source.map);
142
+ } catch (error) {
143
+ if (options.failLoudly) {
144
+ throw new Error(`[llmweb] Transformation Error: Failed to map source "${key}". ${error}`);
145
+ }
146
+ console.error(`[llmweb] Warning: Mapping failed for "${key}". Using raw data.`);
147
+ dynamicTruth[key] = rawData;
148
+ }
149
+ } else {
150
+ dynamicTruth[key] = rawData;
151
+ }
152
+ }
153
+ }
154
+ return merge(staticTruth, dynamicTruth);
155
+ }
156
+
157
+ // src/core/injector.ts
158
+ function toScriptString(data) {
159
+ const json = JSON.stringify(data);
160
+ return `<script type="application/llm+json">${json}</script>`;
161
+ }
162
+ function toWindowString(data, key = "__LLM__") {
163
+ const json = JSON.stringify(data);
164
+ return `window.${key} = ${json};`;
165
+ }
166
+ function toLDJSONString(data) {
167
+ return JSON.stringify(data);
168
+ }
169
+
170
+ // src/index.browser.ts
171
+ var browserLoader = async (path) => {
172
+ console.warn(`[llmweb] Static files cannot be loaded in the browser: ${path}. Please use dynamic sources or pass data directly.`);
173
+ return null;
174
+ };
175
+ async function createLLMSource(config, options = {}) {
176
+ return compileLLM(config, browserLoader, options);
177
+ }
178
+ // Annotate the CommonJS export names for ESM import in node:
179
+ 0 && (module.exports = {
180
+ createLLMSource,
181
+ toLDJSONString,
182
+ toScriptString,
183
+ toWindowString,
184
+ transform
185
+ });
@@ -0,0 +1,154 @@
1
+ // src/core/resolver.ts
2
+ async function resolveAll(dynamicSources, options = {}) {
3
+ const keys = Object.keys(dynamicSources);
4
+ const results = await Promise.allSettled(
5
+ keys.map((key) => resolveSource(dynamicSources[key], options.timeout))
6
+ );
7
+ const data = {};
8
+ results.forEach((result, index) => {
9
+ const key = keys[index];
10
+ if (result.status === "fulfilled") {
11
+ data[key] = result.value;
12
+ } else {
13
+ console.error(`[llmweb] Failed to resolve source "${key}":`, result.reason);
14
+ data[key] = null;
15
+ }
16
+ });
17
+ return data;
18
+ }
19
+ async function resolveSource(source, timeoutMs) {
20
+ const { from } = source;
21
+ const controller = timeoutMs ? new AbortController() : null;
22
+ const signal = controller?.signal;
23
+ const timeoutPromise = timeoutMs ? new Promise(
24
+ (_, reject) => setTimeout(() => {
25
+ controller?.abort();
26
+ reject(new Error(`Timeout of ${timeoutMs}ms exceeded`));
27
+ }, timeoutMs)
28
+ ) : null;
29
+ const resolvePromise = (async () => {
30
+ if (from.type === "fetch") {
31
+ if (!from.url) throw new Error("Fetch source requires a URL");
32
+ const response = await fetch(from.url, { signal });
33
+ if (!response.ok) throw new Error(`HTTP error ${response.status} for ${from.url}`);
34
+ return response.json();
35
+ }
36
+ if (from.type === "fn") {
37
+ if (!from.call) throw new Error('Function source requires a "call" property');
38
+ return from.call();
39
+ }
40
+ throw new Error(`Unsupported source type: ${from.type}`);
41
+ })();
42
+ if (timeoutPromise) {
43
+ return Promise.race([resolvePromise, timeoutPromise]);
44
+ }
45
+ return resolvePromise;
46
+ }
47
+
48
+ // src/core/transformer.ts
49
+ function transform(sourceData, schema) {
50
+ if (!sourceData) return sourceData;
51
+ if (Array.isArray(sourceData)) {
52
+ return sourceData.map((item) => transform(item, schema));
53
+ }
54
+ if (typeof sourceData !== "object") return sourceData;
55
+ const result = {};
56
+ for (const [targetKey, mapping] of Object.entries(schema)) {
57
+ if (typeof mapping === "string") {
58
+ result[targetKey] = getValueByPath(sourceData, mapping);
59
+ } else if (typeof mapping === "function") {
60
+ result[targetKey] = mapping(sourceData);
61
+ } else if (typeof mapping === "object" && mapping !== null) {
62
+ result[targetKey] = transform(sourceData, mapping);
63
+ }
64
+ }
65
+ return result;
66
+ }
67
+ function getValueByPath(obj, path) {
68
+ return path.split(".").reduce((acc, part) => acc && acc[part], obj);
69
+ }
70
+
71
+ // src/core/merger.ts
72
+ function merge(staticData, dynamicResults) {
73
+ if (!staticData) return dynamicResults;
74
+ return {
75
+ ...staticData,
76
+ ...dynamicResults
77
+ };
78
+ }
79
+
80
+ // src/core/compiler.ts
81
+ async function compileLLM(config, loader, options = {}) {
82
+ const { static: staticRef, dynamic, ...topLevelMetadata } = config;
83
+ let staticTruth = { ...topLevelMetadata };
84
+ if (staticRef) {
85
+ try {
86
+ if (typeof staticRef === "object") {
87
+ staticTruth = { ...staticTruth, ...staticRef };
88
+ } else {
89
+ const content = await loader(staticRef);
90
+ if (content) {
91
+ staticTruth = { ...staticTruth, ...JSON.parse(content) };
92
+ }
93
+ }
94
+ } catch (error) {
95
+ if (options.failLoudly) {
96
+ throw new Error(`[llmweb] Static Truth Error: Failed to load/parse JSON at ${staticRef}. ${error}`);
97
+ }
98
+ console.warn(`[llmweb] Warning: Could not load static JSON at ${staticRef}. Proceeding with dynamic data only.`);
99
+ }
100
+ }
101
+ const dynamicTruth = {};
102
+ if (config.dynamic) {
103
+ const rawResults = await resolveAll(config.dynamic, { timeout: options.timeout });
104
+ for (const [key, source] of Object.entries(config.dynamic)) {
105
+ const rawData = rawResults[key];
106
+ if (rawData === null && options.failLoudly) {
107
+ throw new Error(`[llmweb] Dynamic Truth Error: Source "${key}" failed to resolve.`);
108
+ }
109
+ if (rawData && source.map) {
110
+ try {
111
+ dynamicTruth[key] = transform(rawData, source.map);
112
+ } catch (error) {
113
+ if (options.failLoudly) {
114
+ throw new Error(`[llmweb] Transformation Error: Failed to map source "${key}". ${error}`);
115
+ }
116
+ console.error(`[llmweb] Warning: Mapping failed for "${key}". Using raw data.`);
117
+ dynamicTruth[key] = rawData;
118
+ }
119
+ } else {
120
+ dynamicTruth[key] = rawData;
121
+ }
122
+ }
123
+ }
124
+ return merge(staticTruth, dynamicTruth);
125
+ }
126
+
127
+ // src/core/injector.ts
128
+ function toScriptString(data) {
129
+ const json = JSON.stringify(data);
130
+ return `<script type="application/llm+json">${json}</script>`;
131
+ }
132
+ function toWindowString(data, key = "__LLM__") {
133
+ const json = JSON.stringify(data);
134
+ return `window.${key} = ${json};`;
135
+ }
136
+ function toLDJSONString(data) {
137
+ return JSON.stringify(data);
138
+ }
139
+
140
+ // src/index.browser.ts
141
+ var browserLoader = async (path) => {
142
+ console.warn(`[llmweb] Static files cannot be loaded in the browser: ${path}. Please use dynamic sources or pass data directly.`);
143
+ return null;
144
+ };
145
+ async function createLLMSource(config, options = {}) {
146
+ return compileLLM(config, browserLoader, options);
147
+ }
148
+ export {
149
+ createLLMSource,
150
+ toLDJSONString,
151
+ toScriptString,
152
+ toWindowString,
153
+ transform
154
+ };
@@ -0,0 +1,11 @@
1
+ import { L as LLMConfig, T as TransformerOptions } from './index-C5RUlsf2.mjs';
2
+ export { D as DynamicSource, a as LLMJsonProps, M as MapSchema, S as SourceDefinition, b as SourceType } from './index-C5RUlsf2.mjs';
3
+ export { t as toLDJSONString, a as toScriptString, b as toWindowString, c as transform } from './injector-DD7MSHW4.mjs';
4
+
5
+ /**
6
+ * The main "Compiler" for LLM truth (Node.js version).
7
+ * Uses dynamic imports for fs/path to be browser-bundler friendly.
8
+ */
9
+ declare function createLLMSource(config: LLMConfig, options?: TransformerOptions): Promise<any>;
10
+
11
+ export { LLMConfig, TransformerOptions, createLLMSource };
@@ -0,0 +1,11 @@
1
+ import { L as LLMConfig, T as TransformerOptions } from './index-C5RUlsf2.js';
2
+ export { D as DynamicSource, a as LLMJsonProps, M as MapSchema, S as SourceDefinition, b as SourceType } from './index-C5RUlsf2.js';
3
+ export { t as toLDJSONString, a as toScriptString, b as toWindowString, c as transform } from './injector-Gh9wJgRZ.js';
4
+
5
+ /**
6
+ * The main "Compiler" for LLM truth (Node.js version).
7
+ * Uses dynamic imports for fs/path to be browser-bundler friendly.
8
+ */
9
+ declare function createLLMSource(config: LLMConfig, options?: TransformerOptions): Promise<any>;
10
+
11
+ export { LLMConfig, TransformerOptions, createLLMSource };
package/dist/index.js ADDED
@@ -0,0 +1,208 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ createLLMSource: () => createLLMSource,
34
+ toLDJSONString: () => toLDJSONString,
35
+ toScriptString: () => toScriptString,
36
+ toWindowString: () => toWindowString,
37
+ transform: () => transform
38
+ });
39
+ module.exports = __toCommonJS(index_exports);
40
+
41
+ // src/core/resolver.ts
42
+ async function resolveAll(dynamicSources, options = {}) {
43
+ const keys = Object.keys(dynamicSources);
44
+ const results = await Promise.allSettled(
45
+ keys.map((key) => resolveSource(dynamicSources[key], options.timeout))
46
+ );
47
+ const data = {};
48
+ results.forEach((result, index) => {
49
+ const key = keys[index];
50
+ if (result.status === "fulfilled") {
51
+ data[key] = result.value;
52
+ } else {
53
+ console.error(`[llmweb] Failed to resolve source "${key}":`, result.reason);
54
+ data[key] = null;
55
+ }
56
+ });
57
+ return data;
58
+ }
59
+ async function resolveSource(source, timeoutMs) {
60
+ const { from } = source;
61
+ const controller = timeoutMs ? new AbortController() : null;
62
+ const signal = controller?.signal;
63
+ const timeoutPromise = timeoutMs ? new Promise(
64
+ (_, reject) => setTimeout(() => {
65
+ controller?.abort();
66
+ reject(new Error(`Timeout of ${timeoutMs}ms exceeded`));
67
+ }, timeoutMs)
68
+ ) : null;
69
+ const resolvePromise = (async () => {
70
+ if (from.type === "fetch") {
71
+ if (!from.url) throw new Error("Fetch source requires a URL");
72
+ const response = await fetch(from.url, { signal });
73
+ if (!response.ok) throw new Error(`HTTP error ${response.status} for ${from.url}`);
74
+ return response.json();
75
+ }
76
+ if (from.type === "fn") {
77
+ if (!from.call) throw new Error('Function source requires a "call" property');
78
+ return from.call();
79
+ }
80
+ throw new Error(`Unsupported source type: ${from.type}`);
81
+ })();
82
+ if (timeoutPromise) {
83
+ return Promise.race([resolvePromise, timeoutPromise]);
84
+ }
85
+ return resolvePromise;
86
+ }
87
+
88
+ // src/core/transformer.ts
89
+ function transform(sourceData, schema) {
90
+ if (!sourceData) return sourceData;
91
+ if (Array.isArray(sourceData)) {
92
+ return sourceData.map((item) => transform(item, schema));
93
+ }
94
+ if (typeof sourceData !== "object") return sourceData;
95
+ const result = {};
96
+ for (const [targetKey, mapping] of Object.entries(schema)) {
97
+ if (typeof mapping === "string") {
98
+ result[targetKey] = getValueByPath(sourceData, mapping);
99
+ } else if (typeof mapping === "function") {
100
+ result[targetKey] = mapping(sourceData);
101
+ } else if (typeof mapping === "object" && mapping !== null) {
102
+ result[targetKey] = transform(sourceData, mapping);
103
+ }
104
+ }
105
+ return result;
106
+ }
107
+ function getValueByPath(obj, path) {
108
+ return path.split(".").reduce((acc, part) => acc && acc[part], obj);
109
+ }
110
+
111
+ // src/core/merger.ts
112
+ function merge(staticData, dynamicResults) {
113
+ if (!staticData) return dynamicResults;
114
+ return {
115
+ ...staticData,
116
+ ...dynamicResults
117
+ };
118
+ }
119
+
120
+ // src/core/compiler.ts
121
+ async function compileLLM(config, loader, options = {}) {
122
+ const { static: staticRef, dynamic, ...topLevelMetadata } = config;
123
+ let staticTruth = { ...topLevelMetadata };
124
+ if (staticRef) {
125
+ try {
126
+ if (typeof staticRef === "object") {
127
+ staticTruth = { ...staticTruth, ...staticRef };
128
+ } else {
129
+ const content = await loader(staticRef);
130
+ if (content) {
131
+ staticTruth = { ...staticTruth, ...JSON.parse(content) };
132
+ }
133
+ }
134
+ } catch (error) {
135
+ if (options.failLoudly) {
136
+ throw new Error(`[llmweb] Static Truth Error: Failed to load/parse JSON at ${staticRef}. ${error}`);
137
+ }
138
+ console.warn(`[llmweb] Warning: Could not load static JSON at ${staticRef}. Proceeding with dynamic data only.`);
139
+ }
140
+ }
141
+ const dynamicTruth = {};
142
+ if (config.dynamic) {
143
+ const rawResults = await resolveAll(config.dynamic, { timeout: options.timeout });
144
+ for (const [key, source] of Object.entries(config.dynamic)) {
145
+ const rawData = rawResults[key];
146
+ if (rawData === null && options.failLoudly) {
147
+ throw new Error(`[llmweb] Dynamic Truth Error: Source "${key}" failed to resolve.`);
148
+ }
149
+ if (rawData && source.map) {
150
+ try {
151
+ dynamicTruth[key] = transform(rawData, source.map);
152
+ } catch (error) {
153
+ if (options.failLoudly) {
154
+ throw new Error(`[llmweb] Transformation Error: Failed to map source "${key}". ${error}`);
155
+ }
156
+ console.error(`[llmweb] Warning: Mapping failed for "${key}". Using raw data.`);
157
+ dynamicTruth[key] = rawData;
158
+ }
159
+ } else {
160
+ dynamicTruth[key] = rawData;
161
+ }
162
+ }
163
+ }
164
+ return merge(staticTruth, dynamicTruth);
165
+ }
166
+
167
+ // src/core/injector.ts
168
+ function toScriptString(data) {
169
+ const json = JSON.stringify(data);
170
+ return `<script type="application/llm+json">${json}</script>`;
171
+ }
172
+ function toWindowString(data, key = "__LLM__") {
173
+ const json = JSON.stringify(data);
174
+ return `window.${key} = ${json};`;
175
+ }
176
+ function toLDJSONString(data) {
177
+ return JSON.stringify(data);
178
+ }
179
+
180
+ // src/index.ts
181
+ async function createLLMSource(config, options = {}) {
182
+ const nodeLoader = async (path) => {
183
+ try {
184
+ const fs = await import(
185
+ /* webpackIgnore: true */
186
+ "fs"
187
+ );
188
+ const nodePath = await import(
189
+ /* webpackIgnore: true */
190
+ "path"
191
+ );
192
+ const fullPath = path.startsWith("/") ? path : nodePath.join(process.cwd(), path);
193
+ return fs.readFileSync(fullPath, "utf-8");
194
+ } catch (error) {
195
+ console.warn(`[llmweb] Warning: 'node:fs' not available. Skipping static file load: ${path}`);
196
+ return null;
197
+ }
198
+ };
199
+ return compileLLM(config, nodeLoader, options);
200
+ }
201
+ // Annotate the CommonJS export names for ESM import in node:
202
+ 0 && (module.exports = {
203
+ createLLMSource,
204
+ toLDJSONString,
205
+ toScriptString,
206
+ toWindowString,
207
+ transform
208
+ });