forked from CassiopeiaCode/q2api
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathstream_handler.ts
More file actions
171 lines (150 loc) · 5.42 KB
/
stream_handler.ts
File metadata and controls
171 lines (150 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {
build_message_start, build_content_block_start, build_content_block_delta,
build_content_block_stop, build_ping, build_message_stop,
build_tool_use_start, build_tool_use_input_delta
} from "./sse_builder.ts";
import { get_encoding } from "tiktoken";
// Initialize tokenizer (cl100k_base is used by gpt-4, gpt-3.5-turbo)
let ENCODING: any = null;
try {
ENCODING = get_encoding("cl100k_base");
} catch (e) {
console.error("Failed to load tiktoken encoding:", e);
}
function countTokens(text: string): number {
if (!text || !ENCODING) return 0;
try {
return ENCODING.encode(text).length;
} catch (e) {
return 0;
}
}
export class ClaudeStreamHandler {
model: string;
inputTokens: number;
responseBuffer: string[];
contentBlockIndex: number;
contentBlockStarted: boolean;
contentBlockStartSent: boolean;
contentBlockStopSent: boolean;
messageStartSent: boolean;
conversationId: string | null;
currentToolUse: Record<string, any> | null;
toolInputBuffer: string[];
toolUseId: string | null;
toolName: string | null;
processedToolUseIds: Set<string>;
allToolInputs: string[];
constructor(model: string, inputTokens: number = 0) {
this.model = model;
this.inputTokens = inputTokens;
this.responseBuffer = [];
this.contentBlockIndex = -1;
this.contentBlockStarted = false;
this.contentBlockStartSent = false;
this.contentBlockStopSent = false;
this.messageStartSent = false;
this.conversationId = null;
this.currentToolUse = null;
this.toolInputBuffer = [];
this.toolUseId = null;
this.toolName = null;
this.processedToolUseIds = new Set();
this.allToolInputs = [];
}
async *handleEvent(eventType: string, payload: any): AsyncGenerator<string> {
// 1. Message Start
if (eventType === "initial-response") {
if (!this.messageStartSent) {
const convId = payload.conversationId || this.conversationId || "unknown";
this.conversationId = convId;
yield build_message_start(convId, this.model, this.inputTokens);
this.messageStartSent = true;
yield build_ping();
}
}
// 2. Content Block Delta
else if (eventType === "assistantResponseEvent") {
const content = payload.content || "";
// Close tool use if open
if (this.currentToolUse && !this.contentBlockStopSent) {
yield build_content_block_stop(this.contentBlockIndex);
this.contentBlockStopSent = true;
this.currentToolUse = null;
}
// Start content block
if (!this.contentBlockStartSent) {
this.contentBlockIndex += 1;
yield build_content_block_start(this.contentBlockIndex, "text");
this.contentBlockStartSent = true;
this.contentBlockStarted = true;
}
// Send delta
if (content) {
this.responseBuffer.push(content);
yield build_content_block_delta(this.contentBlockIndex, content);
}
}
// 3. Tool Use
else if (eventType === "toolUseEvent") {
const toolUseId = payload.toolUseId;
const toolName = payload.name;
const toolInput = payload.input || {};
const isStop = payload.stop || false;
// Start new tool use
if (toolUseId && toolName && !this.currentToolUse) {
if (this.contentBlockStartSent && !this.contentBlockStopSent) {
yield build_content_block_stop(this.contentBlockIndex);
this.contentBlockStopSent = true;
}
this.processedToolUseIds.add(toolUseId);
this.contentBlockIndex += 1;
yield build_tool_use_start(this.contentBlockIndex, toolUseId, toolName);
this.contentBlockStarted = true;
this.currentToolUse = { toolUseId, name: toolName };
this.toolUseId = toolUseId;
this.toolName = toolName;
this.toolInputBuffer = [];
this.contentBlockStopSent = false;
this.contentBlockStartSent = true;
}
// Accumulate input
if (this.currentToolUse && toolInput) {
let fragment = "";
if (typeof toolInput === "string") fragment = toolInput;
else fragment = JSON.stringify(toolInput);
this.toolInputBuffer.push(fragment);
yield build_tool_use_input_delta(this.contentBlockIndex, fragment);
}
// Stop tool use
if (isStop && this.currentToolUse) {
const fullInput = this.toolInputBuffer.join("");
this.allToolInputs.push(fullInput);
yield build_content_block_stop(this.contentBlockIndex);
this.contentBlockStopSent = true;
this.contentBlockStarted = false;
this.currentToolUse = null;
this.toolUseId = null;
this.toolName = null;
this.toolInputBuffer = [];
}
}
// 4. Assistant Response End
else if (eventType === "assistantResponseEnd") {
if (this.contentBlockStarted && !this.contentBlockStopSent) {
yield build_content_block_stop(this.contentBlockIndex);
this.contentBlockStopSent = true;
}
}
}
async *finish(): AsyncGenerator<string> {
if (this.contentBlockStarted && !this.contentBlockStopSent) {
yield build_content_block_stop(this.contentBlockIndex);
this.contentBlockStopSent = true;
}
const fullText = this.responseBuffer.join("");
const fullToolInput = this.allToolInputs.join("");
const outputTokens = countTokens(fullText) + countTokens(fullToolInput);
yield build_message_stop(this.inputTokens, outputTokens, "end_turn");
}
}