@kaleabdenbel/llmweb 1.0.1 → 1.0.3
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 +2 -1
- package/dist/adapters/express.js +22 -8
- package/dist/adapters/express.mjs +131 -3
- package/dist/adapters/next.js +22 -8
- package/dist/adapters/next.mjs +131 -3
- package/dist/adapters/react.js +253 -0
- package/dist/adapters/react.mjs +217 -0
- package/dist/adapters/vanilla.js +175 -0
- package/dist/{chunk-PYG5H54N.mjs → adapters/vanilla.mjs} +36 -10
- package/dist/index.browser.js +175 -0
- package/dist/index.browser.mjs +145 -0
- package/dist/index.js +38 -8
- package/dist/index.mjs +145 -4
- package/package.json +15 -3
- package/dist/adapters/express.d.ts +0 -5
- package/dist/adapters/next.d.ts +0 -18
- package/dist/core/merger.d.ts +0 -7
- package/dist/core/resolver.d.ts +0 -12
- package/dist/core/transformer.d.ts +0 -7
- package/dist/index.d.ts +0 -7
- package/dist/types/index.d.ts +0 -23
package/README.md
CHANGED
|
@@ -9,6 +9,7 @@ The data compiler that transforms messy application state into LLM-readable trut
|
|
|
9
9
|
- **Static Truth**: Your contract with the world. Deterministic. Versioned.
|
|
10
10
|
- **Dynamic Truth**: Live data from APIs, Server Actions, or DB calls.
|
|
11
11
|
- **The Engine**: Compiles these into a single, structured JSON object for LLMs.
|
|
12
|
+
- **Framework Adapters**: Utilities for [Next.js](docs/next.md), [React](docs/react.md), [Express](docs/express.md), and [Vanilla JS](docs/vanilla.md).
|
|
12
13
|
|
|
13
14
|
## Installation
|
|
14
15
|
|
|
@@ -24,7 +25,7 @@ Create an LLM route to expose your site's data.
|
|
|
24
25
|
|
|
25
26
|
```typescript
|
|
26
27
|
// app/llm/route.ts
|
|
27
|
-
import { createLLMHandler } from "llmweb/adapters/next";
|
|
28
|
+
import { createLLMHandler } from "@kaleabdenbel/llmweb/adapters/next";
|
|
28
29
|
import { getStaffList } from "@/actions/getStaffList";
|
|
29
30
|
|
|
30
31
|
const config = {
|
package/dist/adapters/express.js
CHANGED
|
@@ -73,7 +73,11 @@ async function resolveSource(source, timeoutMs) {
|
|
|
73
73
|
|
|
74
74
|
// src/core/transformer.ts
|
|
75
75
|
function transform(sourceData, schema) {
|
|
76
|
-
if (!sourceData
|
|
76
|
+
if (!sourceData) return sourceData;
|
|
77
|
+
if (Array.isArray(sourceData)) {
|
|
78
|
+
return sourceData.map((item) => transform(item, schema));
|
|
79
|
+
}
|
|
80
|
+
if (typeof sourceData !== "object") return sourceData;
|
|
77
81
|
const result = {};
|
|
78
82
|
for (const [targetKey, mapping] of Object.entries(schema)) {
|
|
79
83
|
if (typeof mapping === "string") {
|
|
@@ -99,16 +103,15 @@ function merge(staticData, dynamicResults) {
|
|
|
99
103
|
};
|
|
100
104
|
}
|
|
101
105
|
|
|
102
|
-
// src/
|
|
103
|
-
|
|
104
|
-
var import_node_path = require("path");
|
|
105
|
-
async function createLLMSource(config, options = {}) {
|
|
106
|
+
// src/core/compiler.ts
|
|
107
|
+
async function compileLLM(config, loader, options = {}) {
|
|
106
108
|
let staticTruth = {};
|
|
107
109
|
if (config.static) {
|
|
108
110
|
try {
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
111
|
+
const content = await loader(config.static);
|
|
112
|
+
if (content) {
|
|
113
|
+
staticTruth = JSON.parse(content);
|
|
114
|
+
}
|
|
112
115
|
} catch (error) {
|
|
113
116
|
if (options.failLoudly) {
|
|
114
117
|
throw new Error(`[llmweb] Static Truth Error: Failed to load/parse JSON at ${config.static}. ${error}`);
|
|
@@ -142,6 +145,17 @@ async function createLLMSource(config, options = {}) {
|
|
|
142
145
|
return merge(staticTruth, dynamicTruth);
|
|
143
146
|
}
|
|
144
147
|
|
|
148
|
+
// src/index.ts
|
|
149
|
+
var import_node_fs = require("fs");
|
|
150
|
+
var import_node_path = require("path");
|
|
151
|
+
var nodeLoader = async (path) => {
|
|
152
|
+
const fullPath = path.startsWith("/") ? path : (0, import_node_path.join)(process.cwd(), path);
|
|
153
|
+
return (0, import_node_fs.readFileSync)(fullPath, "utf-8");
|
|
154
|
+
};
|
|
155
|
+
async function createLLMSource(config, options = {}) {
|
|
156
|
+
return compileLLM(config, nodeLoader, options);
|
|
157
|
+
}
|
|
158
|
+
|
|
145
159
|
// src/adapters/express.ts
|
|
146
160
|
function llmMiddleware(config) {
|
|
147
161
|
return async (req, res, next) => {
|
|
@@ -1,6 +1,134 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
+
let staticTruth = {};
|
|
83
|
+
if (config.static) {
|
|
84
|
+
try {
|
|
85
|
+
const content = await loader(config.static);
|
|
86
|
+
if (content) {
|
|
87
|
+
staticTruth = JSON.parse(content);
|
|
88
|
+
}
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (options.failLoudly) {
|
|
91
|
+
throw new Error(`[llmweb] Static Truth Error: Failed to load/parse JSON at ${config.static}. ${error}`);
|
|
92
|
+
}
|
|
93
|
+
console.warn(`[llmweb] Warning: Could not load static JSON at ${config.static}. Proceeding with dynamic data only.`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const dynamicTruth = {};
|
|
97
|
+
if (config.dynamic) {
|
|
98
|
+
const rawResults = await resolveAll(config.dynamic, { timeout: options.timeout });
|
|
99
|
+
for (const [key, source] of Object.entries(config.dynamic)) {
|
|
100
|
+
const rawData = rawResults[key];
|
|
101
|
+
if (rawData === null && options.failLoudly) {
|
|
102
|
+
throw new Error(`[llmweb] Dynamic Truth Error: Source "${key}" failed to resolve.`);
|
|
103
|
+
}
|
|
104
|
+
if (rawData && source.map) {
|
|
105
|
+
try {
|
|
106
|
+
dynamicTruth[key] = transform(rawData, source.map);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
if (options.failLoudly) {
|
|
109
|
+
throw new Error(`[llmweb] Transformation Error: Failed to map source "${key}". ${error}`);
|
|
110
|
+
}
|
|
111
|
+
console.error(`[llmweb] Warning: Mapping failed for "${key}". Using raw data.`);
|
|
112
|
+
dynamicTruth[key] = rawData;
|
|
113
|
+
}
|
|
114
|
+
} else {
|
|
115
|
+
dynamicTruth[key] = rawData;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return merge(staticTruth, dynamicTruth);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// src/index.ts
|
|
123
|
+
import { readFileSync } from "fs";
|
|
124
|
+
import { join } from "path";
|
|
125
|
+
var nodeLoader = async (path) => {
|
|
126
|
+
const fullPath = path.startsWith("/") ? path : join(process.cwd(), path);
|
|
127
|
+
return readFileSync(fullPath, "utf-8");
|
|
128
|
+
};
|
|
129
|
+
async function createLLMSource(config, options = {}) {
|
|
130
|
+
return compileLLM(config, nodeLoader, options);
|
|
131
|
+
}
|
|
4
132
|
|
|
5
133
|
// src/adapters/express.ts
|
|
6
134
|
function llmMiddleware(config) {
|
package/dist/adapters/next.js
CHANGED
|
@@ -74,7 +74,11 @@ async function resolveSource(source, timeoutMs) {
|
|
|
74
74
|
|
|
75
75
|
// src/core/transformer.ts
|
|
76
76
|
function transform(sourceData, schema) {
|
|
77
|
-
if (!sourceData
|
|
77
|
+
if (!sourceData) return sourceData;
|
|
78
|
+
if (Array.isArray(sourceData)) {
|
|
79
|
+
return sourceData.map((item) => transform(item, schema));
|
|
80
|
+
}
|
|
81
|
+
if (typeof sourceData !== "object") return sourceData;
|
|
78
82
|
const result = {};
|
|
79
83
|
for (const [targetKey, mapping] of Object.entries(schema)) {
|
|
80
84
|
if (typeof mapping === "string") {
|
|
@@ -100,16 +104,15 @@ function merge(staticData, dynamicResults) {
|
|
|
100
104
|
};
|
|
101
105
|
}
|
|
102
106
|
|
|
103
|
-
// src/
|
|
104
|
-
|
|
105
|
-
var import_node_path = require("path");
|
|
106
|
-
async function createLLMSource(config, options = {}) {
|
|
107
|
+
// src/core/compiler.ts
|
|
108
|
+
async function compileLLM(config, loader, options = {}) {
|
|
107
109
|
let staticTruth = {};
|
|
108
110
|
if (config.static) {
|
|
109
111
|
try {
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
112
|
+
const content = await loader(config.static);
|
|
113
|
+
if (content) {
|
|
114
|
+
staticTruth = JSON.parse(content);
|
|
115
|
+
}
|
|
113
116
|
} catch (error) {
|
|
114
117
|
if (options.failLoudly) {
|
|
115
118
|
throw new Error(`[llmweb] Static Truth Error: Failed to load/parse JSON at ${config.static}. ${error}`);
|
|
@@ -143,6 +146,17 @@ async function createLLMSource(config, options = {}) {
|
|
|
143
146
|
return merge(staticTruth, dynamicTruth);
|
|
144
147
|
}
|
|
145
148
|
|
|
149
|
+
// src/index.ts
|
|
150
|
+
var import_node_fs = require("fs");
|
|
151
|
+
var import_node_path = require("path");
|
|
152
|
+
var nodeLoader = async (path) => {
|
|
153
|
+
const fullPath = path.startsWith("/") ? path : (0, import_node_path.join)(process.cwd(), path);
|
|
154
|
+
return (0, import_node_fs.readFileSync)(fullPath, "utf-8");
|
|
155
|
+
};
|
|
156
|
+
async function createLLMSource(config, options = {}) {
|
|
157
|
+
return compileLLM(config, nodeLoader, options);
|
|
158
|
+
}
|
|
159
|
+
|
|
146
160
|
// src/adapters/next.tsx
|
|
147
161
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
148
162
|
async function LLMJson({ config, className }) {
|
package/dist/adapters/next.mjs
CHANGED
|
@@ -1,6 +1,134 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
+
let staticTruth = {};
|
|
83
|
+
if (config.static) {
|
|
84
|
+
try {
|
|
85
|
+
const content = await loader(config.static);
|
|
86
|
+
if (content) {
|
|
87
|
+
staticTruth = JSON.parse(content);
|
|
88
|
+
}
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (options.failLoudly) {
|
|
91
|
+
throw new Error(`[llmweb] Static Truth Error: Failed to load/parse JSON at ${config.static}. ${error}`);
|
|
92
|
+
}
|
|
93
|
+
console.warn(`[llmweb] Warning: Could not load static JSON at ${config.static}. Proceeding with dynamic data only.`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const dynamicTruth = {};
|
|
97
|
+
if (config.dynamic) {
|
|
98
|
+
const rawResults = await resolveAll(config.dynamic, { timeout: options.timeout });
|
|
99
|
+
for (const [key, source] of Object.entries(config.dynamic)) {
|
|
100
|
+
const rawData = rawResults[key];
|
|
101
|
+
if (rawData === null && options.failLoudly) {
|
|
102
|
+
throw new Error(`[llmweb] Dynamic Truth Error: Source "${key}" failed to resolve.`);
|
|
103
|
+
}
|
|
104
|
+
if (rawData && source.map) {
|
|
105
|
+
try {
|
|
106
|
+
dynamicTruth[key] = transform(rawData, source.map);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
if (options.failLoudly) {
|
|
109
|
+
throw new Error(`[llmweb] Transformation Error: Failed to map source "${key}". ${error}`);
|
|
110
|
+
}
|
|
111
|
+
console.error(`[llmweb] Warning: Mapping failed for "${key}". Using raw data.`);
|
|
112
|
+
dynamicTruth[key] = rawData;
|
|
113
|
+
}
|
|
114
|
+
} else {
|
|
115
|
+
dynamicTruth[key] = rawData;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return merge(staticTruth, dynamicTruth);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// src/index.ts
|
|
123
|
+
import { readFileSync } from "fs";
|
|
124
|
+
import { join } from "path";
|
|
125
|
+
var nodeLoader = async (path) => {
|
|
126
|
+
const fullPath = path.startsWith("/") ? path : join(process.cwd(), path);
|
|
127
|
+
return readFileSync(fullPath, "utf-8");
|
|
128
|
+
};
|
|
129
|
+
async function createLLMSource(config, options = {}) {
|
|
130
|
+
return compileLLM(config, nodeLoader, options);
|
|
131
|
+
}
|
|
4
132
|
|
|
5
133
|
// src/adapters/next.tsx
|
|
6
134
|
import { jsx } from "react/jsx-runtime";
|
|
@@ -0,0 +1,253 @@
|
|
|
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/adapters/react.tsx
|
|
31
|
+
var react_exports = {};
|
|
32
|
+
__export(react_exports, {
|
|
33
|
+
LLMInjector: () => LLMInjector,
|
|
34
|
+
LLMJson: () => LLMJson
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(react_exports);
|
|
37
|
+
var import_react = __toESM(require("react"));
|
|
38
|
+
|
|
39
|
+
// src/core/resolver.ts
|
|
40
|
+
async function resolveAll(dynamicSources, options = {}) {
|
|
41
|
+
const keys = Object.keys(dynamicSources);
|
|
42
|
+
const results = await Promise.allSettled(
|
|
43
|
+
keys.map((key) => resolveSource(dynamicSources[key], options.timeout))
|
|
44
|
+
);
|
|
45
|
+
const data = {};
|
|
46
|
+
results.forEach((result, index) => {
|
|
47
|
+
const key = keys[index];
|
|
48
|
+
if (result.status === "fulfilled") {
|
|
49
|
+
data[key] = result.value;
|
|
50
|
+
} else {
|
|
51
|
+
console.error(`[llmweb] Failed to resolve source "${key}":`, result.reason);
|
|
52
|
+
data[key] = null;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
return data;
|
|
56
|
+
}
|
|
57
|
+
async function resolveSource(source, timeoutMs) {
|
|
58
|
+
const { from } = source;
|
|
59
|
+
const controller = timeoutMs ? new AbortController() : null;
|
|
60
|
+
const signal = controller?.signal;
|
|
61
|
+
const timeoutPromise = timeoutMs ? new Promise(
|
|
62
|
+
(_, reject) => setTimeout(() => {
|
|
63
|
+
controller?.abort();
|
|
64
|
+
reject(new Error(`Timeout of ${timeoutMs}ms exceeded`));
|
|
65
|
+
}, timeoutMs)
|
|
66
|
+
) : null;
|
|
67
|
+
const resolvePromise = (async () => {
|
|
68
|
+
if (from.type === "fetch") {
|
|
69
|
+
if (!from.url) throw new Error("Fetch source requires a URL");
|
|
70
|
+
const response = await fetch(from.url, { signal });
|
|
71
|
+
if (!response.ok) throw new Error(`HTTP error ${response.status} for ${from.url}`);
|
|
72
|
+
return response.json();
|
|
73
|
+
}
|
|
74
|
+
if (from.type === "fn") {
|
|
75
|
+
if (!from.call) throw new Error('Function source requires a "call" property');
|
|
76
|
+
return from.call();
|
|
77
|
+
}
|
|
78
|
+
throw new Error(`Unsupported source type: ${from.type}`);
|
|
79
|
+
})();
|
|
80
|
+
if (timeoutPromise) {
|
|
81
|
+
return Promise.race([resolvePromise, timeoutPromise]);
|
|
82
|
+
}
|
|
83
|
+
return resolvePromise;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/core/transformer.ts
|
|
87
|
+
function transform(sourceData, schema) {
|
|
88
|
+
if (!sourceData) return sourceData;
|
|
89
|
+
if (Array.isArray(sourceData)) {
|
|
90
|
+
return sourceData.map((item) => transform(item, schema));
|
|
91
|
+
}
|
|
92
|
+
if (typeof sourceData !== "object") return sourceData;
|
|
93
|
+
const result = {};
|
|
94
|
+
for (const [targetKey, mapping] of Object.entries(schema)) {
|
|
95
|
+
if (typeof mapping === "string") {
|
|
96
|
+
result[targetKey] = getValueByPath(sourceData, mapping);
|
|
97
|
+
} else if (typeof mapping === "function") {
|
|
98
|
+
result[targetKey] = mapping(sourceData);
|
|
99
|
+
} else if (typeof mapping === "object" && mapping !== null) {
|
|
100
|
+
result[targetKey] = transform(sourceData, mapping);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
function getValueByPath(obj, path) {
|
|
106
|
+
return path.split(".").reduce((acc, part) => acc && acc[part], obj);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// src/core/merger.ts
|
|
110
|
+
function merge(staticData, dynamicResults) {
|
|
111
|
+
if (!staticData) return dynamicResults;
|
|
112
|
+
return {
|
|
113
|
+
...staticData,
|
|
114
|
+
...dynamicResults
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/core/compiler.ts
|
|
119
|
+
async function compileLLM(config, loader, options = {}) {
|
|
120
|
+
let staticTruth = {};
|
|
121
|
+
if (config.static) {
|
|
122
|
+
try {
|
|
123
|
+
const content = await loader(config.static);
|
|
124
|
+
if (content) {
|
|
125
|
+
staticTruth = JSON.parse(content);
|
|
126
|
+
}
|
|
127
|
+
} catch (error) {
|
|
128
|
+
if (options.failLoudly) {
|
|
129
|
+
throw new Error(`[llmweb] Static Truth Error: Failed to load/parse JSON at ${config.static}. ${error}`);
|
|
130
|
+
}
|
|
131
|
+
console.warn(`[llmweb] Warning: Could not load static JSON at ${config.static}. Proceeding with dynamic data only.`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const dynamicTruth = {};
|
|
135
|
+
if (config.dynamic) {
|
|
136
|
+
const rawResults = await resolveAll(config.dynamic, { timeout: options.timeout });
|
|
137
|
+
for (const [key, source] of Object.entries(config.dynamic)) {
|
|
138
|
+
const rawData = rawResults[key];
|
|
139
|
+
if (rawData === null && options.failLoudly) {
|
|
140
|
+
throw new Error(`[llmweb] Dynamic Truth Error: Source "${key}" failed to resolve.`);
|
|
141
|
+
}
|
|
142
|
+
if (rawData && source.map) {
|
|
143
|
+
try {
|
|
144
|
+
dynamicTruth[key] = transform(rawData, source.map);
|
|
145
|
+
} catch (error) {
|
|
146
|
+
if (options.failLoudly) {
|
|
147
|
+
throw new Error(`[llmweb] Transformation Error: Failed to map source "${key}". ${error}`);
|
|
148
|
+
}
|
|
149
|
+
console.error(`[llmweb] Warning: Mapping failed for "${key}". Using raw data.`);
|
|
150
|
+
dynamicTruth[key] = rawData;
|
|
151
|
+
}
|
|
152
|
+
} else {
|
|
153
|
+
dynamicTruth[key] = rawData;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return merge(staticTruth, dynamicTruth);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// src/index.ts
|
|
161
|
+
var import_node_fs = require("fs");
|
|
162
|
+
var import_node_path = require("path");
|
|
163
|
+
|
|
164
|
+
// src/core/injector.ts
|
|
165
|
+
function toWindowString(data, key = "__LLM__") {
|
|
166
|
+
const json = JSON.stringify(data);
|
|
167
|
+
return `window.${key} = ${json};`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// src/index.ts
|
|
171
|
+
var nodeLoader = async (path) => {
|
|
172
|
+
const fullPath = path.startsWith("/") ? path : (0, import_node_path.join)(process.cwd(), path);
|
|
173
|
+
return (0, import_node_fs.readFileSync)(fullPath, "utf-8");
|
|
174
|
+
};
|
|
175
|
+
async function createLLMSource(config, options = {}) {
|
|
176
|
+
return compileLLM(config, nodeLoader, options);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// src/adapters/react.tsx
|
|
180
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
181
|
+
async function LLMJson({
|
|
182
|
+
config,
|
|
183
|
+
mode = "script",
|
|
184
|
+
targetKey = "__LLM__",
|
|
185
|
+
className
|
|
186
|
+
}) {
|
|
187
|
+
const truth = await createLLMSource(config);
|
|
188
|
+
if (mode === "window") {
|
|
189
|
+
const scriptBody = toWindowString(truth, targetKey);
|
|
190
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
191
|
+
"script",
|
|
192
|
+
{
|
|
193
|
+
id: `llm-window-${targetKey}`,
|
|
194
|
+
dangerouslySetInnerHTML: { __html: scriptBody }
|
|
195
|
+
}
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
if (mode === "pre") {
|
|
199
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("pre", { id: "llm-truth-pre", className, children: JSON.stringify(truth, null, 2) });
|
|
200
|
+
}
|
|
201
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
202
|
+
"script",
|
|
203
|
+
{
|
|
204
|
+
id: "llm-truth-script",
|
|
205
|
+
type: "application/llm+json",
|
|
206
|
+
dangerouslySetInnerHTML: { __html: JSON.stringify(truth) }
|
|
207
|
+
}
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
function LLMInjector({
|
|
211
|
+
config,
|
|
212
|
+
mode = "script",
|
|
213
|
+
targetKey = "__LLM__",
|
|
214
|
+
className
|
|
215
|
+
}) {
|
|
216
|
+
const [truth, setTruth] = import_react.default.useState(null);
|
|
217
|
+
import_react.default.useEffect(() => {
|
|
218
|
+
let mounted = true;
|
|
219
|
+
createLLMSource(config).then((data) => {
|
|
220
|
+
if (mounted) setTruth(data);
|
|
221
|
+
});
|
|
222
|
+
return () => {
|
|
223
|
+
mounted = false;
|
|
224
|
+
};
|
|
225
|
+
}, [config]);
|
|
226
|
+
if (!truth) return null;
|
|
227
|
+
if (mode === "window") {
|
|
228
|
+
const scriptBody = toWindowString(truth, targetKey);
|
|
229
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
230
|
+
"script",
|
|
231
|
+
{
|
|
232
|
+
id: `llm-window-${targetKey}`,
|
|
233
|
+
dangerouslySetInnerHTML: { __html: scriptBody }
|
|
234
|
+
}
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
if (mode === "pre") {
|
|
238
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("pre", { id: "llm-truth-pre", className, children: JSON.stringify(truth, null, 2) });
|
|
239
|
+
}
|
|
240
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
241
|
+
"script",
|
|
242
|
+
{
|
|
243
|
+
id: "llm-truth-script",
|
|
244
|
+
type: "application/llm+json",
|
|
245
|
+
dangerouslySetInnerHTML: { __html: JSON.stringify(truth) }
|
|
246
|
+
}
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
250
|
+
0 && (module.exports = {
|
|
251
|
+
LLMInjector,
|
|
252
|
+
LLMJson
|
|
253
|
+
});
|