@markbrutx/promptbook-cli 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -2
- package/dist/src/config.d.ts +29 -12
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +52 -32
- package/dist/src/config.js.map +1 -1
- package/package.json +4 -4
- package/src/config.ts +59 -35
package/README.md
CHANGED
|
@@ -19,8 +19,11 @@ promptbook view # open the viewer (needs @markbrutx/prom
|
|
|
19
19
|
promptbook annotations list|resolve|clear
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
-
The prompts folder is resolved from `--dir`, then `promptbook.json
|
|
23
|
-
|
|
22
|
+
The prompts folder is resolved from `--dir`, then `promptbook.json` (the
|
|
23
|
+
nearest one found by walking up from the current directory — same model as
|
|
24
|
+
`git`/`biome`/`eslint`; the `promptsDir` value is taken relative to wherever
|
|
25
|
+
the config file lives, so one config at the repo root works from every
|
|
26
|
+
subfolder), then `./prompts`.
|
|
24
27
|
|
|
25
28
|
```
|
|
26
29
|
promptbook resolve assistant --ctx mode=terse --ctx locale=ru
|
package/dist/src/config.d.ts
CHANGED
|
@@ -18,20 +18,32 @@ interface PromptbookConfig {
|
|
|
18
18
|
lint?: unknown;
|
|
19
19
|
eval?: unknown;
|
|
20
20
|
}
|
|
21
|
+
/** A loaded `promptbook.json`: parsed data plus the directory that held it. */
|
|
22
|
+
export interface LoadedConfig {
|
|
23
|
+
/** Parsed JSON object (or `{}` when no file was found or parsing failed). */
|
|
24
|
+
data: PromptbookConfig;
|
|
25
|
+
/** Absolute directory where the config was found; `undefined` if nothing matched up the tree. */
|
|
26
|
+
dir?: string;
|
|
27
|
+
}
|
|
21
28
|
/** lint options sourced from the `lint` section of `promptbook.json`. */
|
|
22
29
|
export interface LintConfig {
|
|
23
30
|
maxTokens?: number;
|
|
24
31
|
bannedTokens?: string[];
|
|
25
32
|
}
|
|
26
33
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
34
|
+
* Walk up from `io.cwd()` to find the first `promptbook.json`, parse it, and
|
|
35
|
+
* return its data + the directory it lived in. Walking up (rather than only
|
|
36
|
+
* checking cwd) is what makes `promptbook` work like `git`/`biome`/`eslint`:
|
|
37
|
+
* one config at the repo root reaches every subfolder. Path-valued keys
|
|
38
|
+
* (currently just `promptsDir`) are resolved relative to {@link LoadedConfig.dir}
|
|
39
|
+
* by {@link resolvePromptsDir}, not relative to wherever the shell happens to
|
|
40
|
+
* be — so `pnpm exec` snapping cwd to a workspace package cannot break the
|
|
41
|
+
* lookup. Missing, unreadable or malformed files yield an empty config
|
|
42
|
+
* (best-effort), so callers can layer flags on top.
|
|
31
43
|
*/
|
|
32
|
-
export declare function loadConfig(io: IO): Promise<
|
|
44
|
+
export declare function loadConfig(io: IO): Promise<LoadedConfig>;
|
|
33
45
|
/** Extract the `lint` section from an already-loaded config. */
|
|
34
|
-
export declare function lintConfigFrom(
|
|
46
|
+
export declare function lintConfigFrom(loaded: LoadedConfig): LintConfig;
|
|
35
47
|
/** Convenience wrapper: load config and extract its `lint` section. */
|
|
36
48
|
export declare function loadLintConfig(io: IO): Promise<LintConfig>;
|
|
37
49
|
/** eval options sourced from the `eval` section of `promptbook.json`. */
|
|
@@ -40,17 +52,22 @@ export interface EvalConfig {
|
|
|
40
52
|
baseUrl?: string;
|
|
41
53
|
}
|
|
42
54
|
/** Extract the `eval` section from an already-loaded config. */
|
|
43
|
-
export declare function evalConfigFrom(
|
|
55
|
+
export declare function evalConfigFrom(loaded: LoadedConfig): EvalConfig;
|
|
44
56
|
/**
|
|
45
|
-
* Resolve the prompts folder by priority:
|
|
46
|
-
*
|
|
47
|
-
*
|
|
57
|
+
* Resolve the prompts folder by priority:
|
|
58
|
+
* 1. `--dir <path>` — relative to **cwd** (explicit per-invocation override).
|
|
59
|
+
* 2. `promptbook.json` `promptsDir` — relative to **the config file's directory**
|
|
60
|
+
* (so the value can stay stable while the user shells around in subfolders).
|
|
61
|
+
* 3. `./prompts` — relative to cwd (back-compat default when no config exists).
|
|
62
|
+
*
|
|
63
|
+
* All results are absolute. Pass a preloaded {@link LoadedConfig} to reuse a
|
|
64
|
+
* single read; otherwise it is loaded here.
|
|
48
65
|
*/
|
|
49
|
-
export declare function resolvePromptsDir(io: IO, dirFlag?: string,
|
|
66
|
+
export declare function resolvePromptsDir(io: IO, dirFlag?: string, loaded?: LoadedConfig): Promise<string>;
|
|
50
67
|
/**
|
|
51
68
|
* Resolve the prompts folder and confirm it exists. On a missing folder, write
|
|
52
69
|
* a clear error to stderr and return null so the caller can exit non-zero.
|
|
53
70
|
*/
|
|
54
|
-
export declare function requirePromptsDir(io: IO, dirFlag?: string,
|
|
71
|
+
export declare function requirePromptsDir(io: IO, dirFlag?: string, loaded?: LoadedConfig): Promise<string | null>;
|
|
55
72
|
export {};
|
|
56
73
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/src/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AACxE,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AASlC;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,CAWtD;AAED,gFAAgF;AAChF,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CActD;AAuBD;;;GAGG;AACH,wBAAsB,YAAY,CAAC,EAAE,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,WAAW,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAalG;AAED,UAAU,gBAAgB;IACxB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,IAAI,CAAC,EAAE,OA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EAAE,YAAY,GAAG,UAAU,CAa/D;AAED,uEAAuE;AACvE,wBAAsB,cAAc,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAEhE;AAED,yEAAyE;AACzE,MAAM,WAAW,UAAU;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,gEAAgE;AAChE,wBAAgB,cAAc,CAAC,MAAM,EAAE,YAAY,GAAG,UAAU,CAa/D;AAED;;;;;;;;;GASG;AACH,wBAAsB,iBAAiB,CAAC,EAAE,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,CASxG;AAYD;;;GAGG;AACH,wBAAsB,iBAAiB,CACrC,EAAE,EAAE,EAAE,EACN,OAAO,CAAC,EAAE,MAAM,EAChB,MAAM,CAAC,EAAE,YAAY,GACpB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAOxB"}
|
package/dist/src/config.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { resolve as resolvePath } from "node:path";
|
|
1
|
+
import { dirname, resolve as resolvePath } from "node:path";
|
|
2
2
|
const NUMERIC = /^-?\d+(?:\.\d+)?$/;
|
|
3
3
|
/** True for a plain JSON object: an object that is neither null nor an array. */
|
|
4
4
|
function isJsonObject(value) {
|
|
@@ -79,31 +79,46 @@ export async function buildContext(io, pairs, contextFile) {
|
|
|
79
79
|
return { ...fileContext, ...parseCtxPairs(pairs) };
|
|
80
80
|
}
|
|
81
81
|
/**
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
82
|
+
* Walk up from `io.cwd()` to find the first `promptbook.json`, parse it, and
|
|
83
|
+
* return its data + the directory it lived in. Walking up (rather than only
|
|
84
|
+
* checking cwd) is what makes `promptbook` work like `git`/`biome`/`eslint`:
|
|
85
|
+
* one config at the repo root reaches every subfolder. Path-valued keys
|
|
86
|
+
* (currently just `promptsDir`) are resolved relative to {@link LoadedConfig.dir}
|
|
87
|
+
* by {@link resolvePromptsDir}, not relative to wherever the shell happens to
|
|
88
|
+
* be — so `pnpm exec` snapping cwd to a workspace package cannot break the
|
|
89
|
+
* lookup. Missing, unreadable or malformed files yield an empty config
|
|
90
|
+
* (best-effort), so callers can layer flags on top.
|
|
86
91
|
*/
|
|
87
92
|
export async function loadConfig(io) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
raw
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
93
|
+
let dir = resolvePath(io.cwd());
|
|
94
|
+
for (;;) {
|
|
95
|
+
const configPath = resolvePath(dir, "promptbook.json");
|
|
96
|
+
let raw;
|
|
97
|
+
try {
|
|
98
|
+
raw = await io.fs.readFile(configPath);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
// not found at this level; try the parent
|
|
102
|
+
}
|
|
103
|
+
if (raw !== undefined) {
|
|
104
|
+
try {
|
|
105
|
+
const parsed = JSON.parse(raw);
|
|
106
|
+
return { data: isJsonObject(parsed) ? parsed : {}, dir };
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
return { data: {}, dir };
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const parent = dirname(dir);
|
|
113
|
+
if (parent === dir) {
|
|
114
|
+
return { data: {} };
|
|
115
|
+
}
|
|
116
|
+
dir = parent;
|
|
102
117
|
}
|
|
103
118
|
}
|
|
104
119
|
/** Extract the `lint` section from an already-loaded config. */
|
|
105
|
-
export function lintConfigFrom(
|
|
106
|
-
const section =
|
|
120
|
+
export function lintConfigFrom(loaded) {
|
|
121
|
+
const section = loaded.data.lint;
|
|
107
122
|
if (!isJsonObject(section)) {
|
|
108
123
|
return {};
|
|
109
124
|
}
|
|
@@ -121,8 +136,8 @@ export async function loadLintConfig(io) {
|
|
|
121
136
|
return lintConfigFrom(await loadConfig(io));
|
|
122
137
|
}
|
|
123
138
|
/** Extract the `eval` section from an already-loaded config. */
|
|
124
|
-
export function evalConfigFrom(
|
|
125
|
-
const section =
|
|
139
|
+
export function evalConfigFrom(loaded) {
|
|
140
|
+
const section = loaded.data.eval;
|
|
126
141
|
if (!isJsonObject(section)) {
|
|
127
142
|
return {};
|
|
128
143
|
}
|
|
@@ -136,17 +151,22 @@ export function evalConfigFrom(config) {
|
|
|
136
151
|
return evalConfig;
|
|
137
152
|
}
|
|
138
153
|
/**
|
|
139
|
-
* Resolve the prompts folder by priority:
|
|
140
|
-
*
|
|
141
|
-
*
|
|
154
|
+
* Resolve the prompts folder by priority:
|
|
155
|
+
* 1. `--dir <path>` — relative to **cwd** (explicit per-invocation override).
|
|
156
|
+
* 2. `promptbook.json` `promptsDir` — relative to **the config file's directory**
|
|
157
|
+
* (so the value can stay stable while the user shells around in subfolders).
|
|
158
|
+
* 3. `./prompts` — relative to cwd (back-compat default when no config exists).
|
|
159
|
+
*
|
|
160
|
+
* All results are absolute. Pass a preloaded {@link LoadedConfig} to reuse a
|
|
161
|
+
* single read; otherwise it is loaded here.
|
|
142
162
|
*/
|
|
143
|
-
export async function resolvePromptsDir(io, dirFlag,
|
|
163
|
+
export async function resolvePromptsDir(io, dirFlag, loaded) {
|
|
144
164
|
if (dirFlag !== undefined) {
|
|
145
165
|
return resolvePath(io.cwd(), dirFlag);
|
|
146
166
|
}
|
|
147
|
-
const resolved =
|
|
148
|
-
if (typeof resolved.promptsDir === "string") {
|
|
149
|
-
return resolvePath(
|
|
167
|
+
const resolved = loaded ?? (await loadConfig(io));
|
|
168
|
+
if (typeof resolved.data.promptsDir === "string" && resolved.dir !== undefined) {
|
|
169
|
+
return resolvePath(resolved.dir, resolved.data.promptsDir);
|
|
150
170
|
}
|
|
151
171
|
return resolvePath(io.cwd(), "prompts");
|
|
152
172
|
}
|
|
@@ -164,8 +184,8 @@ async function dirExists(io, dir) {
|
|
|
164
184
|
* Resolve the prompts folder and confirm it exists. On a missing folder, write
|
|
165
185
|
* a clear error to stderr and return null so the caller can exit non-zero.
|
|
166
186
|
*/
|
|
167
|
-
export async function requirePromptsDir(io, dirFlag,
|
|
168
|
-
const promptsDir = await resolvePromptsDir(io, dirFlag,
|
|
187
|
+
export async function requirePromptsDir(io, dirFlag, loaded) {
|
|
188
|
+
const promptsDir = await resolvePromptsDir(io, dirFlag, loaded);
|
|
169
189
|
if (!(await dirExists(io, promptsDir))) {
|
|
170
190
|
io.stderr(`error: prompts folder not found: ${promptsDir}\n`);
|
|
171
191
|
return null;
|
package/dist/src/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,WAAW,CAAC;AAI5D,MAAM,OAAO,GAAG,mBAAmB,CAAC;AAEpC,iFAAiF;AACjF,SAAS,YAAY,CAAC,KAAc;IAClC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,GAAW;IACtC,IAAI,GAAG,KAAK,MAAM,EAAE,CAAC;QACnB,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,GAAG,KAAK,OAAO,EAAE,CAAC;QACpB,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;Q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,CAAC,GAAG,KAAK,SAAS,EAAE,CAAC;QAC/E,OAAO,WAAW,CAAC,QAAQ,CAAC,GAAG,EAAE,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC7D,CAAC;IACD,OAAO,WAAW,CAAC,EAAE,CAAC,GAAG,EAAE,EAAE,SAAS,CAAC,CAAC;AAC1C,CAAC;AAED,oFAAoF;AACpF,KAAK,UAAU,SAAS,CAAC,EAAM,EAAE,GAAW;IAC1C,IAAI,CAAC;QACH,MAAM,EAAE,CAAC,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACzB,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,EAAM,EACN,OAAgB,EAChB,MAAqB;IAErB,MAAM,UAAU,GAAG,MAAM,iBAAiB,CAAC,EAAE,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IAChE,IAAI,CAAC,CAAC,MAAM,SAAS,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC;QACvC,EAAE,CAAC,MAAM,CAAC,oCAAoC,UAAU,IAAI,CAAC,CAAC;QAC9D,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markbrutx/promptbook-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Thin terminal surface over @markbrutx/promptbook-core: resolve and ls for agents and CI.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -48,11 +48,11 @@
|
|
|
48
48
|
"check": "biome check ."
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@markbrutx/promptbook-core": "^0.
|
|
52
|
-
"@markbrutx/promptbook-openrouter": "^0.
|
|
51
|
+
"@markbrutx/promptbook-core": "^0.3.0",
|
|
52
|
+
"@markbrutx/promptbook-openrouter": "^0.3.0"
|
|
53
53
|
},
|
|
54
54
|
"optionalDependencies": {
|
|
55
|
-
"@markbrutx/promptbook-viewer": "^0.
|
|
55
|
+
"@markbrutx/promptbook-viewer": "^0.3.0"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@biomejs/biome": "latest",
|
package/src/config.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { resolve as resolvePath } from "node:path";
|
|
1
|
+
import { dirname, resolve as resolvePath } from "node:path";
|
|
2
2
|
import type { Context, ContextValue } from "@markbrutx/promptbook-core";
|
|
3
3
|
import type { IO } from "./io.js";
|
|
4
4
|
|
|
@@ -90,6 +90,14 @@ interface PromptbookConfig {
|
|
|
90
90
|
eval?: unknown;
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
/** A loaded `promptbook.json`: parsed data plus the directory that held it. */
|
|
94
|
+
export interface LoadedConfig {
|
|
95
|
+
/** Parsed JSON object (or `{}` when no file was found or parsing failed). */
|
|
96
|
+
data: PromptbookConfig;
|
|
97
|
+
/** Absolute directory where the config was found; `undefined` if nothing matched up the tree. */
|
|
98
|
+
dir?: string;
|
|
99
|
+
}
|
|
100
|
+
|
|
93
101
|
/** lint options sourced from the `lint` section of `promptbook.json`. */
|
|
94
102
|
export interface LintConfig {
|
|
95
103
|
maxTokens?: number;
|
|
@@ -97,30 +105,45 @@ export interface LintConfig {
|
|
|
97
105
|
}
|
|
98
106
|
|
|
99
107
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
108
|
+
* Walk up from `io.cwd()` to find the first `promptbook.json`, parse it, and
|
|
109
|
+
* return its data + the directory it lived in. Walking up (rather than only
|
|
110
|
+
* checking cwd) is what makes `promptbook` work like `git`/`biome`/`eslint`:
|
|
111
|
+
* one config at the repo root reaches every subfolder. Path-valued keys
|
|
112
|
+
* (currently just `promptsDir`) are resolved relative to {@link LoadedConfig.dir}
|
|
113
|
+
* by {@link resolvePromptsDir}, not relative to wherever the shell happens to
|
|
114
|
+
* be — so `pnpm exec` snapping cwd to a workspace package cannot break the
|
|
115
|
+
* lookup. Missing, unreadable or malformed files yield an empty config
|
|
116
|
+
* (best-effort), so callers can layer flags on top.
|
|
104
117
|
*/
|
|
105
|
-
export async function loadConfig(io: IO): Promise<
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
raw
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
+
export async function loadConfig(io: IO): Promise<LoadedConfig> {
|
|
119
|
+
let dir = resolvePath(io.cwd());
|
|
120
|
+
for (;;) {
|
|
121
|
+
const configPath = resolvePath(dir, "promptbook.json");
|
|
122
|
+
let raw: string | undefined;
|
|
123
|
+
try {
|
|
124
|
+
raw = await io.fs.readFile(configPath);
|
|
125
|
+
} catch {
|
|
126
|
+
// not found at this level; try the parent
|
|
127
|
+
}
|
|
128
|
+
if (raw !== undefined) {
|
|
129
|
+
try {
|
|
130
|
+
const parsed = JSON.parse(raw) as unknown;
|
|
131
|
+
return { data: isJsonObject(parsed) ? parsed : {}, dir };
|
|
132
|
+
} catch {
|
|
133
|
+
return { data: {}, dir };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const parent = dirname(dir);
|
|
137
|
+
if (parent === dir) {
|
|
138
|
+
return { data: {} };
|
|
139
|
+
}
|
|
140
|
+
dir = parent;
|
|
118
141
|
}
|
|
119
142
|
}
|
|
120
143
|
|
|
121
144
|
/** Extract the `lint` section from an already-loaded config. */
|
|
122
|
-
export function lintConfigFrom(
|
|
123
|
-
const section =
|
|
145
|
+
export function lintConfigFrom(loaded: LoadedConfig): LintConfig {
|
|
146
|
+
const section = loaded.data.lint;
|
|
124
147
|
if (!isJsonObject(section)) {
|
|
125
148
|
return {};
|
|
126
149
|
}
|
|
@@ -146,8 +169,8 @@ export interface EvalConfig {
|
|
|
146
169
|
}
|
|
147
170
|
|
|
148
171
|
/** Extract the `eval` section from an already-loaded config. */
|
|
149
|
-
export function evalConfigFrom(
|
|
150
|
-
const section =
|
|
172
|
+
export function evalConfigFrom(loaded: LoadedConfig): EvalConfig {
|
|
173
|
+
const section = loaded.data.eval;
|
|
151
174
|
if (!isJsonObject(section)) {
|
|
152
175
|
return {};
|
|
153
176
|
}
|
|
@@ -162,21 +185,22 @@ export function evalConfigFrom(config: PromptbookConfig): EvalConfig {
|
|
|
162
185
|
}
|
|
163
186
|
|
|
164
187
|
/**
|
|
165
|
-
* Resolve the prompts folder by priority:
|
|
166
|
-
*
|
|
167
|
-
*
|
|
188
|
+
* Resolve the prompts folder by priority:
|
|
189
|
+
* 1. `--dir <path>` — relative to **cwd** (explicit per-invocation override).
|
|
190
|
+
* 2. `promptbook.json` `promptsDir` — relative to **the config file's directory**
|
|
191
|
+
* (so the value can stay stable while the user shells around in subfolders).
|
|
192
|
+
* 3. `./prompts` — relative to cwd (back-compat default when no config exists).
|
|
193
|
+
*
|
|
194
|
+
* All results are absolute. Pass a preloaded {@link LoadedConfig} to reuse a
|
|
195
|
+
* single read; otherwise it is loaded here.
|
|
168
196
|
*/
|
|
169
|
-
export async function resolvePromptsDir(
|
|
170
|
-
io: IO,
|
|
171
|
-
dirFlag?: string,
|
|
172
|
-
config?: PromptbookConfig,
|
|
173
|
-
): Promise<string> {
|
|
197
|
+
export async function resolvePromptsDir(io: IO, dirFlag?: string, loaded?: LoadedConfig): Promise<string> {
|
|
174
198
|
if (dirFlag !== undefined) {
|
|
175
199
|
return resolvePath(io.cwd(), dirFlag);
|
|
176
200
|
}
|
|
177
|
-
const resolved =
|
|
178
|
-
if (typeof resolved.promptsDir === "string") {
|
|
179
|
-
return resolvePath(
|
|
201
|
+
const resolved = loaded ?? (await loadConfig(io));
|
|
202
|
+
if (typeof resolved.data.promptsDir === "string" && resolved.dir !== undefined) {
|
|
203
|
+
return resolvePath(resolved.dir, resolved.data.promptsDir);
|
|
180
204
|
}
|
|
181
205
|
return resolvePath(io.cwd(), "prompts");
|
|
182
206
|
}
|
|
@@ -198,9 +222,9 @@ async function dirExists(io: IO, dir: string): Promise<boolean> {
|
|
|
198
222
|
export async function requirePromptsDir(
|
|
199
223
|
io: IO,
|
|
200
224
|
dirFlag?: string,
|
|
201
|
-
|
|
225
|
+
loaded?: LoadedConfig,
|
|
202
226
|
): Promise<string | null> {
|
|
203
|
-
const promptsDir = await resolvePromptsDir(io, dirFlag,
|
|
227
|
+
const promptsDir = await resolvePromptsDir(io, dirFlag, loaded);
|
|
204
228
|
if (!(await dirExists(io, promptsDir))) {
|
|
205
229
|
io.stderr(`error: prompts folder not found: ${promptsDir}\n`);
|
|
206
230
|
return null;
|