Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Qdrant Configuration Guidance Implementation Plan

> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.

**Goal:** Explain Qdrant configuration and prevent activation until a real embedding-to-Qdrant search verifies the selected model and collection work together.

**Architecture:** The health route remains read-only but exposes collection vector metadata. The card provides a localized mini tutorial and requires a successful search test before activation; that test produces an actual embedding, so it detects mismatched dimensions without guessing a model's size.

**Tech Stack:** Next.js App Router, React, TypeScript, Zod, next-intl, Node test runner, Vitest.

---

### Task 1: Read collection metadata in health checks

**Files:**

- Modify: `src/lib/memory/qdrant.ts`
- Modify: `tests/integration/qdrant-routes.test.ts`

- [ ] Add a failing integration test that mocks `/readyz` and `GET /collections/omniroute_memory`, then expects `collection: { exists: true, vectorSize: 2048, vectorName: "omniao" }` from the health route.
- [ ] Run `node --import tsx/esm --test tests/integration/qdrant-routes.test.ts` and observe the expected failure because health lacks collection metadata.
- [ ] Add `getQdrantCollectionMetadata()` to `src/lib/memory/qdrant.ts`. It may only read `GET /collections/<encoded collection>` and returns `{ exists: false }` or `{ exists: true, vectorSize, vectorName }`. It handles unnamed `vectors.size` and named-vector maps; it never returns API keys or changes Qdrant state.
- [ ] Extend `checkQdrantHealth()` to return this metadata after a successful `/readyz` probe.
- [ ] Re-run `node --import tsx/esm --test tests/integration/qdrant-routes.test.ts` and confirm it passes.

### Task 2: Tutorial and search-validation gate

**Files:**

- Modify: `src/app/(dashboard)/dashboard/memory/components/QdrantConfigCard.tsx`
- Modify: `tests/unit/ui/qdrant-config-card.test.tsx`

- [ ] Add failing component tests for a `data-testid="qdrant-setup-tutorial"` trigger, tutorial credit, disabled enable action before validation, and enabled action after a successful `/api/settings/qdrant/search` result.
- [ ] Run `npx vitest run tests/unit/ui/qdrant-config-card.test.tsx` and observe the expected failure.
- [ ] Add `tutorialOpen` and `searchValidated` state. Reset `searchValidated` when configuration is saved or search fails; set it only after `{ ok: true }` from the search endpoint.
- [ ] Disable only the transition that enables Qdrant while `searchValidated` is false; allow disabling normally.
- [ ] Render a compact modal opened from the tutorial trigger. It explains vector-memory retrieval, indirect token savings, HTTPS/API-key protection, matching dimensions, collection creation, and Save → Test connection → Test search. Add credit text through i18n: `Rafa Martins — rafacpti@gmail.com`.
- [ ] Display the health-route collection state: missing collection, unnamed vector size, or named vector plus size.
- [ ] Re-run `npx vitest run tests/unit/ui/qdrant-config-card.test.tsx` and confirm it passes.

### Task 3: Localization and verification

**Files:**

- Modify: `src/i18n/messages/en.json`
- Modify: `src/i18n/messages/pt-BR.json`

- [ ] Add matching English and Portuguese `memory.qdrant` strings for tutorial content, collection states, validation requirement, and credit.
- [ ] Format changed code with `npx prettier --write`.
- [ ] Run `node --import tsx/esm --test tests/integration/qdrant-routes.test.ts`.
- [ ] Run `npx vitest run src/lib/memory/__tests__/qdrant-wiring.test.ts tests/unit/ui/qdrant-config-card.test.tsx`.
- [ ] Run `npm run typecheck:core`.
- [ ] Commit with `feat: guide Qdrant memory configuration`, push `rafacpti23/qdrant-configuration-guidance` to `origin`, and open a draft PR to `diegosouzapw/OmniRoute`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Qdrant Configuration Guidance Design

## Goal

Make the Memory > Engine > Qdrant experience explain what Qdrant does, guide users through a safe configuration, and verify that the selected Qdrant collection accepts embeddings produced by the configured OmniRoute model before Qdrant is enabled.

## Scope

- Add a concise, localized explanation that Qdrant stores semantic-memory vectors for relevant-context retrieval. It is not a token compressor; token savings are indirect and depend on less irrelevant context being injected.
- Add a configuration checklist covering a protected Qdrant endpoint, host/port, collection, embedding provider/model, matching vector dimensions, connection test, and search test.
- Extend the authenticated Qdrant health route to inspect the configured collection without creating, updating, searching, or deleting points. Return the collection vector dimension and a clear state when the collection is absent or uses named vectors.
- Show a pre-enable compatibility result in the Qdrant card. If the endpoint is reachable but the vector dimension cannot be determined from the selected embedding model, the UI must explain that the search test is the authoritative end-to-end validation. If dimensions differ, the UI must block enabling and explain how to create a compatible collection.
- Keep the existing behavior that initial writes create a missing collection using the embedding dimension detected from the first successful embedding.

## User Flow

1. The user opens Dashboard > Memory > Engine and reads the purpose and prerequisites.
2. The user enters Qdrant host, port, collection, optional API key, and an embedding provider/model with a configured provider credential.
3. The user saves settings and clicks Test connection.
4. The health result reports endpoint status and, for an existing collection, its vector dimensions and named-vector configuration.
5. The user runs Test search. This generates an embedding through OmniRoute and proves that the model dimension matches the collection and that retrieval works.
6. The Enable control remains unavailable after a known incompatibility; otherwise it follows the existing setting update path, which sets `memoryVectorStore` to `qdrant`.

## Collection Creation Guidance

The UI will provide copyable Qdrant REST guidance, using a placeholder dimension rather than assuming one for every model:

```json
PUT /collections/<collection>
{
"vectors": { "size": <embedding-dimension>, "distance": "Cosine" }
}
```

For the audited server, the existing `omniroute_memory` collection has a named 2048-dimensional vector. It must be paired with the same 2048-dimensional embedding model that created it. The default `openai/text-embedding-3-small` emits 1536-dimensional vectors and therefore requires a separate 1536-dimensional collection.

## API Contract

`GET /api/settings/qdrant/health` will retain `{ ok, latencyMs, error? }` and add optional read-only metadata:

```ts
{
collection?: {
exists: boolean;
vectorSize?: number;
vectorName?: string | null;
};
}
```

The route must never expose Qdrant API keys. It must sanitize upstream error text before returning it.

## Error Handling

- A disconnected endpoint remains an error result, without changing settings.
- A missing collection is guidance, not an error: OmniRoute creates it on the first successful Qdrant write.
- A known dimension mismatch blocks enabling and tells the user to choose a matching model or a separate collection.
- A model whose dimension cannot be determined does not claim compatibility; the user must run Test search.

## Testing

- Route tests cover health metadata for single-vector, named-vector, missing-collection, and sanitized upstream-error responses.
- Component tests cover the purpose explanation, checklist, compatible/mismatch/missing collection states, and disabled enable action on a mismatch.
- Existing Qdrant route and card tests remain green.
102 changes: 92 additions & 10 deletions src/app/(dashboard)/dashboard/memory/components/QdrantConfigCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ export default function QdrantConfigCard() {
const [apiKeyInput, setApiKeyInput] = useState("");
const [saving, setSaving] = useState(false);
const [saveStatus, setSaveStatus] = useState<"" | "saved" | "error">("");
const [health, setHealth] = useState<{ ok: boolean; latencyMs: number; error?: string } | null>(
null
);
const [checking, setChecking] = useState(false);
const [health, setHealth] = useState<{
ok: boolean;
latencyMs: number;
error?: string;
collection?: { exists: boolean; vectorSize?: number; vectorName?: string | null };
} | null>(null);
const [searchValidated, setSearchValidated] = useState(false);
const [tutorialOpen, setTutorialOpen] = useState(false); const [checking, setChecking] = useState(false);
const [searchQuery, setSearchQuery] = useState("");
const [searching, setSearching] = useState(false);
const [searchResults, setSearchResults] = useState<
Expand Down Expand Up @@ -105,7 +109,7 @@ export default function QdrantConfigCard() {
// invalidate in-flight checks so they cannot overwrite the new state.
healthSeqRef.current += 1;
setHealth(null);
setQdrant(next);
setSearchValidated(false); setQdrant(next);
setSaving(true);
setSaveStatus("");
try {
Expand Down Expand Up @@ -149,8 +153,7 @@ export default function QdrantConfigCard() {
setSaving(false);
}
},
[qdrant, checkHealth]
);
[qdrant, checkHealth] );

// Auto-check on mount once settings load: without this the status badge
// renders red after a page refresh because `health` starts as null and the
Expand All @@ -176,9 +179,13 @@ export default function QdrantConfigCard() {
const data = await res.json().catch(() => null);
if (res.ok && data?.ok) {
setSearchResults(Array.isArray(data.results) ? data.results : []);
setSearchValidated(true);
} else {
setSearchValidated(false);
}
} catch {
setSearchResults([]);
setSearchValidated(false);
} finally {
setSearching(false);
}
Expand Down Expand Up @@ -221,6 +228,14 @@ export default function QdrantConfigCard() {
<div className="flex-1 min-w-0">
<h3 className="text-sm font-semibold text-text-main">{t("qdrant.title")}</h3>
<p className="text-xs text-text-muted">{t("qdrant.description")}</p>
<button
type="button"
data-testid="qdrant-setup-tutorial"
onClick={() => setTutorialOpen(true)}
className="mt-1 text-xs font-medium text-emerald-500 hover:underline"
>
Como configurar o Qdrant corretamente
</button>
</div>
<span
className={`inline-flex items-center gap-1.5 text-xs font-medium ${
Expand All @@ -230,8 +245,7 @@ export default function QdrantConfigCard() {
? "text-text-muted"
: health.ok
? "text-emerald-500"
: "text-red-500"
}`}
: "text-red-500" }`}
>
<span
className={`inline-block w-2.5 h-2.5 rounded-full ${
Expand Down Expand Up @@ -277,7 +291,7 @@ export default function QdrantConfigCard() {
<button
data-testid="qdrant-enabled-switch"
onClick={() => save({ enabled: !qdrant.enabled })}
disabled={saving}
disabled={saving || (!qdrant.enabled && !searchValidated)}
role="switch"
aria-checked={qdrant.enabled}
className={`relative w-11 h-6 rounded-full transition-colors ${
Expand All @@ -293,6 +307,13 @@ export default function QdrantConfigCard() {
</div>
</div>

{!qdrant.enabled && !searchValidated && (
<p className="-mt-2 mb-4 text-xs text-amber-500">
Execute um teste de busca bem-sucedido antes de ativar. Ele valida o modelo de embedding e
a dimensão da coleção.
</p>
)}

{health && (
<div
className={`mb-4 text-xs font-medium flex items-center gap-1 ${health.ok ? "text-emerald-500" : "text-red-500"}`}
Expand All @@ -305,6 +326,23 @@ export default function QdrantConfigCard() {
: (health.error ?? t("qdrant.healthError"))}
</div>
)}
{health?.collection && (
<div className="mb-4 rounded-lg border border-border/50 bg-surface/30 p-3 text-xs text-text-muted">
{health.collection.exists ? (
<>
Coleção compatível com vetores de dimensão{" "}
<strong>{health.collection.vectorSize}</strong>
{health.collection.vectorName ? ` (vetor: ${health.collection.vectorName})` : ""}. O
modelo configurado deve gerar a mesma dimensão.
</>
) : (
<>
A coleção ainda não existe. Ela será criada na primeira gravação com o modelo
validado.
</>
)}
</div>
)}

{saveStatus === "saved" && (
<div className="mb-4 text-xs font-medium text-emerald-500 flex items-center gap-1">
Expand Down Expand Up @@ -472,6 +510,50 @@ export default function QdrantConfigCard() {
</div>
{cleanupMsg && <p className="mt-2 text-xs text-text-muted">{cleanupMsg}</p>}
</div>
{tutorialOpen && (
<div
role="dialog"
aria-modal="true"
aria-label="Tutorial de configuração do Qdrant"
className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4"
>
<div className="max-h-[85vh] w-full max-w-2xl overflow-y-auto rounded-xl border border-border bg-background p-6 shadow-xl">
<div className="flex items-start justify-between gap-4">
<div>
<h4 className="text-lg font-semibold">Tutorial rápido: memória com Qdrant</h4>
<p className="mt-2 text-sm text-text-muted">
O Qdrant guarda vetores e metadados das memórias para recuperar contexto
relevante. Ele não comprime tokens diretamente; a economia é indireta, ao evitar
contexto sem relação com a solicitação.
</p>
</div>
<button
type="button"
onClick={() => setTutorialOpen(false)}
aria-label="Fechar tutorial"
>
×
</button>
</div>
<ol className="mt-5 list-decimal space-y-3 pl-5 text-sm text-text-muted">
<li>Proteja o servidor com HTTPS e API key antes de uso produtivo.</li>
<li>
Informe host, porta, coleção e um modelo no formato provider/model com credencial
configurada.
</li>
<li>
A dimensão da coleção precisa ser igual à dimensão produzida pelo modelo. Uma
coleção existente de 2048 dimensões não aceita embeddings de 1536 dimensões.
</li>
<li>Salve, teste a conexão e execute o teste de busca. Só então ative o Qdrant.</li>
</ol>
<pre className="mt-5 overflow-x-auto rounded-lg bg-surface p-3 text-xs">{`PUT /collections/minha_memoria\n{\n "vectors": { "size": <dimensão-do-modelo>, "distance": "Cosine" }\n}`}</pre>
<p className="mt-5 text-xs text-text-muted">
Créditos: Rafa Martins — rafacpti@gmail.com
</p>
</div>
</div>
)}
</Card>
);
}
43 changes: 38 additions & 5 deletions src/lib/memory/qdrant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export function normalizeQdrantConfig(settings: Record<string, unknown>): Qdrant
? process.env.QDRANT_API_KEY.trim()
: undefined;
const envCollection =
typeof process.env.QDRANT_COLLECTION === "string" && process.env.QDRANT_COLLECTION.trim().length > 0
typeof process.env.QDRANT_COLLECTION === "string" &&
process.env.QDRANT_COLLECTION.trim().length > 0
? process.env.QDRANT_COLLECTION.trim()
: undefined;

Expand All @@ -84,15 +85,19 @@ export function normalizeQdrantConfig(settings: Record<string, unknown>): Qdrant
? Math.round(portRaw)
: typeof portRaw === "string"
? Math.round(Number(portRaw) || 6333)
: envPort ?? 6333;
: (envPort ?? 6333);
const apiKey =
(typeof settings.qdrantApiKey === "string" && settings.qdrantApiKey.trim().length > 0
? settings.qdrantApiKey.trim()
: null) ?? envApiKey ?? null;
: null) ??
envApiKey ??
null;
const collection =
(typeof settings.qdrantCollection === "string" && settings.qdrantCollection.trim().length > 0
? settings.qdrantCollection.trim()
: null) ?? envCollection ?? "omniroute_memory";
: null) ??
envCollection ??
"omniroute_memory";
const embeddingModel =
(typeof settings.qdrantEmbeddingModel === "string" &&
settings.qdrantEmbeddingModel.trim().length > 0
Expand Down Expand Up @@ -175,10 +180,37 @@ async function qdrantFetch(cfg: QdrantConfig, path: string, init?: RequestInit):
});
}

export type QdrantCollectionMetadata =
{ exists: false } | { exists: true; vectorSize: number; vectorName: string | null };

export async function getQdrantCollectionMetadata(): Promise<QdrantCollectionMetadata | null> {
const cfg = await getQdrantConfig();
if (!cfg.enabled || !cfg.host) return null;

const res = await qdrantFetch(cfg, `/collections/${encodeURIComponent(cfg.collection)}`, {
method: "GET",
});
if (res.status === 404) return { exists: false };
if (!res.ok) return null;

const data = (await res.json().catch(() => null)) as any;
const vectors = data?.result?.config?.params?.vectors;
if (!vectors || typeof vectors !== "object" || Array.isArray(vectors)) return null;
if (typeof vectors.size === "number") {
return { exists: true, vectorSize: vectors.size, vectorName: null };
}

const vectorName = Object.keys(vectors)[0];
const vectorSize = vectorName ? vectors[vectorName]?.size : null;
if (typeof vectorSize !== "number") return null;
return { exists: true, vectorSize, vectorName };
}

export async function checkQdrantHealth(): Promise<{
ok: boolean;
latencyMs: number;
error?: string;
collection?: QdrantCollectionMetadata;
}> {
const cfg = await getQdrantConfig();
const start = Date.now();
Expand All @@ -193,7 +225,8 @@ export async function checkQdrantHealth(): Promise<{
const text = await res.text().catch(() => "");
return { ok: false, latencyMs, error: text.slice(0, 200) || `HTTP ${res.status}` };
}
return { ok: true, latencyMs };
const collection = await getQdrantCollectionMetadata();
return { ok: true, latencyMs, ...(collection ? { collection } : {}) };
} catch (err) {
return {
ok: false,
Expand Down
Loading
Loading