@markbrutx/promptbook-core 0.1.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/LICENSE +21 -0
- package/README.md +53 -0
- package/dist/annotations.d.ts +56 -0
- package/dist/annotations.d.ts.map +1 -0
- package/dist/annotations.js +50 -0
- package/dist/annotations.js.map +1 -0
- package/dist/bundle.d.ts +44 -0
- package/dist/bundle.d.ts.map +1 -0
- package/dist/bundle.js +135 -0
- package/dist/bundle.js.map +1 -0
- package/dist/edge/index.js +192 -0
- package/dist/edge.d.ts +12 -0
- package/dist/edge.d.ts.map +1 -0
- package/dist/edge.js +11 -0
- package/dist/edge.js.map +1 -0
- package/dist/eval/assertions.d.ts +15 -0
- package/dist/eval/assertions.d.ts.map +1 -0
- package/dist/eval/assertions.js +131 -0
- package/dist/eval/assertions.js.map +1 -0
- package/dist/eval/evaluate.d.ts +15 -0
- package/dist/eval/evaluate.d.ts.map +1 -0
- package/dist/eval/evaluate.js +65 -0
- package/dist/eval/evaluate.js.map +1 -0
- package/dist/eval/load-fixtures.d.ts +12 -0
- package/dist/eval/load-fixtures.d.ts.map +1 -0
- package/dist/eval/load-fixtures.js +87 -0
- package/dist/eval/load-fixtures.js.map +1 -0
- package/dist/eval/types.d.ts +123 -0
- package/dist/eval/types.d.ts.map +1 -0
- package/dist/eval/types.js +2 -0
- package/dist/eval/types.js.map +1 -0
- package/dist/frontmatter.d.ts +12 -0
- package/dist/frontmatter.d.ts.map +1 -0
- package/dist/frontmatter.js +22 -0
- package/dist/frontmatter.js.map +1 -0
- package/dist/fs.d.ts +11 -0
- package/dist/fs.d.ts.map +1 -0
- package/dist/fs.js +20 -0
- package/dist/fs.js.map +1 -0
- package/dist/guards.d.ts +6 -0
- package/dist/guards.d.ts.map +1 -0
- package/dist/guards.js +9 -0
- package/dist/guards.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/interpolate.d.ts +11 -0
- package/dist/interpolate.d.ts.map +1 -0
- package/dist/interpolate.js +25 -0
- package/dist/interpolate.js.map +1 -0
- package/dist/lint/lint.d.ts +11 -0
- package/dist/lint/lint.d.ts.map +1 -0
- package/dist/lint/lint.js +30 -0
- package/dist/lint/lint.js.map +1 -0
- package/dist/lint/references.d.ts +18 -0
- package/dist/lint/references.d.ts.map +1 -0
- package/dist/lint/references.js +39 -0
- package/dist/lint/references.js.map +1 -0
- package/dist/lint/rules/banned-tokens.d.ts +13 -0
- package/dist/lint/rules/banned-tokens.d.ts.map +1 -0
- package/dist/lint/rules/banned-tokens.js +38 -0
- package/dist/lint/rules/banned-tokens.js.map +1 -0
- package/dist/lint/rules/dangling-reference.d.ts +11 -0
- package/dist/lint/rules/dangling-reference.d.ts.map +1 -0
- package/dist/lint/rules/dangling-reference.js +37 -0
- package/dist/lint/rules/dangling-reference.js.map +1 -0
- package/dist/lint/rules/dead-rule.d.ts +21 -0
- package/dist/lint/rules/dead-rule.d.ts.map +1 -0
- package/dist/lint/rules/dead-rule.js +135 -0
- package/dist/lint/rules/dead-rule.js.map +1 -0
- package/dist/lint/rules/example-balance.d.ts +19 -0
- package/dist/lint/rules/example-balance.d.ts.map +1 -0
- package/dist/lint/rules/example-balance.js +57 -0
- package/dist/lint/rules/example-balance.js.map +1 -0
- package/dist/lint/rules/index.d.ts +28 -0
- package/dist/lint/rules/index.d.ts.map +1 -0
- package/dist/lint/rules/index.js +30 -0
- package/dist/lint/rules/index.js.map +1 -0
- package/dist/lint/rules/language-directive-position.d.ts +16 -0
- package/dist/lint/rules/language-directive-position.d.ts.map +1 -0
- package/dist/lint/rules/language-directive-position.js +42 -0
- package/dist/lint/rules/language-directive-position.js.map +1 -0
- package/dist/lint/rules/token-budget.d.ts +18 -0
- package/dist/lint/rules/token-budget.d.ts.map +1 -0
- package/dist/lint/rules/token-budget.js +39 -0
- package/dist/lint/rules/token-budget.js.map +1 -0
- package/dist/lint/rules/unused-fragment.d.ts +11 -0
- package/dist/lint/rules/unused-fragment.d.ts.map +1 -0
- package/dist/lint/rules/unused-fragment.js +33 -0
- package/dist/lint/rules/unused-fragment.js.map +1 -0
- package/dist/lint/types.d.ts +50 -0
- package/dist/lint/types.d.ts.map +1 -0
- package/dist/lint/types.js +2 -0
- package/dist/lint/types.js.map +1 -0
- package/dist/load.d.ts +12 -0
- package/dist/load.d.ts.map +1 -0
- package/dist/load.js +238 -0
- package/dist/load.js.map +1 -0
- package/dist/paths.d.ts +12 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +25 -0
- package/dist/paths.js.map +1 -0
- package/dist/resolve-book.d.ts +15 -0
- package/dist/resolve-book.d.ts.map +1 -0
- package/dist/resolve-book.js +195 -0
- package/dist/resolve-book.js.map +1 -0
- package/dist/resolve.d.ts +13 -0
- package/dist/resolve.d.ts.map +1 -0
- package/dist/resolve.js +17 -0
- package/dist/resolve.js.map +1 -0
- package/dist/types.d.ts +173 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/package.json +48 -0
- package/src/annotations.ts +100 -0
- package/src/bundle.ts +163 -0
- package/src/edge.ts +11 -0
- package/src/eval/assertions.ts +174 -0
- package/src/eval/evaluate.ts +84 -0
- package/src/eval/load-fixtures.ts +91 -0
- package/src/eval/types.ts +134 -0
- package/src/frontmatter.ts +28 -0
- package/src/fs.ts +21 -0
- package/src/guards.ts +11 -0
- package/src/index.ts +84 -0
- package/src/interpolate.ts +27 -0
- package/src/lint/lint.ts +32 -0
- package/src/lint/references.ts +50 -0
- package/src/lint/rules/banned-tokens.ts +46 -0
- package/src/lint/rules/dangling-reference.ts +43 -0
- package/src/lint/rules/dead-rule.ts +147 -0
- package/src/lint/rules/example-balance.ts +68 -0
- package/src/lint/rules/index.ts +47 -0
- package/src/lint/rules/language-directive-position.ts +51 -0
- package/src/lint/rules/token-budget.ts +50 -0
- package/src/lint/rules/unused-fragment.ts +38 -0
- package/src/lint/types.ts +55 -0
- package/src/load.ts +282 -0
- package/src/paths.ts +27 -0
- package/src/resolve-book.ts +237 -0
- package/src/resolve.ts +18 -0
- package/src/types.ts +191 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public data model for the promptbook core.
|
|
3
|
+
*
|
|
4
|
+
* The core is domain-agnostic: it only selects, orders and interpolates
|
|
5
|
+
* fragments. Any computed value (digests, scores, sorted lists) must be
|
|
6
|
+
* supplied by the caller through {@link Context}.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** A scalar context value. The core never stores domain objects. */
|
|
10
|
+
export type ContextValue = string | number | boolean;
|
|
11
|
+
|
|
12
|
+
/** A flat bag of facts the caller knows at resolve time. */
|
|
13
|
+
export type Context = Record<string, ContextValue>;
|
|
14
|
+
|
|
15
|
+
/** A reusable micro-prompt (the WHAT). */
|
|
16
|
+
export interface Fragment {
|
|
17
|
+
/** Unique id within a prompts folder. */
|
|
18
|
+
id: string;
|
|
19
|
+
/** Optional classification, e.g. "directive" or "format". */
|
|
20
|
+
kind?: string;
|
|
21
|
+
/** Optional free-form tags. */
|
|
22
|
+
tags?: string[];
|
|
23
|
+
/** Body text; may contain `${path}` placeholders. */
|
|
24
|
+
body: string;
|
|
25
|
+
/** Absolute/relative path the fragment was loaded from. */
|
|
26
|
+
sourceFile: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Equality conditions on context. An empty object always matches. */
|
|
30
|
+
export type When = Record<string, ContextValue>;
|
|
31
|
+
|
|
32
|
+
/** The single action a rule performs. */
|
|
33
|
+
export type RuleAction = "add" | "replace" | "forbid" | "order";
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A declarative selection rule (the WHEN). Exactly one action field is set,
|
|
37
|
+
* matching {@link RuleAction}.
|
|
38
|
+
*/
|
|
39
|
+
export interface Rule {
|
|
40
|
+
/** Position in the composition's `rules` list; drives the cascade. */
|
|
41
|
+
index: number;
|
|
42
|
+
/** Match conditions. Empty = always fires. */
|
|
43
|
+
when: When;
|
|
44
|
+
/** Which action this rule performs. */
|
|
45
|
+
action: RuleAction;
|
|
46
|
+
/** ids to insert (action "add"). */
|
|
47
|
+
add?: string[];
|
|
48
|
+
/** Optional anchor: insert added ids after this id. */
|
|
49
|
+
after?: string;
|
|
50
|
+
/** Map of old id to new id (action "replace"). */
|
|
51
|
+
replace?: Record<string, string>;
|
|
52
|
+
/** ids to remove from the result (action "forbid"). */
|
|
53
|
+
forbid?: string[];
|
|
54
|
+
/** Explicit final order of ids (action "order"). */
|
|
55
|
+
order?: string[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A full system prompt assembled from fragments under one context. */
|
|
59
|
+
export interface Composition {
|
|
60
|
+
/** Lookup name, e.g. "assistant". */
|
|
61
|
+
name: string;
|
|
62
|
+
/** Ordered base list of fragment ids. */
|
|
63
|
+
base: string[];
|
|
64
|
+
/** Optional explicit order override declared on the composition. */
|
|
65
|
+
order?: string[];
|
|
66
|
+
/** Ordered selection rules. */
|
|
67
|
+
rules: Rule[];
|
|
68
|
+
/** File the composition was loaded from. */
|
|
69
|
+
sourceFile: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* A captured snapshot of a builder-backed prompt's output. The core never
|
|
74
|
+
* runs the builder; it only stores the frozen text so the registry can show it.
|
|
75
|
+
*/
|
|
76
|
+
export interface CodePromptSample {
|
|
77
|
+
/** Label identifying the sample, e.g. a scenario name. */
|
|
78
|
+
label: string;
|
|
79
|
+
/** Optional context the sample was captured under. */
|
|
80
|
+
context?: Context;
|
|
81
|
+
/** The captured builder output, stored verbatim. */
|
|
82
|
+
output: string;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* A builder-backed prompt registered in the book (the HOW lives in code, not in
|
|
87
|
+
* rules). The core holds metadata plus captured {@link CodePromptSample}s and
|
|
88
|
+
* **never executes the builder** — it stays dumb and deterministic. This lets a
|
|
89
|
+
* book index every prompt of a domain, computed ones included, as one menu.
|
|
90
|
+
*/
|
|
91
|
+
export interface CodePrompt {
|
|
92
|
+
/** Lookup name, e.g. "digest-table". */
|
|
93
|
+
name: string;
|
|
94
|
+
/** Optional human description. */
|
|
95
|
+
description?: string;
|
|
96
|
+
/** Captured output samples (snapshots, not live executions). */
|
|
97
|
+
samples: CodePromptSample[];
|
|
98
|
+
/** Manifest file the code-prompt was loaded from. */
|
|
99
|
+
sourceFile: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** A loaded prompts folder: fragments + compositions + code-prompts + load-time warnings. */
|
|
103
|
+
export interface PromptBook {
|
|
104
|
+
fragments: Map<string, Fragment>;
|
|
105
|
+
compositions: Map<string, Composition>;
|
|
106
|
+
/** Builder-backed prompts registered with snapshot output (never executed). */
|
|
107
|
+
codePrompts: Map<string, CodePrompt>;
|
|
108
|
+
warnings: string[];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Per-rule entry in the explain trace. */
|
|
112
|
+
export interface RuleTrace {
|
|
113
|
+
index: number;
|
|
114
|
+
action: RuleAction;
|
|
115
|
+
when: When;
|
|
116
|
+
/** Whether the rule's `when` matched the context. */
|
|
117
|
+
fired: boolean;
|
|
118
|
+
/** When `fired` is false, why (which key failed). */
|
|
119
|
+
reason?: string;
|
|
120
|
+
/** When `fired` is true, a human-readable summary of the effect. */
|
|
121
|
+
effect?: string;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface ReplaceTrace {
|
|
125
|
+
from: string;
|
|
126
|
+
to: string;
|
|
127
|
+
ruleIndex: number;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface AddTrace {
|
|
131
|
+
id: string;
|
|
132
|
+
after?: string;
|
|
133
|
+
ruleIndex: number;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export interface ForbidTrace {
|
|
137
|
+
id: string;
|
|
138
|
+
ruleIndex: number;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** A context axis that rules referenced but none matched its value. */
|
|
142
|
+
export interface UnmatchedAxis {
|
|
143
|
+
key: string;
|
|
144
|
+
value: ContextValue;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** The explain trace: everything needed to see why the prompt looks as it does. */
|
|
148
|
+
export interface Trace {
|
|
149
|
+
/** Composition name that was resolved. */
|
|
150
|
+
prompt: string;
|
|
151
|
+
/** Context the resolution ran against. */
|
|
152
|
+
context: Context;
|
|
153
|
+
/** Every rule, in declaration order, with fired/why. */
|
|
154
|
+
rules: RuleTrace[];
|
|
155
|
+
/** Final fragment ids in join order (only fragments that rendered). */
|
|
156
|
+
finalOrder: string[];
|
|
157
|
+
/** Replacements applied. */
|
|
158
|
+
replaced: ReplaceTrace[];
|
|
159
|
+
/** Additions applied. */
|
|
160
|
+
added: AddTrace[];
|
|
161
|
+
/** Forbids applied. */
|
|
162
|
+
forbidden: ForbidTrace[];
|
|
163
|
+
/** Axes present in context that no rule matched (e.g. unknown industry). */
|
|
164
|
+
unmatchedAxes: UnmatchedAxis[];
|
|
165
|
+
/** Missing variables, missing fragment refs, and other non-fatal issues. */
|
|
166
|
+
warnings: string[];
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** The result of {@link resolve}. */
|
|
170
|
+
export interface ResolveResult {
|
|
171
|
+
text: string;
|
|
172
|
+
trace: Trace;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Injectable filesystem so the core stays runtime-agnostic (Node/Deno/Bun). */
|
|
176
|
+
export interface FsAdapter {
|
|
177
|
+
readFile(path: string): Promise<string>;
|
|
178
|
+
readDir(path: string): Promise<string[]>;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Input to {@link resolve}. */
|
|
182
|
+
export interface ResolveInput {
|
|
183
|
+
/** Folder containing `fragments/` and `rules/`. */
|
|
184
|
+
promptsDir: string;
|
|
185
|
+
/** Composition name to assemble. */
|
|
186
|
+
prompt: string;
|
|
187
|
+
/** Facts to match rules against and interpolate into bodies. */
|
|
188
|
+
context?: Context;
|
|
189
|
+
/** Optional filesystem adapter; defaults to a lazy Node adapter. */
|
|
190
|
+
fs?: FsAdapter;
|
|
191
|
+
}
|