fix(cancel): moved to a lib component
This commit is contained in:
parent
d1613c56ba
commit
215d0e6114
3 changed files with 38 additions and 34 deletions
|
|
@ -1,37 +1,5 @@
|
||||||
import { NextRequest } from 'next/server';
|
import { NextRequest } from 'next/server';
|
||||||
|
import { cancelRequest } from '@/lib/cancel-tokens';
|
||||||
// In-memory map to store cancel tokens by messageId
|
|
||||||
const cancelTokens: Record<string, AbortController> = {};
|
|
||||||
|
|
||||||
// Export for use in chat/route.ts
|
|
||||||
export function registerCancelToken(
|
|
||||||
messageId: string,
|
|
||||||
controller: AbortController,
|
|
||||||
) {
|
|
||||||
cancelTokens[messageId] = controller;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function cleanupCancelToken(messageId: string) {
|
|
||||||
var cancelled = false;
|
|
||||||
if (messageId in cancelTokens) {
|
|
||||||
delete cancelTokens[messageId];
|
|
||||||
cancelled = true;
|
|
||||||
}
|
|
||||||
return cancelled;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function cancelRequest(messageId: string) {
|
|
||||||
const controller = cancelTokens[messageId];
|
|
||||||
if (controller) {
|
|
||||||
try {
|
|
||||||
controller.abort();
|
|
||||||
} catch (e) {
|
|
||||||
console.error(`Error aborting request for messageId ${messageId}:`, e);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function POST(req: NextRequest) {
|
export async function POST(req: NextRequest) {
|
||||||
const { messageId } = await req.json();
|
const { messageId } = await req.json();
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ import { EventEmitter } from 'stream';
|
||||||
import {
|
import {
|
||||||
registerCancelToken,
|
registerCancelToken,
|
||||||
cleanupCancelToken,
|
cleanupCancelToken,
|
||||||
} from './cancel/route';
|
} from '@/lib/cancel-tokens';
|
||||||
|
|
||||||
export const runtime = 'nodejs';
|
export const runtime = 'nodejs';
|
||||||
export const dynamic = 'force-dynamic';
|
export const dynamic = 'force-dynamic';
|
||||||
|
|
|
||||||
36
src/lib/cancel-tokens.ts
Normal file
36
src/lib/cancel-tokens.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
export {};
|
||||||
|
|
||||||
|
// In-memory map to store cancel tokens by messageId
|
||||||
|
const cancelTokens: Record<string, AbortController> = {};
|
||||||
|
|
||||||
|
// Register a cancel token for a message ID
|
||||||
|
export function registerCancelToken(
|
||||||
|
messageId: string,
|
||||||
|
controller: AbortController,
|
||||||
|
) {
|
||||||
|
cancelTokens[messageId] = controller;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove a cancel token from the map
|
||||||
|
export function cleanupCancelToken(messageId: string) {
|
||||||
|
var cancelled = false;
|
||||||
|
if (messageId in cancelTokens) {
|
||||||
|
delete cancelTokens[messageId];
|
||||||
|
cancelled = true;
|
||||||
|
}
|
||||||
|
return cancelled;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cancel a request by its message ID
|
||||||
|
export function cancelRequest(messageId: string) {
|
||||||
|
const controller = cancelTokens[messageId];
|
||||||
|
if (controller) {
|
||||||
|
try {
|
||||||
|
controller.abort();
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`Error aborting request for messageId ${messageId}:`, e);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue