@kairyou/agent-tools 0.4.0 → 0.5.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/README.md +4 -3
- package/README.zh-CN.md +3 -3
- package/dist/statusline/claude-statusline.mjs +866 -61
- package/dist/usage/core.mjs +1199 -377
- package/integrations/statusline/claude-statusline.mjs +6 -65
- package/integrations/usage/core.mjs +13 -1103
- package/integrations/usage/lib/cache.mjs +110 -0
- package/integrations/usage/lib/config.mjs +99 -0
- package/integrations/usage/lib/context.mjs +138 -0
- package/integrations/usage/lib/format.mjs +265 -0
- package/integrations/usage/lib/http.mjs +186 -0
- package/integrations/usage/lib/routes.mjs +265 -0
- package/integrations/usage/lib/urls.mjs +48 -0
- package/package.json +1 -1
|
@@ -14,6 +14,7 @@ import { execFileSync, spawn } from "node:child_process";
|
|
|
14
14
|
import fs from "node:fs";
|
|
15
15
|
import { basename, dirname, join } from "node:path";
|
|
16
16
|
import { fileURLToPath } from "node:url";
|
|
17
|
+
import { parse as parseJsonc } from "jsonc-parser";
|
|
17
18
|
|
|
18
19
|
const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url));
|
|
19
20
|
const AGENT_TOOLS_HOME = process.env.AGENT_TOOLS_HOME || join(SCRIPT_DIR, "..", "..");
|
|
@@ -62,76 +63,16 @@ async function readStdin() {
|
|
|
62
63
|
function readJsonFile(file) {
|
|
63
64
|
try {
|
|
64
65
|
if (!fs.existsSync(file)) return {};
|
|
65
|
-
const raw =
|
|
66
|
-
|
|
66
|
+
const raw = fs.readFileSync(file, "utf8").replace(/^\uFEFF/, "");
|
|
67
|
+
if (!raw.trim()) return {};
|
|
68
|
+
const errors = [];
|
|
69
|
+
const parsed = parseJsonc(raw, errors, { allowTrailingComma: true });
|
|
70
|
+
return errors.length === 0 && parsed && typeof parsed === "object" ? parsed : {};
|
|
67
71
|
} catch {
|
|
68
72
|
return {};
|
|
69
73
|
}
|
|
70
74
|
}
|
|
71
75
|
|
|
72
|
-
function stripJsonComments(input) {
|
|
73
|
-
let out = "";
|
|
74
|
-
let inString = false;
|
|
75
|
-
let escaped = false;
|
|
76
|
-
for (let i = 0; i < input.length; i++) {
|
|
77
|
-
const ch = input[i];
|
|
78
|
-
const next = input[i + 1];
|
|
79
|
-
if (inString) {
|
|
80
|
-
out += ch;
|
|
81
|
-
escaped = ch === "\\" ? !escaped : false;
|
|
82
|
-
if (ch === "\"" && !escaped) inString = false;
|
|
83
|
-
continue;
|
|
84
|
-
}
|
|
85
|
-
if (ch === "\"") {
|
|
86
|
-
inString = true;
|
|
87
|
-
out += ch;
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
90
|
-
if (ch === "/" && next === "/") {
|
|
91
|
-
while (i < input.length && input[i] !== "\n") i++;
|
|
92
|
-
out += "\n";
|
|
93
|
-
continue;
|
|
94
|
-
}
|
|
95
|
-
if (ch === "/" && next === "*") {
|
|
96
|
-
i += 2;
|
|
97
|
-
while (i < input.length && !(input[i] === "*" && input[i + 1] === "/")) i++;
|
|
98
|
-
i++;
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
out += ch;
|
|
102
|
-
}
|
|
103
|
-
return out;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// Matches jsonc-parser's allowTrailingComma so every config.jsonc reader in
|
|
107
|
-
// this package accepts the same syntax. Runs on comment-stripped input.
|
|
108
|
-
function stripTrailingCommas(input) {
|
|
109
|
-
let out = "";
|
|
110
|
-
let inString = false;
|
|
111
|
-
let escaped = false;
|
|
112
|
-
for (let i = 0; i < input.length; i++) {
|
|
113
|
-
const ch = input[i];
|
|
114
|
-
if (inString) {
|
|
115
|
-
out += ch;
|
|
116
|
-
escaped = ch === "\\" ? !escaped : false;
|
|
117
|
-
if (ch === "\"" && !escaped) inString = false;
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
if (ch === "\"") {
|
|
121
|
-
inString = true;
|
|
122
|
-
out += ch;
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
125
|
-
if (ch === ",") {
|
|
126
|
-
let j = i + 1;
|
|
127
|
-
while (j < input.length && /\s/.test(input[j])) j++;
|
|
128
|
-
if (input[j] === "}" || input[j] === "]") continue;
|
|
129
|
-
}
|
|
130
|
-
out += ch;
|
|
131
|
-
}
|
|
132
|
-
return out;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
76
|
function parseArgs(argv) {
|
|
136
77
|
const opts = {};
|
|
137
78
|
for (let i = 0; i < argv.length; i++) {
|