oh-langfuse 0.1.7

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,62 @@
1
+ # Codex Langfuse implementation plan
2
+
3
+ ## Goal
4
+
5
+ Add Codex support without depending on an unstable Codex plugin API. The first
6
+ version uses Codex's `notify` lifecycle command to trigger an exporter after a
7
+ turn, then the exporter reads Codex session JSONL files and sends the new events
8
+ to Langfuse.
9
+
10
+ ## Architecture
11
+
12
+ ```text
13
+ Codex turn ends
14
+ -> ~/.codex/config.toml notify command
15
+ -> ~/.codex/hooks/codex_langfuse_notify.py
16
+ -> incremental read of ~/.codex/sessions/**/*.jsonl
17
+ -> Langfuse trace / generation / tool observations
18
+ -> ~/.codex/langfuse/state.json stores offsets
19
+ ```
20
+
21
+ ## Files
22
+
23
+ - `codex_langfuse_notify.py`: Python notify hook and JSONL exporter.
24
+ - `scripts/codex-langfuse-setup.mjs`: installs the hook, writes Langfuse
25
+ config, creates `~/.codex/langfuse-venv`, updates `~/.codex/config.toml`,
26
+ and installs the Python SDK inside the venv.
27
+ - `scripts/codex-langfuse-check.mjs`: validates hook/config/session/package
28
+ availability.
29
+ - `bin/cli.js`: adds `setup codex` and `check codex`.
30
+ - `package.json`: adds Codex npm scripts and publishes the hook file.
31
+
32
+ ## Commands
33
+
34
+ ```sh
35
+ npm run codex:setup -- --userId=h00613222
36
+ npm run codex:check
37
+ ```
38
+
39
+ or:
40
+
41
+ ```sh
42
+ node bin/cli.js setup codex
43
+ node bin/cli.js check codex
44
+ ```
45
+
46
+ Restart Codex after setup so it reloads `~/.codex/config.toml`.
47
+
48
+ ## Behavior
49
+
50
+ - The exporter reads Langfuse keys from environment variables first, then from
51
+ `~/.codex/langfuse/config.json`.
52
+ - It prefers a session path from the notify payload when Codex provides one.
53
+ If no path is present, it falls back to the latest session JSONL file.
54
+ - It fails open. Errors are logged to
55
+ `~/.codex/langfuse/codex_langfuse_notify.log` and never block Codex.
56
+
57
+ ## Future improvements
58
+
59
+ - If Codex exposes a stable OTEL or plugin trace API, add it as an optional
60
+ second backend.
61
+ - Split shared Langfuse emit logic from the Claude and Codex Python hooks.
62
+ - Add a redaction layer before sending tool output to Langfuse.
package/README.md ADDED
@@ -0,0 +1,153 @@
1
+ # oh-langfuse
2
+
3
+ `oh-langfuse` 是用于配置 Langfuse 链路追踪的交互式命令行工具,支持
4
+ Claude Code、OpenCode 和 Codex。它提供终端安装向导、直接 setup/check
5
+ 命令,以及不同工具所需的安装脚本和校验脚本。
6
+
7
+ ## 项目定位
8
+
9
+ 本仓是 AI Coding 工具链中的 Langfuse 能力包,负责:
10
+
11
+ - 检查 Node.js、npm、Python、pip、OpenCode CLI 等本地依赖;
12
+ - 收集 Langfuse 用户标识,通常是员工号,例如 `h00613222`;
13
+ - 安装或更新 hook 脚本、插件文件、Python 虚拟环境和用户级配置;
14
+ - 校验 Claude Code、OpenCode、Codex 的 Langfuse 配置是否生效。
15
+
16
+ 本包发布到 npm 后暴露 `oh-langfuse` 命令,并被聚合安装器
17
+ `oh-aicoding-tool` 调用。
18
+
19
+ 迁移期内,npm 包仍保留旧命令别名 `code-tool-langfuse`。
20
+
21
+ ## 环境要求
22
+
23
+ | 目标工具 | 依赖 |
24
+ | --- | --- |
25
+ | Claude Code | Node.js、npm、Python、pip |
26
+ | OpenCode | Node.js、npm、OpenCode CLI |
27
+ | Codex | Node.js、npm、Python、pip |
28
+
29
+ Linux 环境如果创建 Python venv 失败,请先安装 `python3-venv`。
30
+
31
+ ## 使用方式
32
+
33
+ 从当前仓库运行:
34
+
35
+ ```bash
36
+ npm install
37
+ npm start
38
+ ```
39
+
40
+ npm 发布后运行:
41
+
42
+ ```bash
43
+ npx oh-langfuse
44
+ ```
45
+
46
+ 交互界面支持:
47
+
48
+ - `Up` / `Down`:移动选项;
49
+ - 数字键:快速选择;
50
+ - `Space`:多选切换;
51
+ - `Enter`:确认;
52
+ - `q` 或 `Esc`:退出。
53
+
54
+ 在不支持 raw input 的终端中,会自动退回到数字输入模式。
55
+
56
+ ## 命令
57
+
58
+ ```bash
59
+ oh-langfuse
60
+ oh-langfuse setup
61
+ oh-langfuse setup claude
62
+ oh-langfuse setup opencode
63
+ oh-langfuse setup codex
64
+ oh-langfuse check
65
+ oh-langfuse check environment
66
+ oh-langfuse check claude
67
+ oh-langfuse check opencode
68
+ oh-langfuse check codex
69
+ ```
70
+
71
+ 预览安装动作,不写文件、不安装依赖:
72
+
73
+ ```bash
74
+ oh-langfuse setup --dry-run
75
+ ```
76
+
77
+ 本仓内也可以直接运行脚本:
78
+
79
+ ```bash
80
+ npm run claude:setup
81
+ npm run opencode:setup
82
+ npm run codex:setup
83
+ npm run claude:check
84
+ npm run opencode:check
85
+ npm run codex:check
86
+ ```
87
+
88
+ ## 各目标工具写入的配置
89
+
90
+ ### Claude Code
91
+
92
+ 安装 `langfuse_hook.py`,创建 `~/.claude/langfuse-venv`,安装 Python 包
93
+ `langfuse`,并把 Langfuse 环境变量和 `Stop` hook 合并写入
94
+ `~/.claude/settings.json`。
95
+
96
+ ### OpenCode
97
+
98
+ 安装并 patch `opencode-plugin-langfuse`,在
99
+ `~/.config/opencode/opencode.json` 中启用 OpenTelemetry,写入插件用户配置,
100
+ 并可写入用户级 `LANGFUSE_*` 环境变量。Windows 下还会生成
101
+ `launch-opencode-langfuse.cmd`,用于带环境变量启动 OpenCode。
102
+
103
+ ### Codex
104
+
105
+ 安装 `codex_langfuse_notify.py`,创建 `~/.codex/langfuse-venv`,安装 Python
106
+ 包 `langfuse`,把 Langfuse 凭据写入 `~/.codex/langfuse/config.json`,并更新
107
+ `~/.codex/config.toml` 顶层 `notify` 命令。安装后需要重启 Codex。
108
+
109
+ Codex 集成通过 notify hook 实现:每次通知事件会增量读取对应的
110
+ `~/.codex/sessions/**/*.jsonl`,把新增的 user、assistant、tool、token 事件转换成
111
+ Langfuse observation,并把读取偏移记录到 `~/.codex/langfuse/state.json`。
112
+
113
+ ## 环境变量覆盖
114
+
115
+ 工具内置了内部 Langfuse 服务的默认值,可通过以下变量覆盖:
116
+
117
+ | 变量 | 作用 |
118
+ | --- | --- |
119
+ | `LANGFUSE_BASEURL` / `LANGFUSE_HOST` | Langfuse 服务地址 |
120
+ | `LANGFUSE_PUBLIC_KEY` | Langfuse public key |
121
+ | `LANGFUSE_SECRET_KEY` | Langfuse secret key |
122
+ | `LANGFUSE_USER_ID` / `CC_USER_ID` | 用户标识或员工号 |
123
+ | `CODEX_HOME` | 自定义 Codex home 目录 |
124
+ | `OPENCODE_SKIP_PLUGIN_INSTALL` | OpenCode 插件已准备好时跳过 npm 安装 |
125
+
126
+ 交互界面不会明文打印 secret key。
127
+
128
+ ## 目录结构
129
+
130
+ | 路径 | 说明 |
131
+ | --- | --- |
132
+ | `bin/cli.js` | 交互式和直接命令入口 |
133
+ | `scripts/langfuse-setup.mjs` | Claude Code Langfuse 安装 |
134
+ | `scripts/opencode-langfuse-setup.mjs` | OpenCode Langfuse 安装 |
135
+ | `scripts/codex-langfuse-setup.mjs` | Codex Langfuse 安装 |
136
+ | `scripts/*-check.mjs` | 配置校验脚本 |
137
+ | `langfuse_hook.py` | Claude Code hook exporter |
138
+ | `codex_langfuse_notify.py` | Codex notify exporter |
139
+
140
+ ## 发布与维护
141
+
142
+ - npm 包名:`oh-langfuse`
143
+ - 主命令:`oh-langfuse`
144
+ - 兼容命令:`code-tool-langfuse`
145
+ - 发布前建议运行:
146
+
147
+ ```bash
148
+ npm run check
149
+ npm pack --dry-run
150
+ ```
151
+
152
+ 维护时请保持 README 命令列表、`bin/cli.js` 和 `package.json` 的 `bin/files`
153
+ 字段一致。