Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions web/CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Commands

All commands use **Yarn** (not npm).

```bash
yarn dev # Start dev server (processes env YAML first via scripts/unyamlify-env-local.ts)
yarn build # Type-check (tsc) then build for production
yarn test # Run all tests once (Vitest, non-watch)
yarn format # Format all .ts/.tsx/.json/.md files with Prettier
yarn format:check # Check formatting without writing
yarn storybook # Launch Storybook on port 6006
```

**Run a single test file:**

```bash
yarn vitest run src/core/usecases/launcher/decoupledLogic/computeHelmValues.test.ts
```

**Run tests matching a name pattern:**

```bash
yarn vitest run --reporter=verbose -t "pattern"
```

Pre-commit hooks run `eslint --fix` and `prettier --write` via lint-staged.

## Architecture

Onyxia Web is a React SPA — a data science platform portal for launching Kubernetes services (Helm charts), browsing catalogs, managing S3 files, managing Vault secrets, and querying data via DuckDB. It is deployed as static files served by nginx.

### Core principles

- **React is only for rendering.** Business logic is React-agnostic and lives in `src/core/`. The `src/ui/` layer is strictly for React components and hooks.
- **Unidirectional dependencies.** `src/core/` never imports from `src/ui/`, not even for types.
- **Reactive over promise-based.** Thunks update observable state; the UI reacts to state changes. Prefer dispatching actions and reading state over returning values from thunks.
- **Constants outside Redux state.** Values that don't change are not stored in state — they are retrieved from thunks when needed, to avoid unnecessary re-renders.

### `src/core/` — Business logic

Follows a clean-architecture / ports-and-adapters pattern using the `clean-architecture` npm package (a Redux-like store without Redux).

- **`ports/`** — TypeScript interfaces defining contracts for external dependencies (`OnyxiaApi`, `Oidc`, `S3Client`, `SecretsManager`, `SqlOlap`).
- **`adapters/`** — Concrete implementations: `onyxiaApi/` (axios-based HTTP), `oidc/` (oidc-spa), `s3Client/` (AWS SDK v3), `secretManager/` (Vault), `sqlOlap/` (DuckDB WASM). Each adapter has a mock counterpart for dev/testing.
- **`usecases/`** — One folder per feature (20+ total: `catalog`, `launcher`, `serviceManagement`, `fileExplorer`, `secretExplorer`, `dataExplorer`, etc.). Each usecase follows the pattern:
- `state.ts` — state shape + `createUsecaseActions` (slice-like)
- `thunks.ts` — async side effects, accesses adapters via `createUsecaseContextApi`
- `selectors.ts` — memoized state derivations
- `index.ts` — re-exports all three
- **`bootstrap.ts`** — Wires adapters together and creates the core store.
- **`index.ts`** — Exports `useCoreState`, `getCore`, `createReactApi` bindings consumed by `src/ui/`.

**Complex use-cases** (especially `launcher/`) have a `decoupledLogic/` subfolder with pure functions and no framework dependencies — this is where most unit tests live.

### `src/ui/` — React layer

- **`App/`** — Root layout: Header, LeftBar, Main, Footer. `App.tsx` triggers core bootstrap; `Main.tsx` is the route-based page switcher.
- **`pages/`** — One folder per route/page. Each page exports `routeDefs` (via `type-route`'s `defineRoute`) and `routeGroup`. All are merged in `pages/index.ts`.
- **`routes.tsx`** — Router instantiation. Navigation uses `routes.catalog(...).push()` or `session.push()`.
- **`i18n/`** — i18nifty setup. Translation keys are declared at the component level via `declareComponentKeys`, collected into a `ComponentKey` union in `i18n/types.ts`. Nine languages: en, fr, zh-CN, no, fi, nl, it, es, de.
- **`theme/`** — onyxia-ui theme setup (palette, fonts, favicon).
- **`shared/`** — Reusable components (CommandBar, CodeBlock, SettingField, etc.).

### Key patterns

**Consuming core state in React:**

```ts
import { useCoreState, getCore } from "core";
const helmReleases = useCoreState(state => state.serviceManagement.helmReleases);
await getCore().dispatch(usecases.serviceManagement.thunks.initialize());
```

**Styling — tss-react** (not plain CSS modules):

```ts
import { tss } from "tss";
const useStyles = tss.withName({ MyComponent }).create(({ theme }) => ({ ... }));
const { classes, cx } = useStyles();
```

**Absolute imports** — `tsconfig.json` sets `baseUrl: "src"`, so use `import { foo } from "core/usecases/catalog"` (not relative paths).

**Environment variables** — All env vars are centrally parsed and validated in `src/env.ts`. The `index.html` is an EJS template processed by `vite-envs` at build time.

**Authentication** — OIDC init (`oidc-spa`) happens before React renders, in `main.tsx`. Use the `Oidc` port interface, not the adapter directly.

**Plugin system** — `src/pluginSystem.ts` exposes `window.onyxia` after boot and fires an `"onyxiaready"` `CustomEvent`, allowing external JS to interact with core state, routes, theme, and i18n.

**Keycloak theme** — `src/keycloak-theme/` is a Keycloakify login theme that shares env and i18n infrastructure with the main app. Build with `yarn build-keycloak-theme`.

## Key libraries

| Library | Role |
| -------------------- | ------------------------------------------------------------ |
| `onyxia-ui` | In-house design system on top of MUI v6 |
| `type-route` | Strongly-typed client-side router |
| `i18nifty` | Component-level i18n |
| `clean-architecture` | Redux-like store (ports/usecases pattern) |
| `oidc-spa` | OIDC/OAuth2 authentication |
| `keycloakify` | Keycloak login theme from React components |
| `tss-react` | CSS-in-JS bound to onyxia-ui theme |
| `vite-envs` | Env var injection into EJS `index.html` at build time |
| DuckDB WASM | In-browser SQL OLAP queries (`dataExplorer`, `sqlOlapShell`) |
1 change: 1 addition & 0 deletions web/src/core/adapters/ai/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./openWebUi";
18 changes: 18 additions & 0 deletions web/src/core/adapters/ai/mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Ai } from "core/ports/Ai";

export function createAi(params: { id: string; name: string; webUiUrl: string }): Ai {
const { id, name, webUiUrl } = params;

return {
id,
name,
webUiUrl,
apiBase: `${webUiUrl}/api`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Normalize webUiUrl before composing apiBase.

If webUiUrl ends with /, this produces a double slash (...//api), which can break strict gateways/proxies.

Suggested change
 export function createAi(params: { id: string; name: string; webUiUrl: string }): Ai {
     const { id, name, webUiUrl } = params;
+    const normalizedWebUiUrl = webUiUrl.replace(/\/+$/, "");

     return {
         id,
         name,
         webUiUrl,
-        apiBase: `${webUiUrl}/api`,
+        apiBase: `${normalizedWebUiUrl}/api`,
         getToken: async () => ({ status: "success" as const, token: "mock-ai-token" }),
         listModels: async () => [
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
apiBase: `${webUiUrl}/api`,
export function createAi(params: { id: string; name: string; webUiUrl: string }): Ai {
const { id, name, webUiUrl } = params;
const normalizedWebUiUrl = webUiUrl.replace(/\/+$/, "");
return {
id,
name,
webUiUrl,
apiBase: `${normalizedWebUiUrl}/api`,
getToken: async () => ({ status: "success" as const, token: "mock-ai-token" }),
listModels: async () => [
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@web/src/core/adapters/ai/mock.ts` at line 10, The apiBase string composition
concatenates webUiUrl directly with `/api`, which creates a double slash
(`...//api`) if webUiUrl ends with a forward slash, potentially breaking strict
gateways or proxies. Normalize the webUiUrl by removing any trailing slash
before composing the apiBase string. Use a method to strip the trailing slash
from webUiUrl (such as using replace or trimEnd with proper regex/string
manipulation) so that the final apiBase always has exactly one slash between the
base URL and the `/api` path.

getToken: async () => ({ status: "success" as const, token: "mock-ai-token" }),
listModels: async () => [
{ id: "llama3.2", name: "Llama 3.2" },
{ id: "mistral-7b", name: "Mistral 7B" },
{ id: "codestral", name: "Codestral" }
]
};
}
52 changes: 52 additions & 0 deletions web/src/core/adapters/ai/openWebUi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { Ai, GetTokenResult } from "core/ports/Ai";
import { oidcTokenExchange, OidcTokenExchangeError } from "core/tools/oidcTokenExchange";
import { z } from "zod";

export function createAi(params: {
id: string;
name: string;
webUiUrl: string;
oauthProvider: string;
getOidcAccessToken: () => Promise<string>;
}): Ai {
const { id, name, webUiUrl, oauthProvider, getOidcAccessToken } = params;

const apiBase = `${webUiUrl}/api`;

return {
id,
name,
webUiUrl,
apiBase,
getToken: async (): Promise<GetTokenResult> => {
const oidcAccessToken = await getOidcAccessToken();

return oidcTokenExchange({
tokenExchangeEndpoint: `${webUiUrl}/api/v1/auths/oauth/${oauthProvider}/token/exchange`,
oidcAccessToken
})
.then(token => ({ status: "success" as const, token }))
.catch((error: unknown) => {
if (error instanceof OidcTokenExchangeError && error.status === 403) {
return { status: "no-account" as const };
}
return { status: "error" as const };
});
},
listModels: async (token: string) => {
const response = await fetch(`${apiBase}/models`, {
headers: { Authorization: `Bearer ${token}` }
});

if (!response.ok) {
throw new Error(`Failed to list models (${response.status})`);
}

const { data } = z
.object({ data: z.array(z.object({ id: z.string(), name: z.string() })) })
.parse(await response.json());

return data.map(({ id, name }) => ({ id, name }));
}
};
}
7 changes: 7 additions & 0 deletions web/src/core/adapters/onyxiaApi/ApiTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ export type ApiTypes = {
};
};
data?: {
ai?: ArrayOrNot<{
id?: string;
URL: string;
name?: string;
oauthProvider: string;
oidcConfiguration?: Partial<ApiTypes.OidcConfiguration>;
}>;
S3?: ArrayOrNot<{
URL: string;
pathStyleAccess?: true;
Expand Down
21 changes: 21 additions & 0 deletions web/src/core/adapters/onyxiaApi/onyxiaApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,27 @@
apiRegion.vault.oidcConfiguration
)
},
ai: (() => {
const value = apiRegion.data?.ai;

const aiConfigs_api =
value === undefined
? []
: value instanceof Array

Check warning on line 444 in web/src/core/adapters/onyxiaApi/onyxiaApi.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Avoid using `instanceof` for type checking as it can lead to unreliable results.

See more on https://sonarcloud.io/project/issues?id=InseeFrLab_onyxia&issues=AZ7Z7tvVCtsnrywd-NQd&open=AZ7Z7tvVCtsnrywd-NQd&pullRequest=1072
? value
: [value];

Check warning on line 446 in web/src/core/adapters/onyxiaApi/onyxiaApi.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this nested ternary operation into an independent statement.

See more on https://sonarcloud.io/project/issues?id=InseeFrLab_onyxia&issues=AZ7Z7tvVCtsnrywd-NQc&open=AZ7Z7tvVCtsnrywd-NQc&pullRequest=1072

return aiConfigs_api.map(aiConfig_api => ({

Check failure on line 448 in web/src/core/adapters/onyxiaApi/onyxiaApi.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this code to not nest functions more than 4 levels deep.

See more on https://sonarcloud.io/project/issues?id=InseeFrLab_onyxia&issues=AZ7Z7tvVCtsnrywd-NQe&open=AZ7Z7tvVCtsnrywd-NQe&pullRequest=1072
id: aiConfig_api.id ?? aiConfig_api.URL,
url: aiConfig_api.URL,
name: aiConfig_api.name,
oauthProvider: aiConfig_api.oauthProvider,
oidcParams:
apiTypesOidcConfigurationToOidcParams_Partial(
aiConfig_api.oidcConfiguration
)
}));
})(),
proxyInjection:
apiRegion.proxyInjection === undefined
? undefined
Expand Down
74 changes: 70 additions & 4 deletions web/src/core/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import type { SqlOlap } from "core/ports/SqlOlap";
import { usecases } from "./usecases";
import type { SecretsManager } from "core/ports/SecretsManager";
import type { Ai } from "core/ports/Ai";
import type { Oidc } from "core/ports/Oidc";
import type { Language } from "core/ports/OnyxiaApi/Language";
import { createDuckDbSqlOlap } from "core/adapters/sqlOlap";
Expand Down Expand Up @@ -38,11 +39,12 @@
onyxiaApi: OnyxiaApi;
secretsManager: SecretsManager;
sqlOlap: SqlOlap;
ai: Ai[];
};

export type Core = GenericCore<typeof usecases, Context>;

export async function bootstrapCore(

Check failure on line 47 in web/src/core/bootstrap.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 18 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=InseeFrLab_onyxia&issues=AZ7Z7t0ACtsnrywd-NQf&open=AZ7Z7t0ACtsnrywd-NQf&pullRequest=1072
params: ParamsOfBootstrapCore
): Promise<{ core: Core }> {
const {
Expand Down Expand Up @@ -83,7 +85,6 @@
);
} catch (error) {
if (error instanceof AccessError) {
// NOTE: Not initialized yet, it's not a bug.
return undefined;
}
throw error;
Expand All @@ -105,7 +106,6 @@
);
} catch (error) {
if (error instanceof AccessError) {
// NOTE: Not initialized yet, it's not a bug.
return undefined;
}
throw error;
Expand Down Expand Up @@ -137,7 +137,6 @@

if (isAuthGloballyRequired && !oidc.isUserLoggedIn) {
await oidc.login({ doesCurrentHrefRequiresAuth: true });
// NOTE: Never reached
}

const context: Context = {
Expand Down Expand Up @@ -177,7 +176,8 @@
s3_region: s3Profile.paramsOfCreateS3Client.region
};
}
})
}),
ai: []
};

const { core, dispatch, getState } = createCore({
Expand Down Expand Up @@ -275,6 +275,72 @@
await dispatch(usecases.s3ProfilesManagement.protectedThunks.initialize());
}

init_ai: {

Check warning on line 278 in web/src/core/bootstrap.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this "init_ai" label.

See more on https://sonarcloud.io/project/issues?id=InseeFrLab_onyxia&issues=AZ6sSNoaj_1DzmHiyyhx&open=AZ6sSNoaj_1DzmHiyyhx&pullRequest=1072

Check warning on line 278 in web/src/core/bootstrap.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code to remove this label and the need for it.

See more on https://sonarcloud.io/project/issues?id=InseeFrLab_onyxia&issues=AZ6sSNoaj_1DzmHiyyhy&open=AZ6sSNoaj_1DzmHiyyhy&pullRequest=1072
if (!oidc.isUserLoggedIn) {
break init_ai;
}

const deploymentRegion =
usecases.deploymentRegionManagement.selectors.currentDeploymentRegion(
getState()
);

if (deploymentRegion.ai.length === 0) {
break init_ai;
}

const [{ createAi }, { createOidc, mergeOidcParams }, { oidcParams }] =
await Promise.all([
import("core/adapters/ai"),
import("core/adapters/oidc"),
onyxiaApi.getAvailableRegionsAndOidcParams()
]);

assert(oidcParams !== undefined);

// One Ai adapter per region-provided provider, but providers may share the
// same OIDC client: oidc-spa identifies a client by issuerUri + clientId, so
// creating it twice would collide. Create each distinct client only once.
const getOidcAccessTokenByOidcKey = new Map<string, () => Promise<string>>();

for (const aiConfig of deploymentRegion.ai) {
const oidcParams_ai = mergeOidcParams({
oidcParams,
oidcParams_partial: aiConfig.oidcParams
});

const oidcKey = `${oidcParams_ai.issuerUri}${oidcParams_ai.clientId}`;

let getOidcAccessToken = getOidcAccessTokenByOidcKey.get(oidcKey);

if (getOidcAccessToken === undefined) {
const oidc_ai = await createOidc({
...oidcParams_ai,
transformBeforeRedirectForKeycloakTheme,
getCurrentLang,
autoLogin: true,
enableDebugLogs: enableOidcDebugLogs
});

getOidcAccessToken = async () => (await oidc_ai.getTokens()).accessToken;

getOidcAccessTokenByOidcKey.set(oidcKey, getOidcAccessToken);
}

context.ai.push(
createAi({
id: aiConfig.id,
name: aiConfig.name ?? new URL(aiConfig.url).hostname,
webUiUrl: aiConfig.url,
oauthProvider: aiConfig.oauthProvider,
getOidcAccessToken
})
);
}

await dispatch(usecases.ai.protectedThunks.initialize());
}

pluginSystemInitCore({ core, context });

return { core };
Expand Down
13 changes: 13 additions & 0 deletions web/src/core/ports/Ai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type Ai = {
id: string;
name: string;
webUiUrl: string;
apiBase: string;
getToken: () => Promise<GetTokenResult>;
listModels: (token: string) => Promise<{ id: string; name: string }[]>;
};

export type GetTokenResult =
| { status: "success"; token: string }
| { status: "no-account" }
| { status: "error" };
7 changes: 7 additions & 0 deletions web/src/core/ports/OnyxiaApi/DeploymentRegion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ export type DeploymentRegion = {
oidcParams: OidcParams_Partial;
}
| undefined;
ai: {
id: string;
url: string;
name: string | undefined;
oauthProvider: string;
oidcParams: OidcParams_Partial;
}[];
proxyInjection:
| {
enabled: boolean | undefined;
Expand Down
10 changes: 10 additions & 0 deletions web/src/core/ports/OnyxiaApi/XOnyxia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ export type XOnyxiaContext = {
useCertManager: boolean;
certManagerClusterIssuer: string | undefined;
};
ai:
| {
enabled: boolean;
apiKey: string;
apiBase: string;
model: string;
provider: string;
embeddingsModel: string;
}
| undefined;
proxyInjection:
| {
enabled: boolean | undefined;
Expand Down
Loading
Loading