feat(ai): confirm before resending a chat message

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-09-26 21:46:05 +08:00
1 parent 3829513cfb
commit 572757b933
14 files changed
+118 -3

No files matched your search

+25 -1
View File
@@ -1,5 +1,29 @@
import { describe, expect, it } from "vitest";
import { visibleToActualIndex } from "@/lib/ai/aiMessageEdit";
import { retryableUserMessageIndex, visibleToActualIndex } from "@/lib/ai/aiMessageEdit";
describe("retryableUserMessageIndex", () => {
it("finds the closest preceding user turn", () => {
const messages = [
{ role: "user" as const, content: "first" },
{ role: "assistant" as const, content: "reply" },
{ role: "user" as const, content: "latest" },
{ role: "assistant" as const, content: "latest reply" },
];
expect(retryableUserMessageIndex(messages, 3)).toBe(2);
});
it("keeps attachment-only requests retryable", () => {
const messages = [
{ role: "user" as const, content: "", imageAttachments: [{}] },
{ role: "assistant" as const, content: "reply" },
];
expect(retryableUserMessageIndex(messages, 1)).toBe(0);
});
it("returns -1 when no preceding user request exists", () => {
expect(retryableUserMessageIndex([{ role: "assistant", content: "reply" }], 0)).toBe(-1);
});
});
describe("visibleToActualIndex", () => {
it("maps visible index 0 to first non-summary message", () => {
+20
View File
@@ -2,6 +2,26 @@ export interface MessageWithKind {
kind?: string;
}
export interface RetryableMessage {
role: "user" | "assistant";
content: string;
mentions?: unknown[];
csvAttachments?: unknown[];
imageAttachments?: unknown[];
}
/** Finds the closest preceding user turn that can be submitted again for an
* assistant reply in the visible transcript. */
export function retryableUserMessageIndex(messages: RetryableMessage[], assistantVisibleIndex: number): number {
for (let i = assistantVisibleIndex - 1; i >= 0; i--) {
const message = messages[i];
if (message.role !== "user") continue;
if (message.content.trim() || message.mentions?.length || message.csvAttachments?.length || message.imageAttachments?.length) return i;
return -1;
}
return -1;
}
/**
* Maps a visible message index (contextSummary messages excluded) to the
* actual index in the full messages array.