@adminiumjs/llm 0.1.0-rc.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.
- package/LICENSE +661 -0
- package/dist/apply/diff.d.ts +79 -0
- package/dist/apply/diff.d.ts.map +1 -0
- package/dist/apply/diff.js +459 -0
- package/dist/apply/diff.js.map +1 -0
- package/dist/apply/index.d.ts +6 -0
- package/dist/apply/index.d.ts.map +1 -0
- package/dist/apply/index.js +6 -0
- package/dist/apply/index.js.map +1 -0
- package/dist/apply/normalize.d.ts +55 -0
- package/dist/apply/normalize.d.ts.map +1 -0
- package/dist/apply/normalize.js +232 -0
- package/dist/apply/normalize.js.map +1 -0
- package/dist/apply/plan.d.ts +206 -0
- package/dist/apply/plan.d.ts.map +1 -0
- package/dist/apply/plan.js +422 -0
- package/dist/apply/plan.js.map +1 -0
- package/dist/apply/suggestion-id.d.ts +103 -0
- package/dist/apply/suggestion-id.d.ts.map +1 -0
- package/dist/apply/suggestion-id.js +121 -0
- package/dist/apply/suggestion-id.js.map +1 -0
- package/dist/crypto.d.ts +48 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/crypto.js +35 -0
- package/dist/crypto.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/prompt/builder.d.ts +27 -0
- package/dist/prompt/builder.d.ts.map +1 -0
- package/dist/prompt/builder.js +263 -0
- package/dist/prompt/builder.js.map +1 -0
- package/dist/prompt/chunker.d.ts +89 -0
- package/dist/prompt/chunker.d.ts.map +1 -0
- package/dist/prompt/chunker.js +383 -0
- package/dist/prompt/chunker.js.map +1 -0
- package/dist/prompt/index.d.ts +14 -0
- package/dist/prompt/index.d.ts.map +1 -0
- package/dist/prompt/index.js +14 -0
- package/dist/prompt/index.js.map +1 -0
- package/dist/prompt/serializer.d.ts +138 -0
- package/dist/prompt/serializer.d.ts.map +1 -0
- package/dist/prompt/serializer.js +178 -0
- package/dist/prompt/serializer.js.map +1 -0
- package/dist/prompt/templates/v1.d.ts +41 -0
- package/dist/prompt/templates/v1.d.ts.map +1 -0
- package/dist/prompt/templates/v1.js +312 -0
- package/dist/prompt/templates/v1.js.map +1 -0
- package/dist/prompt/token-estimate.d.ts +16 -0
- package/dist/prompt/token-estimate.d.ts.map +1 -0
- package/dist/prompt/token-estimate.js +20 -0
- package/dist/prompt/token-estimate.js.map +1 -0
- package/dist/prompt/types.d.ts +134 -0
- package/dist/prompt/types.d.ts.map +1 -0
- package/dist/prompt/types.js +44 -0
- package/dist/prompt/types.js.map +1 -0
- package/dist/providers/anthropic.d.ts +3 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +65 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/factory.d.ts +3 -0
- package/dist/providers/factory.d.ts.map +1 -0
- package/dist/providers/factory.js +37 -0
- package/dist/providers/factory.js.map +1 -0
- package/dist/providers/http.d.ts +56 -0
- package/dist/providers/http.d.ts.map +1 -0
- package/dist/providers/http.js +148 -0
- package/dist/providers/http.js.map +1 -0
- package/dist/providers/index.d.ts +15 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +15 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/model-catalog.d.ts +21 -0
- package/dist/providers/model-catalog.d.ts.map +1 -0
- package/dist/providers/model-catalog.js +121 -0
- package/dist/providers/model-catalog.js.map +1 -0
- package/dist/providers/ollama.d.ts +3 -0
- package/dist/providers/ollama.d.ts.map +1 -0
- package/dist/providers/ollama.js +55 -0
- package/dist/providers/ollama.js.map +1 -0
- package/dist/providers/openai-compatible.d.ts +3 -0
- package/dist/providers/openai-compatible.d.ts.map +1 -0
- package/dist/providers/openai-compatible.js +50 -0
- package/dist/providers/openai-compatible.js.map +1 -0
- package/dist/providers/openai.d.ts +18 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +68 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/types.d.ts +112 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +55 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/response/errors.d.ts +60 -0
- package/dist/response/errors.d.ts.map +1 -0
- package/dist/response/errors.js +101 -0
- package/dist/response/errors.js.map +1 -0
- package/dist/response/extract.d.ts +38 -0
- package/dist/response/extract.d.ts.map +1 -0
- package/dist/response/extract.js +77 -0
- package/dist/response/extract.js.map +1 -0
- package/dist/response/index.d.ts +8 -0
- package/dist/response/index.d.ts.map +1 -0
- package/dist/response/index.js +8 -0
- package/dist/response/index.js.map +1 -0
- package/dist/response/merge.d.ts +53 -0
- package/dist/response/merge.d.ts.map +1 -0
- package/dist/response/merge.js +348 -0
- package/dist/response/merge.js.map +1 -0
- package/dist/response/referential.d.ts +113 -0
- package/dist/response/referential.d.ts.map +1 -0
- package/dist/response/referential.js +556 -0
- package/dist/response/referential.js.map +1 -0
- package/dist/response/schema.d.ts +506 -0
- package/dist/response/schema.d.ts.map +1 -0
- package/dist/response/schema.js +191 -0
- package/dist/response/schema.js.map +1 -0
- package/dist/response/validate.d.ts +29 -0
- package/dist/response/validate.d.ts.map +1 -0
- package/dist/response/validate.js +167 -0
- package/dist/response/validate.js.map +1 -0
- package/dist/types.d.ts +95 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +49 -0
- package/dist/types.js.map +1 -0
- package/package.json +30 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"suggestion-id.js","sourceRoot":"","sources":["../../src/apply/suggestion-id.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,kFAAkF;AAClF,MAAM,CAAC,MAAM,sBAAsB,GAAG;IACpC,OAAO;IACP,KAAK;IACL,MAAM;IACN,UAAU;IACV,KAAK;IACL,UAAU;IACV,OAAO;IACP,WAAW;IACX,QAAQ;IACR,MAAM;CACE,CAAC;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AK,CAAC,CAAC;IAC7B,CAAC;AACH,CAAC;AAED,yFAAyF;AACzF,MAAM,UAAU,kBAAkB,CAAC,EAAU;IAC3C,MAAM,KAAK,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAC9B,IAAI,KAAK,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IAC5B,MAAM,MAAM,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;IAClC,OAAQ,sBAA4C,CAAC,QAAQ,CAAC,MAAM,CAAC;QACnE,CAAC,CAAE,MAA6B;QAChC,CAAC,CAAC,IAAI,CAAC;AACX,CAAC"}
|
package/dist/crypto.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AES-256-GCM secret-at-rest for the encrypted `llm.apiKey` setting
|
|
3
|
+
* (06-llm-assist.md §3.2; 01-architecture.md §7.1). The LLM key is protected with
|
|
4
|
+
* the SAME mechanism connection strings use: an HKDF-SHA256 key derived from
|
|
5
|
+
* `ADMINIUM_SECRET`, purpose-scoped by salt, producing `enc:v1:` GCM tokens
|
|
6
|
+
* (`apps/server/src/config/secrets.ts` + `apps/server/src/connections/crypto.ts`).
|
|
7
|
+
*
|
|
8
|
+
* ⚠️ BLOCKER (see this track's followUps): those primitives currently live ONLY
|
|
9
|
+
* in the server tree. `@adminium/llm` is consumed by the browser dashboard, so it
|
|
10
|
+
* cannot import server code, and a package→app edge is impossible anyway. Until
|
|
11
|
+
* the AES-256-GCM helper is extracted into a shared, browser-safe leaf, this
|
|
12
|
+
* module ships the crypto CONTRACT and the LLM purpose-scoped wiring, taking the
|
|
13
|
+
* primitives by INJECTION. M6 Wave 2 (server) supplies the real `node:crypto`
|
|
14
|
+
* implementation; unit tests inject a fake. No `node:*` import here keeps the
|
|
15
|
+
* package browser-safe.
|
|
16
|
+
*/
|
|
17
|
+
/** Purpose scope for the LLM API-key encryption key (mirror of `DSN_KEY_SALT`). */
|
|
18
|
+
export declare const LLM_KEY_SALT: "adminium:llm-key:v1";
|
|
19
|
+
/**
|
|
20
|
+
* The AES-256-GCM primitives this module wires together — the exact shape of
|
|
21
|
+
* `apps/server/src/config/secrets.ts`. Injected so the browser-safe `@adminium/llm`
|
|
22
|
+
* never has to import the `node:crypto`-backed implementation directly.
|
|
23
|
+
*/
|
|
24
|
+
export interface SecretCryptoHelpers {
|
|
25
|
+
/** HKDF-SHA256 → 32-byte key, purpose-scoped by `salt` (+ optional `info`). */
|
|
26
|
+
deriveKey(secret: string, salt: string | Uint8Array, info?: string): Uint8Array;
|
|
27
|
+
/** UTF-8 plaintext → `enc:v1:` GCM token with a fresh random IV. */
|
|
28
|
+
encryptSecret(plaintext: string, key: Uint8Array): string;
|
|
29
|
+
/** `enc:v1:` token → UTF-8 plaintext; throws on tamper / wrong key. */
|
|
30
|
+
decryptSecret(token: string, key: Uint8Array): string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Encrypt/decrypt closures scoped to the LLM API-key purpose — the exact shape of
|
|
34
|
+
* `@adminium/meta`'s `DsnCrypto`, so the meta store stays crypto-agnostic and the
|
|
35
|
+
* server can hand the LLM-config repo these closures the same way it does for DSNs.
|
|
36
|
+
*/
|
|
37
|
+
export interface LlmKeyCrypto {
|
|
38
|
+
encrypt(plaintext: string): string;
|
|
39
|
+
decrypt(token: string): string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Builds the LLM-key crypto closures from the master secret and injected
|
|
43
|
+
* primitives (mirror of `dsnCryptoFromSecret`). The `ADMINIUM_SECRET`-derived key
|
|
44
|
+
* is scoped by {@link LLM_KEY_SALT} so LLM-key, DSN, TOTP, and session keys never
|
|
45
|
+
* coincide even for the same master secret.
|
|
46
|
+
*/
|
|
47
|
+
export declare function llmKeyCryptoFromSecret(masterSecret: string, helpers: SecretCryptoHelpers): LlmKeyCrypto;
|
|
48
|
+
//# sourceMappingURL=crypto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,mFAAmF;AACnF,eAAO,MAAM,YAAY,EAAG,qBAA8B,CAAC;AAE3D;;;;GAIG;AACH,MAAM,WAAW,mBAAmB;IAClC,+EAA+E;IAC/E,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,UAAU,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,UAAU,CAAC;IAChF,oEAAoE;IACpE,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,GAAG,MAAM,CAAC;IAC1D,uEAAuE;IACvE,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,GAAG,MAAM,CAAC;CACvD;AAED;;;;GAIG;AACH,MAAM,WAAW,YAAY;IAC3B,OAAO,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAAC;IACnC,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;CAChC;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACpC,YAAY,EAAE,MAAM,EACpB,OAAO,EAAE,mBAAmB,GAC3B,YAAY,CASd"}
|
package/dist/crypto.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AES-256-GCM secret-at-rest for the encrypted `llm.apiKey` setting
|
|
3
|
+
* (06-llm-assist.md §3.2; 01-architecture.md §7.1). The LLM key is protected with
|
|
4
|
+
* the SAME mechanism connection strings use: an HKDF-SHA256 key derived from
|
|
5
|
+
* `ADMINIUM_SECRET`, purpose-scoped by salt, producing `enc:v1:` GCM tokens
|
|
6
|
+
* (`apps/server/src/config/secrets.ts` + `apps/server/src/connections/crypto.ts`).
|
|
7
|
+
*
|
|
8
|
+
* ⚠️ BLOCKER (see this track's followUps): those primitives currently live ONLY
|
|
9
|
+
* in the server tree. `@adminium/llm` is consumed by the browser dashboard, so it
|
|
10
|
+
* cannot import server code, and a package→app edge is impossible anyway. Until
|
|
11
|
+
* the AES-256-GCM helper is extracted into a shared, browser-safe leaf, this
|
|
12
|
+
* module ships the crypto CONTRACT and the LLM purpose-scoped wiring, taking the
|
|
13
|
+
* primitives by INJECTION. M6 Wave 2 (server) supplies the real `node:crypto`
|
|
14
|
+
* implementation; unit tests inject a fake. No `node:*` import here keeps the
|
|
15
|
+
* package browser-safe.
|
|
16
|
+
*/
|
|
17
|
+
/** Purpose scope for the LLM API-key encryption key (mirror of `DSN_KEY_SALT`). */
|
|
18
|
+
export const LLM_KEY_SALT = 'adminium:llm-key:v1';
|
|
19
|
+
/**
|
|
20
|
+
* Builds the LLM-key crypto closures from the master secret and injected
|
|
21
|
+
* primitives (mirror of `dsnCryptoFromSecret`). The `ADMINIUM_SECRET`-derived key
|
|
22
|
+
* is scoped by {@link LLM_KEY_SALT} so LLM-key, DSN, TOTP, and session keys never
|
|
23
|
+
* coincide even for the same master secret.
|
|
24
|
+
*/
|
|
25
|
+
export function llmKeyCryptoFromSecret(masterSecret, helpers) {
|
|
26
|
+
if (masterSecret.length === 0) {
|
|
27
|
+
throw new Error('cannot derive an LLM key-crypto closure from an empty secret');
|
|
28
|
+
}
|
|
29
|
+
const key = helpers.deriveKey(masterSecret, LLM_KEY_SALT);
|
|
30
|
+
return {
|
|
31
|
+
encrypt: (plaintext) => helpers.encryptSecret(plaintext, key),
|
|
32
|
+
decrypt: (token) => helpers.decryptSecret(token, key),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=crypto.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,mFAAmF;AACnF,MAAM,CAAC,MAAM,YAAY,GAAG,qBAA8B,CAAC;AA0B3D;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CACpC,YAAoB,EACpB,OAA4B;IAE5B,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,8DAA8D,CAAC,CAAC;IAClF,CAAC;IACD,MAAM,GAAG,GAAG,OAAO,CAAC,SAAS,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC;IAC1D,OAAO;QACL,OAAO,EAAE,CAAC,SAAS,EAAE,EAAE,CAAC,OAAO,CAAC,aAAa,CAAC,SAAS,EAAE,GAAG,CAAC;QAC7D,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,aAAa,CAAC,KAAK,EAAE,GAAG,CAAC;KACtD,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@adminium/llm` — headless LLM-assist: provider clients, prompt builder,
|
|
3
|
+
* response contract + validation, EnrichmentSet normalization/diff, and the
|
|
4
|
+
* BYO round-trip. Browser-safe (pure Zod / pure TS + global `fetch`); consumed
|
|
5
|
+
* by both `@adminium/server` and `@adminium/dashboard`. See 06-llm-assist.md.
|
|
6
|
+
*
|
|
7
|
+
* This is the single top-level barrel: it re-exports every subdir barrel. The
|
|
8
|
+
* tracks own their own subdir barrels; this file stitches them together.
|
|
9
|
+
*/
|
|
10
|
+
export declare const PACKAGE_NAME = "@adminium/llm";
|
|
11
|
+
export * from './types.js';
|
|
12
|
+
export * from './crypto.js';
|
|
13
|
+
export * from './response/index.js';
|
|
14
|
+
export * from './prompt/index.js';
|
|
15
|
+
export * from './providers/index.js';
|
|
16
|
+
export * from './apply/index.js';
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,eAAO,MAAM,YAAY,kBAAkB,CAAC;AAG5C,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAG5B,cAAc,qBAAqB,CAAC;AAGpC,cAAc,mBAAmB,CAAC;AAGlC,cAAc,sBAAsB,CAAC;AAGrC,cAAc,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@adminium/llm` — headless LLM-assist: provider clients, prompt builder,
|
|
3
|
+
* response contract + validation, EnrichmentSet normalization/diff, and the
|
|
4
|
+
* BYO round-trip. Browser-safe (pure Zod / pure TS + global `fetch`); consumed
|
|
5
|
+
* by both `@adminium/server` and `@adminium/dashboard`. See 06-llm-assist.md.
|
|
6
|
+
*
|
|
7
|
+
* This is the single top-level barrel: it re-exports every subdir barrel. The
|
|
8
|
+
* tracks own their own subdir barrels; this file stitches them together.
|
|
9
|
+
*/
|
|
10
|
+
export const PACKAGE_NAME = '@adminium/llm';
|
|
11
|
+
// Shared contract types (§2, §7.1) + the encrypted-key crypto contract (§3.2).
|
|
12
|
+
export * from './types.js';
|
|
13
|
+
export * from './crypto.js';
|
|
14
|
+
// Response contract + validation pipeline (§6, §7).
|
|
15
|
+
export * from './response/index.js';
|
|
16
|
+
// Prompt builder + chunker (§4, §5).
|
|
17
|
+
export * from './prompt/index.js';
|
|
18
|
+
// Direct-API provider clients (§3).
|
|
19
|
+
export * from './providers/index.js';
|
|
20
|
+
// Apply: EnrichmentSet normalization + field-by-field diff (§8).
|
|
21
|
+
export * from './apply/index.js';
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,eAAe,CAAC;AAE5C,+EAA+E;AAC/E,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAE5B,oDAAoD;AACpD,cAAc,qBAAqB,CAAC;AAEpC,qCAAqC;AACrC,cAAc,mBAAmB,CAAC;AAElC,oCAAoC;AACpC,cAAc,sBAAsB,CAAC;AAErC,iEAAiE;AACjE,cAAc,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type BuildPromptOptions, type PromptArtifact, type PromptChunkInfo, type PromptInput, type RequestedSection } from './types.js';
|
|
2
|
+
/** BYO flattening dividers (§1 invariant 1, §5). */
|
|
3
|
+
export declare const BYO_SYSTEM_MARKER = "=== SYSTEM ===";
|
|
4
|
+
export declare const BYO_USER_MARKER = "=== USER ===";
|
|
5
|
+
/**
|
|
6
|
+
* Flatten a system/user pair into the single copyable BYO document. The bytes
|
|
7
|
+
* between the markers equal `system` and `user` exactly, so a response produced
|
|
8
|
+
* from this document is indistinguishable from a direct-path response.
|
|
9
|
+
*/
|
|
10
|
+
export declare function flattenByo(system: string, user: string): string;
|
|
11
|
+
/** Build the enrichment prompt for one (possibly chunked) input. */
|
|
12
|
+
export declare function buildPrompt(input: PromptInput, options: BuildPromptOptions): PromptArtifact;
|
|
13
|
+
/** Dedupe + canonically order the requested sections; empty ⇒ all (§4.4). */
|
|
14
|
+
export declare function normalizeSections(requested: readonly RequestedSection[]): readonly RequestedSection[];
|
|
15
|
+
/**
|
|
16
|
+
* Apply the section toggles to the user template (§4.4): remove BOTH the numbered
|
|
17
|
+
* decision blocks AND the embedded response-schema keys of deselected sections
|
|
18
|
+
* (per the §5 builder note "the builder deletes the numbered instruction blocks
|
|
19
|
+
* (and schema keys) of deselected sections"). Kept decision blocks retain their
|
|
20
|
+
* original numbers (gaps are fine) so the template's "for decision 6" / "Skip
|
|
21
|
+
* decisions 2 and 7" references stay valid. With all sections active the template
|
|
22
|
+
* is returned unchanged (verbatim — acceptance criterion 2).
|
|
23
|
+
*/
|
|
24
|
+
export declare function applySectionToggles(template: string, sections: readonly RequestedSection[]): string;
|
|
25
|
+
/** The `{{CHUNK_INFO}}` sentence for a chunked run; empty otherwise (§4.5, §5.2). */
|
|
26
|
+
export declare function renderChunkInfo(chunk: PromptChunkInfo | undefined): string;
|
|
27
|
+
//# sourceMappingURL=builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../../src/prompt/builder.ts"],"names":[],"mappings":"AA2BA,OAAO,EAKL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EAEnB,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACtB,MAAM,YAAY,CAAC;AAEpB,oDAAoD;AACpD,eAAO,MAAM,iBAAiB,mBAAmB,CAAC;AAClD,eAAO,MAAM,eAAe,iBAAiB,CAAC;AAE9C;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAE/D;AAED,oEAAoE;AACpE,wBAAgB,WAAW,CAAC,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,kBAAkB,GAAG,cAAc,CAuB3F;AAID,6EAA6E;AAC7E,wBAAgB,iBAAiB,CAC/B,SAAS,EAAE,SAAS,gBAAgB,EAAE,GACrC,SAAS,gBAAgB,EAAE,CAI7B;AAUD;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,SAAS,gBAAgB,EAAE,GACpC,MAAM,CAkBR;AA4KD,qFAAqF;AACrF,wBAAgB,eAAe,CAAC,KAAK,EAAE,eAAe,GAAG,SAAS,GAAG,MAAM,CAQ1E"}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `buildPrompt(input, opts)` — the ONLY producer of prompt text (06-llm-assist.md
|
|
3
|
+
* §1 invariant 1, §4, §5).
|
|
4
|
+
*
|
|
5
|
+
* It renders the verbatim {@link PROMPT_V1} templates into a
|
|
6
|
+
* {@link PromptArtifact}: the direct path sends `system`/`user` as separate
|
|
7
|
+
* messages; the BYO path copies `byo`, the two flattened between
|
|
8
|
+
* `=== SYSTEM ===` / `=== USER ===` dividers. The content between the dividers is
|
|
9
|
+
* byte-identical to `system` + `user` (acceptance criterion 1).
|
|
10
|
+
*
|
|
11
|
+
* Responsibilities:
|
|
12
|
+
* - section toggles (§4.4): drop deselected numbered decision blocks, keeping
|
|
13
|
+
* the original numbering so the template's "decision N" cross-references stay
|
|
14
|
+
* valid;
|
|
15
|
+
* - locale injection (§5.2): the requested OUTPUT locales are injected as data;
|
|
16
|
+
* the prompt TEXT itself is always English (Open decision 3);
|
|
17
|
+
* - allow-list bounds (§5 builder notes): the injected page-template / widget
|
|
18
|
+
* vocabularies come from `opts.allowed`;
|
|
19
|
+
* - sample-free serialization by default (§4.2), delegated to `serializer.ts`.
|
|
20
|
+
*
|
|
21
|
+
* Determinism: pure string assembly, no `Date.now`/`Math.random`; string-literal
|
|
22
|
+
* token replacement uses function replacers so a `$` in injected JSON is never
|
|
23
|
+
* interpreted as a replacement pattern.
|
|
24
|
+
*/
|
|
25
|
+
import { serializeSampling, serializeSchemaIr, serializeStats, toPromptJson } from './serializer.js';
|
|
26
|
+
import { PROMPT_V1_SYSTEM, PROMPT_V1_USER } from './templates/v1.js';
|
|
27
|
+
import { estimateTokens } from './token-estimate.js';
|
|
28
|
+
import { ALL_SECTIONS, DEFAULT_TOKEN_BUDGET, REQUESTED_SECTIONS, SECTION_DECISION_NUMBER, } from './types.js';
|
|
29
|
+
/** BYO flattening dividers (§1 invariant 1, §5). */
|
|
30
|
+
export const BYO_SYSTEM_MARKER = '=== SYSTEM ===';
|
|
31
|
+
export const BYO_USER_MARKER = '=== USER ===';
|
|
32
|
+
/**
|
|
33
|
+
* Flatten a system/user pair into the single copyable BYO document. The bytes
|
|
34
|
+
* between the markers equal `system` and `user` exactly, so a response produced
|
|
35
|
+
* from this document is indistinguishable from a direct-path response.
|
|
36
|
+
*/
|
|
37
|
+
export function flattenByo(system, user) {
|
|
38
|
+
return `${BYO_SYSTEM_MARKER}\n${system}\n\n${BYO_USER_MARKER}\n${user}`;
|
|
39
|
+
}
|
|
40
|
+
/** Build the enrichment prompt for one (possibly chunked) input. */
|
|
41
|
+
export function buildPrompt(input, options) {
|
|
42
|
+
requireEnUs(input.locales);
|
|
43
|
+
const sections = normalizeSections(input.sections);
|
|
44
|
+
const tokenBudget = options.tokenBudget ?? DEFAULT_TOKEN_BUDGET;
|
|
45
|
+
const system = PROMPT_V1_SYSTEM;
|
|
46
|
+
const user = renderUserSection(input, options, sections);
|
|
47
|
+
const byo = flattenByo(system, user);
|
|
48
|
+
const tokenEstimate = estimateTokens(byo);
|
|
49
|
+
const overBudget = estimateTokens(user) > tokenBudget;
|
|
50
|
+
const chunk = {
|
|
51
|
+
index: input.chunk?.index ?? 1,
|
|
52
|
+
total: input.chunk?.total ?? 1,
|
|
53
|
+
system,
|
|
54
|
+
user,
|
|
55
|
+
byo,
|
|
56
|
+
tokenEstimate,
|
|
57
|
+
};
|
|
58
|
+
return { system, user, byo, chunks: [chunk], tokenEstimate, overBudget, sections };
|
|
59
|
+
}
|
|
60
|
+
// ─── Section normalization + block removal (§4.4) ────────────────────────────
|
|
61
|
+
/** Dedupe + canonically order the requested sections; empty ⇒ all (§4.4). */
|
|
62
|
+
export function normalizeSections(requested) {
|
|
63
|
+
const set = new Set(requested);
|
|
64
|
+
const ordered = REQUESTED_SECTIONS.filter((section) => set.has(section));
|
|
65
|
+
return ordered.length > 0 ? ordered : ALL_SECTIONS;
|
|
66
|
+
}
|
|
67
|
+
const DECISIONS_START = '{{REQUESTED_SECTIONS_LIST}}\n\n';
|
|
68
|
+
const DECISIONS_END = '\n\n=== TRIGGER TAXONOMY (for decision 6) ===';
|
|
69
|
+
/**
|
|
70
|
+
* Apply the section toggles to the user template (§4.4): remove BOTH the numbered
|
|
71
|
+
* decision blocks AND the embedded response-schema keys of deselected sections
|
|
72
|
+
* (per the §5 builder note "the builder deletes the numbered instruction blocks
|
|
73
|
+
* (and schema keys) of deselected sections"). Kept decision blocks retain their
|
|
74
|
+
* original numbers (gaps are fine) so the template's "for decision 6" / "Skip
|
|
75
|
+
* decisions 2 and 7" references stay valid. With all sections active the template
|
|
76
|
+
* is returned unchanged (verbatim — acceptance criterion 2).
|
|
77
|
+
*/
|
|
78
|
+
export function applySectionToggles(template, sections) {
|
|
79
|
+
if (sections.length === REQUESTED_SECTIONS.length)
|
|
80
|
+
return template;
|
|
81
|
+
const startIndex = template.indexOf(DECISIONS_START);
|
|
82
|
+
const endIndex = template.indexOf(DECISIONS_END);
|
|
83
|
+
if (startIndex < 0 || endIndex < 0)
|
|
84
|
+
return template;
|
|
85
|
+
const bodyStart = startIndex + DECISIONS_START.length;
|
|
86
|
+
const body = template.slice(bodyStart, endIndex);
|
|
87
|
+
const activeNumbers = new Set(sections.map((section) => SECTION_DECISION_NUMBER[section]));
|
|
88
|
+
const kept = splitDecisionBlocks(body)
|
|
89
|
+
.filter((block) => activeNumbers.has(block.num))
|
|
90
|
+
.map((block) => block.text)
|
|
91
|
+
.join('\n\n');
|
|
92
|
+
const withDecisions = template.slice(0, bodyStart) + kept + template.slice(endIndex);
|
|
93
|
+
return pruneResponseSchema(withDecisions, new Set(sections));
|
|
94
|
+
}
|
|
95
|
+
// ─── Response-schema key removal (§4.4, §5 builder note) ──────────────────────
|
|
96
|
+
/** Anchors bounding the embedded `=== RESPONSE SCHEMA … ===` object notation. */
|
|
97
|
+
const SCHEMA_OBJECT_START = '{\n schema_version: "adminium.llm/v1",';
|
|
98
|
+
const SCHEMA_OBJECT_END = '\n\n=== INPUT: DATABASE SCHEMA ===';
|
|
99
|
+
/**
|
|
100
|
+
* Whole-line splice: drop every line from the one containing `startAnchor`
|
|
101
|
+
* through the one containing `endAnchor` (searched AFTER the start), replacing the
|
|
102
|
+
* span with `replacement`. Anchor-based (not whitespace-hardcoded) so it tracks
|
|
103
|
+
* the verbatim template even if its indentation is later re-flowed. A missing
|
|
104
|
+
* anchor is a no-op — the schema stays intact rather than being corrupted.
|
|
105
|
+
*/
|
|
106
|
+
function spliceLines(block, startAnchor, endAnchor, replacement) {
|
|
107
|
+
const s = block.indexOf(startAnchor);
|
|
108
|
+
if (s < 0)
|
|
109
|
+
return block;
|
|
110
|
+
const e = block.indexOf(endAnchor, s);
|
|
111
|
+
if (e < 0)
|
|
112
|
+
return block;
|
|
113
|
+
const lineStart = block.lastIndexOf('\n', s) + 1;
|
|
114
|
+
const afterEnd = block.indexOf('\n', e + endAnchor.length);
|
|
115
|
+
const lineEnd = afterEnd < 0 ? block.length : afterEnd + 1;
|
|
116
|
+
return block.slice(0, lineStart) + replacement + block.slice(lineEnd);
|
|
117
|
+
}
|
|
118
|
+
function cutLines(block, startAnchor, endAnchor) {
|
|
119
|
+
return spliceLines(block, startAnchor, endAnchor, '');
|
|
120
|
+
}
|
|
121
|
+
/** Leading whitespace of the line containing `anchor` (for indent-preserving splices). */
|
|
122
|
+
function indentOf(block, anchor) {
|
|
123
|
+
const i = block.indexOf(anchor);
|
|
124
|
+
if (i < 0)
|
|
125
|
+
return '';
|
|
126
|
+
return block.slice(block.lastIndexOf('\n', i) + 1, i);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Single-section schema-key removals keyed by the section deselected. Each entry
|
|
130
|
+
* is a `[startAnchor, endAnchor]` pair identifying the line span to delete.
|
|
131
|
+
* `groups`, `widgets` and the nav-group icon are handled separately below because
|
|
132
|
+
* they share the `navGroups` / `dashboards` structures.
|
|
133
|
+
*/
|
|
134
|
+
const SCHEMA_KEY_CUTS = {
|
|
135
|
+
labels: [
|
|
136
|
+
['label: L10n, description: L10n,', 'label: L10n, description: L10n,'],
|
|
137
|
+
['label: L10n, description?: L10n,', 'label: L10n, description?: L10n,'],
|
|
138
|
+
],
|
|
139
|
+
keys: [
|
|
140
|
+
['displayColumn: string | null,', 'displayColumn: string | null,'],
|
|
141
|
+
['naturalKey: string[] | null,', 'naturalKey: string[] | null,'],
|
|
142
|
+
],
|
|
143
|
+
templates: [['pageTemplates: [{', 'confidence: number }],']],
|
|
144
|
+
microcopy: [['microcopy: {', 'pageSubtitle: L10n }']],
|
|
145
|
+
pii: [['pii: null | {', 'confidence: number }']],
|
|
146
|
+
enums: [['enums: [{', 'confidence: number }],']],
|
|
147
|
+
relations: [['relations: {', ' },']],
|
|
148
|
+
icons: [['// lucide kebab-case', '// lucide kebab-case']],
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Remove the response-schema keys of deselected sections from the embedded
|
|
152
|
+
* RESPONSE SCHEMA block (§4.4: deselected keys are "omitted from … the embedded
|
|
153
|
+
* response schema"), so a scoped run's schema reflects only the requested
|
|
154
|
+
* decisions and the model does not emit — and pay output tokens for — keys the
|
|
155
|
+
* run neither asked for nor validates.
|
|
156
|
+
*/
|
|
157
|
+
function pruneResponseSchema(template, active) {
|
|
158
|
+
const startIdx = template.indexOf(SCHEMA_OBJECT_START);
|
|
159
|
+
const endIdx = template.indexOf(SCHEMA_OBJECT_END);
|
|
160
|
+
if (startIdx < 0 || endIdx < 0 || endIdx < startIdx)
|
|
161
|
+
return template;
|
|
162
|
+
let block = template.slice(startIdx, endIdx);
|
|
163
|
+
// Single-section, self-contained key spans.
|
|
164
|
+
for (const [section, cuts] of Object.entries(SCHEMA_KEY_CUTS)) {
|
|
165
|
+
if (active.has(section))
|
|
166
|
+
continue;
|
|
167
|
+
for (const [start, end] of cuts)
|
|
168
|
+
block = cutLines(block, start, end);
|
|
169
|
+
}
|
|
170
|
+
// navGroups (owned by `groups`) + dashboards (`groups` → domain, `widgets` →
|
|
171
|
+
// widgets) share structure, so resolve them together — BEFORE the nav-group
|
|
172
|
+
// icon strip, which expects the navGroups line to still exist.
|
|
173
|
+
const groupsOff = !active.has('groups');
|
|
174
|
+
const widgetsOff = !active.has('widgets');
|
|
175
|
+
if (groupsOff) {
|
|
176
|
+
block = cutLines(block, 'navGroups: [{', 'tables: string[], confidence: number }],');
|
|
177
|
+
}
|
|
178
|
+
if (groupsOff && widgetsOff) {
|
|
179
|
+
block = cutLines(block, 'dashboards: [{', 'confidence: number }] }],');
|
|
180
|
+
}
|
|
181
|
+
else if (groupsOff) {
|
|
182
|
+
block = block.split('domain: string, ').join(''); // dashboards kept for its widgets
|
|
183
|
+
}
|
|
184
|
+
else if (widgetsOff) {
|
|
185
|
+
// The dashboards `tables: string[],` is the only one directly followed by a
|
|
186
|
+
// newline (the navGroups one is `tables: string[], confidence …`), so the
|
|
187
|
+
// trailing `\n` disambiguates it. Remove the widgets sub-block and drop the
|
|
188
|
+
// now-dangling comma so the dashboards object still closes.
|
|
189
|
+
const anchor = 'tables: string[],\n';
|
|
190
|
+
const indent = indentOf(block, anchor);
|
|
191
|
+
block = spliceLines(block, anchor, 'confidence: number }] }],', `${indent}tables: string[] }],\n`);
|
|
192
|
+
}
|
|
193
|
+
// navGroups icon (owned by `icons`) — only when navGroups is still present.
|
|
194
|
+
if (!active.has('icons') && !groupsOff) {
|
|
195
|
+
block = block
|
|
196
|
+
.split('label: L10n, icon: string, order: number,')
|
|
197
|
+
.join('label: L10n, order: number,');
|
|
198
|
+
}
|
|
199
|
+
return template.slice(0, startIdx) + block + template.slice(endIdx);
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Split the decisions body into numbered blocks. A segment beginning `N.` starts
|
|
203
|
+
* a new block; any following non-numbered segment (defensive — the verbatim
|
|
204
|
+
* template has none) reattaches to the current block so an internal blank line
|
|
205
|
+
* would survive intact.
|
|
206
|
+
*/
|
|
207
|
+
function splitDecisionBlocks(body) {
|
|
208
|
+
const blocks = [];
|
|
209
|
+
for (const segment of body.split('\n\n')) {
|
|
210
|
+
const match = /^(\d+)\./.exec(segment);
|
|
211
|
+
if (match?.[1] !== undefined) {
|
|
212
|
+
blocks.push({ num: Number(match[1]), text: segment });
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const last = blocks.at(-1);
|
|
216
|
+
if (last !== undefined)
|
|
217
|
+
last.text = `${last.text}\n\n${segment}`;
|
|
218
|
+
}
|
|
219
|
+
return blocks;
|
|
220
|
+
}
|
|
221
|
+
// ─── User-section rendering ──────────────────────────────────────────────────
|
|
222
|
+
function renderUserSection(input, options, sections) {
|
|
223
|
+
const stubTables = new Set(input.chunk?.stubTables ?? []);
|
|
224
|
+
const schemaIr = serializeSchemaIr(input.schemaIr, { stubTables, stats: input.stats });
|
|
225
|
+
const statsJson = serializeStats(input.stats, { defaultSchema: input.schemaIr.defaultSchema });
|
|
226
|
+
const samplingBlock = serializeSampling(input.schemaIr, input.stats, input.sampling);
|
|
227
|
+
const chunkInfo = renderChunkInfo(input.chunk);
|
|
228
|
+
let user = applySectionToggles(PROMPT_V1_USER, sections);
|
|
229
|
+
user = replaceOnce(user, '{{RUN_ID}}', input.runId);
|
|
230
|
+
user = chunkInfo === '' ? user.replace('{{CHUNK_INFO}}\n', '') : replaceOnce(user, '{{CHUNK_INFO}}', chunkInfo);
|
|
231
|
+
user = replaceOnce(user, '{{REQUESTED_SECTIONS_LIST}}', `Requested: ${sections.join(', ')}`);
|
|
232
|
+
user = replaceOnce(user, '{{ALLOWED_PAGE_TEMPLATE_IDS_JSON}}', toPromptJson(options.allowed.templates));
|
|
233
|
+
user = replaceOnce(user, '{{ALLOWED_WIDGET_IDS_JSON}}', toPromptJson(options.allowed.widgets));
|
|
234
|
+
user = replaceOnce(user, '{{LOCALES_JSON}}', toPromptJson(input.locales));
|
|
235
|
+
user = replaceOnce(user, '{{SCHEMA_IR_JSON}}', toPromptJson(schemaIr));
|
|
236
|
+
user = replaceOnce(user, '{{STATS_JSON}}', toPromptJson(statsJson));
|
|
237
|
+
user = replaceOnce(user, '{{SAMPLING_BLOCK}}', samplingBlock);
|
|
238
|
+
return user;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Replace the first (and only) occurrence of `token` with a literal value. A
|
|
242
|
+
* function replacer is used so `$`-sequences inside injected JSON (`$&`, `$1`, …)
|
|
243
|
+
* are never interpreted as replacement patterns.
|
|
244
|
+
*/
|
|
245
|
+
function replaceOnce(text, token, value) {
|
|
246
|
+
return text.replace(token, () => value);
|
|
247
|
+
}
|
|
248
|
+
/** The `{{CHUNK_INFO}}` sentence for a chunked run; empty otherwise (§4.5, §5.2). */
|
|
249
|
+
export function renderChunkInfo(chunk) {
|
|
250
|
+
if (chunk === undefined)
|
|
251
|
+
return '';
|
|
252
|
+
return (`This is chunk ${chunk.index} of ${chunk.total} of a larger schema. ` +
|
|
253
|
+
`Tables marked "stub": true belong to other chunks — use them as relation ` +
|
|
254
|
+
`context only. Skip decisions 2 and 7 unless this is chunk 1; a separate ` +
|
|
255
|
+
`merge step consolidates them.`);
|
|
256
|
+
}
|
|
257
|
+
// ─── Guards ──────────────────────────────────────────────────────────────────
|
|
258
|
+
function requireEnUs(locales) {
|
|
259
|
+
if (!locales.includes('en_US')) {
|
|
260
|
+
throw new Error('buildPrompt: locales must include "en_US" (06-llm-assist.md §4.1).');
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
//# sourceMappingURL=builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../src/prompt/builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AACrG,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EACL,YAAY,EACZ,oBAAoB,EACpB,kBAAkB,EAClB,uBAAuB,GAOxB,MAAM,YAAY,CAAC;AAEpB,oDAAoD;AACpD,MAAM,CAAC,MAAM,iBAAiB,GAAG,gBAAgB,CAAC;AAClD,MAAM,CAAC,MAAM,eAAe,GAAG,cAAc,CAAC;AAE9C;;;;GAIG;AAC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AI,EAAE,kBAAkB,EAAE,YAAY,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;IAC1E,IAAI,GAAG,WAAW,CAAC,IAAI,EAAE,oBAAoB,EAAE,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC;IACvE,IAAI,GAAG,WAAW,CAAC,IAAI,EAAE,gBAAgB,EAAE,YAAY,CAAC,SAAS,CAAC,CAAC,CAAC;IACpE,IAAI,GAAG,WAAW,CAAC,IAAI,EAAE,oBAAoB,EAAE,aAAa,CAAC,CAAC;IAE9D,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;GAIG;AACH,SAAS,WAAW,CAAC,IAAY,EAAE,KAAa,EAAE,KAAa;IAC7D,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC;AAC1C,CAAC;AAED,qFAAqF;AACrF,MAAM,UAAU,eAAe,CAAC,KAAkC;IAChE,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,EAAE,CAAC;IACnC,OAAO,CACL,iBAAiB,KAAK,CAAC,KAAK,OAAO,KAAK,CAAC,KAAK,uBAAuB;QACrE,2EAA2E;QAC3E,0EAA0E;QAC1E,+BAA+B,CAChC,CAAC;AACJ,CAAC;AAED,gFAAgF;AAEhF,SAAS,WAAW,CAAC,OAA0B;IAC7C,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CAAC,oEAAoE,CAAC,CAAC;IACxF,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { DatabaseModel } from '@adminium/engine';
|
|
2
|
+
import type { BuildPromptOptions, PromptChunk, PromptInput, RequestedSection } from './types.js';
|
|
3
|
+
/** Per-chunk token ceiling (§4.5: "chunks of ≤ 45,000 tokens"). */
|
|
4
|
+
export declare const DEFAULT_CHUNK_TOKEN_BUDGET = 45000;
|
|
5
|
+
export interface ChunkPromptOptions extends BuildPromptOptions {
|
|
6
|
+
/** Per-chunk estimated-token ceiling (§4.5). Default {@link DEFAULT_CHUNK_TOKEN_BUDGET}. */
|
|
7
|
+
chunkTokenBudget?: number;
|
|
8
|
+
}
|
|
9
|
+
/** One planned chunk: which tables it owns in full and which it stubs (§4.5). */
|
|
10
|
+
export interface SchemaChunk {
|
|
11
|
+
/** 1-based position. */
|
|
12
|
+
index: number;
|
|
13
|
+
/** Total chunk count for this plan. */
|
|
14
|
+
total: number;
|
|
15
|
+
/** Qualified ids of the tables this chunk owns in full, in model order. */
|
|
16
|
+
tableIds: string[];
|
|
17
|
+
/** Qualified ids of out-of-chunk FK targets rendered as `"stub": true`, sorted. */
|
|
18
|
+
stubTables: string[];
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Result of {@link buildChunkedPrompts}: one {@link PromptChunk} per chunk plus
|
|
22
|
+
* the parallel {@link SchemaChunk} plan. `chunked` is `false` when the whole
|
|
23
|
+
* schema fit in a single prompt (one chunk, no stubs, no chunk-info sentence).
|
|
24
|
+
*/
|
|
25
|
+
export interface ChunkedPromptArtifact {
|
|
26
|
+
chunked: boolean;
|
|
27
|
+
chunks: PromptChunk[];
|
|
28
|
+
plan: SchemaChunk[];
|
|
29
|
+
sections: readonly RequestedSection[];
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Partition the model's tables into FK-connected components. The graph is
|
|
33
|
+
* undirected over declared + inferred relations (both endpoints, and the join
|
|
34
|
+
* table for m2m) and column FK mirrors. Tables touched by no relation are
|
|
35
|
+
* singleton components. Each component is returned id-sorted, and the outer list
|
|
36
|
+
* is sorted by smallest member id — fully deterministic.
|
|
37
|
+
*/
|
|
38
|
+
export declare function clusterTables(model: DatabaseModel): string[][];
|
|
39
|
+
/**
|
|
40
|
+
* Estimated token contribution of each full table to a prompt: its serialized
|
|
41
|
+
* schema-IR entry + any enum-column entries + declared relations it owns + its
|
|
42
|
+
* statistics entry + (opt-in) its sampling entry. Measured off the real
|
|
43
|
+
* serializers so it tracks the actual prompt bytes; a couple of tokens per
|
|
44
|
+
* fragment cover the surrounding commas.
|
|
45
|
+
*/
|
|
46
|
+
export declare function computeTableWeights(input: PromptInput): Map<string, number>;
|
|
47
|
+
/** Estimated token cost of rendering a table as a names-only stub (§4.5). */
|
|
48
|
+
export declare function computeStubWeights(model: DatabaseModel): Map<string, number>;
|
|
49
|
+
/**
|
|
50
|
+
* The fixed per-chunk overhead (system section + full instruction template +
|
|
51
|
+
* allow-lists + locales + the chunk-info sentence + the empty structural JSON
|
|
52
|
+
* wrappers), measured by rendering a prompt for a table-less model. Every chunk
|
|
53
|
+
* repeats this cost regardless of how many tables it carries.
|
|
54
|
+
*/
|
|
55
|
+
export declare function computeOverheadTokens(input: PromptInput, options: ChunkPromptOptions): number;
|
|
56
|
+
/**
|
|
57
|
+
* The out-of-chunk FK targets a chunk's full tables reference: the `to` (and
|
|
58
|
+
* join `through`) tables of every relation the chunk owns (relation `from` side
|
|
59
|
+
* is in the chunk), plus every column FK-mirror target — restricted to tables
|
|
60
|
+
* not in the chunk. These become the chunk's stub entries so no rendered
|
|
61
|
+
* relation or `references` points at a missing table.
|
|
62
|
+
*/
|
|
63
|
+
export declare function computeStubsForChunk(model: DatabaseModel, fullTables: ReadonlySet<string>): Set<string>;
|
|
64
|
+
/**
|
|
65
|
+
* Partition a schema into map-phase chunks. FFD-packs whole FK clusters into
|
|
66
|
+
* chunks of ≤ `chunkTokenBudget`; any cluster too big for one chunk is split by
|
|
67
|
+
* table row-count descending. Every chunk records the out-of-chunk FK targets it
|
|
68
|
+
* must stub. Deterministic for a given (model, stats, options).
|
|
69
|
+
*/
|
|
70
|
+
export declare function planSchemaChunks(input: PromptInput, options: ChunkPromptOptions): SchemaChunk[];
|
|
71
|
+
/**
|
|
72
|
+
* Build a self-contained sub-model for one chunk: its full tables plus its stub
|
|
73
|
+
* tables (rendered names-only by the serializer via the chunk's `stubTables`
|
|
74
|
+
* set), with enums narrowed to those the full tables reference and relations
|
|
75
|
+
* narrowed to those the chunk owns (`from` in the chunk, `to`/`through` present
|
|
76
|
+
* as full or stub).
|
|
77
|
+
*/
|
|
78
|
+
export declare function subModelForChunk(model: DatabaseModel, fullTables: ReadonlySet<string>, stubTables: ReadonlySet<string>): DatabaseModel;
|
|
79
|
+
/**
|
|
80
|
+
* Build the map-phase prompt(s) for a schema. Split ONLY when the assembled user
|
|
81
|
+
* section exceeds the per-prompt `tokenBudget` (default 60,000 — §4.5); a schema
|
|
82
|
+
* that fits in one prompt is returned unchunked (no stubs, no chunk-info
|
|
83
|
+
* sentence) even when it is larger than the 45,000-token per-chunk packing
|
|
84
|
+
* ceiling. When it does not fit, it is chunked per {@link planSchemaChunks}, each
|
|
85
|
+
* chunk a self-contained prompt with correct FK stubs and each within
|
|
86
|
+
* `chunkTokenBudget`.
|
|
87
|
+
*/
|
|
88
|
+
export declare function buildChunkedPrompts(input: PromptInput, options: ChunkPromptOptions): ChunkedPromptArtifact;
|
|
89
|
+
//# sourceMappingURL=chunker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chunker.d.ts","sourceRoot":"","sources":["../../src/prompt/chunker.ts"],"names":[],"mappings":"AA0BA,OAAO,KAAK,EAAE,aAAa,EAAe,MAAM,kBAAkB,CAAC;AAUnE,OAAO,KAAK,EACV,kBAAkB,EAClB,WAAW,EACX,WAAW,EACX,gBAAgB,EACjB,MAAM,YAAY,CAAC;AAEpB,mEAAmE;AACnE,eAAO,MAAM,0BAA0B,QAAS,CAAC;AAqBjD,MAAM,WAAW,kBAAmB,SAAQ,kBAAkB;IAC5D,4FAA4F;IAC5F,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,iFAAiF;AACjF,MAAM,WAAW,WAAW;IAC1B,wBAAwB;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,uCAAuC;IACvC,KAAK,EAAE,MAAM,CAAC;IACd,2EAA2E;IAC3E,QAAQ,EAA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