File size: 1,434 Bytes
1794757
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { getRuntimeEnv } from "./env";
import { HttpClient } from "./http";
import { SessionClient } from "./session-client";
import type { AgentId } from "./data-sources";
import { AGENT_SOURCE_REGISTRY, buildLiveSourcePlan, validateSourceRegistry } from "./data-sources";

export type PlatformRuntime = {
  bootedAt: string;
  env: ReturnType<typeof getRuntimeEnv>;
  backendStatus: "unknown" | "healthy" | "unreachable";
  sessionClient: SessionClient;
  sourceRegistry: typeof AGENT_SOURCE_REGISTRY;
  sourceValidation: ReturnType<typeof validateSourceRegistry>;
  liveSourcePlans: Record<AgentId, ReturnType<typeof buildLiveSourcePlan>>;
};

export async function createPlatformRuntime(): Promise<PlatformRuntime> {
  const env = getRuntimeEnv();
  const sessionClient = new SessionClient(new HttpClient(env.apiBaseUrl));
  let backendStatus: PlatformRuntime["backendStatus"] = "unknown";

  try {
    await sessionClient.health();
    backendStatus = "healthy";
  } catch {
    backendStatus = "unreachable";
  }

  return {
    liveSourcePlans: Object.fromEntries(
      Object.keys(AGENT_SOURCE_REGISTRY).map((agentId) => [agentId, buildLiveSourcePlan(agentId as AgentId)]),
    ) as Record<AgentId, ReturnType<typeof buildLiveSourcePlan>>,
    bootedAt: new Date().toISOString(),
    env,
    backendStatus,
    sessionClient,
    sourceRegistry: AGENT_SOURCE_REGISTRY,
    sourceValidation: validateSourceRegistry(),
  };
}