claude-code-jsonl-parser 0.0.0 → 0.1.0-beta.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Tomachi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,127 @@
1
+ # claude-code-jsonl-parser
2
+
3
+ Claude Code の JSONL 会話ログ (`~/.claude/projects/**/*.jsonl` など) を型安全にパースする、依存ゼロの TypeScript ライブラリ。
4
+
5
+ ## ✨ Features
6
+
7
+ - **依存ゼロ** — 実行時 dependencies なし
8
+ - **例外を投げない** — 1 行ずつのパース結果を `known` / `unknown` / `error` の 3 値で判別
9
+ - **ESM + CJS 両対応** — Node.js の ESM/CJS どちらからも利用可能
10
+ - **診断可能な `unknown`** — 既知 type だが形状不一致の行は `typeHint`/`reason` 付きで表現され、データを失わない
11
+ - **一括 API とストリーミング API** — ファイル全体を一括で読む API と、定数メモリで逐次処理する API の両方を提供
12
+
13
+ ## 📦 インストール
14
+
15
+ ```bash
16
+ npm install claude-code-jsonl-parser
17
+ ```
18
+
19
+ ## 🚀 クイックスタート
20
+
21
+ ```ts
22
+ import { parseJsonlFile } from 'claude-code-jsonl-parser'
23
+
24
+ const result = await parseJsonlFile('/path/to/session.jsonl')
25
+ if (result.isOk) {
26
+ for (const line of result.value) {
27
+ if (line._kind === 'known' && line.type === 'assistant') {
28
+ console.log(line.message.content)
29
+ }
30
+ }
31
+ }
32
+ ```
33
+
34
+ `parseJsonlFile` はファイル I/O エラー (存在しない、権限がない等) のみ `Err` を返す。
35
+ JSON 構文エラーや未知の形状の行は例外にはならず、各行の `ParsedLine` 自体が
36
+ `_kind: 'error'` / `_kind: 'unknown'` として表現される (後述)。
37
+
38
+ ## 📖 API
39
+
40
+ ### 一括 API とストリーミング API
41
+
42
+ | 関数 | 用途 |
43
+ | --- | --- |
44
+ | `parseJsonl(text)` | 文字列全体を一括パース (`Result<ParsedLine[], never>`) |
45
+ | `parseJsonlLines(text)` | 文字列全体を行単位の同期イテレータでパース |
46
+ | `parseJsonlFile(path)` | ファイルを一括読み込み・一括パース (`ResultAsync<ParsedLine[], FileReadError>`) |
47
+ | `parseJsonlFileStream(path)` | ファイルを 1 行ずつ非同期イテレータでパース (定数メモリ) |
48
+
49
+ 数十 MB 以上の大きなログファイルを扱う場合は `parseJsonlFileStream` を使うと、
50
+ 全行を配列に保持しないぶんメモリ使用量を抑えられる。
51
+
52
+ ```ts
53
+ import { parseJsonlFileStream } from 'claude-code-jsonl-parser'
54
+
55
+ for await (const line of parseJsonlFileStream('/path/to/large-session.jsonl')) {
56
+ if (line._kind === 'known' && line.type === 'user') {
57
+ // ...
58
+ }
59
+ }
60
+ ```
61
+
62
+ ### ParsedLine の 3 値判別
63
+
64
+ 行ごとのパース結果は `_kind` で判別する。
65
+
66
+ ```ts
67
+ import type { ParsedLine } from 'claude-code-jsonl-parser'
68
+
69
+ function handle(line: ParsedLine) {
70
+ switch (line._kind) {
71
+ case 'known': {
72
+ // line.type で assistant / user / system / ... にさらに絞り込める
73
+ break
74
+ }
75
+ case 'unknown': {
76
+ // 未知 type、または既知 type だが形状不一致。
77
+ // line.typeHint: 形状不一致なら元の type 値 (未知 type なら undefined)
78
+ // line.reason: 形状不一致の場合、どのフィールドが原因かの診断メッセージ
79
+ break
80
+ }
81
+ case 'error': {
82
+ // JSON 構文エラー行。line.message に構文エラーメッセージ
83
+ break
84
+ }
85
+ }
86
+ }
87
+ ```
88
+
89
+ `known` かつ既知 type だがガードの必須フィールドを満たさない行は、データを失わず
90
+ `unknown` (`typeHint` に元の type、`reason` に不一致理由) として表現される。
91
+ 例外は投げない。
92
+
93
+ ### content ブロックの絞り込み
94
+
95
+ `assistant`/`user` の `message.content` 配列の要素 (`AssistantContentBlock` /
96
+ `UserContentBlock`) も、既知ブロックと `UnknownContentBlock` の判別 union になっている。
97
+ 先に `_kind` で `'unknown'` かどうかを判定してから `.type` で絞り込む。
98
+
99
+ ```ts
100
+ import type { AssistantContentBlock } from 'claude-code-jsonl-parser'
101
+
102
+ function describe(block: AssistantContentBlock): string {
103
+ if (block._kind === 'unknown') return '(未知ブロック)'
104
+ if (block.type === 'text') return block.text
105
+ if (block.type === 'thinking') return block.thinking
106
+ return `tool_use: ${block.name}`
107
+ }
108
+ ```
109
+
110
+ ### 公開されている型ガード (上級者向け)
111
+
112
+ `guards` / `guardByType` / `KNOWN_TYPES` / `normalizeAssistantContent` /
113
+ `normalizeUserContent` は、独自のパースパイプラインを組みたい場合のために
114
+ 公開されている。通常の利用では `parseJsonlFile` 系の API で十分。
115
+
116
+ ```ts
117
+ import { guardByType } from 'claude-code-jsonl-parser'
118
+
119
+ const result = guardByType('assistant', someRawObject)
120
+ // result === undefined → 'assistant' というガード自体が存在しない
121
+ // result.ok === true → 形状一致
122
+ // result.ok === false, result.reason → 形状不一致とその理由
123
+ ```
124
+
125
+ ## 📑 ライセンス
126
+
127
+ このプロジェクトは [MIT License](./LICENSE) の下で公開されている。