forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreaming.ts
More file actions
220 lines (194 loc) · 5.97 KB
/
Copy pathstreaming.ts
File metadata and controls
220 lines (194 loc) · 5.97 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { ReplaySubject } from "rxjs";
import {
CopilotKitLowLevelError,
CopilotKitError,
CopilotKitErrorCode,
ensureStructuredError,
} from "@copilotkit/shared";
import { errorConfig, getFallbackMessage } from "./error-messages";
export async function writeJsonLineResponseToEventStream<T>(
response: ReadableStream<Uint8Array>,
eventStream$: ReplaySubject<T>,
) {
const reader = response.getReader();
const decoder = new TextDecoder();
let buffer = [];
function flushBuffer() {
const currentBuffer = buffer.join("");
if (currentBuffer.trim().length === 0) {
return;
}
const parts = currentBuffer.split("\n");
if (parts.length === 0) {
return;
}
const lastPartIsComplete = currentBuffer.endsWith("\n");
// truncate buffer
buffer = [];
if (!lastPartIsComplete) {
// put back the last part
buffer.push(parts.pop());
}
parts
.map((part) => part.trim())
.filter((part) => part != "")
.forEach((part) => {
eventStream$.next(JSON.parse(part));
});
}
try {
while (true) {
const { done, value } = await reader.read();
if (!done) {
buffer.push(decoder.decode(value, { stream: true }));
}
flushBuffer();
if (done) {
break;
}
}
} catch (error) {
// Preserve already structured CopilotKit errors, only convert unstructured errors
const structuredError = ensureStructuredError(
error,
convertStreamingErrorToStructured,
);
eventStream$.error(structuredError);
return;
}
eventStream$.complete();
}
function convertStreamingErrorToStructured(error: any): CopilotKitError {
// Determine a more helpful error message based on context
let helpfulMessage = generateHelpfulErrorMessage(error);
// For network-related errors, use CopilotKitLowLevelError to preserve the original error
if (
error?.message?.includes("fetch failed") ||
error?.message?.includes("ECONNREFUSED") ||
error?.message?.includes("ENOTFOUND") ||
error?.message?.includes("ETIMEDOUT") ||
error?.message?.includes("terminated") ||
error?.cause?.code === "UND_ERR_SOCKET" ||
error?.message?.includes("other side closed") ||
error?.code === "UND_ERR_SOCKET"
) {
console.log("error", error);
return new CopilotKitLowLevelError({
error: error instanceof Error ? error : new Error(String(error)),
url: "streaming connection",
message: helpfulMessage,
});
}
// For all other errors, preserve the raw error in a basic CopilotKitError
return new CopilotKitError({
message: helpfulMessage,
code: CopilotKitErrorCode.UNKNOWN,
});
}
/**
* Generates a helpful error message based on error patterns and context
*/
export function generateHelpfulErrorMessage(
error: any,
context: string = "connection",
): string {
const baseMessage = error?.message || String(error);
// Check for preserved error information from Python agent
const originalErrorType =
error?.originalErrorType || error?.extensions?.originalErrorType;
const statusCode = error?.statusCode || error?.extensions?.statusCode;
const responseData = error?.responseData || error?.extensions?.responseData;
// First, try to match by original error type if available (more specific)
if (originalErrorType) {
const typeConfig = errorConfig.errorPatterns[originalErrorType];
if (typeConfig) {
return typeConfig.message.replace("{context}", context);
}
}
// Check for specific error patterns from configuration
for (const [pattern, config] of Object.entries(errorConfig.errorPatterns)) {
const shouldMatch =
baseMessage?.includes(pattern) ||
error?.cause?.code === pattern ||
error?.code === pattern ||
statusCode === parseInt(pattern) ||
(pattern === "other_side_closed" &&
baseMessage?.includes("other side closed")) ||
(pattern === "fetch_failed" && baseMessage?.includes("fetch failed")) ||
(responseData && JSON.stringify(responseData).includes(pattern));
if (shouldMatch) {
// Replace {context} placeholder with actual context
return config.message.replace("{context}", context);
}
}
// Try to match by category for fallback messages
if (isNetworkError(error)) {
return getFallbackMessage("network");
}
if (isConnectionError(error)) {
return getFallbackMessage("connection");
}
if (isAuthenticationError(error)) {
return getFallbackMessage("authentication");
}
// Default fallback
return getFallbackMessage("default");
}
/**
* Determines if an error is network-related
*/
function isNetworkError(error: any): boolean {
const networkPatterns = [
"ECONNREFUSED",
"ENOTFOUND",
"ETIMEDOUT",
"fetch_failed",
];
return networkPatterns.some(
(pattern) =>
error?.message?.includes(pattern) ||
error?.cause?.code === pattern ||
error?.code === pattern,
);
}
/**
* Determines if an error is connection-related
*/
function isConnectionError(error: any): boolean {
const connectionPatterns = [
"terminated",
"UND_ERR_SOCKET",
"other side closed",
];
return connectionPatterns.some(
(pattern) =>
error?.message?.includes(pattern) ||
error?.cause?.code === pattern ||
error?.code === pattern,
);
}
/**
* Determines if an error is authentication-related
*/
function isAuthenticationError(error: any): boolean {
const authPatterns = [
"401",
"api key",
"unauthorized",
"authentication",
"AuthenticationError",
"PermissionDeniedError",
];
const baseMessage = error?.message || String(error);
const originalErrorType =
error?.originalErrorType || error?.extensions?.originalErrorType;
const statusCode = error?.statusCode || error?.extensions?.statusCode;
return authPatterns.some(
(pattern) =>
baseMessage?.toLowerCase().includes(pattern.toLowerCase()) ||
originalErrorType === pattern ||
statusCode === 401 ||
error?.status === 401 ||
error?.statusCode === 401,
);
}