feat(ai): confirm before resending a chat message
This commit is contained in:
14 files changed
+118
-3
No files matched your search
+25
-1
@@ -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", () => {
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user