@venturekit-pro/ai 0.0.0-dev.20260701100017 → 0.0.1

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 (58) hide show
  1. package/README.md +48 -0
  2. package/dist/chat/anthropic.d.ts.map +1 -1
  3. package/dist/chat/anthropic.js +39 -32
  4. package/dist/chat/anthropic.js.map +1 -1
  5. package/dist/chat/google.d.ts.map +1 -1
  6. package/dist/chat/google.js +32 -25
  7. package/dist/chat/google.js.map +1 -1
  8. package/dist/chat/openai.d.ts.map +1 -1
  9. package/dist/chat/openai.js +34 -27
  10. package/dist/chat/openai.js.map +1 -1
  11. package/dist/cost-ledger/index.d.ts +2 -0
  12. package/dist/cost-ledger/index.d.ts.map +1 -1
  13. package/dist/cost-ledger/index.js +1 -0
  14. package/dist/cost-ledger/index.js.map +1 -1
  15. package/dist/cost-ledger/migrations/vk_ai_cost_0001_init.sql +6 -6
  16. package/dist/cost-ledger/migrations/vk_ai_cost_0002_llm_models.sql +4 -4
  17. package/dist/cost-ledger/query.js +4 -4
  18. package/dist/cost-ledger/record.js +2 -2
  19. package/dist/cost-ledger/record.js.map +1 -1
  20. package/dist/cost-ledger/windows.d.ts +82 -0
  21. package/dist/cost-ledger/windows.d.ts.map +1 -0
  22. package/dist/cost-ledger/windows.js +147 -0
  23. package/dist/cost-ledger/windows.js.map +1 -0
  24. package/dist/image/client.d.ts +32 -0
  25. package/dist/image/client.d.ts.map +1 -0
  26. package/dist/image/client.js +25 -0
  27. package/dist/image/client.js.map +1 -0
  28. package/dist/image/google.d.ts +24 -0
  29. package/dist/image/google.d.ts.map +1 -0
  30. package/dist/image/google.js +60 -0
  31. package/dist/image/google.js.map +1 -0
  32. package/dist/image/index.d.ts +11 -0
  33. package/dist/image/index.d.ts.map +1 -0
  34. package/dist/image/index.js +9 -0
  35. package/dist/image/index.js.map +1 -0
  36. package/dist/image/types.d.ts +67 -0
  37. package/dist/image/types.d.ts.map +1 -0
  38. package/dist/image/types.js +19 -0
  39. package/dist/image/types.js.map +1 -0
  40. package/dist/index.d.ts +6 -2
  41. package/dist/index.d.ts.map +1 -1
  42. package/dist/index.js +5 -1
  43. package/dist/index.js.map +1 -1
  44. package/dist/model-catalog/crud.d.ts +42 -0
  45. package/dist/model-catalog/crud.d.ts.map +1 -0
  46. package/dist/model-catalog/crud.js +162 -0
  47. package/dist/model-catalog/crud.js.map +1 -0
  48. package/dist/model-catalog/index.d.ts +11 -0
  49. package/dist/model-catalog/index.d.ts.map +1 -0
  50. package/dist/model-catalog/index.js +10 -0
  51. package/dist/model-catalog/index.js.map +1 -0
  52. package/dist/model-catalog/types.d.ts +97 -0
  53. package/dist/model-catalog/types.d.ts.map +1 -0
  54. package/dist/model-catalog/types.js +16 -0
  55. package/dist/model-catalog/types.js.map +1 -0
  56. package/package.json +3 -3
  57. package/src/cost-ledger/migrations/vk_ai_cost_0001_init.sql +6 -6
  58. package/src/cost-ledger/migrations/vk_ai_cost_0002_llm_models.sql +4 -4
@@ -0,0 +1,147 @@
1
+ /**
2
+ * Window-based cost rollups over `vk_llm_cost_events`.
3
+ *
4
+ * Generic, app-agnostic aggregation for the "cost dashboard" use-case:
5
+ * pick a trailing window (`7d` / `30d` / `90d` / `ytd`), get KPI totals
6
+ * + a per-model and per-source breakdown, plus the same totals for the
7
+ * immediately-prior window so the page can render a period-over-period
8
+ * delta.
9
+ *
10
+ * Distinct from the `query.ts` rollups (which split by `provider` and
11
+ * return `LlmCostRollup`): this module is keyed on the canonical
12
+ * `cost_micro_usd` (bigint — safer to SUM than the `cost_usd` numeric)
13
+ * and surfaces both micro-USD (client math) and USD (display).
14
+ *
15
+ * App-specific rollups that JOIN the consumer's own tables (e.g. the
16
+ * CMS's per-blog-post rollup over `editorial_runs`) stay in the app —
17
+ * this module only covers the table the package owns.
18
+ */
19
+ // ─── Window math ───────────────────────────────────────────────────
20
+ /**
21
+ * Resolve `(current, previous)` ranges for the given key. `now` is
22
+ * threaded through so callers / tests can pin the clock.
23
+ *
24
+ * The `ytd` prior window is the same span last year, ending at "today"
25
+ * one year ago. Fixed windows (`7d`/`30d`/`90d`) use the immediately
26
+ * preceding span of equal length.
27
+ */
28
+ export function computeWindows(key, now = new Date()) {
29
+ const endedAt = now;
30
+ let startedAt;
31
+ let prevStartedAt;
32
+ let prevEndedAt;
33
+ if (key === 'ytd') {
34
+ startedAt = new Date(now.getFullYear(), 0, 1, 0, 0, 0, 0);
35
+ prevStartedAt = new Date(now.getFullYear() - 1, 0, 1, 0, 0, 0, 0);
36
+ prevEndedAt = new Date(now.getFullYear() - 1, now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds(), 0);
37
+ }
38
+ else {
39
+ const days = key === '7d' ? 7 : key === '30d' ? 30 : key === '90d' ? 90 : 30;
40
+ const ms = days * 24 * 60 * 60 * 1000;
41
+ startedAt = new Date(now.getTime() - ms);
42
+ prevStartedAt = new Date(startedAt.getTime() - ms);
43
+ prevEndedAt = startedAt;
44
+ }
45
+ return {
46
+ current: {
47
+ startedAt: startedAt.toISOString(),
48
+ endedAt: endedAt.toISOString(),
49
+ },
50
+ previous: {
51
+ startedAt: prevStartedAt.toISOString(),
52
+ endedAt: prevEndedAt.toISOString(),
53
+ },
54
+ };
55
+ }
56
+ async function sumTotals(querier, tenantId, window) {
57
+ const rows = await querier(`SELECT
58
+ COUNT(*)::text AS "callCount",
59
+ COALESCE(SUM(cost_micro_usd), 0)::text AS "costMicroUsd",
60
+ COALESCE(SUM(tokens_in), 0)::text AS "tokensIn",
61
+ COALESCE(SUM(tokens_out), 0)::text AS "tokensOut"
62
+ FROM vk_llm_cost_events
63
+ WHERE tenant_id = $1
64
+ AND created_at >= $2
65
+ AND created_at < $3`, [tenantId, window.startedAt, window.endedAt]);
66
+ const row = rows[0];
67
+ const costMicroUsd = Number(row.costMicroUsd ?? 0);
68
+ return {
69
+ calls: Number(row.callCount ?? 0),
70
+ costMicroUsd,
71
+ costUsd: costMicroUsd / 1_000_000,
72
+ tokensIn: Number(row.tokensIn ?? 0),
73
+ tokensOut: Number(row.tokensOut ?? 0),
74
+ };
75
+ }
76
+ async function groupByModel(querier, tenantId, window) {
77
+ const rows = await querier(`SELECT
78
+ provider,
79
+ model,
80
+ COUNT(*)::text AS "callCount",
81
+ COALESCE(SUM(cost_micro_usd), 0)::text AS "costMicroUsd",
82
+ COALESCE(SUM(tokens_in), 0)::text AS "tokensIn",
83
+ COALESCE(SUM(tokens_out), 0)::text AS "tokensOut"
84
+ FROM vk_llm_cost_events
85
+ WHERE tenant_id = $1
86
+ AND created_at >= $2
87
+ AND created_at < $3
88
+ GROUP BY provider, model
89
+ ORDER BY SUM(cost_micro_usd) DESC NULLS LAST, provider, model`, [tenantId, window.startedAt, window.endedAt]);
90
+ return rows.map((r) => {
91
+ const costMicroUsd = Number(r.costMicroUsd ?? 0);
92
+ return {
93
+ provider: r.provider,
94
+ model: r.model,
95
+ calls: Number(r.callCount ?? 0),
96
+ costMicroUsd,
97
+ costUsd: costMicroUsd / 1_000_000,
98
+ tokensIn: Number(r.tokensIn ?? 0),
99
+ tokensOut: Number(r.tokensOut ?? 0),
100
+ };
101
+ });
102
+ }
103
+ async function groupBySource(querier, tenantId, window) {
104
+ const rows = await querier(`SELECT
105
+ source,
106
+ COUNT(*)::text AS "callCount",
107
+ COALESCE(SUM(cost_micro_usd), 0)::text AS "costMicroUsd"
108
+ FROM vk_llm_cost_events
109
+ WHERE tenant_id = $1
110
+ AND created_at >= $2
111
+ AND created_at < $3
112
+ GROUP BY source
113
+ ORDER BY SUM(cost_micro_usd) DESC NULLS LAST, source`, [tenantId, window.startedAt, window.endedAt]);
114
+ return rows.map((r) => {
115
+ const costMicroUsd = Number(r.costMicroUsd ?? 0);
116
+ return {
117
+ source: r.source,
118
+ calls: Number(r.callCount ?? 0),
119
+ costMicroUsd,
120
+ costUsd: costMicroUsd / 1_000_000,
121
+ };
122
+ });
123
+ }
124
+ /**
125
+ * Roll up costs for one tenant over the given window. Issues four
126
+ * parallel queries (totals, prev totals, byModel, bySource) and combines
127
+ * them into a single dashboard DTO. Empty windows return zeros (not
128
+ * NULLs) so callers can do delta math without null-guarding.
129
+ */
130
+ export async function rollupCostWindow(querier, args) {
131
+ const { current, previous } = computeWindows(args.windowKey, args.now ?? new Date());
132
+ const [totals, prevTotals, byModel, bySource] = await Promise.all([
133
+ sumTotals(querier, args.tenantId, current),
134
+ sumTotals(querier, args.tenantId, previous),
135
+ groupByModel(querier, args.tenantId, current),
136
+ groupBySource(querier, args.tenantId, current),
137
+ ]);
138
+ return {
139
+ window: current,
140
+ prevWindow: previous,
141
+ totals,
142
+ prevTotals,
143
+ byModel,
144
+ bySource,
145
+ };
146
+ }
147
+ //# sourceMappingURL=windows.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"windows.js","sourceRoot":"","sources":["../../src/cost-ledger/windows.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AA+CH,sEAAsE;AAEtE;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAC5B,GAAkB,EAClB,MAAY,IAAI,IAAI,EAAE;IAEtB,MAAM,OAAO,GAAG,GAAG,CAAC;IACpB,IAAI,SAAe,CAAC;IACpB,IAAI,aAAmB,CAAC;IACxB,IAAI,WAAiB,CAAC;IAEtB,IAAI,GAAG,KAAK,KAAK,EAAE,CAAC;QAClB,SAAS,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,WAAW,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QAC1D,aAAa,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,WAAW,EAAE,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QAClE,WAAW,GAAG,IAAI,IAAI,CACpB,GAAG,CAAC,WAAW,EAAE,GAAG,CAAC,EACrB,GAAG,CAA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 * Construct one client per (provider, apiKey) via `createImageClient()`
6
+ * and call `.generate()` against it. The factory switches on `provider`
7
+ * once, at construction.
8
+ */
9
+ import type { ImageGenerationRequest, ImageGenerationResult, ImageProvider } from './types.js';
10
+ import { type GoogleImageAdapterConfig } from './google.js';
11
+ /**
12
+ * Minimal interface for image generation. Implementations are
13
+ * per-provider thin wrappers that hold the API key + base URL + timeout
14
+ * and translate to the vendor's REST shape.
15
+ */
16
+ export interface ImageGenerationClient {
17
+ readonly provider: ImageProvider;
18
+ generate(request: ImageGenerationRequest): Promise<ImageGenerationResult>;
19
+ }
20
+ /**
21
+ * Construct-time configuration, discriminated by `provider` so more
22
+ * vendors can be added without widening the others' option bags.
23
+ */
24
+ export type ImageClientConfig = {
25
+ provider: 'google';
26
+ } & GoogleImageAdapterConfig;
27
+ /**
28
+ * Build an image client. Throws synchronously on an unknown provider so
29
+ * the caller fails fast rather than at the first `.generate()` call.
30
+ */
31
+ export declare function createImageClient(config: ImageClientConfig): ImageGenerationClient;
32
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/image/client.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,sBAAsB,EACtB,qBAAqB,EACrB,aAAa,EACd,MAAM,YAAY,CAAC;AACpB,OAAO,EAA4B,KAAK,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAEtF;;;;GAIG;AACH,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,QAAQ,EAAE,aAAa,CAAC;IACjC,QAAQ,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAC;CAC3E;AAED;;;GAGG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,GAAG,wBAAwB,CAAC;AAElF;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,iBAAiB,GAAG,qBAAqB,CAclF"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Image generation client — provider-neutral entry point, mirroring
3
+ * `chat/client.ts`.
4
+ *
5
+ * Construct one client per (provider, apiKey) via `createImageClient()`
6
+ * and call `.generate()` against it. The factory switches on `provider`
7
+ * once, at construction.
8
+ */
9
+ import { generateWithGoogleImagen } from './google.js';
10
+ /**
11
+ * Build an image client. Throws synchronously on an unknown provider so
12
+ * the caller fails fast rather than at the first `.generate()` call.
13
+ */
14
+ export function createImageClient(config) {
15
+ if (config.provider !== 'google') {
16
+ throw new Error(`[ai/image] Unsupported provider: ${String(config.provider)}`);
17
+ }
18
+ const { provider: _ignored, ...adapterConfig } = config;
19
+ void _ignored;
20
+ return {
21
+ provider: 'google',
22
+ generate: (request) => generateWithGoogleImagen(adapterConfig, request),
23
+ };
24
+ }
25
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/image/client.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAOH,OAAO,EAAE,wBAAwB,EAAiC,MAAM,aAAa,CAAC;AAkBtF;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAyB;IACzD,IAAI,MAAM,CAAC,QAAQ,KAAK,QAAQ,EAAE,CAAC;QACjC,MAAM,IAAI,KAAK,CACb,oCAAoC,MAAM,CACvC,MAA+B,CAAC,QAAQ,CAC1C,EAAE,CACJ,CAAC;IACJ,CAAC;IACD,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,aAAa,EAAE,GAAG,MAAM,CAAC;IACxD,KAAK,QAAQ,CAAC;IACd,OAAO;QACL,QAAQ,EAAE,QAAQ;QAClB,QAAQ,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,wBAAwB,CAAC,aAAa,EAAE,OAAO,CAAC;KACxE,CAAC;AACJ,CAAC"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Google Imagen adapter (Generative Language REST API).
3
+ *
4
+ * Talks to the `:generate` endpoint on
5
+ * `generativelanguage.googleapis.com` — the same surface the CMS media
6
+ * pipeline used before this module existed, lifted here verbatim so the
7
+ * swap is behavior-preserving. Imagen returns one image per call, so we
8
+ * loop `count` times and collect the bytes.
9
+ *
10
+ * Auth is via the `?key=` query param (the documented REST form for
11
+ * Generative Language API keys), escaped through `encodeURIComponent`.
12
+ */
13
+ import type { ImageGenerationRequest, ImageGenerationResult } from './types.js';
14
+ export interface GoogleImageAdapterConfig {
15
+ apiKey: string;
16
+ /** Override the API host. Defaults to `https://generativelanguage.googleapis.com`. */
17
+ baseUrl?: string;
18
+ /** API version. Defaults to `v1beta`. */
19
+ apiVersion?: string;
20
+ /** Request timeout in milliseconds. Default 120s — image gen is slow. */
21
+ timeoutMs?: number;
22
+ }
23
+ export declare function generateWithGoogleImagen(config: GoogleImageAdapterConfig, request: ImageGenerationRequest): Promise<ImageGenerationResult>;
24
+ //# sourceMappingURL=google.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"google.d.ts","sourceRoot":"","sources":["../../src/image/google.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAEV,sBAAsB,EACtB,qBAAqB,EACtB,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,wBAAwB;IACvC,MAAM,EAAE,MAAM,CAAC;IACf,sFAAsF;IACtF,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,yCAAyC;IACzC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,yEAAyE;IACzE,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAMD,wBAAsB,wBAAwB,CAC5C,MAAM,EAAE,wBAAwB,EAChC,OAAO,EAAE,sBAAsB,GAC9B,OAAO,CAAC,qBAAqB,CAAC,CAyDhC"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Google Imagen adapter (Generative Language REST API).
3
+ *
4
+ * Talks to the `:generate` endpoint on
5
+ * `generativelanguage.googleapis.com` — the same surface the CMS media
6
+ * pipeline used before this module existed, lifted here verbatim so the
7
+ * swap is behavior-preserving. Imagen returns one image per call, so we
8
+ * loop `count` times and collect the bytes.
9
+ *
10
+ * Auth is via the `?key=` query param (the documented REST form for
11
+ * Generative Language API keys), escaped through `encodeURIComponent`.
12
+ */
13
+ export async function generateWithGoogleImagen(config, request) {
14
+ const startedAt = Date.now();
15
+ const baseUrl = (config.baseUrl ?? 'https://generativelanguage.googleapis.com').replace(/\/+$/, '');
16
+ const apiVersion = config.apiVersion ?? 'v1beta';
17
+ const timeoutMs = request.timeoutMs ?? config.timeoutMs ?? 120_000;
18
+ // At least one image; the caller (e.g. the CMS media route) owns any
19
+ // upper cap appropriate to its quota.
20
+ const count = Math.max(1, request.count ?? 1);
21
+ const aspectRatio = request.aspectRatio ?? '16:9';
22
+ const url = `${baseUrl}/${apiVersion}/models/${encodeURIComponent(request.model)}:generate?key=${encodeURIComponent(config.apiKey)}`;
23
+ const images = [];
24
+ for (let i = 0; i < count; i++) {
25
+ const controller = new AbortController();
26
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
27
+ let response;
28
+ try {
29
+ response = await fetch(url, {
30
+ method: 'POST',
31
+ headers: { 'Content-Type': 'application/json' },
32
+ body: JSON.stringify({ prompt: request.prompt, aspectRatio }),
33
+ signal: controller.signal,
34
+ });
35
+ }
36
+ finally {
37
+ clearTimeout(timer);
38
+ }
39
+ if (!response.ok) {
40
+ const text = (await response.text()).slice(0, 500);
41
+ throw new Error(`[ai/image/google] ${response.status} ${response.statusText}: ${text}`);
42
+ }
43
+ const json = (await response.json());
44
+ const im = json.images?.[0];
45
+ if (!im?.bytesBase64Encoded) {
46
+ throw new Error('[ai/image/google] No image bytes returned');
47
+ }
48
+ images.push({
49
+ bytes: Uint8Array.from(Buffer.from(im.bytesBase64Encoded, 'base64')),
50
+ mimeType: im.mimeType ?? 'image/png',
51
+ });
52
+ }
53
+ return {
54
+ images,
55
+ provider: 'google',
56
+ model: request.model,
57
+ latencyMs: Date.now() - startedAt,
58
+ };
59
+ }
60
+ //# sourceMappingURL=google.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"google.js","sourceRoot":"","sources":["../../src/image/google.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAsBH,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAC5C,MAAgC,EAChC,OAA+B;IAE/B,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAC7B,MAAM,OAAO,GAAG,CAAC,MAAM,CAAC,OAAO,IAAI,2CAA2C,CAAC,CAAC,OAAO,CACrF,MAAM,EACN,EAAE,CACH,CAAC;IACF,MAAM,UAAU,GAAG,MAAM,CAAC,UAAU,IAAI,QAAQ,CAAC;IACjD,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,MAAM,CAAC,SAAS,IAAI,OAAO,CAAC;IACnE,qEAAqE;IACrE,sCAAsC;IACtC,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC;IAC9C,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,MAAM,CAAC;IAElD,MAAM,GAAG,GAAG,GAAG,OAAO,IAAI,UAAU,WAAW,kBAAkB,CAC/D,OAAO,CAAC,KAAK,CACd,iBAAiB,kBAAkB,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;IAEtD,MAAM,MAAM,GAAqB,EAAE,CAAC;IACpC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC;QAC/B,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;QACzC,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,EAAE,SAAS,CAAC,CAAC;QAC9D,IAAI,QAAkB,CAAC;QACvB,IAAI,CAAC;YACH,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AAC,GAAG,EAAE,GAAG,SAAS;KAClC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Image generation — provider-neutral text-to-image with raw byte
3
+ * output. Currently Google Imagen; mirrors `chat/` in shape (no tool
4
+ * use, no editing).
5
+ */
6
+ export type { ImageProvider, ImageAspectRatio, ImageGenerationRequest, ImageGenerationResult, GeneratedImage, } from './types.js';
7
+ export { createImageClient } from './client.js';
8
+ export type { ImageGenerationClient, ImageClientConfig } from './client.js';
9
+ export { generateWithGoogleImagen } from './google.js';
10
+ export type { GoogleImageAdapterConfig } from './google.js';
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/image/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,EACV,aAAa,EACb,gBAAgB,EAChB,sBAAsB,EACtB,qBAAqB,EACrB,cAAc,GACf,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,YAAY,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAG5E,OAAO,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AACvD,YAAY,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Image generation — provider-neutral text-to-image with raw byte
3
+ * output. Currently Google Imagen; mirrors `chat/` in shape (no tool
4
+ * use, no editing).
5
+ */
6
+ export { createImageClient } from './client.js';
7
+ // Adapter exported for callers that want to bypass the factory.
8
+ export { generateWithGoogleImagen } from './google.js';
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/image/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAUH,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGhD,gEAAgE;AAChE,OAAO,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Image generation types.
3
+ *
4
+ * Provider-neutral text-to-image generation, mirroring `chat/` in
5
+ * shape: the request carries the model + prompt + framing options, the
6
+ * result carries the raw image bytes plus a provider / model echo for
7
+ * audit trails.
8
+ *
9
+ * Cost is intentionally NOT stamped here. Image vendors price
10
+ * per-image (not per-token) and the per-image rate isn't carried on the
11
+ * request, so cost attribution is left to the caller's cost ledger —
12
+ * unlike `chat/` where token usage makes an inline stamp natural.
13
+ *
14
+ * Tool use, inpainting / editing, and reference-image conditioning are
15
+ * explicit non-goals for this first cut — it covers the single
16
+ * text→image call the CMS media pipeline needs.
17
+ */
18
+ /**
19
+ * The vendor whose HTTP API an image client is wired to.
20
+ *
21
+ * Adding a value requires a corresponding adapter file under
22
+ * `image/<provider>.ts` and a branch in `image/client.ts`.
23
+ */
24
+ export type ImageProvider = 'google';
25
+ /** Supported output framings. */
26
+ export type ImageAspectRatio = '1:1' | '4:3' | '16:9' | '9:16';
27
+ /**
28
+ * A text-to-image request. Provider-neutral on the wire — the adapter
29
+ * translates these fields into its vendor's request shape.
30
+ */
31
+ export interface ImageGenerationRequest {
32
+ /** Vendor-specific model id (e.g. `'imagen-4.0-ultra-generate-001'`). */
33
+ model: string;
34
+ /**
35
+ * The full prompt sent to the model. Any brand / style suffix is the
36
+ * caller's concern — fold it in before calling.
37
+ */
38
+ prompt: string;
39
+ /** Output framing. Defaults to `'16:9'`. */
40
+ aspectRatio?: ImageAspectRatio;
41
+ /** Number of images to generate. Defaults to `1`. */
42
+ count?: number;
43
+ /**
44
+ * Per-request timeout in milliseconds. Falls back to the client's
45
+ * configured `timeoutMs`.
46
+ */
47
+ timeoutMs?: number;
48
+ }
49
+ /** One generated image. */
50
+ export interface GeneratedImage {
51
+ /** Raw decoded image bytes. */
52
+ bytes: Uint8Array;
53
+ /** MIME type reported by the vendor (e.g. `'image/png'`). */
54
+ mimeType: string;
55
+ }
56
+ /** Result of an image generation call. Provider-neutral. */
57
+ export interface ImageGenerationResult {
58
+ /** The generated images, in request order. Length matches `count`. */
59
+ images: GeneratedImage[];
60
+ /** Provider that served the request. */
61
+ provider: ImageProvider;
62
+ /** Model id echoed back for audit trails. */
63
+ model: string;
64
+ /** Wall-clock latency across all images, in milliseconds. */
65
+ latencyMs: number;
66
+ }
67
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/image/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH;;;;;GAKG;AACH,MAAM,MAAM,aAAa,GAAG,QAAQ,CAAC;AAErC,iCAAiC;AACjC,MAAM,MAAM,gBAAgB,GAAG,KAAK,GAAG,KAAK,GAAG,MAAM,GAAG,MAAM,CAAC;AAE/D;;;GAGG;AACH,MAAM,WAAW,sBAAsB;IACrC,yEAAyE;IACzE,KAAK,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,MAAM,EAAE,MAAM,CAAC;IACf,4CAA4C;IAC5C,WAAW,CAAC,EAAE,gBAAgB,CAAC;IAC/B,qDAAqD;IACrD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,2BAA2B;AAC3B,MAAM,WAAW,cAAc;IAC7B,+BAA+B;IAC/B,KAAK,EAAE,UAAU,CAAC;IAClB,6DAA6D;IAC7D,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,4DAA4D;AAC5D,MAAM,WAAW,qBAAqB;IACpC,sEAAsE;IACtE,MAAM,EAAE,cAAc,EAAE,CAAC;IACzB,wCAAwC;IACxC,QAAQ,EAAE,aAAa,CAAC;IACxB,6CAA6C;IAC7C,KAAK,EAAE,MAAM,CAAC;IACd,6DAA6D;IAC7D,SAAS,EAAE,MAAM,CAAC;CACnB"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Image generation types.
3
+ *
4
+ * Provider-neutral text-to-image generation, mirroring `chat/` in
5
+ * shape: the request carries the model + prompt + framing options, the
6
+ * result carries the raw image bytes plus a provider / model echo for
7
+ * audit trails.
8
+ *
9
+ * Cost is intentionally NOT stamped here. Image vendors price
10
+ * per-image (not per-token) and the per-image rate isn't carried on the
11
+ * request, so cost attribution is left to the caller's cost ledger —
12
+ * unlike `chat/` where token usage makes an inline stamp natural.
13
+ *
14
+ * Tool use, inpainting / editing, and reference-image conditioning are
15
+ * explicit non-goals for this first cut — it covers the single
16
+ * text→image call the CMS media pipeline needs.
17
+ */
18
+ export {};
19
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/image/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG"}
package/dist/index.d.ts CHANGED
@@ -20,12 +20,16 @@ export { buildContextPrompt, tenantFilter, createContextAwareAgent, createContex
20
20
  export type { VentureKitAIContext, PermissionCheck, ContextAwareAgentOptions, ContextAwareRagOptions, } from './context/context.js';
21
21
  export { createChatClient, completeWithAnthropic, completeWithOpenAi, completeWithGoogle, stampCost, round8, } from './chat/index.js';
22
22
  export type { ChatProvider, ChatMessage, ChatModelPricing, ChatCompletionRequest, ChatCompletionResult, ChatTokenUsage, ChatFinishReason, CostStamp, ChatCompletionClient, ChatClientConfig, AnthropicAdapterConfig, OpenAiAdapterConfig, GoogleAdapterConfig, } from './chat/index.js';
23
+ export { createImageClient, generateWithGoogleImagen } from './image/index.js';
24
+ export type { ImageProvider, ImageAspectRatio, ImageGenerationRequest, ImageGenerationResult, GeneratedImage, ImageGenerationClient, ImageClientConfig, GoogleImageAdapterConfig, } from './image/index.js';
23
25
  export { estimateTokens, estimateCost, estimateCostForModel, createModelCatalog, } from './cost/index.js';
24
26
  export type { ModelDescriptor, ModelStatus, ModelCatalog, CostEstimate, CostEstimateInput, } from './cost/index.js';
25
27
  export { renderTemplate, renderPromptTemplate, promptContentHash, } from './prompts/index.js';
26
28
  export type { PromptTemplate, PromptVariable, RenderResult, } from './prompts/index.js';
27
29
  export { retryWithBackoff, isRetryableError, withFallback, } from './fallback/index.js';
28
30
  export type { RetryOptions, FallbackOptions } from './fallback/index.js';
29
- export { recordLlmCost, listLlmCosts, costByTarget as llmCostByTarget, costByCorrelation as llmCostByCorrelation, costByTenant as llmCostByTenant, costByMonth as llmCostByMonth, getAiCostMigrationsDir, } from './cost-ledger/index.js';
30
- export type { LlmCostInput, LlmCostEvent, LlmCostRollup, ListLlmCostsArgs, CostByTargetArgs as LlmCostByTargetArgs, CostByCorrelationArgs as LlmCostByCorrelationArgs, CostByTenantArgs as LlmCostByTenantArgs, CostByMonthArgs as LlmCostByMonthArgs, MonthlyCostBucket as LlmMonthlyCostBucket, } from './cost-ledger/index.js';
31
+ export { recordLlmCost, listLlmCosts, costByTarget as llmCostByTarget, costByCorrelation as llmCostByCorrelation, costByTenant as llmCostByTenant, costByMonth as llmCostByMonth, computeWindows, rollupCostWindow, getAiCostMigrationsDir, } from './cost-ledger/index.js';
32
+ export type { LlmCostInput, LlmCostEvent, LlmCostRollup, ListLlmCostsArgs, CostByTargetArgs as LlmCostByTargetArgs, CostByCorrelationArgs as LlmCostByCorrelationArgs, CostByTenantArgs as LlmCostByTenantArgs, CostByMonthArgs as LlmCostByMonthArgs, MonthlyCostBucket as LlmMonthlyCostBucket, CostWindowKey, CostWindowRange, CostTotals, CostByModelRow, CostBySourceRow, CostRollupResult, RollupCostWindowArgs, } from './cost-ledger/index.js';
33
+ export { listModels, getModelByKey, createModel, patchModel, isUniqueViolation as isLlmModelUniqueViolation, isForeignKeyViolation as isLlmModelForeignKeyViolation, } from './model-catalog/index.js';
34
+ export type { LlmModelStatus, LlmModelRow, CreateLlmModelInput, PatchLlmModelInput, } from './model-catalog/index.js';
31
35
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src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cAAc,EACd,cAAc,EACd,YAAY,GACb,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,GACb,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAGzE,OAAO,EACL,aAAa,EACb,YAAY,EACZ,YAAY,IAAI,eAAe,EAC/B,iBAAiB,IAAI,oBAAoB,EACzC,YAAY,IAAI,eAAe,EAC/B,WAAW,IAAI,cAAc,EAC7B,cAAc,EACd,gBAAgB,EAChB,sBAAsB,GACvB,MAAM,wBAAwB,CAAC;AAChC,YAAY,EACV,YAAY,EACZ,YAAY,EACZ,aAAa,EACb,gBAAgB,EAChB,gBAAgB,IAAI,mBAAmB,EACvC,qBAAqB,IAAI,wBAAwB,EACjD,gBAAgB,IAAI,mBAAmB,EACvC,eAAe,IAAI,kBAAkB,EACrC,iBAAiB,IAAI,oBAAoB,EACzC,aAAa,EACb,eAAe,EACf,UAAU,EACV,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,wBAAwB,CAAC;AAGhC,OAAO,EACL,UAAU,EACV,aAAa,EACb,WAAW,EACX,UAAU,EACV,iBAAiB,IAAI,yBAAyB,EAC9C,qBAAqB,IAAI,6BAA6B,GACvD,MAAM,0BAA0B,CAAC;AAClC,YAAY,EACV,cAAc,EACd,WAAW,EACX,mBAAmB,EACnB,kBAAkB,GACnB,MAAM,0BAA0B,CAAC"}
package/dist/index.js CHANGED
@@ -21,6 +21,8 @@ export { createAgentConfig, DEFAULT_AGENT_CONFIG, createAgent, defineTool, } fro
21
21
  export { buildContextPrompt, tenantFilter, createContextAwareAgent, createContextAwareRag, } from './context/context.js';
22
22
  // Chat completion (single-turn / multi-turn, no tool-use)
23
23
  export { createChatClient, completeWithAnthropic, completeWithOpenAi, completeWithGoogle, stampCost, round8, } from './chat/index.js';
24
+ // Image generation (text-to-image, raw bytes)
25
+ export { createImageClient, generateWithGoogleImagen } from './image/index.js';
24
26
  // Model catalog + cost estimation
25
27
  export { estimateTokens, estimateCost, estimateCostForModel, createModelCatalog, } from './cost/index.js';
26
28
  // Prompt templates
@@ -28,5 +30,7 @@ export { renderTemplate, renderPromptTemplate, promptContentHash, } from './prom
28
30
  // Retry + primary→backup fallback
29
31
  export { retryWithBackoff, isRetryableError, withFallback, } from './fallback/index.js';
30
32
  // LLM cost ledger (llm_cost_events table + record/query helpers)
31
- export { recordLlmCost, listLlmCosts, costByTarget as llmCostByTarget, costByCorrelation as llmCostByCorrelation, costByTenant as llmCostByTenant, costByMonth as llmCostByMonth, getAiCostMigrationsDir, } from './cost-ledger/index.js';
33
+ export { recordLlmCost, listLlmCosts, costByTarget as llmCostByTarget, costByCorrelation as llmCostByCorrelation, costByTenant as llmCostByTenant, costByMonth as llmCostByMonth, computeWindows, rollupCostWindow, getAiCostMigrationsDir, } from './cost-ledger/index.js';
34
+ // LLM model catalog (vk_llm_models table CRUD)
35
+ export { listModels, getModelByKey, createModel, patchModel, isUniqueViolation as isLlmModelUniqueViolation, isForeignKeyViolation as isLlmModelForeignKeyViolation, } from './model-catalog/index.js';
32
36
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,yDAAyD;AACzD,cAAc,kBAAkB,CAAC;AAEjC,aAAa;AACb,OAAO,EACL,qBAAqB,EACrB,wBAAwB,EACxB,cAAc,GACf,MAAM,uBAAuB,CAAC;AAG/B,eAAe;AACf,OAAO,EACL,uBAAuB,EACvB,2BAA2B,EAC3B,iBAAiB,GAClB,MAAM,oBAAoB,CAAC;AAG5B,MAAM;AACN,OAAO,EACL,eAAe,EACf,kBAAkB,EAClB,iBAAiB,EACjB,SAAS,GACV,MAAM,gBAAgB,CAAC;AAGxB,yBAAyB;AACzB,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EACpB,WAAW,EACX,UAAU,GACX,MAAM,mBAAmB,CAAC;AAG3B,8CAA8C;AAC9C,OAAO,EACL,kBAAkB,EAClB,YAAY,EACZ,uBAAuB,EACvB,qBAAqB,GACtB,MAAM,sBAAsB,CAAC;AAQ9B,0DAA0D;AAC1D,OAAO,EACL,gBAAgB,EAChB,qBAAqB,EACrB,kBAAkB,EAClB,kBAAkB,EAClB,SAAS,EACT,MAAM,GACP,MAAM,iBAAiB,CAAC;AAiBzB,kCAAkC;AAClC,OAAO,EACL,cAAc,EACd,YAAY,EACZ,oBAAoB,EACpB,kBAAkB,GACnB,MAAM,iBAAiB,CAAC;AASzB,mBAAmB;AACnB,OAAO,EACL,cAAc,EACd,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,oBAAoB,CAAC;AAO5B,kCAAkC;AAClC,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,GACb,MAAM,qBAAqB,CAAC;AAG7B,iEAAiE;AACjE,OAAO,EACL,aAAa,EACb,YAAY,EACZ,YAAY,IAAI,eAAe,EAC/B,iBAAiB,IAAI,oBAAoB,EACzC,YAAY,IAAI,eAAe,EAC/B,WAAW,IAAI,cAAc,EAC7B,sBAAsB,GACvB,MAAM,wBAAwB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,yDAAyD;AACzD,cAAc,kBAAkB,CAAC;AAEjC,aAAa;AACb,OAAO,EACL,qBAAqB,EACrB,wBAAwB,EACxB,cAAc,GACf,MAAM,uBAAuB,CAAC;AAG/B,eAAe;AACf,OAAO,EACL,uBAAuB,EACvB,2BAA2B,EAC3B,iBAAiB,GAClB,MAAM,oBAAoB,CAAC;AAG5B,MAAM;AACN,OAAO,EACL,eAAe,EACf,kBAAkB,EAClB,iBAAiB,EACjB,SAAS,GACV,MAAM,gBAAgB,CAAC;AAGxB,yBAAyB;AACzB,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EACpB,WAAW,EACX,UAAU,GACX,MAAM,mBAAmB,CAAC;AAG3B,8CAA8C;AAC9C,OAAO,EACL,kBAAkB,EAClB,YAAY,EACZ,uBAAuB,EACvB,qBAAqB,GACtB,MAAM,sBAAsB,CAAC;AAQ9B,0DAA0D;AAC1D,OAAO,EACL,gBAAgB,EAChB,qBAAqB,EACrB,kBAAkB,EAClB,kBAAkB,EAClB,SAAS,EACT,MAAM,GACP,MAAM,iBAAiB,CAAC;AAiBzB,8CAA8C;AAC9C,OAAO,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,MAAM,kBAAkB,CAAC;AAY/E,kCAAkC;AAClC,OAAO,EACL,cAAc,EACd,YAAY,EACZ,oBAAoB,EACpB,kBAAkB,GACnB,MAAM,iBAAiB,CAAC;AASzB,mBAAmB;AACnB,OAAO,EACL,cAAc,EACd,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,oBAAoB,CAAC;AAO5B,kCAAkC;AAClC,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,GACb,MAAM,qBAAqB,CAAC;AAG7B,iEAAiE;AACjE,OAAO,EACL,aAAa,EACb,YAAY,EACZ,YAAY,IAAI,eAAe,EAC/B,iBAAiB,IAAI,oBAAoB,EACzC,YAAY,IAAI,eAAe,EAC/B,WAAW,IAAI,cAAc,EAC7B,cAAc,EACd,gBAAgB,EAChB,sBAAsB,GACvB,MAAM,wBAAwB,CAAC;AAoBhC,+CAA+C;AAC/C,OAAO,EACL,UAAU,EACV,aAAa,EACb,WAAW,EACX,UAAU,EACV,iBAAiB,IAAI,yBAAyB,EAC9C,qBAAqB,IAAI,6BAA6B,GACvD,MAAM,0BAA0B,CAAC"}
@@ -0,0 +1,42 @@
1
+ /**
2
+ * CRUD over `vk_llm_models` — the model catalog.
3
+ *
4
+ * Pure SQL against a caller-supplied `Querier`; no `@venturekit/data`
5
+ * import (the consumer passes their `query` / transaction querier).
6
+ *
7
+ * Lifecycle timestamps are auto-managed on `status` transitions and the
8
+ * DB enforces `llm_models_status_timestamps_chk` so a `deprecated` /
9
+ * `disabled` row always has its matching timestamp. The `key` PK and the
10
+ * `(provider, model)` unique constraint are DB-enforced — callers map
11
+ * `23505` to a 409 via `isUniqueViolation`. The self-referencing
12
+ * `replacement_key` FK trips `23503` when it names a non-existent key.
13
+ */
14
+ import type { CreateLlmModelInput, LlmModelRow, PatchLlmModelInput, Querier } from './types.js';
15
+ /** List every catalog row, ordered by provider then key. */
16
+ export declare function listModels(querier: Querier): Promise<LlmModelRow[]>;
17
+ /** Fetch one catalog row by `key` PK, or `null` when absent. */
18
+ export declare function getModelByKey(querier: Querier, key: string): Promise<LlmModelRow | null>;
19
+ /**
20
+ * Insert a new catalog row. When created already in a `deprecated` /
21
+ * `disabled` state the matching lifecycle timestamp is auto-stamped to
22
+ * `NOW()` so the happy path stays one-shot (and satisfies the DB CHECK).
23
+ */
24
+ export declare function createModel(querier: Querier, input: CreateLlmModelInput): Promise<LlmModelRow>;
25
+ /**
26
+ * Patch a catalog row. Returns `null` when the row doesn't exist.
27
+ *
28
+ * Status transitions auto-stamp / auto-clear the lifecycle timestamps:
29
+ * - `available` — clears both `deprecated_at` + `disabled_at`.
30
+ * - `deprecated` — sets `deprecated_at` (COALESCE preserves the
31
+ * original transition time across no-op repeats), clears `disabled_at`.
32
+ * - `disabled` — sets `disabled_at` (same COALESCE preservation).
33
+ */
34
+ export declare function patchModel(querier: Querier, key: string, input: PatchLlmModelInput): Promise<LlmModelRow | null>;
35
+ /** True when `err` is a Postgres `unique_violation` (23505). */
36
+ export declare function isUniqueViolation(err: unknown): boolean;
37
+ /**
38
+ * True when `err` is a Postgres `foreign_key_violation` (23503) — the
39
+ * catalog's only FK is the self-referencing `replacement_key`.
40
+ */
41
+ export declare function isForeignKeyViolation(err: unknown): boolean;
42
+ //# sourceMappingURL=crud.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crud.d.ts","sourceRoot":"","sources":["../../src/model-catalog/crud.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EACV,mBAAmB,EACnB,WAAW,EACX,kBAAkB,EAClB,OAAO,EACR,MAAM,YAAY,CAAC;AAEpB,4DAA4D;AAC5D,wBAAsB,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAIzE;AAED,gEAAgE;AAChE,wBAAsB,aAAa,CACjC,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAM7B;AAED;;;;GAIG;AACH,wBAAsB,WAAW,CAC/B,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,mBAAmB,GACzB,OAAO,CAAC,WAAW,CAAC,CA+CtB;AAED;;;;;;;;GAQG;AACH,wBAAsB,UAAU,CAC9B,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,kBAAkB,GACxB,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAiD7B;AAID,gEAAgE;AAChE,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAMvD;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAK3D"}