Compare commits
2 Commits
e2a8f2dda3
...
46a28f77f0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
46a28f77f0 | ||
|
|
aafc970ae8 |
@ -576,7 +576,7 @@ export async function POST(request: Request) {
|
||||
: "image/jpeg"),
|
||||
}));
|
||||
|
||||
await persistPendingClawdTask({
|
||||
const persistedTask = await persistPendingClawdTask({
|
||||
chatId: parsed.id,
|
||||
userMessageId,
|
||||
assistantMessageId,
|
||||
@ -587,6 +587,22 @@ export async function POST(request: Request) {
|
||||
skillTagName,
|
||||
processingHint,
|
||||
});
|
||||
if (
|
||||
persistedTask &&
|
||||
persistedTask.assistantMessageId !== assistantMessageId
|
||||
) {
|
||||
return NextResponse.json(
|
||||
buildTaskResponse({
|
||||
chatId: parsed.id,
|
||||
userMessageId,
|
||||
assistantMessageId: persistedTask.assistantMessageId,
|
||||
status: persistedTask.status,
|
||||
processingHint:
|
||||
persistedTask.processingHint || DEFAULT_PROCESSING_HINT,
|
||||
}),
|
||||
{ status: 202, headers: { "Retry-After": "2" } }
|
||||
);
|
||||
}
|
||||
|
||||
const userProfile = await getUserById({ id: session.user.id }).catch(
|
||||
() => null
|
||||
|
||||
@ -509,14 +509,16 @@ const CLAWD_IMAGE_ALLOWED_EXTENSIONS = new Set([
|
||||
]);
|
||||
function isClawdSpreadsheetAttachment(
|
||||
url: string,
|
||||
contentType?: string | null
|
||||
contentType?: string | null,
|
||||
name?: string | null
|
||||
) {
|
||||
const normalizedType = String(contentType || "").toLowerCase();
|
||||
return (
|
||||
normalizedType.includes("spreadsheet") ||
|
||||
normalizedType === "text/csv" ||
|
||||
normalizedType === "text/tab-separated-values" ||
|
||||
/\.(xlsx|csv|tsv)(?:$|[?#])/i.test(String(url || ""))
|
||||
/\.(xlsx|csv|tsv)(?:$|[?#])/i.test(String(url || "")) ||
|
||||
/\.(xlsx|csv|tsv)$/i.test(String(name || "").trim())
|
||||
);
|
||||
}
|
||||
|
||||
@ -1032,38 +1034,43 @@ function parseClawdSpreadsheetAttachmentsFromText(
|
||||
.filter((part: any) => part?.type === "text" && part?.text)
|
||||
.map((part: any) => String(part.text))
|
||||
.join("\n");
|
||||
const summaryMatch = text.match(
|
||||
/(?:^|\n)已上传表格:\n((?:- [^\n]+(?:\n|$))+)/m
|
||||
);
|
||||
if (!summaryMatch?.[1]) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const attachments: ClawdSpreadsheetAttachment[] = [];
|
||||
for (const line of summaryMatch[1].trim().split("\n")) {
|
||||
const lineMatch = line.match(/^- (.+?)(?:((.+)))?$/);
|
||||
const name = String(lineMatch?.[1] || "").trim();
|
||||
const details = String(lineMatch?.[2] || "");
|
||||
if (!name) continue;
|
||||
const sectionPattern =
|
||||
/(?:^|\n)(已上传表格|已上传文件):\n((?:- [^\n]+(?:\n|$))+)/gm;
|
||||
for (const sectionMatch of text.matchAll(sectionPattern)) {
|
||||
const sectionType = String(sectionMatch[1] || "");
|
||||
const sectionLines = String(sectionMatch[2] || "");
|
||||
for (const line of sectionLines.trim().split("\n")) {
|
||||
const lineMatch = line.match(/^- (.+?)(?:((.+)))?$/);
|
||||
const name = String(lineMatch?.[1] || "").trim();
|
||||
const details = String(lineMatch?.[2] || "");
|
||||
if (
|
||||
!name ||
|
||||
(sectionType === "已上传文件" &&
|
||||
!isClawdSpreadsheetAttachment("", "", name))
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const sizeMatch = details.match(/([\d.]+)\s*(KB|MB)/i);
|
||||
const sheetCountMatch = details.match(/(\d+)\s*个工作表/);
|
||||
const rowCountMatch = details.match(/(\d+)\s*行/);
|
||||
const sizeValue = Number(sizeMatch?.[1]);
|
||||
const sizeUnit = String(sizeMatch?.[2] || "").toUpperCase();
|
||||
const sizeMatch = details.match(/([\d.]+)\s*(KB|MB)/i);
|
||||
const sheetCountMatch = details.match(/(\d+)\s*个工作表/);
|
||||
const rowCountMatch = details.match(/(\d+)\s*行/);
|
||||
const sizeValue = Number(sizeMatch?.[1]);
|
||||
const sizeUnit = String(sizeMatch?.[2] || "").toUpperCase();
|
||||
|
||||
attachments.push({
|
||||
name,
|
||||
size:
|
||||
Number.isFinite(sizeValue) && sizeValue > 0
|
||||
? Math.round(
|
||||
sizeValue * (sizeUnit === "MB" ? 1024 * 1024 : 1024)
|
||||
)
|
||||
: undefined,
|
||||
sheetCount: sheetCountMatch ? Number(sheetCountMatch[1]) : undefined,
|
||||
rowCount: rowCountMatch ? Number(rowCountMatch[1]) : undefined,
|
||||
truncated: details.includes("已截断"),
|
||||
});
|
||||
attachments.push({
|
||||
name,
|
||||
size:
|
||||
Number.isFinite(sizeValue) && sizeValue > 0
|
||||
? Math.round(
|
||||
sizeValue * (sizeUnit === "MB" ? 1024 * 1024 : 1024)
|
||||
)
|
||||
: undefined,
|
||||
sheetCount: sheetCountMatch ? Number(sheetCountMatch[1]) : undefined,
|
||||
rowCount: rowCountMatch ? Number(rowCountMatch[1]) : undefined,
|
||||
truncated: details.includes("已截断"),
|
||||
});
|
||||
}
|
||||
}
|
||||
return attachments;
|
||||
}
|
||||
@ -1086,11 +1093,7 @@ function getClawdSpreadsheetAttachments(
|
||||
: Array.isArray(videoMetadata.spreadsheetAttachments)
|
||||
? videoMetadata.spreadsheetAttachments
|
||||
: [];
|
||||
if (rawAttachments.length === 0) {
|
||||
return parseClawdSpreadsheetAttachmentsFromText(message);
|
||||
}
|
||||
|
||||
return rawAttachments
|
||||
const normalizedAttachments = rawAttachments
|
||||
.map((attachment: any) => {
|
||||
const name = String(
|
||||
attachment?.name || attachment?.fileName || ""
|
||||
@ -1124,14 +1127,72 @@ function getClawdSpreadsheetAttachments(
|
||||
attachment: ClawdSpreadsheetAttachment | null
|
||||
): attachment is ClawdSpreadsheetAttachment => Boolean(attachment)
|
||||
);
|
||||
|
||||
const knownNames = new Set(
|
||||
normalizedAttachments.map((attachment: ClawdSpreadsheetAttachment) =>
|
||||
attachment.name.toLowerCase()
|
||||
)
|
||||
);
|
||||
const inferredAttachments = [
|
||||
...(((message as any).attachments || []) as any[]),
|
||||
...((message.parts || []).filter((part: any) => part?.type === "file") as any[]),
|
||||
];
|
||||
for (const attachment of inferredAttachments) {
|
||||
const name = String(attachment?.name || attachment?.fileName || "").trim();
|
||||
const url = String(attachment?.url || attachment?.data || "").trim();
|
||||
const contentType = String(
|
||||
attachment?.contentType ||
|
||||
attachment?.content_type ||
|
||||
attachment?.mediaType ||
|
||||
""
|
||||
).trim();
|
||||
if (
|
||||
!name ||
|
||||
knownNames.has(name.toLowerCase()) ||
|
||||
!isClawdSpreadsheetAttachment(url, contentType, name)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
normalizedAttachments.push({ name, contentType: contentType || undefined });
|
||||
knownNames.add(name.toLowerCase());
|
||||
}
|
||||
|
||||
for (const attachment of parseClawdSpreadsheetAttachmentsFromText(message)) {
|
||||
if (knownNames.has(attachment.name.toLowerCase())) continue;
|
||||
normalizedAttachments.push(attachment);
|
||||
knownNames.add(attachment.name.toLowerCase());
|
||||
}
|
||||
|
||||
return normalizedAttachments;
|
||||
}
|
||||
|
||||
function stripClawdSpreadsheetSummary(text: string) {
|
||||
function stripClawdSpreadsheetSummary(
|
||||
text: string,
|
||||
attachments: ClawdSpreadsheetAttachment[]
|
||||
) {
|
||||
const spreadsheetNames = new Set(
|
||||
attachments.map((attachment) => attachment.name.toLowerCase())
|
||||
);
|
||||
return text
|
||||
.replace(
|
||||
/(?:^|\n{2})已上传表格:\n(?:- [^\n]*(?:\n|$))+/g,
|
||||
"\n"
|
||||
)
|
||||
.replace(
|
||||
/(?:^|\n{2})已上传文件:\n((?:- [^\n]*(?:\n|$))+)/g,
|
||||
(_section, lines: string) => {
|
||||
const remainingLines = String(lines)
|
||||
.trim()
|
||||
.split("\n")
|
||||
.filter((line) => {
|
||||
const name = line.replace(/^- /, "").trim().toLowerCase();
|
||||
return !spreadsheetNames.has(name);
|
||||
});
|
||||
return remainingLines.length > 0
|
||||
? `\n\n已上传文件:\n${remainingLines.join("\n")}`
|
||||
: "\n";
|
||||
}
|
||||
)
|
||||
.replace(/\n{3,}/g, "\n\n")
|
||||
.trim();
|
||||
}
|
||||
@ -12344,6 +12405,7 @@ export function CanvasChatPanel({
|
||||
id: userMessageId,
|
||||
role: "user",
|
||||
parts: [{ type: "text", text: optimisticMessageText }],
|
||||
attachments: sanitizedAttachmentsSnapshot,
|
||||
createdAt: optimisticCreatedAt,
|
||||
metadata: {
|
||||
createdAt: optimisticCreatedAt.toISOString(),
|
||||
@ -16216,7 +16278,10 @@ export function CanvasChatPanel({
|
||||
.trim();
|
||||
const text =
|
||||
spreadsheetAttachments.length > 0
|
||||
? stripClawdSpreadsheetSummary(normalizedText)
|
||||
? stripClawdSpreadsheetSummary(
|
||||
normalizedText,
|
||||
spreadsheetAttachments
|
||||
)
|
||||
: normalizedText;
|
||||
if (!text) return;
|
||||
renderedUserParts.push(
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import type { Attachment, ChatMessage } from "@/lib/types";
|
||||
import { isOpenClawChatFileAttachment } from "@/lib/openclaw-chat/media-attachments";
|
||||
|
||||
export const SESSION_TITLE_MAX_LEN = 18;
|
||||
export const MAX_CHAT_TEXT_PART_LENGTH = 2000;
|
||||
@ -80,6 +81,9 @@ export function normalizeChatMediaType(value?: string | null): string {
|
||||
if (ALLOWED_CHAT_MEDIA_TYPES.has(normalized)) {
|
||||
return normalized;
|
||||
}
|
||||
if (isOpenClawChatFileAttachment("", normalized)) {
|
||||
return normalized;
|
||||
}
|
||||
return "image/jpeg";
|
||||
}
|
||||
|
||||
|
||||
@ -8,7 +8,10 @@ import {
|
||||
updateMessageById,
|
||||
} from "@/lib/db/queries";
|
||||
import type { DBMessage } from "@/lib/db/schema";
|
||||
import { encryptOpenClawTaskSecret } from "@/lib/openclaw-chat/task-secret";
|
||||
import {
|
||||
decryptOpenClawTaskSecret,
|
||||
encryptOpenClawTaskSecret,
|
||||
} from "@/lib/openclaw-chat/task-secret";
|
||||
import {
|
||||
appendClawdTaskProgress,
|
||||
CLAWD_TASK_KIND,
|
||||
@ -37,6 +40,32 @@ export type ClawdUpstreamTaskState = {
|
||||
lastEventId?: number | null;
|
||||
};
|
||||
|
||||
export async function findPersistedClawdUpstreamTaskState(params: {
|
||||
chatId: string;
|
||||
taskId: string;
|
||||
}): Promise<ClawdUpstreamTaskState | null> {
|
||||
const messages = await getMessagesByChatId({ id: params.chatId }).catch(
|
||||
() => []
|
||||
);
|
||||
for (const message of messages.toReversed()) {
|
||||
const metadata = normalizeClawdTaskMetadata(message.videoMetadata);
|
||||
if (
|
||||
!metadata ||
|
||||
metadata.upstreamTaskId !== params.taskId ||
|
||||
!metadata.upstreamTaskKey
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
return {
|
||||
taskId: metadata.upstreamTaskId,
|
||||
taskKey: decryptOpenClawTaskSecret(metadata.upstreamTaskKey),
|
||||
eventsEndpoint: metadata.upstreamEventsEndpoint,
|
||||
lastEventId: metadata.upstreamLastEventId,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function buildTaskMetadata(params: {
|
||||
userMessageId: string;
|
||||
assistantMessageId: string;
|
||||
|
||||
@ -2,6 +2,7 @@ import "server-only";
|
||||
|
||||
import {
|
||||
appendPersistedClawdTaskProgress,
|
||||
findPersistedClawdUpstreamTaskState,
|
||||
type ClawdUpstreamTaskState,
|
||||
persistClawdUpstreamTaskState,
|
||||
} from "@/lib/openclaw-chat/task-store";
|
||||
@ -301,19 +302,37 @@ export async function submitOpenClawDurableTask(params: {
|
||||
const task = readRecord(root.task);
|
||||
const taskId = readText(root.task_id) || readText(task.task_id);
|
||||
if (!taskId) throw new Error("OpenClaw 未返回 task_id");
|
||||
const responseTaskKey = readText(root.task_key);
|
||||
const persistedTask = responseTaskKey
|
||||
? null
|
||||
: await findPersistedClawdUpstreamTaskState({
|
||||
chatId: params.target.chatId,
|
||||
taskId,
|
||||
});
|
||||
const taskKey = responseTaskKey || persistedTask?.taskKey || null;
|
||||
if (!taskKey) {
|
||||
throw new OpenClawTaskCredentialUnavailableError(taskId);
|
||||
}
|
||||
const submittedTask = mergeUpstreamTaskState(
|
||||
{
|
||||
taskId,
|
||||
taskKey: readText(root.task_key) || null,
|
||||
eventsEndpoint: getTaskEventsEndpoint(payload, taskId),
|
||||
lastEventId: 0,
|
||||
taskKey,
|
||||
eventsEndpoint:
|
||||
getTaskEventsEndpoint(payload, taskId) ||
|
||||
persistedTask?.eventsEndpoint ||
|
||||
null,
|
||||
lastEventId: persistedTask?.lastEventId || 0,
|
||||
},
|
||||
payload
|
||||
);
|
||||
// A task-creation response can include the latest upstream cursor even though
|
||||
// none of its events have been persisted locally yet. Always consume the
|
||||
// event stream from the beginning after accepting a new durable task.
|
||||
const upstreamTask = { ...submittedTask, lastEventId: 0 };
|
||||
const upstreamTask = {
|
||||
...submittedTask,
|
||||
taskKey,
|
||||
lastEventId: persistedTask?.lastEventId || 0,
|
||||
};
|
||||
await persistClawdUpstreamTaskState({
|
||||
...params.target,
|
||||
upstreamTask,
|
||||
@ -342,6 +361,11 @@ export async function refreshOpenClawDurableTask(params: {
|
||||
target: ClawdTaskPersistenceTarget;
|
||||
signal?: AbortSignal;
|
||||
}): Promise<OpenClawTaskOutcome> {
|
||||
if (!params.upstreamTask.taskKey) {
|
||||
throw new OpenClawTaskCredentialUnavailableError(
|
||||
params.upstreamTask.taskId
|
||||
);
|
||||
}
|
||||
const eventsUrl = buildUpstreamUrl(
|
||||
params.config.baseUrl,
|
||||
params.upstreamTask.eventsEndpoint ||
|
||||
@ -459,6 +483,14 @@ export class OpenClawTaskDetachedError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
export class OpenClawTaskCredentialUnavailableError extends OpenClawTaskDetachedError {
|
||||
constructor(readonly taskId: string) {
|
||||
super();
|
||||
this.name = "OpenClawTaskCredentialUnavailableError";
|
||||
this.message = "OpenClaw task key is unavailable; keep the task detached";
|
||||
}
|
||||
}
|
||||
|
||||
export async function waitForOpenClawDurableTask(params: {
|
||||
config: OpenClawUpstreamConfig;
|
||||
upstreamTask: ClawdUpstreamTaskState;
|
||||
|
||||
@ -235,7 +235,11 @@ import {
|
||||
normalizeUploadedAssetId,
|
||||
validateCanvasImageUploadFile,
|
||||
} from "@/components/canvas/chat-panel/canvas-upload-helpers";
|
||||
import type { PickedObjectAttachment } from "@/components/canvas/chat-panel/canvas-chat-session";
|
||||
import {
|
||||
normalizeChatMediaType,
|
||||
sanitizeChatAttachment,
|
||||
type PickedObjectAttachment,
|
||||
} from "@/components/canvas/chat-panel/canvas-chat-session";
|
||||
import type { CanvasAgentContext } from "@/types/canvas/agent-protocol";
|
||||
import type { ChatMessage } from "@/lib/types";
|
||||
import type { DBMessage } from "@/lib/db/schema";
|
||||
@ -253,6 +257,22 @@ function dbTextMessage(
|
||||
}
|
||||
|
||||
test.describe("canvas chat pure helpers", () => {
|
||||
test("preserves spreadsheet attachment content types", () => {
|
||||
const spreadsheetContentType =
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||
|
||||
expect(normalizeChatMediaType(spreadsheetContentType)).toBe(
|
||||
spreadsheetContentType
|
||||
);
|
||||
expect(
|
||||
sanitizeChatAttachment({
|
||||
name: "测试用.xlsx",
|
||||
url: "https://cdn.example.com/uploads/test.xlsx",
|
||||
contentType: spreadsheetContentType,
|
||||
})?.contentType
|
||||
).toBe(spreadsheetContentType);
|
||||
});
|
||||
|
||||
test("keeps optimistic messages without timestamps after persisted history", () => {
|
||||
const messages = [
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user