@@ -54,14 +54,18 @@ export async function mockOpenCodeServer(page: Page, config: MockServerConfig) { |
54 | 54 | if (url.port !== targetPort && url.port !== appPort) return route.fallback() |
55 | 55 |
|
56 | 56 | const path = url.pathname |
57 | | - if (path === "/global/event" || path === "/event") return sse(route, config.events?.(), config.eventRetry) |
| 57 | + if (path === "/global/event" || path === "/event" || path === "/api/event") { |
| 58 | + const events = config.events?.() |
| 59 | + return sse( |
| 60 | + route, |
| 61 | + path === "/api/event" |
| 62 | + ? [{ id: "evt_mock_connected", type: "server.connected", data: {} }, ...(events?.map(currentEvent) ?? [])] |
| 63 | + : events, |
| 64 | + config.eventRetry, |
| 65 | + ) |
| 66 | + } |
58 | 67 | if (path === "/global/health") return json(route, { healthy: true }) |
59 | | - if (path === "/api/session") |
60 | | - return json(route, { |
61 | | - data: config.sessions.map((session) => v2Session(session, config.directory)), |
62 | | - cursor: {}, |
63 | | - }) |
64 | | - if (path === "/experimental/capabilities") return json(route, { backgroundSubagents: false }) |
| 68 | + if (path === "/experimental/capabilities") return json(route, { backgroundSubagents: true }) |
65 | 69 | if (path === "/permission") |
66 | 70 | return json(route, typeof config.permissions === "function" ? config.permissions() : (config.permissions ?? [])) |
67 | 71 | if (path === "/question") |
@@ -89,10 +93,122 @@ export async function mockOpenCodeServer(page: Page, config: MockServerConfig) { |
89 | 93 | }, |
90 | 94 | data: [], |
91 | 95 | }) |
| 96 | + if (path === "/api/agent") |
| 97 | + return json(route, { |
| 98 | + location: location(config), |
| 99 | + data: [ |
| 100 | + { |
| 101 | + id: "build", |
| 102 | + name: "Build", |
| 103 | + mode: "primary", |
| 104 | + hidden: false, |
| 105 | + request: { settings: {}, headers: {}, body: {} }, |
| 106 | + permissions: [], |
| 107 | + }, |
| 108 | + ], |
| 109 | + }) |
| 110 | + if (path === "/api/command") return json(route, { location: location(config), data: [] }) |
| 111 | + if (path === "/api/mcp") return json(route, { location: location(config), data: [] }) |
| 112 | + if (path === "/api/mcp/resource") |
| 113 | + return json(route, { location: location(config), data: { resources: [], templates: [] } }) |
| 114 | + const integration = path.match(/^\/api\/integration\/([^/]+)$/)?.[1] |
| 115 | + if (integration && route.request().method() === "GET") |
| 116 | + return json(route, { |
| 117 | + location: location(config), |
| 118 | + data: { id: integration, name: integration, methods: [{ type: "key", label: "API key" }], connections: [] }, |
| 119 | + }) |
| 120 | + if (/^\/api\/integration\/[^/]+\/connect\/key$/.test(path) && route.request().method() === "POST") |
| 121 | + return route.fulfill({ status: 204, headers: { "access-control-allow-origin": "*" } }) |
| 122 | + if (path === "/api/project") return json(route, [config.project]) |
| 123 | + if (path === "/api/project/current") |
| 124 | + return json(route, { id: (config.project as { id?: string }).id, directory: config.directory }) |
| 125 | + if (path.startsWith("/api/project/") && route.request().method() === "PATCH") return json(route, config.project) |
| 126 | + if (path === "/api/path") |
| 127 | + return json(route, { |
| 128 | + state: config.directory, |
| 129 | + config: config.directory, |
| 130 | + worktree: config.directory, |
| 131 | + directory: config.directory, |
| 132 | + home: "C:/OpenCode", |
| 133 | + }) |
| 134 | + if (path === "/api/permission/request") |
| 135 | + return json(route, { |
| 136 | + location: location(config), |
| 137 | + data: (typeof config.permissions === "function" ? config.permissions() : (config.permissions ?? [])).map( |
| 138 | + currentPermission, |
| 139 | + ), |
| 140 | + }) |
| 141 | + if (path === "/api/question/request") |
| 142 | + return json(route, { |
| 143 | + location: location(config), |
| 144 | + data: typeof config.questions === "function" ? config.questions() : (config.questions ?? []), |
| 145 | + }) |
| 146 | + if (path === "/api/vcs") |
| 147 | + return json(route, { location: location(config), data: { branch: "main", defaultBranch: "main" } }) |
| 148 | + if (path === "/api/vcs/status") return json(route, { location: location(config), data: [] }) |
| 149 | + if (path === "/api/vcs/diff") return json(route, { location: location(config), data: config.vcsDiff ?? [] }) |
| 150 | + if (path === "/api/pty/shells") return json(route, { location: location(config), data: [] }) |
| 151 | + if (/^\/api\/pty\/[^/]+\/connect-token$/.test(path)) |
| 152 | + return json(route, { location: location(config), data: { ticket: "e2e-ticket", expires_in: 60 } }) |
92 | 153 | if (emptyObject.has(path)) return json(route, {}) |
93 | 154 | if (emptyList.has(path)) return json(route, []) |
| 155 | + if (path === "/api/session") { |
| 156 | + const directory = url.searchParams.get("directory") |
| 157 | + const parentID = url.searchParams.get("parentID") |
| 158 | + const limit = Number(url.searchParams.get("limit") ?? 50) |
| 159 | + const offset = Number(url.searchParams.get("cursor") ?? 0) |
| 160 | + const sessions = config.sessions |
| 161 | + .filter((session) => !directory || session.directory === directory) |
| 162 | + .filter((session) => parentID !== "null" || session.parentID === undefined) |
| 163 | + .filter((session) => { |
| 164 | + const search = url.searchParams.get("search")?.toLowerCase() |
| 165 | + return !search || String(session.title ?? "").toLowerCase().includes(search) |
| 166 | + }) |
| 167 | + const ordered = url.searchParams.get("order") === "asc" ? sessions.toReversed() : sessions |
| 168 | + const data = ordered.slice(offset, offset + limit) |
| 169 | + const next = offset + limit < ordered.length ? String(offset + limit) : undefined |
| 170 | + return json(route, { |
| 171 | + data: data.map((session) => currentSession(session, config.directory)), |
| 172 | + cursor: { next }, |
| 173 | + }) |
| 174 | + } |
| 175 | + if (path === "/api/session/active") { |
| 176 | + const statuses = (config.sessionStatus ?? {}) as Record<string, { type?: string }> |
| 177 | + return json(route, { |
| 178 | + data: Object.fromEntries( |
| 179 | + Object.entries(statuses).flatMap(([id, status]) => (status.type === "idle" ? [] : [[id, { type: "running" }]])), |
| 180 | + ), |
| 181 | + }) |
| 182 | + } |
| 183 | + if (/^\/api\/session\/[^/]+\/shell$/.test(path) && route.request().method() === "POST") { |
| 184 | + return route.fulfill({ status: 204, headers: { "access-control-allow-origin": "*" } }) |
| 185 | + } |
| 186 | + if (/^\/api\/session\/[^/]+\/question\/[^/]+\/(reply|reject)$/.test(path) && route.request().method() === "POST") { |
| 187 | + return route.fulfill({ status: 204, headers: { "access-control-allow-origin": "*" } }) |
| 188 | + } |
| 189 | + if (/^\/api\/session\/[^/]+\/permission\/[^/]+\/reply$/.test(path) && route.request().method() === "POST") { |
| 190 | + return route.fulfill({ status: 204, headers: { "access-control-allow-origin": "*" } }) |
| 191 | + } |
| 192 | + if ( |
| 193 | + /^\/api\/session\/[^/]+\/(archive|rename|interrupt|revert\/clear|revert\/commit)$/.test(path) && |
| 194 | + route.request().method() === "POST" |
| 195 | + ) { |
| 196 | + return route.fulfill({ status: 204, headers: { "access-control-allow-origin": "*" } }) |
| 197 | + } |
| 198 | + if (/^\/api\/session\/[^/]+$/.test(path) && route.request().method() === "DELETE") { |
| 199 | + return route.fulfill({ status: 204, headers: { "access-control-allow-origin": "*" } }) |
| 200 | + } |
94 | 201 | if (path in staticRoutes) return json(route, staticRoutes[path]) |
95 | 202 |
|
| 203 | + const currentSessionMatch = path.match(/^\/api\/session\/([^/]+)$/) |
| 204 | + if (currentSessionMatch) { |
| 205 | + const session = config.sessions.find((item) => item.id === currentSessionMatch[1]) |
| 206 | + if (!session) return json(route, { error: "Session not found" }, undefined, 404) |
| 207 | + return json(route, { |
| 208 | + data: currentSession(session, config.directory), |
| 209 | + }) |
| 210 | + } |
| 211 | + |
96 | 212 | const sessionMatch = path.match(/^\/session\/([^/]+)$/) |
97 | 213 | if (sessionMatch) { |
98 | 214 | const session = config.sessions.find((s) => s.id === sessionMatch[1]) |
@@ -115,6 +231,24 @@ export async function mockOpenCodeServer(page: Page, config: MockServerConfig) { |
115 | 231 | if (todoMatch) return json(route, config.todos?.(todoMatch[1]!) ?? []) |
116 | 232 | if (/^\/session\/[^/]+\/(children|diff)$/.test(path)) return json(route, []) |
117 | 233 |
|
| 234 | + const currentMessagesMatch = path.match(/^\/api\/session\/([^/]+)\/message$/) |
| 235 | + if (currentMessagesMatch) { |
| 236 | + const token = url.searchParams.get("cursor") ?? undefined |
| 237 | + const before = token ? cursors.get(token) : undefined |
| 238 | + if (token && !before) return json(route, { error: "Invalid cursor" }, undefined, 400) |
| 239 | + config.onMessages?.({ sessionID: currentMessagesMatch[1], before, phase: "start" }) |
| 240 | + await config.beforeMessagesResponse?.({ sessionID: currentMessagesMatch[1]!, before }) |
| 241 | + if (config.messageDelay !== undefined) await new Promise((resolve) => setTimeout(resolve, config.messageDelay)) |
| 242 | + const pageData = config.pageMessages(currentMessagesMatch[1], Number(url.searchParams.get("limit") ?? 50), before) |
| 243 | + config.onMessages?.({ sessionID: currentMessagesMatch[1], before, phase: "end" }) |
| 244 | + const cursor = pageData.cursor ? `cursor_${++nextCursor}` : undefined |
| 245 | + if (cursor) cursors.set(cursor, pageData.cursor!) |
| 246 | + return json(route, { |
| 247 | + data: pageData.items.map(currentMessage).reverse(), |
| 248 | + cursor: { next: cursor }, |
| 249 | + }) |
| 250 | + } |
| 251 | + |
118 | 252 | const messagesMatch = path.match(/^\/session\/([^/]+)\/message$/) |
119 | 253 | if (messagesMatch) { |
120 | 254 | const token = url.searchParams.get("before") ?? undefined |
@@ -137,12 +271,36 @@ export async function mockOpenCodeServer(page: Page, config: MockServerConfig) { |
137 | 271 | }) |
138 | 272 | } |
139 | 273 |
|
140 | | -function v2Session(session: { id: string } & Record<string, unknown>, fallbackDirectory: string) { |
| 274 | +function location(config: MockServerConfig) { |
| 275 | + return { |
| 276 | + directory: config.directory, |
| 277 | + project: { id: (config.project as { id?: string }).id, directory: config.directory }, |
| 278 | + } |
| 279 | +} |
| 280 | + |
| 281 | +function currentPermission(value: unknown) { |
| 282 | + const permission = value as Record<string, unknown> |
| 283 | + if (permission.action) return permission |
| 284 | + const tool = permission.tool as { messageID?: string; callID?: string } | undefined |
| 285 | + return { |
| 286 | + id: permission.id, |
| 287 | + sessionID: permission.sessionID, |
| 288 | + action: permission.permission, |
| 289 | + resources: permission.patterns ?? [], |
| 290 | + save: permission.always, |
| 291 | + metadata: permission.metadata, |
| 292 | + source: tool?.messageID && tool.callID ? { type: "tool", messageID: tool.messageID, callID: tool.callID } : undefined, |
| 293 | + } |
| 294 | +} |
| 295 | + |
| 296 | +export function currentSession(session: { id: string } & Record<string, unknown>, fallbackDirectory?: string) { |
141 | 297 | const time = session.time && typeof session.time === "object" ? session.time : {} |
142 | 298 | return { |
143 | 299 | id: session.id, |
144 | 300 | parentID: session.parentID, |
145 | 301 | projectID: session.projectID ?? "project", |
| 302 | + agent: session.agent ?? "build", |
| 303 | + model: session.model ?? { id: "mock-model", providerID: "mock-provider" }, |
146 | 304 | cost: session.cost ?? 0, |
147 | 305 | tokens: session.tokens ?? { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
148 | 306 | time: { |
@@ -157,7 +315,67 @@ function v2Session(session: { id: string } & Record<string, unknown>, fallbackDi |
157 | 315 | directory: typeof session.directory === "string" ? session.directory : fallbackDirectory, |
158 | 316 | ...(typeof session.workspaceID === "string" ? { workspaceID: session.workspaceID } : {}), |
159 | 317 | }, |
160 | | - ...(typeof session.path === "string" ? { subpath: session.path } : {}), |
| 318 | + subpath: session.path, |
| 319 | + revert: session.revert, |
| 320 | + } |
| 321 | +} |
| 322 | + |
| 323 | +function currentMessage(value: unknown) { |
| 324 | + const item = value as { |
| 325 | + info: Record<string, unknown> & { id: string; role: "user" | "assistant"; time: { created: number } } |
| 326 | + parts: Array<Record<string, unknown> & { type: string }> |
| 327 | + } |
| 328 | + if (item.info.role === "user") { |
| 329 | + return { |
| 330 | + id: item.info.id, |
| 331 | + type: "user", |
| 332 | + time: item.info.time, |
| 333 | + text: item.parts |
| 334 | + .flatMap((part) => (part.type === "text" && typeof part.text === "string" ? [part.text] : [])) |
| 335 | + .join("\n"), |
| 336 | + } |
| 337 | + } |
| 338 | + return { |
| 339 | + id: item.info.id, |
| 340 | + type: "assistant", |
| 341 | + time: item.info.time, |
| 342 | + agent: item.info.agent ?? "build", |
| 343 | + model: { id: item.info.modelID ?? "model", providerID: item.info.providerID ?? "provider" }, |
| 344 | + cost: item.info.cost, |
| 345 | + tokens: item.info.tokens, |
| 346 | + error: item.info.error, |
| 347 | + content: item.parts.flatMap<unknown>((part) => { |
| 348 | + if (part.type === "text" || part.type === "reasoning") return [{ type: part.type, text: part.text ?? "" }] |
| 349 | + if (part.type !== "tool") return [] |
| 350 | + const state = part.state as Record<string, unknown> |
| 351 | + return [ |
| 352 | + { |
| 353 | + type: "tool", |
| 354 | + id: part.id, |
| 355 | + name: part.tool, |
| 356 | + time: state.time ?? { created: item.info.time.created }, |
| 357 | + state: |
| 358 | + state.status === "pending" |
| 359 | + ? { status: "streaming", input: state.raw ?? JSON.stringify(state.input ?? {}) } |
| 360 | + : state.status === "completed" |
| 361 | + ? { |
| 362 | + status: "completed", |
| 363 | + input: state.input ?? {}, |
| 364 | + structured: state.metadata ?? {}, |
| 365 | + content: [{ type: "text", text: state.output ?? "" }], |
| 366 | + } |
| 367 | + : state.status === "error" |
| 368 | + ? { |
| 369 | + status: "error", |
| 370 | + input: state.input ?? {}, |
| 371 | + structured: state.metadata ?? {}, |
| 372 | + content: [], |
| 373 | + error: { type: "ToolError", message: state.error ?? "Tool failed" }, |
| 374 | + } |
| 375 | + : { status: "running", input: state.input ?? {}, structured: state.metadata ?? {}, content: [] }, |
| 376 | + }, |
| 377 | + ] |
| 378 | + }), |
161 | 379 | } |
162 | 380 | } |
163 | 381 |
|
@@ -181,3 +399,18 @@ function sse(route: Route, events?: unknown[], retry?: number) { |
181 | 399 | body: `${retry === undefined ? "" : `retry: ${retry}\n\n`}${events?.map((event) => `data: ${JSON.stringify(event)}\n\n`).join("") || ": ok\n\n"}`, |
182 | 400 | }) |
183 | 401 | } |
| 402 | + |
| 403 | +function currentEvent(input: unknown) { |
| 404 | + if (!input || typeof input !== "object" || !("payload" in input)) return input |
| 405 | + const envelope = input as { directory?: string; payload?: unknown } |
| 406 | + if (!envelope.payload || typeof envelope.payload !== "object") return input |
| 407 | + const payload = envelope.payload as { id?: string; type?: string; properties?: unknown } |
| 408 | + if (!payload.type) return input |
| 409 | + return { |
| 410 | + id: payload.id ?? `evt_mock_${Date.now()}`, |
| 411 | + created: Date.now(), |
| 412 | + type: payload.type, |
| 413 | + data: payload.properties ?? {}, |
| 414 | + location: envelope.directory && envelope.directory !== "global" ? { directory: envelope.directory } : undefined, |
| 415 | + } |
| 416 | +} |