fix(web): add prompt cache affinity to canvas agent (#74)

Co-authored-by: Tiantianr <260026737+Tiantianr@users.noreply.github.com>
This commit is contained in:
Tiantianr
2026-08-02 05:28:02 +08:00
committed by GitHub
parent c574e2d72e
commit e37c1114b3
4 changed files with 64 additions and 10 deletions

View File

@@ -23,6 +23,7 @@ import { NODE_DEFAULT_SIZE } from "@/constant/canvas";
import { CanvasNodeType, type CanvasAssistantMessage, type CanvasAssistantPendingBackendSession, type CanvasAssistantReference, type CanvasAssistantSession, type CanvasNodeData } from "@/types/canvas";
import { useCanvasAgentStore } from "@/stores/canvas/use-canvas-agent-store";
import { previewCanvasAgentOps, summarizeCanvasAgentOps, type CanvasAgentOp, type CanvasAgentOperationImpact, type CanvasAgentSnapshot } from "@/lib/canvas/canvas-agent-ops";
import { canvasAgentPromptCacheKey } from "@/lib/openai-prompt-cache";
export const CANVAS_AGENT_PANEL_MOTION_MS = 500;
const PANEL_MOTION_SECONDS = CANVAS_AGENT_PANEL_MOTION_MS / 1000;
@@ -412,7 +413,7 @@ export function CanvasAssistantPanel({ nodes, selectedNodeIds, snapshot, project
const result = await requestToolResponse({ ...requestConfig, systemPrompt: "" }, messages, ONLINE_AGENT_TOOLS, "required", (text) => {
streamed = text;
if (text.trim()) upsertMessage(sessionId, { id: assistantId, role: "assistant", text });
});
}, { promptCacheKey: canvasAgentPromptCacheKey(sessionId) });
addOnlineLog("模型工具回复", result);
if (result.toolCalls.length) {
const writableCalls = result.toolCalls.filter(isWritableToolCall);
@@ -468,7 +469,7 @@ export function CanvasAssistantPanel({ nodes, selectedNodeIds, snapshot, project
const next = await requestToolResponse({ ...requestConfig, systemPrompt: "" }, nextMessages, ONLINE_AGENT_TOOLS, "auto", (text) => {
streamed = text;
if (text.trim()) upsertMessage(sessionId, { id: assistantId, role: "assistant", text });
});
}, { promptCacheKey: canvasAgentPromptCacheKey(sessionId) });
addOnlineLog(`Agent Tool Loop ${step + 1} 回复`, next);
if (next.toolCalls.length) {
const writableCalls = next.toolCalls.filter(isWritableToolCall);

View File

@@ -0,0 +1,17 @@
export const OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH = 64;
export function clampOpenAIPromptCacheKey(value?: string) {
const key = value?.trim();
if (!key) return undefined;
return Array.from(key).slice(0, OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH).join("");
}
export function canvasAgentPromptCacheKey(sessionId: string) {
const value = sessionId.trim();
return value ? clampOpenAIPromptCacheKey(`canvas-agent:${value}`) : undefined;
}
export function withOpenAIPromptCacheKey<T extends Record<string, unknown>>(body: T, value?: string): T & { prompt_cache_key?: string } {
const key = clampOpenAIPromptCacheKey(value);
return key ? { ...body, prompt_cache_key: key } : body;
}

View File

@@ -7,6 +7,7 @@ import { buildImageReferencePromptText } from "@/lib/image-reference-prompt";
import { channelRequest } from "@/services/api/custom-channel-relay";
import { imageToDataUrl } from "@/services/image-storage";
import type { ReferenceImage } from "@/types/image";
import { withOpenAIPromptCacheKey } from "@/lib/openai-prompt-cache";
export type AiTextMessage = {
role: "system" | "user" | "assistant";
@@ -100,7 +101,7 @@ type GeminiPayload = {
promptFeedback?: { blockReason?: string };
};
type GeminiStreamState = { buffer: string; text: string; toolCalls: ResponseToolCall[]; error?: string };
type RequestOptions = { signal?: AbortSignal };
type RequestOptions = { signal?: AbortSignal; promptCacheKey?: string };
const QUALITY_BASE: Record<string, number> = {
low: 1024,
@@ -945,13 +946,21 @@ export async function requestToolResponse(config: AiConfig, messages: ResponseIn
parallel_tool_calls: false,
}, onDelta, options);
}
return await requestStreamingResponse(requestConfig, {
model: requestConfig.model,
input: toResponseInput(withSystemMessage(requestConfig, messages)),
tools: tools.map(toResponseTool),
tool_choice: toolChoice,
parallel_tool_calls: false,
}, onDelta, options);
return await requestStreamingResponse(
requestConfig,
withOpenAIPromptCacheKey(
{
model: requestConfig.model,
input: toResponseInput(withSystemMessage(requestConfig, messages)),
tools: tools.map(toResponseTool),
tool_choice: toolChoice,
parallel_tool_calls: false,
},
options?.promptCacheKey,
),
onDelta,
options,
);
} catch (error) {
throw new Error(readAxiosError(error, "请求失败"));
}