@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.
@@ -0,0 +1,145 @@
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/core/injector.ts
123
+ function toScriptString(data) {
124
+ const json = JSON.stringify(data);
125
+ return `<script type="application/llm+json">${json}</script>`;
126
+ }
127
+ function toWindowString(data, key = "__LLM__") {
128
+ const json = JSON.stringify(data);
129
+ return `window.${key} = ${json};`;
130
+ }
131
+
132
+ // src/index.browser.ts
133
+ var browserLoader = async (path) => {
134
+ console.warn(`[llmweb] Static files cannot be loaded in the browser: ${path}. Please use dynamic sources or pass data directly.`);
135
+ return null;
136
+ };
137
+ async function createLLMSource(config, options = {}) {
138
+ return compileLLM(config, browserLoader, options);
139
+ }
140
+ export {
141
+ createLLMSource,
142
+ toScriptString,
143
+ toWindowString,
144
+ transform
145
+ };
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 || typeof sourceData !== "object") return 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") {
@@ -100,16 +106,15 @@ function merge(staticData, dynamicResults) {
100
106
  };
101
107
  }
102
108
 
103
- // src/index.ts
104
- var import_node_fs = require("fs");
105
- var import_node_path = require("path");
106
- async function createLLMSource(config, options = {}) {
109
+ // src/core/compiler.ts
110
+ async function compileLLM(config, loader, options = {}) {
107
111
  let staticTruth = {};
108
112
  if (config.static) {
109
113
  try {
110
- const staticPath = config.static.startsWith("/") ? config.static : (0, import_node_path.join)(process.cwd(), config.static);
111
- const content = (0, import_node_fs.readFileSync)(staticPath, "utf-8");
112
- staticTruth = JSON.parse(content);
114
+ const content = await loader(config.static);
115
+ if (content) {
116
+ staticTruth = JSON.parse(content);
117
+ }
113
118
  } catch (error) {
114
119
  if (options.failLoudly) {
115
120
  throw new Error(`[llmweb] Static Truth Error: Failed to load/parse JSON at ${config.static}. ${error}`);
@@ -142,8 +147,33 @@ async function createLLMSource(config, options = {}) {
142
147
  }
143
148
  return merge(staticTruth, dynamicTruth);
144
149
  }
150
+
151
+ // src/index.ts
152
+ var import_node_fs = require("fs");
153
+ var import_node_path = require("path");
154
+
155
+ // src/core/injector.ts
156
+ function toScriptString(data) {
157
+ const json = JSON.stringify(data);
158
+ return `<script type="application/llm+json">${json}</script>`;
159
+ }
160
+ function toWindowString(data, key = "__LLM__") {
161
+ const json = JSON.stringify(data);
162
+ return `window.${key} = ${json};`;
163
+ }
164
+
165
+ // src/index.ts
166
+ var nodeLoader = async (path) => {
167
+ const fullPath = path.startsWith("/") ? path : (0, import_node_path.join)(process.cwd(), path);
168
+ return (0, import_node_fs.readFileSync)(fullPath, "utf-8");
169
+ };
170
+ async function createLLMSource(config, options = {}) {
171
+ return compileLLM(config, nodeLoader, options);
172
+ }
145
173
  // Annotate the CommonJS export names for ESM import in node:
146
174
  0 && (module.exports = {
147
175
  createLLMSource,
176
+ toScriptString,
177
+ toWindowString,
148
178
  transform
149
179
  });
package/dist/index.mjs CHANGED
@@ -1,8 +1,149 @@
1
- import {
2
- createLLMSource,
3
- transform
4
- } from "./chunk-PYG5H54N.mjs";
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
+
126
+ // src/core/injector.ts
127
+ function toScriptString(data) {
128
+ const json = JSON.stringify(data);
129
+ return `<script type="application/llm+json">${json}</script>`;
130
+ }
131
+ function toWindowString(data, key = "__LLM__") {
132
+ const json = JSON.stringify(data);
133
+ return `window.${key} = ${json};`;
134
+ }
135
+
136
+ // src/index.ts
137
+ var nodeLoader = async (path) => {
138
+ const fullPath = path.startsWith("/") ? path : join(process.cwd(), path);
139
+ return readFileSync(fullPath, "utf-8");
140
+ };
141
+ async function createLLMSource(config, options = {}) {
142
+ return compileLLM(config, nodeLoader, options);
143
+ }
5
144
  export {
6
145
  createLLMSource,
146
+ toScriptString,
147
+ toWindowString,
7
148
  transform
8
149
  };
package/package.json CHANGED
@@ -1,15 +1,17 @@
1
1
  {
2
2
  "name": "@kaleabdenbel/llmweb",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "A compiler for LLM-readable truth from static and dynamic sources.",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
8
  "main": "./dist/index.js",
9
9
  "module": "./dist/index.mjs",
10
+ "browser": "./dist/index.browser.mjs",
10
11
  "types": "./dist/index.d.ts",
11
12
  "exports": {
12
13
  ".": {
14
+ "browser": "./dist/index.browser.mjs",
13
15
  "import": "./dist/index.mjs",
14
16
  "require": "./dist/index.js",
15
17
  "types": "./dist/index.d.ts"
@@ -19,6 +21,16 @@
19
21
  "require": "./dist/adapters/next.js",
20
22
  "types": "./dist/adapters/next.d.ts"
21
23
  },
24
+ "./adapters/react": {
25
+ "import": "./dist/adapters/react.mjs",
26
+ "require": "./dist/adapters/react.js",
27
+ "types": "./dist/adapters/react.d.ts"
28
+ },
29
+ "./adapters/vanilla": {
30
+ "import": "./dist/adapters/vanilla.mjs",
31
+ "require": "./dist/adapters/vanilla.js",
32
+ "types": "./dist/adapters/vanilla.d.ts"
33
+ },
22
34
  "./adapters/express": {
23
35
  "import": "./dist/adapters/express.mjs",
24
36
  "require": "./dist/adapters/express.js",
@@ -31,8 +43,8 @@
31
43
  "README.md"
32
44
  ],
33
45
  "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",
46
+ "build": "tsup src/index.ts src/index.browser.ts src/adapters/next.tsx src/adapters/react.tsx src/adapters/vanilla.ts src/adapters/express.ts --format cjs,esm --dts --clean --no-splitting",
47
+ "dev": "tsup src/index.ts src/index.browser.ts src/adapters/next.tsx src/adapters/react.tsx src/adapters/vanilla.ts src/adapters/express.ts --format cjs,esm --watch --dts --no-splitting",
36
48
  "lint": "tsc",
37
49
  "test": "vitest run"
38
50
  },
@@ -1,5 +0,0 @@
1
- import { LLMConfig } from '../types';
2
- /**
3
- * An Express middleware that serves the LLM truth.
4
- */
5
- export declare function llmMiddleware(config: LLMConfig): (req: any, res: any, next: any) => Promise<void>;
@@ -1,18 +0,0 @@
1
- import { LLMConfig } from "../types";
2
- interface LLMJsonProps {
3
- config: LLMConfig;
4
- /**
5
- * Optional custom styling for the rendered JSON if viewing in a browser.
6
- */
7
- className?: string;
8
- }
9
- /**
10
- * A Next.js Server Component that resolves the LLM truth and renders it.
11
- * By default, it renders a <pre> tag with the JSON string.
12
- */
13
- export declare function LLMJson({ config, className }: LLMJsonProps): Promise<import("react/jsx-runtime").JSX.Element>;
14
- /**
15
- * A helper for Next.js App Router Route Handlers (route.ts).
16
- */
17
- export declare function createLLMHandler(config: LLMConfig): () => Promise<Response>;
18
- export {};
@@ -1,7 +0,0 @@
1
- /**
2
- * Merges static truth with dynamic truth.
3
- * Rules:
4
- * - Static defines the base object.
5
- * - Dynamic results overwrite or fill slots in the static base based on their keys.
6
- */
7
- export declare function merge(staticData: any, dynamicResults: any): any;
@@ -1,12 +0,0 @@
1
- import { DynamicSource } from '../types';
2
- export interface ResolvedData {
3
- [key: string]: any;
4
- }
5
- /**
6
- * Resolves all dynamic sources in parallel.
7
- */
8
- export declare function resolveAll(dynamicSources: {
9
- [key: string]: DynamicSource;
10
- }, options?: {
11
- timeout?: number;
12
- }): Promise<ResolvedData>;
@@ -1,7 +0,0 @@
1
- import { MapSchema } from '../types';
2
- /**
3
- * Transforms source data according to a MapSchema.
4
- * The schema keys represent the target structure, and the values
5
- * represent the source keys or transformation functions.
6
- */
7
- export declare function transform(sourceData: any, schema: MapSchema): any;
package/dist/index.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import { LLMConfig, TransformerOptions } from './types';
2
- /**
3
- * The main "Compiler" for LLM truth.
4
- */
5
- export declare function createLLMSource(config: LLMConfig, options?: TransformerOptions): Promise<any>;
6
- export * from './types';
7
- export { transform } from './core/transformer';
@@ -1,23 +0,0 @@
1
- export type SourceType = 'fetch' | 'fn';
2
- export interface SourceDefinition {
3
- type: SourceType;
4
- url?: string;
5
- call?: (...args: any[]) => Promise<any> | any;
6
- }
7
- export interface MapSchema {
8
- [targetKey: string]: string | ((sourceData: any) => any) | MapSchema;
9
- }
10
- export interface DynamicSource {
11
- from: SourceDefinition;
12
- map?: MapSchema;
13
- }
14
- export interface LLMConfig {
15
- static?: string;
16
- dynamic?: {
17
- [key: string]: DynamicSource;
18
- };
19
- }
20
- export interface TransformerOptions {
21
- failLoudly?: boolean;
22
- timeout?: number;
23
- }