@sohqureshi/tokenwise 1.0.0 → 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/LICENSE +20 -20
- package/README.md +9 -7
- package/dist/cli.cjs +0 -0
- package/dist/cli.js +0 -0
- package/package.json +33 -29
- package/dist/chunk-33GBS5YN.js +0 -288
- package/dist/chunk-33RRCCAT.js +0 -287
- package/dist/chunk-HKCPRPJL.js +0 -294
- package/dist/chunk-JBKETSTK.js +0 -270
- package/dist/chunk-SURFMVNH.js +0 -285
package/dist/chunk-JBKETSTK.js
DELETED
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
// src/core/prune.ts
|
|
2
|
-
function prune(obj, options = {}) {
|
|
3
|
-
const {
|
|
4
|
-
removeKeys = [],
|
|
5
|
-
removeNull = true,
|
|
6
|
-
removeUndefined = true,
|
|
7
|
-
removeEmptyObjects = true,
|
|
8
|
-
removeEmptyArrays = false
|
|
9
|
-
} = options;
|
|
10
|
-
if (obj === null) return removeNull ? void 0 : obj;
|
|
11
|
-
if (obj === void 0) return removeUndefined ? void 0 : obj;
|
|
12
|
-
if (typeof obj !== "object") return obj;
|
|
13
|
-
if (Array.isArray(obj)) {
|
|
14
|
-
const arr = obj.map((item) => prune(item, options)).filter((item) => item !== void 0);
|
|
15
|
-
if (removeEmptyArrays && arr.length === 0) {
|
|
16
|
-
return void 0;
|
|
17
|
-
}
|
|
18
|
-
return arr;
|
|
19
|
-
}
|
|
20
|
-
const result = {};
|
|
21
|
-
for (const key in obj) {
|
|
22
|
-
if (removeKeys.includes(key)) continue;
|
|
23
|
-
const value = prune(obj[key], options);
|
|
24
|
-
if (value === void 0) continue;
|
|
25
|
-
if (removeEmptyObjects && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0) {
|
|
26
|
-
continue;
|
|
27
|
-
}
|
|
28
|
-
result[key] = value;
|
|
29
|
-
}
|
|
30
|
-
return result;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// src/core/flatten.ts
|
|
34
|
-
function flatten(obj, prefix = "", res = {}) {
|
|
35
|
-
if (obj === null || obj === void 0) return res;
|
|
36
|
-
if (typeof obj !== "object") {
|
|
37
|
-
res[prefix] = obj;
|
|
38
|
-
return res;
|
|
39
|
-
}
|
|
40
|
-
for (const key in obj) {
|
|
41
|
-
const value = obj[key];
|
|
42
|
-
const newKey = prefix ? `${prefix}.${key}` : key;
|
|
43
|
-
if (typeof value === "object" && value !== null) {
|
|
44
|
-
flatten(value, newKey, res);
|
|
45
|
-
} else {
|
|
46
|
-
res[newKey] = value;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return res;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// src/core/compact.ts
|
|
53
|
-
function compact(obj) {
|
|
54
|
-
const pruned = prune(obj);
|
|
55
|
-
const flat = flatten(pruned);
|
|
56
|
-
return Object.entries(flat).map(([k, v]) => `${k}=${v}`).join("|");
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// src/core/toon.ts
|
|
60
|
-
function toTOON(data, indent = 0) {
|
|
61
|
-
const space = " ".repeat(indent);
|
|
62
|
-
if (Array.isArray(data)) {
|
|
63
|
-
if (data.length === 0) return "[]";
|
|
64
|
-
if (typeof data[0] === "object") {
|
|
65
|
-
const keys = Object.keys(data[0]);
|
|
66
|
-
let result = `${space}[${data.length}]{${keys.join(",")}}:
|
|
67
|
-
`;
|
|
68
|
-
for (const item of data) {
|
|
69
|
-
result += space + " " + keys.map((k) => formatValue(item[k])).join(",") + "\n";
|
|
70
|
-
}
|
|
71
|
-
return result;
|
|
72
|
-
}
|
|
73
|
-
return `${space}[${data.length}]: ${data.join(",")}`;
|
|
74
|
-
}
|
|
75
|
-
if (typeof data === "object" && data !== null) {
|
|
76
|
-
let result = "";
|
|
77
|
-
for (const key in data) {
|
|
78
|
-
const value = data[key];
|
|
79
|
-
if (typeof value === "object") {
|
|
80
|
-
result += `${space}${key}:
|
|
81
|
-
${toTOON(value, indent + 1)}
|
|
82
|
-
`;
|
|
83
|
-
} else {
|
|
84
|
-
result += `${space}${key}: ${formatValue(value)}
|
|
85
|
-
`;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return result.trim();
|
|
89
|
-
}
|
|
90
|
-
return formatValue(data);
|
|
91
|
-
}
|
|
92
|
-
function formatValue(val) {
|
|
93
|
-
if (val === null || val === void 0) return "";
|
|
94
|
-
if (typeof val === "string") return val;
|
|
95
|
-
return String(val);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// src/core/token.ts
|
|
99
|
-
function estimateTokens(text) {
|
|
100
|
-
if (!text) return 0;
|
|
101
|
-
return Math.ceil(text.length / 4);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// src/core/analyze.ts
|
|
105
|
-
function analyze(input, options = {}) {
|
|
106
|
-
if (!input || typeof input === "object" && input !== null && Object.keys(input).length === 0) {
|
|
107
|
-
return {
|
|
108
|
-
originalTokens: 0,
|
|
109
|
-
optimizedTokens: 0,
|
|
110
|
-
savings: 0,
|
|
111
|
-
savingsPercent: 0,
|
|
112
|
-
optimizedData: null,
|
|
113
|
-
reductionRatio: 1
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
let originalTokens = estimateTokens(input);
|
|
117
|
-
if (isNaN(originalTokens) || !isFinite(originalTokens)) originalTokens = 0;
|
|
118
|
-
let optimizedData = input;
|
|
119
|
-
if (options.prune && Array.isArray(options.prune)) {
|
|
120
|
-
optimizedData = prune(optimizedData);
|
|
121
|
-
}
|
|
122
|
-
if (options.compact) {
|
|
123
|
-
optimizedData = compact(optimizedData);
|
|
124
|
-
}
|
|
125
|
-
if (options.flatten) {
|
|
126
|
-
optimizedData = flatten(optimizedData);
|
|
127
|
-
}
|
|
128
|
-
if (options.toTOON === true || options.toon === true) {
|
|
129
|
-
optimizedData = toTOON(optimizedData);
|
|
130
|
-
}
|
|
131
|
-
let optimizedTokens = estimateTokens(optimizedData);
|
|
132
|
-
if (isNaN(optimizedTokens) || !isFinite(optimizedTokens)) optimizedTokens = 0;
|
|
133
|
-
const savings = Math.max(0, originalTokens - optimizedTokens);
|
|
134
|
-
const savingsPercent = originalTokens > 0 ? Math.round(savings / originalTokens * 100) : 0;
|
|
135
|
-
const reductionRatio = originalTokens > 0 ? optimizedTokens / originalTokens : 1;
|
|
136
|
-
return {
|
|
137
|
-
originalTokens,
|
|
138
|
-
optimizedTokens,
|
|
139
|
-
savings,
|
|
140
|
-
savingsPercent,
|
|
141
|
-
optimizedData,
|
|
142
|
-
reductionRatio
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// src/core/natural.ts
|
|
147
|
-
function toNatural(data, depth = 0) {
|
|
148
|
-
if (data === null || data === void 0) return "nothing";
|
|
149
|
-
if (typeof data === "string") return data;
|
|
150
|
-
if (typeof data === "number") return String(data);
|
|
151
|
-
if (typeof data === "boolean") return data ? "yes" : "no";
|
|
152
|
-
if (Array.isArray(data)) {
|
|
153
|
-
if (data.length === 0) return "empty list";
|
|
154
|
-
const items = data.map((item) => toNatural(item, depth + 1));
|
|
155
|
-
if (items.length === 1) return items[0];
|
|
156
|
-
const lastItem = items.pop();
|
|
157
|
-
return items.join(", ") + " and " + lastItem;
|
|
158
|
-
}
|
|
159
|
-
if (typeof data === "object") {
|
|
160
|
-
return buildContextualStory(data, depth);
|
|
161
|
-
}
|
|
162
|
-
return String(data);
|
|
163
|
-
}
|
|
164
|
-
function buildContextualStory(obj, depth = 0) {
|
|
165
|
-
let name = obj.name || obj.userName || obj.user;
|
|
166
|
-
if (typeof name === "object" && name !== null && name.name) {
|
|
167
|
-
name = name.name;
|
|
168
|
-
}
|
|
169
|
-
const entries = Object.entries(obj).filter(([key]) => {
|
|
170
|
-
return !["id", "timestamp", "apiKey"].includes(key);
|
|
171
|
-
});
|
|
172
|
-
if (entries.length === 0) return "";
|
|
173
|
-
let story = name && typeof name === "string" ? `User ${name}` : "";
|
|
174
|
-
const clauses = entries.map(([key, value]) => {
|
|
175
|
-
if (key === "user" && name && typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
176
|
-
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(", ");
|
|
177
|
-
return userProps ? `(${userProps})` : null;
|
|
178
|
-
}
|
|
179
|
-
return formatPropertyClause(key, value, depth);
|
|
180
|
-
}).filter((c) => c !== null && c !== "");
|
|
181
|
-
if (clauses.length === 0) return story;
|
|
182
|
-
if (story) {
|
|
183
|
-
story += " " + clauses.join(", ");
|
|
184
|
-
} else {
|
|
185
|
-
story = clauses.join(", ");
|
|
186
|
-
}
|
|
187
|
-
return story + ".";
|
|
188
|
-
}
|
|
189
|
-
function formatPropertyClause(key, value, depth) {
|
|
190
|
-
const naturalKey = camelToWords(key);
|
|
191
|
-
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
192
|
-
if (["internal", "metadata", "debug"].includes(key)) {
|
|
193
|
-
return null;
|
|
194
|
-
}
|
|
195
|
-
const nested = Object.entries(value).filter(([k]) => !["id", "timestamp", "apiKey", "createdAt", "updatedAt"].includes(k)).map(([k, v]) => {
|
|
196
|
-
const propValue = toNatural(v, depth + 1);
|
|
197
|
-
if (propValue === "yes" || propValue === "no") {
|
|
198
|
-
return `${camelToWords(k)} ${propValue === "yes" ? "enabled" : "disabled"}`;
|
|
199
|
-
}
|
|
200
|
-
return `${camelToWords(k)} ${propValue}`;
|
|
201
|
-
}).filter((c) => c && c.trim()).join(", ");
|
|
202
|
-
if (!nested) return null;
|
|
203
|
-
return `${naturalKey}: ${nested}`;
|
|
204
|
-
}
|
|
205
|
-
if (Array.isArray(value)) {
|
|
206
|
-
if (value.length === 0) return null;
|
|
207
|
-
if (["skills", "hobbies", "interests", "tags", "languages", "items"].includes(key)) {
|
|
208
|
-
const items2 = value.map((item) => {
|
|
209
|
-
const str = String(item);
|
|
210
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
211
|
-
}).join(", ");
|
|
212
|
-
return `Having ${items2}`;
|
|
213
|
-
}
|
|
214
|
-
const items = value.map((item) => toNatural(item, depth + 1)).join(", ");
|
|
215
|
-
return `${naturalKey}: ${items}`;
|
|
216
|
-
}
|
|
217
|
-
const naturalValue = toNatural(value, depth + 1);
|
|
218
|
-
if (naturalValue === "yes") return `${naturalKey} enabled`;
|
|
219
|
-
if (naturalValue === "no") return `${naturalKey} disabled`;
|
|
220
|
-
if (key === "theme") return `prefers the ${naturalValue} ${key}`;
|
|
221
|
-
if (key === "age") return `age: ${naturalValue}`;
|
|
222
|
-
if (key === "email") return `email: ${naturalValue}`;
|
|
223
|
-
if (key === "city") return `address: ${naturalValue}`;
|
|
224
|
-
if (key === "debug" && naturalValue === "yes") return `debug enabled`;
|
|
225
|
-
return `${naturalKey}: ${naturalValue}`;
|
|
226
|
-
}
|
|
227
|
-
function camelToWords(str) {
|
|
228
|
-
return str.replace(/([A-Z])/g, " $1").toLowerCase().trim();
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// src/chain.ts
|
|
232
|
-
var AIChain = class {
|
|
233
|
-
constructor(data) {
|
|
234
|
-
this.data = data;
|
|
235
|
-
}
|
|
236
|
-
prune(options) {
|
|
237
|
-
this.data = prune(this.data, options);
|
|
238
|
-
return this;
|
|
239
|
-
}
|
|
240
|
-
compact() {
|
|
241
|
-
this.data = compact(this.data);
|
|
242
|
-
return this;
|
|
243
|
-
}
|
|
244
|
-
flatten() {
|
|
245
|
-
this.data = flatten(this.data);
|
|
246
|
-
return this;
|
|
247
|
-
}
|
|
248
|
-
toTOON() {
|
|
249
|
-
this.data = toTOON(this.data);
|
|
250
|
-
return this;
|
|
251
|
-
}
|
|
252
|
-
toNatural() {
|
|
253
|
-
return toNatural(this.data);
|
|
254
|
-
}
|
|
255
|
-
analyze() {
|
|
256
|
-
return analyze(this.data);
|
|
257
|
-
}
|
|
258
|
-
value() {
|
|
259
|
-
return this.data;
|
|
260
|
-
}
|
|
261
|
-
};
|
|
262
|
-
|
|
263
|
-
export {
|
|
264
|
-
prune,
|
|
265
|
-
compact,
|
|
266
|
-
toTOON,
|
|
267
|
-
analyze,
|
|
268
|
-
toNatural,
|
|
269
|
-
AIChain
|
|
270
|
-
};
|
package/dist/chunk-SURFMVNH.js
DELETED
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
// src/core/prune.ts
|
|
2
|
-
function prune(obj, options = {}) {
|
|
3
|
-
const normalizedOptions = Array.isArray(options) ? { removeKeys: options } : options;
|
|
4
|
-
const {
|
|
5
|
-
removeKeys = [],
|
|
6
|
-
removeNull = true,
|
|
7
|
-
removeUndefined = true,
|
|
8
|
-
removeEmptyObjects = true,
|
|
9
|
-
removeEmptyArrays = false
|
|
10
|
-
} = normalizedOptions;
|
|
11
|
-
if (obj === null) return removeNull ? void 0 : obj;
|
|
12
|
-
if (obj === void 0) return removeUndefined ? void 0 : obj;
|
|
13
|
-
if (typeof obj !== "object") return obj;
|
|
14
|
-
if (Array.isArray(obj)) {
|
|
15
|
-
const arr = obj.map((item) => prune(item, normalizedOptions)).filter((item) => item !== void 0);
|
|
16
|
-
if (removeEmptyArrays && arr.length === 0) {
|
|
17
|
-
return void 0;
|
|
18
|
-
}
|
|
19
|
-
return arr;
|
|
20
|
-
}
|
|
21
|
-
const result = {};
|
|
22
|
-
for (const key in obj) {
|
|
23
|
-
if (removeKeys.includes(key)) continue;
|
|
24
|
-
const value = prune(obj[key], normalizedOptions);
|
|
25
|
-
if (value === void 0) continue;
|
|
26
|
-
if (removeEmptyObjects && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0) {
|
|
27
|
-
continue;
|
|
28
|
-
}
|
|
29
|
-
result[key] = value;
|
|
30
|
-
}
|
|
31
|
-
return result;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// src/core/flatten.ts
|
|
35
|
-
function flatten(obj, prefix = "", res = {}) {
|
|
36
|
-
if (obj === null || obj === void 0) return res;
|
|
37
|
-
if (typeof obj !== "object") {
|
|
38
|
-
res[prefix] = obj;
|
|
39
|
-
return res;
|
|
40
|
-
}
|
|
41
|
-
for (const key in obj) {
|
|
42
|
-
const value = obj[key];
|
|
43
|
-
const newKey = prefix ? `${prefix}.${key}` : key;
|
|
44
|
-
if (typeof value === "object" && value !== null) {
|
|
45
|
-
flatten(value, newKey, res);
|
|
46
|
-
} else {
|
|
47
|
-
res[newKey] = value;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return res;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// src/core/compact.ts
|
|
54
|
-
function compact(obj) {
|
|
55
|
-
const pruned = prune(obj);
|
|
56
|
-
const flat = flatten(pruned);
|
|
57
|
-
return Object.entries(flat).map(([k, v]) => `${k}=${v}`).join("|");
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// src/core/toon.ts
|
|
61
|
-
function toTOON(data, indent = 0) {
|
|
62
|
-
const space = " ".repeat(indent);
|
|
63
|
-
if (Array.isArray(data)) {
|
|
64
|
-
if (data.length === 0) return "[]";
|
|
65
|
-
if (typeof data[0] === "object") {
|
|
66
|
-
const keys = Object.keys(data[0]);
|
|
67
|
-
let result = `${space}[${data.length}]{${keys.join(",")}}:
|
|
68
|
-
`;
|
|
69
|
-
for (const item of data) {
|
|
70
|
-
result += space + " " + keys.map((k) => formatValue(item[k])).join(",") + "\n";
|
|
71
|
-
}
|
|
72
|
-
return result;
|
|
73
|
-
}
|
|
74
|
-
return `${space}[${data.length}]: ${data.join(",")}`;
|
|
75
|
-
}
|
|
76
|
-
if (typeof data === "object" && data !== null) {
|
|
77
|
-
let result = "";
|
|
78
|
-
for (const key in data) {
|
|
79
|
-
const value = data[key];
|
|
80
|
-
if (typeof value === "object") {
|
|
81
|
-
result += `${space}${key}:
|
|
82
|
-
${toTOON(value, indent + 1)}
|
|
83
|
-
`;
|
|
84
|
-
} else {
|
|
85
|
-
result += `${space}${key}: ${formatValue(value)}
|
|
86
|
-
`;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return result.trim();
|
|
90
|
-
}
|
|
91
|
-
return formatValue(data);
|
|
92
|
-
}
|
|
93
|
-
function formatValue(val) {
|
|
94
|
-
if (val === null || val === void 0) return "";
|
|
95
|
-
if (typeof val === "string") return val;
|
|
96
|
-
return String(val);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// src/core/token.ts
|
|
100
|
-
function estimateTokens(text) {
|
|
101
|
-
if (!text) return 0;
|
|
102
|
-
return Math.ceil(text.length / 4);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// src/core/analyze.ts
|
|
106
|
-
function analyze(input, options = {}) {
|
|
107
|
-
if (!input || typeof input === "object" && input !== null && Object.keys(input).length === 0) {
|
|
108
|
-
return {
|
|
109
|
-
originalTokens: 0,
|
|
110
|
-
optimizedTokens: 0,
|
|
111
|
-
savings: 0,
|
|
112
|
-
savingsPercent: 0,
|
|
113
|
-
optimizedData: null,
|
|
114
|
-
reductionRatio: 1
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
let originalTokens = estimateTokens(input);
|
|
118
|
-
if (isNaN(originalTokens) || !isFinite(originalTokens)) originalTokens = 0;
|
|
119
|
-
let optimizedData = input;
|
|
120
|
-
if (options.prune && Array.isArray(options.prune)) {
|
|
121
|
-
optimizedData = prune(optimizedData, options.prune);
|
|
122
|
-
}
|
|
123
|
-
if (options.compact) {
|
|
124
|
-
optimizedData = compact(optimizedData);
|
|
125
|
-
}
|
|
126
|
-
if (options.flatten) {
|
|
127
|
-
optimizedData = flatten(optimizedData);
|
|
128
|
-
}
|
|
129
|
-
if (options.toTOON === true || options.toon === true) {
|
|
130
|
-
optimizedData = toTOON(optimizedData);
|
|
131
|
-
}
|
|
132
|
-
let optimizedTokens = estimateTokens(optimizedData);
|
|
133
|
-
if (isNaN(optimizedTokens) || !isFinite(optimizedTokens)) optimizedTokens = 0;
|
|
134
|
-
const savings = Math.max(0, originalTokens - optimizedTokens);
|
|
135
|
-
const savingsPercent = originalTokens > 0 ? Math.round(savings / originalTokens * 100) : 0;
|
|
136
|
-
const reductionRatio = originalTokens > 0 ? optimizedTokens / originalTokens : 1;
|
|
137
|
-
return {
|
|
138
|
-
originalTokens,
|
|
139
|
-
optimizedTokens,
|
|
140
|
-
savings,
|
|
141
|
-
savingsPercent,
|
|
142
|
-
optimizedData,
|
|
143
|
-
reductionRatio
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// src/core/natural.ts
|
|
148
|
-
function toNatural(data, depth = 0) {
|
|
149
|
-
if (data === null || data === void 0) return "nothing";
|
|
150
|
-
if (typeof data === "string") return data;
|
|
151
|
-
if (typeof data === "number") return String(data);
|
|
152
|
-
if (typeof data === "boolean") return data ? "yes" : "no";
|
|
153
|
-
if (Array.isArray(data)) {
|
|
154
|
-
if (data.length === 0) return "empty list";
|
|
155
|
-
if (data.every(isPlainObject)) {
|
|
156
|
-
return data.map((item, index) => `${index + 1}. ${stripTrailingPeriod(toNatural(item, depth + 1))}.`).join(" ");
|
|
157
|
-
}
|
|
158
|
-
const items = data.map((item) => toNatural(item, depth + 1));
|
|
159
|
-
return joinNaturalList(items);
|
|
160
|
-
}
|
|
161
|
-
if (typeof data === "object") {
|
|
162
|
-
return buildContextualStory(data, depth);
|
|
163
|
-
}
|
|
164
|
-
return String(data);
|
|
165
|
-
}
|
|
166
|
-
function buildContextualStory(obj, depth = 0) {
|
|
167
|
-
let name = obj.name || obj.userName || obj.user;
|
|
168
|
-
if (typeof name === "object" && name !== null && name.name) {
|
|
169
|
-
name = name.name;
|
|
170
|
-
}
|
|
171
|
-
const entries = Object.entries(obj).filter(([key]) => {
|
|
172
|
-
return !["id", "timestamp", "apiKey"].includes(key);
|
|
173
|
-
});
|
|
174
|
-
if (entries.length === 0) return "";
|
|
175
|
-
let story = name && typeof name === "string" ? `User ${name}` : "";
|
|
176
|
-
const clauses = entries.map(([key, value]) => {
|
|
177
|
-
if (key === "user" && name && typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
178
|
-
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(", ");
|
|
179
|
-
return userProps ? `(${userProps})` : null;
|
|
180
|
-
}
|
|
181
|
-
return formatPropertyClause(key, value, depth);
|
|
182
|
-
}).filter((c) => c !== null && c !== "");
|
|
183
|
-
if (clauses.length === 0) return story;
|
|
184
|
-
if (story) {
|
|
185
|
-
story += " " + clauses.join(", ");
|
|
186
|
-
} else {
|
|
187
|
-
story = clauses.join(", ");
|
|
188
|
-
}
|
|
189
|
-
return story + ".";
|
|
190
|
-
}
|
|
191
|
-
function formatPropertyClause(key, value, depth) {
|
|
192
|
-
const naturalKey = camelToWords(key);
|
|
193
|
-
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
194
|
-
if (["internal", "metadata", "debug"].includes(key)) {
|
|
195
|
-
return null;
|
|
196
|
-
}
|
|
197
|
-
const nested = Object.entries(value).filter(([k]) => !["id", "timestamp", "apiKey", "createdAt", "updatedAt"].includes(k)).map(([k, v]) => {
|
|
198
|
-
const propValue = toNatural(v, depth + 1);
|
|
199
|
-
if (propValue === "yes" || propValue === "no") {
|
|
200
|
-
return `${camelToWords(k)} ${propValue === "yes" ? "enabled" : "disabled"}`;
|
|
201
|
-
}
|
|
202
|
-
return `${camelToWords(k)} ${propValue}`;
|
|
203
|
-
}).filter((c) => c && c.trim()).join(", ");
|
|
204
|
-
if (!nested) return null;
|
|
205
|
-
return `${naturalKey}: ${nested}`;
|
|
206
|
-
}
|
|
207
|
-
if (Array.isArray(value)) {
|
|
208
|
-
if (value.length === 0) return null;
|
|
209
|
-
if (["skills", "hobbies", "interests", "tags", "languages", "items"].includes(key)) {
|
|
210
|
-
const items2 = value.map((item) => {
|
|
211
|
-
const str = String(item);
|
|
212
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
213
|
-
});
|
|
214
|
-
return `Having ${joinNaturalList(items2)}`;
|
|
215
|
-
}
|
|
216
|
-
const items = joinNaturalList(value.map((item) => toNatural(item, depth + 1)));
|
|
217
|
-
return `${naturalKey}: ${items}`;
|
|
218
|
-
}
|
|
219
|
-
const naturalValue = toNatural(value, depth + 1);
|
|
220
|
-
if (naturalValue === "yes") return `${naturalKey} enabled`;
|
|
221
|
-
if (naturalValue === "no") return `${naturalKey} disabled`;
|
|
222
|
-
if (key === "theme") return `prefers the ${naturalValue} ${key}`;
|
|
223
|
-
if (key === "age") return `age: ${naturalValue}`;
|
|
224
|
-
if (key === "email") return `email: ${naturalValue}`;
|
|
225
|
-
if (key === "city") return `address: ${naturalValue}`;
|
|
226
|
-
if (key === "debug" && naturalValue === "yes") return `debug enabled`;
|
|
227
|
-
return `${naturalKey}: ${naturalValue}`;
|
|
228
|
-
}
|
|
229
|
-
function camelToWords(str) {
|
|
230
|
-
return str.replace(/([A-Z])/g, " $1").toLowerCase().trim();
|
|
231
|
-
}
|
|
232
|
-
function isPlainObject(value) {
|
|
233
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
234
|
-
}
|
|
235
|
-
function joinNaturalList(items) {
|
|
236
|
-
if (items.length === 0) return "";
|
|
237
|
-
if (items.length === 1) return items[0];
|
|
238
|
-
if (items.length === 2) return `${items[0]} and ${items[1]}`;
|
|
239
|
-
const lastItem = items[items.length - 1];
|
|
240
|
-
return `${items.slice(0, -1).join(", ")} and ${lastItem}`;
|
|
241
|
-
}
|
|
242
|
-
function stripTrailingPeriod(value) {
|
|
243
|
-
return value.replace(/\.+$/, "");
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// src/chain.ts
|
|
247
|
-
var AIChain = class {
|
|
248
|
-
constructor(data) {
|
|
249
|
-
this.data = data;
|
|
250
|
-
}
|
|
251
|
-
prune(options) {
|
|
252
|
-
this.data = prune(this.data, options);
|
|
253
|
-
return this;
|
|
254
|
-
}
|
|
255
|
-
compact() {
|
|
256
|
-
this.data = compact(this.data);
|
|
257
|
-
return this;
|
|
258
|
-
}
|
|
259
|
-
flatten() {
|
|
260
|
-
this.data = flatten(this.data);
|
|
261
|
-
return this;
|
|
262
|
-
}
|
|
263
|
-
toTOON() {
|
|
264
|
-
this.data = toTOON(this.data);
|
|
265
|
-
return this;
|
|
266
|
-
}
|
|
267
|
-
toNatural() {
|
|
268
|
-
return toNatural(this.data);
|
|
269
|
-
}
|
|
270
|
-
analyze() {
|
|
271
|
-
return analyze(this.data);
|
|
272
|
-
}
|
|
273
|
-
value() {
|
|
274
|
-
return this.data;
|
|
275
|
-
}
|
|
276
|
-
};
|
|
277
|
-
|
|
278
|
-
export {
|
|
279
|
-
prune,
|
|
280
|
-
compact,
|
|
281
|
-
toTOON,
|
|
282
|
-
analyze,
|
|
283
|
-
toNatural,
|
|
284
|
-
AIChain
|
|
285
|
-
};
|