@trazum/cli 1.60.3 → 1.61.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/dist/i18n/en.d.ts.map +1 -1
- package/dist/i18n/en.js +91 -0
- package/dist/i18n/en.js.map +1 -1
- package/dist/i18n/es.d.ts.map +1 -1
- package/dist/i18n/es.js +91 -0
- package/dist/i18n/es.js.map +1 -1
- package/dist/i18n/types.d.ts +37 -0
- package/dist/i18n/types.d.ts.map +1 -1
- package/dist/index.js +160 -2
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/i18n/en.ts +98 -0
- package/src/i18n/es.ts +98 -0
- package/src/i18n/types.ts +39 -0
- package/src/index.ts +159 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trazum/cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.61.0",
|
|
4
4
|
"description": "Trazum CLI: find where your LLM bill goes, price every finding per month, and enforce token budgets in CI.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "David Mu\u00f1oz Rey",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"prepublishOnly": "npm run build && npm test"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@trazum/core": "1.
|
|
40
|
+
"@trazum/core": "1.61.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/node": "^26.2.0",
|
package/src/i18n/en.ts
CHANGED
|
@@ -58,6 +58,7 @@ ${bold('USAGE')}
|
|
|
58
58
|
trazum conform <file|-> [--contract <name>]
|
|
59
59
|
trazum rollup <document...|dir> [--json]
|
|
60
60
|
trazum pulse [--max-stale-hours <n>]
|
|
61
|
+
trazum write [--answers <file>] [--json] [-o <file>]
|
|
61
62
|
trazum models
|
|
62
63
|
trazum rules [--measure <dir>] [--level <safe|aggressive>]
|
|
63
64
|
trazum gateway <provider> --on-cannot-tell <fail-open|fail-closed>
|
|
@@ -117,6 +118,16 @@ ${bold('OPTIONS FOR rollup')}
|
|
|
117
118
|
|
|
118
119
|
Exits 1 when a contribution was handed over and could not be merged.
|
|
119
120
|
|
|
121
|
+
${bold('OPTIONS FOR write')}
|
|
122
|
+
--answers <file> A JSON object of slot ids and answers. Without it,
|
|
123
|
+
the questions are asked one at a time; an empty
|
|
124
|
+
line declines one, which is an answer.
|
|
125
|
+
--json The prompt-draft document, refusals included.
|
|
126
|
+
-o, --out <file> Where to write the prompt. Defaults to stdout,
|
|
127
|
+
with everything else on stderr.
|
|
128
|
+
--calls <n> Calls per month, for the estimate.
|
|
129
|
+
--avg-output <n> Average output tokens per call, for the estimate.
|
|
130
|
+
|
|
120
131
|
${bold('OPTIONS FOR pulse')}
|
|
121
132
|
--max-stale-hours <n> Exit 1 when something that runs here has not run
|
|
122
133
|
in over n hours. Without it, the ages are
|
|
@@ -2676,4 +2687,91 @@ ${bold('EXAMPLES')}
|
|
|
2676
2687
|
moneyIncomparablePricing: (was, now) =>
|
|
2677
2688
|
`Prices were reviewed ${was} when the baseline was recorded and ${now} now, so the monthly figures are not the same measurement. The token comparison is unaffected.`,
|
|
2678
2689
|
},
|
|
2690
|
+
|
|
2691
|
+
write: {
|
|
2692
|
+
slots: {
|
|
2693
|
+
task: {
|
|
2694
|
+
question: 'What should the model do, in one sentence?',
|
|
2695
|
+
unlocks: 'the whole prompt — without it there is nothing to write',
|
|
2696
|
+
},
|
|
2697
|
+
role: {
|
|
2698
|
+
question: 'Who is the model being while it does that?',
|
|
2699
|
+
unlocks: 'the stance of the answer; left out, the model picks one for you',
|
|
2700
|
+
},
|
|
2701
|
+
inputs: {
|
|
2702
|
+
question: 'What changes from one call to the next?',
|
|
2703
|
+
unlocks: 'the varying part — without it the prompt hard-codes one case and is rewritten for the next',
|
|
2704
|
+
},
|
|
2705
|
+
'output-shape': {
|
|
2706
|
+
question: 'What should come back: prose, json, list or table?',
|
|
2707
|
+
unlocks: 'the output contract, and whether a consumer can parse the answer at all',
|
|
2708
|
+
},
|
|
2709
|
+
'output-schema': {
|
|
2710
|
+
question: 'Which fields or columns, and which are always present?',
|
|
2711
|
+
unlocks: 'field names a consumer can rely on rather than infer from one sample',
|
|
2712
|
+
},
|
|
2713
|
+
'output-length': {
|
|
2714
|
+
question: 'How long should the answer be, at most?',
|
|
2715
|
+
unlocks: 'the ceiling that stops a paid overrun nobody reads',
|
|
2716
|
+
},
|
|
2717
|
+
audience: {
|
|
2718
|
+
question: 'Who reads the output?',
|
|
2719
|
+
unlocks: 'the register — an answer for an engineer and one for a customer are different answers',
|
|
2720
|
+
},
|
|
2721
|
+
constraints: {
|
|
2722
|
+
question: 'What must it never do?',
|
|
2723
|
+
unlocks: 'the prohibitions; stated once here rather than discovered one incident at a time',
|
|
2724
|
+
},
|
|
2725
|
+
refusal: {
|
|
2726
|
+
question: 'What should it do when it cannot answer?',
|
|
2727
|
+
unlocks: 'a refusal that arrives with a reason instead of a confident guess',
|
|
2728
|
+
},
|
|
2729
|
+
examples: {
|
|
2730
|
+
question: 'Is there an example of a good answer?',
|
|
2731
|
+
unlocks: 'few-shot guidance, and the chance to check it is not repeating itself',
|
|
2732
|
+
},
|
|
2733
|
+
'example-inputs': {
|
|
2734
|
+
question: 'What input produced that example?',
|
|
2735
|
+
unlocks: 'the pairing — an example answer with no input teaches the shape and not the mapping',
|
|
2736
|
+
},
|
|
2737
|
+
'failure-modes': {
|
|
2738
|
+
question: 'What has gone wrong with this before?',
|
|
2739
|
+
unlocks: 'the corrections worth stating, which are the ones a generic prompt never has',
|
|
2740
|
+
},
|
|
2741
|
+
model: {
|
|
2742
|
+
question: 'Which model is this for?',
|
|
2743
|
+
unlocks: 'the cost estimate — this changes the report and never the prompt',
|
|
2744
|
+
},
|
|
2745
|
+
budget: {
|
|
2746
|
+
question: 'What is the monthly ceiling for this prompt?',
|
|
2747
|
+
unlocks: 'the budget check — this changes the report and never the prompt',
|
|
2748
|
+
},
|
|
2749
|
+
},
|
|
2750
|
+
missing: (count) =>
|
|
2751
|
+
count === 1
|
|
2752
|
+
? 'One answer is still needed before a prompt can be written:'
|
|
2753
|
+
: `${count} answers are still needed before a prompt can be written:`,
|
|
2754
|
+
done: () => 'Nothing left worth asking.',
|
|
2755
|
+
answersNotAnObject: (path) =>
|
|
2756
|
+
`${path} must hold a JSON object of slot ids and answers.`,
|
|
2757
|
+
unknownSlot: (id, nearest) =>
|
|
2758
|
+
nearest === null
|
|
2759
|
+
? `"${id}" is not a slot. Run "trazum write" with no --answers to be asked them.`
|
|
2760
|
+
: `"${id}" is not a slot. Did you mean "${nearest}"?`,
|
|
2761
|
+
answerNotText: (id) => `"${id}" must be a string, or null to decline it.`,
|
|
2762
|
+
tokens: (count) => `${count} tokens`,
|
|
2763
|
+
monthly: (usd) => `${usd} per month, estimated — nobody has sent this prompt yet`,
|
|
2764
|
+
budget: (verdict, limit) =>
|
|
2765
|
+
verdict === 'over' ? `over the budget of ${limit}` : `within the budget of ${limit}`,
|
|
2766
|
+
noVerdict: (reason) =>
|
|
2767
|
+
reason === 'no-budget'
|
|
2768
|
+
? 'No budget answered, so there is nothing to check it against.'
|
|
2769
|
+
: reason === 'no-model'
|
|
2770
|
+
? 'No model answered, so it cannot be priced.'
|
|
2771
|
+
: 'That model is not in the price catalogue, so it cannot be priced.',
|
|
2772
|
+
clean: () => 'trazum optimize recovers nothing from this.',
|
|
2773
|
+
notClean: (rules, tokens) =>
|
|
2774
|
+
`trazum optimize would still recover ${tokens} tokens here (${rules}) — from your answers, not from the structure.`,
|
|
2775
|
+
declined: (ids) => `Declined, and left out: ${ids}`,
|
|
2776
|
+
},
|
|
2679
2777
|
};
|
package/src/i18n/es.ts
CHANGED
|
@@ -45,6 +45,7 @@ ${bold('USO')}
|
|
|
45
45
|
trazum conform <fichero|-> [--contract <nombre>]
|
|
46
46
|
trazum rollup <documento...|dir> [--json]
|
|
47
47
|
trazum pulse [--max-stale-hours <n>]
|
|
48
|
+
trazum write [--answers <fichero>] [--json] [-o <fichero>]
|
|
48
49
|
trazum models
|
|
49
50
|
trazum rules [--measure <dir>] [--level <safe|aggressive>]
|
|
50
51
|
trazum gateway <provider> --on-cannot-tell <fail-open|fail-closed>
|
|
@@ -105,6 +106,16 @@ ${bold('OPCIONES DE rollup')}
|
|
|
105
106
|
|
|
106
107
|
Sale con 1 cuando una contribución se entregó y no se pudo agregar.
|
|
107
108
|
|
|
109
|
+
${bold('OPCIONES DE write')}
|
|
110
|
+
--answers <fichero> Un objeto JSON de ids de slot y respuestas. Sin
|
|
111
|
+
esto, las preguntas se hacen una a una; una línea
|
|
112
|
+
vacía declina una, que también es una respuesta.
|
|
113
|
+
--json El documento prompt-draft, negativas incluidas.
|
|
114
|
+
-o, --out <fichero> Dónde escribir el prompt. Por defecto stdout, con
|
|
115
|
+
todo lo demás en stderr.
|
|
116
|
+
--calls <n> Llamadas al mes, para la estimación.
|
|
117
|
+
--avg-output <n> Tokens de salida por llamada, para la estimación.
|
|
118
|
+
|
|
108
119
|
${bold('OPCIONES DE pulse')}
|
|
109
120
|
--max-stale-hours <n> Sale con 1 cuando algo que corre aquí lleva más
|
|
110
121
|
de n horas sin correr. Sin esto, se informan las
|
|
@@ -2684,4 +2695,91 @@ ${bold('EJEMPLOS')}
|
|
|
2684
2695
|
moneyIncomparablePricing: (was, now) =>
|
|
2685
2696
|
`Los precios se revisaron el ${was} cuando se registr\u00f3 la l\u00ednea base y el ${now} ahora, as\u00ed que las cifras mensuales no son la misma medida. La comparaci\u00f3n de tokens no se ve afectada.`,
|
|
2686
2697
|
},
|
|
2698
|
+
|
|
2699
|
+
write: {
|
|
2700
|
+
slots: {
|
|
2701
|
+
task: {
|
|
2702
|
+
question: '¿Qué debe hacer el modelo, en una frase?',
|
|
2703
|
+
unlocks: 'el prompt entero — sin esto no hay nada que escribir',
|
|
2704
|
+
},
|
|
2705
|
+
role: {
|
|
2706
|
+
question: '¿Quién es el modelo mientras lo hace?',
|
|
2707
|
+
unlocks: 'la postura de la respuesta; si falta, el modelo elige una por ti',
|
|
2708
|
+
},
|
|
2709
|
+
inputs: {
|
|
2710
|
+
question: '¿Qué cambia de una llamada a otra?',
|
|
2711
|
+
unlocks: 'la parte variable — sin ella el prompt fija un solo caso y hay que reescribirlo para el siguiente',
|
|
2712
|
+
},
|
|
2713
|
+
'output-shape': {
|
|
2714
|
+
question: '¿Qué debe volver: prosa, json, lista o tabla?',
|
|
2715
|
+
unlocks: 'el contrato de salida, y si un consumidor puede parsear la respuesta siquiera',
|
|
2716
|
+
},
|
|
2717
|
+
'output-schema': {
|
|
2718
|
+
question: '¿Qué campos o columnas, y cuáles están siempre?',
|
|
2719
|
+
unlocks: 'nombres de campo en los que un consumidor puede confiar en vez de inferirlos de una muestra',
|
|
2720
|
+
},
|
|
2721
|
+
'output-length': {
|
|
2722
|
+
question: '¿Cuánto debe ocupar la respuesta, como máximo?',
|
|
2723
|
+
unlocks: 'el techo que evita pagar por texto que nadie lee',
|
|
2724
|
+
},
|
|
2725
|
+
audience: {
|
|
2726
|
+
question: '¿Quién lee la salida?',
|
|
2727
|
+
unlocks: 'el registro — una respuesta para un ingeniero y otra para un cliente no son la misma respuesta',
|
|
2728
|
+
},
|
|
2729
|
+
constraints: {
|
|
2730
|
+
question: '¿Qué no debe hacer nunca?',
|
|
2731
|
+
unlocks: 'las prohibiciones, dichas una vez aquí en vez de descubiertas incidente a incidente',
|
|
2732
|
+
},
|
|
2733
|
+
refusal: {
|
|
2734
|
+
question: '¿Qué debe hacer cuando no pueda responder?',
|
|
2735
|
+
unlocks: 'una negativa que llega con su motivo en vez de una conjetura segura de sí misma',
|
|
2736
|
+
},
|
|
2737
|
+
examples: {
|
|
2738
|
+
question: '¿Hay algún ejemplo de respuesta buena?',
|
|
2739
|
+
unlocks: 'la guía few-shot, y la ocasión de comprobar que no se repite',
|
|
2740
|
+
},
|
|
2741
|
+
'example-inputs': {
|
|
2742
|
+
question: '¿Qué entrada produjo ese ejemplo?',
|
|
2743
|
+
unlocks: 'el emparejamiento — un ejemplo sin su entrada enseña la forma, no la correspondencia',
|
|
2744
|
+
},
|
|
2745
|
+
'failure-modes': {
|
|
2746
|
+
question: '¿Qué ha salido mal con esto antes?',
|
|
2747
|
+
unlocks: 'las correcciones que merecen decirse, que son justo las que un prompt genérico nunca tiene',
|
|
2748
|
+
},
|
|
2749
|
+
model: {
|
|
2750
|
+
question: '¿Para qué modelo es esto?',
|
|
2751
|
+
unlocks: 'la estimación de coste — esto cambia el informe, nunca el prompt',
|
|
2752
|
+
},
|
|
2753
|
+
budget: {
|
|
2754
|
+
question: '¿Cuál es el techo mensual de este prompt?',
|
|
2755
|
+
unlocks: 'la comprobación de presupuesto — esto cambia el informe, nunca el prompt',
|
|
2756
|
+
},
|
|
2757
|
+
},
|
|
2758
|
+
missing: (count) =>
|
|
2759
|
+
count === 1
|
|
2760
|
+
? 'Falta una respuesta antes de poder escribir un prompt:'
|
|
2761
|
+
: `Faltan ${count} respuestas antes de poder escribir un prompt:`,
|
|
2762
|
+
done: () => 'No queda nada que merezca preguntarse.',
|
|
2763
|
+
answersNotAnObject: (path) =>
|
|
2764
|
+
`${path} debe contener un objeto JSON de ids de slot y respuestas.`,
|
|
2765
|
+
unknownSlot: (id, nearest) =>
|
|
2766
|
+
nearest === null
|
|
2767
|
+
? `"${id}" no es un slot. Ejecuta "trazum write" sin --answers para que te los pregunte.`
|
|
2768
|
+
: `"${id}" no es un slot. ¿Querías decir "${nearest}"?`,
|
|
2769
|
+
answerNotText: (id) => `"${id}" debe ser una cadena, o null para declinarlo.`,
|
|
2770
|
+
tokens: (count) => `${count} tokens`,
|
|
2771
|
+
monthly: (usd) => `${usd} al mes, estimado — nadie ha enviado este prompt todavía`,
|
|
2772
|
+
budget: (verdict, limit) =>
|
|
2773
|
+
verdict === 'over' ? `por encima del presupuesto de ${limit}` : `dentro del presupuesto de ${limit}`,
|
|
2774
|
+
noVerdict: (reason) =>
|
|
2775
|
+
reason === 'no-budget'
|
|
2776
|
+
? 'No hay presupuesto respondido, así que no hay contra qué comprobarlo.'
|
|
2777
|
+
: reason === 'no-model'
|
|
2778
|
+
? 'No hay modelo respondido, así que no se puede tasar.'
|
|
2779
|
+
: 'Ese modelo no está en el catálogo de precios, así que no se puede tasar.',
|
|
2780
|
+
clean: () => 'trazum optimize no recupera nada de esto.',
|
|
2781
|
+
notClean: (rules, tokens) =>
|
|
2782
|
+
`trazum optimize aún recuperaría ${tokens} tokens aquí (${rules}) — de tus respuestas, no de la estructura.`,
|
|
2783
|
+
declined: (ids) => `Declinados, y fuera: ${ids}`,
|
|
2784
|
+
},
|
|
2687
2785
|
};
|
package/src/i18n/types.ts
CHANGED
|
@@ -27,6 +27,18 @@ export interface HelpDefaults {
|
|
|
27
27
|
contracts: readonly string[];
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
/**
|
|
31
|
+
* One question, and what is lost by declining it.
|
|
32
|
+
*
|
|
33
|
+
* `unlocks` is not decoration. A required slot left unanswered stops the
|
|
34
|
+
* assembly, and a refusal never arrives bare — whatever cannot be built is
|
|
35
|
+
* reported with this sentence beside the slot that would have unblocked it.
|
|
36
|
+
*/
|
|
37
|
+
export interface SlotCopy {
|
|
38
|
+
question: string;
|
|
39
|
+
unlocks: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
30
42
|
export interface CliMessages {
|
|
31
43
|
locale: Locale;
|
|
32
44
|
/** BCP 47 tag used to format numbers. */
|
|
@@ -34,6 +46,33 @@ export interface CliMessages {
|
|
|
34
46
|
|
|
35
47
|
help(defaults: HelpDefaults, bold: (s: string) => string): string;
|
|
36
48
|
|
|
49
|
+
/**
|
|
50
|
+
* The interview behind `trazum write`, one entry per slot in
|
|
51
|
+
* `@trazum/core`'s catalogue.
|
|
52
|
+
*
|
|
53
|
+
* Keyed by slot id and held to the catalogue in both directions: a slot with
|
|
54
|
+
* no entry here fails, and an entry for a slot that does not exist fails too.
|
|
55
|
+
* The words are the product in this command, so a missing one is not a
|
|
56
|
+
* cosmetic gap — it is a question nobody can be asked.
|
|
57
|
+
*/
|
|
58
|
+
write: {
|
|
59
|
+
slots: Readonly<Record<string, SlotCopy>>;
|
|
60
|
+
/** Printed when required slots are unanswered, with each named below it. */
|
|
61
|
+
missing: (count: number) => string;
|
|
62
|
+
/** Printed when there is nothing left worth asking. */
|
|
63
|
+
done: () => string;
|
|
64
|
+
answersNotAnObject: (path: string) => string;
|
|
65
|
+
unknownSlot: (id: string, nearest: string | null) => string;
|
|
66
|
+
answerNotText: (id: string) => string;
|
|
67
|
+
tokens: (count: string) => string;
|
|
68
|
+
monthly: (usd: string) => string;
|
|
69
|
+
budget: (verdict: 'within' | 'over', limit: string) => string;
|
|
70
|
+
noVerdict: (reason: 'no-budget' | 'no-model' | 'model-unpriced') => string;
|
|
71
|
+
clean: () => string;
|
|
72
|
+
notClean: (rules: string, tokens: string) => string;
|
|
73
|
+
declined: (ids: string) => string;
|
|
74
|
+
};
|
|
75
|
+
|
|
37
76
|
/** The on-disk cache of model answers for `--suggest`. */
|
|
38
77
|
cache: {
|
|
39
78
|
cleared(entries: number, bytes: number, dir: string): string;
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { readFileSync } from 'node:fs';
|
|
3
3
|
import { open, readdir, readFile, stat, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { createInterface } from 'node:readline/promises';
|
|
4
5
|
import { dirname, join, resolve as resolvePath } from 'node:path';
|
|
5
6
|
import { fileURLToPath } from 'node:url';
|
|
6
7
|
import { gunzipSync } from 'node:zlib';
|
|
@@ -91,7 +92,11 @@ import {
|
|
|
91
92
|
MAX_BASELINE_BYTES,
|
|
92
93
|
moneyIsComparable,
|
|
93
94
|
mostSpecificMatch,
|
|
95
|
+
assemble,
|
|
96
|
+
interview,
|
|
94
97
|
nearestName,
|
|
98
|
+
slot,
|
|
99
|
+
SLOT_IDS,
|
|
95
100
|
optimize,
|
|
96
101
|
parseBaseline,
|
|
97
102
|
PHRASE_LANGUAGES,
|
|
@@ -265,6 +270,9 @@ interface Args {
|
|
|
265
270
|
}
|
|
266
271
|
|
|
267
272
|
const VALUE_FLAGS = new Set([
|
|
273
|
+
'answers',
|
|
274
|
+
'calls',
|
|
275
|
+
'avg-output',
|
|
268
276
|
'a',
|
|
269
277
|
'at',
|
|
270
278
|
'year',
|
|
@@ -603,6 +611,7 @@ const COMMAND_FLAGS: Record<string, string[]> = {
|
|
|
603
611
|
conform: ['contract', 'json'],
|
|
604
612
|
rollup: ['json'],
|
|
605
613
|
pulse: ['json', 'max-stale-hours'],
|
|
614
|
+
write: ['answers', 'json', 'out', 'o', 'calls', 'avg-output'],
|
|
606
615
|
feedback: [],
|
|
607
616
|
gateway: ['on-cannot-tell', 'port', 'socket', 'pricing', 'pricing-live'],
|
|
608
617
|
ladder: ['pricing', 'pricing-live', 'since', 'until', 'label'],
|
|
@@ -2329,6 +2338,149 @@ async function commandRollup(args: Args, t: CliMessages): Promise<void> {
|
|
|
2329
2338
|
* the measurements reach — that is a provider reporting on its own schedule,
|
|
2330
2339
|
* not a job that failed.
|
|
2331
2340
|
*/
|
|
2341
|
+
/**
|
|
2342
|
+
* `trazum write` — the interview, on a terminal.
|
|
2343
|
+
*
|
|
2344
|
+
* Two ways in, and the same document out. Interactive, it asks the open slots
|
|
2345
|
+
* one at a time and takes an empty line as a decline. With `--answers`, it
|
|
2346
|
+
* reads a JSON object of slot ids and asks nothing, which is what a script or
|
|
2347
|
+
* a second run needs.
|
|
2348
|
+
*
|
|
2349
|
+
* **The prompt goes to stdout and everything else to stderr**, so
|
|
2350
|
+
* `trazum write --answers a.json > prompt.txt` is a file with a prompt in it
|
|
2351
|
+
* and not a file with an interview in it.
|
|
2352
|
+
*/
|
|
2353
|
+
async function commandWrite(args: Args, t: CliMessages): Promise<void> {
|
|
2354
|
+
const n = (value: number): string => value.toLocaleString(t.numberLocale);
|
|
2355
|
+
const answersPath = stringFlag(args, 'answers');
|
|
2356
|
+
let answers: Record<string, string | null> = {};
|
|
2357
|
+
|
|
2358
|
+
if (answersPath !== undefined) {
|
|
2359
|
+
const parsed: unknown = JSON.parse(await readFile(answersPath, 'utf8'));
|
|
2360
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
2361
|
+
throw new Error(t.write.answersNotAnObject(answersPath));
|
|
2362
|
+
}
|
|
2363
|
+
for (const [id, value] of Object.entries(parsed as Record<string, unknown>)) {
|
|
2364
|
+
if (slot(id) === undefined) {
|
|
2365
|
+
const nearest = nearestName(id, [...SLOT_IDS]);
|
|
2366
|
+
throw new Error(t.write.unknownSlot(id, nearest));
|
|
2367
|
+
}
|
|
2368
|
+
if (value !== null && typeof value !== 'string') throw new Error(t.write.answerNotText(id));
|
|
2369
|
+
answers[id] = value as string | null;
|
|
2370
|
+
}
|
|
2371
|
+
} else {
|
|
2372
|
+
/**
|
|
2373
|
+
* Interactive on a terminal, and a list of lines when it is not.
|
|
2374
|
+
*
|
|
2375
|
+
* An empty line is a **decline**, which is an answer: it closes the
|
|
2376
|
+
* follow-up a real one would have opened. Input running out is not a
|
|
2377
|
+
* decline — the remaining slots stay unasked and the refusal below names
|
|
2378
|
+
* them.
|
|
2379
|
+
*
|
|
2380
|
+
* The two paths exist because `readline` on a piped stream closes as soon
|
|
2381
|
+
* as the buffer drains, and a question asked after that never settles: the
|
|
2382
|
+
* event loop empties and the process leaves with status 0 and nothing
|
|
2383
|
+
* printed, which is an interview that stopped halfway and reported
|
|
2384
|
+
* success. A script piping answers is really handing over an ordered list,
|
|
2385
|
+
* so that is what this reads.
|
|
2386
|
+
*/
|
|
2387
|
+
let next: () => Promise<string | null>;
|
|
2388
|
+
let close = () => {};
|
|
2389
|
+
|
|
2390
|
+
if (process.stdin.isTTY) {
|
|
2391
|
+
const rl = createInterface({ input: process.stdin, output: process.stderr });
|
|
2392
|
+
next = async () => {
|
|
2393
|
+
try {
|
|
2394
|
+
return await rl.question('> ');
|
|
2395
|
+
} catch {
|
|
2396
|
+
return null;
|
|
2397
|
+
}
|
|
2398
|
+
};
|
|
2399
|
+
close = () => rl.close();
|
|
2400
|
+
} else {
|
|
2401
|
+
const piped: string[] = [];
|
|
2402
|
+
for await (const chunk of process.stdin) piped.push(String(chunk));
|
|
2403
|
+
const lines = piped.join('').split('\n');
|
|
2404
|
+
// A trailing newline is the end of the last answer, not an extra one.
|
|
2405
|
+
if (lines.length > 0 && lines[lines.length - 1] === '') lines.pop();
|
|
2406
|
+
let at = 0;
|
|
2407
|
+
next = async () => (at < lines.length ? (lines[at++] as string) : null);
|
|
2408
|
+
}
|
|
2409
|
+
|
|
2410
|
+
let ended = false;
|
|
2411
|
+
try {
|
|
2412
|
+
for (;;) {
|
|
2413
|
+
const state = interview(answers);
|
|
2414
|
+
if (state.done) break;
|
|
2415
|
+
const id = state.next as string;
|
|
2416
|
+
const copy = t.write.slots[id] as { question: string; unlocks: string };
|
|
2417
|
+
process.stderr.write(`${copy.question}\n`);
|
|
2418
|
+
const typed = await next();
|
|
2419
|
+
if (typed === null) {
|
|
2420
|
+
ended = true;
|
|
2421
|
+
break;
|
|
2422
|
+
}
|
|
2423
|
+
answers = { ...answers, [id]: typed.trim().length > 0 ? typed.trim() : null };
|
|
2424
|
+
}
|
|
2425
|
+
} finally {
|
|
2426
|
+
close();
|
|
2427
|
+
}
|
|
2428
|
+
if (!ended) console.error(t.write.done());
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
const draft = assemble(answers, {
|
|
2432
|
+
callsPerMonth: numberFlag(args, 'calls', Number.NaN, t) || undefined,
|
|
2433
|
+
avgOutputTokens: numberFlag(args, 'avg-output', Number.NaN, t) || undefined,
|
|
2434
|
+
});
|
|
2435
|
+
|
|
2436
|
+
if (boolFlag(args, 'json')) {
|
|
2437
|
+
console.log(JSON.stringify(draft, null, 2));
|
|
2438
|
+
if (draft.prompt === null) process.exitCode = 1;
|
|
2439
|
+
return;
|
|
2440
|
+
}
|
|
2441
|
+
|
|
2442
|
+
if (draft.prompt === null) {
|
|
2443
|
+
// A refusal never arrives bare: each missing slot with what it unlocks.
|
|
2444
|
+
console.error(c.red(t.write.missing(draft.missing.length)));
|
|
2445
|
+
for (const id of draft.missing) {
|
|
2446
|
+
console.error(` ${c.bold(id)} — ${(t.write.slots[id] as { unlocks: string }).unlocks}`);
|
|
2447
|
+
}
|
|
2448
|
+
process.exitCode = 1;
|
|
2449
|
+
return;
|
|
2450
|
+
}
|
|
2451
|
+
|
|
2452
|
+
/*
|
|
2453
|
+
`out`, not `o`.
|
|
2454
|
+
|
|
2455
|
+
The parser normalises `-o` to `out` on the way in, so `stringFlag(args, 'o')`
|
|
2456
|
+
can never match anything: reading it meant `-o` was accepted, ignored, and
|
|
2457
|
+
the prompt went to stdout instead of the file somebody named. A flag that
|
|
2458
|
+
parses and does nothing is the defect this CLI already refuses one layer up
|
|
2459
|
+
with "Did you mean --max-growth?".
|
|
2460
|
+
*/
|
|
2461
|
+
const out = stringFlag(args, 'out');
|
|
2462
|
+
if (out !== undefined) await writeFile(out, `${draft.prompt}\n`, 'utf8');
|
|
2463
|
+
else console.log(draft.prompt);
|
|
2464
|
+
|
|
2465
|
+
const m = draft.measured;
|
|
2466
|
+
if (m !== null) {
|
|
2467
|
+
console.error('');
|
|
2468
|
+
console.error(t.write.tokens(n(m.cheap.tokens)));
|
|
2469
|
+
if (m.cheap.monthlyUsd !== null) console.error(t.write.monthly(formatUsd(m.cheap.monthlyUsd)));
|
|
2470
|
+
if (m.cheap.verdict !== 'cannot-tell') {
|
|
2471
|
+
console.error(t.write.budget(m.cheap.verdict, formatUsd(m.cheap.budgetUsd as number)));
|
|
2472
|
+
} else if (m.cheap.reason !== null) {
|
|
2473
|
+
console.error(t.write.noVerdict(m.cheap.reason));
|
|
2474
|
+
}
|
|
2475
|
+
console.error(
|
|
2476
|
+
m.clean.rules.length === 0
|
|
2477
|
+
? t.write.clean()
|
|
2478
|
+
: t.write.notClean(m.clean.rules.map((rule) => rule.id).join(', '), n(m.clean.tokensRecoverable)),
|
|
2479
|
+
);
|
|
2480
|
+
if (m.complete.declined.length > 0) console.error(t.write.declined(m.complete.declined.join(', ')));
|
|
2481
|
+
}
|
|
2482
|
+
}
|
|
2483
|
+
|
|
2332
2484
|
async function commandPulse(args: Args, t: CliMessages): Promise<void> {
|
|
2333
2485
|
const root = process.cwd();
|
|
2334
2486
|
const maxStaleHours = numberFlag(args, 'max-stale-hours', Number.NaN, t);
|
|
@@ -8422,7 +8574,10 @@ async function commandBaseline(
|
|
|
8422
8574
|
files,
|
|
8423
8575
|
};
|
|
8424
8576
|
|
|
8425
|
-
|
|
8577
|
+
// `?? stringFlag(args, 'o')` used to sit here and could never fire: the
|
|
8578
|
+
// parser rewrites `-o` to `out`, so the key `o` is never set. Removed rather
|
|
8579
|
+
// than left as a fallback nobody can reach.
|
|
8580
|
+
const out = stringFlag(args, 'out') ?? config.baseline?.path ?? BASELINE_FILENAME;
|
|
8426
8581
|
await writeFile(out, formatBaseline(document), 'utf8');
|
|
8427
8582
|
|
|
8428
8583
|
if (boolFlag(args, 'json')) {
|
|
@@ -9996,6 +10151,9 @@ async function main(): Promise<void> {
|
|
|
9996
10151
|
case 'pulse':
|
|
9997
10152
|
await commandPulse(args, t);
|
|
9998
10153
|
break;
|
|
10154
|
+
case 'write':
|
|
10155
|
+
await commandWrite(args, t);
|
|
10156
|
+
break;
|
|
9999
10157
|
case 'init':
|
|
10000
10158
|
await commandInit(args, config, pricing, t);
|
|
10001
10159
|
break;
|