File size: 6,103 Bytes
2f1e7ff
 
eb60b6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f1e7ff
eb60b6b
 
 
 
 
 
 
 
 
2f1e7ff
eb60b6b
2f1e7ff
 
eb60b6b
 
 
 
 
 
 
 
 
 
 
 
 
2f1e7ff
eb60b6b
 
 
 
 
 
 
 
2f1e7ff
eb60b6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f1e7ff
 
 
 
 
eb60b6b
 
 
2f1e7ff
 
 
eb60b6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f1e7ff
 
 
 
eb60b6b
 
2f1e7ff
 
 
 
 
eb60b6b
2f1e7ff
 
 
 
 
eb60b6b
 
2f1e7ff
 
 
 
 
 
 
 
eb60b6b
 
2f1e7ff
 
 
eb60b6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { requireAcceptedTask } from "./ingestContract.mjs";
import { clearSession, loadSession, saveSession, normalizeBaseUrl, type AuthSession } from "./auth";

export class ApiError extends Error {
  status: number;

  constructor(status: number, message: string) {
    super(message);
    this.status = status;
  }
}

export interface BrainRecord {
  id?: string;
  name_key: string;
}

export interface ApiFetchOptions {
  logoutOn401?: boolean;
}

export interface Workspace {
  id?: string;
  brain_id: string;
  slug: string;
  display_name: string;
  description?: string | null;
  archived: boolean;
  api_base_url: string;
  console_url: string;
}

export interface CreateWorkspaceInput {
  slug: string;
  display_name: string;
  description?: string | null;
}

export interface UpdateWorkspaceInput {
  display_name?: string;
  description?: string | null;
  archived?: boolean;
}

let session: AuthSession | null = null;

export function getSession(): AuthSession | null {
  return session;
}

export function setSession(next: AuthSession | null): void {
  session = next;
}

function authHeaders(brainId?: string | null, path?: string): Headers {
  const headers = new Headers();
  if (session?.csrfToken) headers.set("X-CSRF-Token", session.csrfToken);
  if (brainId && path && !path.startsWith("/system")) headers.set("X-Brain-ID", brainId);
  return headers;
}

export async function apiFetch<T>(
  path: string,
  options: RequestInit = {},
  overrideBrainId?: string,
  fetchOptions: ApiFetchOptions = {},
): Promise<T> {
  if (!session) {
    throw new ApiError(401, "Not authenticated");
  }

  const headers = authHeaders(overrideBrainId ?? session.brainId, path);
  for (const [key, value] of new Headers(options.headers).entries()) {
    headers.set(key, value);
  }
  if (options.body && !headers.has("Content-Type")) {
    headers.set("Content-Type", "application/json");
  }

  const url = `${normalizeBaseUrl(session.apiBaseUrl)}${path}`;
  const response = await fetch(url, { ...options, headers, credentials: "include" });

  if (response.status === 401 && fetchOptions.logoutOn401 !== false) {
    clearSession();
    setSession(null);
    if (!window.location.pathname.endsWith("/login")) {
      window.location.href = "/console/login";
    }
    throw new ApiError(401, "Unauthorized");
  }

  const text = await response.text();
  let data: unknown = null;
  if (text) {
    try {
      data = JSON.parse(text);
    } catch {
      data = text;
    }
  }

  if (!response.ok) {
    const envelope = typeof data === "object" && data !== null ? data as {detail?: unknown; error?: {message?: string}} : null;
    const reason = envelope?.detail;
    const detail = typeof reason === "string" ? reason
      : reason ? `${envelope?.error?.message ?? "Request failed"}: ${JSON.stringify(reason)}`
      : envelope?.error?.message ?? `Request failed (${response.status})`;
    throw new ApiError(response.status, detail);
  }

  if ((path.startsWith("/ingest/") || path.includes("/agent/memory")) && options.method === "POST") {
    requireAcceptedTask(response.status, data);
  }
  return data as T;
}

export function workspaceApiFetch<T>(
  workspace: Pick<Workspace, "brain_id">,
  path: string,
  options: RequestInit = {},
  fetchOptions: ApiFetchOptions = {},
): Promise<T> {
  return apiFetch<T>(path, options, workspace.brain_id, fetchOptions);
}

export async function fetchWorkspaces(): Promise<Workspace[]> {
  const result = await apiFetch<{ workspaces: Workspace[] }>(
    "/system/workspaces",
  );
  return result.workspaces;
}

export function fetchWorkspace(slug: string): Promise<Workspace> {
  return apiFetch<Workspace>(`/system/workspaces/${encodeURIComponent(slug)}`);
}

export function createWorkspace(
  input: CreateWorkspaceInput,
): Promise<Workspace> {
  return apiFetch<Workspace>("/system/workspaces", {
    method: "POST",
    body: JSON.stringify(input),
  });
}

export function updateWorkspace(
  slug: string,
  input: UpdateWorkspaceInput,
): Promise<Workspace> {
  return apiFetch<Workspace>(
    `/system/workspaces/${encodeURIComponent(slug)}`,
    { method: "PATCH", body: JSON.stringify(input) },
  );
}

interface SessionIdentity {
  is_system: boolean;
  brain_id: string | null;
  csrf_token?: string;
}

export async function connectSession(apiBaseUrl: string, token: string): Promise<AuthSession> {
  const base = normalizeBaseUrl(apiBaseUrl);
  const response = await fetch(`${base}/auth/session`, {
    method: "POST", credentials: "include", headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ token }),
  });
  if (!response.ok) throw new ApiError(response.status, "Access token rejected");
  const identity: SessionIdentity = await response.json();
  session = { apiBaseUrl: base, brainId: identity.brain_id, isSystemPat: identity.is_system, csrfToken: identity.csrf_token };
  // The credential has served its purpose; all subsequent calls use the cookie.
  return session;
}

export async function restoreSession(): Promise<AuthSession | null> {
  const base = loadSession()?.apiBaseUrl ?? window.location.origin;
  const response = await fetch(`${normalizeBaseUrl(base)}/auth/me`, { credentials: "include" });
  if (!response.ok) { session = null; return null; }
  const identity: SessionIdentity = await response.json();
  session = { apiBaseUrl: base, brainId: identity.brain_id, isSystemPat: identity.is_system, csrfToken: identity.csrf_token };
  saveSession(session);
  return session;
}

export async function logoutSession(): Promise<void> {
  try { await apiFetch("/auth/logout", { method: "POST" }); }
  finally { clearSession(); session = null; }
}

export function mergeBrainOptions(
  brains: BrainRecord[],
  activeBrainId: string,
): BrainRecord[] {
  const byKey = new Map<string, BrainRecord>();
  for (const brain of brains) {
    byKey.set(brain.name_key, brain);
  }
  if (activeBrainId && !byKey.has(activeBrainId)) {
    byKey.set(activeBrainId, { name_key: activeBrainId });
  }
  return [...byKey.values()].sort((a, b) =>
    a.name_key.localeCompare(b.name_key),
  );
}