@dbx-tools/dbx-model-catalog 0.1.9 → 0.3.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 +296 -55
- package/dist/_generated/channels.d.ts +4 -3
- package/dist/_generated/json-schema.d.ts +6527 -175
- package/dist/_generated/schemas.d.ts +154 -0
- package/dist/_generated/workspace-rules.d.ts +133 -0
- package/dist/browser.d.ts +34 -15
- package/dist/browser.js +1152 -9042
- package/dist/catalog-index.d.ts +15 -2
- package/dist/chunk-engbh7tj.js +39 -0
- package/dist/chunk-gzjer0mc.js +1797 -0
- package/dist/chunk-pp7fw379.js +51 -0
- package/dist/chunk-pwssxaq0.js +39 -0
- package/dist/cli.js +41 -9619
- package/dist/effective.d.ts +31 -0
- package/dist/index.d.ts +33 -16
- package/dist/index.js +60 -9630
- package/dist/local-client.d.ts +16 -3
- package/dist/pynect.d.ts +6 -3
- package/dist/search.d.ts +7 -1
- package/dist/serving-endpoints.d.ts +15 -0
- package/dist/sources.d.ts +30 -0
- package/dist/types.d.ts +31 -3
- package/dist/validation.d.ts +15 -1
- package/dist/workspace-cache.d.ts +68 -0
- package/dist/workspace-client.d.ts +49 -0
- package/dist/workspace-connection.d.ts +67 -0
- package/dist/workspace-file-cache.d.ts +11 -0
- package/dist/workspace-identity.d.ts +27 -0
- package/dist/workspace-overlay.d.ts +71 -0
- package/package.json +8 -12
- package/src/_generated/channels.ts +41 -0
- package/src/_generated/index.ts +6 -0
- package/src/_generated/json-schema.ts +5 -0
- package/src/_generated/published.ts +7 -0
- package/src/_generated/schemas.ts +585 -0
- package/src/_generated/workspace-rules.ts +285 -0
- package/src/browser.ts +115 -0
- package/src/catalog-index.ts +121 -0
- package/src/cli.ts +99 -0
- package/src/effective.ts +200 -0
- package/src/filter.ts +47 -0
- package/src/identity.ts +56 -0
- package/src/index.ts +187 -0
- package/src/local-client.ts +127 -0
- package/src/pynect.ts +31 -0
- package/src/search.ts +582 -0
- package/src/serving-endpoints.ts +157 -0
- package/src/sources.ts +107 -0
- package/src/types.ts +49 -0
- package/src/validation.ts +127 -0
- package/src/workspace-cache.ts +234 -0
- package/src/workspace-client.ts +134 -0
- package/src/workspace-connection.ts +160 -0
- package/src/workspace-file-cache.ts +45 -0
- package/src/workspace-identity.ts +77 -0
- package/src/workspace-overlay.ts +200 -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 +0 -553
- 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 -9515
- package/dist/snapshot.js.map +0 -36
package/src/effective.ts
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CatalogModel,
|
|
3
|
+
CatalogSearchIndexDocument,
|
|
4
|
+
CatalogSearchIndexPayload,
|
|
5
|
+
EffectiveEndpoint,
|
|
6
|
+
EffectiveModelCatalog,
|
|
7
|
+
FieldOrigin,
|
|
8
|
+
MatchedPublicIdentity,
|
|
9
|
+
ModelCatalog,
|
|
10
|
+
WorkspaceRefresh,
|
|
11
|
+
} from "./_generated/schemas.js";
|
|
12
|
+
import { WORKSPACE_OVERLAY_RULES } from "./_generated/workspace-rules.js";
|
|
13
|
+
import { CatalogIdentityIndex, catalogRecords } from "./catalog-index.js";
|
|
14
|
+
import { catalogSearchDocument, catalogSearchQuality, type CatalogSearchQuality } from "./search.js";
|
|
15
|
+
import { compareCodePoints, type WorkspaceEndpoint } from "./serving-endpoints.js";
|
|
16
|
+
import { CatalogModelSchema } from "./validation.js";
|
|
17
|
+
|
|
18
|
+
/** Version of the effective-catalog contract; part of every workspace cache key. */
|
|
19
|
+
export const EFFECTIVE_CATALOG_CONTRACT_VERSION: number = WORKSPACE_OVERLAY_RULES.contract_version;
|
|
20
|
+
|
|
21
|
+
const RULES = WORKSPACE_OVERLAY_RULES;
|
|
22
|
+
type Json = Record<string, unknown>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Merge normalized endpoints over `publicCatalog` with live-over-public precedence, following
|
|
26
|
+
* the Python reference (`build_effective_catalog`) and the shared `WORKSPACE_OVERLAY_RULES`:
|
|
27
|
+
*
|
|
28
|
+
* - A repeated endpoint name keeps its first entry.
|
|
29
|
+
* - An endpoint matches the first identity candidate the public index resolves exactly
|
|
30
|
+
* (case-insensitive) or by normalized identity; never fuzzy.
|
|
31
|
+
* - Each overlay field the endpoint supplies replaces the public value (origin `workspace`) and
|
|
32
|
+
* drops `sources[target]`; omitted fields keep the public value and sources (origin `public`).
|
|
33
|
+
* Unmatched endpoints start from schema-required placeholders (origin `placeholder`).
|
|
34
|
+
* - An unmatched endpoint whose task maps to no type gets no record or search document: it is
|
|
35
|
+
* listed in `endpoints` only, with no field origins, because any type would be invented.
|
|
36
|
+
* - The record id is the endpoint name. Public order is kept, an endpoint named like a public id
|
|
37
|
+
* replaces it in place, and other endpoint records follow sorted by name.
|
|
38
|
+
* - Endpoint identities other than the name become aliases only when the public index does not
|
|
39
|
+
* resolve them and no public record or earlier endpoint already claims them.
|
|
40
|
+
*/
|
|
41
|
+
export function buildEffectiveCatalog(
|
|
42
|
+
publicCatalog: ModelCatalog,
|
|
43
|
+
endpoints: Iterable<WorkspaceEndpoint> = [],
|
|
44
|
+
workspace: WorkspaceRefresh = { status: "disabled" },
|
|
45
|
+
): EffectiveModelCatalog {
|
|
46
|
+
const index = new CatalogIdentityIndex(catalogRecords(publicCatalog));
|
|
47
|
+
const unique = new Map<string, WorkspaceEndpoint>();
|
|
48
|
+
for (const endpoint of endpoints) if (!unique.has(endpoint.name)) unique.set(endpoint.name, endpoint);
|
|
49
|
+
const claimed = new Set<string>([
|
|
50
|
+
...Object.entries(publicCatalog.models).flatMap(([id, model]) => [id, ...model.aliases]),
|
|
51
|
+
...unique.keys(),
|
|
52
|
+
].map(value => value.toLowerCase()));
|
|
53
|
+
|
|
54
|
+
const overlays: Record<string, EffectiveEndpoint> = {};
|
|
55
|
+
const endpointModels = new Map<string, CatalogModel>();
|
|
56
|
+
for (const name of [...unique.keys()].sort(compareCodePoints)) {
|
|
57
|
+
const endpoint = unique.get(name)!;
|
|
58
|
+
const match = matchEndpoint(endpoint, index);
|
|
59
|
+
if (!match && endpoint.type == null) {
|
|
60
|
+
overlays[name] = { ...structuredClone(endpoint), fields: {} };
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const matched = match ? publicCatalog.models[match.id] : undefined;
|
|
64
|
+
const { data, fields } = overlay(endpoint, matched);
|
|
65
|
+
const extra: string[] = [];
|
|
66
|
+
for (const candidate of endpoint.identities) {
|
|
67
|
+
const key = candidate.value.toLowerCase();
|
|
68
|
+
if (candidate.value !== name && !claimed.has(key) && !index.resolve(candidate.value)) {
|
|
69
|
+
claimed.add(key);
|
|
70
|
+
extra.push(candidate.value);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
data.aliases = [...(match?.id === name ? (data.aliases as string[]) : []), ...extra];
|
|
74
|
+
endpointModels.set(name, CatalogModelSchema.parse(data));
|
|
75
|
+
overlays[name] = { ...structuredClone(endpoint), ...(match ? { match } : {}), fields };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!endpointModels.size) {
|
|
79
|
+
return { contract_version: EFFECTIVE_CATALOG_CONTRACT_VERSION, catalog: publicCatalog, workspace: refreshState(workspace), endpoints: overlays };
|
|
80
|
+
}
|
|
81
|
+
const models: Record<string, CatalogModel> = {};
|
|
82
|
+
for (const [id, model] of Object.entries(publicCatalog.models)) models[id] = endpointModels.get(id) ?? model;
|
|
83
|
+
for (const name of [...endpointModels.keys()].sort(compareCodePoints)) models[name] ??= endpointModels.get(name)!;
|
|
84
|
+
return {
|
|
85
|
+
contract_version: EFFECTIVE_CATALOG_CONTRACT_VERSION,
|
|
86
|
+
catalog: { schema_version: publicCatalog.schema_version, updated_at: publicCatalog.updated_at, models },
|
|
87
|
+
workspace: refreshState(workspace),
|
|
88
|
+
endpoints: overlays,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** True when the workspace listed `id` as an invocable endpoint. */
|
|
93
|
+
export function isEffectiveModelAccessible(effective: EffectiveModelCatalog, id: string): boolean {
|
|
94
|
+
return Object.hasOwn(effective.endpoints, id);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Search documents for every effective record. Text fields come from the effective record;
|
|
99
|
+
* quality comes from the public search index: an unchanged public record reuses its own document,
|
|
100
|
+
* an endpoint record inherits its matched public record's quality, and an unmatched endpoint uses
|
|
101
|
+
* `runtime_search_quality`.
|
|
102
|
+
*/
|
|
103
|
+
export function buildEffectiveSearchIndex(
|
|
104
|
+
effective: EffectiveModelCatalog,
|
|
105
|
+
publicIndex: CatalogSearchIndexPayload,
|
|
106
|
+
): CatalogSearchIndexPayload {
|
|
107
|
+
const publicDocuments = new Map(publicIndex.documents.map(document => [document.id, document]));
|
|
108
|
+
const runtime = catalogSearchQuality(RULES.runtime_search_quality, publicIndex.weights);
|
|
109
|
+
const documents: CatalogSearchIndexDocument[] = catalogRecords(effective.catalog).map(record => {
|
|
110
|
+
const endpoint = effective.endpoints[record.id];
|
|
111
|
+
const own = endpoint ? undefined : publicDocuments.get(record.id);
|
|
112
|
+
if (own) return own;
|
|
113
|
+
const source = endpoint?.match ? publicDocuments.get(endpoint.match.id) : undefined;
|
|
114
|
+
const quality: CatalogSearchQuality = source ?? runtime;
|
|
115
|
+
return catalogSearchDocument(record, quality);
|
|
116
|
+
});
|
|
117
|
+
return {
|
|
118
|
+
schema_version: publicIndex.schema_version,
|
|
119
|
+
catalog_schema_version: effective.catalog.schema_version,
|
|
120
|
+
updated_at: effective.catalog.updated_at,
|
|
121
|
+
weights: publicIndex.weights,
|
|
122
|
+
documents,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function refreshState(workspace: WorkspaceRefresh): WorkspaceRefresh {
|
|
127
|
+
return { persistent: false, ...workspace };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function matchEndpoint(endpoint: WorkspaceEndpoint, index: CatalogIdentityIndex): MatchedPublicIdentity | undefined {
|
|
131
|
+
for (const candidate of endpoint.identities) {
|
|
132
|
+
const resolved = index.resolve(candidate.value);
|
|
133
|
+
if (resolved) return { id: resolved.record.id, kind: candidate.kind, value: candidate.value };
|
|
134
|
+
}
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function overlay(
|
|
139
|
+
endpoint: WorkspaceEndpoint,
|
|
140
|
+
matched: CatalogModel | undefined,
|
|
141
|
+
): { data: Json; fields: Record<string, FieldOrigin> } {
|
|
142
|
+
const fields: Record<string, FieldOrigin> = {};
|
|
143
|
+
let data: Json;
|
|
144
|
+
if (matched) {
|
|
145
|
+
data = structuredClone(matched) as unknown as Json;
|
|
146
|
+
} else {
|
|
147
|
+
data = {
|
|
148
|
+
name: endpoint.name,
|
|
149
|
+
aliases: [],
|
|
150
|
+
capabilities: {},
|
|
151
|
+
tasks: [],
|
|
152
|
+
use_cases: [],
|
|
153
|
+
availability: {},
|
|
154
|
+
price: {},
|
|
155
|
+
average_price: {},
|
|
156
|
+
sources: {},
|
|
157
|
+
notices: [],
|
|
158
|
+
rankings: [],
|
|
159
|
+
benchmarks: [],
|
|
160
|
+
};
|
|
161
|
+
fields.name = "workspace";
|
|
162
|
+
for (const [target, value] of Object.entries(RULES.placeholders)) {
|
|
163
|
+
setPath(data, target, structuredClone(value));
|
|
164
|
+
fields[target] = "placeholder";
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const sources = data.sources as Json;
|
|
168
|
+
for (const rule of RULES.overlay_fields) {
|
|
169
|
+
if ("unmatched_only" in rule && rule.unmatched_only && matched) continue;
|
|
170
|
+
const value = getPath(endpoint, rule.source);
|
|
171
|
+
if (value != null) {
|
|
172
|
+
setPath(data, rule.target, structuredClone(value));
|
|
173
|
+
delete sources[rule.target];
|
|
174
|
+
fields[rule.target] = "workspace";
|
|
175
|
+
} else if (matched && getPath(data, rule.target) != null) {
|
|
176
|
+
fields[rule.target] = "public";
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
data,
|
|
181
|
+
fields: Object.fromEntries(Object.entries(fields).sort(([left], [right]) => compareCodePoints(left, right))),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function getPath(data: unknown, path: string): unknown {
|
|
186
|
+
let node = data;
|
|
187
|
+
for (const part of path.split(".")) {
|
|
188
|
+
if (typeof node !== "object" || node === null || Array.isArray(node)) return undefined;
|
|
189
|
+
node = (node as Json)[part];
|
|
190
|
+
}
|
|
191
|
+
return node;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function setPath(data: Json, path: string, value: unknown): void {
|
|
195
|
+
const parts = path.split(".");
|
|
196
|
+
const leaf = parts.pop()!;
|
|
197
|
+
let node = data;
|
|
198
|
+
for (const part of parts) node = (node[part] ??= {}) as Json;
|
|
199
|
+
node[leaf] = value;
|
|
200
|
+
}
|
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,56 @@
|
|
|
1
|
+
import type { CatalogModelRecord } from "./_generated/schemas.js";
|
|
2
|
+
import { WORKSPACE_OVERLAY_RULES } from "./_generated/workspace-rules.js";
|
|
3
|
+
|
|
4
|
+
export type ModelIdentityKind = "endpoint" | "model-service" | "provider" | "alias";
|
|
5
|
+
|
|
6
|
+
export interface ModelIdentity {
|
|
7
|
+
readonly kind: ModelIdentityKind;
|
|
8
|
+
readonly provider?: string;
|
|
9
|
+
readonly value: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const providerPrefixes: ReadonlySet<string> = new Set(WORKSPACE_OVERLAY_RULES.provider_prefixes);
|
|
13
|
+
const providerFamilies: readonly string[] = WORKSPACE_OVERLAY_RULES.provider_families;
|
|
14
|
+
|
|
15
|
+
/** Normalize endpoint, system-service, and provider identities for exact joins. */
|
|
16
|
+
export function normalizeModelIdentity(value: string): string {
|
|
17
|
+
return value
|
|
18
|
+
.trim()
|
|
19
|
+
.toLowerCase()
|
|
20
|
+
.replace(/^system\.ai[.-]/, "")
|
|
21
|
+
.replace(/^databricks-/, "")
|
|
22
|
+
.replace(/([a-z])(\d)/g, "$1-$2")
|
|
23
|
+
.replace(/(\d)([a-z])/g, "$1-$2")
|
|
24
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
25
|
+
.replace(/^-+|-+$/g, "");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Return the broad provider family used for serving and routing policy. */
|
|
29
|
+
export function modelProviderFamily(value: string): string | undefined {
|
|
30
|
+
const tokens = normalizeModelIdentity(value).split("-");
|
|
31
|
+
return providerFamilies.find(family => tokens.includes(family));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Return up to three numeric version components from a model identity. */
|
|
35
|
+
export function modelVersion(value: string): readonly number[] {
|
|
36
|
+
const match = /(?:^|[^a-z0-9])(\d+)(?:[.\-_](\d+))?(?:[.\-_](\d+))?(?![a-z0-9])/i.exec(value);
|
|
37
|
+
return match ? match.slice(1).filter(Boolean).map(Number) : [];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Classify every published identity without losing the existing aliases array. */
|
|
41
|
+
export function modelIdentities(record: CatalogModelRecord): readonly ModelIdentity[] {
|
|
42
|
+
const identities = [record.id, ...record.aliases];
|
|
43
|
+
return identities.map((value, index) => {
|
|
44
|
+
if (index === 0 || value.startsWith("databricks-")) return { kind: "endpoint", value };
|
|
45
|
+
if (value.startsWith("system.ai.")) return { kind: "model-service", value };
|
|
46
|
+
const separator = value.indexOf("/");
|
|
47
|
+
if (separator > 0 && providerPrefixes.has(value.slice(0, separator).toLowerCase())) {
|
|
48
|
+
return {
|
|
49
|
+
kind: "provider",
|
|
50
|
+
provider: value.slice(0, separator).toLowerCase(),
|
|
51
|
+
value,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return { kind: "alias", value };
|
|
55
|
+
});
|
|
56
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, resolve } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import type { CatalogSearchIndexPayload } from "./_generated/schemas.js";
|
|
5
|
+
import { type LoadedCatalog, createCatalogClient, workspaceOverlay } from "./local-client.js";
|
|
6
|
+
import {
|
|
7
|
+
type CatalogSourceOptions,
|
|
8
|
+
type ModelCatalogSourceInput,
|
|
9
|
+
isHttpUrl,
|
|
10
|
+
isWebSocketUrl,
|
|
11
|
+
loadBrowserSource,
|
|
12
|
+
loadMemory,
|
|
13
|
+
loadPublished,
|
|
14
|
+
} from "./sources.js";
|
|
15
|
+
import type { ModelCatalogClient } from "./types.js";
|
|
16
|
+
import { parseCatalogSearchIndex, parseModelCatalog } from "./validation.js";
|
|
17
|
+
import { type ModelServingClientOption, connectModelServing } from "./workspace-client.js";
|
|
18
|
+
import { createPersistentWorkspaceCatalogCache } from "./workspace-file-cache.js";
|
|
19
|
+
import type { CachedWorkspaceOverlay, WorkspaceCacheOptions } from "./workspace-overlay.js";
|
|
20
|
+
|
|
21
|
+
/** Options for the Node `createModelCatalogClient`, which also accepts SDK clients. */
|
|
22
|
+
export interface ModelCatalogClientOptions extends CatalogSourceOptions {
|
|
23
|
+
/**
|
|
24
|
+
* Workspace connection: a `@databricks/sdk-experimental` `WorkspaceClient`, a
|
|
25
|
+
* `@databricks/sdk-modelserving` `ModelServingClient`, a `{ host, headers }` connection, or a sync
|
|
26
|
+
* or async function returning one. Validated before any request.
|
|
27
|
+
*/
|
|
28
|
+
modelServingClient?: ModelServingClientOption;
|
|
29
|
+
/** Workspace overlay refresh and cache settings. */
|
|
30
|
+
workspaceCache?: NodeWorkspaceCacheOptions;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Node workspace cache settings. By default principal-scoped overlays persist to a `keyv-file`
|
|
35
|
+
* store under the user cache directory; `persistent: false` keeps them in process memory.
|
|
36
|
+
*/
|
|
37
|
+
export interface NodeWorkspaceCacheOptions extends WorkspaceCacheOptions {
|
|
38
|
+
/** Cache file for the default persistent store. */
|
|
39
|
+
filename?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Create a catalog client from any source: the published catalog by default (or the files named by
|
|
44
|
+
* `DBX_MODEL_CATALOG_PATH`), an `http(s)` catalog URL, a `ws(s)` URL of the Python catalog server
|
|
45
|
+
* (needs the optional `pynect` and `@chanx-js/client` peers), a catalog file path or `file:` URL
|
|
46
|
+
* (`search-index.json` beside it), or an in-memory catalog with `options.searchIndex`. With
|
|
47
|
+
* `modelServingClient` every read serves the workspace-effective catalog, whatever the source.
|
|
48
|
+
*/
|
|
49
|
+
export async function createModelCatalogClient(
|
|
50
|
+
source?: ModelCatalogSourceInput,
|
|
51
|
+
options: ModelCatalogClientOptions = {},
|
|
52
|
+
): Promise<ModelCatalogClient> {
|
|
53
|
+
// Validate the optional workspace connection before any request.
|
|
54
|
+
const connector = options.modelServingClient === undefined
|
|
55
|
+
? undefined
|
|
56
|
+
: connectModelServing(options.modelServingClient, { fetch: options.fetch });
|
|
57
|
+
const loaded = await loadSource(source, options);
|
|
58
|
+
return createCatalogClient(loaded, workspaceOverlay(connector, nodeWorkspaceCache(options.workspaceCache)));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function loadSource(source: ModelCatalogSourceInput | undefined, options: CatalogSourceOptions): Promise<LoadedCatalog> {
|
|
62
|
+
if (source === undefined) {
|
|
63
|
+
const configured = process.env.DBX_MODEL_CATALOG_PATH?.trim();
|
|
64
|
+
return configured ? loadFiles(configured, options.searchIndex) : loadPublished(options);
|
|
65
|
+
}
|
|
66
|
+
if (typeof source !== "string" && !(source instanceof URL)) return Promise.resolve(loadMemory(source, options));
|
|
67
|
+
const location = source.toString();
|
|
68
|
+
if (isHttpUrl(location) || isWebSocketUrl(location)) return loadBrowserSource(location, options);
|
|
69
|
+
return loadFiles(location.startsWith("file:") ? fileURLToPath(location) : location, options.searchIndex);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function loadFiles(
|
|
73
|
+
catalogPath: string,
|
|
74
|
+
searchIndex: CatalogSearchIndexPayload | string | undefined,
|
|
75
|
+
): Promise<LoadedCatalog> {
|
|
76
|
+
const location = resolve(catalogPath);
|
|
77
|
+
const indexPath = typeof searchIndex === "string" ? resolve(searchIndex) : resolve(dirname(location), "search-index.json");
|
|
78
|
+
const [catalog, index] = await Promise.all([
|
|
79
|
+
readFile(location, "utf8").then(value => parseModelCatalog(JSON.parse(value))),
|
|
80
|
+
typeof searchIndex === "object"
|
|
81
|
+
? searchIndex
|
|
82
|
+
: readFile(indexPath, "utf8").then(value => parseCatalogSearchIndex(JSON.parse(value))),
|
|
83
|
+
]);
|
|
84
|
+
return { catalog, searchIndex: index, source: { kind: "file", location } };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function nodeWorkspaceCache(options: NodeWorkspaceCacheOptions = {}): WorkspaceCacheOptions {
|
|
88
|
+
const { filename, ...cache } = options;
|
|
89
|
+
if (cache.cache !== undefined || cache.store !== undefined || cache.persistent === false) return cache;
|
|
90
|
+
return {
|
|
91
|
+
...cache,
|
|
92
|
+
persistent: true,
|
|
93
|
+
cache: createPersistentWorkspaceCatalogCache<CachedWorkspaceOverlay>({
|
|
94
|
+
...(cache.refreshAfterMs === undefined ? {} : { refreshAfterMs: cache.refreshAfterMs }),
|
|
95
|
+
...(cache.staleOnErrorMs === undefined ? {} : { staleOnErrorMs: cache.staleOnErrorMs }),
|
|
96
|
+
...(cache.now === undefined ? {} : { now: cache.now }),
|
|
97
|
+
...(filename === undefined ? {} : { filename }),
|
|
98
|
+
}),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export { createPersistentWorkspaceCatalogCache } from "./workspace-file-cache.js";
|
|
103
|
+
export type {
|
|
104
|
+
ModelServingClientOption,
|
|
105
|
+
ModelServingSdkClientLike,
|
|
106
|
+
WorkspaceClientLike,
|
|
107
|
+
WorkspaceClientRequest,
|
|
108
|
+
} from "./workspace-client.js";
|
|
109
|
+
export type {
|
|
110
|
+
Availability,
|
|
111
|
+
AveragePrice,
|
|
112
|
+
Benchmark,
|
|
113
|
+
BrowserModelServingClientOption,
|
|
114
|
+
CachedWorkspaceOverlay,
|
|
115
|
+
Capabilities,
|
|
116
|
+
CatalogFilters,
|
|
117
|
+
CatalogIdentityMatch,
|
|
118
|
+
CatalogModel,
|
|
119
|
+
CatalogModelRecord,
|
|
120
|
+
CatalogSearchIndexDocument,
|
|
121
|
+
CatalogSearchIndexPayload,
|
|
122
|
+
CatalogSearchMatch,
|
|
123
|
+
CatalogSearchQualityScores,
|
|
124
|
+
EffectiveEndpoint,
|
|
125
|
+
EffectiveModelCatalog,
|
|
126
|
+
FieldOrigin,
|
|
127
|
+
IdentityCandidate,
|
|
128
|
+
Latency,
|
|
129
|
+
Limits,
|
|
130
|
+
MatchedPublicIdentity,
|
|
131
|
+
Modalities,
|
|
132
|
+
ModelCatalog,
|
|
133
|
+
ModelCatalogClient,
|
|
134
|
+
ModelCatalogSource,
|
|
135
|
+
ModelCatalogSourceInput,
|
|
136
|
+
ModelCatalogSourceKind,
|
|
137
|
+
ModelIdentity,
|
|
138
|
+
ModelIdentityKind,
|
|
139
|
+
ModelServingConnection,
|
|
140
|
+
ModelServingConnectionGenerator,
|
|
141
|
+
Notice,
|
|
142
|
+
Price,
|
|
143
|
+
Rankings,
|
|
144
|
+
RateLimits,
|
|
145
|
+
Reasoning,
|
|
146
|
+
Retirement,
|
|
147
|
+
SourceLink,
|
|
148
|
+
Status,
|
|
149
|
+
Type,
|
|
150
|
+
WorkspaceCacheOptions,
|
|
151
|
+
WorkspaceCacheStore,
|
|
152
|
+
WorkspaceCatalogCache,
|
|
153
|
+
WorkspaceEndpointCapabilities,
|
|
154
|
+
WorkspaceEndpointSource,
|
|
155
|
+
WorkspaceModelProfile,
|
|
156
|
+
WorkspaceRefresh,
|
|
157
|
+
WorkspaceRefreshStatus,
|
|
158
|
+
} from "./browser.js";
|
|
159
|
+
export {
|
|
160
|
+
CatalogAveragePriceSchema,
|
|
161
|
+
CatalogCapabilitiesSchema,
|
|
162
|
+
CatalogLatencySchema,
|
|
163
|
+
CatalogModelRecordSchema,
|
|
164
|
+
CatalogModelSchema,
|
|
165
|
+
CatalogPreferenceRankingSchema,
|
|
166
|
+
CatalogPriceSchema,
|
|
167
|
+
CatalogRankingSchema,
|
|
168
|
+
CatalogRateLimitsSchema,
|
|
169
|
+
CatalogScoreRankingSchema,
|
|
170
|
+
CatalogSearchIndexSchema,
|
|
171
|
+
CatalogStatusSchema,
|
|
172
|
+
EFFECTIVE_CATALOG_CONTRACT_VERSION,
|
|
173
|
+
EffectiveModelCatalogSchema,
|
|
174
|
+
ModelCatalogSchema,
|
|
175
|
+
PUBLISHED_CATALOG_DATABASE_URL,
|
|
176
|
+
PUBLISHED_CATALOG_INDEX_URL,
|
|
177
|
+
PUBLISHED_CATALOG_ROOT_URL,
|
|
178
|
+
PUBLISHED_CATALOG_SCHEMA_URL,
|
|
179
|
+
PUBLISHED_CATALOG_SCHEMA_VERSION,
|
|
180
|
+
PUBLISHED_CATALOG_SEARCH_INDEX_URL,
|
|
181
|
+
PUBLISHED_CATALOG_URL,
|
|
182
|
+
WorkspaceRefreshSchema,
|
|
183
|
+
parseCatalogModelRecord,
|
|
184
|
+
parseCatalogSearchIndex,
|
|
185
|
+
parseEffectiveModelCatalog,
|
|
186
|
+
parseModelCatalog,
|
|
187
|
+
} from "./browser.js";
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CatalogFilters,
|
|
3
|
+
CatalogModelRecord,
|
|
4
|
+
CatalogSearchIndexPayload,
|
|
5
|
+
EffectiveModelCatalog,
|
|
6
|
+
ModelCatalog,
|
|
7
|
+
WorkspaceRefresh,
|
|
8
|
+
} from "./_generated/schemas.js";
|
|
9
|
+
import type { ModelCatalogClient, ModelCatalogSource } from "./types.js";
|
|
10
|
+
import { ModelCatalogIndex } from "./catalog-index.js";
|
|
11
|
+
import { buildEffectiveCatalog, buildEffectiveSearchIndex, isEffectiveModelAccessible } from "./effective.js";
|
|
12
|
+
import type { ModelServingConnector } from "./workspace-connection.js";
|
|
13
|
+
import {
|
|
14
|
+
type WorkspaceCacheOptions,
|
|
15
|
+
type WorkspaceOverlayState,
|
|
16
|
+
WorkspaceOverlaySession,
|
|
17
|
+
} from "./workspace-overlay.js";
|
|
18
|
+
|
|
19
|
+
interface CatalogView {
|
|
20
|
+
readonly effective: EffectiveModelCatalog;
|
|
21
|
+
readonly index: ModelCatalogIndex;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Overlay session for a normalized connector, or `undefined` for a public-only client. Internal. */
|
|
25
|
+
export function workspaceOverlay(
|
|
26
|
+
connector: ModelServingConnector | undefined,
|
|
27
|
+
cache?: WorkspaceCacheOptions,
|
|
28
|
+
): WorkspaceOverlaySession | undefined {
|
|
29
|
+
return connector && new WorkspaceOverlaySession(connector, cache);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** A loaded public catalog, its search index, and where they came from. */
|
|
33
|
+
export interface LoadedCatalog {
|
|
34
|
+
readonly catalog: ModelCatalog;
|
|
35
|
+
readonly searchIndex: CatalogSearchIndexPayload;
|
|
36
|
+
readonly source: ModelCatalogSource;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The effective catalog client over a loaded public catalog and an optional overlay session. Every
|
|
41
|
+
* source builds this same client, so workspace enrichment works the same for each. Internal.
|
|
42
|
+
*/
|
|
43
|
+
export function createCatalogClient(
|
|
44
|
+
{ catalog, searchIndex, source }: LoadedCatalog,
|
|
45
|
+
overlay: WorkspaceOverlaySession | undefined,
|
|
46
|
+
): ModelCatalogClient {
|
|
47
|
+
const publicView = catalogView(buildEffectiveCatalog(catalog), searchIndex);
|
|
48
|
+
let view = publicView;
|
|
49
|
+
let state: WorkspaceOverlayState | undefined;
|
|
50
|
+
|
|
51
|
+
const current = async (force = false): Promise<CatalogView> => {
|
|
52
|
+
if (!overlay) return publicView;
|
|
53
|
+
const next = await overlay.load({ force });
|
|
54
|
+
if (next !== state) {
|
|
55
|
+
state = next;
|
|
56
|
+
view = catalogView(buildEffectiveCatalog(catalog, next.endpoints ?? [], next.refresh), searchIndex);
|
|
57
|
+
}
|
|
58
|
+
return view;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const close = async (): Promise<void> => {
|
|
62
|
+
await overlay?.settle();
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
source,
|
|
67
|
+
|
|
68
|
+
async catalog(): Promise<ModelCatalog> {
|
|
69
|
+
return (await current()).effective.catalog;
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
async searchIndex(): Promise<CatalogSearchIndexPayload> {
|
|
73
|
+
return (await current()).index.searchIndex;
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
async list(filters: CatalogFilters = {}): Promise<CatalogModelRecord[]> {
|
|
77
|
+
return (await current()).index.list(filters);
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
async get(identity: string): Promise<CatalogModelRecord | undefined> {
|
|
81
|
+
return (await current()).index.resolve(identity)?.record;
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
async resolve(identity: string) {
|
|
85
|
+
return (await current()).index.resolve(identity);
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
async search(query: string, limit: number | null = 50) {
|
|
89
|
+
return (await current()).index.search(query, limit);
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
async *stream(filters: CatalogFilters = {}): AsyncGenerator<CatalogModelRecord> {
|
|
93
|
+
for (const record of (await current()).index.list(filters)) yield record;
|
|
94
|
+
},
|
|
95
|
+
|
|
96
|
+
async effectiveCatalog(): Promise<EffectiveModelCatalog> {
|
|
97
|
+
return (await current()).effective;
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
async workspaceStatus(): Promise<WorkspaceRefresh> {
|
|
101
|
+
return (await current()).effective.workspace;
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
async isAccessible(id: string): Promise<boolean> {
|
|
105
|
+
return isEffectiveModelAccessible((await current()).effective, id);
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
async refreshWorkspace(): Promise<WorkspaceRefresh> {
|
|
109
|
+
return (await current(true)).effective.workspace;
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
async evictWorkspace(evict: { all?: boolean } = {}): Promise<void> {
|
|
113
|
+
await overlay?.evict(evict);
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
close,
|
|
117
|
+
|
|
118
|
+
[Symbol.asyncDispose]: close,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function catalogView(effective: EffectiveModelCatalog, publicIndex: CatalogSearchIndexPayload): CatalogView {
|
|
123
|
+
const searchIndex = Object.keys(effective.endpoints).length
|
|
124
|
+
? buildEffectiveSearchIndex(effective, publicIndex)
|
|
125
|
+
: publicIndex;
|
|
126
|
+
return { effective, index: new ModelCatalogIndex(effective.catalog, searchIndex) };
|
|
127
|
+
}
|
package/src/pynect.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket catalog transport over the Python server. Internal: `createModelCatalogClient`
|
|
3
|
+
* imports this module dynamically for `ws://` and `wss://` sources, so the optional `pynect`
|
|
4
|
+
* and `@chanx-js/client` peers load only then.
|
|
5
|
+
*/
|
|
6
|
+
import { createPynectClient } from "pynect";
|
|
7
|
+
import { models } from "./_generated/channels.js";
|
|
8
|
+
import type { CatalogSearchIndexPayload, ModelCatalog } from "./_generated/schemas.js";
|
|
9
|
+
import { parseCatalogSearchIndex, parseModelCatalog } from "./validation.js";
|
|
10
|
+
|
|
11
|
+
const READY_TIMEOUT_MS = 10_000;
|
|
12
|
+
|
|
13
|
+
/** Pull the catalog and its search index from the server, then close the connection. */
|
|
14
|
+
export async function downloadWebSocketCatalog(
|
|
15
|
+
baseUrl: string,
|
|
16
|
+
): Promise<{ catalog: ModelCatalog; searchIndex: CatalogSearchIndexPayload }> {
|
|
17
|
+
const connection = createPynectClient({ baseUrl }).connect(models);
|
|
18
|
+
try {
|
|
19
|
+
await connection.ready({ timeout: READY_TIMEOUT_MS });
|
|
20
|
+
const [catalog, searchIndex] = await Promise.all([
|
|
21
|
+
connection.request({ action: "get_catalog", payload: {} }),
|
|
22
|
+
connection.request({ action: "get_search_index", payload: {} }),
|
|
23
|
+
]);
|
|
24
|
+
return {
|
|
25
|
+
catalog: parseModelCatalog(catalog.payload),
|
|
26
|
+
searchIndex: parseCatalogSearchIndex(searchIndex.payload),
|
|
27
|
+
};
|
|
28
|
+
} finally {
|
|
29
|
+
connection.close();
|
|
30
|
+
}
|
|
31
|
+
}
|