forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
187 lines (162 loc) · 5.23 KB
/
Copy pathindex.ts
File metadata and controls
187 lines (162 loc) · 5.23 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
import { CreateCopilotRuntimeServerOptions, getCommonConfig } from "../shared";
import telemetry, {
getRuntimeInstanceTelemetryInfo,
} from "../../telemetry-client";
import { createCopilotEndpointSingleRoute } from "../../../v2/runtime";
import type { IncomingMessage, ServerResponse } from "node:http";
import {
getFullUrl,
IncomingWithBody,
isDisturbedOrLockedError,
isStreamConsumed,
nodeStreamToReadableStream,
readableStreamToNodeStream,
synthesizeBodyFromParsedBody,
toHeaders,
} from "./request-handler";
export function copilotRuntimeNodeHttpEndpoint(
options: CreateCopilotRuntimeServerOptions,
) {
const commonConfig = getCommonConfig(options);
telemetry.setGlobalProperties({
runtime: {
framework: "node-http",
},
});
if (options.properties?._copilotkit) {
telemetry.setGlobalProperties({
_copilotkit: options.properties._copilotkit,
});
}
telemetry.capture(
"oss.runtime.instance_created",
getRuntimeInstanceTelemetryInfo(options),
);
const logger = commonConfig.logging;
logger.debug("Creating Node HTTP endpoint");
const serviceAdapter = options.serviceAdapter;
if (serviceAdapter) {
options.runtime.handleServiceAdapter(serviceAdapter);
}
// Note: cors option requires @copilotkit/runtime with credentials support
const honoApp = createCopilotEndpointSingleRoute({
runtime: options.runtime.instance,
basePath: options.baseUrl ?? options.endpoint,
...(options.cors && { cors: options.cors }),
} as any);
const handle = async function handler(
req: IncomingWithBody,
res: ServerResponse,
) {
const url = getFullUrl(req);
const hasBody = req.method !== "GET" && req.method !== "HEAD";
const baseHeaders = toHeaders(req.headers);
const parsedBody = req.body;
const streamConsumed = isStreamConsumed(req) || parsedBody !== undefined;
const canStream = hasBody && !streamConsumed;
let requestBody: BodyInit | null | undefined = undefined;
let useDuplex = false;
if (hasBody && canStream) {
requestBody = nodeStreamToReadableStream(req);
useDuplex = true;
}
if (hasBody && streamConsumed) {
if (parsedBody !== undefined) {
const synthesized = synthesizeBodyFromParsedBody(
parsedBody,
baseHeaders,
);
requestBody = synthesized.body ?? undefined;
baseHeaders.delete("content-length");
if (synthesized.contentType) {
baseHeaders.set("content-type", synthesized.contentType);
}
logger.debug(
"Request stream already consumed; using parsed req.body to rebuild request.",
);
} else {
logger.warn(
"Request stream consumed with no available body; sending empty payload.",
);
requestBody = undefined;
}
}
const buildRequest = (
body: BodyInit | null | undefined,
headers: Headers,
duplex: boolean,
) =>
new Request(url, {
method: req.method,
headers,
body,
duplex: duplex ? "half" : undefined,
} as RequestInit);
let response: Response;
try {
response = await honoApp.fetch(
buildRequest(requestBody, baseHeaders, useDuplex),
);
} catch (error) {
if (isDisturbedOrLockedError(error) && hasBody) {
logger.warn(
"Encountered disturbed/locked request body; rebuilding request using parsed body or empty payload.",
);
const fallbackHeaders = new Headers(baseHeaders);
let fallbackBody: BodyInit | null | undefined;
if (parsedBody !== undefined) {
const synthesized = synthesizeBodyFromParsedBody(
parsedBody,
fallbackHeaders,
);
fallbackBody = synthesized.body ?? undefined;
fallbackHeaders.delete("content-length");
if (synthesized.contentType) {
fallbackHeaders.set("content-type", synthesized.contentType);
}
} else {
fallbackBody = undefined;
}
response = await honoApp.fetch(
buildRequest(fallbackBody, fallbackHeaders, false),
);
} else {
throw error;
}
}
res.statusCode = response.status;
response.headers.forEach((value, key) => {
res.setHeader(key, value);
});
if (response.body) {
readableStreamToNodeStream(response.body).pipe(res);
} else {
res.end();
}
};
// Duck-type check for Request-like objects (handles polyfilled Request from @hono/node-server)
function isRequestLike(obj: unknown): obj is Request {
return (
obj instanceof Request ||
(typeof obj === "object" &&
obj !== null &&
"url" in obj &&
"method" in obj &&
"headers" in obj &&
typeof (obj as any).url === "string" &&
typeof (obj as any).method === "string")
);
}
return function (
reqOrRequest: IncomingMessage | Request,
res?: ServerResponse,
): Promise<void> | Promise<Response> | Response {
if (isRequestLike(reqOrRequest) && !res) {
return honoApp.fetch(reqOrRequest as Request);
}
if (!res) {
throw new TypeError("ServerResponse is required for Node HTTP requests");
}
return handle(reqOrRequest as IncomingMessage, res);
};
}