@volcengine/tls-observer-claude-code 0.0.1

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,6 @@
1
+ import type { SessionState, ToolObservation, TracingState } from './types';
2
+ export declare function loadState(stateFilePath: string): TracingState;
3
+ export declare function getSessionState(state: TracingState, sessionId: string): SessionState;
4
+ export declare function atomicUpdateState(stateFilePath: string, fn: (state: TracingState) => TracingState): Promise<void>;
5
+ export declare function pruneOldSessions(state: TracingState, now?: number): TracingState;
6
+ export declare function putToolObservation(session: SessionState, observation: ToolObservation): SessionState;
@@ -0,0 +1 @@
1
+ export declare function readStdinJson<T>(): Promise<T>;
@@ -0,0 +1,12 @@
1
+ export interface TlsProducerConfig {
2
+ endpoint: string;
3
+ region: string;
4
+ traceTopicId: string;
5
+ apiKey?: string;
6
+ ak?: string;
7
+ sk?: string;
8
+ timeoutMs?: number;
9
+ }
10
+ export declare function normalizeProducerEndpoint(endpoint: string): string;
11
+ export declare function sendTlsLogRecords(config: TlsProducerConfig, records: Array<Record<string, string>>, fileName: string): Promise<void>;
12
+ export declare function closeTraceLogProducers(): Promise<void>;
@@ -0,0 +1,23 @@
1
+ import type { Config } from './config';
2
+ import type { SessionState, PostCompactHookInput, StopFailureHookInput, StopHookInput, TraceModel, Turn } from './types';
3
+ export declare function toTraceId(value: string): string;
4
+ export declare function toSpanId(...parts: unknown[]): string;
5
+ export declare function buildTraceFromTurn(options: {
6
+ input: StopHookInput;
7
+ turn: Turn;
8
+ turnIndex: number;
9
+ sessionState: SessionState;
10
+ config: Config;
11
+ source?: string;
12
+ traceSeedSuffix?: string;
13
+ extraAttributes?: Record<string, unknown>;
14
+ }): TraceModel;
15
+ export declare function buildCompactTrace(options: {
16
+ input: PostCompactHookInput;
17
+ sessionState: SessionState;
18
+ config: Config;
19
+ }): TraceModel;
20
+ export declare function buildStopFailureTrace(options: {
21
+ input: StopFailureHookInput;
22
+ config: Config;
23
+ }): TraceModel;
@@ -0,0 +1,15 @@
1
+ import type { AssistantMessage, ToolResultMessage, TranscriptMessage, Turn, UserMessage } from './types';
2
+ export declare function readTranscript(filePath: string, afterLine?: number, afterOffset?: number): {
3
+ messages: TranscriptMessage[];
4
+ lastLine: number;
5
+ lastOffset: number;
6
+ };
7
+ export declare function getTranscriptEndPosition(filePath: string): {
8
+ lastLine: number;
9
+ lastOffset: number;
10
+ };
11
+ export declare function isHumanMessage(msg: TranscriptMessage): msg is UserMessage;
12
+ export declare function isToolResult(msg: TranscriptMessage): msg is ToolResultMessage;
13
+ export declare function isAssistantMessage(msg: TranscriptMessage): msg is AssistantMessage;
14
+ export declare function stripModelDateSuffix(model: string): string;
15
+ export declare function groupIntoTurns(messages: TranscriptMessage[]): Turn[];
@@ -0,0 +1,223 @@
1
+ export interface HookInputBase {
2
+ session_id?: string;
3
+ transcript_path?: string;
4
+ cwd?: string;
5
+ permission_mode?: string;
6
+ hook_event_name?: string;
7
+ agent_id?: string;
8
+ agent_type?: string;
9
+ }
10
+ export interface UserPromptSubmitHookInput extends HookInputBase {
11
+ hook_event_name: 'UserPromptSubmit';
12
+ prompt?: string;
13
+ }
14
+ export interface PreToolUseHookInput extends HookInputBase {
15
+ hook_event_name: 'PreToolUse';
16
+ tool_use_id?: string;
17
+ tool_name?: string;
18
+ tool_input?: Record<string, unknown>;
19
+ }
20
+ export interface PostToolUseHookInput extends HookInputBase {
21
+ hook_event_name: 'PostToolUse' | 'PostToolUseFailure';
22
+ tool_use_id?: string;
23
+ tool_name?: string;
24
+ tool_input?: Record<string, unknown>;
25
+ tool_response?: unknown;
26
+ }
27
+ export interface StopHookInput extends HookInputBase {
28
+ hook_event_name: 'Stop';
29
+ stop_hook_active?: boolean;
30
+ last_assistant_message?: string;
31
+ }
32
+ export interface StopFailureHookInput extends HookInputBase {
33
+ hook_event_name: 'StopFailure';
34
+ error?: string;
35
+ error_type?: string;
36
+ }
37
+ export interface SubagentStopHookInput extends HookInputBase {
38
+ hook_event_name: 'SubagentStop';
39
+ agent_id?: string;
40
+ agent_type?: string;
41
+ agent_transcript_path?: string;
42
+ }
43
+ export interface PreCompactHookInput extends HookInputBase {
44
+ hook_event_name: 'PreCompact';
45
+ trigger?: 'manual' | 'auto' | string;
46
+ custom_instructions?: string;
47
+ }
48
+ export interface PostCompactHookInput extends HookInputBase {
49
+ hook_event_name: 'PostCompact';
50
+ trigger?: 'manual' | 'auto' | string;
51
+ compact_summary?: string;
52
+ }
53
+ export interface SessionEndHookInput extends HookInputBase {
54
+ hook_event_name: 'SessionEnd';
55
+ reason?: string;
56
+ }
57
+ export type HookInput = UserPromptSubmitHookInput | PreToolUseHookInput | PostToolUseHookInput | StopHookInput | StopFailureHookInput | SubagentStopHookInput | PreCompactHookInput | PostCompactHookInput | SessionEndHookInput | HookInputBase;
58
+ export interface TextBlock {
59
+ type: 'text';
60
+ text: string;
61
+ }
62
+ export interface ThinkingBlock {
63
+ type: 'thinking';
64
+ thinking: string;
65
+ }
66
+ export interface ToolUseBlock {
67
+ type: 'tool_use';
68
+ id: string;
69
+ name: string;
70
+ input: Record<string, unknown>;
71
+ }
72
+ export interface ToolResultBlock {
73
+ type: 'tool_result';
74
+ tool_use_id: string;
75
+ content: string | Array<{
76
+ type: string;
77
+ text?: string;
78
+ }>;
79
+ is_error?: boolean;
80
+ }
81
+ export type ContentBlock = TextBlock | ThinkingBlock | ToolUseBlock;
82
+ export interface Usage {
83
+ input_tokens?: number;
84
+ output_tokens?: number;
85
+ reasoning_output_tokens?: number;
86
+ cache_read_input_tokens?: number;
87
+ cache_creation_input_tokens?: number;
88
+ }
89
+ export interface UserMessage {
90
+ type: 'user';
91
+ message: {
92
+ role: 'user';
93
+ content: string | Array<Record<string, unknown>>;
94
+ };
95
+ timestamp?: string;
96
+ promptId?: string;
97
+ }
98
+ export interface ToolResultMessage {
99
+ type: 'user';
100
+ message: {
101
+ role: 'user';
102
+ content: ToolResultBlock[];
103
+ };
104
+ timestamp?: string;
105
+ promptId?: string;
106
+ toolUseResult?: {
107
+ agentId?: string;
108
+ };
109
+ }
110
+ export interface AssistantMessage {
111
+ type: 'assistant';
112
+ message: {
113
+ id?: string;
114
+ role: 'assistant';
115
+ model?: string;
116
+ content?: ContentBlock[];
117
+ usage?: Usage;
118
+ stop_reason?: string | null;
119
+ };
120
+ timestamp?: string;
121
+ promptId?: string;
122
+ }
123
+ export type TranscriptMessage = UserMessage | ToolResultMessage | AssistantMessage;
124
+ export interface ToolCall {
125
+ tool_use: ToolUseBlock;
126
+ result?: {
127
+ content: string;
128
+ timestamp: string;
129
+ is_error?: boolean;
130
+ };
131
+ agentId?: string;
132
+ }
133
+ export interface LlmCall {
134
+ content: ContentBlock[];
135
+ model: string;
136
+ usage: Usage;
137
+ startTime: string;
138
+ endTime: string;
139
+ toolCalls: ToolCall[];
140
+ stopReason?: string | null;
141
+ synthetic?: boolean;
142
+ }
143
+ export interface Turn {
144
+ userContent: string | Array<Record<string, unknown>>;
145
+ userTimestamp: string;
146
+ promptId?: string;
147
+ llmCalls: LlmCall[];
148
+ isComplete: boolean;
149
+ }
150
+ export interface ToolObservation {
151
+ tool_use_id: string;
152
+ tool_name?: string;
153
+ tool_input?: unknown;
154
+ tool_response?: unknown;
155
+ start_time_ms?: number;
156
+ end_time_ms?: number;
157
+ status?: 'ok' | 'error';
158
+ }
159
+ export interface PendingSubagentTrace {
160
+ agent_id?: string;
161
+ agent_type?: string;
162
+ agent_transcript_path: string;
163
+ session_id?: string;
164
+ }
165
+ export interface SessionState {
166
+ last_line: number;
167
+ last_offset?: number;
168
+ turn_count: number;
169
+ updated: string;
170
+ current_turn_id?: string;
171
+ current_turn_start_ms?: number;
172
+ current_prompt?: string;
173
+ tool_start_times?: Record<string, number>;
174
+ tool_observations?: Record<string, ToolObservation>;
175
+ pending_subagent_traces?: PendingSubagentTrace[];
176
+ subagent_turn_count?: number;
177
+ compaction_start_time_ms?: number;
178
+ compaction_trigger?: string;
179
+ compaction_custom_instructions?: string;
180
+ }
181
+ export interface TracingState {
182
+ [sessionId: string]: SessionState;
183
+ }
184
+ export interface TraceSpan {
185
+ trace_id: string;
186
+ span_id: string;
187
+ parent_span_id?: string | null;
188
+ name: string;
189
+ type: 'entry' | 'model' | 'tool' | 'event';
190
+ span_kind: 'SERVER' | 'CLIENT' | 'INTERNAL';
191
+ start_time: string;
192
+ end_time: string;
193
+ start_time_unix_ms: number;
194
+ end_time_unix_ms: number;
195
+ duration_ms: number;
196
+ attributes: Record<string, unknown>;
197
+ input?: unknown;
198
+ output?: unknown;
199
+ status?: {
200
+ code?: 'UNSET' | 'OK' | 'ERROR';
201
+ message?: string;
202
+ };
203
+ }
204
+ export interface TraceModel {
205
+ schema_url: string;
206
+ source: string;
207
+ trace_id: string;
208
+ resource: Record<string, unknown>;
209
+ spans: TraceSpan[];
210
+ }
211
+ export interface ExportResult {
212
+ status: 'success' | 'skipped' | 'failed';
213
+ reason?: string;
214
+ message?: string;
215
+ status_code?: number;
216
+ trace_id?: string;
217
+ span_count?: number;
218
+ payload_bytes?: number;
219
+ endpoint_host?: string;
220
+ endpoint_path?: string;
221
+ attempts?: number;
222
+ duration_ms?: number;
223
+ }
@@ -0,0 +1,121 @@
1
+ {
2
+ "description": "TLS Observer hooks for Claude Code traces.",
3
+ "hooks": {
4
+ "UserPromptSubmit": [
5
+ {
6
+ "hooks": [
7
+ {
8
+ "type": "command",
9
+ "command": "node \"${CLAUDE_PLUGIN_ROOT}/dist/index.js\"",
10
+ "timeout": 30,
11
+ "statusMessage": "Recording Claude Code turn"
12
+ }
13
+ ]
14
+ }
15
+ ],
16
+ "PreToolUse": [
17
+ {
18
+ "hooks": [
19
+ {
20
+ "type": "command",
21
+ "command": "node \"${CLAUDE_PLUGIN_ROOT}/dist/index.js\"",
22
+ "timeout": 10
23
+ }
24
+ ]
25
+ }
26
+ ],
27
+ "PostToolUse": [
28
+ {
29
+ "hooks": [
30
+ {
31
+ "type": "command",
32
+ "command": "node \"${CLAUDE_PLUGIN_ROOT}/dist/index.js\"",
33
+ "timeout": 30,
34
+ "async": true
35
+ }
36
+ ]
37
+ }
38
+ ],
39
+ "PostToolUseFailure": [
40
+ {
41
+ "hooks": [
42
+ {
43
+ "type": "command",
44
+ "command": "node \"${CLAUDE_PLUGIN_ROOT}/dist/index.js\"",
45
+ "timeout": 30,
46
+ "async": true
47
+ }
48
+ ]
49
+ }
50
+ ],
51
+ "Stop": [
52
+ {
53
+ "hooks": [
54
+ {
55
+ "type": "command",
56
+ "command": "node \"${CLAUDE_PLUGIN_ROOT}/dist/index.js\"",
57
+ "timeout": 120,
58
+ "statusMessage": "Exporting Claude Code trace to TLS"
59
+ }
60
+ ]
61
+ }
62
+ ],
63
+ "StopFailure": [
64
+ {
65
+ "hooks": [
66
+ {
67
+ "type": "command",
68
+ "command": "node \"${CLAUDE_PLUGIN_ROOT}/dist/index.js\"",
69
+ "timeout": 30
70
+ }
71
+ ]
72
+ }
73
+ ],
74
+ "SubagentStop": [
75
+ {
76
+ "hooks": [
77
+ {
78
+ "type": "command",
79
+ "command": "node \"${CLAUDE_PLUGIN_ROOT}/dist/index.js\"",
80
+ "timeout": 60,
81
+ "async": true
82
+ }
83
+ ]
84
+ }
85
+ ],
86
+ "PreCompact": [
87
+ {
88
+ "hooks": [
89
+ {
90
+ "type": "command",
91
+ "command": "node \"${CLAUDE_PLUGIN_ROOT}/dist/index.js\"",
92
+ "timeout": 10
93
+ }
94
+ ]
95
+ }
96
+ ],
97
+ "PostCompact": [
98
+ {
99
+ "hooks": [
100
+ {
101
+ "type": "command",
102
+ "command": "node \"${CLAUDE_PLUGIN_ROOT}/dist/index.js\"",
103
+ "timeout": 30,
104
+ "async": true
105
+ }
106
+ ]
107
+ }
108
+ ],
109
+ "SessionEnd": [
110
+ {
111
+ "hooks": [
112
+ {
113
+ "type": "command",
114
+ "command": "node \"${CLAUDE_PLUGIN_ROOT}/dist/index.js\"",
115
+ "timeout": 30
116
+ }
117
+ ]
118
+ }
119
+ ]
120
+ }
121
+ }
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@volcengine/tls-observer-claude-code",
3
+ "version": "0.0.1",
4
+ "description": "Export Claude Code hook telemetry to Volcengine TLS with OTLP traces.",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ }
12
+ },
13
+ "types": "./dist/index.d.ts",
14
+ "files": [
15
+ "dist",
16
+ "hooks",
17
+ ".claude-plugin",
18
+ "README.md",
19
+ "LICENSE"
20
+ ],
21
+ "scripts": {
22
+ "build": "rslib build",
23
+ "dev": "rslib build --watch",
24
+ "test": "jest --runInBand",
25
+ "test:coverage": "jest --runInBand --coverage"
26
+ },
27
+ "engines": {
28
+ "node": ">=18"
29
+ },
30
+ "keywords": [
31
+ "claude-code",
32
+ "hooks",
33
+ "observability",
34
+ "opentelemetry",
35
+ "tls"
36
+ ],
37
+ "publishConfig": {
38
+ "registry": "https://registry.npmjs.org/",
39
+ "access": "public"
40
+ },
41
+ "dependencies": {
42
+ "@volcengine/openapi": "^1.36.1"
43
+ },
44
+ "devDependencies": {
45
+ "@rslib/core": "^0.21.5",
46
+ "@swc/core": "1.15.41",
47
+ "@swc/jest": "0.2.39",
48
+ "@types/jest": "^30.0.0",
49
+ "@types/node": "^24.10.9",
50
+ "@volcengine/tls-observer-shared": "workspace:*",
51
+ "jest": "30.4.2",
52
+ "typescript": "^6.0.3"
53
+ }
54
+ }