@siteboon/claude-code-ui 1.19.1 → 1.21.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,140 @@
1
+ // Gemini Response Handler - JSON Stream processing
2
+ class GeminiResponseHandler {
3
+ constructor(ws, options = {}) {
4
+ this.ws = ws;
5
+ this.buffer = '';
6
+ this.onContentFragment = options.onContentFragment || null;
7
+ this.onInit = options.onInit || null;
8
+ this.onToolUse = options.onToolUse || null;
9
+ this.onToolResult = options.onToolResult || null;
10
+ }
11
+
12
+ // Process incoming raw data from Gemini stream-json
13
+ processData(data) {
14
+ this.buffer += data;
15
+
16
+ // Split by newline
17
+ const lines = this.buffer.split('\n');
18
+
19
+ // Keep the last incomplete line in the buffer
20
+ this.buffer = lines.pop() || '';
21
+
22
+ for (const line of lines) {
23
+ if (!line.trim()) continue;
24
+
25
+ try {
26
+ const event = JSON.parse(line);
27
+ this.handleEvent(event);
28
+ } catch (err) {
29
+ // Not a JSON line, probably debug output or CLI warnings
30
+ // console.error('[Gemini Handler] Non-JSON line ignored:', line);
31
+ }
32
+ }
33
+ }
34
+
35
+ handleEvent(event) {
36
+ const socketSessionId = typeof this.ws.getSessionId === 'function' ? this.ws.getSessionId() : null;
37
+
38
+ if (event.type === 'init') {
39
+ if (this.onInit) {
40
+ this.onInit(event);
41
+ }
42
+ return;
43
+ }
44
+
45
+ if (event.type === 'message' && event.role === 'assistant') {
46
+ const content = event.content || '';
47
+
48
+ // Notify the parent CLI handler of accumulated text
49
+ if (this.onContentFragment && content) {
50
+ this.onContentFragment(content);
51
+ }
52
+
53
+ let payload = {
54
+ type: 'gemini-response',
55
+ data: {
56
+ type: 'message',
57
+ content: content,
58
+ isPartial: event.delta === true
59
+ }
60
+ };
61
+ if (socketSessionId) payload.sessionId = socketSessionId;
62
+ this.ws.send(payload);
63
+ }
64
+ else if (event.type === 'tool_use') {
65
+ if (this.onToolUse) {
66
+ this.onToolUse(event);
67
+ }
68
+ let payload = {
69
+ type: 'gemini-tool-use',
70
+ toolName: event.tool_name,
71
+ toolId: event.tool_id,
72
+ parameters: event.parameters || {}
73
+ };
74
+ if (socketSessionId) payload.sessionId = socketSessionId;
75
+ this.ws.send(payload);
76
+ }
77
+ else if (event.type === 'tool_result') {
78
+ if (this.onToolResult) {
79
+ this.onToolResult(event);
80
+ }
81
+ let payload = {
82
+ type: 'gemini-tool-result',
83
+ toolId: event.tool_id,
84
+ status: event.status,
85
+ output: event.output || ''
86
+ };
87
+ if (socketSessionId) payload.sessionId = socketSessionId;
88
+ this.ws.send(payload);
89
+ }
90
+ else if (event.type === 'result') {
91
+ // Send a finalize message string
92
+ let payload = {
93
+ type: 'gemini-response',
94
+ data: {
95
+ type: 'message',
96
+ content: '',
97
+ isPartial: false
98
+ }
99
+ };
100
+ if (socketSessionId) payload.sessionId = socketSessionId;
101
+ this.ws.send(payload);
102
+
103
+ if (event.stats && event.stats.total_tokens) {
104
+ let statsPayload = {
105
+ type: 'claude-status',
106
+ data: {
107
+ status: 'Complete',
108
+ tokens: event.stats.total_tokens
109
+ }
110
+ };
111
+ if (socketSessionId) statsPayload.sessionId = socketSessionId;
112
+ this.ws.send(statsPayload);
113
+ }
114
+ }
115
+ else if (event.type === 'error') {
116
+ let payload = {
117
+ type: 'gemini-error',
118
+ error: event.error || event.message || 'Unknown Gemini streaming error'
119
+ };
120
+ if (socketSessionId) payload.sessionId = socketSessionId;
121
+ this.ws.send(payload);
122
+ }
123
+ }
124
+
125
+ forceFlush() {
126
+ // If the buffer has content, try to parse it one last time
127
+ if (this.buffer.trim()) {
128
+ try {
129
+ const event = JSON.parse(this.buffer);
130
+ this.handleEvent(event);
131
+ } catch (err) { }
132
+ }
133
+ }
134
+
135
+ destroy() {
136
+ this.buffer = '';
137
+ }
138
+ }
139
+
140
+ export default GeminiResponseHandler;