fix(milvus): derive default writes from schema
This commit is contained in:
1 parent
dfdf916964
commit
b39a0f18a4
6 files changed
+956
-175
No files matched your search
@@ -630,18 +630,19 @@ async function toggle() {
|
||||
const collectionRef = (node.meta as { collectionId?: string } | undefined)?.collectionId ?? node.label;
|
||||
const tab = queryStore.createTab(node.connectionId, node.database || "default", node.label, "vector");
|
||||
queryStore.updateSql(tab, collectionRef);
|
||||
api
|
||||
.vectorGetCollectionDetail(node.connectionId, node.database || "default", collectionRef)
|
||||
.then((info) => {
|
||||
if (info.dimension != null) {
|
||||
if (connectionStore.getConfig(node.connectionId)?.db_type !== "milvus") {
|
||||
api
|
||||
.vectorGetCollectionDetail(node.connectionId, node.database || "default", collectionRef)
|
||||
.then((info) => {
|
||||
if (info.dimension == null) return;
|
||||
if (node.meta) {
|
||||
(node.meta as Record<string, unknown>).dimension = info.dimension;
|
||||
} else {
|
||||
node.meta = { dimension: info.dimension } as any;
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(() => {});
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
} else if (node.type === "database" && node.connectionId && hasTreeNodeDatabaseContext(node)) {
|
||||
if (node.catalog && node.catalog !== "internal") {
|
||||
await connectionStore.loadDorisCatalogTables(node);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, watch, defineAsyncComponent } from "vue";
|
||||
import { computed, ref, watch, defineAsyncComponent, onBeforeUnmount } from "vue";
|
||||
import { Play, RefreshCcw, RotateCcw, Save, Search, Trash2 } from "@lucide/vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { Button } from "@/components/ui/button";
|
||||
@@ -7,7 +7,7 @@ import ErrorBanner from "@/components/ui/ErrorBanner.vue";
|
||||
import QueryLoadingState from "@/components/common/QueryLoadingState.vue";
|
||||
import * as api from "@/lib/backend/api";
|
||||
import { uuid } from "@/lib/common/utils";
|
||||
import type { DatabaseType, QueryResult } from "@/types/database";
|
||||
import type { DatabaseType, MilvusCollectionSchema, MilvusFieldInfo, QueryResult } from "@/types/database";
|
||||
|
||||
const DataGrid = defineAsyncComponent(() => import("@/components/grid/DataGrid.vue"));
|
||||
const { t } = useI18n();
|
||||
@@ -31,14 +31,22 @@ const error = ref("");
|
||||
const statusMessage = ref("");
|
||||
const result = ref<QueryResult>(emptyResult());
|
||||
const operationMode = ref<VectorOperationMode>("browse");
|
||||
const requestText = ref(defaultRequestText(props.databaseType, props.database, props.collection, operationMode.value));
|
||||
const requestText = ref("");
|
||||
const requestIsDefault = ref(true);
|
||||
const searchVector = ref("");
|
||||
const searchTopK = ref(10);
|
||||
let loadingTimer: ReturnType<typeof setInterval> | undefined;
|
||||
const milvusSchema = ref<MilvusCollectionSchema>();
|
||||
const milvusDetailError = ref("");
|
||||
let milvusDetailGeneration = 0;
|
||||
let milvusDetailLoad: Promise<void> | undefined;
|
||||
|
||||
const dim = computed(() => props.dimension || 4);
|
||||
function sampleVector(): number[] {
|
||||
return Array.from({ length: dim.value }, (_, i) => parseFloat(((i + 1) / 10).toFixed(1)));
|
||||
const milvusFields = computed(() => milvusSchema.value?.fields ?? []);
|
||||
const milvusSearchField = computed(() => milvusFields.value.find((field) => isMilvusVectorField(field) && !field.isFunctionOutput) ?? milvusFields.value.find(isMilvusVectorField));
|
||||
const collectionDimension = computed(() => milvusSearchField.value?.dimension ?? props.dimension);
|
||||
const dim = computed(() => collectionDimension.value ?? 4);
|
||||
function sampleVector(dimension = dim.value): number[] {
|
||||
return Array.from({ length: Math.max(1, dimension) }, (_, i) => parseFloat(((i + 1) / 10).toFixed(1)));
|
||||
}
|
||||
const productLabel = computed(() => {
|
||||
switch (props.databaseType) {
|
||||
@@ -79,20 +87,24 @@ const needsExplicitSearchVector = computed(() => operationMode.value === "search
|
||||
const executeDisabled = computed(() => loading.value || !requestText.value.trim() || needsExplicitSearchVector.value);
|
||||
|
||||
watch(
|
||||
() => [props.databaseType, props.database, props.collection] as const,
|
||||
([databaseType, database, collection]) => {
|
||||
requestText.value = defaultRequestText(databaseType, database, collection, operationMode.value);
|
||||
() => [props.connectionId, props.databaseType, props.database, props.collection] as const,
|
||||
() => {
|
||||
invalidateRequest();
|
||||
resetRequest();
|
||||
result.value = emptyResult();
|
||||
error.value = "";
|
||||
statusMessage.value = "";
|
||||
resetMilvusCollectionDetail();
|
||||
if (props.databaseType === "milvus") void loadMilvusCollectionDetail();
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
watch(
|
||||
() => [operationMode.value, searchVector.value, searchTopK.value, props.dimension] as const,
|
||||
() => [operationMode.value, searchVector.value, searchTopK.value, collectionDimension.value] as const,
|
||||
() => {
|
||||
if (operationMode.value === "search") {
|
||||
requestText.value = defaultRequestText(props.databaseType, props.database, props.collection, operationMode.value);
|
||||
if (operationMode.value === "search" && requestIsDefault.value) {
|
||||
resetRequest();
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -112,46 +124,103 @@ function pathSegment(value: string): string {
|
||||
return encodeURIComponent(value || "collection");
|
||||
}
|
||||
|
||||
function defaultRequestText(databaseType: DatabaseType | undefined, database: string, collection: string, mode: VectorOperationMode): string {
|
||||
if (databaseType === "milvus") {
|
||||
const body =
|
||||
mode === "delete"
|
||||
? {
|
||||
dbName: database || "default",
|
||||
collectionName: collection,
|
||||
filter: "id in [1]",
|
||||
}
|
||||
: mode === "upsert"
|
||||
? {
|
||||
dbName: database || "default",
|
||||
collectionName: collection,
|
||||
data: [
|
||||
{
|
||||
id: 1,
|
||||
vector: sampleVector(),
|
||||
title: "updated vector",
|
||||
kind: "demo",
|
||||
},
|
||||
],
|
||||
}
|
||||
: mode === "search"
|
||||
? {
|
||||
dbName: database || "default",
|
||||
collectionName: collection,
|
||||
data: [tryParseVector(searchVector.value)],
|
||||
limit: searchTopK.value,
|
||||
outputFields: ["*"],
|
||||
}
|
||||
: {
|
||||
dbName: database || "default",
|
||||
collectionName: collection,
|
||||
filter: "",
|
||||
limit: 100,
|
||||
outputFields: ["*"],
|
||||
};
|
||||
const endpoint = mode === "delete" ? "delete" : mode === "upsert" ? "upsert" : mode === "search" ? "search" : "query";
|
||||
return `POST /v2/vectordb/entities/${endpoint}\n${JSON.stringify(body, null, 2)}`;
|
||||
function milvusDataType(field: Pick<MilvusFieldInfo, "dataType">): string {
|
||||
return field.dataType.toLowerCase();
|
||||
}
|
||||
|
||||
function isMilvusVectorField(field: MilvusFieldInfo): boolean {
|
||||
return milvusDataType(field).endsWith("vector");
|
||||
}
|
||||
|
||||
function defaultMilvusValue(field: MilvusFieldInfo): unknown {
|
||||
const type = milvusDataType(field);
|
||||
if (isMilvusVectorField(field)) {
|
||||
const dimension = field.dimension ?? dim.value;
|
||||
if (type === "sparsefloatvector") return { "0": 0.1 };
|
||||
if (type === "binaryvector") return Array.from({ length: Math.max(8, dimension) }, (_, index) => Number(index === 0));
|
||||
if (type === "int8vector") return Array.from({ length: Math.max(1, dimension) }, () => 1);
|
||||
return sampleVector(dimension);
|
||||
}
|
||||
switch (type) {
|
||||
case "bool":
|
||||
return false;
|
||||
case "int8":
|
||||
case "int16":
|
||||
case "int32":
|
||||
case "int64":
|
||||
return 1;
|
||||
case "float":
|
||||
case "double":
|
||||
return 0.1;
|
||||
case "json":
|
||||
return {};
|
||||
case "array":
|
||||
return [];
|
||||
case "geometry":
|
||||
return "POINT (0 0)";
|
||||
default:
|
||||
return "x";
|
||||
}
|
||||
}
|
||||
|
||||
function defaultMilvusUpsertEntity(): Record<string, unknown> {
|
||||
return Object.fromEntries(milvusFields.value.filter((field) => !field.isFunctionOutput && (!field.autoId || field.primaryKey) && (isMilvusVectorField(field) || (!field.nullable && !field.hasDefaultValue))).map((field) => [field.name, defaultMilvusValue(field)]));
|
||||
}
|
||||
|
||||
function defaultMilvusPrimaryKeyFilter(): string {
|
||||
const primaryKey = milvusFields.value.find((field) => field.primaryKey);
|
||||
if (!primaryKey) return "id in [1]";
|
||||
return `${primaryKey.name} in [${JSON.stringify(defaultMilvusValue(primaryKey))}]`;
|
||||
}
|
||||
|
||||
function parseJsonValue(input: string): unknown | undefined {
|
||||
try {
|
||||
return JSON.parse(input);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function tryParseMilvusSearchVector(input: string): unknown {
|
||||
const field = milvusSearchField.value;
|
||||
const parsed = parseJsonValue(input);
|
||||
if (field?.isFunctionOutput) return typeof parsed === "string" ? parsed : input.trim() || "x";
|
||||
if (Array.isArray(parsed) || (parsed && typeof parsed === "object")) return parsed;
|
||||
return field ? defaultMilvusValue(field) : [];
|
||||
}
|
||||
|
||||
function milvusRequestText(database: string, collection: string, mode: VectorOperationMode, strongConsistency = false): string {
|
||||
const base = { dbName: database || "default", collectionName: collection };
|
||||
let endpoint: string;
|
||||
let body: Record<string, unknown>;
|
||||
switch (mode) {
|
||||
case "delete":
|
||||
endpoint = "delete";
|
||||
body = { ...base, filter: defaultMilvusPrimaryKeyFilter() };
|
||||
break;
|
||||
case "upsert":
|
||||
endpoint = "upsert";
|
||||
body = { ...base, data: [defaultMilvusUpsertEntity()] };
|
||||
break;
|
||||
case "search":
|
||||
endpoint = "search";
|
||||
body = {
|
||||
...base,
|
||||
data: [tryParseMilvusSearchVector(searchVector.value)],
|
||||
limit: searchTopK.value,
|
||||
outputFields: ["*"],
|
||||
...(milvusSearchField.value ? { annsField: milvusSearchField.value.name } : {}),
|
||||
};
|
||||
break;
|
||||
default:
|
||||
endpoint = "query";
|
||||
body = { ...base, filter: "", limit: 100, outputFields: ["*"], ...(strongConsistency && { consistencyLevel: "Strong" }) };
|
||||
}
|
||||
return `POST /v2/vectordb/entities/${endpoint}\n${JSON.stringify(body, null, 2)}`;
|
||||
}
|
||||
|
||||
function defaultRequestText(databaseType: DatabaseType | undefined, database: string, collection: string, mode: VectorOperationMode, strongConsistency = false): string {
|
||||
if (databaseType === "milvus") return milvusRequestText(database, collection, mode, strongConsistency);
|
||||
if (databaseType === "weaviate") {
|
||||
const collectionName = collection || "Collection";
|
||||
if (mode === "delete") {
|
||||
@@ -218,12 +287,48 @@ function defaultRequestText(databaseType: DatabaseType | undefined, database: st
|
||||
return `POST /collections/${collectionPath}/points/scroll\n${JSON.stringify({ limit: 100, with_payload: true, with_vector: false }, null, 2)}`;
|
||||
}
|
||||
|
||||
function resetMilvusCollectionDetail() {
|
||||
milvusDetailGeneration += 1;
|
||||
milvusDetailLoad = undefined;
|
||||
milvusSchema.value = undefined;
|
||||
milvusDetailError.value = "";
|
||||
}
|
||||
|
||||
function loadMilvusCollectionDetail(): Promise<void> {
|
||||
if (props.databaseType !== "milvus" || !props.connectionId || !props.collection) return Promise.resolve();
|
||||
if (milvusDetailLoad) return milvusDetailLoad;
|
||||
|
||||
const generation = ++milvusDetailGeneration;
|
||||
const promise = api
|
||||
.vectorGetCollectionDetail(props.connectionId, props.database || "default", props.collection)
|
||||
.then((info) => {
|
||||
if (generation !== milvusDetailGeneration) return;
|
||||
milvusSchema.value = info.milvusSchema;
|
||||
if (!milvusSearchField.value) milvusDetailError.value = "Milvus collection schema did not return a supported vector field.";
|
||||
if (requestIsDefault.value) resetRequest();
|
||||
})
|
||||
.catch((reason: unknown) => {
|
||||
if (generation !== milvusDetailGeneration) return;
|
||||
milvusDetailError.value = reason instanceof Error ? reason.message : String(reason);
|
||||
milvusDetailLoad = undefined;
|
||||
});
|
||||
return (milvusDetailLoad = promise);
|
||||
}
|
||||
|
||||
async function ensureMilvusDefaultSchema() {
|
||||
if (props.databaseType !== "milvus" || operationMode.value === "browse" || !requestIsDefault.value) return;
|
||||
if (!milvusSchema.value) await loadMilvusCollectionDetail();
|
||||
if (!milvusSchema.value) {
|
||||
throw new Error(milvusDetailError.value || "Unable to load the Milvus collection schema for the default request.");
|
||||
}
|
||||
if (operationMode.value === "search" && !milvusSearchField.value) {
|
||||
throw new Error(milvusDetailError.value || "Unable to determine the Milvus vector field for the default request.");
|
||||
}
|
||||
}
|
||||
|
||||
function parseVector(input: string): number[] | undefined {
|
||||
try {
|
||||
const parsed = JSON.parse(input);
|
||||
if (Array.isArray(parsed) && parsed.length > 0) return parsed;
|
||||
} catch {}
|
||||
return undefined;
|
||||
const parsed = parseJsonValue(input);
|
||||
return Array.isArray(parsed) && parsed.length > 0 ? (parsed as number[]) : undefined;
|
||||
}
|
||||
|
||||
function tryParseVector(input: string): number[] {
|
||||
@@ -256,7 +361,7 @@ async function executeRequestText(text: string): Promise<QueryResult> {
|
||||
return firstResult(results);
|
||||
}
|
||||
|
||||
async function refreshResult() {
|
||||
async function withLoading(run: (id: string) => Promise<void>) {
|
||||
if (loading.value) return;
|
||||
const id = uuid();
|
||||
executionId.value = id;
|
||||
@@ -266,56 +371,74 @@ async function refreshResult() {
|
||||
statusMessage.value = "";
|
||||
startTimer();
|
||||
try {
|
||||
const browseText = defaultRequestText(props.databaseType, props.database, props.collection, "browse");
|
||||
const nextResult = await executeRequestText(browseText);
|
||||
if (executionId.value === id) result.value = nextResult;
|
||||
await run(id);
|
||||
} catch (e: unknown) {
|
||||
if (executionId.value === id) error.value = e instanceof Error ? e.message : String(e);
|
||||
} finally {
|
||||
if (executionId.value === id) {
|
||||
loading.value = false;
|
||||
executionId.value = "";
|
||||
stopTimer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function runRequest() {
|
||||
if (loading.value) return;
|
||||
const id = uuid();
|
||||
executionId.value = id;
|
||||
loading.value = true;
|
||||
cancelling.value = false;
|
||||
error.value = "";
|
||||
statusMessage.value = "";
|
||||
startTimer();
|
||||
try {
|
||||
function refreshResult() {
|
||||
return withLoading(async (id) => {
|
||||
const browseText = defaultRequestText(props.databaseType, props.database, props.collection, "browse");
|
||||
const nextResult = await executeRequestText(browseText);
|
||||
if (executionId.value === id) result.value = nextResult;
|
||||
});
|
||||
}
|
||||
|
||||
function runRequest() {
|
||||
return withLoading(async (id) => {
|
||||
await ensureMilvusDefaultSchema();
|
||||
if (executionId.value !== id) return;
|
||||
const nextResult = await executeRequestText(requestText.value);
|
||||
if (executionId.value !== id) return;
|
||||
if (operationMode.value === "browse" || operationMode.value === "search") {
|
||||
result.value = nextResult;
|
||||
} else {
|
||||
const browseText = defaultRequestText(props.databaseType, props.database, props.collection, "browse");
|
||||
result.value = await executeRequestText(browseText);
|
||||
const browseText = defaultRequestText(props.databaseType, props.database, props.collection, "browse", true);
|
||||
const browseResult = await executeRequestText(browseText);
|
||||
if (executionId.value !== id) return;
|
||||
result.value = browseResult;
|
||||
statusMessage.value = t("vector.operationSuccess");
|
||||
}
|
||||
} catch (e: unknown) {
|
||||
if (executionId.value === id) error.value = e instanceof Error ? e.message : String(e);
|
||||
} finally {
|
||||
if (executionId.value === id) {
|
||||
loading.value = false;
|
||||
stopTimer();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function cancelRequest() {
|
||||
if (!executionId.value) return;
|
||||
const id = executionId.value;
|
||||
if (!id || cancelling.value) return;
|
||||
cancelling.value = true;
|
||||
await api.cancelQuery(executionId.value).catch(() => false);
|
||||
executionId.value = "";
|
||||
try {
|
||||
await api.cancelQuery(id).catch(() => false);
|
||||
} finally {
|
||||
loading.value = false;
|
||||
cancelling.value = false;
|
||||
stopTimer();
|
||||
}
|
||||
}
|
||||
|
||||
function invalidateRequest() {
|
||||
const id = executionId.value;
|
||||
executionId.value = "";
|
||||
loading.value = false;
|
||||
cancelling.value = false;
|
||||
stopTimer();
|
||||
if (id) void api.cancelQuery(id).catch(() => false);
|
||||
}
|
||||
|
||||
function resetRequest() {
|
||||
requestText.value = defaultRequestText(props.databaseType, props.database, props.collection, operationMode.value);
|
||||
requestIsDefault.value = true;
|
||||
}
|
||||
|
||||
function markRequestAsCustom() {
|
||||
requestIsDefault.value = false;
|
||||
}
|
||||
|
||||
function setOperationMode(mode: VectorOperationMode) {
|
||||
@@ -325,6 +448,11 @@ function setOperationMode(mode: VectorOperationMode) {
|
||||
error.value = "";
|
||||
statusMessage.value = "";
|
||||
}
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
invalidateRequest();
|
||||
resetMilvusCollectionDetail();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -334,7 +462,7 @@ function setOperationMode(mode: VectorOperationMode) {
|
||||
<div class="truncate text-sm font-semibold">{{ collectionLabel }}</div>
|
||||
<div class="truncate text-xs text-muted-foreground">
|
||||
{{ t("vector.productCollection", { product: productLabel }) }}
|
||||
<span v-if="dimension != null" class="ml-1.5 inline-flex items-center rounded border bg-muted/50 px-1.5 py-px text-[11px] font-medium text-foreground/80">{{ dimension }}d</span>
|
||||
<span v-if="collectionDimension != null" class="ml-1.5 inline-flex items-center rounded border bg-muted/50 px-1.5 py-px text-[11px] font-medium text-foreground/80">{{ collectionDimension }}d</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex shrink-0 items-center gap-1.5">
|
||||
@@ -362,17 +490,27 @@ function setOperationMode(mode: VectorOperationMode) {
|
||||
<div v-if="operationMode === 'search'" class="flex shrink-0 flex-wrap items-center gap-3 border-b px-3 py-2">
|
||||
<div class="flex items-center gap-1.5 text-xs text-muted-foreground">
|
||||
<span>{{ t("vector.vectorLabel") }}</span>
|
||||
<input v-model="searchVector" class="h-7 w-80 rounded border bg-muted/30 px-2 font-mono text-xs outline-none focus:border-primary" placeholder="[0.1, 0.2, ...]" spellcheck="false" />
|
||||
<input v-model="searchVector" :disabled="loading" class="h-7 w-80 rounded border bg-muted/30 px-2 font-mono text-xs outline-none focus:border-primary" placeholder="[0.1, 0.2, ...]" spellcheck="false" />
|
||||
</div>
|
||||
<div class="flex items-center gap-1.5 text-xs text-muted-foreground">
|
||||
<span>topK</span>
|
||||
<input v-model.number="searchTopK" type="number" min="1" max="1000" class="h-7 w-16 rounded border bg-muted/30 px-2 text-xs outline-none focus:border-primary" />
|
||||
<input v-model.number="searchTopK" :disabled="loading" type="number" min="1" max="1000" class="h-7 w-16 rounded border bg-muted/30 px-2 text-xs outline-none focus:border-primary" />
|
||||
</div>
|
||||
<span v-if="needsExplicitSearchVector" class="text-xs text-amber-600 dark:text-amber-400">{{ t("vector.vectorDimensionRequired") }}</span>
|
||||
</div>
|
||||
<div class="grid min-h-0 flex-1 grid-rows-[minmax(9rem,15rem)_1fr]">
|
||||
<div class="min-h-0 border-b">
|
||||
<textarea v-model="requestText" class="dbx-editor-font-family h-full w-full resize-none bg-background px-3 py-2 text-xs leading-5 outline-none" :aria-label="t('vector.requestEditor')" spellcheck="false" autocomplete="off" autocapitalize="off" autocorrect="off" />
|
||||
<textarea
|
||||
v-model="requestText"
|
||||
:readonly="loading"
|
||||
class="dbx-editor-font-family h-full w-full resize-none bg-background px-3 py-2 text-xs leading-5 outline-none"
|
||||
:aria-label="t('vector.requestEditor')"
|
||||
spellcheck="false"
|
||||
autocomplete="off"
|
||||
autocapitalize="off"
|
||||
autocorrect="off"
|
||||
@input="markRequestAsCustom"
|
||||
/>
|
||||
</div>
|
||||
<div class="min-h-0">
|
||||
<ErrorBanner v-if="error" :message="error" copy-mode="label" dismissible @dismiss="error = ''" />
|
||||
|
||||
@@ -0,0 +1,466 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { createApp, defineComponent, h, nextTick, ref, type App } from "vue";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { CollectionInfo, MilvusFieldInfo } from "@/types/database";
|
||||
|
||||
const backend = vi.hoisted(() => ({
|
||||
cancelQuery: vi.fn(),
|
||||
executeMulti: vi.fn(),
|
||||
vectorGetCollectionDetail: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
|
||||
vi.mock("@/lib/backend/api", () => backend);
|
||||
vi.mock("@lucide/vue", async () => {
|
||||
const { defineComponent, h } = await import("vue");
|
||||
const Icon = defineComponent({ setup: () => () => h("i") });
|
||||
return { Play: Icon, RefreshCcw: Icon, RotateCcw: Icon, Save: Icon, Search: Icon, Trash2: Icon };
|
||||
});
|
||||
vi.mock("@/components/ui/button", async () => {
|
||||
const { defineComponent, h } = await import("vue");
|
||||
return {
|
||||
Button: defineComponent({
|
||||
inheritAttrs: false,
|
||||
setup(_, { attrs, slots }) {
|
||||
return () => h("button", attrs, slots.default?.());
|
||||
},
|
||||
}),
|
||||
};
|
||||
});
|
||||
vi.mock("@/components/common/QueryLoadingState.vue", async () => {
|
||||
const { defineComponent, h } = await import("vue");
|
||||
return {
|
||||
default: defineComponent({
|
||||
emits: ["cancel"],
|
||||
setup(_, { emit }) {
|
||||
return () => h("button", { onClick: () => emit("cancel") }, "cancel");
|
||||
},
|
||||
}),
|
||||
};
|
||||
});
|
||||
vi.mock("@/components/ui/ErrorBanner.vue", async () => {
|
||||
const { defineComponent, h } = await import("vue");
|
||||
return { default: defineComponent({ setup: () => () => h("div") }) };
|
||||
});
|
||||
vi.mock("@/components/grid/DataGrid.vue", async () => {
|
||||
const { defineComponent, h } = await import("vue");
|
||||
return { __esModule: true, default: defineComponent({ setup: () => () => h("div") }) };
|
||||
});
|
||||
|
||||
import VectorBrowser from "@/components/vector/VectorBrowser.vue";
|
||||
|
||||
let app: App<Element> | null = null;
|
||||
let root: HTMLDivElement | null = null;
|
||||
|
||||
async function flushUi() {
|
||||
await Promise.resolve();
|
||||
await nextTick();
|
||||
await Promise.resolve();
|
||||
await nextTick();
|
||||
}
|
||||
|
||||
function buttonWithText(text: string): HTMLButtonElement {
|
||||
const button = [...root!.querySelectorAll<HTMLButtonElement>("button")].find((candidate) => candidate.textContent?.trim() === text);
|
||||
if (!button) throw new Error(`Button not found: ${text}`);
|
||||
return button;
|
||||
}
|
||||
|
||||
function requestBody() {
|
||||
const request = root!.querySelector<HTMLTextAreaElement>("textarea")!.value;
|
||||
return JSON.parse(request.slice(request.indexOf("\n") + 1));
|
||||
}
|
||||
|
||||
function field(name: string, dataType: string, overrides: Partial<MilvusFieldInfo> = {}): MilvusFieldInfo {
|
||||
return { name, dataType, primaryKey: false, autoId: false, nullable: false, hasDefaultValue: false, isFunctionOutput: false, ...overrides };
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
backend.cancelQuery.mockReset();
|
||||
backend.executeMulti.mockReset();
|
||||
backend.vectorGetCollectionDetail.mockReset();
|
||||
backend.cancelQuery.mockResolvedValue(false);
|
||||
backend.executeMulti.mockResolvedValue([{ columns: [], column_types: [], column_sortables: [], rows: [], affected_rows: 0, execution_time_ms: 0 }]);
|
||||
root = document.createElement("div");
|
||||
document.body.appendChild(root);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
app?.unmount();
|
||||
app = null;
|
||||
root?.remove();
|
||||
root = null;
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
describe("VectorBrowser default requests", () => {
|
||||
it("keeps non-Milvus browse defaults schema-free", async () => {
|
||||
for (const [databaseType, expected] of [
|
||||
["qdrant", "POST /collections/demo/points/scroll"],
|
||||
["weaviate", "GET /v1/objects?class=demo&limit=100"],
|
||||
["chromadb", "POST /api/v2/tenants/default_tenant/databases/default_database/collections/demo/get"],
|
||||
] as const) {
|
||||
app = createApp(VectorBrowser, { connectionId: "vector-1", database: "default", collection: "demo", databaseType });
|
||||
app.mount(root!);
|
||||
await flushUi();
|
||||
expect(root!.querySelector<HTMLTextAreaElement>("textarea")!.value).toContain(expected);
|
||||
app.unmount();
|
||||
app = null;
|
||||
root!.replaceChildren();
|
||||
}
|
||||
expect(backend.vectorGetCollectionDetail).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("resolves the schema before executing a generated upsert request", async () => {
|
||||
let resolveDetail: (value: CollectionInfo) => void;
|
||||
backend.vectorGetCollectionDetail.mockReturnValue(
|
||||
new Promise((resolve) => {
|
||||
resolveDetail = resolve;
|
||||
}),
|
||||
);
|
||||
|
||||
app = createApp(VectorBrowser, {
|
||||
connectionId: "milvus-1",
|
||||
database: "default",
|
||||
collection: "script_milvus_crud_demo",
|
||||
databaseType: "milvus",
|
||||
});
|
||||
app.mount(root!);
|
||||
await nextTick();
|
||||
|
||||
buttonWithText("vector.upsert").click();
|
||||
await nextTick();
|
||||
buttonWithText("vector.apply").click();
|
||||
await nextTick();
|
||||
expect(requestBody().data).toEqual([{}]);
|
||||
expect(root!.querySelector<HTMLTextAreaElement>("textarea")!.readOnly).toBe(true);
|
||||
expect(backend.executeMulti).not.toHaveBeenCalled();
|
||||
expect(backend.vectorGetCollectionDetail).toHaveBeenCalledTimes(1);
|
||||
|
||||
resolveDetail!({
|
||||
name: "script_milvus_crud_demo",
|
||||
id: "script_milvus_crud_demo",
|
||||
dimension: 4,
|
||||
milvusSchema: { fields: [field("id", "Int64", { primaryKey: true }), field("embedding", "FloatVector", { dimension: 4 })] },
|
||||
});
|
||||
await flushUi();
|
||||
|
||||
const request = root!.querySelector<HTMLTextAreaElement>("textarea")!.value;
|
||||
expect(request).toContain('"embedding"');
|
||||
expect(request).not.toContain('"vector"');
|
||||
expect(backend.executeMulti.mock.calls[0][2]).toContain('"embedding"');
|
||||
});
|
||||
|
||||
it("generates a default entity from required schema fields", async () => {
|
||||
backend.vectorGetCollectionDetail.mockResolvedValue({
|
||||
name: "documents",
|
||||
id: "documents",
|
||||
dimension: 3,
|
||||
milvusSchema: {
|
||||
fields: [
|
||||
field("document_id", "VarChar", { primaryKey: true }),
|
||||
field("embedding", "FloatVector", { dimension: 3 }),
|
||||
field("keywords", "SparseFloatVector"),
|
||||
field("rating", "Double"),
|
||||
field("metadata", "JSON"),
|
||||
field("tags", "Array"),
|
||||
field("optional_note", "VarChar", { nullable: true }),
|
||||
field("status", "VarChar", { hasDefaultValue: true }),
|
||||
field("bm25", "SparseFloatVector", { isFunctionOutput: true }),
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
app = createApp(VectorBrowser, {
|
||||
connectionId: "milvus-1",
|
||||
database: "default",
|
||||
collection: "documents",
|
||||
databaseType: "milvus",
|
||||
dimension: 9,
|
||||
});
|
||||
app.mount(root!);
|
||||
await flushUi();
|
||||
|
||||
buttonWithText("vector.upsert").click();
|
||||
await nextTick();
|
||||
|
||||
expect(requestBody().data[0]).toEqual({
|
||||
document_id: "x",
|
||||
embedding: [0.1, 0.2, 0.3],
|
||||
keywords: { "0": 0.1 },
|
||||
rating: 0.1,
|
||||
metadata: {},
|
||||
tags: [],
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps an auto-generated primary key in the default upsert entity", async () => {
|
||||
backend.vectorGetCollectionDetail.mockResolvedValue({
|
||||
name: "documents",
|
||||
id: "documents",
|
||||
milvusSchema: {
|
||||
fields: [field("document_id", "Int64", { primaryKey: true, autoId: true }), field("embedding", "FloatVector", { dimension: 3 })],
|
||||
},
|
||||
});
|
||||
|
||||
app = createApp(VectorBrowser, {
|
||||
connectionId: "milvus-1",
|
||||
database: "default",
|
||||
collection: "documents",
|
||||
databaseType: "milvus",
|
||||
});
|
||||
app.mount(root!);
|
||||
await flushUi();
|
||||
|
||||
buttonWithText("vector.upsert").click();
|
||||
await nextTick();
|
||||
|
||||
expect(requestBody().data[0]).toEqual({ document_id: 1, embedding: [0.1, 0.2, 0.3] });
|
||||
});
|
||||
|
||||
it("uses a BM25 function output field for full-text search", async () => {
|
||||
backend.vectorGetCollectionDetail.mockResolvedValue({
|
||||
name: "documents",
|
||||
id: "documents",
|
||||
milvusSchema: {
|
||||
fields: [field("document_id", "Int64", { primaryKey: true }), field("text", "VarChar"), field("sparse", "SparseFloatVector", { isFunctionOutput: true })],
|
||||
},
|
||||
});
|
||||
|
||||
app = createApp(VectorBrowser, {
|
||||
connectionId: "milvus-1",
|
||||
database: "default",
|
||||
collection: "documents",
|
||||
databaseType: "milvus",
|
||||
});
|
||||
app.mount(root!);
|
||||
await flushUi();
|
||||
|
||||
buttonWithText("vector.search").click();
|
||||
await nextTick();
|
||||
|
||||
expect(requestBody()).toMatchObject({ annsField: "sparse", data: ["x"] });
|
||||
|
||||
const searchButtons = [...root!.querySelectorAll<HTMLButtonElement>("button")].filter((button) => button.textContent?.trim() === "vector.search");
|
||||
searchButtons[searchButtons.length - 1].click();
|
||||
await flushUi();
|
||||
expect(backend.executeMulti).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("keeps a custom request when the schema arrives", async () => {
|
||||
let resolveDetail: (value: CollectionInfo) => void;
|
||||
backend.vectorGetCollectionDetail.mockReturnValue(
|
||||
new Promise((resolve) => {
|
||||
resolveDetail = resolve;
|
||||
}),
|
||||
);
|
||||
|
||||
app = createApp(VectorBrowser, {
|
||||
connectionId: "milvus-1",
|
||||
database: "default",
|
||||
collection: "documents",
|
||||
databaseType: "milvus",
|
||||
});
|
||||
app.mount(root!);
|
||||
await nextTick();
|
||||
|
||||
buttonWithText("vector.upsert").click();
|
||||
await nextTick();
|
||||
const editor = root!.querySelector<HTMLTextAreaElement>("textarea")!;
|
||||
const customRequest = 'POST /v2/vectordb/entities/upsert\n{"collectionName":"documents","data":[{"id":7,"embedding":[1,2,3]}]}';
|
||||
editor.value = customRequest;
|
||||
editor.dispatchEvent(new Event("input", { bubbles: true }));
|
||||
await nextTick();
|
||||
|
||||
resolveDetail!({
|
||||
name: "documents",
|
||||
id: "documents",
|
||||
milvusSchema: { fields: [field("id", "Int64", { primaryKey: true }), field("embedding", "FloatVector", { dimension: 3 })] },
|
||||
});
|
||||
await flushUi();
|
||||
|
||||
expect(editor.value).toBe(customRequest);
|
||||
});
|
||||
|
||||
it("does not send a generated request when schema discovery fails", async () => {
|
||||
backend.vectorGetCollectionDetail.mockRejectedValue(new Error("schema unavailable"));
|
||||
|
||||
app = createApp(VectorBrowser, {
|
||||
connectionId: "milvus-1",
|
||||
database: "default",
|
||||
collection: "documents",
|
||||
databaseType: "milvus",
|
||||
});
|
||||
app.mount(root!);
|
||||
await flushUi();
|
||||
|
||||
buttonWithText("vector.upsert").click();
|
||||
await nextTick();
|
||||
buttonWithText("vector.apply").click();
|
||||
await flushUi();
|
||||
|
||||
expect(backend.executeMulti).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not execute a generated request after cancellation during schema loading", async () => {
|
||||
let resolveDetail: (value: CollectionInfo) => void;
|
||||
backend.vectorGetCollectionDetail.mockReturnValue(
|
||||
new Promise((resolve) => {
|
||||
resolveDetail = resolve;
|
||||
}),
|
||||
);
|
||||
|
||||
app = createApp(VectorBrowser, {
|
||||
connectionId: "milvus-1",
|
||||
database: "default",
|
||||
collection: "documents",
|
||||
databaseType: "milvus",
|
||||
});
|
||||
app.mount(root!);
|
||||
await nextTick();
|
||||
|
||||
buttonWithText("vector.upsert").click();
|
||||
await nextTick();
|
||||
buttonWithText("vector.apply").click();
|
||||
await nextTick();
|
||||
buttonWithText("cancel").click();
|
||||
await flushUi();
|
||||
|
||||
resolveDetail!({
|
||||
name: "documents",
|
||||
id: "documents",
|
||||
milvusSchema: { fields: [field("id", "Int64", { primaryKey: true }), field("embedding", "FloatVector", { dimension: 3 })] },
|
||||
});
|
||||
await flushUi();
|
||||
|
||||
expect(backend.executeMulti).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not execute a generated request after the browser unmounts", async () => {
|
||||
let resolveDetail: (value: CollectionInfo) => void;
|
||||
backend.vectorGetCollectionDetail.mockReturnValue(
|
||||
new Promise((resolve) => {
|
||||
resolveDetail = resolve;
|
||||
}),
|
||||
);
|
||||
|
||||
app = createApp(VectorBrowser, {
|
||||
connectionId: "milvus-1",
|
||||
database: "default",
|
||||
collection: "documents",
|
||||
databaseType: "milvus",
|
||||
});
|
||||
app.mount(root!);
|
||||
await nextTick();
|
||||
|
||||
buttonWithText("vector.upsert").click();
|
||||
await nextTick();
|
||||
buttonWithText("vector.apply").click();
|
||||
await nextTick();
|
||||
app.unmount();
|
||||
app = null;
|
||||
|
||||
resolveDetail!({
|
||||
name: "documents",
|
||||
id: "documents",
|
||||
milvusSchema: { fields: [field("id", "Int64", { primaryKey: true }), field("embedding", "FloatVector", { dimension: 3 })] },
|
||||
});
|
||||
await flushUi();
|
||||
|
||||
expect(backend.executeMulti).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not execute a generated request after the collection changes", async () => {
|
||||
let resolveDetail: (value: CollectionInfo) => void;
|
||||
const collection = ref("documents");
|
||||
backend.vectorGetCollectionDetail.mockReturnValue(
|
||||
new Promise((resolve) => {
|
||||
resolveDetail = resolve;
|
||||
}),
|
||||
);
|
||||
|
||||
app = createApp(
|
||||
defineComponent({
|
||||
setup: () => () => h(VectorBrowser, { connectionId: "milvus-1", database: "default", collection: collection.value, databaseType: "milvus" }),
|
||||
}),
|
||||
);
|
||||
app.mount(root!);
|
||||
await nextTick();
|
||||
|
||||
buttonWithText("vector.upsert").click();
|
||||
await nextTick();
|
||||
buttonWithText("vector.apply").click();
|
||||
await nextTick();
|
||||
collection.value = "other_documents";
|
||||
await nextTick();
|
||||
|
||||
resolveDetail!({
|
||||
name: "documents",
|
||||
id: "documents",
|
||||
milvusSchema: { fields: [field("id", "Int64", { primaryKey: true }), field("embedding", "FloatVector", { dimension: 3 })] },
|
||||
});
|
||||
await flushUi();
|
||||
|
||||
expect(backend.executeMulti).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps a custom search request when the schema arrives", async () => {
|
||||
let resolveDetail: (value: CollectionInfo) => void;
|
||||
backend.vectorGetCollectionDetail.mockReturnValue(
|
||||
new Promise((resolve) => {
|
||||
resolveDetail = resolve;
|
||||
}),
|
||||
);
|
||||
|
||||
app = createApp(VectorBrowser, {
|
||||
connectionId: "milvus-1",
|
||||
database: "default",
|
||||
collection: "documents",
|
||||
databaseType: "milvus",
|
||||
});
|
||||
app.mount(root!);
|
||||
await nextTick();
|
||||
|
||||
buttonWithText("vector.search").click();
|
||||
await nextTick();
|
||||
const editor = root!.querySelector<HTMLTextAreaElement>("textarea")!;
|
||||
const customRequest = 'POST /v2/vectordb/entities/search\n{"collectionName":"documents","data":[[1,2,3]]}';
|
||||
editor.value = customRequest;
|
||||
editor.dispatchEvent(new Event("input", { bubbles: true }));
|
||||
await nextTick();
|
||||
|
||||
resolveDetail!({
|
||||
name: "documents",
|
||||
id: "documents",
|
||||
milvusSchema: { fields: [field("id", "Int64", { primaryKey: true }), field("embedding", "FloatVector", { dimension: 3 })] },
|
||||
});
|
||||
await flushUi();
|
||||
|
||||
expect(editor.value).toBe(customRequest);
|
||||
});
|
||||
|
||||
it("uses the schema primary key and vector field for delete and search defaults", async () => {
|
||||
backend.vectorGetCollectionDetail.mockResolvedValue({
|
||||
name: "documents",
|
||||
id: "documents",
|
||||
milvusSchema: { fields: [field("document_id", "VarChar", { primaryKey: true }), field("embedding", "FloatVector", { dimension: 3 })] },
|
||||
});
|
||||
|
||||
app = createApp(VectorBrowser, {
|
||||
connectionId: "milvus-1",
|
||||
database: "default",
|
||||
collection: "documents",
|
||||
databaseType: "milvus",
|
||||
});
|
||||
app.mount(root!);
|
||||
await flushUi();
|
||||
|
||||
buttonWithText("vector.delete").click();
|
||||
await nextTick();
|
||||
expect(requestBody().filter).toBe('document_id in ["x"]');
|
||||
|
||||
buttonWithText("vector.search").click();
|
||||
await nextTick();
|
||||
expect(requestBody()).toMatchObject({ annsField: "embedding", data: [[0.1, 0.2, 0.3]] });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user