@sohqureshi/tokenwise 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +260 -0
- package/dist/chunk-2IQAGI7Q.js +54 -0
- package/dist/chunk-33GBS5YN.js +288 -0
- package/dist/chunk-33RRCCAT.js +287 -0
- package/dist/chunk-3ECVBELU.js +49 -0
- package/dist/chunk-D4CFTFM3.js +105 -0
- package/dist/chunk-EUFLW42L.js +13 -0
- package/dist/chunk-GQ62V6ZK.js +61 -0
- package/dist/chunk-HKCPRPJL.js +294 -0
- package/dist/chunk-IAV75SZA.js +36 -0
- package/dist/chunk-JBKETSTK.js +270 -0
- package/dist/chunk-SURFMVNH.js +285 -0
- package/dist/chunk-TM6L7YF2.js +9 -0
- package/dist/chunk-YMOSWQGE.js +22 -0
- package/dist/cli.cjs +352 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +51 -0
- package/dist/core/analyze.cjs +181 -0
- package/dist/core/analyze.d.cts +13 -0
- package/dist/core/analyze.d.ts +13 -0
- package/dist/core/analyze.js +11 -0
- package/dist/core/compact.cjs +68 -0
- package/dist/core/compact.d.cts +3 -0
- package/dist/core/compact.d.ts +3 -0
- package/dist/core/compact.js +7 -0
- package/dist/core/flatten.cjs +46 -0
- package/dist/core/flatten.d.cts +18 -0
- package/dist/core/flatten.d.ts +18 -0
- package/dist/core/flatten.js +6 -0
- package/dist/core/natural.cjs +129 -0
- package/dist/core/natural.d.cts +28 -0
- package/dist/core/natural.d.ts +28 -0
- package/dist/core/natural.js +6 -0
- package/dist/core/prune.cjs +60 -0
- package/dist/core/prune.d.cts +11 -0
- package/dist/core/prune.d.ts +11 -0
- package/dist/core/prune.js +6 -0
- package/dist/core/token.cjs +33 -0
- package/dist/core/token.d.cts +12 -0
- package/dist/core/token.d.ts +12 -0
- package/dist/core/token.js +6 -0
- package/dist/core/toon.cjs +73 -0
- package/dist/core/toon.d.cts +25 -0
- package/dist/core/toon.d.ts +25 -0
- package/dist/core/toon.js +6 -0
- package/dist/index.cjs +344 -0
- package/dist/index.d.cts +40 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.js +49 -0
- package/package.json +78 -0
|
@@ -0,0 +1,60 @@
|
|
|
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/core/prune.ts
|
|
21
|
+
var prune_exports = {};
|
|
22
|
+
__export(prune_exports, {
|
|
23
|
+
prune: () => prune
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(prune_exports);
|
|
26
|
+
function prune(obj, options = {}) {
|
|
27
|
+
const normalizedOptions = Array.isArray(options) ? { removeKeys: options } : options;
|
|
28
|
+
const {
|
|
29
|
+
removeKeys = [],
|
|
30
|
+
removeNull = true,
|
|
31
|
+
removeUndefined = true,
|
|
32
|
+
removeEmptyObjects = true,
|
|
33
|
+
removeEmptyArrays = false
|
|
34
|
+
} = normalizedOptions;
|
|
35
|
+
if (obj === null) return removeNull ? void 0 : obj;
|
|
36
|
+
if (obj === void 0) return removeUndefined ? void 0 : obj;
|
|
37
|
+
if (typeof obj !== "object") return obj;
|
|
38
|
+
if (Array.isArray(obj)) {
|
|
39
|
+
const arr = obj.map((item) => prune(item, normalizedOptions)).filter((item) => item !== void 0);
|
|
40
|
+
if (removeEmptyArrays && arr.length === 0) {
|
|
41
|
+
return void 0;
|
|
42
|
+
}
|
|
43
|
+
return arr;
|
|
44
|
+
}
|
|
45
|
+
const result = {};
|
|
46
|
+
for (const key in obj) {
|
|
47
|
+
if (removeKeys.includes(key)) continue;
|
|
48
|
+
const value = prune(obj[key], normalizedOptions);
|
|
49
|
+
if (value === void 0) continue;
|
|
50
|
+
if (removeEmptyObjects && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
result[key] = value;
|
|
54
|
+
}
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
58
|
+
0 && (module.exports = {
|
|
59
|
+
prune
|
|
60
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
type PruneOptions = {
|
|
2
|
+
removeKeys?: string[];
|
|
3
|
+
removeNull?: boolean;
|
|
4
|
+
removeUndefined?: boolean;
|
|
5
|
+
removeEmptyObjects?: boolean;
|
|
6
|
+
removeEmptyArrays?: boolean;
|
|
7
|
+
};
|
|
8
|
+
type PruneInput = PruneOptions | string[];
|
|
9
|
+
declare function prune(obj: any, options?: PruneInput): any;
|
|
10
|
+
|
|
11
|
+
export { prune };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
type PruneOptions = {
|
|
2
|
+
removeKeys?: string[];
|
|
3
|
+
removeNull?: boolean;
|
|
4
|
+
removeUndefined?: boolean;
|
|
5
|
+
removeEmptyObjects?: boolean;
|
|
6
|
+
removeEmptyArrays?: boolean;
|
|
7
|
+
};
|
|
8
|
+
type PruneInput = PruneOptions | string[];
|
|
9
|
+
declare function prune(obj: any, options?: PruneInput): any;
|
|
10
|
+
|
|
11
|
+
export { prune };
|
|
@@ -0,0 +1,33 @@
|
|
|
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/core/token.ts
|
|
21
|
+
var token_exports = {};
|
|
22
|
+
__export(token_exports, {
|
|
23
|
+
estimateTokens: () => estimateTokens
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(token_exports);
|
|
26
|
+
function estimateTokens(text) {
|
|
27
|
+
if (!text) return 0;
|
|
28
|
+
return Math.ceil(text.length / 4);
|
|
29
|
+
}
|
|
30
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
31
|
+
0 && (module.exports = {
|
|
32
|
+
estimateTokens
|
|
33
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Estimates token count from string length.
|
|
3
|
+
*
|
|
4
|
+
* Approximation:
|
|
5
|
+
* 1 token ≈ 4 characters (common for GPT models)
|
|
6
|
+
*
|
|
7
|
+
* @param text - Input string
|
|
8
|
+
* @returns Estimated token count
|
|
9
|
+
*/
|
|
10
|
+
declare function estimateTokens(text: string): number;
|
|
11
|
+
|
|
12
|
+
export { estimateTokens };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Estimates token count from string length.
|
|
3
|
+
*
|
|
4
|
+
* Approximation:
|
|
5
|
+
* 1 token ≈ 4 characters (common for GPT models)
|
|
6
|
+
*
|
|
7
|
+
* @param text - Input string
|
|
8
|
+
* @returns Estimated token count
|
|
9
|
+
*/
|
|
10
|
+
declare function estimateTokens(text: string): number;
|
|
11
|
+
|
|
12
|
+
export { estimateTokens };
|
|
@@ -0,0 +1,73 @@
|
|
|
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/core/toon.ts
|
|
21
|
+
var toon_exports = {};
|
|
22
|
+
__export(toon_exports, {
|
|
23
|
+
toTOON: () => toTOON
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(toon_exports);
|
|
26
|
+
function toTOON(data, indent = 0) {
|
|
27
|
+
const space = " ".repeat(indent);
|
|
28
|
+
if (Array.isArray(data)) {
|
|
29
|
+
if (data.length === 0) return "[]";
|
|
30
|
+
if (data.every(isPlainObject)) {
|
|
31
|
+
const keys = Array.from(
|
|
32
|
+
new Set(data.flatMap((item) => Object.keys(item)))
|
|
33
|
+
);
|
|
34
|
+
let result = `${space}[${data.length}]{${keys.join(",")}}:
|
|
35
|
+
`;
|
|
36
|
+
for (const item of data) {
|
|
37
|
+
result += space + " " + keys.map((k) => formatValue(item[k])).join(",") + "\n";
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
return `${space}[${data.length}]: ${data.join(",")}`;
|
|
42
|
+
}
|
|
43
|
+
if (typeof data === "object" && data !== null) {
|
|
44
|
+
let result = "";
|
|
45
|
+
for (const key in data) {
|
|
46
|
+
const value = data[key];
|
|
47
|
+
if (typeof value === "object" && value !== null) {
|
|
48
|
+
result += `${space}${key}:
|
|
49
|
+
${toTOON(value, indent + 1)}
|
|
50
|
+
`;
|
|
51
|
+
} else {
|
|
52
|
+
const formatted = formatValue(value);
|
|
53
|
+
result += formatted ? `${space}${key}: ${formatted}
|
|
54
|
+
` : `${space}${key}:
|
|
55
|
+
`;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return result.trim();
|
|
59
|
+
}
|
|
60
|
+
return formatValue(data);
|
|
61
|
+
}
|
|
62
|
+
function formatValue(val) {
|
|
63
|
+
if (val === null || val === void 0) return "";
|
|
64
|
+
if (typeof val === "string") return val;
|
|
65
|
+
return String(val);
|
|
66
|
+
}
|
|
67
|
+
function isPlainObject(value) {
|
|
68
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
69
|
+
}
|
|
70
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
71
|
+
0 && (module.exports = {
|
|
72
|
+
toTOON
|
|
73
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts JSON into TOON-like (Token-Oriented Object Notation) format.
|
|
3
|
+
*
|
|
4
|
+
* Goal:
|
|
5
|
+
* - Remove repeated keys
|
|
6
|
+
* - Represent arrays as table-like structure
|
|
7
|
+
* - Reduce token usage for LLMs
|
|
8
|
+
*
|
|
9
|
+
* @param data - Input JSON
|
|
10
|
+
* @param indent - Internal indentation level
|
|
11
|
+
* @returns Token-efficient string format
|
|
12
|
+
*
|
|
13
|
+
* Example:
|
|
14
|
+
* Input:
|
|
15
|
+
* {
|
|
16
|
+
* users: [{ id: 1, name: "Ali" }]
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* Output:
|
|
20
|
+
* users[1]{id,name}:
|
|
21
|
+
* 1,Ali
|
|
22
|
+
*/
|
|
23
|
+
declare function toTOON(data: any, indent?: number): string;
|
|
24
|
+
|
|
25
|
+
export { toTOON };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts JSON into TOON-like (Token-Oriented Object Notation) format.
|
|
3
|
+
*
|
|
4
|
+
* Goal:
|
|
5
|
+
* - Remove repeated keys
|
|
6
|
+
* - Represent arrays as table-like structure
|
|
7
|
+
* - Reduce token usage for LLMs
|
|
8
|
+
*
|
|
9
|
+
* @param data - Input JSON
|
|
10
|
+
* @param indent - Internal indentation level
|
|
11
|
+
* @returns Token-efficient string format
|
|
12
|
+
*
|
|
13
|
+
* Example:
|
|
14
|
+
* Input:
|
|
15
|
+
* {
|
|
16
|
+
* users: [{ id: 1, name: "Ali" }]
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* Output:
|
|
20
|
+
* users[1]{id,name}:
|
|
21
|
+
* 1,Ali
|
|
22
|
+
*/
|
|
23
|
+
declare function toTOON(data: any, indent?: number): string;
|
|
24
|
+
|
|
25
|
+
export { toTOON };
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
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.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
AIChain: () => AIChain,
|
|
24
|
+
analyze: () => analyze,
|
|
25
|
+
compact: () => compact,
|
|
26
|
+
default: () => index_default,
|
|
27
|
+
estimateTokens: () => estimateTokens,
|
|
28
|
+
flatten: () => flatten,
|
|
29
|
+
prune: () => prune,
|
|
30
|
+
toNatural: () => toNatural,
|
|
31
|
+
toTOON: () => toTOON
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(index_exports);
|
|
34
|
+
|
|
35
|
+
// src/core/prune.ts
|
|
36
|
+
function prune(obj, options = {}) {
|
|
37
|
+
const normalizedOptions = Array.isArray(options) ? { removeKeys: options } : options;
|
|
38
|
+
const {
|
|
39
|
+
removeKeys = [],
|
|
40
|
+
removeNull = true,
|
|
41
|
+
removeUndefined = true,
|
|
42
|
+
removeEmptyObjects = true,
|
|
43
|
+
removeEmptyArrays = false
|
|
44
|
+
} = normalizedOptions;
|
|
45
|
+
if (obj === null) return removeNull ? void 0 : obj;
|
|
46
|
+
if (obj === void 0) return removeUndefined ? void 0 : obj;
|
|
47
|
+
if (typeof obj !== "object") return obj;
|
|
48
|
+
if (Array.isArray(obj)) {
|
|
49
|
+
const arr = obj.map((item) => prune(item, normalizedOptions)).filter((item) => item !== void 0);
|
|
50
|
+
if (removeEmptyArrays && arr.length === 0) {
|
|
51
|
+
return void 0;
|
|
52
|
+
}
|
|
53
|
+
return arr;
|
|
54
|
+
}
|
|
55
|
+
const result = {};
|
|
56
|
+
for (const key in obj) {
|
|
57
|
+
if (removeKeys.includes(key)) continue;
|
|
58
|
+
const value = prune(obj[key], normalizedOptions);
|
|
59
|
+
if (value === void 0) continue;
|
|
60
|
+
if (removeEmptyObjects && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
result[key] = value;
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/core/flatten.ts
|
|
69
|
+
function flatten(obj, prefix = "", res = {}) {
|
|
70
|
+
if (obj === null || obj === void 0) return res;
|
|
71
|
+
if (typeof obj !== "object") {
|
|
72
|
+
res[prefix] = obj;
|
|
73
|
+
return res;
|
|
74
|
+
}
|
|
75
|
+
for (const key in obj) {
|
|
76
|
+
const value = obj[key];
|
|
77
|
+
const newKey = prefix ? `${prefix}.${key}` : key;
|
|
78
|
+
if (typeof value === "object" && value !== null) {
|
|
79
|
+
flatten(value, newKey, res);
|
|
80
|
+
} else {
|
|
81
|
+
res[newKey] = value;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return res;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// src/core/compact.ts
|
|
88
|
+
function compact(obj) {
|
|
89
|
+
const pruned = prune(obj);
|
|
90
|
+
return JSON.stringify(pruned) ?? "";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/core/toon.ts
|
|
94
|
+
function toTOON(data, indent = 0) {
|
|
95
|
+
const space = " ".repeat(indent);
|
|
96
|
+
if (Array.isArray(data)) {
|
|
97
|
+
if (data.length === 0) return "[]";
|
|
98
|
+
if (data.every(isPlainObject)) {
|
|
99
|
+
const keys = Array.from(
|
|
100
|
+
new Set(data.flatMap((item) => Object.keys(item)))
|
|
101
|
+
);
|
|
102
|
+
let result = `${space}[${data.length}]{${keys.join(",")}}:
|
|
103
|
+
`;
|
|
104
|
+
for (const item of data) {
|
|
105
|
+
result += space + " " + keys.map((k) => formatValue(item[k])).join(",") + "\n";
|
|
106
|
+
}
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
return `${space}[${data.length}]: ${data.join(",")}`;
|
|
110
|
+
}
|
|
111
|
+
if (typeof data === "object" && data !== null) {
|
|
112
|
+
let result = "";
|
|
113
|
+
for (const key in data) {
|
|
114
|
+
const value = data[key];
|
|
115
|
+
if (typeof value === "object" && value !== null) {
|
|
116
|
+
result += `${space}${key}:
|
|
117
|
+
${toTOON(value, indent + 1)}
|
|
118
|
+
`;
|
|
119
|
+
} else {
|
|
120
|
+
const formatted = formatValue(value);
|
|
121
|
+
result += formatted ? `${space}${key}: ${formatted}
|
|
122
|
+
` : `${space}${key}:
|
|
123
|
+
`;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return result.trim();
|
|
127
|
+
}
|
|
128
|
+
return formatValue(data);
|
|
129
|
+
}
|
|
130
|
+
function formatValue(val) {
|
|
131
|
+
if (val === null || val === void 0) return "";
|
|
132
|
+
if (typeof val === "string") return val;
|
|
133
|
+
return String(val);
|
|
134
|
+
}
|
|
135
|
+
function isPlainObject(value) {
|
|
136
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// src/core/token.ts
|
|
140
|
+
function estimateTokens(text) {
|
|
141
|
+
if (!text) return 0;
|
|
142
|
+
return Math.ceil(text.length / 4);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// src/core/analyze.ts
|
|
146
|
+
function analyze(input, options = {}) {
|
|
147
|
+
if (!input || typeof input === "object" && input !== null && Object.keys(input).length === 0) {
|
|
148
|
+
return {
|
|
149
|
+
originalTokens: 0,
|
|
150
|
+
optimizedTokens: 0,
|
|
151
|
+
savings: 0,
|
|
152
|
+
savingsPercent: 0,
|
|
153
|
+
optimizedData: null,
|
|
154
|
+
reductionRatio: 1
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
let originalTokens = estimateTokens(input);
|
|
158
|
+
if (isNaN(originalTokens) || !isFinite(originalTokens)) originalTokens = 0;
|
|
159
|
+
let optimizedData = input;
|
|
160
|
+
if (options.prune && Array.isArray(options.prune)) {
|
|
161
|
+
optimizedData = prune(optimizedData, options.prune);
|
|
162
|
+
}
|
|
163
|
+
if (options.compact) {
|
|
164
|
+
optimizedData = compact(optimizedData);
|
|
165
|
+
}
|
|
166
|
+
if (options.flatten) {
|
|
167
|
+
optimizedData = flatten(optimizedData);
|
|
168
|
+
}
|
|
169
|
+
if (options.toTOON === true || options.toon === true) {
|
|
170
|
+
optimizedData = toTOON(optimizedData);
|
|
171
|
+
}
|
|
172
|
+
let optimizedTokens = estimateTokens(optimizedData);
|
|
173
|
+
if (isNaN(optimizedTokens) || !isFinite(optimizedTokens)) optimizedTokens = 0;
|
|
174
|
+
const savings = Math.max(0, originalTokens - optimizedTokens);
|
|
175
|
+
const savingsPercent = originalTokens > 0 ? Math.round(savings / originalTokens * 100) : 0;
|
|
176
|
+
const reductionRatio = originalTokens > 0 ? optimizedTokens / originalTokens : 1;
|
|
177
|
+
return {
|
|
178
|
+
originalTokens,
|
|
179
|
+
optimizedTokens,
|
|
180
|
+
savings,
|
|
181
|
+
savingsPercent,
|
|
182
|
+
optimizedData,
|
|
183
|
+
reductionRatio
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// src/core/natural.ts
|
|
188
|
+
function toNatural(data, depth = 0) {
|
|
189
|
+
if (data === null || data === void 0) return "nothing";
|
|
190
|
+
if (typeof data === "string") return data;
|
|
191
|
+
if (typeof data === "number") return String(data);
|
|
192
|
+
if (typeof data === "boolean") return data ? "yes" : "no";
|
|
193
|
+
if (Array.isArray(data)) {
|
|
194
|
+
if (data.length === 0) return "empty list";
|
|
195
|
+
if (data.every(isPlainObject2)) {
|
|
196
|
+
return data.map((item, index) => `${index + 1}. ${stripTrailingPeriod(toNatural(item, depth + 1))}.`).join(" ");
|
|
197
|
+
}
|
|
198
|
+
const items = data.map((item) => toNatural(item, depth + 1));
|
|
199
|
+
return joinNaturalList(items);
|
|
200
|
+
}
|
|
201
|
+
if (typeof data === "object") {
|
|
202
|
+
return buildContextualStory(data, depth);
|
|
203
|
+
}
|
|
204
|
+
return String(data);
|
|
205
|
+
}
|
|
206
|
+
function buildContextualStory(obj, depth = 0) {
|
|
207
|
+
let name = obj.name || obj.userName || obj.user;
|
|
208
|
+
if (typeof name === "object" && name !== null && name.name) {
|
|
209
|
+
name = name.name;
|
|
210
|
+
}
|
|
211
|
+
const entries = Object.entries(obj).filter(([key]) => {
|
|
212
|
+
return !["id", "timestamp", "apiKey"].includes(key);
|
|
213
|
+
});
|
|
214
|
+
if (entries.length === 0) return "";
|
|
215
|
+
let story = name && typeof name === "string" ? `User ${name}` : "";
|
|
216
|
+
const clauses = entries.map(([key, value]) => {
|
|
217
|
+
if (key === "user" && name && typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
218
|
+
const userProps = Object.entries(value).filter(([k]) => !["id", "name", "timestamp", "apiKey"].includes(k)).map(([k, v]) => formatPropertyClause(k, v, depth + 1)).filter((c) => c !== null && c !== "").join(", ");
|
|
219
|
+
return userProps ? `(${userProps})` : null;
|
|
220
|
+
}
|
|
221
|
+
return formatPropertyClause(key, value, depth);
|
|
222
|
+
}).filter((c) => c !== null && c !== "");
|
|
223
|
+
if (clauses.length === 0) return story;
|
|
224
|
+
if (story) {
|
|
225
|
+
story += " " + clauses.join(", ");
|
|
226
|
+
} else {
|
|
227
|
+
story = clauses.join(", ");
|
|
228
|
+
}
|
|
229
|
+
return story + ".";
|
|
230
|
+
}
|
|
231
|
+
function formatPropertyClause(key, value, depth) {
|
|
232
|
+
const naturalKey = camelToWords(key);
|
|
233
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
234
|
+
if (["internal", "metadata", "debug"].includes(key)) {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
const nested = Object.entries(value).filter(([k]) => !["id", "timestamp", "apiKey", "createdAt", "updatedAt"].includes(k)).map(([k, v]) => {
|
|
238
|
+
if (typeof v === "object" && v !== null) {
|
|
239
|
+
return formatPropertyClause(k, v, depth + 1);
|
|
240
|
+
}
|
|
241
|
+
const propValue = toNatural(v, depth + 1);
|
|
242
|
+
if (propValue === "yes" || propValue === "no") {
|
|
243
|
+
return `${camelToWords(k)} ${propValue === "yes" ? "enabled" : "disabled"}`;
|
|
244
|
+
}
|
|
245
|
+
return `${camelToWords(k)} ${propValue}`;
|
|
246
|
+
}).filter((c) => c && c.trim()).join(", ");
|
|
247
|
+
if (!nested) return null;
|
|
248
|
+
return `${naturalKey}: ${nested}`;
|
|
249
|
+
}
|
|
250
|
+
if (Array.isArray(value)) {
|
|
251
|
+
if (value.length === 0) return null;
|
|
252
|
+
if (["skills", "hobbies", "interests", "tags", "languages", "items"].includes(key)) {
|
|
253
|
+
const items2 = value.map((item) => {
|
|
254
|
+
const str = String(item);
|
|
255
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
256
|
+
});
|
|
257
|
+
return `Having ${joinNaturalList(items2)}`;
|
|
258
|
+
}
|
|
259
|
+
const items = joinNaturalList(value.map((item) => toNatural(item, depth + 1)));
|
|
260
|
+
return `${naturalKey}: ${items}`;
|
|
261
|
+
}
|
|
262
|
+
const naturalValue = toNatural(value, depth + 1);
|
|
263
|
+
if (naturalValue === "yes") return `${naturalKey} enabled`;
|
|
264
|
+
if (naturalValue === "no") return `${naturalKey} disabled`;
|
|
265
|
+
if (key === "theme") return `prefers the ${naturalValue} ${key}`;
|
|
266
|
+
if (key === "age") return `age: ${naturalValue}`;
|
|
267
|
+
if (key === "email") return `email: ${naturalValue}`;
|
|
268
|
+
if (key === "city") return `address: ${naturalValue}`;
|
|
269
|
+
if (key === "debug" && naturalValue === "yes") return `debug enabled`;
|
|
270
|
+
return `${naturalKey}: ${naturalValue}`;
|
|
271
|
+
}
|
|
272
|
+
function camelToWords(str) {
|
|
273
|
+
return str.replace(/([A-Z])/g, " $1").toLowerCase().trim();
|
|
274
|
+
}
|
|
275
|
+
function isPlainObject2(value) {
|
|
276
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
277
|
+
}
|
|
278
|
+
function joinNaturalList(items) {
|
|
279
|
+
if (items.length === 0) return "";
|
|
280
|
+
if (items.length === 1) return items[0];
|
|
281
|
+
if (items.length === 2) return `${items[0]} and ${items[1]}`;
|
|
282
|
+
const lastItem = items[items.length - 1];
|
|
283
|
+
return `${items.slice(0, -1).join(", ")} and ${lastItem}`;
|
|
284
|
+
}
|
|
285
|
+
function stripTrailingPeriod(value) {
|
|
286
|
+
return value.replace(/\.+$/, "");
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// src/chain.ts
|
|
290
|
+
var AIChain = class {
|
|
291
|
+
constructor(data) {
|
|
292
|
+
this.data = data;
|
|
293
|
+
}
|
|
294
|
+
prune(options) {
|
|
295
|
+
this.data = prune(this.data, options);
|
|
296
|
+
return this;
|
|
297
|
+
}
|
|
298
|
+
compact() {
|
|
299
|
+
this.data = compact(this.data);
|
|
300
|
+
return this;
|
|
301
|
+
}
|
|
302
|
+
flatten() {
|
|
303
|
+
this.data = flatten(this.data);
|
|
304
|
+
return this;
|
|
305
|
+
}
|
|
306
|
+
toTOON() {
|
|
307
|
+
this.data = toTOON(this.data);
|
|
308
|
+
return this;
|
|
309
|
+
}
|
|
310
|
+
toNatural() {
|
|
311
|
+
return toNatural(this.data);
|
|
312
|
+
}
|
|
313
|
+
analyze() {
|
|
314
|
+
return analyze(this.data);
|
|
315
|
+
}
|
|
316
|
+
value() {
|
|
317
|
+
return this.data;
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
// src/index.ts
|
|
322
|
+
function ai(data) {
|
|
323
|
+
return new AIChain(data);
|
|
324
|
+
}
|
|
325
|
+
ai.prune = prune;
|
|
326
|
+
ai.compact = compact;
|
|
327
|
+
ai.flatten = flatten;
|
|
328
|
+
ai.toTOON = toTOON;
|
|
329
|
+
ai.analyze = analyze;
|
|
330
|
+
ai.toNatural = toNatural;
|
|
331
|
+
ai.estimateTokens = estimateTokens;
|
|
332
|
+
ai.AIChain = AIChain;
|
|
333
|
+
var index_default = ai;
|
|
334
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
335
|
+
0 && (module.exports = {
|
|
336
|
+
AIChain,
|
|
337
|
+
analyze,
|
|
338
|
+
compact,
|
|
339
|
+
estimateTokens,
|
|
340
|
+
flatten,
|
|
341
|
+
prune,
|
|
342
|
+
toNatural,
|
|
343
|
+
toTOON
|
|
344
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { estimateTokens } from './core/token.cjs';
|
|
2
|
+
import { toNatural } from './core/natural.cjs';
|
|
3
|
+
import { analyze } from './core/analyze.cjs';
|
|
4
|
+
import { toTOON } from './core/toon.cjs';
|
|
5
|
+
import { flatten } from './core/flatten.cjs';
|
|
6
|
+
import { compact } from './core/compact.cjs';
|
|
7
|
+
import { prune } from './core/prune.cjs';
|
|
8
|
+
|
|
9
|
+
declare class AIChain {
|
|
10
|
+
private data;
|
|
11
|
+
constructor(data: any);
|
|
12
|
+
prune(options?: any): this;
|
|
13
|
+
compact(): this;
|
|
14
|
+
flatten(): this;
|
|
15
|
+
toTOON(): this;
|
|
16
|
+
toNatural(): string;
|
|
17
|
+
analyze(): {
|
|
18
|
+
originalTokens: number;
|
|
19
|
+
optimizedTokens: number;
|
|
20
|
+
savings: number;
|
|
21
|
+
savingsPercent: number;
|
|
22
|
+
optimizedData: any;
|
|
23
|
+
reductionRatio: number;
|
|
24
|
+
};
|
|
25
|
+
value(): any;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare function ai(data: any): AIChain;
|
|
29
|
+
declare namespace ai {
|
|
30
|
+
var prune: typeof prune;
|
|
31
|
+
var compact: typeof compact;
|
|
32
|
+
var flatten: typeof flatten;
|
|
33
|
+
var toTOON: typeof toTOON;
|
|
34
|
+
var analyze: typeof analyze;
|
|
35
|
+
var toNatural: typeof toNatural;
|
|
36
|
+
var estimateTokens: typeof estimateTokens;
|
|
37
|
+
var AIChain: typeof AIChain;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { AIChain, analyze, compact, ai as default, estimateTokens, flatten, prune, toNatural, toTOON };
|