-
Notifications
You must be signed in to change notification settings - Fork 647
fix: rate limit #1422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: rate limit #1422
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,12 @@ import type { | |
| } from '@shared/types/agent-interface' | ||
| import type { MCPToolCall, MCPToolResponse } from '@shared/types/core/mcp' | ||
| import type { ChatMessage } from '@shared/types/core/chat-message' | ||
| import type { IConfigPresenter, ILlmProviderPresenter, ModelConfig } from '@shared/presenter' | ||
| import type { | ||
| IConfigPresenter, | ||
| ILlmProviderPresenter, | ||
| ModelConfig, | ||
| RateLimitQueueSnapshot | ||
| } from '@shared/presenter' | ||
| import type { MCPToolDefinition } from '@shared/types/core/mcp' | ||
| import type { IToolPresenter } from '@shared/types/presenters/tool.presenter' | ||
| import type { ReasoningPortrait } from '@shared/types/model-db' | ||
|
|
@@ -114,6 +119,8 @@ const isReasoningEffort = (value: unknown): value is 'minimal' | 'low' | 'medium | |
| const isVerbosity = (value: unknown): value is 'low' | 'medium' | 'high' => | ||
| value === 'low' || value === 'medium' || value === 'high' | ||
|
|
||
| const RATE_LIMIT_STREAM_MESSAGE_PREFIX = '__rate_limit__:' | ||
|
|
||
| const createAbortError = (): Error => { | ||
| if (typeof DOMException !== 'undefined') { | ||
| return new DOMException('Aborted', 'AbortError') | ||
|
|
@@ -1342,6 +1349,7 @@ export class DeepChatAgentPresenter implements IAgentImplementation { | |
| } | ||
|
|
||
| const traceEnabled = this.configPresenter.getSetting<boolean>('traceDebugEnabled') === true | ||
| const llmProviderPresenter = this.llmProviderPresenter | ||
| const pendingInputCoordinator = this.pendingInputCoordinator | ||
| const injectSteerInputsIntoRequest = this.injectSteerInputsIntoRequest.bind(this) | ||
| const persistMessageTrace = this.persistMessageTrace.bind(this) | ||
|
|
@@ -1374,6 +1382,9 @@ export class DeepChatAgentPresenter implements IAgentImplementation { | |
|
|
||
| const abortController = new AbortController() | ||
| const activeGeneration = this.registerActiveGeneration(sessionId, messageId, abortController) | ||
| const rateLimitMessageId = this.buildRateLimitStreamMessageId(activeGeneration.runId) | ||
| const emitRateLimitWaitingMessage = this.emitRateLimitWaitingMessage.bind(this) | ||
| const clearRateLimitWaitingMessage = this.clearRateLimitWaitingMessage.bind(this) | ||
|
|
||
| try { | ||
| this.dispatchHook('SessionStart', { | ||
|
|
@@ -1407,8 +1418,21 @@ export class DeepChatAgentPresenter implements IAgentImplementation { | |
| ) | ||
|
|
||
| let didConsumeSteerBatch = false | ||
| let queuedForRateLimit = false | ||
|
|
||
| try { | ||
| await llmProviderPresenter.executeWithRateLimit(state.providerId, { | ||
| signal: abortController.signal, | ||
| onQueued: (snapshot) => { | ||
| queuedForRateLimit = true | ||
| emitRateLimitWaitingMessage(sessionId, rateLimitMessageId, snapshot) | ||
| } | ||
| }) | ||
| if (queuedForRateLimit) { | ||
| clearRateLimitWaitingMessage(sessionId, rateLimitMessageId) | ||
| queuedForRateLimit = false | ||
| } | ||
|
Comment on lines
+1484
to
+1494
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Re-check cancellation after the rate-limit wait. After 💡 Suggested fix await llmProviderPresenter.executeWithRateLimit(state.providerId, {
signal: abortController.signal,
onQueued: (snapshot) => {
queuedForRateLimit = true
emitRateLimitWaitingMessage(sessionId, rateLimitMessageId, snapshot)
}
})
+ this.throwIfAbortRequested(abortController.signal)
if (queuedForRateLimit) {
clearRateLimitWaitingMessage(sessionId, rateLimitMessageId)
queuedForRateLimit = false
}🤖 Prompt for AI Agents |
||
|
|
||
| for await (const event of provider.coreStream( | ||
| injectedMessages, | ||
| requestModelId, | ||
|
|
@@ -1428,6 +1452,9 @@ export class DeepChatAgentPresenter implements IAgentImplementation { | |
| pendingInputCoordinator.consumeClaimedSteerBatch(sessionId) | ||
| } | ||
| } catch (error) { | ||
| if (queuedForRateLimit) { | ||
| clearRateLimitWaitingMessage(sessionId, rateLimitMessageId) | ||
| } | ||
| if (!didConsumeSteerBatch && claimedSteerBatch.length > 0) { | ||
| pendingInputCoordinator.releaseClaimedInputs(sessionId) | ||
| } | ||
|
|
@@ -1669,6 +1696,47 @@ export class DeepChatAgentPresenter implements IAgentImplementation { | |
| return this.activeGenerations.get(sessionId)?.runId === runId | ||
| } | ||
|
|
||
| private buildRateLimitStreamMessageId(runId: string): string { | ||
| return `${RATE_LIMIT_STREAM_MESSAGE_PREFIX}${runId}` | ||
| } | ||
|
|
||
| private emitRateLimitWaitingMessage( | ||
| sessionId: string, | ||
| messageId: string, | ||
| snapshot: RateLimitQueueSnapshot | ||
| ): void { | ||
| const block: AssistantMessageBlock = { | ||
| type: 'action', | ||
| action_type: 'rate_limit', | ||
| content: '', | ||
| status: 'pending', | ||
| timestamp: Date.now(), | ||
| extra: { | ||
| providerId: snapshot.providerId, | ||
| qpsLimit: snapshot.qpsLimit, | ||
| currentQps: snapshot.currentQps, | ||
| queueLength: snapshot.queueLength, | ||
| estimatedWaitTime: snapshot.estimatedWaitTime | ||
| } | ||
| } | ||
|
|
||
| eventBus.sendToRenderer(STREAM_EVENTS.RESPONSE, SendTarget.ALL_WINDOWS, { | ||
| conversationId: sessionId, | ||
| eventId: messageId, | ||
| messageId, | ||
| blocks: [block] | ||
| }) | ||
| } | ||
|
|
||
| private clearRateLimitWaitingMessage(sessionId: string, messageId: string): void { | ||
| eventBus.sendToRenderer(STREAM_EVENTS.RESPONSE, SendTarget.ALL_WINDOWS, { | ||
| conversationId: sessionId, | ||
| eventId: messageId, | ||
| messageId, | ||
| blocks: [] | ||
| }) | ||
| } | ||
|
|
||
| private applyProcessResultStatus( | ||
| sessionId: string, | ||
| result: ProcessResult | null | undefined, | ||
|
|
@@ -3207,6 +3275,9 @@ export class DeepChatAgentPresenter implements IAgentImplementation { | |
| visionModel.modelId, | ||
| visionModel.providerId | ||
| ) | ||
| await this.llmProviderPresenter.executeWithRateLimit(visionModel.providerId, { | ||
| signal: abortSignal | ||
| }) | ||
| const response = await this.llmProviderPresenter.generateCompletionStandalone( | ||
| visionModel.providerId, | ||
| messages, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.