Upload 14 files
Browse files- src/lightweight-client-express.js +2 -5
- src/lightweight-client.js +21 -14
src/lightweight-client-express.js
CHANGED
|
@@ -209,9 +209,6 @@ app.post('/v1/chat/completions', authenticate, async (req, res) => {
|
|
| 209 |
});
|
| 210 |
}
|
| 211 |
|
| 212 |
-
// 构建Notion请求
|
| 213 |
-
const notionRequestBody = buildNotionRequest(requestData);
|
| 214 |
-
|
| 215 |
// 处理流式响应
|
| 216 |
if (requestData.stream) {
|
| 217 |
res.setHeader('Content-Type', 'text/event-stream');
|
|
@@ -219,7 +216,7 @@ app.post('/v1/chat/completions', authenticate, async (req, res) => {
|
|
| 219 |
res.setHeader('Connection', 'keep-alive');
|
| 220 |
|
| 221 |
logger.info(`开始流式响应`);
|
| 222 |
-
const stream = await streamNotionResponse(
|
| 223 |
stream.pipe(res);
|
| 224 |
|
| 225 |
// 处理客户端断开连接
|
|
@@ -231,7 +228,7 @@ app.post('/v1/chat/completions', authenticate, async (req, res) => {
|
|
| 231 |
// 创建一个内部流来收集完整响应
|
| 232 |
logger.info(`开始非流式响应`);
|
| 233 |
const chunks = [];
|
| 234 |
-
const stream = await streamNotionResponse(
|
| 235 |
|
| 236 |
return new Promise((resolve, reject) => {
|
| 237 |
stream.on('data', (chunk) => {
|
|
|
|
| 209 |
});
|
| 210 |
}
|
| 211 |
|
|
|
|
|
|
|
|
|
|
| 212 |
// 处理流式响应
|
| 213 |
if (requestData.stream) {
|
| 214 |
res.setHeader('Content-Type', 'text/event-stream');
|
|
|
|
| 216 |
res.setHeader('Connection', 'keep-alive');
|
| 217 |
|
| 218 |
logger.info(`开始流式响应`);
|
| 219 |
+
const stream = await streamNotionResponse(requestData);
|
| 220 |
stream.pipe(res);
|
| 221 |
|
| 222 |
// 处理客户端断开连接
|
|
|
|
| 228 |
// 创建一个内部流来收集完整响应
|
| 229 |
logger.info(`开始非流式响应`);
|
| 230 |
const chunks = [];
|
| 231 |
+
const stream = await streamNotionResponse(requestData);
|
| 232 |
|
| 233 |
return new Promise((resolve, reject) => {
|
| 234 |
stream.on('data', (chunk) => {
|
src/lightweight-client.js
CHANGED
|
@@ -311,7 +311,7 @@ async function fetchAndSetNotionIds(cookie) {
|
|
| 311 |
}
|
| 312 |
|
| 313 |
// 构建Notion请求
|
| 314 |
-
function buildNotionRequest(requestData) {
|
| 315 |
// 当前时间
|
| 316 |
const now = new Date();
|
| 317 |
// 格式化为ISO字符串,确保包含毫秒和时区
|
|
@@ -346,8 +346,8 @@ function buildNotionRequest(requestData) {
|
|
| 346 |
transcript.push(new NotionTranscriptItem({
|
| 347 |
type: "context",
|
| 348 |
value: new NotionTranscriptContextValue({
|
| 349 |
-
userId:
|
| 350 |
-
spaceId:
|
| 351 |
surface: "home_module",
|
| 352 |
timezone: "America/Los_Angeles",
|
| 353 |
userName: userName,
|
|
@@ -387,7 +387,7 @@ function buildNotionRequest(requestData) {
|
|
| 387 |
transcript.push(new NotionTranscriptItemByuser({
|
| 388 |
type: "user",
|
| 389 |
value: [[content]],
|
| 390 |
-
userId:
|
| 391 |
createdAt: message.createdAt || isoString
|
| 392 |
}));
|
| 393 |
} else if (message.role === "user") {
|
|
@@ -395,7 +395,7 @@ function buildNotionRequest(requestData) {
|
|
| 395 |
transcript.push(new NotionTranscriptItemByuser({
|
| 396 |
type: "user",
|
| 397 |
value: [[content]],
|
| 398 |
-
userId:
|
| 399 |
createdAt: message.createdAt || isoString
|
| 400 |
}));
|
| 401 |
} else if (message.role === "assistant") {
|
|
@@ -411,7 +411,7 @@ function buildNotionRequest(requestData) {
|
|
| 411 |
|
| 412 |
// 创建请求体
|
| 413 |
return new NotionRequestBody({
|
| 414 |
-
spaceId:
|
| 415 |
transcript: transcript,
|
| 416 |
createThread: true,
|
| 417 |
traceId: randomUUID(),
|
|
@@ -426,24 +426,31 @@ function buildNotionRequest(requestData) {
|
|
| 426 |
}
|
| 427 |
|
| 428 |
// 流式处理Notion响应
|
| 429 |
-
async function streamNotionResponse(
|
| 430 |
const chunkQueue = new PassThrough();
|
| 431 |
let timeoutId = null;
|
| 432 |
|
| 433 |
try {
|
| 434 |
-
// 动态获取cookie
|
| 435 |
const currentCookie = getCookie();
|
| 436 |
if (!currentCookie) {
|
| 437 |
-
throw new Error("无法获取 Notion cookie");
|
| 438 |
}
|
| 439 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
|
| 441 |
-
//
|
| 442 |
-
notionRequestBody.spaceId = NOTION_SPACE_IDS[currentCookie];
|
| 443 |
const headers = {
|
| 444 |
'Content-Type': 'application/json',
|
| 445 |
'Cookie': currentCookie,
|
| 446 |
-
'x-notion-active-user-header':
|
| 447 |
"accept": "application/json",
|
| 448 |
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"
|
| 449 |
};
|
|
@@ -872,7 +879,7 @@ async function initialize() {
|
|
| 872 |
for (const cookie of NOTION_COOKIES) {
|
| 873 |
try {
|
| 874 |
const ids = await fetchAndSetNotionIds(cookie);
|
| 875 |
-
if (ids) {
|
| 876 |
NOTION_SPACE_IDS[cookie] = ids.spaceId;
|
| 877 |
NOTION_USER_IDS[cookie] = ids.userId;
|
| 878 |
logger.success(`成功获取 cookie 的 ID: ...${cookie.slice(-10)}`);
|
|
|
|
| 311 |
}
|
| 312 |
|
| 313 |
// 构建Notion请求
|
| 314 |
+
function buildNotionRequest(requestData, userId, spaceId) {
|
| 315 |
// 当前时间
|
| 316 |
const now = new Date();
|
| 317 |
// 格式化为ISO字符串,确保包含毫秒和时区
|
|
|
|
| 346 |
transcript.push(new NotionTranscriptItem({
|
| 347 |
type: "context",
|
| 348 |
value: new NotionTranscriptContextValue({
|
| 349 |
+
userId: userId,
|
| 350 |
+
spaceId: spaceId,
|
| 351 |
surface: "home_module",
|
| 352 |
timezone: "America/Los_Angeles",
|
| 353 |
userName: userName,
|
|
|
|
| 387 |
transcript.push(new NotionTranscriptItemByuser({
|
| 388 |
type: "user",
|
| 389 |
value: [[content]],
|
| 390 |
+
userId: userId,
|
| 391 |
createdAt: message.createdAt || isoString
|
| 392 |
}));
|
| 393 |
} else if (message.role === "user") {
|
|
|
|
| 395 |
transcript.push(new NotionTranscriptItemByuser({
|
| 396 |
type: "user",
|
| 397 |
value: [[content]],
|
| 398 |
+
userId: userId,
|
| 399 |
createdAt: message.createdAt || isoString
|
| 400 |
}));
|
| 401 |
} else if (message.role === "assistant") {
|
|
|
|
| 411 |
|
| 412 |
// 创建请求体
|
| 413 |
return new NotionRequestBody({
|
| 414 |
+
spaceId: spaceId,
|
| 415 |
transcript: transcript,
|
| 416 |
createThread: true,
|
| 417 |
traceId: randomUUID(),
|
|
|
|
| 426 |
}
|
| 427 |
|
| 428 |
// 流式处理Notion响应
|
| 429 |
+
async function streamNotionResponse(requestData) {
|
| 430 |
const chunkQueue = new PassThrough();
|
| 431 |
let timeoutId = null;
|
| 432 |
|
| 433 |
try {
|
| 434 |
+
// 动态获取cookie并查找其对应的ID
|
| 435 |
const currentCookie = getCookie();
|
| 436 |
if (!currentCookie) {
|
| 437 |
+
throw new Error("无法获取 Notion cookie,请检查配置");
|
| 438 |
}
|
| 439 |
+
const userId = NOTION_USER_IDS[currentCookie];
|
| 440 |
+
const spaceId = NOTION_SPACE_IDS[currentCookie];
|
| 441 |
+
|
| 442 |
+
if (!userId || !spaceId) {
|
| 443 |
+
throw new Error(`无法找到 cookie ...${currentCookie.slice(-10)} 对应的ID`);
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
// 使用正确的ID构建请求体
|
| 447 |
+
const notionRequestBody = buildNotionRequest(requestData, userId, spaceId);
|
| 448 |
|
| 449 |
+
// 设置请求头
|
|
|
|
| 450 |
const headers = {
|
| 451 |
'Content-Type': 'application/json',
|
| 452 |
'Cookie': currentCookie,
|
| 453 |
+
'x-notion-active-user-header': userId,
|
| 454 |
"accept": "application/json",
|
| 455 |
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"
|
| 456 |
};
|
|
|
|
| 879 |
for (const cookie of NOTION_COOKIES) {
|
| 880 |
try {
|
| 881 |
const ids = await fetchAndSetNotionIds(cookie);
|
| 882 |
+
if (ids && ids.spaceId && ids.userId) {
|
| 883 |
NOTION_SPACE_IDS[cookie] = ids.spaceId;
|
| 884 |
NOTION_USER_IDS[cookie] = ids.userId;
|
| 885 |
logger.success(`成功获取 cookie 的 ID: ...${cookie.slice(-10)}`);
|