|
| 1 | +// Run in an authenticated isolated app tab; pass fixture.json and a live model selection. |
| 2 | +globalThis.seedIssue16455 = async function (fixture, modelSelection) { |
| 3 | + if (!modelSelection?.instanceId || !modelSelection?.model) { |
| 4 | + throw new Error( |
| 5 | + "Pass a valid model selection from this environment. No provider turn is started.", |
| 6 | + ); |
| 7 | + } |
| 8 | + const response = await fetch("/api/orchestration/shell", { |
| 9 | + headers: { "x-t3-orchestration-protocol": "2" }, |
| 10 | + }); |
| 11 | + if (!response.ok) throw new Error(`Shell snapshot: HTTP ${response.status}`); |
| 12 | + const snapshot = await response.json(); |
| 13 | + const socket = new WebSocket( |
| 14 | + `${location.origin.replace(/^http/, "ws")}/ws?orchestrationProtocol=2`, |
| 15 | + ); |
| 16 | + await new Promise((resolve, reject) => { |
| 17 | + socket.addEventListener("open", resolve, { once: true }); |
| 18 | + socket.addEventListener( |
| 19 | + "error", |
| 20 | + () => reject(new Error("Could not connect to the authenticated app socket")), |
| 21 | + { once: true }, |
| 22 | + ); |
| 23 | + }); |
| 24 | + let nextId = 1; |
| 25 | + const pending = new Map(); |
| 26 | + socket.addEventListener("message", (event) => { |
| 27 | + const message = JSON.parse(event.data); |
| 28 | + if (message._tag === "Ping") { |
| 29 | + socket.send(JSON.stringify({ _tag: "Pong" })); |
| 30 | + return; |
| 31 | + } |
| 32 | + const entry = pending.get(String(message.requestId)); |
| 33 | + if (!entry || message._tag !== "Exit") return; |
| 34 | + pending.delete(String(message.requestId)); |
| 35 | + clearTimeout(entry.timeout); |
| 36 | + if (message.exit._tag === "Success") entry.resolve(message.exit.value); |
| 37 | + else entry.reject(new Error(JSON.stringify(message.exit))); |
| 38 | + }); |
| 39 | + const rpc = (tag, payload) => |
| 40 | + new Promise((resolve, reject) => { |
| 41 | + const id = String(nextId++); |
| 42 | + const timeout = setTimeout(() => { |
| 43 | + pending.delete(id); |
| 44 | + reject(new Error(`${tag} timed out`)); |
| 45 | + }, 30000); |
| 46 | + pending.set(id, { resolve, reject, timeout }); |
| 47 | + socket.send(JSON.stringify({ _tag: "Request", id, tag, payload, headers: [] })); |
| 48 | + }); |
| 49 | + try { |
| 50 | + for (const project of fixture.projects) { |
| 51 | + if (!snapshot.projects.some((item) => item.id === project.id)) |
| 52 | + await rpc("projects.mutate", { |
| 53 | + type: "project.create", |
| 54 | + commandId: crypto.randomUUID(), |
| 55 | + projectId: project.id, |
| 56 | + title: project.title, |
| 57 | + workspaceRoot: `${fixture.root}/${project.directory}`, |
| 58 | + }); |
| 59 | + } |
| 60 | + if (!snapshot.threads.some((item) => item.id === fixture.thread.id)) |
| 61 | + await rpc("orchestration.dispatchCommand", { |
| 62 | + type: "thread.create", |
| 63 | + commandId: crypto.randomUUID(), |
| 64 | + threadId: fixture.thread.id, |
| 65 | + projectId: fixture.thread.projectId, |
| 66 | + title: fixture.thread.title, |
| 67 | + modelSelection, |
| 68 | + runtimeMode: "full-access", |
| 69 | + interactionMode: "default", |
| 70 | + branch: fixture.thread.branch, |
| 71 | + worktreePath: null, |
| 72 | + createdBy: "user", |
| 73 | + creationSource: "web", |
| 74 | + }); |
| 75 | + await rpc("orchestration.dispatchCommand", { |
| 76 | + type: "thread.pull-request.link", |
| 77 | + commandId: crypto.randomUUID(), |
| 78 | + threadId: fixture.thread.id, |
| 79 | + ...fixture.pullRequest, |
| 80 | + source: "manual", |
| 81 | + }); |
| 82 | + return { threadId: fixture.thread.id, pullRequest: fixture.pullRequest.url }; |
| 83 | + } finally { |
| 84 | + socket.close(); |
| 85 | + } |
| 86 | +}; |
0 commit comments