@yishiguji/tokenarena 0.26.0 → 0.27.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/dist/index.js +361 -75
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -7111,6 +7111,292 @@ var MuseParser = class {
|
|
|
7111
7111
|
};
|
|
7112
7112
|
registerParser(new MuseParser());
|
|
7113
7113
|
|
|
7114
|
+
// src/parsers/stepcode.ts
|
|
7115
|
+
import { createHash as createHash6 } from "crypto";
|
|
7116
|
+
import { existsSync as existsSync32, statSync as statSync8 } from "fs";
|
|
7117
|
+
import { homedir as homedir32 } from "os";
|
|
7118
|
+
import { join as join33 } from "path";
|
|
7119
|
+
var TOOL_ID23 = "stepcode";
|
|
7120
|
+
var TOOL_NAME21 = "StepCode";
|
|
7121
|
+
var DEFAULT_AGENT_DIR = join33(homedir32(), ".stepcode", "agent");
|
|
7122
|
+
var DEFAULT_SESSIONS_DIR8 = join33(DEFAULT_AGENT_DIR, "sessions");
|
|
7123
|
+
function getStepCodeSessionsDir(env = process.env) {
|
|
7124
|
+
const sessionDir = env.STEP_CODING_AGENT_SESSION_DIR?.trim();
|
|
7125
|
+
if (sessionDir) return sessionDir;
|
|
7126
|
+
const agentDir = env.STEP_CODING_AGENT_DIR?.trim();
|
|
7127
|
+
return agentDir ? join33(agentDir, "sessions") : DEFAULT_SESSIONS_DIR8;
|
|
7128
|
+
}
|
|
7129
|
+
function getPathLeaf8(value) {
|
|
7130
|
+
const normalized = trimTrailingSeparators(value.replace(/\\/g, "/"));
|
|
7131
|
+
return normalized.split("/").filter(Boolean).pop() || "unknown";
|
|
7132
|
+
}
|
|
7133
|
+
function normalizeForPrefix6(value) {
|
|
7134
|
+
return trimTrailingSeparators(value.replace(/\\/g, "/"));
|
|
7135
|
+
}
|
|
7136
|
+
function extractStepCodeProjectFromCwd(cwd) {
|
|
7137
|
+
return getPathLeaf8(cwd);
|
|
7138
|
+
}
|
|
7139
|
+
function extractStepCodeProjectFromDir(filePath, sessionsDir = DEFAULT_SESSIONS_DIR8) {
|
|
7140
|
+
const normalizedFilePath = normalizeForPrefix6(filePath);
|
|
7141
|
+
const normalizedSessionsDir = normalizeForPrefix6(sessionsDir);
|
|
7142
|
+
const prefix = `${normalizedSessionsDir}/`;
|
|
7143
|
+
if (!normalizedFilePath.startsWith(prefix)) return "unknown";
|
|
7144
|
+
const firstSegment = normalizedFilePath.slice(prefix.length).split("/")[0];
|
|
7145
|
+
if (!firstSegment) return "unknown";
|
|
7146
|
+
try {
|
|
7147
|
+
const decoded = decodeURIComponent(firstSegment);
|
|
7148
|
+
if (decoded.includes("/") || decoded.includes("\\")) {
|
|
7149
|
+
return getPathLeaf8(decoded);
|
|
7150
|
+
}
|
|
7151
|
+
} catch {
|
|
7152
|
+
}
|
|
7153
|
+
return firstSegment.split("-").filter(Boolean).pop() || "unknown";
|
|
7154
|
+
}
|
|
7155
|
+
function tokenCount6(value) {
|
|
7156
|
+
if (typeof value !== "number") return null;
|
|
7157
|
+
return Number.isSafeInteger(value) && value >= 0 ? value : null;
|
|
7158
|
+
}
|
|
7159
|
+
function parseUsage(value) {
|
|
7160
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
7161
|
+
const usage = value;
|
|
7162
|
+
const counts = [
|
|
7163
|
+
tokenCount6(usage.input === void 0 ? 0 : usage.input),
|
|
7164
|
+
tokenCount6(usage.output === void 0 ? 0 : usage.output),
|
|
7165
|
+
tokenCount6(usage.cacheRead === void 0 ? 0 : usage.cacheRead),
|
|
7166
|
+
tokenCount6(usage.cacheWrite === void 0 ? 0 : usage.cacheWrite)
|
|
7167
|
+
];
|
|
7168
|
+
if (counts.some((count) => count === null)) return null;
|
|
7169
|
+
return {
|
|
7170
|
+
inputTokens: counts[0] ?? 0,
|
|
7171
|
+
outputTokens: counts[1] ?? 0,
|
|
7172
|
+
cachedTokens: counts[2] ?? 0,
|
|
7173
|
+
cacheCreationTokens: counts[3] ?? 0
|
|
7174
|
+
};
|
|
7175
|
+
}
|
|
7176
|
+
function parseTimestamp6(value) {
|
|
7177
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
7178
|
+
const timestamp = new Date(value);
|
|
7179
|
+
return Number.isNaN(timestamp.getTime()) ? null : timestamp;
|
|
7180
|
+
}
|
|
7181
|
+
if (typeof value === "string" && value.trim()) {
|
|
7182
|
+
const asNumber = Number(value);
|
|
7183
|
+
if (Number.isFinite(asNumber)) {
|
|
7184
|
+
const timestamp2 = new Date(asNumber);
|
|
7185
|
+
return Number.isNaN(timestamp2.getTime()) ? null : timestamp2;
|
|
7186
|
+
}
|
|
7187
|
+
const timestamp = new Date(value);
|
|
7188
|
+
return Number.isNaN(timestamp.getTime()) ? null : timestamp;
|
|
7189
|
+
}
|
|
7190
|
+
return null;
|
|
7191
|
+
}
|
|
7192
|
+
function canonicalJson2(value) {
|
|
7193
|
+
if (Array.isArray(value)) return `[${value.map(canonicalJson2).join(",")}]`;
|
|
7194
|
+
if (value && typeof value === "object") {
|
|
7195
|
+
return `{${Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([key, item]) => `${JSON.stringify(key)}:${canonicalJson2(item)}`).join(",")}}`;
|
|
7196
|
+
}
|
|
7197
|
+
return JSON.stringify(value) ?? "null";
|
|
7198
|
+
}
|
|
7199
|
+
function usageIdentity2(row, kind, usage) {
|
|
7200
|
+
const message = row.message;
|
|
7201
|
+
const semantic = createHash6("sha256").update(
|
|
7202
|
+
canonicalJson2({
|
|
7203
|
+
kind,
|
|
7204
|
+
timestamp: row.timestamp,
|
|
7205
|
+
messageTimestamp: message?.timestamp,
|
|
7206
|
+
provider: message?.provider,
|
|
7207
|
+
model: message?.model,
|
|
7208
|
+
responseModel: message?.responseModel,
|
|
7209
|
+
responseId: message?.responseId,
|
|
7210
|
+
api: message?.api,
|
|
7211
|
+
toolCallId: message?.toolCallId,
|
|
7212
|
+
toolName: message?.toolName,
|
|
7213
|
+
stopReason: message?.stopReason,
|
|
7214
|
+
errorMessage: message?.errorMessage,
|
|
7215
|
+
content: message?.content,
|
|
7216
|
+
summary: row.summary,
|
|
7217
|
+
usage
|
|
7218
|
+
})
|
|
7219
|
+
).digest("hex");
|
|
7220
|
+
return {
|
|
7221
|
+
semantic,
|
|
7222
|
+
request: row.id ? JSON.stringify([kind, row.id, row.timestamp, message?.timestamp]) : semantic,
|
|
7223
|
+
hasId: Boolean(row.id)
|
|
7224
|
+
};
|
|
7225
|
+
}
|
|
7226
|
+
function parseJsonl2(content) {
|
|
7227
|
+
const rows = [];
|
|
7228
|
+
let partial = false;
|
|
7229
|
+
for (const line of content.split("\n")) {
|
|
7230
|
+
const trimmed = line.trim();
|
|
7231
|
+
if (!trimmed) continue;
|
|
7232
|
+
let parsed;
|
|
7233
|
+
try {
|
|
7234
|
+
parsed = JSON.parse(trimmed);
|
|
7235
|
+
} catch {
|
|
7236
|
+
partial = true;
|
|
7237
|
+
continue;
|
|
7238
|
+
}
|
|
7239
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
7240
|
+
partial = true;
|
|
7241
|
+
continue;
|
|
7242
|
+
}
|
|
7243
|
+
rows.push(parsed);
|
|
7244
|
+
}
|
|
7245
|
+
return { rows, partial };
|
|
7246
|
+
}
|
|
7247
|
+
var StepCodeParser = class {
|
|
7248
|
+
constructor(sessionsDir = getStepCodeSessionsDir()) {
|
|
7249
|
+
this.sessionsDir = sessionsDir;
|
|
7250
|
+
this.tool = { id: TOOL_ID23, name: TOOL_NAME21, dataDir: sessionsDir };
|
|
7251
|
+
}
|
|
7252
|
+
sessionsDir;
|
|
7253
|
+
tool;
|
|
7254
|
+
async parse() {
|
|
7255
|
+
const sessionFiles = findJsonlFiles(this.sessionsDir);
|
|
7256
|
+
if (sessionFiles.length === 0) return { buckets: [], sessions: [] };
|
|
7257
|
+
const entries = [];
|
|
7258
|
+
const sessionEvents = [];
|
|
7259
|
+
const seenRequests = /* @__PURE__ */ new Set();
|
|
7260
|
+
const seenSemantics = /* @__PURE__ */ new Set();
|
|
7261
|
+
const legacySemantics = /* @__PURE__ */ new Set();
|
|
7262
|
+
let partial = false;
|
|
7263
|
+
for (const filePath of sessionFiles) {
|
|
7264
|
+
const content = readFileSafe(filePath);
|
|
7265
|
+
if (content === null) {
|
|
7266
|
+
if (existsSync32(filePath)) partial = true;
|
|
7267
|
+
continue;
|
|
7268
|
+
}
|
|
7269
|
+
const parsed = parseJsonl2(content);
|
|
7270
|
+
partial ||= parsed.partial;
|
|
7271
|
+
const rows = parsed.rows;
|
|
7272
|
+
if (rows.length === 0) continue;
|
|
7273
|
+
let sessionId = filePath;
|
|
7274
|
+
let project = extractStepCodeProjectFromDir(filePath, this.sessionsDir);
|
|
7275
|
+
let sessionTimestamp = null;
|
|
7276
|
+
for (const row of rows) {
|
|
7277
|
+
if (row.type !== "session") continue;
|
|
7278
|
+
if (row.id) sessionId = row.id;
|
|
7279
|
+
if (row.cwd) project = extractStepCodeProjectFromCwd(row.cwd);
|
|
7280
|
+
sessionTimestamp = parseTimestamp6(row.timestamp);
|
|
7281
|
+
break;
|
|
7282
|
+
}
|
|
7283
|
+
let fallbackTimestamp = sessionTimestamp;
|
|
7284
|
+
if (!fallbackTimestamp) {
|
|
7285
|
+
try {
|
|
7286
|
+
fallbackTimestamp = statSync8(filePath).mtime;
|
|
7287
|
+
} catch {
|
|
7288
|
+
}
|
|
7289
|
+
}
|
|
7290
|
+
for (const row of rows) {
|
|
7291
|
+
const message = row.message;
|
|
7292
|
+
const timestamp = (row.type === "message" ? parseTimestamp6(message?.timestamp) : null) ?? parseTimestamp6(row.timestamp) ?? fallbackTimestamp;
|
|
7293
|
+
if (row.type === "message" && message?.role === "user") {
|
|
7294
|
+
if (!timestamp) {
|
|
7295
|
+
partial = true;
|
|
7296
|
+
continue;
|
|
7297
|
+
}
|
|
7298
|
+
sessionEvents.push({
|
|
7299
|
+
sessionId,
|
|
7300
|
+
source: TOOL_ID23,
|
|
7301
|
+
project,
|
|
7302
|
+
timestamp,
|
|
7303
|
+
role: "user"
|
|
7304
|
+
});
|
|
7305
|
+
continue;
|
|
7306
|
+
}
|
|
7307
|
+
const kind = row.type === "message" ? message?.role : row.type;
|
|
7308
|
+
if (kind !== "assistant" && kind !== "toolResult" && kind !== "compaction" && kind !== "branch_summary") {
|
|
7309
|
+
continue;
|
|
7310
|
+
}
|
|
7311
|
+
const rawUsage = row.type === "message" ? message?.usage : row.usage;
|
|
7312
|
+
if (rawUsage === void 0) {
|
|
7313
|
+
if (kind === "assistant" && timestamp) {
|
|
7314
|
+
sessionEvents.push({
|
|
7315
|
+
sessionId,
|
|
7316
|
+
source: TOOL_ID23,
|
|
7317
|
+
project,
|
|
7318
|
+
timestamp,
|
|
7319
|
+
role: "assistant"
|
|
7320
|
+
});
|
|
7321
|
+
}
|
|
7322
|
+
continue;
|
|
7323
|
+
}
|
|
7324
|
+
const usage = parseUsage(rawUsage);
|
|
7325
|
+
if (!usage) {
|
|
7326
|
+
partial = true;
|
|
7327
|
+
if (kind === "assistant" && timestamp) {
|
|
7328
|
+
sessionEvents.push({
|
|
7329
|
+
sessionId,
|
|
7330
|
+
source: TOOL_ID23,
|
|
7331
|
+
project,
|
|
7332
|
+
timestamp,
|
|
7333
|
+
role: "assistant"
|
|
7334
|
+
});
|
|
7335
|
+
}
|
|
7336
|
+
continue;
|
|
7337
|
+
}
|
|
7338
|
+
if (usage.inputTokens === 0 && usage.outputTokens === 0 && usage.cachedTokens === 0 && usage.cacheCreationTokens === 0) {
|
|
7339
|
+
if (kind === "assistant" && timestamp) {
|
|
7340
|
+
sessionEvents.push({
|
|
7341
|
+
sessionId,
|
|
7342
|
+
source: TOOL_ID23,
|
|
7343
|
+
project,
|
|
7344
|
+
timestamp,
|
|
7345
|
+
role: "assistant"
|
|
7346
|
+
});
|
|
7347
|
+
}
|
|
7348
|
+
continue;
|
|
7349
|
+
}
|
|
7350
|
+
if (!timestamp) {
|
|
7351
|
+
partial = true;
|
|
7352
|
+
continue;
|
|
7353
|
+
}
|
|
7354
|
+
const identity = usageIdentity2(row, kind, rawUsage);
|
|
7355
|
+
if (seenRequests.has(identity.request) || (identity.hasId ? legacySemantics : seenSemantics).has(
|
|
7356
|
+
identity.semantic
|
|
7357
|
+
)) {
|
|
7358
|
+
continue;
|
|
7359
|
+
}
|
|
7360
|
+
seenRequests.add(identity.request);
|
|
7361
|
+
seenSemantics.add(identity.semantic);
|
|
7362
|
+
if (!identity.hasId) legacySemantics.add(identity.semantic);
|
|
7363
|
+
sessionEvents.push({
|
|
7364
|
+
sessionId,
|
|
7365
|
+
source: TOOL_ID23,
|
|
7366
|
+
project,
|
|
7367
|
+
timestamp,
|
|
7368
|
+
role: "assistant"
|
|
7369
|
+
});
|
|
7370
|
+
entries.push({
|
|
7371
|
+
sessionId,
|
|
7372
|
+
source: TOOL_ID23,
|
|
7373
|
+
model: kind === "assistant" ? message?.responseModel || message?.model || "unknown" : "unknown",
|
|
7374
|
+
project,
|
|
7375
|
+
timestamp,
|
|
7376
|
+
inputTokens: usage.inputTokens,
|
|
7377
|
+
outputTokens: usage.outputTokens,
|
|
7378
|
+
reasoningTokens: 0,
|
|
7379
|
+
cachedTokens: usage.cachedTokens,
|
|
7380
|
+
cacheCreationTokens: usage.cacheCreationTokens
|
|
7381
|
+
});
|
|
7382
|
+
}
|
|
7383
|
+
}
|
|
7384
|
+
const result = {
|
|
7385
|
+
buckets: aggregateToBuckets(entries),
|
|
7386
|
+
sessions: extractSessions(sessionEvents, entries)
|
|
7387
|
+
};
|
|
7388
|
+
if (partial) result.partial = true;
|
|
7389
|
+
return result;
|
|
7390
|
+
}
|
|
7391
|
+
listSourceFiles() {
|
|
7392
|
+
return findJsonlFiles(this.sessionsDir);
|
|
7393
|
+
}
|
|
7394
|
+
isInstalled() {
|
|
7395
|
+
return existsSync32(this.sessionsDir);
|
|
7396
|
+
}
|
|
7397
|
+
};
|
|
7398
|
+
registerParser(new StepCodeParser());
|
|
7399
|
+
|
|
7114
7400
|
// src/cli.ts
|
|
7115
7401
|
import { Command, Option } from "commander";
|
|
7116
7402
|
|
|
@@ -7146,8 +7432,8 @@ function getInsecureApiUrlReason(apiUrl) {
|
|
|
7146
7432
|
|
|
7147
7433
|
// src/infrastructure/config/manager.ts
|
|
7148
7434
|
import { randomUUID as randomUUID2 } from "crypto";
|
|
7149
|
-
import { existsSync as
|
|
7150
|
-
import { join as
|
|
7435
|
+
import { existsSync as existsSync33, readFileSync as readFileSync10, unlinkSync } from "fs";
|
|
7436
|
+
import { join as join35 } from "path";
|
|
7151
7437
|
|
|
7152
7438
|
// src/infrastructure/fs/private-files.ts
|
|
7153
7439
|
import { randomUUID } from "crypto";
|
|
@@ -7156,7 +7442,7 @@ import {
|
|
|
7156
7442
|
mkdirSync,
|
|
7157
7443
|
renameSync,
|
|
7158
7444
|
rmSync as rmSync3,
|
|
7159
|
-
statSync as
|
|
7445
|
+
statSync as statSync9,
|
|
7160
7446
|
writeFileSync
|
|
7161
7447
|
} from "fs";
|
|
7162
7448
|
var PRIVATE_DIR_MODE = 448;
|
|
@@ -7166,7 +7452,7 @@ function restrictToOwner(path) {
|
|
|
7166
7452
|
return;
|
|
7167
7453
|
}
|
|
7168
7454
|
try {
|
|
7169
|
-
const { mode } =
|
|
7455
|
+
const { mode } = statSync9(path);
|
|
7170
7456
|
if ((mode & 63) !== 0) {
|
|
7171
7457
|
chmodSync(path, mode & 448);
|
|
7172
7458
|
}
|
|
@@ -7192,22 +7478,22 @@ function writePrivateFile(path, content) {
|
|
|
7192
7478
|
}
|
|
7193
7479
|
|
|
7194
7480
|
// src/infrastructure/xdg.ts
|
|
7195
|
-
import { homedir as
|
|
7196
|
-
import { join as
|
|
7481
|
+
import { homedir as homedir33 } from "os";
|
|
7482
|
+
import { join as join34 } from "path";
|
|
7197
7483
|
function getConfigHome() {
|
|
7198
|
-
return process.env.XDG_CONFIG_HOME ||
|
|
7484
|
+
return process.env.XDG_CONFIG_HOME || join34(homedir33(), ".config");
|
|
7199
7485
|
}
|
|
7200
7486
|
function getStateHome() {
|
|
7201
|
-
return process.env.XDG_STATE_HOME ||
|
|
7487
|
+
return process.env.XDG_STATE_HOME || join34(homedir33(), ".local", "state");
|
|
7202
7488
|
}
|
|
7203
7489
|
function getRuntimeDir() {
|
|
7204
7490
|
return process.env.XDG_RUNTIME_DIR || getStateHome();
|
|
7205
7491
|
}
|
|
7206
7492
|
|
|
7207
7493
|
// src/infrastructure/config/manager.ts
|
|
7208
|
-
var CONFIG_DIR =
|
|
7494
|
+
var CONFIG_DIR = join35(getConfigHome(), "tokenarena");
|
|
7209
7495
|
var isDev = process.env.TOKEN_ARENA_DEV === "1";
|
|
7210
|
-
var CONFIG_FILE =
|
|
7496
|
+
var CONFIG_FILE = join35(CONFIG_DIR, isDev ? "config.dev.json" : "config.json");
|
|
7211
7497
|
var DEFAULT_API_URL = "https://token.yishiguji.com";
|
|
7212
7498
|
var VALID_CONFIG_KEYS = [
|
|
7213
7499
|
"apiKey",
|
|
@@ -7223,7 +7509,7 @@ function getConfigDir() {
|
|
|
7223
7509
|
return CONFIG_DIR;
|
|
7224
7510
|
}
|
|
7225
7511
|
function loadConfig() {
|
|
7226
|
-
if (!
|
|
7512
|
+
if (!existsSync33(CONFIG_FILE)) return null;
|
|
7227
7513
|
restrictToOwner(CONFIG_DIR);
|
|
7228
7514
|
restrictToOwner(CONFIG_FILE);
|
|
7229
7515
|
try {
|
|
@@ -7243,7 +7529,7 @@ function saveConfig(config) {
|
|
|
7243
7529
|
`);
|
|
7244
7530
|
}
|
|
7245
7531
|
function deleteConfig() {
|
|
7246
|
-
if (
|
|
7532
|
+
if (existsSync33(CONFIG_FILE)) {
|
|
7247
7533
|
unlinkSync(CONFIG_FILE);
|
|
7248
7534
|
}
|
|
7249
7535
|
}
|
|
@@ -7688,11 +7974,11 @@ function toProjectIdentity(input2) {
|
|
|
7688
7974
|
}
|
|
7689
7975
|
|
|
7690
7976
|
// src/domain/upload-manifest.ts
|
|
7691
|
-
import { createHash as
|
|
7977
|
+
import { createHash as createHash7 } from "crypto";
|
|
7692
7978
|
var MANIFEST_VERSION = 1;
|
|
7693
7979
|
var SNAPSHOT_PROTOCOL_VERSION = 1;
|
|
7694
7980
|
function fingerprint(value) {
|
|
7695
|
-
return
|
|
7981
|
+
return createHash7("sha256").update(value).digest("hex").slice(0, 16);
|
|
7696
7982
|
}
|
|
7697
7983
|
function normalizeApiUrl(apiUrl) {
|
|
7698
7984
|
let end = apiUrl.length;
|
|
@@ -8119,7 +8405,7 @@ var ApiClient = class {
|
|
|
8119
8405
|
// src/infrastructure/runtime/lock.ts
|
|
8120
8406
|
import {
|
|
8121
8407
|
closeSync,
|
|
8122
|
-
existsSync as
|
|
8408
|
+
existsSync as existsSync34,
|
|
8123
8409
|
openSync,
|
|
8124
8410
|
readFileSync as readFileSync11,
|
|
8125
8411
|
rmSync as rmSync4,
|
|
@@ -8127,22 +8413,22 @@ import {
|
|
|
8127
8413
|
} from "fs";
|
|
8128
8414
|
|
|
8129
8415
|
// src/infrastructure/runtime/paths.ts
|
|
8130
|
-
import { join as
|
|
8416
|
+
import { join as join36 } from "path";
|
|
8131
8417
|
var APP_NAME = "tokenarena";
|
|
8132
8418
|
function getRuntimeDirPath() {
|
|
8133
|
-
return
|
|
8419
|
+
return join36(getRuntimeDir(), APP_NAME);
|
|
8134
8420
|
}
|
|
8135
8421
|
function getStateDir() {
|
|
8136
|
-
return
|
|
8422
|
+
return join36(getStateHome(), APP_NAME);
|
|
8137
8423
|
}
|
|
8138
8424
|
function getSyncLockPath() {
|
|
8139
|
-
return
|
|
8425
|
+
return join36(getRuntimeDirPath(), "sync.lock");
|
|
8140
8426
|
}
|
|
8141
8427
|
function getSyncStatePath() {
|
|
8142
|
-
return
|
|
8428
|
+
return join36(getStateDir(), "status.json");
|
|
8143
8429
|
}
|
|
8144
8430
|
function getUploadManifestPath() {
|
|
8145
|
-
return
|
|
8431
|
+
return join36(getStateDir(), "upload-manifest.json");
|
|
8146
8432
|
}
|
|
8147
8433
|
function ensureAppDirs() {
|
|
8148
8434
|
ensurePrivateDir(getRuntimeDirPath());
|
|
@@ -8160,7 +8446,7 @@ function isProcessAlive(pid) {
|
|
|
8160
8446
|
}
|
|
8161
8447
|
}
|
|
8162
8448
|
function readLockMetadata(lockPath) {
|
|
8163
|
-
if (!
|
|
8449
|
+
if (!existsSync34(lockPath)) {
|
|
8164
8450
|
return null;
|
|
8165
8451
|
}
|
|
8166
8452
|
try {
|
|
@@ -8234,13 +8520,13 @@ function describeExistingSyncLock() {
|
|
|
8234
8520
|
}
|
|
8235
8521
|
|
|
8236
8522
|
// src/infrastructure/runtime/state.ts
|
|
8237
|
-
import { existsSync as
|
|
8523
|
+
import { existsSync as existsSync35, readFileSync as readFileSync12 } from "fs";
|
|
8238
8524
|
function getDefaultState() {
|
|
8239
8525
|
return { status: "idle" };
|
|
8240
8526
|
}
|
|
8241
8527
|
function loadSyncState() {
|
|
8242
8528
|
const path = getSyncStatePath();
|
|
8243
|
-
if (!
|
|
8529
|
+
if (!existsSync35(path)) {
|
|
8244
8530
|
return getDefaultState();
|
|
8245
8531
|
}
|
|
8246
8532
|
try {
|
|
@@ -8297,7 +8583,7 @@ function markSyncFailed(source, error, status) {
|
|
|
8297
8583
|
}
|
|
8298
8584
|
|
|
8299
8585
|
// src/infrastructure/runtime/upload-manifest.ts
|
|
8300
|
-
import { existsSync as
|
|
8586
|
+
import { existsSync as existsSync36, readFileSync as readFileSync13 } from "fs";
|
|
8301
8587
|
function isRecordOfStrings(value) {
|
|
8302
8588
|
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
8303
8589
|
return false;
|
|
@@ -8313,7 +8599,7 @@ function isUploadManifest(value) {
|
|
|
8313
8599
|
}
|
|
8314
8600
|
function loadUploadManifest() {
|
|
8315
8601
|
const path = getUploadManifestPath();
|
|
8316
|
-
if (!
|
|
8602
|
+
if (!existsSync36(path)) {
|
|
8317
8603
|
return null;
|
|
8318
8604
|
}
|
|
8319
8605
|
try {
|
|
@@ -8342,20 +8628,20 @@ function saveUploadManifest(manifest) {
|
|
|
8342
8628
|
}
|
|
8343
8629
|
|
|
8344
8630
|
// src/services/parse-cache.ts
|
|
8345
|
-
import { createHash as
|
|
8346
|
-
import { readFileSync as readFileSync14, statSync as
|
|
8631
|
+
import { createHash as createHash8 } from "crypto";
|
|
8632
|
+
import { readFileSync as readFileSync14, statSync as statSync10 } from "fs";
|
|
8347
8633
|
import { hostname as hostname6 } from "os";
|
|
8348
|
-
import { join as
|
|
8634
|
+
import { join as join38 } from "path";
|
|
8349
8635
|
|
|
8350
8636
|
// src/infrastructure/runtime/cli-version.ts
|
|
8351
|
-
import { dirname as dirname11, join as
|
|
8637
|
+
import { dirname as dirname11, join as join37 } from "path";
|
|
8352
8638
|
import { fileURLToPath } from "url";
|
|
8353
8639
|
var FALLBACK_VERSION = "0.0.0";
|
|
8354
8640
|
var versionCache = /* @__PURE__ */ new Map();
|
|
8355
8641
|
function readPackageVersion(startDir) {
|
|
8356
8642
|
let dir = startDir;
|
|
8357
8643
|
while (true) {
|
|
8358
|
-
const contents = readFileSafe(
|
|
8644
|
+
const contents = readFileSafe(join37(dir, "package.json"));
|
|
8359
8645
|
if (contents !== null) {
|
|
8360
8646
|
try {
|
|
8361
8647
|
const { version } = JSON.parse(contents);
|
|
@@ -8386,20 +8672,20 @@ function getCliVersion(metaUrl = import.meta.url) {
|
|
|
8386
8672
|
var CACHE_FORMAT_VERSION = 1;
|
|
8387
8673
|
var SETTLE_MS = 5e3;
|
|
8388
8674
|
function getParseCacheDir() {
|
|
8389
|
-
return
|
|
8675
|
+
return join38(getStateDir(), "parse-cache");
|
|
8390
8676
|
}
|
|
8391
8677
|
function getParseCachePath(toolId) {
|
|
8392
|
-
return
|
|
8678
|
+
return join38(getParseCacheDir(), `${toolId}.json`);
|
|
8393
8679
|
}
|
|
8394
8680
|
function computeScanFingerprint(files, now = Date.now()) {
|
|
8395
|
-
const hash =
|
|
8681
|
+
const hash = createHash8("sha256");
|
|
8396
8682
|
hash.update(`v${CACHE_FORMAT_VERSION}\0`);
|
|
8397
8683
|
hash.update(`${getCliVersion()}\0`);
|
|
8398
8684
|
hash.update(`${hostname6()}\0`);
|
|
8399
8685
|
for (const file of [...files].sort()) {
|
|
8400
8686
|
let stats;
|
|
8401
8687
|
try {
|
|
8402
|
-
stats =
|
|
8688
|
+
stats = statSync10(file);
|
|
8403
8689
|
} catch {
|
|
8404
8690
|
return null;
|
|
8405
8691
|
}
|
|
@@ -9072,18 +9358,18 @@ View your dashboard at: ${apiUrl}/usage`);
|
|
|
9072
9358
|
|
|
9073
9359
|
// src/commands/init.ts
|
|
9074
9360
|
import { execFileSync as execFileSync8, spawn } from "child_process";
|
|
9075
|
-
import { existsSync as
|
|
9361
|
+
import { existsSync as existsSync39 } from "fs";
|
|
9076
9362
|
import { appendFile, mkdir, readFile } from "fs/promises";
|
|
9077
|
-
import { homedir as
|
|
9078
|
-
import { dirname as dirname12, join as
|
|
9363
|
+
import { homedir as homedir36, platform as platform5 } from "os";
|
|
9364
|
+
import { dirname as dirname12, join as join39, posix as posix3, win32 as win322 } from "path";
|
|
9079
9365
|
|
|
9080
9366
|
// src/infrastructure/service/index.ts
|
|
9081
9367
|
import { platform as platform4 } from "os";
|
|
9082
9368
|
|
|
9083
9369
|
// src/infrastructure/service/linux-systemd.ts
|
|
9084
9370
|
import { execFileSync as execFileSync6 } from "child_process";
|
|
9085
|
-
import { existsSync as
|
|
9086
|
-
import { homedir as
|
|
9371
|
+
import { existsSync as existsSync37, mkdirSync as mkdirSync2, rmSync as rmSync5, writeFileSync as writeFileSync3 } from "fs";
|
|
9372
|
+
import { homedir as homedir34, platform as platform2 } from "os";
|
|
9087
9373
|
import { posix } from "path";
|
|
9088
9374
|
|
|
9089
9375
|
// src/infrastructure/service/utils.ts
|
|
@@ -9145,10 +9431,10 @@ function escapeXml(value) {
|
|
|
9145
9431
|
|
|
9146
9432
|
// src/infrastructure/service/linux-systemd.ts
|
|
9147
9433
|
var SYSTEMD_SERVICE_NAME = "tokenarena";
|
|
9148
|
-
function getLinuxSystemdServiceDir(homePath =
|
|
9434
|
+
function getLinuxSystemdServiceDir(homePath = homedir34()) {
|
|
9149
9435
|
return posix.join(homePath, ".config", "systemd", "user");
|
|
9150
9436
|
}
|
|
9151
|
-
function getLinuxSystemdServiceFile(homePath =
|
|
9437
|
+
function getLinuxSystemdServiceFile(homePath = homedir34()) {
|
|
9152
9438
|
return posix.join(
|
|
9153
9439
|
getLinuxSystemdServiceDir(homePath),
|
|
9154
9440
|
`${SYSTEMD_SERVICE_NAME}.service`
|
|
@@ -9205,7 +9491,7 @@ function ensureSystemdAvailable() {
|
|
|
9205
9491
|
}
|
|
9206
9492
|
function createLinuxSystemdServiceBackend() {
|
|
9207
9493
|
function isInstalled() {
|
|
9208
|
-
return
|
|
9494
|
+
return existsSync37(getLinuxSystemdServiceFile());
|
|
9209
9495
|
}
|
|
9210
9496
|
async function setup(skipPrompt = false) {
|
|
9211
9497
|
if (!ensureSystemdAvailable()) {
|
|
@@ -9330,7 +9616,7 @@ function createLinuxSystemdServiceBackend() {
|
|
|
9330
9616
|
}
|
|
9331
9617
|
async function uninstall(skipPrompt = false) {
|
|
9332
9618
|
const serviceFile = getLinuxSystemdServiceFile();
|
|
9333
|
-
if (!
|
|
9619
|
+
if (!existsSync37(serviceFile)) {
|
|
9334
9620
|
logger.info(formatBullet("\u670D\u52A1\u6587\u4EF6\u4E0D\u5B58\u5728\u3002", "warning"));
|
|
9335
9621
|
return;
|
|
9336
9622
|
}
|
|
@@ -9391,17 +9677,17 @@ function createLinuxSystemdServiceBackend() {
|
|
|
9391
9677
|
|
|
9392
9678
|
// src/infrastructure/service/macos-launchd.ts
|
|
9393
9679
|
import { execFileSync as execFileSync7 } from "child_process";
|
|
9394
|
-
import { existsSync as
|
|
9395
|
-
import { homedir as
|
|
9680
|
+
import { existsSync as existsSync38, mkdirSync as mkdirSync3, rmSync as rmSync6, writeFileSync as writeFileSync4 } from "fs";
|
|
9681
|
+
import { homedir as homedir35, platform as platform3 } from "os";
|
|
9396
9682
|
import { posix as posix2 } from "path";
|
|
9397
9683
|
var MACOS_LAUNCHD_LABEL = "com.guji.tokenarena";
|
|
9398
9684
|
function getCurrentUid() {
|
|
9399
9685
|
return typeof process.getuid === "function" ? process.getuid() : null;
|
|
9400
9686
|
}
|
|
9401
|
-
function getMacosLaunchAgentDir(homePath =
|
|
9687
|
+
function getMacosLaunchAgentDir(homePath = homedir35()) {
|
|
9402
9688
|
return posix2.join(homePath, "Library", "LaunchAgents");
|
|
9403
9689
|
}
|
|
9404
|
-
function getMacosLaunchAgentFile(homePath =
|
|
9690
|
+
function getMacosLaunchAgentFile(homePath = homedir35()) {
|
|
9405
9691
|
return posix2.join(
|
|
9406
9692
|
getMacosLaunchAgentDir(homePath),
|
|
9407
9693
|
`${MACOS_LAUNCHD_LABEL}.plist`
|
|
@@ -9528,7 +9814,7 @@ function writeLaunchAgentPlist() {
|
|
|
9528
9814
|
label: MACOS_LAUNCHD_LABEL,
|
|
9529
9815
|
programArguments: [command.execPath, ...command.args],
|
|
9530
9816
|
environment: getManagedServiceEnvironment(),
|
|
9531
|
-
workingDirectory:
|
|
9817
|
+
workingDirectory: homedir35(),
|
|
9532
9818
|
standardOutPath: stdoutPath,
|
|
9533
9819
|
standardErrorPath: stderrPath
|
|
9534
9820
|
});
|
|
@@ -9553,7 +9839,7 @@ function bootstrapLaunchAgent() {
|
|
|
9553
9839
|
}
|
|
9554
9840
|
function createMacosLaunchdServiceBackend() {
|
|
9555
9841
|
function isInstalled() {
|
|
9556
|
-
return
|
|
9842
|
+
return existsSync38(getMacosLaunchAgentFile());
|
|
9557
9843
|
}
|
|
9558
9844
|
async function setup(skipPrompt = false) {
|
|
9559
9845
|
if (!ensureLaunchctlAvailable()) {
|
|
@@ -9694,7 +9980,7 @@ function createMacosLaunchdServiceBackend() {
|
|
|
9694
9980
|
}
|
|
9695
9981
|
async function uninstall(skipPrompt = false) {
|
|
9696
9982
|
const plistFile = getMacosLaunchAgentFile();
|
|
9697
|
-
if (!
|
|
9983
|
+
if (!existsSync38(plistFile)) {
|
|
9698
9984
|
logger.info(formatBullet("\u670D\u52A1\u6587\u4EF6\u4E0D\u5B58\u5728\u3002", "warning"));
|
|
9699
9985
|
return;
|
|
9700
9986
|
}
|
|
@@ -9805,7 +10091,7 @@ function resolvePowerShellProfilePath() {
|
|
|
9805
10091
|
const systemRoot = process.env.SYSTEMROOT || "C:\\Windows";
|
|
9806
10092
|
const candidates = [
|
|
9807
10093
|
resolveCommandPath("pwsh.exe"),
|
|
9808
|
-
|
|
10094
|
+
join39(systemRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe")
|
|
9809
10095
|
].filter((command) => command !== null);
|
|
9810
10096
|
for (const command of candidates) {
|
|
9811
10097
|
try {
|
|
@@ -9834,8 +10120,8 @@ function resolvePowerShellProfilePath() {
|
|
|
9834
10120
|
function resolveShellAliasSetup(options = {}) {
|
|
9835
10121
|
const currentPlatform = options.currentPlatform ?? platform5();
|
|
9836
10122
|
const env = options.env ?? process.env;
|
|
9837
|
-
const homeDir = options.homeDir ??
|
|
9838
|
-
const pathExists = options.exists ??
|
|
10123
|
+
const homeDir = options.homeDir ?? homedir36();
|
|
10124
|
+
const pathExists = options.exists ?? existsSync39;
|
|
9839
10125
|
const shellFromEnv = env.SHELL ? basenameLikeShell(env.SHELL).toLowerCase() : "";
|
|
9840
10126
|
const shellName = shellFromEnv || (currentPlatform === "win32" ? "powershell" : "");
|
|
9841
10127
|
const aliasName = "ta";
|
|
@@ -10037,7 +10323,7 @@ async function setupShellAlias() {
|
|
|
10037
10323
|
try {
|
|
10038
10324
|
await mkdir(dirname12(setup.configFile), { recursive: true });
|
|
10039
10325
|
let existingContent = "";
|
|
10040
|
-
if (
|
|
10326
|
+
if (existsSync39(setup.configFile)) {
|
|
10041
10327
|
existingContent = await readFile(setup.configFile, "utf-8");
|
|
10042
10328
|
}
|
|
10043
10329
|
const normalizedContent = existingContent.toLowerCase();
|
|
@@ -10698,8 +10984,8 @@ async function runSyncCommand(opts = {}) {
|
|
|
10698
10984
|
}
|
|
10699
10985
|
|
|
10700
10986
|
// src/commands/uninstall.ts
|
|
10701
|
-
import { existsSync as
|
|
10702
|
-
import { homedir as
|
|
10987
|
+
import { existsSync as existsSync40, readFileSync as readFileSync15, rmSync as rmSync7, writeFileSync as writeFileSync5 } from "fs";
|
|
10988
|
+
import { homedir as homedir37, platform as platform6 } from "os";
|
|
10703
10989
|
function removeShellAlias() {
|
|
10704
10990
|
const shell = process.env.SHELL;
|
|
10705
10991
|
if (!shell) return;
|
|
@@ -10708,22 +10994,22 @@ function removeShellAlias() {
|
|
|
10708
10994
|
let configFile;
|
|
10709
10995
|
switch (shellName) {
|
|
10710
10996
|
case "zsh":
|
|
10711
|
-
configFile = `${
|
|
10997
|
+
configFile = `${homedir37()}/.zshrc`;
|
|
10712
10998
|
break;
|
|
10713
10999
|
case "bash":
|
|
10714
|
-
if (platform6() === "darwin" &&
|
|
10715
|
-
configFile = `${
|
|
11000
|
+
if (platform6() === "darwin" && existsSync40(`${homedir37()}/.bash_profile`)) {
|
|
11001
|
+
configFile = `${homedir37()}/.bash_profile`;
|
|
10716
11002
|
} else {
|
|
10717
|
-
configFile = `${
|
|
11003
|
+
configFile = `${homedir37()}/.bashrc`;
|
|
10718
11004
|
}
|
|
10719
11005
|
break;
|
|
10720
11006
|
case "fish":
|
|
10721
|
-
configFile = `${
|
|
11007
|
+
configFile = `${homedir37()}/.config/fish/config.fish`;
|
|
10722
11008
|
break;
|
|
10723
11009
|
default:
|
|
10724
11010
|
return;
|
|
10725
11011
|
}
|
|
10726
|
-
if (!
|
|
11012
|
+
if (!existsSync40(configFile)) return;
|
|
10727
11013
|
try {
|
|
10728
11014
|
let content = readFileSync15(configFile, "utf-8");
|
|
10729
11015
|
const aliasPatterns = [
|
|
@@ -10762,7 +11048,7 @@ async function runUninstall() {
|
|
|
10762
11048
|
const runtimeDir = getRuntimeDirPath();
|
|
10763
11049
|
const serviceBackend = getServiceBackend();
|
|
10764
11050
|
const hasInstalledService = serviceBackend?.isInstalled() ?? false;
|
|
10765
|
-
const hasLocalArtifacts =
|
|
11051
|
+
const hasLocalArtifacts = existsSync40(configPath) || existsSync40(configDir) || existsSync40(stateDir) || existsSync40(runtimeDir) || hasInstalledService;
|
|
10766
11052
|
if (!hasLocalArtifacts) {
|
|
10767
11053
|
logger.info(formatHeader("\u5378\u8F7D TokenArena"));
|
|
10768
11054
|
logger.info(formatBullet("\u672A\u53D1\u73B0\u672C\u5730\u914D\u7F6E\uFF0C\u65E0\u9700\u5378\u8F7D\u3002"));
|
|
@@ -10806,22 +11092,22 @@ async function runUninstall() {
|
|
|
10806
11092
|
}
|
|
10807
11093
|
}
|
|
10808
11094
|
logger.info(formatSection("\u6267\u884C\u7ED3\u679C"));
|
|
10809
|
-
if (
|
|
11095
|
+
if (existsSync40(configPath)) {
|
|
10810
11096
|
deleteConfig();
|
|
10811
11097
|
logger.info(formatBullet("\u5DF2\u5220\u9664\u914D\u7F6E\u6587\u4EF6\u3002", "success"));
|
|
10812
11098
|
}
|
|
10813
|
-
if (
|
|
11099
|
+
if (existsSync40(configDir)) {
|
|
10814
11100
|
try {
|
|
10815
11101
|
rmSync7(configDir, { recursive: false, force: true });
|
|
10816
11102
|
logger.info(formatBullet("\u5DF2\u5220\u9664\u914D\u7F6E\u76EE\u5F55\u3002", "success"));
|
|
10817
11103
|
} catch {
|
|
10818
11104
|
}
|
|
10819
11105
|
}
|
|
10820
|
-
if (
|
|
11106
|
+
if (existsSync40(stateDir)) {
|
|
10821
11107
|
rmSync7(stateDir, { recursive: true, force: true });
|
|
10822
11108
|
logger.info(formatBullet("\u5DF2\u5220\u9664\u72B6\u6001\u6570\u636E\u3002", "success"));
|
|
10823
11109
|
}
|
|
10824
|
-
if (
|
|
11110
|
+
if (existsSync40(runtimeDir)) {
|
|
10825
11111
|
rmSync7(runtimeDir, { recursive: true, force: true });
|
|
10826
11112
|
logger.info(formatBullet("\u5DF2\u5220\u9664\u8FD0\u884C\u65F6\u6570\u636E\u3002", "success"));
|
|
10827
11113
|
}
|
|
@@ -10953,7 +11239,7 @@ import { readFileSync as readFileSync16 } from "fs";
|
|
|
10953
11239
|
import { promisify } from "util";
|
|
10954
11240
|
|
|
10955
11241
|
// src/services/muse-export.ts
|
|
10956
|
-
import { createHash as
|
|
11242
|
+
import { createHash as createHash9 } from "crypto";
|
|
10957
11243
|
import { mkdirSync as mkdirSync4 } from "fs";
|
|
10958
11244
|
import { dirname as dirname13 } from "path";
|
|
10959
11245
|
var MUSE_USAGE_ROWS_QUERY = `SELECT json_build_object(
|
|
@@ -10974,9 +11260,9 @@ var MUSE_USAGE_TOTALS_QUERY = `SELECT count(*) AS records,
|
|
|
10974
11260
|
max(created_at) AS max_ts
|
|
10975
11261
|
FROM agent.agent_message_token_usage`;
|
|
10976
11262
|
function sha256Hex(value) {
|
|
10977
|
-
return
|
|
11263
|
+
return createHash9("sha256").update(value, "utf8").digest("hex");
|
|
10978
11264
|
}
|
|
10979
|
-
function
|
|
11265
|
+
function tokenCount7(value) {
|
|
10980
11266
|
if (typeof value !== "number") return null;
|
|
10981
11267
|
return Number.isSafeInteger(value) && value >= 0 ? value : null;
|
|
10982
11268
|
}
|
|
@@ -11001,8 +11287,8 @@ function parseMuseRawRow(value, index) {
|
|
|
11001
11287
|
"messageId, sessionId and agentId must be non-empty strings"
|
|
11002
11288
|
);
|
|
11003
11289
|
}
|
|
11004
|
-
const inputTokens =
|
|
11005
|
-
const outputTokens =
|
|
11290
|
+
const inputTokens = tokenCount7(raw.inputTokens);
|
|
11291
|
+
const outputTokens = tokenCount7(raw.outputTokens);
|
|
11006
11292
|
if (inputTokens === null || outputTokens === null) {
|
|
11007
11293
|
throw invalidRow(
|
|
11008
11294
|
index,
|
|
@@ -11409,7 +11695,7 @@ function createCli() {
|
|
|
11409
11695
|
}
|
|
11410
11696
|
|
|
11411
11697
|
// src/infrastructure/runtime/main-module.ts
|
|
11412
|
-
import { existsSync as
|
|
11698
|
+
import { existsSync as existsSync41, realpathSync as realpathSync2 } from "fs";
|
|
11413
11699
|
import { resolve as resolve4 } from "path";
|
|
11414
11700
|
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
11415
11701
|
function isMainModule(metaUrl, argvEntry = process.argv[1]) {
|
|
@@ -11420,7 +11706,7 @@ function isMainModule(metaUrl, argvEntry = process.argv[1]) {
|
|
|
11420
11706
|
try {
|
|
11421
11707
|
return realpathSync2(argvEntry) === realpathSync2(currentModulePath);
|
|
11422
11708
|
} catch {
|
|
11423
|
-
if (!
|
|
11709
|
+
if (!existsSync41(argvEntry)) {
|
|
11424
11710
|
return false;
|
|
11425
11711
|
}
|
|
11426
11712
|
return resolve4(argvEntry) === resolve4(currentModulePath);
|