polyergon 0.0.0-stage → 0.1.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/.next/BUILD_ID +1 -0
- package/.next/app-build-manifest.json +35 -0
- package/.next/app-path-routes-manifest.json +1 -0
- package/.next/build-manifest.json +32 -0
- package/.next/export-marker.json +1 -0
- package/.next/images-manifest.json +1 -0
- package/.next/next-minimal-server.js.nft.json +1 -0
- package/.next/next-server.js.nft.json +1 -0
- package/.next/package.json +1 -0
- package/.next/prerender-manifest.json +1 -0
- package/.next/react-loadable-manifest.json +10 -0
- package/.next/required-server-files.json +1 -0
- package/.next/routes-manifest.json +1 -0
- package/.next/server/app/_not-found/page.js +1 -0
- package/.next/server/app/_not-found/page.js.nft.json +1 -0
- package/.next/server/app/_not-found/page_client-reference-manifest.js +1 -0
- package/.next/server/app/_not-found.html +1 -0
- package/.next/server/app/_not-found.meta +6 -0
- package/.next/server/app/_not-found.rsc +9 -0
- package/.next/server/app/api/automations/existing/route.js +1 -0
- package/.next/server/app/api/automations/existing/route.js.nft.json +1 -0
- package/.next/server/app/api/backends/route.js +1 -0
- package/.next/server/app/api/backends/route.js.nft.json +1 -0
- package/.next/server/app/api/limits/route.js +1 -0
- package/.next/server/app/api/limits/route.js.nft.json +1 -0
- package/.next/server/app/api/meetings/[id]/route.js +1 -0
- package/.next/server/app/api/meetings/[id]/route.js.nft.json +1 -0
- package/.next/server/app/api/meetings/[id]/slack/route.js +3 -0
- package/.next/server/app/api/meetings/[id]/slack/route.js.nft.json +1 -0
- package/.next/server/app/api/meetings/library/route.js +1 -0
- package/.next/server/app/api/meetings/library/route.js.nft.json +1 -0
- package/.next/server/app/api/meetings/route.js +1 -0
- package/.next/server/app/api/meetings/route.js.nft.json +1 -0
- package/.next/server/app/api/models/route.js +1 -0
- package/.next/server/app/api/models/route.js.nft.json +1 -0
- package/.next/server/app/api/notes/route.js +1 -0
- package/.next/server/app/api/notes/route.js.nft.json +1 -0
- package/.next/server/app/api/orgs/[id]/route.js +1 -0
- package/.next/server/app/api/orgs/[id]/route.js.nft.json +1 -0
- package/.next/server/app/api/orgs/route.js +1 -0
- package/.next/server/app/api/orgs/route.js.nft.json +1 -0
- package/.next/server/app/api/prs/inbox/route.js +1 -0
- package/.next/server/app/api/prs/inbox/route.js.nft.json +1 -0
- package/.next/server/app/api/prs/refresh/route.js +1 -0
- package/.next/server/app/api/prs/refresh/route.js.nft.json +1 -0
- package/.next/server/app/api/prs/route.js +1 -0
- package/.next/server/app/api/prs/route.js.nft.json +1 -0
- package/.next/server/app/api/session-names/route.js +1 -0
- package/.next/server/app/api/session-names/route.js.nft.json +1 -0
- package/.next/server/app/api/sessions/[id]/route.js +1 -0
- package/.next/server/app/api/sessions/[id]/route.js.nft.json +1 -0
- package/.next/server/app/api/sessions/route.js +1 -0
- package/.next/server/app/api/sessions/route.js.nft.json +1 -0
- package/.next/server/app/api/settings/auth/route.js +1 -0
- package/.next/server/app/api/settings/auth/route.js.nft.json +1 -0
- package/.next/server/app/api/settings/route.js +1 -0
- package/.next/server/app/api/settings/route.js.nft.json +1 -0
- package/.next/server/app/api/settings/transcription/route.js +1 -0
- package/.next/server/app/api/settings/transcription/route.js.nft.json +1 -0
- package/.next/server/app/api/skills/[name]/route.js +8 -0
- package/.next/server/app/api/skills/[name]/route.js.nft.json +1 -0
- package/.next/server/app/api/skills/marketplace/route.js +2 -0
- package/.next/server/app/api/skills/marketplace/route.js.nft.json +1 -0
- package/.next/server/app/api/skills/route.js +8 -0
- package/.next/server/app/api/skills/route.js.nft.json +1 -0
- package/.next/server/app/api/skills/view/route.js +8 -0
- package/.next/server/app/api/skills/view/route.js.nft.json +1 -0
- package/.next/server/app/api/slack/channels/route.js +1 -0
- package/.next/server/app/api/slack/channels/route.js.nft.json +1 -0
- package/.next/server/app/api/slack/configured/route.js +1 -0
- package/.next/server/app/api/slack/configured/route.js.nft.json +1 -0
- package/.next/server/app/api/slack/inbox/route.js +1 -0
- package/.next/server/app/api/slack/inbox/route.js.nft.json +1 -0
- package/.next/server/app/api/usage/route.js +1 -0
- package/.next/server/app/api/usage/route.js.nft.json +1 -0
- package/.next/server/app/demo/page.js +1 -0
- package/.next/server/app/demo/page.js.nft.json +1 -0
- package/.next/server/app/demo/page_client-reference-manifest.js +1 -0
- package/.next/server/app/demo.html +1 -0
- package/.next/server/app/demo.meta +6 -0
- package/.next/server/app/demo.rsc +7 -0
- package/.next/server/app/favicon.ico/route.js +12 -0
- package/.next/server/app/favicon.ico/route.js.nft.json +1 -0
- package/.next/server/app/favicon.ico.body +0 -0
- package/.next/server/app/favicon.ico.meta +1 -0
- package/.next/server/app/index.html +1 -0
- package/.next/server/app/index.meta +5 -0
- package/.next/server/app/index.rsc +7 -0
- package/.next/server/app/page.js +1 -0
- package/.next/server/app/page.js.nft.json +1 -0
- package/.next/server/app/page_client-reference-manifest.js +1 -0
- package/.next/server/app-paths-manifest.json +34 -0
- package/.next/server/chunks/348.js +1 -0
- package/.next/server/chunks/471.js +2 -0
- package/.next/server/chunks/590.js +7 -0
- package/.next/server/chunks/621.js +1 -0
- package/.next/server/chunks/682.js +6 -0
- package/.next/server/chunks/904.js +1 -0
- package/.next/server/chunks/948.js +2 -0
- package/.next/server/chunks/972.js +12 -0
- package/.next/server/chunks/font-manifest.json +1 -0
- package/.next/server/font-manifest.json +1 -0
- package/.next/server/functions-config-manifest.json +1 -0
- package/.next/server/interception-route-rewrite-manifest.js +1 -0
- package/.next/server/middleware-build-manifest.js +1 -0
- package/.next/server/middleware-manifest.json +6 -0
- package/.next/server/middleware-react-loadable-manifest.js +1 -0
- package/.next/server/next-font-manifest.js +1 -0
- package/.next/server/next-font-manifest.json +1 -0
- package/.next/server/pages/404.html +1 -0
- package/.next/server/pages/500.html +1 -0
- package/.next/server/pages/_app.js +1 -0
- package/.next/server/pages/_app.js.nft.json +1 -0
- package/.next/server/pages/_document.js +1 -0
- package/.next/server/pages/_document.js.nft.json +1 -0
- package/.next/server/pages/_error.js +1 -0
- package/.next/server/pages/_error.js.nft.json +1 -0
- package/.next/server/pages-manifest.json +1 -0
- package/.next/server/server-reference-manifest.js +1 -0
- package/.next/server/server-reference-manifest.json +1 -0
- package/.next/server/webpack-runtime.js +1 -0
- package/.next/static/chunks/117-0f4ac3c2c1871d4c.js +2 -0
- package/.next/static/chunks/171-f72ed164561ba218.js +1 -0
- package/.next/static/chunks/235.aef4cdc783360b73.js +1 -0
- package/.next/static/chunks/30d07d85.9045c666bb43d9e1.js +3 -0
- package/.next/static/chunks/app/_not-found/page-8ff6572bc7c9bc61.js +1 -0
- package/.next/static/chunks/app/demo/page-6711b34cd2af0e4a.js +1 -0
- package/.next/static/chunks/app/layout-d058836232e72324.js +1 -0
- package/.next/static/chunks/app/page-cebeba7d6d08548a.js +1 -0
- package/.next/static/chunks/fd9d1056-26bd8d656b496dba.js +1 -0
- package/.next/static/chunks/framework-f66176bb897dc684.js +1 -0
- package/.next/static/chunks/main-960df1c7509a5a8b.js +1 -0
- package/.next/static/chunks/main-app-4b855ea14675b961.js +1 -0
- package/.next/static/chunks/pages/_app-72b849fbd24ac258.js +1 -0
- package/.next/static/chunks/pages/_error-7ba65e1336b92748.js +1 -0
- package/.next/static/chunks/polyfills-42372ed130431b0a.js +1 -0
- package/.next/static/chunks/webpack-1f89b7cb1fca69c4.js +1 -0
- package/.next/static/css/a537d3e86c0f36d2.css +3 -0
- package/.next/static/css/ab3e4b0f5035257a.css +1 -0
- package/.next/static/q-HMeHYZY36qljBJhsyS6/_buildManifest.js +1 -0
- package/.next/static/q-HMeHYZY36qljBJhsyS6/_ssgManifest.js +1 -0
- package/.next/trace +22 -0
- package/.next/types/app/api/automations/existing/route.ts +343 -0
- package/.next/types/app/api/backends/route.ts +343 -0
- package/.next/types/app/api/limits/route.ts +343 -0
- package/.next/types/app/api/meetings/[id]/route.ts +343 -0
- package/.next/types/app/api/meetings/[id]/slack/route.ts +343 -0
- package/.next/types/app/api/meetings/library/route.ts +343 -0
- package/.next/types/app/api/meetings/route.ts +343 -0
- package/.next/types/app/api/models/route.ts +343 -0
- package/.next/types/app/api/notes/route.ts +343 -0
- package/.next/types/app/api/orgs/[id]/route.ts +343 -0
- package/.next/types/app/api/orgs/route.ts +343 -0
- package/.next/types/app/api/prs/inbox/route.ts +343 -0
- package/.next/types/app/api/prs/refresh/route.ts +343 -0
- package/.next/types/app/api/prs/route.ts +343 -0
- package/.next/types/app/api/session-names/route.ts +343 -0
- package/.next/types/app/api/sessions/[id]/route.ts +343 -0
- package/.next/types/app/api/sessions/route.ts +343 -0
- package/.next/types/app/api/settings/auth/route.ts +343 -0
- package/.next/types/app/api/settings/route.ts +343 -0
- package/.next/types/app/api/settings/transcription/route.ts +343 -0
- package/.next/types/app/api/skills/[name]/route.ts +343 -0
- package/.next/types/app/api/skills/marketplace/route.ts +343 -0
- package/.next/types/app/api/skills/route.ts +343 -0
- package/.next/types/app/api/skills/view/route.ts +343 -0
- package/.next/types/app/api/slack/channels/route.ts +343 -0
- package/.next/types/app/api/slack/configured/route.ts +343 -0
- package/.next/types/app/api/slack/inbox/route.ts +343 -0
- package/.next/types/app/api/usage/route.ts +343 -0
- package/.next/types/app/demo/page.ts +79 -0
- package/.next/types/app/layout.ts +79 -0
- package/.next/types/app/page.ts +79 -0
- package/.next/types/package.json +1 -0
- package/.server-dist/server.js +554 -0
- package/.server-dist/src/lib/activeOrg.js +11 -0
- package/.server-dist/src/lib/audioCapture.js +157 -0
- package/.server-dist/src/lib/backends.js +111 -0
- package/.server-dist/src/lib/claudeAuth.js +57 -0
- package/.server-dist/src/lib/configFile.js +23 -0
- package/.server-dist/src/lib/dataDir.js +7 -0
- package/.server-dist/src/lib/existingAutomations.js +132 -0
- package/.server-dist/src/lib/ghInbox.js +151 -0
- package/.server-dist/src/lib/ghPrStatus.js +86 -0
- package/.server-dist/src/lib/gitUtils.js +27 -0
- package/.server-dist/src/lib/keepAwake.js +32 -0
- package/.server-dist/src/lib/liveSessionStore.js +89 -0
- package/.server-dist/src/lib/marketplace.js +63 -0
- package/.server-dist/src/lib/meetingAssistant.js +138 -0
- package/.server-dist/src/lib/meetingConfig.js +81 -0
- package/.server-dist/src/lib/meetingContext.js +82 -0
- package/.server-dist/src/lib/meetingManager.js +246 -0
- package/.server-dist/src/lib/meetingStore.js +198 -0
- package/.server-dist/src/lib/modelCatalog.js +107 -0
- package/.server-dist/src/lib/notesStore.js +67 -0
- package/.server-dist/src/lib/orgFilter.js +19 -0
- package/.server-dist/src/lib/orgParam.js +15 -0
- package/.server-dist/src/lib/orgRegistry.js +93 -0
- package/.server-dist/src/lib/pipelineView.js +62 -0
- package/.server-dist/src/lib/pipelines.js +401 -0
- package/.server-dist/src/lib/planLimits.js +101 -0
- package/.server-dist/src/lib/prDetector.js +16 -0
- package/.server-dist/src/lib/prScanCache.js +144 -0
- package/.server-dist/src/lib/prSessionEntries.js +96 -0
- package/.server-dist/src/lib/prTypes.js +2 -0
- package/.server-dist/src/lib/ptyManager.js +428 -0
- package/.server-dist/src/lib/reminders.js +10 -0
- package/.server-dist/src/lib/requestGuard.js +29 -0
- package/.server-dist/src/lib/sessionNameStore.js +69 -0
- package/.server-dist/src/lib/sessionStore.js +215 -0
- package/.server-dist/src/lib/settingsStore.js +51 -0
- package/.server-dist/src/lib/skillsReader.js +200 -0
- package/.server-dist/src/lib/slackBridge.js +270 -0
- package/.server-dist/src/lib/slackInbox.js +96 -0
- package/.server-dist/src/lib/slackWatchers.js +210 -0
- package/.server-dist/src/lib/statusDetector.js +46 -0
- package/.server-dist/src/lib/transcriber.js +61 -0
- package/.server-dist/src/lib/usageStore.js +254 -0
- package/.server-dist/tsconfig.server.tsbuildinfo +1 -0
- package/LICENSE +21 -0
- package/README.md +99 -2
- package/bin/polyergon.js +34 -0
- package/next.config.mjs +4 -0
- package/package.json +81 -4
- package/public/.gitkeep +0 -0
- package/public/apple-touch-icon.png +0 -0
- package/public/icon-192.png +0 -0
- package/public/icon-512.png +0 -0
- package/public/icon.svg +1 -0
- package/public/manifest.webmanifest +15 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transcribeChunk = transcribeChunk;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const audioCapture_1 = require("./audioCapture");
|
|
6
|
+
const MAX_ATTEMPTS = 3;
|
|
7
|
+
function requestFor(config, buf, prompt) {
|
|
8
|
+
const form = new FormData();
|
|
9
|
+
form.append('file', new Blob([new Uint8Array(buf)], { type: 'audio/wav' }), 'chunk.wav');
|
|
10
|
+
// Prior transcript as context — keeps language and terminology consistent
|
|
11
|
+
// across chunks (crucial for short chunks and mixed Hebrew/English)
|
|
12
|
+
if (prompt)
|
|
13
|
+
form.append('prompt', prompt);
|
|
14
|
+
if (config.kind === 'openai') {
|
|
15
|
+
form.append('model', 'whisper-1');
|
|
16
|
+
return {
|
|
17
|
+
url: 'https://api.openai.com/v1/audio/transcriptions',
|
|
18
|
+
headers: { Authorization: `Bearer ${config.apiKey}` },
|
|
19
|
+
form,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
url: `${config.endpoint}/openai/deployments/${config.deployment}/audio/transcriptions?api-version=2025-03-01-preview`,
|
|
24
|
+
headers: { 'api-key': config.apiKey },
|
|
25
|
+
form,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
// Transcribes one WAV chunk. Returns null for silence (checked locally, no
|
|
29
|
+
// API call) or empty results. Throws after retries are exhausted — the caller
|
|
30
|
+
// records a gap and moves on.
|
|
31
|
+
async function transcribeChunk(filePath, config, prompt = '') {
|
|
32
|
+
var _a;
|
|
33
|
+
const buf = (0, fs_1.readFileSync)(filePath);
|
|
34
|
+
if ((0, audioCapture_1.wavRms)(buf) < audioCapture_1.SILENCE_RMS_THRESHOLD)
|
|
35
|
+
return null;
|
|
36
|
+
let lastError;
|
|
37
|
+
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
|
|
38
|
+
if (attempt > 0)
|
|
39
|
+
await new Promise(r => setTimeout(r, 1000 * 2 ** attempt));
|
|
40
|
+
try {
|
|
41
|
+
const { url, headers, form } = requestFor(config, buf, prompt);
|
|
42
|
+
// Hard timeout — without it a hung transcription request never resolves,
|
|
43
|
+
// and since meeting stop() waits for the chunk queue to drain, one stalled
|
|
44
|
+
// fetch freezes meeting teardown (and anything waiting on it) indefinitely.
|
|
45
|
+
const res = await fetch(url, { method: 'POST', headers, body: form, signal: AbortSignal.timeout(30000) });
|
|
46
|
+
if (res.status === 400) {
|
|
47
|
+
// Bad audio (e.g. truncated final chunk) won't get better on retry
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
if (!res.ok)
|
|
51
|
+
throw new Error(`Transcription API ${res.status}: ${(await res.text()).slice(0, 200)}`);
|
|
52
|
+
const data = await res.json();
|
|
53
|
+
const text = (_a = data.text) === null || _a === void 0 ? void 0 : _a.trim();
|
|
54
|
+
return text || null;
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
lastError = e;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
throw lastError;
|
|
61
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
3
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
4
|
+
var m = o[Symbol.asyncIterator], i;
|
|
5
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
6
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
7
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
8
|
+
};
|
|
9
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
10
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
11
|
+
};
|
|
12
|
+
var _a;
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.usageScanner = exports.UsageScanner = void 0;
|
|
15
|
+
const dataDir_1 = require("./dataDir");
|
|
16
|
+
const fs_1 = require("fs");
|
|
17
|
+
const readline_1 = require("readline");
|
|
18
|
+
const path_1 = __importDefault(require("path"));
|
|
19
|
+
const orgRegistry_1 = require("./orgRegistry");
|
|
20
|
+
// Estimated API-equivalent pricing, $ per MTok. Cache reads bill ~0.1× input,
|
|
21
|
+
// cache writes ~1.25× input (5-minute TTL).
|
|
22
|
+
const PRICING = [
|
|
23
|
+
[/fable|mythos/i, { input: 10, output: 50 }],
|
|
24
|
+
[/opus/i, { input: 5, output: 25 }],
|
|
25
|
+
[/sonnet/i, { input: 3, output: 15 }],
|
|
26
|
+
[/haiku/i, { input: 1, output: 5 }],
|
|
27
|
+
];
|
|
28
|
+
function priceFor(model) {
|
|
29
|
+
var _a, _b;
|
|
30
|
+
return (_b = (_a = PRICING.find(([re]) => re.test(model))) === null || _a === void 0 ? void 0 : _a[1]) !== null && _b !== void 0 ? _b : { input: 5, output: 25 };
|
|
31
|
+
}
|
|
32
|
+
const HOUR = 3600000;
|
|
33
|
+
const PRUNE_MS = 8 * 24 * HOUR;
|
|
34
|
+
const DEFAULT_CACHE_FILE = path_1.default.join(dataDir_1.DATA_DIR, 'usage-cache.json');
|
|
35
|
+
function emptyUsage() {
|
|
36
|
+
return {
|
|
37
|
+
tokens: { input: 0, output: 0, cacheRead: 0, cacheCreation: 0 },
|
|
38
|
+
costUSD: 0,
|
|
39
|
+
requests: 0,
|
|
40
|
+
lastActivity: 0,
|
|
41
|
+
contextTokens: 0,
|
|
42
|
+
hourlyCost: {},
|
|
43
|
+
hourlyOutput: {},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
// Streams one transcript and aggregates its assistant-message usage.
|
|
47
|
+
// Claude Code writes the same message id on multiple lines as content
|
|
48
|
+
// streams in (usage repeated) — dedupe by message id, last write wins.
|
|
49
|
+
async function scanFile(filePath) {
|
|
50
|
+
var _a, e_1, _b, _c;
|
|
51
|
+
var _d, _e, _f, _g, _h;
|
|
52
|
+
const byMessage = new Map();
|
|
53
|
+
const rl = (0, readline_1.createInterface)({ input: (0, fs_1.createReadStream)(filePath, { encoding: 'utf-8' }), crlfDelay: Infinity });
|
|
54
|
+
try {
|
|
55
|
+
for (var _j = true, rl_1 = __asyncValues(rl), rl_1_1; rl_1_1 = await rl_1.next(), _a = rl_1_1.done, !_a; _j = true) {
|
|
56
|
+
_c = rl_1_1.value;
|
|
57
|
+
_j = false;
|
|
58
|
+
const line = _c;
|
|
59
|
+
if (!line.includes('"assistant"') || !line.includes('"usage"'))
|
|
60
|
+
continue;
|
|
61
|
+
try {
|
|
62
|
+
const entry = JSON.parse(line);
|
|
63
|
+
const msg = entry.message;
|
|
64
|
+
const u = msg === null || msg === void 0 ? void 0 : msg.usage;
|
|
65
|
+
if (!u || !(msg === null || msg === void 0 ? void 0 : msg.id))
|
|
66
|
+
continue;
|
|
67
|
+
const model = (_d = msg.model) !== null && _d !== void 0 ? _d : '';
|
|
68
|
+
if (model === '<synthetic>')
|
|
69
|
+
continue;
|
|
70
|
+
const p = priceFor(model);
|
|
71
|
+
const input = (_e = u.input_tokens) !== null && _e !== void 0 ? _e : 0;
|
|
72
|
+
const output = (_f = u.output_tokens) !== null && _f !== void 0 ? _f : 0;
|
|
73
|
+
const cacheRead = (_g = u.cache_read_input_tokens) !== null && _g !== void 0 ? _g : 0;
|
|
74
|
+
const cacheCreation = (_h = u.cache_creation_input_tokens) !== null && _h !== void 0 ? _h : 0;
|
|
75
|
+
const cost = (input * p.input + output * p.output + cacheRead * 0.1 * p.input + cacheCreation * 1.25 * p.input) / 1000000;
|
|
76
|
+
const t = entry.timestamp ? Date.parse(entry.timestamp) : 0;
|
|
77
|
+
byMessage.set(msg.id, { usage: { input, output, cacheRead, cacheCreation, cost }, t: Number.isNaN(t) ? 0 : t });
|
|
78
|
+
}
|
|
79
|
+
catch ( /* malformed line */_k) { /* malformed line */ }
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
83
|
+
finally {
|
|
84
|
+
try {
|
|
85
|
+
if (!_j && !_a && (_b = rl_1.return)) await _b.call(rl_1);
|
|
86
|
+
}
|
|
87
|
+
finally { if (e_1) throw e_1.error; }
|
|
88
|
+
}
|
|
89
|
+
const usage = emptyUsage();
|
|
90
|
+
byMessage.forEach(({ usage: u, t }) => {
|
|
91
|
+
var _a, _b;
|
|
92
|
+
usage.tokens.input += u.input;
|
|
93
|
+
usage.tokens.output += u.output;
|
|
94
|
+
usage.tokens.cacheRead += u.cacheRead;
|
|
95
|
+
usage.tokens.cacheCreation += u.cacheCreation;
|
|
96
|
+
usage.costUSD += u.cost;
|
|
97
|
+
usage.requests += 1;
|
|
98
|
+
if (t >= usage.lastActivity)
|
|
99
|
+
usage.contextTokens = u.input + u.cacheRead + u.cacheCreation;
|
|
100
|
+
if (t > usage.lastActivity)
|
|
101
|
+
usage.lastActivity = t;
|
|
102
|
+
if (t > 0) {
|
|
103
|
+
const bucket = String(Math.floor(t / HOUR) * HOUR);
|
|
104
|
+
usage.hourlyCost[bucket] = ((_a = usage.hourlyCost[bucket]) !== null && _a !== void 0 ? _a : 0) + u.cost;
|
|
105
|
+
usage.hourlyOutput[bucket] = ((_b = usage.hourlyOutput[bucket]) !== null && _b !== void 0 ? _b : 0) + u.output;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return usage;
|
|
109
|
+
}
|
|
110
|
+
function pruneHourly(usage) {
|
|
111
|
+
const cutoff = Date.now() - PRUNE_MS;
|
|
112
|
+
for (const key of Object.keys(usage.hourlyCost)) {
|
|
113
|
+
if (Number(key) < cutoff) {
|
|
114
|
+
delete usage.hourlyCost[key];
|
|
115
|
+
delete usage.hourlyOutput[key];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
class UsageScanner {
|
|
120
|
+
constructor(cacheFile = DEFAULT_CACHE_FILE) {
|
|
121
|
+
this.cache = new Map(); // key: sessionId
|
|
122
|
+
this.loaded = false;
|
|
123
|
+
this.lastReport = null;
|
|
124
|
+
this.inFlight = null;
|
|
125
|
+
this.cacheFile = cacheFile;
|
|
126
|
+
}
|
|
127
|
+
async load() {
|
|
128
|
+
if (this.loaded)
|
|
129
|
+
return;
|
|
130
|
+
this.loaded = true;
|
|
131
|
+
try {
|
|
132
|
+
const raw = await fs_1.promises.readFile(this.cacheFile, 'utf-8');
|
|
133
|
+
this.cache = new Map(Object.entries(JSON.parse(raw)));
|
|
134
|
+
}
|
|
135
|
+
catch ( /* no cache yet */_a) { /* no cache yet */ }
|
|
136
|
+
}
|
|
137
|
+
async persist() {
|
|
138
|
+
try {
|
|
139
|
+
await fs_1.promises.mkdir(path_1.default.dirname(this.cacheFile), { recursive: true });
|
|
140
|
+
await fs_1.promises.writeFile(this.cacheFile, JSON.stringify(Object.fromEntries(this.cache)));
|
|
141
|
+
}
|
|
142
|
+
catch ( /* best-effort */_a) { /* best-effort */ }
|
|
143
|
+
}
|
|
144
|
+
async report(ttlMs = 60000) {
|
|
145
|
+
if (this.lastReport && Date.now() - this.lastReport.scannedAt < ttlMs)
|
|
146
|
+
return this.lastReport;
|
|
147
|
+
if (!this.inFlight) {
|
|
148
|
+
this.inFlight = this.buildReport()
|
|
149
|
+
.then(r => { this.lastReport = r; return r; })
|
|
150
|
+
.finally(() => { this.inFlight = null; });
|
|
151
|
+
}
|
|
152
|
+
return this.inFlight;
|
|
153
|
+
}
|
|
154
|
+
async buildReport() {
|
|
155
|
+
var _a, _b, _c;
|
|
156
|
+
await this.load();
|
|
157
|
+
const sessions = {};
|
|
158
|
+
const orgs = {};
|
|
159
|
+
let cacheChanged = false;
|
|
160
|
+
const now = Date.now();
|
|
161
|
+
const todayStart = new Date(now);
|
|
162
|
+
todayStart.setHours(0, 0, 0, 0);
|
|
163
|
+
const dayStarts = Array.from({ length: 7 }, (_, i) => todayStart.getTime() - (6 - i) * 24 * HOUR);
|
|
164
|
+
for (const { orgId, root } of orgRegistry_1.orgRegistry.projectRoots()) {
|
|
165
|
+
let projectDirs;
|
|
166
|
+
try {
|
|
167
|
+
projectDirs = await fs_1.promises.readdir(root);
|
|
168
|
+
}
|
|
169
|
+
catch (_d) {
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
const orgUsage = (_a = orgs[orgId]) !== null && _a !== void 0 ? _a : {
|
|
173
|
+
last5h: { costUSD: 0, outputTokens: 0 },
|
|
174
|
+
last24h: { costUSD: 0, outputTokens: 0 },
|
|
175
|
+
last7d: { costUSD: 0, outputTokens: 0 },
|
|
176
|
+
totalCostUSD: 0,
|
|
177
|
+
totalTokens: { input: 0, output: 0, cacheRead: 0, cacheCreation: 0 },
|
|
178
|
+
sessions: 0,
|
|
179
|
+
daily: dayStarts.map(dayStart => ({ dayStart, costUSD: 0 })),
|
|
180
|
+
};
|
|
181
|
+
orgs[orgId] = orgUsage;
|
|
182
|
+
for (const projectDir of projectDirs) {
|
|
183
|
+
let files;
|
|
184
|
+
try {
|
|
185
|
+
files = await fs_1.promises.readdir(path_1.default.join(root, projectDir));
|
|
186
|
+
}
|
|
187
|
+
catch (_e) {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
for (const file of files) {
|
|
191
|
+
if (!file.endsWith('.jsonl'))
|
|
192
|
+
continue;
|
|
193
|
+
const sessionId = file.replace('.jsonl', '');
|
|
194
|
+
const filePath = path_1.default.join(root, projectDir, file);
|
|
195
|
+
let stat;
|
|
196
|
+
try {
|
|
197
|
+
stat = await fs_1.promises.stat(filePath);
|
|
198
|
+
}
|
|
199
|
+
catch (_f) {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
let entry = this.cache.get(sessionId);
|
|
203
|
+
// `contextTokens === undefined` = entry cached before the field existed → rescan once
|
|
204
|
+
if (!entry || entry.mtimeMs !== stat.mtimeMs || entry.size !== stat.size || entry.usage.contextTokens === undefined) {
|
|
205
|
+
try {
|
|
206
|
+
entry = { mtimeMs: stat.mtimeMs, size: stat.size, usage: await scanFile(filePath) };
|
|
207
|
+
}
|
|
208
|
+
catch (_g) {
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
this.cache.set(sessionId, entry);
|
|
212
|
+
cacheChanged = true;
|
|
213
|
+
}
|
|
214
|
+
pruneHourly(entry.usage);
|
|
215
|
+
const u = entry.usage;
|
|
216
|
+
if (u.requests === 0)
|
|
217
|
+
continue;
|
|
218
|
+
sessions[sessionId] = { costUSD: u.costUSD, tokens: u.tokens, requests: u.requests, contextTokens: (_b = u.contextTokens) !== null && _b !== void 0 ? _b : 0, orgId };
|
|
219
|
+
orgUsage.sessions += 1;
|
|
220
|
+
orgUsage.totalCostUSD += u.costUSD;
|
|
221
|
+
orgUsage.totalTokens.input += u.tokens.input;
|
|
222
|
+
orgUsage.totalTokens.output += u.tokens.output;
|
|
223
|
+
orgUsage.totalTokens.cacheRead += u.tokens.cacheRead;
|
|
224
|
+
orgUsage.totalTokens.cacheCreation += u.tokens.cacheCreation;
|
|
225
|
+
for (const [bucket, cost] of Object.entries(u.hourlyCost)) {
|
|
226
|
+
const t = Number(bucket);
|
|
227
|
+
const age = now - t;
|
|
228
|
+
const out = (_c = u.hourlyOutput[bucket]) !== null && _c !== void 0 ? _c : 0;
|
|
229
|
+
if (age < 5 * HOUR) {
|
|
230
|
+
orgUsage.last5h.costUSD += cost;
|
|
231
|
+
orgUsage.last5h.outputTokens += out;
|
|
232
|
+
}
|
|
233
|
+
if (age < 24 * HOUR) {
|
|
234
|
+
orgUsage.last24h.costUSD += cost;
|
|
235
|
+
orgUsage.last24h.outputTokens += out;
|
|
236
|
+
}
|
|
237
|
+
if (age < 7 * 24 * HOUR) {
|
|
238
|
+
orgUsage.last7d.costUSD += cost;
|
|
239
|
+
orgUsage.last7d.outputTokens += out;
|
|
240
|
+
}
|
|
241
|
+
const day = orgUsage.daily.find(d => t >= d.dayStart && t < d.dayStart + 24 * HOUR);
|
|
242
|
+
if (day)
|
|
243
|
+
day.costUSD += cost;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (cacheChanged)
|
|
249
|
+
await this.persist();
|
|
250
|
+
return { sessions, orgs, scannedAt: Date.now() };
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
exports.UsageScanner = UsageScanner;
|
|
254
|
+
exports.usageScanner = (_a = global.__usageScanner) !== null && _a !== void 0 ? _a : (global.__usageScanner = new UsageScanner());
|