|
| 1 | +/// <reference types="vite/client" /> |
| 2 | +import { createEffect, Show } from "solid-js" |
| 3 | +import { createStore } from "solid-js/store" |
| 4 | +import { render } from "solid-js/web" |
| 5 | +import { PlatformProvider, type Platform } from "@/runtime/platform/platform" |
| 6 | +import { createBrowserDraftStore } from "@/runtime/persistence/drafts" |
| 7 | +import { createComposerHistory } from "@/composer/history/store" |
| 8 | +import { ComposerEditor } from "@/composer/editor/editor" |
| 9 | +import { createComposerEditor } from "@/composer/editor/interaction" |
| 10 | +import type { ComposerPersistedState } from "@/composer/types" |
| 11 | +import "@/index.css" |
| 12 | + |
| 13 | +const shape = new URLSearchParams(location.search).get("shape") ?? "text" |
| 14 | +const normal = Array.from({ length: 100 }, (_, index) => { |
| 15 | + const content = |
| 16 | + `Review the retry policy in src/network/request-${index}.ts. Preserve cancellation and the existing error messages.\n\n` + |
| 17 | + `The request should stop after three attempts. Add coverage for a 429 response, a connection reset, and a successful retry. Verify that only idempotent requests are retried.\n\n` + |
| 18 | + `Report ${index}:\n\`\`\`ts\nexport async function request(input: Request) {\n const response = await fetch(input)\n if (!response.ok) throw new Error(response.statusText)\n return response.json()\n}\n\`\`\`` |
| 19 | + return { |
| 20 | + prompt: [ |
| 21 | + { type: "text", content, start: 0, end: content.length }, |
| 22 | + ...(shape !== "text" && index % 2 === 0 |
| 23 | + ? [ |
| 24 | + { |
| 25 | + type: "image", |
| 26 | + id: `attachment-${index}`, |
| 27 | + filename: `request-${index}.png`, |
| 28 | + mime: "image/png", |
| 29 | + blob: { id: `screenshot-${shape === "repeated" ? index % 10 : index}` }, |
| 30 | + }, |
| 31 | + ] |
| 32 | + : []), |
| 33 | + ], |
| 34 | + comments: [], |
| 35 | + } |
| 36 | +}) |
| 37 | +const shell = Array.from({ length: 100 }, (_, index) => { |
| 38 | + const content = `bun test src/network/request-${index}.test.ts --timeout 30000` |
| 39 | + return { prompt: [{ type: "text", content, start: 0, end: content.length }], comments: [] } |
| 40 | +}) |
| 41 | + |
| 42 | +// Seed only this Playwright context, before opening the production draft store. |
| 43 | +const request = indexedDB.open("opencode-drafts", 1) |
| 44 | +request.onupgradeneeded = () => { |
| 45 | + request.result.createObjectStore("documents") |
| 46 | + request.result.createObjectStore("blobs") |
| 47 | +} |
| 48 | +const db = await new Promise<IDBDatabase>((resolve, reject) => { |
| 49 | + request.onsuccess = () => resolve(request.result) |
| 50 | + request.onerror = () => reject(request.error) |
| 51 | +}) |
| 52 | +const ids = [...new Set(normal.flatMap((entry) => entry.prompt.flatMap((part) => (part.blob ? [part.blob.id] : []))))] |
| 53 | +const screenshots: { id: string; blob: Blob }[] = [] |
| 54 | +for (const id of ids) { |
| 55 | + const canvas = document.createElement("canvas") |
| 56 | + canvas.width = 1440 |
| 57 | + canvas.height = 900 |
| 58 | + const context = canvas.getContext("2d")! |
| 59 | + context.fillStyle = "#15191f" |
| 60 | + context.fillRect(0, 0, canvas.width, canvas.height) |
| 61 | + context.font = "16px monospace" |
| 62 | + context.fillStyle = "#b8c8d8" |
| 63 | + context.fillText(`request.ts - ${id}`, 30, 35) |
| 64 | + for (let line = 0; line < 38; line++) { |
| 65 | + context.fillStyle = line % 3 ? "#a8c7ba" : "#d4a882" |
| 66 | + context.fillText( |
| 67 | + `${String(line + 1).padStart(3)} const response${line} = await fetch('/api/request/${id}/${line}', { signal, headers });`, |
| 68 | + 30, |
| 69 | + 70 + line * 20, |
| 70 | + ) |
| 71 | + } |
| 72 | + const blob = await new Promise<Blob>((resolve) => canvas.toBlob((blob) => resolve(blob!), "image/png")) |
| 73 | + screenshots.push({ id, blob }) |
| 74 | +} |
| 75 | +const transaction = db.transaction(["documents", "blobs"], "readwrite") |
| 76 | +transaction.objectStore("documents").put(JSON.stringify({ entries: normal }), "opencode.global.dat:prompt-history") |
| 77 | +transaction.objectStore("documents").put(JSON.stringify({ entries: shell }), "opencode.global.dat:prompt-history-shell") |
| 78 | +screenshots.forEach(({ id, blob }) => transaction.objectStore("blobs").put(blob, id)) |
| 79 | +await new Promise<void>((resolve, reject) => { |
| 80 | + transaction.oncomplete = () => resolve() |
| 81 | + transaction.onerror = () => reject(transaction.error) |
| 82 | +}) |
| 83 | +db.close() |
| 84 | + |
| 85 | +const metrics = { reads: 0, blobBytes: 0, documents: 0 } |
| 86 | +const originalGet = IDBObjectStore.prototype.get |
| 87 | +IDBObjectStore.prototype.get = function (key) { |
| 88 | + const request = originalGet.call(this, key) |
| 89 | + if (this.name === "documents") metrics.documents++ |
| 90 | + if (this.name === "blobs") { |
| 91 | + metrics.reads++ |
| 92 | + request.addEventListener("success", () => { |
| 93 | + metrics.blobBytes += request.result?.size ?? 0 |
| 94 | + }) |
| 95 | + } |
| 96 | + return request |
| 97 | +} |
| 98 | +const platform: Platform = { |
| 99 | + platform: "web", |
| 100 | + draftStore: createBrowserDraftStore(), |
| 101 | + openExternal() {}, |
| 102 | + restart: async () => {}, |
| 103 | + notify: async () => {}, |
| 104 | +} |
| 105 | +const [state, setState] = createStore({ mount: 0, ready: false, result: "" }) |
| 106 | +const workload = { |
| 107 | + shape, |
| 108 | + normalEntries: normal.length, |
| 109 | + shellEntries: shell.length, |
| 110 | + imageReferences: shape === "text" ? 0 : 50, |
| 111 | + uniqueImages: ids.length, |
| 112 | + storedImageBytes: screenshots.reduce((sum, item) => sum + item.blob.size, 0), |
| 113 | + documentBytes: [normal, shell].reduce( |
| 114 | + (sum, entries) => sum + new TextEncoder().encode(JSON.stringify({ entries })).length, |
| 115 | + 0, |
| 116 | + ), |
| 117 | + screenshotDimensions: [1440, 900], |
| 118 | +} |
| 119 | +let started = 0 |
| 120 | +function mount() { |
| 121 | + metrics.reads = 0 |
| 122 | + metrics.blobBytes = 0 |
| 123 | + metrics.documents = 0 |
| 124 | + setState({ ready: false, result: "" }) |
| 125 | + started = performance.now() |
| 126 | + setState("mount", state.mount + 1) |
| 127 | +} |
| 128 | +function Destination() { |
| 129 | + // Same history creation and editor mapping as createComposerModel. Destination draft is empty. |
| 130 | + const history = createComposerHistory() |
| 131 | + const store = createStore<ComposerPersistedState>({ |
| 132 | + prompt: [{ type: "text", content: "", start: 0, end: 0 }], |
| 133 | + cursor: 0, |
| 134 | + context: { items: [] }, |
| 135 | + }) |
| 136 | + const controller = createComposerEditor({ |
| 137 | + store, |
| 138 | + commands: () => [], |
| 139 | + context: () => [], |
| 140 | + searchContextFiles: () => [], |
| 141 | + history: { |
| 142 | + entries: (mode) => history.entries(mode).map((entry) => ({ prompt: entry.prompt, metadata: entry.comments })), |
| 143 | + add: (prompt, mode) => history.add(prompt, mode, []), |
| 144 | + }, |
| 145 | + view: { |
| 146 | + placeholder: () => "Empty destination composer", |
| 147 | + submit: { stopping: () => false, onSubmit() {}, onStop() {} }, |
| 148 | + }, |
| 149 | + }) |
| 150 | + createEffect(() => { |
| 151 | + if (history.entries("normal").length !== 100 || history.entries("shell").length !== 100) return |
| 152 | + setState({ |
| 153 | + ready: true, |
| 154 | + result: JSON.stringify({ historyReadyMs: performance.now() - started, ...metrics, ...workload }), |
| 155 | + }) |
| 156 | + }) |
| 157 | + return <ComposerEditor controller={controller} /> |
| 158 | +} |
| 159 | +render( |
| 160 | + () => ( |
| 161 | + <PlatformProvider value={platform}> |
| 162 | + <main style={{ padding: "40px", width: "900px" }}> |
| 163 | + <h1>Composer global history: {shape}</h1> |
| 164 | + <button onClick={mount}>Mount empty composer</button> |
| 165 | + <output data-testid="history-ready">{state.ready ? "ready" : "idle"}</output> |
| 166 | + <pre data-testid="history-result">{state.result}</pre> |
| 167 | + <Show when={state.mount} keyed> |
| 168 | + {(_mount) => <Destination />} |
| 169 | + </Show> |
| 170 | + </main> |
| 171 | + </PlatformProvider> |
| 172 | + ), |
| 173 | + document.getElementById("root")!, |
| 174 | +) |
0 commit comments