mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
feat(app): keep 100 more text-only prompts in prompt history (#62313)
This commit is contained in:
committed by
GitHub
Unverified
parent
05620f8ff9
commit
a884e040c9
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import type { Prompt } from "@/composer/state"
|
||||
import { prependHistoryEntry, removeHistoryEntry, type PromptHistoryComment } from "./entry"
|
||||
import { prependHistoryEntry, removeHistoryEntry, type PromptHistoryComment, type PromptHistoryEntry } from "./entry"
|
||||
|
||||
const DEFAULT_PROMPT: Prompt = [{ type: "text", content: "", start: 0, end: 0 }]
|
||||
|
||||
@@ -53,6 +53,27 @@ describe("Composer history", () => {
|
||||
expect(removed).toEqual(prependHistoryEntry([], text("earlier")))
|
||||
})
|
||||
|
||||
test("prependHistoryEntry keeps 100 recent entries and up to 100 older attachment-free ones", () => {
|
||||
const attached = (index: number): Prompt => [
|
||||
...text(`shot ${index}`),
|
||||
index % 2
|
||||
? { type: "image", id: `img${index}`, filename: "a.png", mime: "image/png", blob: { id: `h${index}`, url: "" } }
|
||||
: { type: "path", id: `att${index}`, filename: "a.mp4", mime: "video/mp4", path: `/tmp/${index}.mp4` },
|
||||
]
|
||||
|
||||
const sent = Array.from({ length: 300 }, (_, index) => (index % 3 ? text(`prompt ${index}`) : attached(index)))
|
||||
const mixed = sent.reduce<PromptHistoryEntry[]>((entries, prompt) => prependHistoryEntry(entries, prompt), [])
|
||||
const newest = sent.toReversed()
|
||||
|
||||
expect(mixed.slice(0, 100).map((entry) => entry.prompt)).toEqual(newest.slice(0, 100))
|
||||
expect(mixed.slice(100).map((entry) => entry.prompt)).toEqual(
|
||||
newest
|
||||
.slice(100)
|
||||
.filter((prompt) => prompt.length === 1)
|
||||
.slice(0, 100),
|
||||
)
|
||||
})
|
||||
|
||||
test("insertion isolates canonical entries from source mutations", () => {
|
||||
const prompt: Prompt = [
|
||||
{
|
||||
|
||||
@@ -5,7 +5,11 @@ import type { PromptHistoryComment, PromptHistoryEntry } from "../schema"
|
||||
|
||||
export type { PromptHistoryComment, PromptHistoryEntry } from "../schema"
|
||||
|
||||
const MAX_HISTORY = 100
|
||||
// Attachments keep their bytes pinned for as long as history references them, so only the most
|
||||
// recent entries may carry them; older entries are kept only when they are attachment-free.
|
||||
const MAX_RECENT_HISTORY = 100
|
||||
|
||||
const MAX_HISTORY = 200
|
||||
|
||||
export type PromptHistoryStoredEntry = PromptHistoryEntry
|
||||
|
||||
@@ -33,7 +37,6 @@ export function prependHistoryEntry(
|
||||
entries: PromptHistoryStoredEntry[],
|
||||
prompt: Prompt,
|
||||
comments: PromptHistoryComment[] = [],
|
||||
max = MAX_HISTORY,
|
||||
) {
|
||||
const text = prompt
|
||||
.map((part) => ("content" in part ? part.content : ""))
|
||||
@@ -54,7 +57,9 @@ export function prependHistoryEntry(
|
||||
|
||||
if (last && isPromptEqual(last, entry)) return entries
|
||||
|
||||
return [entry, ...entries].slice(0, max)
|
||||
return [entry, ...entries]
|
||||
.filter((item, index) => index < MAX_RECENT_HISTORY || !item.prompt.some(isAttachment))
|
||||
.slice(0, MAX_HISTORY)
|
||||
}
|
||||
|
||||
// A send that failed puts its prompt back in the composer, so the entry recorded for it would
|
||||
|
||||
Reference in New Issue
Block a user