@kaleabdenbel/llmweb 1.0.0 → 1.0.2
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 +1 -1
- package/dist/adapters/express.js +5 -1
- package/dist/adapters/express.mjs +1 -1
- package/dist/adapters/next.js +5 -1
- package/dist/adapters/next.mjs +1 -1
- package/dist/adapters/react.d.ts +15 -0
- package/dist/adapters/react.js +191 -0
- package/dist/adapters/react.mjs +39 -0
- package/dist/adapters/vanilla.d.ts +5 -0
- package/dist/adapters/vanilla.js +165 -0
- package/dist/adapters/vanilla.mjs +20 -0
- package/dist/{chunk-PYG5H54N.mjs → chunk-HRF6I6KB.mjs} +19 -1
- package/dist/core/injector.d.ts +11 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +21 -1
- package/dist/index.mjs +5 -1
- package/package.json +13 -3
package/README.md
CHANGED
|
@@ -24,7 +24,7 @@ Create an LLM route to expose your site's data.
|
|
|
24
24
|
|
|
25
25
|
```typescript
|
|
26
26
|
// app/llm/route.ts
|
|
27
|
-
import { createLLMHandler } from "llmweb/adapters/next";
|
|
27
|
+
import { createLLMHandler } from "@kaleabdenbel/llmweb/adapters/next";
|
|
28
28
|
import { getStaffList } from "@/actions/getStaffList";
|
|
29
29
|
|
|
30
30
|
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") {
|
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") {
|
package/dist/adapters/next.mjs
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { LLMConfig } from "../types";
|
|
2
|
+
export interface LLMJsonProps {
|
|
3
|
+
config: LLMConfig;
|
|
4
|
+
mode?: "script" | "window" | "pre";
|
|
5
|
+
targetKey?: string;
|
|
6
|
+
className?: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Universal LLMJson component for React environments.
|
|
10
|
+
* Handles server-side resolution in Next.js or client-side resolution (if needed).
|
|
11
|
+
*
|
|
12
|
+
* NOTE: When used in Next.js Server Components, this will be async.
|
|
13
|
+
* In standard Client Components, it expects an async resolution pattern or pre-resolved data.
|
|
14
|
+
*/
|
|
15
|
+
export declare function LLMJson({ config, mode, targetKey, className, }: LLMJsonProps): Promise<import("react/jsx-runtime").JSX.Element>;
|
|
@@ -0,0 +1,191 @@
|
|
|
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/adapters/react.tsx
|
|
21
|
+
var react_exports = {};
|
|
22
|
+
__export(react_exports, {
|
|
23
|
+
LLMJson: () => LLMJson
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(react_exports);
|
|
26
|
+
|
|
27
|
+
// src/core/resolver.ts
|
|
28
|
+
async function resolveAll(dynamicSources, options = {}) {
|
|
29
|
+
const keys = Object.keys(dynamicSources);
|
|
30
|
+
const results = await Promise.allSettled(
|
|
31
|
+
keys.map((key) => resolveSource(dynamicSources[key], options.timeout))
|
|
32
|
+
);
|
|
33
|
+
const data = {};
|
|
34
|
+
results.forEach((result, index) => {
|
|
35
|
+
const key = keys[index];
|
|
36
|
+
if (result.status === "fulfilled") {
|
|
37
|
+
data[key] = result.value;
|
|
38
|
+
} else {
|
|
39
|
+
console.error(`[llmweb] Failed to resolve source "${key}":`, result.reason);
|
|
40
|
+
data[key] = null;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return data;
|
|
44
|
+
}
|
|
45
|
+
async function resolveSource(source, timeoutMs) {
|
|
46
|
+
const { from } = source;
|
|
47
|
+
const controller = timeoutMs ? new AbortController() : null;
|
|
48
|
+
const signal = controller?.signal;
|
|
49
|
+
const timeoutPromise = timeoutMs ? new Promise(
|
|
50
|
+
(_, reject) => setTimeout(() => {
|
|
51
|
+
controller?.abort();
|
|
52
|
+
reject(new Error(`Timeout of ${timeoutMs}ms exceeded`));
|
|
53
|
+
}, timeoutMs)
|
|
54
|
+
) : null;
|
|
55
|
+
const resolvePromise = (async () => {
|
|
56
|
+
if (from.type === "fetch") {
|
|
57
|
+
if (!from.url) throw new Error("Fetch source requires a URL");
|
|
58
|
+
const response = await fetch(from.url, { signal });
|
|
59
|
+
if (!response.ok) throw new Error(`HTTP error ${response.status} for ${from.url}`);
|
|
60
|
+
return response.json();
|
|
61
|
+
}
|
|
62
|
+
if (from.type === "fn") {
|
|
63
|
+
if (!from.call) throw new Error('Function source requires a "call" property');
|
|
64
|
+
return from.call();
|
|
65
|
+
}
|
|
66
|
+
throw new Error(`Unsupported source type: ${from.type}`);
|
|
67
|
+
})();
|
|
68
|
+
if (timeoutPromise) {
|
|
69
|
+
return Promise.race([resolvePromise, timeoutPromise]);
|
|
70
|
+
}
|
|
71
|
+
return resolvePromise;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/core/transformer.ts
|
|
75
|
+
function transform(sourceData, schema) {
|
|
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;
|
|
81
|
+
const result = {};
|
|
82
|
+
for (const [targetKey, mapping] of Object.entries(schema)) {
|
|
83
|
+
if (typeof mapping === "string") {
|
|
84
|
+
result[targetKey] = getValueByPath(sourceData, mapping);
|
|
85
|
+
} else if (typeof mapping === "function") {
|
|
86
|
+
result[targetKey] = mapping(sourceData);
|
|
87
|
+
} else if (typeof mapping === "object" && mapping !== null) {
|
|
88
|
+
result[targetKey] = transform(sourceData, mapping);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
function getValueByPath(obj, path) {
|
|
94
|
+
return path.split(".").reduce((acc, part) => acc && acc[part], obj);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// src/core/merger.ts
|
|
98
|
+
function merge(staticData, dynamicResults) {
|
|
99
|
+
if (!staticData) return dynamicResults;
|
|
100
|
+
return {
|
|
101
|
+
...staticData,
|
|
102
|
+
...dynamicResults
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// src/index.ts
|
|
107
|
+
var import_node_fs = require("fs");
|
|
108
|
+
var import_node_path = require("path");
|
|
109
|
+
|
|
110
|
+
// src/core/injector.ts
|
|
111
|
+
function toWindowString(data, key = "__LLM__") {
|
|
112
|
+
const json = JSON.stringify(data);
|
|
113
|
+
return `window.${key} = ${json};`;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// src/index.ts
|
|
117
|
+
async function createLLMSource(config, options = {}) {
|
|
118
|
+
let staticTruth = {};
|
|
119
|
+
if (config.static) {
|
|
120
|
+
try {
|
|
121
|
+
const staticPath = config.static.startsWith("/") ? config.static : (0, import_node_path.join)(process.cwd(), config.static);
|
|
122
|
+
const content = (0, import_node_fs.readFileSync)(staticPath, "utf-8");
|
|
123
|
+
staticTruth = JSON.parse(content);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
if (options.failLoudly) {
|
|
126
|
+
throw new Error(`[llmweb] Static Truth Error: Failed to load/parse JSON at ${config.static}. ${error}`);
|
|
127
|
+
}
|
|
128
|
+
console.warn(`[llmweb] Warning: Could not load static JSON at ${config.static}. 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/adapters/react.tsx
|
|
158
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
159
|
+
async function LLMJson({
|
|
160
|
+
config,
|
|
161
|
+
mode = "script",
|
|
162
|
+
targetKey = "__LLM__",
|
|
163
|
+
className
|
|
164
|
+
}) {
|
|
165
|
+
const truth = await createLLMSource(config);
|
|
166
|
+
if (mode === "window") {
|
|
167
|
+
const scriptBody = toWindowString(truth, targetKey);
|
|
168
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
169
|
+
"script",
|
|
170
|
+
{
|
|
171
|
+
id: `llm-window-${targetKey}`,
|
|
172
|
+
dangerouslySetInnerHTML: { __html: scriptBody }
|
|
173
|
+
}
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
if (mode === "pre") {
|
|
177
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("pre", { id: "llm-truth-pre", className, children: JSON.stringify(truth, null, 2) });
|
|
178
|
+
}
|
|
179
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
180
|
+
"script",
|
|
181
|
+
{
|
|
182
|
+
id: "llm-truth-script",
|
|
183
|
+
type: "application/llm+json",
|
|
184
|
+
dangerouslySetInnerHTML: { __html: JSON.stringify(truth) }
|
|
185
|
+
}
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
189
|
+
0 && (module.exports = {
|
|
190
|
+
LLMJson
|
|
191
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createLLMSource,
|
|
3
|
+
toWindowString
|
|
4
|
+
} from "../chunk-HRF6I6KB.mjs";
|
|
5
|
+
|
|
6
|
+
// src/adapters/react.tsx
|
|
7
|
+
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
async function LLMJson({
|
|
9
|
+
config,
|
|
10
|
+
mode = "script",
|
|
11
|
+
targetKey = "__LLM__",
|
|
12
|
+
className
|
|
13
|
+
}) {
|
|
14
|
+
const truth = await createLLMSource(config);
|
|
15
|
+
if (mode === "window") {
|
|
16
|
+
const scriptBody = toWindowString(truth, targetKey);
|
|
17
|
+
return /* @__PURE__ */ jsx(
|
|
18
|
+
"script",
|
|
19
|
+
{
|
|
20
|
+
id: `llm-window-${targetKey}`,
|
|
21
|
+
dangerouslySetInnerHTML: { __html: scriptBody }
|
|
22
|
+
}
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
if (mode === "pre") {
|
|
26
|
+
return /* @__PURE__ */ jsx("pre", { id: "llm-truth-pre", className, children: JSON.stringify(truth, null, 2) });
|
|
27
|
+
}
|
|
28
|
+
return /* @__PURE__ */ jsx(
|
|
29
|
+
"script",
|
|
30
|
+
{
|
|
31
|
+
id: "llm-truth-script",
|
|
32
|
+
type: "application/llm+json",
|
|
33
|
+
dangerouslySetInnerHTML: { __html: JSON.stringify(truth) }
|
|
34
|
+
}
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
export {
|
|
38
|
+
LLMJson
|
|
39
|
+
};
|
|
@@ -0,0 +1,165 @@
|
|
|
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/adapters/vanilla.ts
|
|
21
|
+
var vanilla_exports = {};
|
|
22
|
+
__export(vanilla_exports, {
|
|
23
|
+
inject: () => inject
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(vanilla_exports);
|
|
26
|
+
|
|
27
|
+
// src/core/resolver.ts
|
|
28
|
+
async function resolveAll(dynamicSources, options = {}) {
|
|
29
|
+
const keys = Object.keys(dynamicSources);
|
|
30
|
+
const results = await Promise.allSettled(
|
|
31
|
+
keys.map((key) => resolveSource(dynamicSources[key], options.timeout))
|
|
32
|
+
);
|
|
33
|
+
const data = {};
|
|
34
|
+
results.forEach((result, index) => {
|
|
35
|
+
const key = keys[index];
|
|
36
|
+
if (result.status === "fulfilled") {
|
|
37
|
+
data[key] = result.value;
|
|
38
|
+
} else {
|
|
39
|
+
console.error(`[llmweb] Failed to resolve source "${key}":`, result.reason);
|
|
40
|
+
data[key] = null;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return data;
|
|
44
|
+
}
|
|
45
|
+
async function resolveSource(source, timeoutMs) {
|
|
46
|
+
const { from } = source;
|
|
47
|
+
const controller = timeoutMs ? new AbortController() : null;
|
|
48
|
+
const signal = controller?.signal;
|
|
49
|
+
const timeoutPromise = timeoutMs ? new Promise(
|
|
50
|
+
(_, reject) => setTimeout(() => {
|
|
51
|
+
controller?.abort();
|
|
52
|
+
reject(new Error(`Timeout of ${timeoutMs}ms exceeded`));
|
|
53
|
+
}, timeoutMs)
|
|
54
|
+
) : null;
|
|
55
|
+
const resolvePromise = (async () => {
|
|
56
|
+
if (from.type === "fetch") {
|
|
57
|
+
if (!from.url) throw new Error("Fetch source requires a URL");
|
|
58
|
+
const response = await fetch(from.url, { signal });
|
|
59
|
+
if (!response.ok) throw new Error(`HTTP error ${response.status} for ${from.url}`);
|
|
60
|
+
return response.json();
|
|
61
|
+
}
|
|
62
|
+
if (from.type === "fn") {
|
|
63
|
+
if (!from.call) throw new Error('Function source requires a "call" property');
|
|
64
|
+
return from.call();
|
|
65
|
+
}
|
|
66
|
+
throw new Error(`Unsupported source type: ${from.type}`);
|
|
67
|
+
})();
|
|
68
|
+
if (timeoutPromise) {
|
|
69
|
+
return Promise.race([resolvePromise, timeoutPromise]);
|
|
70
|
+
}
|
|
71
|
+
return resolvePromise;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/core/transformer.ts
|
|
75
|
+
function transform(sourceData, schema) {
|
|
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;
|
|
81
|
+
const result = {};
|
|
82
|
+
for (const [targetKey, mapping] of Object.entries(schema)) {
|
|
83
|
+
if (typeof mapping === "string") {
|
|
84
|
+
result[targetKey] = getValueByPath(sourceData, mapping);
|
|
85
|
+
} else if (typeof mapping === "function") {
|
|
86
|
+
result[targetKey] = mapping(sourceData);
|
|
87
|
+
} else if (typeof mapping === "object" && mapping !== null) {
|
|
88
|
+
result[targetKey] = transform(sourceData, mapping);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
function getValueByPath(obj, path) {
|
|
94
|
+
return path.split(".").reduce((acc, part) => acc && acc[part], obj);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// src/core/merger.ts
|
|
98
|
+
function merge(staticData, dynamicResults) {
|
|
99
|
+
if (!staticData) return dynamicResults;
|
|
100
|
+
return {
|
|
101
|
+
...staticData,
|
|
102
|
+
...dynamicResults
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// src/index.ts
|
|
107
|
+
var import_node_fs = require("fs");
|
|
108
|
+
var import_node_path = require("path");
|
|
109
|
+
async function createLLMSource(config, options = {}) {
|
|
110
|
+
let staticTruth = {};
|
|
111
|
+
if (config.static) {
|
|
112
|
+
try {
|
|
113
|
+
const staticPath = config.static.startsWith("/") ? config.static : (0, import_node_path.join)(process.cwd(), config.static);
|
|
114
|
+
const content = (0, import_node_fs.readFileSync)(staticPath, "utf-8");
|
|
115
|
+
staticTruth = JSON.parse(content);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
if (options.failLoudly) {
|
|
118
|
+
throw new Error(`[llmweb] Static Truth Error: Failed to load/parse JSON at ${config.static}. ${error}`);
|
|
119
|
+
}
|
|
120
|
+
console.warn(`[llmweb] Warning: Could not load static JSON at ${config.static}. Proceeding with dynamic data only.`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const dynamicTruth = {};
|
|
124
|
+
if (config.dynamic) {
|
|
125
|
+
const rawResults = await resolveAll(config.dynamic, { timeout: options.timeout });
|
|
126
|
+
for (const [key, source] of Object.entries(config.dynamic)) {
|
|
127
|
+
const rawData = rawResults[key];
|
|
128
|
+
if (rawData === null && options.failLoudly) {
|
|
129
|
+
throw new Error(`[llmweb] Dynamic Truth Error: Source "${key}" failed to resolve.`);
|
|
130
|
+
}
|
|
131
|
+
if (rawData && source.map) {
|
|
132
|
+
try {
|
|
133
|
+
dynamicTruth[key] = transform(rawData, source.map);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if (options.failLoudly) {
|
|
136
|
+
throw new Error(`[llmweb] Transformation Error: Failed to map source "${key}". ${error}`);
|
|
137
|
+
}
|
|
138
|
+
console.error(`[llmweb] Warning: Mapping failed for "${key}". Using raw data.`);
|
|
139
|
+
dynamicTruth[key] = rawData;
|
|
140
|
+
}
|
|
141
|
+
} else {
|
|
142
|
+
dynamicTruth[key] = rawData;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return merge(staticTruth, dynamicTruth);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// src/adapters/vanilla.ts
|
|
150
|
+
async function inject(config, mode = "script", targetKey = "__LLM__") {
|
|
151
|
+
const truth = await createLLMSource(config);
|
|
152
|
+
if (mode === "window") {
|
|
153
|
+
window[targetKey] = truth;
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const script = document.createElement("script");
|
|
157
|
+
script.type = "application/llm+json";
|
|
158
|
+
script.id = "llm-truth-script";
|
|
159
|
+
script.text = JSON.stringify(truth);
|
|
160
|
+
document.head.appendChild(script);
|
|
161
|
+
}
|
|
162
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
163
|
+
0 && (module.exports = {
|
|
164
|
+
inject
|
|
165
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createLLMSource
|
|
3
|
+
} from "../chunk-HRF6I6KB.mjs";
|
|
4
|
+
|
|
5
|
+
// src/adapters/vanilla.ts
|
|
6
|
+
async function inject(config, mode = "script", targetKey = "__LLM__") {
|
|
7
|
+
const truth = await createLLMSource(config);
|
|
8
|
+
if (mode === "window") {
|
|
9
|
+
window[targetKey] = truth;
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const script = document.createElement("script");
|
|
13
|
+
script.type = "application/llm+json";
|
|
14
|
+
script.id = "llm-truth-script";
|
|
15
|
+
script.text = JSON.stringify(truth);
|
|
16
|
+
document.head.appendChild(script);
|
|
17
|
+
}
|
|
18
|
+
export {
|
|
19
|
+
inject
|
|
20
|
+
};
|
|
@@ -47,7 +47,11 @@ async function resolveSource(source, timeoutMs) {
|
|
|
47
47
|
|
|
48
48
|
// src/core/transformer.ts
|
|
49
49
|
function transform(sourceData, schema) {
|
|
50
|
-
if (!sourceData
|
|
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;
|
|
51
55
|
const result = {};
|
|
52
56
|
for (const [targetKey, mapping] of Object.entries(schema)) {
|
|
53
57
|
if (typeof mapping === "string") {
|
|
@@ -76,6 +80,18 @@ function merge(staticData, dynamicResults) {
|
|
|
76
80
|
// src/index.ts
|
|
77
81
|
import { readFileSync } from "fs";
|
|
78
82
|
import { join } from "path";
|
|
83
|
+
|
|
84
|
+
// src/core/injector.ts
|
|
85
|
+
function toScriptString(data) {
|
|
86
|
+
const json = JSON.stringify(data);
|
|
87
|
+
return `<script type="application/llm+json">${json}</script>`;
|
|
88
|
+
}
|
|
89
|
+
function toWindowString(data, key = "__LLM__") {
|
|
90
|
+
const json = JSON.stringify(data);
|
|
91
|
+
return `window.${key} = ${json};`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/index.ts
|
|
79
95
|
async function createLLMSource(config, options = {}) {
|
|
80
96
|
let staticTruth = {};
|
|
81
97
|
if (config.static) {
|
|
@@ -118,5 +134,7 @@ async function createLLMSource(config, options = {}) {
|
|
|
118
134
|
|
|
119
135
|
export {
|
|
120
136
|
transform,
|
|
137
|
+
toScriptString,
|
|
138
|
+
toWindowString,
|
|
121
139
|
createLLMSource
|
|
122
140
|
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logic to generate injection strings for DOM discovery.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Wraps JSON data in a <script type="application/llm+json"> tag.
|
|
6
|
+
*/
|
|
7
|
+
export declare function toScriptString(data: any): string;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a global assignment string (e.g., window.__LLM__ = ...).
|
|
10
|
+
*/
|
|
11
|
+
export declare function toWindowString(data: any, key?: string): string;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -21,6 +21,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
createLLMSource: () => createLLMSource,
|
|
24
|
+
toScriptString: () => toScriptString,
|
|
25
|
+
toWindowString: () => toWindowString,
|
|
24
26
|
transform: () => transform
|
|
25
27
|
});
|
|
26
28
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -74,7 +76,11 @@ async function resolveSource(source, timeoutMs) {
|
|
|
74
76
|
|
|
75
77
|
// src/core/transformer.ts
|
|
76
78
|
function transform(sourceData, schema) {
|
|
77
|
-
if (!sourceData
|
|
79
|
+
if (!sourceData) return sourceData;
|
|
80
|
+
if (Array.isArray(sourceData)) {
|
|
81
|
+
return sourceData.map((item) => transform(item, schema));
|
|
82
|
+
}
|
|
83
|
+
if (typeof sourceData !== "object") return sourceData;
|
|
78
84
|
const result = {};
|
|
79
85
|
for (const [targetKey, mapping] of Object.entries(schema)) {
|
|
80
86
|
if (typeof mapping === "string") {
|
|
@@ -103,6 +109,18 @@ function merge(staticData, dynamicResults) {
|
|
|
103
109
|
// src/index.ts
|
|
104
110
|
var import_node_fs = require("fs");
|
|
105
111
|
var import_node_path = require("path");
|
|
112
|
+
|
|
113
|
+
// src/core/injector.ts
|
|
114
|
+
function toScriptString(data) {
|
|
115
|
+
const json = JSON.stringify(data);
|
|
116
|
+
return `<script type="application/llm+json">${json}</script>`;
|
|
117
|
+
}
|
|
118
|
+
function toWindowString(data, key = "__LLM__") {
|
|
119
|
+
const json = JSON.stringify(data);
|
|
120
|
+
return `window.${key} = ${json};`;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// src/index.ts
|
|
106
124
|
async function createLLMSource(config, options = {}) {
|
|
107
125
|
let staticTruth = {};
|
|
108
126
|
if (config.static) {
|
|
@@ -145,5 +163,7 @@ async function createLLMSource(config, options = {}) {
|
|
|
145
163
|
// Annotate the CommonJS export names for ESM import in node:
|
|
146
164
|
0 && (module.exports = {
|
|
147
165
|
createLLMSource,
|
|
166
|
+
toScriptString,
|
|
167
|
+
toWindowString,
|
|
148
168
|
transform
|
|
149
169
|
});
|
package/dist/index.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaleabdenbel/llmweb",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "A compiler for LLM-readable truth from static and dynamic sources.",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -19,6 +19,16 @@
|
|
|
19
19
|
"require": "./dist/adapters/next.js",
|
|
20
20
|
"types": "./dist/adapters/next.d.ts"
|
|
21
21
|
},
|
|
22
|
+
"./adapters/react": {
|
|
23
|
+
"import": "./dist/adapters/react.mjs",
|
|
24
|
+
"require": "./dist/adapters/react.js",
|
|
25
|
+
"types": "./dist/adapters/react.d.ts"
|
|
26
|
+
},
|
|
27
|
+
"./adapters/vanilla": {
|
|
28
|
+
"import": "./dist/adapters/vanilla.mjs",
|
|
29
|
+
"require": "./dist/adapters/vanilla.js",
|
|
30
|
+
"types": "./dist/adapters/vanilla.d.ts"
|
|
31
|
+
},
|
|
22
32
|
"./adapters/express": {
|
|
23
33
|
"import": "./dist/adapters/express.mjs",
|
|
24
34
|
"require": "./dist/adapters/express.js",
|
|
@@ -31,8 +41,8 @@
|
|
|
31
41
|
"README.md"
|
|
32
42
|
],
|
|
33
43
|
"scripts": {
|
|
34
|
-
"build": "tsup src/index.ts src/adapters/next.tsx src/adapters/express.ts --format cjs,esm --dts --clean",
|
|
35
|
-
"dev": "tsup src/index.ts src/adapters/next.tsx src/adapters/express.ts --format cjs,esm --watch --dts",
|
|
44
|
+
"build": "tsup src/index.ts src/adapters/next.tsx src/adapters/react.tsx src/adapters/vanilla.ts src/adapters/express.ts --format cjs,esm --dts --clean",
|
|
45
|
+
"dev": "tsup src/index.ts src/adapters/next.tsx src/adapters/react.tsx src/adapters/vanilla.ts src/adapters/express.ts --format cjs,esm --watch --dts",
|
|
36
46
|
"lint": "tsc",
|
|
37
47
|
"test": "vitest run"
|
|
38
48
|
},
|