MCPcopy Create free account
hub / github.com/CopilotKit/aimock / handleControlAPI

Function handleControlAPI

src/server.ts:282–484  ·  view source on GitHub ↗

* Handle requests under `/__aimock/`. Returns `true` if the request was * handled, `false` if the path doesn't match the control prefix.

(
  req: http.IncomingMessage,
  res: http.ServerResponse,
  pathname: string,
  fixtures: Fixture[],
  journal: Journal,
  videoStates: VideoStateMap,
  openRouterVideoJobs: OpenRouterVideoJobMap,
  veoVideoJobs: VeoVideoJobMap,
  grokVideoJobs: GrokVideoJobMap,
  defaults: HandlerDefaults,
)

Source from the content-addressed store, hash-verified

280 * handled, `false` if the path doesn't match the control prefix.
281 */
282async function handleControlAPI(
283 req: http.IncomingMessage,
284 res: http.ServerResponse,
285 pathname: string,
286 fixtures: Fixture[],
287 journal: Journal,
288 videoStates: VideoStateMap,
289 openRouterVideoJobs: OpenRouterVideoJobMap,
290 veoVideoJobs: VeoVideoJobMap,
291 grokVideoJobs: GrokVideoJobMap,
292 defaults: HandlerDefaults,
293): Promise<boolean> {
294 if (!pathname.startsWith(CONTROL_PREFIX)) return false;
295
296 const subPath = pathname.slice(CONTROL_PREFIX.length);
297 setCorsHeaders(res);
298
299 // GET /__aimock/health
300 if (subPath === "/health" && req.method === "GET") {
301 res.writeHead(200, { "Content-Type": "application/json" });
302 res.end(JSON.stringify({ status: "ok" }));
303 return true;
304 }
305
306 // GET /__aimock/journal
307 if (subPath === "/journal" && req.method === "GET") {
308 res.writeHead(200, { "Content-Type": "application/json" });
309 res.end(JSON.stringify(journal.getAll()));
310 return true;
311 }
312
313 // POST /__aimock/fixtures — add fixtures dynamically
314 if (subPath === "/fixtures" && req.method === "POST") {
315 let raw: string;
316 try {
317 raw = await readBody(req);
318 } catch (err) {
319 const msg = err instanceof Error ? err.message : String(err);
320 defaults.logger.error(`POST /__aimock/fixtures: failed to read body: ${msg}`);
321 res.writeHead(400, { "Content-Type": "application/json" });
322 res.end(JSON.stringify({ error: `Failed to read request body: ${msg}` }));
323 return true;
324 }
325
326 let parsed: { fixtures?: FixtureFileEntry[] };
327 try {
328 parsed = JSON.parse(raw) as { fixtures?: FixtureFileEntry[] };
329 } catch (err) {
330 const msg = err instanceof Error ? err.message : String(err);
331 defaults.logger.error(`POST /__aimock/fixtures: invalid JSON: ${msg}`);
332 res.writeHead(400, { "Content-Type": "application/json" });
333 res.end(JSON.stringify({ error: `Invalid JSON: ${msg}` }));
334 return true;
335 }
336
337 if (!Array.isArray(parsed.fixtures)) {
338 res.writeHead(400, { "Content-Type": "application/json" });
339 res.end(JSON.stringify({ error: 'Missing or invalid "fixtures" array' }));

Callers 1

handleHttpRequestFunction · 0.85

Calls 12

setCorsHeadersFunction · 0.85
readBodyFunction · 0.85
entryToFixtureFunction · 0.85
validateFixturesFunction · 0.85
performFixturesResetFunction · 0.85
handleNotFoundFunction · 0.85
endMethod · 0.80
getAllMethod · 0.80
errorMethod · 0.80
setGaugeMethod · 0.80
clearEntriesMethod · 0.80
warnMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…