update(README): Updated to reflect features in this fork

fix(cancel): moved to a lib component
This commit is contained in:
Willie Zutz
2025-05-14 13:25:06 -06:00
parent 9c7ccf42fc
commit e4f695ec9b
5 changed files with 78 additions and 45 deletions

View File

@@ -1,37 +1,5 @@
import { NextRequest } from 'next/server';
// 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;
}
import { cancelRequest } from '@/lib/cancel-tokens';
export async function POST(req: NextRequest) {
const { messageId } = await req.json();

View File

@@ -21,7 +21,7 @@ import { EventEmitter } from 'stream';
import {
registerCancelToken,
cleanupCancelToken,
} from './cancel/route';
} from '@/lib/cancel-tokens';
export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';

36
src/lib/cancel-tokens.ts Normal file
View 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;
}