@dbx-tools/dbx-model-catalog 0.1.8 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -6
- package/dist/browser.d.ts +2 -0
- package/dist/browser.js +95 -8991
- package/dist/cli.js +181 -9094
- package/dist/index.d.ts +1 -1
- package/dist/index.js +77 -9027
- package/dist/pynect.js +6 -2
- package/dist/validation.d.ts +30 -1
- package/package.json +4 -7
- package/src/_generated/channels.ts +38 -0
- package/src/_generated/index.ts +6 -0
- package/src/_generated/json-schema.ts +3 -0
- package/src/_generated/published.ts +7 -0
- package/src/_generated/schemas.ts +408 -0
- package/src/browser.ts +64 -0
- package/src/catalog-index.ts +94 -0
- package/src/cli.ts +102 -0
- package/src/filter.ts +47 -0
- package/src/identity.ts +79 -0
- package/src/index.ts +114 -0
- package/src/local-client.ts +51 -0
- package/src/pynect.ts +68 -0
- package/src/search.ts +534 -0
- package/src/types.ts +19 -0
- package/src/validation.ts +94 -0
- package/dist/browser.js.map +0 -38
- package/dist/catalog.json +0 -1
- package/dist/cli.js.map +0 -40
- package/dist/index.js.map +0 -39
- package/dist/pynect.js.map +0 -15
- package/dist/search-index.json +0 -1
- package/dist/snapshot.d.ts +0 -7
- package/dist/snapshot.js +0 -9495
- package/dist/snapshot.js.map +0 -36
package/src/filter.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CatalogFilters,
|
|
3
|
+
CatalogModelRecord,
|
|
4
|
+
CatalogSearchIndexPayload,
|
|
5
|
+
} from "./_generated/schemas.js";
|
|
6
|
+
import { searchCatalogRecords } from "./search.js";
|
|
7
|
+
|
|
8
|
+
export function filterCatalogRecords(
|
|
9
|
+
records: readonly CatalogModelRecord[],
|
|
10
|
+
searchIndex: CatalogSearchIndexPayload,
|
|
11
|
+
filters: CatalogFilters = {},
|
|
12
|
+
): CatalogModelRecord[] {
|
|
13
|
+
const limit = filters.limit === undefined ? 50 : filters.limit;
|
|
14
|
+
if (limit !== null && (!Number.isSafeInteger(limit) || limit < 0 || limit > 1_000)) {
|
|
15
|
+
throw new RangeError("limit must be an integer between 0 and 1000, or null");
|
|
16
|
+
}
|
|
17
|
+
const filtered = records.filter(record =>
|
|
18
|
+
includes(filters.status, record.status)
|
|
19
|
+
&& includes(filters.type, record.type)
|
|
20
|
+
&& includes(filters.publisher, record.publisher)
|
|
21
|
+
&& (filters.task ?? []).every(task => record.tasks.includes(task))
|
|
22
|
+
&& (filters.capability ?? []).every(capability => hasCapability(record, capability)),
|
|
23
|
+
);
|
|
24
|
+
const query = filters.query?.trim();
|
|
25
|
+
if (query) return searchCatalogRecords(searchIndex, filtered, query, limit);
|
|
26
|
+
filtered.sort((left, right) => compareText(left.name, right.name) || compareText(left.id, right.id));
|
|
27
|
+
return limit === null ? filtered : filtered.slice(0, limit);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function includes<T>(expected: readonly T[] | undefined, actual: T | null | undefined): boolean {
|
|
31
|
+
return !expected?.length || (actual !== null && actual !== undefined && expected.includes(actual));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function hasCapability(
|
|
35
|
+
record: CatalogModelRecord,
|
|
36
|
+
capability: NonNullable<CatalogFilters["capability"]>[number],
|
|
37
|
+
): boolean {
|
|
38
|
+
const field = capability.replaceAll("-", "_") as keyof CatalogModelRecord["capabilities"];
|
|
39
|
+
const value = record.capabilities[field];
|
|
40
|
+
return capability === "reasoning" ? value !== null && value !== undefined : value === true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function compareText(left: string, right: string): number {
|
|
44
|
+
const normalizedLeft = left.toLowerCase();
|
|
45
|
+
const normalizedRight = right.toLowerCase();
|
|
46
|
+
return normalizedLeft < normalizedRight ? -1 : normalizedLeft > normalizedRight ? 1 : 0;
|
|
47
|
+
}
|
package/src/identity.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { CatalogModelRecord } from "./_generated/schemas.js";
|
|
2
|
+
|
|
3
|
+
export type ModelIdentityKind = "endpoint" | "model-service" | "provider" | "alias";
|
|
4
|
+
|
|
5
|
+
export interface ModelIdentity {
|
|
6
|
+
readonly kind: ModelIdentityKind;
|
|
7
|
+
readonly provider?: string;
|
|
8
|
+
readonly value: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const providerPrefixes = new Set([
|
|
12
|
+
"alibaba",
|
|
13
|
+
"anthropic",
|
|
14
|
+
"deepseek",
|
|
15
|
+
"google",
|
|
16
|
+
"meta",
|
|
17
|
+
"moonshot",
|
|
18
|
+
"openai",
|
|
19
|
+
"xai",
|
|
20
|
+
"zhipu",
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
const providerFamilies = [
|
|
24
|
+
"claude",
|
|
25
|
+
"deepseek",
|
|
26
|
+
"gemini",
|
|
27
|
+
"gemma",
|
|
28
|
+
"glm",
|
|
29
|
+
"gpt",
|
|
30
|
+
"grok",
|
|
31
|
+
"gte",
|
|
32
|
+
"bge",
|
|
33
|
+
"kimi",
|
|
34
|
+
"llama",
|
|
35
|
+
"qwen",
|
|
36
|
+
] as const;
|
|
37
|
+
|
|
38
|
+
/** Normalize endpoint, system-service, and provider identities for exact joins. */
|
|
39
|
+
export function normalizeModelIdentity(value: string): string {
|
|
40
|
+
return value
|
|
41
|
+
.trim()
|
|
42
|
+
.toLowerCase()
|
|
43
|
+
.replace(/^system\.ai[.-]/, "")
|
|
44
|
+
.replace(/^databricks-/, "")
|
|
45
|
+
.replace(/([a-z])(\d)/g, "$1-$2")
|
|
46
|
+
.replace(/(\d)([a-z])/g, "$1-$2")
|
|
47
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
48
|
+
.replace(/^-+|-+$/g, "");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Return the broad provider family used for serving and routing policy. */
|
|
52
|
+
export function modelProviderFamily(value: string): string | undefined {
|
|
53
|
+
const tokens = normalizeModelIdentity(value).split("-");
|
|
54
|
+
return providerFamilies.find(family => tokens.includes(family));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Return up to three numeric version components from a model identity. */
|
|
58
|
+
export function modelVersion(value: string): readonly number[] {
|
|
59
|
+
const match = /(?:^|[^a-z0-9])(\d+)(?:[.\-_](\d+))?(?:[.\-_](\d+))?(?![a-z0-9])/i.exec(value);
|
|
60
|
+
return match ? match.slice(1).filter(Boolean).map(Number) : [];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Classify every published identity without losing the existing aliases array. */
|
|
64
|
+
export function modelIdentities(record: CatalogModelRecord): readonly ModelIdentity[] {
|
|
65
|
+
const identities = [record.id, ...record.aliases];
|
|
66
|
+
return identities.map((value, index) => {
|
|
67
|
+
if (index === 0 || value.startsWith("databricks-")) return { kind: "endpoint", value };
|
|
68
|
+
if (value.startsWith("system.ai.")) return { kind: "model-service", value };
|
|
69
|
+
const separator = value.indexOf("/");
|
|
70
|
+
if (separator > 0 && providerPrefixes.has(value.slice(0, separator).toLowerCase())) {
|
|
71
|
+
return {
|
|
72
|
+
kind: "provider",
|
|
73
|
+
provider: value.slice(0, separator).toLowerCase(),
|
|
74
|
+
value,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return { kind: "alias", value };
|
|
78
|
+
});
|
|
79
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, resolve } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import type { CatalogClient } from "./types.js";
|
|
5
|
+
import type { CatalogSearchIndexPayload, ModelCatalog } from "./_generated/schemas.js";
|
|
6
|
+
import {
|
|
7
|
+
PUBLISHED_CATALOG_SEARCH_INDEX_URL,
|
|
8
|
+
PUBLISHED_CATALOG_URL,
|
|
9
|
+
} from "./_generated/published.js";
|
|
10
|
+
import { createModelCatalogClient } from "./local-client.js";
|
|
11
|
+
import { parseCatalogSearchIndex, parseModelCatalog } from "./validation.js";
|
|
12
|
+
|
|
13
|
+
const packageRootUrl = new URL("..", import.meta.url);
|
|
14
|
+
const packageRoot = fileURLToPath(packageRootUrl);
|
|
15
|
+
|
|
16
|
+
export interface ConnectModelCatalogOptions {
|
|
17
|
+
catalogPath?: string;
|
|
18
|
+
catalogUrl?: string;
|
|
19
|
+
fetch?: typeof globalThis.fetch;
|
|
20
|
+
searchIndexPath?: string;
|
|
21
|
+
searchIndexUrl?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ModelCatalogSession extends AsyncDisposable {
|
|
25
|
+
readonly client: CatalogClient;
|
|
26
|
+
readonly mode: "file" | "published" | "url";
|
|
27
|
+
readonly root: string;
|
|
28
|
+
readonly source: string;
|
|
29
|
+
close(): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function connectModelCatalog(
|
|
33
|
+
input?: string | ConnectModelCatalogOptions,
|
|
34
|
+
): Promise<ModelCatalogSession> {
|
|
35
|
+
const options = typeof input === "string" ? { catalogUrl: input } : (input ?? {});
|
|
36
|
+
const configuredPath = options.catalogPath ?? process.env.DBX_MODEL_CATALOG_PATH;
|
|
37
|
+
const configuredUrl = options.catalogUrl?.trim() || undefined;
|
|
38
|
+
if (configuredUrl?.startsWith("ws:") || configuredUrl?.startsWith("wss:")) {
|
|
39
|
+
throw new Error("WebSocket connections use @dbx-tools/dbx-model-catalog/pynect");
|
|
40
|
+
}
|
|
41
|
+
const loaded = configuredPath
|
|
42
|
+
? await loadFiles(configuredPath, options.searchIndexPath)
|
|
43
|
+
: await loadUrls(configuredUrl, options.searchIndexUrl, options.fetch);
|
|
44
|
+
const client = createModelCatalogClient(loaded.catalog, loaded.searchIndex);
|
|
45
|
+
const close = async (): Promise<void> => {
|
|
46
|
+
client.close();
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
client,
|
|
51
|
+
mode: configuredPath ? "file" : configuredUrl ? "url" : "published",
|
|
52
|
+
root: packageRoot,
|
|
53
|
+
source: loaded.source,
|
|
54
|
+
close,
|
|
55
|
+
async [Symbol.asyncDispose](): Promise<void> {
|
|
56
|
+
await close();
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function loadFiles(
|
|
62
|
+
catalogPath: string,
|
|
63
|
+
searchIndexPath?: string,
|
|
64
|
+
): Promise<{ catalog: ModelCatalog; searchIndex: CatalogSearchIndexPayload; source: string }> {
|
|
65
|
+
const resolvedCatalogPath = resolve(catalogPath);
|
|
66
|
+
const resolvedSearchIndexPath = resolve(
|
|
67
|
+
searchIndexPath ?? resolve(dirname(resolvedCatalogPath), "search-index.json"),
|
|
68
|
+
);
|
|
69
|
+
const [catalog, searchIndex] = await Promise.all([
|
|
70
|
+
readFile(resolvedCatalogPath, "utf8").then(value => parseModelCatalog(JSON.parse(value))),
|
|
71
|
+
readFile(resolvedSearchIndexPath, "utf8").then(value => parseCatalogSearchIndex(JSON.parse(value))),
|
|
72
|
+
]);
|
|
73
|
+
return { catalog, searchIndex, source: resolvedCatalogPath };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function loadUrls(
|
|
77
|
+
catalogUrl?: string,
|
|
78
|
+
searchIndexUrl?: string,
|
|
79
|
+
fetcher: typeof globalThis.fetch = globalThis.fetch,
|
|
80
|
+
): Promise<{ catalog: ModelCatalog; searchIndex: CatalogSearchIndexPayload; source: string }> {
|
|
81
|
+
const source = catalogUrl ?? PUBLISHED_CATALOG_URL;
|
|
82
|
+
const indexSource = searchIndexUrl ?? (
|
|
83
|
+
catalogUrl ? new URL("search-index.json", catalogUrl).toString() : PUBLISHED_CATALOG_SEARCH_INDEX_URL
|
|
84
|
+
);
|
|
85
|
+
const [catalogResponse, searchIndexResponse] = await Promise.all([
|
|
86
|
+
fetcher(source),
|
|
87
|
+
fetcher(indexSource),
|
|
88
|
+
]);
|
|
89
|
+
if (!catalogResponse.ok) throw new Error(`Catalog request failed with ${catalogResponse.status}`);
|
|
90
|
+
if (!searchIndexResponse.ok) {
|
|
91
|
+
throw new Error(`Search index request failed with ${searchIndexResponse.status}`);
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
catalog: parseModelCatalog(await catalogResponse.json()),
|
|
95
|
+
searchIndex: parseCatalogSearchIndex(await searchIndexResponse.json()),
|
|
96
|
+
source,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export type * from "./browser.js";
|
|
101
|
+
export {
|
|
102
|
+
PUBLISHED_CATALOG_DATABASE_URL,
|
|
103
|
+
PUBLISHED_CATALOG_INDEX_URL,
|
|
104
|
+
PUBLISHED_CATALOG_ROOT_URL,
|
|
105
|
+
PUBLISHED_CATALOG_SCHEMA_URL,
|
|
106
|
+
PUBLISHED_CATALOG_SCHEMA_VERSION,
|
|
107
|
+
PUBLISHED_CATALOG_SEARCH_INDEX_URL,
|
|
108
|
+
PUBLISHED_CATALOG_URL,
|
|
109
|
+
createModelCatalogClient,
|
|
110
|
+
filterCatalogRecords,
|
|
111
|
+
loadModelCatalog,
|
|
112
|
+
loadModelCatalogIndex,
|
|
113
|
+
searchCatalogRecords,
|
|
114
|
+
} from "./browser.js";
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CatalogFilters,
|
|
3
|
+
CatalogModelRecord,
|
|
4
|
+
CatalogSearchIndexPayload,
|
|
5
|
+
ModelCatalog,
|
|
6
|
+
} from "./_generated/schemas.js";
|
|
7
|
+
import type { CatalogClient } from "./types.js";
|
|
8
|
+
import { createModelCatalogIndex } from "./catalog-index.js";
|
|
9
|
+
import { filterCatalogRecords } from "./filter.js";
|
|
10
|
+
|
|
11
|
+
export function createModelCatalogClient(
|
|
12
|
+
catalog: ModelCatalog,
|
|
13
|
+
searchIndex: CatalogSearchIndexPayload,
|
|
14
|
+
): CatalogClient {
|
|
15
|
+
const index = createModelCatalogIndex(catalog, searchIndex);
|
|
16
|
+
const records: CatalogModelRecord[] = [...index.records];
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
async catalog(): Promise<ModelCatalog> {
|
|
20
|
+
return catalog;
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
async searchIndex(): Promise<CatalogSearchIndexPayload> {
|
|
24
|
+
return searchIndex;
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
async list(filters: CatalogFilters = {}): Promise<CatalogModelRecord[]> {
|
|
28
|
+
return filterCatalogRecords(records, searchIndex, filters);
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
async get(identity: string): Promise<CatalogModelRecord | undefined> {
|
|
32
|
+
return index.resolve(identity)?.record;
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
async resolve(identity: string) {
|
|
36
|
+
return index.resolve(identity);
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
async search(query: string, limit: number | null = 50) {
|
|
40
|
+
return index.search(query, limit);
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
async *stream(filters: CatalogFilters = {}): AsyncGenerator<CatalogModelRecord> {
|
|
44
|
+
for (const record of filterCatalogRecords(records, searchIndex, filters)) yield record;
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
close(): void {},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { filterCatalogRecords } from "./filter.js";
|
package/src/pynect.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { ClientOptions } from "@chanx-js/client";
|
|
2
|
+
import { createPynectClient } from "pynect";
|
|
3
|
+
import { models } from "./_generated/channels.js";
|
|
4
|
+
import type {
|
|
5
|
+
CatalogFilters,
|
|
6
|
+
CatalogModelRecord,
|
|
7
|
+
CatalogSearchIndexPayload,
|
|
8
|
+
ModelCatalog,
|
|
9
|
+
} from "./_generated/schemas.js";
|
|
10
|
+
import type { CatalogClient } from "./types.js";
|
|
11
|
+
import { createModelCatalogIndex, type ModelCatalogIndex } from "./catalog-index.js";
|
|
12
|
+
|
|
13
|
+
export function createPynectCatalogClient(options: ClientOptions): CatalogClient {
|
|
14
|
+
const connection = createPynectClient(options).connect(models);
|
|
15
|
+
const requestCatalog = async (): Promise<ModelCatalog> => {
|
|
16
|
+
await connection.ready({ timeout: 10_000 });
|
|
17
|
+
return (await connection.request({ action: "get_catalog", payload: {} })).payload;
|
|
18
|
+
};
|
|
19
|
+
const requestSearchIndex = async (): Promise<CatalogSearchIndexPayload> => {
|
|
20
|
+
await connection.ready({ timeout: 10_000 });
|
|
21
|
+
return (await connection.request({ action: "get_search_index", payload: {} })).payload;
|
|
22
|
+
};
|
|
23
|
+
let index: Promise<ModelCatalogIndex> | undefined;
|
|
24
|
+
const catalogIndex = (): Promise<ModelCatalogIndex> => {
|
|
25
|
+
index ??= Promise.all([
|
|
26
|
+
requestCatalog(),
|
|
27
|
+
requestSearchIndex(),
|
|
28
|
+
]).then(([catalog, searchIndex]) => createModelCatalogIndex(catalog, searchIndex));
|
|
29
|
+
return index;
|
|
30
|
+
};
|
|
31
|
+
return {
|
|
32
|
+
async catalog(): Promise<ModelCatalog> {
|
|
33
|
+
return requestCatalog();
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
async searchIndex(): Promise<CatalogSearchIndexPayload> {
|
|
37
|
+
return requestSearchIndex();
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
async list(filters: CatalogFilters = {}): Promise<CatalogModelRecord[]> {
|
|
41
|
+
await connection.ready({ timeout: 10_000 });
|
|
42
|
+
return (await connection.request({ action: "list_models", payload: filters })).payload;
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
async get(identity: string): Promise<CatalogModelRecord | undefined> {
|
|
46
|
+
return (await catalogIndex()).resolve(identity)?.record;
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
async resolve(identity: string) {
|
|
50
|
+
return (await catalogIndex()).resolve(identity);
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
async search(query: string, limit: number | null = 50) {
|
|
54
|
+
return (await catalogIndex()).search(query, limit);
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
async *stream(filters: CatalogFilters = {}): AsyncGenerator<CatalogModelRecord> {
|
|
58
|
+
await connection.ready({ timeout: 10_000 });
|
|
59
|
+
for await (const response of connection.request({ action: "stream_models", payload: filters })) {
|
|
60
|
+
yield response.payload;
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
close(): void {
|
|
65
|
+
connection.close();
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|