@sohqureshi/tokenwise 1.0.7 → 1.0.10

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.
@@ -1,105 +0,0 @@
1
- // src/core/natural.ts
2
- function toNatural(data, depth = 0) {
3
- if (data === null || data === void 0) return "nothing";
4
- if (typeof data === "string") return data;
5
- if (typeof data === "number") return String(data);
6
- if (typeof data === "boolean") return data ? "yes" : "no";
7
- if (Array.isArray(data)) {
8
- if (data.length === 0) return "empty list";
9
- if (data.every(isPlainObject)) {
10
- return data.map((item, index) => `${index + 1}. ${stripTrailingPeriod(toNatural(item, depth + 1))}.`).join(" ");
11
- }
12
- const items = data.map((item) => toNatural(item, depth + 1));
13
- return joinNaturalList(items);
14
- }
15
- if (typeof data === "object") {
16
- return buildContextualStory(data, depth);
17
- }
18
- return String(data);
19
- }
20
- function buildContextualStory(obj, depth = 0) {
21
- let name = obj.name || obj.userName || obj.user;
22
- if (typeof name === "object" && name !== null && name.name) {
23
- name = name.name;
24
- }
25
- const entries = Object.entries(obj).filter(([key]) => {
26
- return !["id", "timestamp", "apiKey"].includes(key);
27
- });
28
- if (entries.length === 0) return "";
29
- let story = name && typeof name === "string" ? `User ${name}` : "";
30
- const clauses = entries.map(([key, value]) => {
31
- if (key === "user" && name && typeof value === "object" && value !== null && !Array.isArray(value)) {
32
- 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(", ");
33
- return userProps ? `(${userProps})` : null;
34
- }
35
- return formatPropertyClause(key, value, depth);
36
- }).filter((c) => c !== null && c !== "");
37
- if (clauses.length === 0) return story;
38
- if (story) {
39
- story += " " + clauses.join(", ");
40
- } else {
41
- story = clauses.join(", ");
42
- }
43
- return story + ".";
44
- }
45
- function formatPropertyClause(key, value, depth) {
46
- const naturalKey = camelToWords(key);
47
- if (typeof value === "object" && value !== null && !Array.isArray(value)) {
48
- if (["internal", "metadata", "debug"].includes(key)) {
49
- return null;
50
- }
51
- const nested = Object.entries(value).filter(([k]) => !["id", "timestamp", "apiKey", "createdAt", "updatedAt"].includes(k)).map(([k, v]) => {
52
- if (typeof v === "object" && v !== null) {
53
- return formatPropertyClause(k, v, depth + 1);
54
- }
55
- const propValue = toNatural(v, depth + 1);
56
- if (propValue === "yes" || propValue === "no") {
57
- return `${camelToWords(k)} ${propValue === "yes" ? "enabled" : "disabled"}`;
58
- }
59
- return `${camelToWords(k)} ${propValue}`;
60
- }).filter((c) => c && c.trim()).join(", ");
61
- if (!nested) return null;
62
- return `${naturalKey}: ${nested}`;
63
- }
64
- if (Array.isArray(value)) {
65
- if (value.length === 0) return null;
66
- if (["skills", "hobbies", "interests", "tags", "languages", "items"].includes(key)) {
67
- const items2 = value.map((item) => {
68
- const str = String(item);
69
- return str.charAt(0).toUpperCase() + str.slice(1);
70
- });
71
- return `Having ${joinNaturalList(items2)}`;
72
- }
73
- const items = joinNaturalList(value.map((item) => toNatural(item, depth + 1)));
74
- return `${naturalKey}: ${items}`;
75
- }
76
- const naturalValue = toNatural(value, depth + 1);
77
- if (naturalValue === "yes") return `${naturalKey} enabled`;
78
- if (naturalValue === "no") return `${naturalKey} disabled`;
79
- if (key === "theme") return `prefers the ${naturalValue} ${key}`;
80
- if (key === "age") return `age: ${naturalValue}`;
81
- if (key === "email") return `email: ${naturalValue}`;
82
- if (key === "city") return `address: ${naturalValue}`;
83
- if (key === "debug" && naturalValue === "yes") return `debug enabled`;
84
- return `${naturalKey}: ${naturalValue}`;
85
- }
86
- function camelToWords(str) {
87
- return str.replace(/([A-Z])/g, " $1").toLowerCase().trim();
88
- }
89
- function isPlainObject(value) {
90
- return typeof value === "object" && value !== null && !Array.isArray(value);
91
- }
92
- function joinNaturalList(items) {
93
- if (items.length === 0) return "";
94
- if (items.length === 1) return items[0];
95
- if (items.length === 2) return `${items[0]} and ${items[1]}`;
96
- const lastItem = items[items.length - 1];
97
- return `${items.slice(0, -1).join(", ")} and ${lastItem}`;
98
- }
99
- function stripTrailingPeriod(value) {
100
- return value.replace(/\.+$/, "");
101
- }
102
-
103
- export {
104
- toNatural
105
- };
@@ -1,55 +0,0 @@
1
- // src/core/token.ts
2
- import { createRequire } from "module";
3
- var require2 = createRequire(import.meta.url);
4
- function serializeForTokenEstimate(value) {
5
- if (typeof value === "string") return value;
6
- const serialized = JSON.stringify(value);
7
- return serialized ?? "";
8
- }
9
- function estimateTokensHeuristic(value) {
10
- return Math.ceil(serializeForTokenEstimate(value).length / 4);
11
- }
12
- function estimateTokens(value, options = {}) {
13
- return estimateTokensWithMeta(value, options).count;
14
- }
15
- function estimateTokensWithMeta(value, options = {}) {
16
- const { exact = false, model = "gpt-4o-mini", fallbackToHeuristic = true } = options;
17
- const heuristic = estimateTokensHeuristic(value);
18
- const heuristicEstimator = "heuristic: 1 token \u2248 4 characters";
19
- if (!exact) {
20
- return { count: heuristic, estimator: heuristicEstimator };
21
- }
22
- try {
23
- const tiktoken = require2("tiktoken");
24
- const encoder = tiktoken.encoding_for_model(model);
25
- const text = serializeForTokenEstimate(value);
26
- const count = encoder.encode(text).length;
27
- let encodingName = null;
28
- if (encoder.name) encodingName = encoder.name;
29
- if (!encodingName && typeof tiktoken.model_to_encoding === "function") {
30
- try {
31
- encodingName = tiktoken.model_to_encoding(model);
32
- } catch (e) {
33
- encodingName = null;
34
- }
35
- }
36
- if (!encodingName) {
37
- const m = String(model || "").toLowerCase();
38
- if (m.includes("davinci") || m.startsWith("text-")) encodingName = "r50k_base";
39
- else encodingName = "cl100k_base";
40
- }
41
- const estimator = `exact tokenizer: model=${model} encoding=${encodingName}`;
42
- return { count, estimator };
43
- } catch (e) {
44
- if (!fallbackToHeuristic) {
45
- return { count: heuristic, estimator: "exact requested but tokenizer unavailable" };
46
- }
47
- return { count: heuristic, estimator: heuristicEstimator };
48
- }
49
- }
50
-
51
- export {
52
- serializeForTokenEstimate,
53
- estimateTokens,
54
- estimateTokensWithMeta
55
- };