claude-sessions 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.
Files changed (48) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +140 -0
  3. package/data/model-pricing.json +113 -0
  4. package/dist/cost-calculator.d.ts +49 -0
  5. package/dist/cost-calculator.d.ts.map +1 -0
  6. package/dist/cost-calculator.js +429 -0
  7. package/dist/cost-calculator.js.map +1 -0
  8. package/dist/index.d.ts +28 -0
  9. package/dist/index.d.ts.map +1 -0
  10. package/dist/index.js +65 -0
  11. package/dist/index.js.map +1 -0
  12. package/dist/projects-service.d.ts +106 -0
  13. package/dist/projects-service.d.ts.map +1 -0
  14. package/dist/projects-service.js +317 -0
  15. package/dist/projects-service.js.map +1 -0
  16. package/dist/session-cache-store.d.ts +48 -0
  17. package/dist/session-cache-store.d.ts.map +1 -0
  18. package/dist/session-cache-store.js +231 -0
  19. package/dist/session-cache-store.js.map +1 -0
  20. package/dist/session-cache.d.ts +266 -0
  21. package/dist/session-cache.d.ts.map +1 -0
  22. package/dist/session-cache.js +1294 -0
  23. package/dist/session-cache.js.map +1 -0
  24. package/dist/session-parser.d.ts +265 -0
  25. package/dist/session-parser.d.ts.map +1 -0
  26. package/dist/session-parser.js +555 -0
  27. package/dist/session-parser.js.map +1 -0
  28. package/dist/session-reader.d.ts +87 -0
  29. package/dist/session-reader.d.ts.map +1 -0
  30. package/dist/session-reader.js +279 -0
  31. package/dist/session-reader.js.map +1 -0
  32. package/dist/tasks-service.d.ts +100 -0
  33. package/dist/tasks-service.d.ts.map +1 -0
  34. package/dist/tasks-service.js +290 -0
  35. package/dist/tasks-service.js.map +1 -0
  36. package/dist/teams-service.d.ts +30 -0
  37. package/dist/teams-service.d.ts.map +1 -0
  38. package/dist/teams-service.js +85 -0
  39. package/dist/teams-service.js.map +1 -0
  40. package/dist/types.d.ts +87 -0
  41. package/dist/types.d.ts.map +1 -0
  42. package/dist/types.js +7 -0
  43. package/dist/types.js.map +1 -0
  44. package/dist/utils/path-utils.d.ts +80 -0
  45. package/dist/utils/path-utils.d.ts.map +1 -0
  46. package/dist/utils/path-utils.js +355 -0
  47. package/dist/utils/path-utils.js.map +1 -0
  48. package/package.json +42 -0
package/README.md ADDED
@@ -0,0 +1,140 @@
1
+ # claude-sessions
2
+
3
+ Read Claude Code session data (sessions, projects, tasks, teams, costs) without running lm-assist.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install claude-sessions
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import {
15
+ createSessionReader,
16
+ createSessionParser,
17
+ createSessionCache,
18
+ createProjectsService,
19
+ createTasksService,
20
+ createAgentTeamsService,
21
+ createCostCalculator,
22
+ } from 'claude-sessions';
23
+
24
+ // List all projects and sessions
25
+ const reader = createSessionReader();
26
+ const projects = reader.listProjects();
27
+ const sessions = reader.listSessions('/path/to/project');
28
+
29
+ // Parse a session with full detail
30
+ const parser = createSessionParser();
31
+ const session = await parser.parseSession('session-uuid', '/path/to/project');
32
+ console.log(session.userPrompts);
33
+ console.log(session.toolUses);
34
+ console.log(session.totalCostUsd);
35
+
36
+ // Get conversation in simplified format
37
+ const convo = await parser.getConversation({
38
+ sessionId: 'session-uuid',
39
+ toolDetail: 'summary',
40
+ includeThinking: true,
41
+ });
42
+
43
+ // LMDB-backed cache for fast repeated reads
44
+ const cache = createSessionCache();
45
+ const data = await cache.getSessionData('/path/to/session.jsonl');
46
+ const allSessions = cache.getAllSessionsFromCache();
47
+ cache.close();
48
+
49
+ // Projects with git info and cost
50
+ const projService = createProjectsService({ sessionCache: cache });
51
+ const projs = projService.listProjects();
52
+
53
+ // Tasks
54
+ const tasks = createTasksService();
55
+ const lists = await tasks.listTaskLists();
56
+ const taskList = await tasks.getTaskList('list-id');
57
+
58
+ // Teams
59
+ const teams = createAgentTeamsService();
60
+ const teamList = teams.listTeams();
61
+
62
+ // Cost calculation
63
+ const calc = createCostCalculator();
64
+ const cost = calc.calculateCost(
65
+ { inputTokens: 100000, outputTokens: 5000, cacheCreationInputTokens: 50000, cacheReadInputTokens: 200000 },
66
+ 'claude-sonnet-4'
67
+ );
68
+ console.log(calc.formatCost(cost.totalCost));
69
+ ```
70
+
71
+ ## API Overview
72
+
73
+ ### SessionReader
74
+ Low-level JSONL file discovery and reading from `~/.claude/projects/`.
75
+
76
+ - `listProjects()` — List all projects
77
+ - `listSessions(cwd?)` — List sessions for a project
78
+ - `listSessionsWithDetails(cwd?)` — Sessions with model/cost/status
79
+ - `readSessionLines(sessionId, cwd?)` — Raw JSONL lines
80
+ - `listSubagentFiles(sessionId?, cwd?)` — Subagent file metadata
81
+
82
+ ### SessionParser
83
+ Parse JSONL sessions into structured data. No server dependencies.
84
+
85
+ - `parseSession(sessionId, cwd?)` — Full session parse
86
+ - `getConversation(options)` — Simplified conversation format
87
+
88
+ ### SessionCache
89
+ LMDB-backed cache for fast repeated reads with incremental parsing.
90
+
91
+ - `getSessionData(filePath)` — Cached session data (async)
92
+ - `getSessionDataSync(filePath)` — Synchronous variant
93
+ - `getRawMessages(filePath)` — Raw parsed messages
94
+ - `getAllSessionsFromCache()` — All cached sessions
95
+ - `getProjectSessionsFromCache(projectPath)` — Project sessions from cache
96
+ - `getPerProjectCosts()` — Per-project cost totals
97
+ - `startWatching()` — File watcher (requires chokidar)
98
+ - `close()` — Shut down LMDB
99
+
100
+ ### ProjectsService
101
+ Project discovery with git info and cost metadata.
102
+
103
+ - `listProjects(options?)` — All projects with metadata
104
+ - `listProjectSessions(projectPath, options?)` — Sessions with enriched data
105
+ - `getGitInfo(projectPath)` — Git branch, remotes, commit
106
+
107
+ ### TasksService
108
+ Read Claude Code task files from `~/.claude/tasks/`.
109
+
110
+ - `listTaskLists()` — All task lists
111
+ - `getTaskList(listId)` — Tasks in a list
112
+ - `getReadyTasks(listId)` — Unblocked tasks
113
+ - `getAllTasksFlat()` — All tasks across all lists
114
+
115
+ ### AgentTeamsService
116
+ Read team configs from `~/.claude/teams/`.
117
+
118
+ - `listTeams()` — All teams
119
+ - `getTeam(teamName)` — Team config
120
+
121
+ ### CostCalculator
122
+ Token cost calculation with tiered pricing.
123
+
124
+ - `calculateCost(usage, model?)` — Calculate cost from token usage
125
+ - `getPricing(model)` — Get pricing for a model
126
+ - `formatCost(cost)` — Format as `$X.XX`
127
+
128
+ ### Path Utilities
129
+ - `legacyEncodeProjectPath(path)` — Dash-encoded path
130
+ - `encodePath(path)` / `decodePath(encoded)` — Base64 path encoding
131
+ - `getProjectsDir()` — `~/.claude/projects/`
132
+ - `extractProjectPath(storagePath)` — Extract project path from storage path
133
+
134
+ ## Optional Dependencies
135
+
136
+ - **chokidar** — File watching for live cache updates. Install with `npm install chokidar` if you need `SessionCache.startWatching()`.
137
+
138
+ ## License
139
+
140
+ MIT
@@ -0,0 +1,113 @@
1
+ [
2
+ {
3
+ "modelPattern": "claude-opus-4-6",
4
+ "displayName": "Claude Opus 4.6",
5
+ "inputPricePerMillion": 5.0,
6
+ "outputPricePerMillion": 25.0,
7
+ "cache5mWritePricePerMillion": 6.25,
8
+ "cache1hWritePricePerMillion": 10.0,
9
+ "cacheReadPricePerMillion": 0.5,
10
+ "inputPricePerMillionAbove200k": 10.0,
11
+ "outputPricePerMillionAbove200k": 37.5,
12
+ "cache5mWritePricePerMillionAbove200k": 12.5,
13
+ "cache1hWritePricePerMillionAbove200k": 20.0,
14
+ "cacheReadPricePerMillionAbove200k": 1.0,
15
+ "tieredThreshold": 200000
16
+ },
17
+ {
18
+ "modelPattern": "claude-opus-4-5",
19
+ "displayName": "Claude Opus 4.5",
20
+ "inputPricePerMillion": 5.0,
21
+ "outputPricePerMillion": 25.0,
22
+ "cache5mWritePricePerMillion": 6.25,
23
+ "cache1hWritePricePerMillion": 10.0,
24
+ "cacheReadPricePerMillion": 0.5
25
+ },
26
+ {
27
+ "modelPattern": "claude-sonnet-4-6",
28
+ "displayName": "Claude Sonnet 4.6",
29
+ "inputPricePerMillion": 3.0,
30
+ "outputPricePerMillion": 15.0,
31
+ "cache5mWritePricePerMillion": 3.75,
32
+ "cache1hWritePricePerMillion": 6.0,
33
+ "cacheReadPricePerMillion": 0.3,
34
+ "inputPricePerMillionAbove200k": 6.0,
35
+ "outputPricePerMillionAbove200k": 22.5,
36
+ "cache5mWritePricePerMillionAbove200k": 7.5,
37
+ "cache1hWritePricePerMillionAbove200k": 12.0,
38
+ "cacheReadPricePerMillionAbove200k": 0.6,
39
+ "tieredThreshold": 200000
40
+ },
41
+ {
42
+ "modelPattern": "claude-sonnet-4-5",
43
+ "displayName": "Claude Sonnet 4.5",
44
+ "inputPricePerMillion": 3.0,
45
+ "outputPricePerMillion": 15.0,
46
+ "cache5mWritePricePerMillion": 3.75,
47
+ "cache1hWritePricePerMillion": 6.0,
48
+ "cacheReadPricePerMillion": 0.3,
49
+ "inputPricePerMillionAbove200k": 6.0,
50
+ "outputPricePerMillionAbove200k": 22.5,
51
+ "cache5mWritePricePerMillionAbove200k": 7.5,
52
+ "cache1hWritePricePerMillionAbove200k": 12.0,
53
+ "cacheReadPricePerMillionAbove200k": 0.6,
54
+ "tieredThreshold": 200000
55
+ },
56
+ {
57
+ "modelPattern": "claude-sonnet-4",
58
+ "displayName": "Claude Sonnet 4",
59
+ "inputPricePerMillion": 3.0,
60
+ "outputPricePerMillion": 15.0,
61
+ "cache5mWritePricePerMillion": 3.75,
62
+ "cache1hWritePricePerMillion": 6.0,
63
+ "cacheReadPricePerMillion": 0.3,
64
+ "inputPricePerMillionAbove200k": 6.0,
65
+ "outputPricePerMillionAbove200k": 22.5,
66
+ "cache5mWritePricePerMillionAbove200k": 7.5,
67
+ "cache1hWritePricePerMillionAbove200k": 12.0,
68
+ "cacheReadPricePerMillionAbove200k": 0.6,
69
+ "tieredThreshold": 200000
70
+ },
71
+ {
72
+ "modelPattern": "claude-haiku-4-5",
73
+ "displayName": "Claude Haiku 4.5",
74
+ "inputPricePerMillion": 1.0,
75
+ "outputPricePerMillion": 5.0,
76
+ "cache5mWritePricePerMillion": 1.25,
77
+ "cache1hWritePricePerMillion": 2.0,
78
+ "cacheReadPricePerMillion": 0.1
79
+ },
80
+ {
81
+ "modelPattern": "claude-3-5-sonnet",
82
+ "displayName": "Claude 3.5 Sonnet",
83
+ "inputPricePerMillion": 3.0,
84
+ "outputPricePerMillion": 15.0,
85
+ "cache5mWritePricePerMillion": 3.75,
86
+ "cache1hWritePricePerMillion": 6.0,
87
+ "cacheReadPricePerMillion": 0.3,
88
+ "inputPricePerMillionAbove200k": 6.0,
89
+ "outputPricePerMillionAbove200k": 30.0,
90
+ "cache5mWritePricePerMillionAbove200k": 7.5,
91
+ "cache1hWritePricePerMillionAbove200k": 12.0,
92
+ "cacheReadPricePerMillionAbove200k": 0.6,
93
+ "tieredThreshold": 200000
94
+ },
95
+ {
96
+ "modelPattern": "claude-3-5-haiku",
97
+ "displayName": "Claude 3.5 Haiku",
98
+ "inputPricePerMillion": 0.8,
99
+ "outputPricePerMillion": 4.0,
100
+ "cache5mWritePricePerMillion": 1.0,
101
+ "cache1hWritePricePerMillion": 1.6,
102
+ "cacheReadPricePerMillion": 0.08
103
+ },
104
+ {
105
+ "modelPattern": "claude-3-opus",
106
+ "displayName": "Claude 3 Opus",
107
+ "inputPricePerMillion": 15.0,
108
+ "outputPricePerMillion": 75.0,
109
+ "cache5mWritePricePerMillion": 18.75,
110
+ "cache1hWritePricePerMillion": 30.0,
111
+ "cacheReadPricePerMillion": 1.5
112
+ }
113
+ ]
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Cost Calculator
3
+ * Calculate token costs based on model pricing.
4
+ *
5
+ * Pricing is loaded from data/model-pricing.json with a hardcoded
6
+ * fallback. Supports tiered pricing (above-200k token threshold) per
7
+ * the LiteLLM/ccusage convention. An optional lazy LiteLLM fetch provides
8
+ * pricing for unknown models.
9
+ */
10
+ import type { TokenUsage, ModelPricing, CostEstimate, UsageSummary } from './types';
11
+ /**
12
+ * Default model pricing — loaded from data/model-pricing.json,
13
+ * falling back to hardcoded values if the file is missing.
14
+ */
15
+ export declare const DEFAULT_MODEL_PRICING: ModelPricing[];
16
+ /**
17
+ * Cost Calculator class
18
+ */
19
+ export declare class CostCalculator {
20
+ private pricing;
21
+ private defaultModel;
22
+ constructor(options?: {
23
+ customPricing?: ModelPricing[];
24
+ defaultModel?: string;
25
+ });
26
+ getPricing(model: string): ModelPricing;
27
+ private calculateTieredTokenCost;
28
+ calculateCost(usage: TokenUsage, model?: string, options?: {
29
+ cumulative?: boolean;
30
+ }): CostEstimate;
31
+ estimatePromptCost(promptChars: number, expectedOutputChars: number, contextChars: number, model?: string): CostEstimate;
32
+ aggregateCosts(estimates: CostEstimate[]): CostEstimate;
33
+ formatCost(cost: number): string;
34
+ formatTokens(tokens: number): string;
35
+ getAllPricing(): ModelPricing[];
36
+ addPricing(pricing: ModelPricing): void;
37
+ setDefaultModel(model: string): void;
38
+ createUsageSummary(data: Array<{
39
+ sessionId: string;
40
+ date: Date;
41
+ model: string;
42
+ tokens: TokenUsage;
43
+ }>): UsageSummary;
44
+ }
45
+ export declare function createCostCalculator(options?: {
46
+ customPricing?: ModelPricing[];
47
+ defaultModel?: string;
48
+ }): CostCalculator;
49
+ //# sourceMappingURL=cost-calculator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cost-calculator.d.ts","sourceRoot":"","sources":["../src/cost-calculator.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AA0GpF;;;GAGG;AACH,eAAO,MAAM,qBAAqB,EAAE,YAAY,EACJ,CAAC;AAwF7C;;GAEG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,OAAO,CAAiB;IAChC,OAAO,CAAC,YAAY,CAAS;gBAEjB,OAAO,CAAC,EAAE;QAAE,aAAa,CAAC,EAAE,YAAY,EAAE,CAAC;QAAC,YAAY,CAAC,EAAE,MAAM,CAAA;KA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