@dbx-tools/dbx-model-catalog 0.2.0 → 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.
Files changed (54) hide show
  1. package/README.md +294 -57
  2. package/dist/_generated/channels.d.ts +4 -3
  3. package/dist/_generated/json-schema.d.ts +6527 -175
  4. package/dist/_generated/schemas.d.ts +154 -0
  5. package/dist/_generated/workspace-rules.d.ts +133 -0
  6. package/dist/browser.d.ts +34 -17
  7. package/dist/browser.js +1192 -149
  8. package/dist/catalog-index.d.ts +15 -2
  9. package/dist/chunk-engbh7tj.js +39 -0
  10. package/dist/chunk-gzjer0mc.js +1797 -0
  11. package/dist/chunk-pp7fw379.js +51 -0
  12. package/dist/chunk-pwssxaq0.js +39 -0
  13. package/dist/cli.js +9 -654
  14. package/dist/effective.d.ts +31 -0
  15. package/dist/index.d.ts +33 -16
  16. package/dist/index.js +60 -660
  17. package/dist/local-client.d.ts +16 -3
  18. package/dist/pynect.d.ts +6 -3
  19. package/dist/search.d.ts +7 -1
  20. package/dist/serving-endpoints.d.ts +15 -0
  21. package/dist/sources.d.ts +30 -0
  22. package/dist/types.d.ts +31 -3
  23. package/dist/validation.d.ts +15 -1
  24. package/dist/workspace-cache.d.ts +68 -0
  25. package/dist/workspace-client.d.ts +49 -0
  26. package/dist/workspace-connection.d.ts +67 -0
  27. package/dist/workspace-file-cache.d.ts +11 -0
  28. package/dist/workspace-identity.d.ts +27 -0
  29. package/dist/workspace-overlay.d.ts +71 -0
  30. package/package.json +5 -6
  31. package/src/_generated/channels.ts +5 -2
  32. package/src/_generated/json-schema.ts +4 -2
  33. package/src/_generated/schemas.ts +177 -0
  34. package/src/_generated/workspace-rules.ts +285 -0
  35. package/src/browser.ts +105 -54
  36. package/src/catalog-index.ts +46 -19
  37. package/src/cli.ts +8 -11
  38. package/src/effective.ts +200 -0
  39. package/src/identity.ts +3 -26
  40. package/src/index.ts +155 -82
  41. package/src/local-client.ts +94 -18
  42. package/src/pynect.ts +28 -65
  43. package/src/search.ts +48 -0
  44. package/src/serving-endpoints.ts +157 -0
  45. package/src/sources.ts +107 -0
  46. package/src/types.ts +32 -2
  47. package/src/validation.ts +33 -0
  48. package/src/workspace-cache.ts +234 -0
  49. package/src/workspace-client.ts +134 -0
  50. package/src/workspace-connection.ts +160 -0
  51. package/src/workspace-file-cache.ts +45 -0
  52. package/src/workspace-identity.ts +77 -0
  53. package/src/workspace-overlay.ts +200 -0
  54. package/dist/pynect.js +0 -557
@@ -0,0 +1,200 @@
1
+ import type { WorkspaceRefresh } from "./_generated/schemas.js";
2
+ import { EFFECTIVE_CATALOG_CONTRACT_VERSION } from "./effective.js";
3
+ import { normalizeServingEndpoints, type WorkspaceEndpoint } from "./serving-endpoints.js";
4
+ import {
5
+ DEFAULT_WORKSPACE_REFRESH_AFTER_MS,
6
+ DEFAULT_WORKSPACE_STALE_ON_ERROR_MS,
7
+ type WorkspaceCacheKey,
8
+ type WorkspaceCacheResult,
9
+ type WorkspaceCacheStore,
10
+ type WorkspaceCatalogCache,
11
+ createWorkspaceCatalogCache,
12
+ workspaceCacheKey,
13
+ } from "./workspace-cache.js";
14
+ import type { ModelServingConnector, ModelServingSnapshot } from "./workspace-connection.js";
15
+ import { formatWorkspaceIdentity } from "./workspace-identity.js";
16
+
17
+ /** Cached overlay payload: the normalized endpoint inventory only, never headers or principals. */
18
+ export interface CachedWorkspaceOverlay {
19
+ readonly endpoints: WorkspaceEndpoint[];
20
+ }
21
+
22
+ /** Workspace overlay refresh and persistence settings. */
23
+ export interface WorkspaceCacheOptions {
24
+ /** Minimum time between refresh attempts. Defaults to five minutes. */
25
+ refreshAfterMs?: number;
26
+ /** Maximum age of an overlay served after a failed refresh. Defaults to seven days. */
27
+ staleOnErrorMs?: number;
28
+ /** Keyv-compatible store for principal-scoped overlays. Defaults to process memory. */
29
+ store?: WorkspaceCacheStore;
30
+ /** Whether `store` keeps entries across processes; reported as `WorkspaceRefresh.persistent`. */
31
+ persistent?: boolean;
32
+ /**
33
+ * A prepared overlay cache, such as `createPersistentWorkspaceCatalogCache()`; replaces the cache
34
+ * built from `store`, `refreshAfterMs`, and `staleOnErrorMs`.
35
+ */
36
+ cache?: WorkspaceCatalogCache<CachedWorkspaceOverlay>;
37
+ now?: () => number;
38
+ }
39
+
40
+ /** The served endpoints (absent when enrichment is unavailable) and their refresh state. */
41
+ export interface WorkspaceOverlayState {
42
+ readonly endpoints?: readonly WorkspaceEndpoint[];
43
+ readonly refresh: WorkspaceRefresh;
44
+ }
45
+
46
+ interface Entry {
47
+ readonly endpoints: WorkspaceEndpoint[];
48
+ readonly writtenAt: number;
49
+ }
50
+
51
+ /**
52
+ * One client's workspace overlay: refresh schedule, cache use, and failure state.
53
+ *
54
+ * A refresh is attempted when none was attempted within the refresh interval, or on
55
+ * `load({ force: true })`. Each attempt takes one connector snapshot (a generator is called once) and
56
+ * uses it for both the principal and Serving Endpoints requests. When the connection exposes no
57
+ * workspace host or principal the overlay stays in session memory. A fresh cache entry
58
+ * written by another client is reused; otherwise the inventory is fetched, coalesced per cache key.
59
+ * A failed refresh serves the newest entry at most `staleOnErrorMs` old as `stale`, or reports the
60
+ * workspace `unavailable` with no endpoints. Failures are reported in the state, never thrown.
61
+ * Internal: catalog clients build it from their `modelServingClient` option.
62
+ */
63
+ export class WorkspaceOverlaySession {
64
+ private readonly cache: WorkspaceCatalogCache<CachedWorkspaceOverlay>;
65
+ private readonly now: () => number;
66
+ private readonly persistent: boolean;
67
+ private readonly refreshAfterMs: number;
68
+ private readonly staleOnErrorMs: number;
69
+ private attemptedAt: number | undefined;
70
+ private entry: Entry | undefined;
71
+ private inflight: Promise<WorkspaceOverlayState> | undefined;
72
+ private key: WorkspaceCacheKey | undefined;
73
+ private state: WorkspaceOverlayState | undefined;
74
+ private workspace: string | undefined;
75
+
76
+ constructor(
77
+ private readonly modelServing: ModelServingConnector,
78
+ options: WorkspaceCacheOptions = {},
79
+ ) {
80
+ this.now = options.now ?? Date.now;
81
+ this.refreshAfterMs = options.refreshAfterMs ?? DEFAULT_WORKSPACE_REFRESH_AFTER_MS;
82
+ this.staleOnErrorMs = options.staleOnErrorMs ?? DEFAULT_WORKSPACE_STALE_ON_ERROR_MS;
83
+ this.persistent = options.persistent ?? false;
84
+ this.cache = options.cache ?? createWorkspaceCatalogCache<CachedWorkspaceOverlay>({
85
+ refreshAfterMs: this.refreshAfterMs,
86
+ staleOnErrorMs: this.staleOnErrorMs,
87
+ now: this.now,
88
+ ...(options.store === undefined ? {} : { store: options.store }),
89
+ });
90
+ }
91
+
92
+ /** Return the current overlay, refreshing it when due (or when `force` is set). */
93
+ load(options: { force?: boolean } = {}): Promise<WorkspaceOverlayState> {
94
+ if (this.inflight) return this.inflight;
95
+ const now = this.now();
96
+ if (
97
+ !options.force && this.state && this.attemptedAt !== undefined
98
+ && now - this.attemptedAt < this.refreshAfterMs
99
+ ) {
100
+ return Promise.resolve(this.state);
101
+ }
102
+ const attempt = this.attempt(Boolean(options.force), now)
103
+ .catch(error => this.failed(error, now, this.workspace));
104
+ this.inflight = attempt.finally(() => {
105
+ this.inflight = undefined;
106
+ });
107
+ return this.inflight;
108
+ }
109
+
110
+ /** Forget this session's overlay and delete its cache entry, or every entry when `all`. */
111
+ async evict(options: { all?: boolean } = {}): Promise<void> {
112
+ await this.inflight;
113
+ if (options.all) await this.cache.clear();
114
+ else if (this.key) await this.cache.evict(this.key);
115
+ this.attemptedAt = this.entry = this.key = this.state = undefined;
116
+ }
117
+
118
+ /** Wait for the in-flight attempt and any background cache refreshes. */
119
+ async settle(): Promise<void> {
120
+ await this.inflight;
121
+ await this.cache.settle();
122
+ }
123
+
124
+ private async attempt(force: boolean, now: number): Promise<WorkspaceOverlayState> {
125
+ this.attemptedAt = now;
126
+ let snapshot: ModelServingSnapshot;
127
+ try {
128
+ snapshot = await this.modelServing.snapshot();
129
+ } catch (error) {
130
+ return this.failed(error, now, this.workspace);
131
+ }
132
+ const scoped = snapshot;
133
+ const workspace = scoped.workspace && formatWorkspaceIdentity(scoped.workspace);
134
+ const principal = await Promise.resolve().then(() => scoped.principal()).catch(() => undefined);
135
+ const key = await workspaceCacheKey({
136
+ workspace: scoped.workspace,
137
+ contractVersion: EFFECTIVE_CATALOG_CONTRACT_VERSION,
138
+ principal,
139
+ });
140
+ if (workspace !== this.workspace || key.id !== this.key?.id) this.entry = undefined;
141
+ this.workspace = workspace;
142
+ this.key = key;
143
+ const loader = async (): Promise<CachedWorkspaceOverlay> => ({
144
+ endpoints: normalizeServingEndpoints(await scoped.listServingEndpoints()),
145
+ });
146
+ try {
147
+ let result = force ? await this.cache.refresh(key, loader) : await this.cache.get(key, loader);
148
+ // An entry past the refresh interval is refreshed now; the cache coalesces the request.
149
+ if (result.status !== "fresh") result = await this.cache.refresh(key, loader);
150
+ return this.succeeded(result, now);
151
+ } catch (error) {
152
+ return this.failed(error, now, workspace);
153
+ }
154
+ }
155
+
156
+ private succeeded(result: WorkspaceCacheResult<CachedWorkspaceOverlay>, now: number): WorkspaceOverlayState {
157
+ this.entry = { endpoints: result.value.endpoints, writtenAt: result.writtenAt };
158
+ const stale = result.status === "stale";
159
+ return this.serve(this.entry, now, {
160
+ status: stale ? "stale" : "current",
161
+ persistent: this.persistent && result.shared,
162
+ ...(stale ? { error: describeError(result.error) } : {}),
163
+ });
164
+ }
165
+
166
+ private failed(error: unknown, now: number, workspace: string | undefined): WorkspaceOverlayState {
167
+ const fallback = this.entry && now - this.entry.writtenAt <= this.staleOnErrorMs ? this.entry : undefined;
168
+ this.entry = fallback;
169
+ this.workspace = workspace;
170
+ return this.serve(fallback, now, {
171
+ status: fallback ? "stale" : "unavailable",
172
+ persistent: Boolean(fallback) && this.persistent && Boolean(this.key?.shared),
173
+ error: describeError(error),
174
+ });
175
+ }
176
+
177
+ private serve(
178
+ entry: Entry | undefined,
179
+ now: number,
180
+ refresh: Pick<WorkspaceRefresh, "status" | "persistent" | "error">,
181
+ ): WorkspaceOverlayState {
182
+ this.state = {
183
+ ...(entry && refresh.status !== "unavailable" ? { endpoints: entry.endpoints } : {}),
184
+ refresh: {
185
+ status: refresh.status,
186
+ ...(this.workspace === undefined ? {} : { workspace: this.workspace }),
187
+ persistent: refresh.persistent ?? false,
188
+ ...(entry ? { refreshed_at: new Date(entry.writtenAt).toISOString() } : {}),
189
+ attempted_at: new Date(now).toISOString(),
190
+ ...(entry ? { age_seconds: Math.max(0, (now - entry.writtenAt) / 1000) } : {}),
191
+ ...(refresh.error ? { error: refresh.error } : {}),
192
+ },
193
+ };
194
+ return this.state;
195
+ }
196
+ }
197
+
198
+ function describeError(error: unknown): string {
199
+ return error instanceof Error ? `${error.name}: ${error.message}` : String(error);
200
+ }