From acb05dbeee776e3f4490045c163054f8e396a701 Mon Sep 17 00:00:00 2001 From: Han Xiao Date: Tue, 18 Feb 2025 13:47:37 +0800 Subject: [PATCH] fix: streaming and query gen --- src/agent.ts | 2 +- src/app.ts | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/agent.ts b/src/agent.ts index 9d8db78..818ef0f 100644 --- a/src/agent.ts +++ b/src/agent.ts @@ -35,7 +35,7 @@ function getSchema(allowReflect: boolean, allowRead: boolean, allowAnswer: boole if (allowSearch) { actions.push("search"); properties.searchQuery = z.string().max(30) - .describe("Required when action='search'. Must be a short, keyword-based query that BM25, tf-idf based search engines can understand. Existing queries must be avoided").optional(); + .describe("Required when action='search'. Must be a short, keyword-based query that BM25, tf-idf based search engines can understand. Must in ${languageStyle}.").optional(); } if (allowCoding) { diff --git a/src/app.ts b/src/app.ts index 759a031..fd8b546 100644 --- a/src/app.ts +++ b/src/app.ts @@ -226,22 +226,22 @@ function calculateDelay(chunk: string, burstMode: boolean): number { // Special handling for URLs if (chunk.match(/^https?:\/\//)) { - return Math.random() * 50 + 100; // Slower typing for URLs + return Math.random() * 50 + 10; // Slower typing for URLs } // Special handling for CJK characters if (/^[\u4e00-\u9fff\u3040-\u30ff\uac00-\ud7af]$/.test(chunk)) { - return Math.random() * 100 + 10; // Longer delay for individual CJK characters + return Math.random() * 50 + 10; // Longer delay for individual CJK characters } // Base delay calculation let baseDelay; if (burstMode) { - baseDelay = Math.random() * 30 + 20; + baseDelay = Math.random() * 30 + 10; } else { const effectiveLength = getEffectiveLength(chunk); const perCharacterDelay = Math.max(10, 40 - effectiveLength * 2); - baseDelay = Math.random() * perCharacterDelay + perCharacterDelay; + baseDelay = Math.random() * perCharacterDelay + 10; } // Add variance based on chunk characteristics @@ -250,14 +250,14 @@ function calculateDelay(chunk: string, burstMode: boolean): number { } if (/[^a-zA-Z\s]/.test(chunk)) { - baseDelay += Math.random() * 30 + 15; + baseDelay += Math.random() * 30 + 10; } // Add pauses for punctuation if (/[.!?]$/.test(chunk)) { - baseDelay += Math.random() * 350 + 200; + baseDelay += Math.random() * 200 + 10; } else if (/[,;:]$/.test(chunk)) { - baseDelay += Math.random() * 150 + 100; + baseDelay += Math.random() * 100 + 10; } return baseDelay; @@ -550,6 +550,8 @@ app.post('/v1/chat/completions', (async (req: Request, res: Response) => { if (step.think) { const content = step.think; await new Promise(resolve => { + streamingState.currentlyStreaming = false; + streamingState.queue.push({ content, resolve