agent-cli-proxy 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.
@@ -0,0 +1,41 @@
1
+ CREATE TABLE IF NOT EXISTS request_logs (
2
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
3
+ provider TEXT NOT NULL,
4
+ model TEXT NOT NULL,
5
+ tool TEXT DEFAULT 'unknown',
6
+ client_id TEXT DEFAULT 'unknown',
7
+ path TEXT NOT NULL,
8
+ streamed INTEGER NOT NULL DEFAULT 0,
9
+ status INTEGER,
10
+ prompt_tokens INTEGER DEFAULT 0,
11
+ completion_tokens INTEGER DEFAULT 0,
12
+ cache_creation_tokens INTEGER DEFAULT 0,
13
+ cache_read_tokens INTEGER DEFAULT 0,
14
+ total_tokens INTEGER DEFAULT 0,
15
+ cost_usd REAL DEFAULT 0,
16
+ incomplete INTEGER NOT NULL DEFAULT 0,
17
+ error_code TEXT,
18
+ latency_ms INTEGER,
19
+ started_at TEXT NOT NULL,
20
+ finished_at TEXT,
21
+ meta_json TEXT
22
+ );
23
+
24
+ CREATE TABLE IF NOT EXISTS daily_usage (
25
+ day TEXT NOT NULL,
26
+ provider TEXT NOT NULL,
27
+ model TEXT NOT NULL,
28
+ request_count INTEGER DEFAULT 0,
29
+ prompt_tokens INTEGER DEFAULT 0,
30
+ completion_tokens INTEGER DEFAULT 0,
31
+ cache_creation_tokens INTEGER DEFAULT 0,
32
+ cache_read_tokens INTEGER DEFAULT 0,
33
+ total_tokens INTEGER DEFAULT 0,
34
+ cost_usd REAL DEFAULT 0,
35
+ PRIMARY KEY (day, provider, model)
36
+ );
37
+
38
+ CREATE INDEX IF NOT EXISTS idx_request_logs_started_at ON request_logs(started_at);
39
+ CREATE INDEX IF NOT EXISTS idx_request_logs_tool ON request_logs(tool);
40
+ CREATE INDEX IF NOT EXISTS idx_request_logs_client_id ON request_logs(client_id);
41
+ CREATE INDEX IF NOT EXISTS idx_daily_usage_day ON daily_usage(day);
@@ -0,0 +1,25 @@
1
+ -- Add agent attribution columns to request_logs
2
+ ALTER TABLE request_logs ADD COLUMN IF NOT EXISTS agent TEXT;
3
+ ALTER TABLE request_logs ADD COLUMN IF NOT EXISTS source TEXT DEFAULT 'proxy';
4
+ ALTER TABLE request_logs ADD COLUMN IF NOT EXISTS msg_id TEXT;
5
+
6
+ -- Unique index for dedup (only for non-null msg_id)
7
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_request_logs_msg_id
8
+ ON request_logs(msg_id) WHERE msg_id IS NOT NULL;
9
+
10
+ -- Quota snapshots table
11
+ CREATE TABLE IF NOT EXISTS quota_snapshots (
12
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
13
+ timestamp TEXT NOT NULL,
14
+ provider TEXT NOT NULL,
15
+ account TEXT NOT NULL,
16
+ quota_type TEXT NOT NULL,
17
+ used_pct REAL,
18
+ remaining REAL,
19
+ remaining_raw TEXT,
20
+ resets_at TEXT,
21
+ raw_json TEXT
22
+ );
23
+
24
+ CREATE INDEX IF NOT EXISTS idx_quota_snapshots_provider
25
+ ON quota_snapshots(provider, account, timestamp);
@@ -0,0 +1,3 @@
1
+ ALTER TABLE request_logs ADD COLUMN actual_model TEXT;
2
+ ALTER TABLE request_logs ADD COLUMN source_ip TEXT;
3
+ ALTER TABLE request_logs ADD COLUMN user_agent TEXT;
@@ -0,0 +1,40 @@
1
+ -- Add CLIProxyAPI account attribution
2
+ ALTER TABLE request_logs ADD COLUMN cliproxy_account TEXT;
3
+ ALTER TABLE request_logs ADD COLUMN cliproxy_auth_index TEXT;
4
+ ALTER TABLE request_logs ADD COLUMN cliproxy_source TEXT;
5
+ ALTER TABLE request_logs ADD COLUMN request_id TEXT;
6
+ ALTER TABLE request_logs ADD COLUMN reasoning_tokens INTEGER DEFAULT 0;
7
+ ALTER TABLE request_logs ADD COLUMN actual_model TEXT;
8
+ ALTER TABLE request_logs ADD COLUMN user_agent TEXT;
9
+ ALTER TABLE request_logs ADD COLUMN source_ip TEXT;
10
+ ALTER TABLE request_logs ADD COLUMN correlated_at TEXT;
11
+
12
+ CREATE INDEX IF NOT EXISTS idx_request_logs_cliproxy_account
13
+ ON request_logs(cliproxy_account);
14
+ CREATE INDEX IF NOT EXISTS idx_request_logs_cliproxy_auth_index
15
+ ON request_logs(cliproxy_auth_index);
16
+ CREATE INDEX IF NOT EXISTS idx_request_logs_request_id
17
+ ON request_logs(request_id);
18
+
19
+ -- Daily usage breakdown by cliproxy account
20
+ CREATE TABLE IF NOT EXISTS daily_account_usage (
21
+ day TEXT NOT NULL,
22
+ provider TEXT NOT NULL,
23
+ model TEXT NOT NULL,
24
+ cliproxy_account TEXT NOT NULL,
25
+ cliproxy_auth_index TEXT,
26
+ request_count INTEGER DEFAULT 0,
27
+ prompt_tokens INTEGER DEFAULT 0,
28
+ completion_tokens INTEGER DEFAULT 0,
29
+ cache_creation_tokens INTEGER DEFAULT 0,
30
+ cache_read_tokens INTEGER DEFAULT 0,
31
+ reasoning_tokens INTEGER DEFAULT 0,
32
+ total_tokens INTEGER DEFAULT 0,
33
+ cost_usd REAL DEFAULT 0,
34
+ PRIMARY KEY (day, provider, model, cliproxy_account)
35
+ );
36
+
37
+ CREATE INDEX IF NOT EXISTS idx_daily_account_usage_day
38
+ ON daily_account_usage(day);
39
+ CREATE INDEX IF NOT EXISTS idx_daily_account_usage_account
40
+ ON daily_account_usage(cliproxy_account);
@@ -0,0 +1,39 @@
1
+ ALTER TABLE request_logs ADD COLUMN lifecycle_status TEXT NOT NULL DEFAULT 'pending' CHECK(lifecycle_status IN ('pending', 'completed', 'error', 'aborted'));
2
+ ALTER TABLE request_logs ADD COLUMN cost_status TEXT NOT NULL DEFAULT 'unresolved' CHECK(cost_status IN ('unresolved', 'ok', 'pending', 'unsupported'));
3
+ ALTER TABLE request_logs ADD COLUMN subscription_code TEXT;
4
+ ALTER TABLE request_logs ADD COLUMN finalized_at TEXT;
5
+ ALTER TABLE request_logs ADD COLUMN error_message TEXT;
6
+
7
+ UPDATE request_logs
8
+ SET lifecycle_status = CASE
9
+ WHEN incomplete = 1
10
+ OR error_code IS NOT NULL
11
+ OR status >= 400 THEN 'error'
12
+ ELSE 'completed'
13
+ END,
14
+ finalized_at = COALESCE(finished_at, started_at),
15
+ cost_status = CASE
16
+ WHEN cost_usd > 0 THEN 'ok'
17
+ ELSE 'pending'
18
+ END;
19
+
20
+ CREATE INDEX IF NOT EXISTS idx_request_logs_lifecycle_status
21
+ ON request_logs(lifecycle_status);
22
+ CREATE INDEX IF NOT EXISTS idx_request_logs_cost_status
23
+ ON request_logs(cost_status);
24
+ CREATE INDEX IF NOT EXISTS idx_request_logs_subscription_code
25
+ ON request_logs(subscription_code) WHERE subscription_code IS NOT NULL;
26
+
27
+ CREATE TABLE IF NOT EXISTS cost_audit (
28
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
29
+ request_log_id INTEGER,
30
+ model TEXT,
31
+ provider TEXT,
32
+ source TEXT,
33
+ base_cost_usd REAL,
34
+ calc_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
35
+ FOREIGN KEY (request_log_id) REFERENCES request_logs(id)
36
+ );
37
+
38
+ CREATE INDEX IF NOT EXISTS idx_cost_audit_request_log_id
39
+ ON cost_audit(request_log_id);
@@ -0,0 +1,8 @@
1
+ CREATE TABLE IF NOT EXISTS account_subscriptions (
2
+ cliproxy_account TEXT PRIMARY KEY,
3
+ subscription_code TEXT NOT NULL,
4
+ bound_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
5
+ );
6
+
7
+ CREATE INDEX IF NOT EXISTS idx_account_subscriptions_subscription_code
8
+ ON account_subscriptions(subscription_code);
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "agent-cli-proxy",
3
+ "version": "0.1.0",
4
+ "description": "Self-hosted AI API proxy with per-tool usage monitoring, usage attribution, cost tracking, and subscription metadata for OpenAI/Anthropic-compatible APIs.",
5
+ "module": "src/index.ts",
6
+ "type": "module",
7
+ "private": false,
8
+ "author": "Agent CLI Proxy contributors",
9
+ "license": "MIT",
10
+ "homepage": "https://github.com/INONONO66/agent-cli-proxy",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/INONONO66/agent-cli-proxy.git"
14
+ },
15
+ "bugs": {
16
+ "url": "https://github.com/INONONO66/agent-cli-proxy/issues"
17
+ },
18
+ "keywords": [
19
+ "ai",
20
+ "proxy",
21
+ "anthropic",
22
+ "openai",
23
+ "claude",
24
+ "cli",
25
+ "usage-tracking",
26
+ "cost-tracking",
27
+ "self-hosted"
28
+ ],
29
+ "engines": {
30
+ "bun": ">=1.0.0",
31
+ "node": ">=20.0.0"
32
+ },
33
+ "bin": {
34
+ "agent-cli-proxy": "./dist/cli.js"
35
+ },
36
+ "files": [
37
+ "dist",
38
+ "README.md",
39
+ "LICENSE"
40
+ ],
41
+ "scripts": {
42
+ "dev": "bun run --watch src/index.ts",
43
+ "build": "bun build src/index.ts src/cli.ts --target=bun --outdir=./dist && rm -rf dist/migrations && cp -R src/storage/migrations dist/migrations && mkdir -p dist/data && cp data/plans.default.json dist/data/plans.default.json && bun run scripts/post-build.ts",
44
+ "typecheck": "bunx tsc --noEmit",
45
+ "test": "bun test",
46
+ "release-check": "bun run typecheck && bun run test && bun run build && (bunx publint dist || bun run scripts/release-fallback-check.ts)",
47
+ "prepublishOnly": "bun run release-check",
48
+ "pack:check": "bun pm pack --dry-run",
49
+ "test:e2e": "bun test tests/e2e/",
50
+ "test:e2e:mock": "MOCK_CLI_PROXY=1 CLI_PROXY_API_URL=http://localhost:18317 bun test tests/e2e/",
51
+ "start": "bun run dist/index.js",
52
+ "backfill:costs": "bun run src/cli.ts backfill-costs --env .env"
53
+ },
54
+ "devDependencies": {
55
+ "@types/bun": "latest",
56
+ "@tokscale/core": "^1.4.3"
57
+ },
58
+ "peerDependencies": {
59
+ "typescript": "^5"
60
+ },
61
+ "dependencies": {}
62
+ }