@sous-io/sous-plugin-api 0.0.0-stage → 1.0.19

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.
@@ -0,0 +1,60 @@
1
+ /**
2
+ * The kinds of entry a reporter shows, and the facts each one carries in `details`. A style says what
3
+ * an entry IS, never how it looks: each front end's reporter decides that. A new style is added here
4
+ * when an output needs one, together with its convenience method in `BaseReporter` (reporter.ts),
5
+ * which is the only way callers produce it.
6
+ *
7
+ * Every styled message also carries a plain-text `message`, so a reporter that ignores styles (a
8
+ * log file, a test buffer) still has something readable.
9
+ */
10
+ /** What kind of entry a reported message is. */
11
+ export const ReportEntryStyle = {
12
+ /** A paragraph of text, which a front end may wrap. `details` is optional data. */
13
+ Plain: "plain",
14
+ /** Text shown exactly as given: no wrapping, no color (a value printed for a script to read). No details. */
15
+ Text: "text",
16
+ /** An explanation that is neither a warning nor an error. No details. */
17
+ Note: "note",
18
+ /** The start of an operation, such as "Building the project". {@link OperationStartDetails}. */
19
+ OperationStart: "operation-start",
20
+ /** A step inside the current operation, such as one target being compiled. {@link StepStartDetails}. */
21
+ StepStart: "step-start",
22
+ /** One line of detail inside the current operation or step. No details. */
23
+ Detail: "detail",
24
+ /** One action done (or to be done) to one target, such as a file written. {@link OperationDetails}. */
25
+ Operation: "operation",
26
+ /** How an operation ended, such as "Done." {@link OperationEndDetails}. */
27
+ OperationEnd: "operation-end",
28
+ /** One label and its value. The message is a `VariableSet`. */
29
+ Variable: "variable",
30
+ /** Labels and values, read as one block, under an optional title. The message is a `VariableSet`. */
31
+ VariableList: "variable-list",
32
+ /** The facts a command runs with: the project, its config, ... The message is a `VariableSet`. */
33
+ CommandVariables: "command-variables",
34
+ /** A list of items, with an optional title and closing sentence. The message is an `ItemList`. */
35
+ List: "list",
36
+ /** A table of rows. The message is a `Table`. */
37
+ Table: "table",
38
+ /** Items nested under other items, such as each contributor and the changes it made. The message is a `Tree`. */
39
+ Tree: "tree",
40
+ /** How many things were found. {@link CountDetails}. */
41
+ Count: "count",
42
+ /** Says that a run is a dry run, or what a dry run left undone. No details. */
43
+ DryRun: "dry-run",
44
+ /** Something wrong, with its cause and what to do about it. {@link ProblemDetails}. */
45
+ Problem: "problem",
46
+ /** What a name on the command line resolved to, and why. {@link ResolvedReferenceFacts}. */
47
+ ResolvedReference: "resolved-reference",
48
+ /** The question about to be asked, and where its answer goes. {@link QuestionDetails}. */
49
+ Question: "question",
50
+ /** A command sous runs (or would run). {@link CommandDetails}. */
51
+ Command: "command",
52
+ /** Progress through a longer step. {@link ProgressEvent}. */
53
+ Progress: "progress",
54
+ /** A structured value, such as a command's result as data. The message is a `DataValue`. */
55
+ Data: "data",
56
+ /** The sous banner. No details. */
57
+ Banner: "banner",
58
+ /** A command's result: the message is `IntentOutputs`. */
59
+ Result: "result",
60
+ };
@@ -0,0 +1,270 @@
1
+ /**
2
+ * Reporting: services and intents never print. They report through the `Reporter` the front
3
+ * end handed over with the chain (`ctx.reporter`), and the front end decides how it looks.
4
+ *
5
+ * Every message goes through one method, `log(level, message, details?, style?)`. The level says how
6
+ * important it is, `details` carries its facts, and the style says what kind of entry it is (an
7
+ * operation, a labeled value, a table, ...). Each front end's reporter decides how each style looks:
8
+ * the CLI draws it with colors and alignment, MCP sends `details` as data. On top of `log` sit one
9
+ * method per level (`info`, `warn`, ...) and one or more methods per style (`operationStart`, ...),
10
+ * all defined once, in {@link BaseReporter}.
11
+ */
12
+ import { IntentOutputs, Payload } from "./outputs.js";
13
+ import { type CommandDetails, type OperationAction, type OperationDetails, type ProblemDetails, type ProgressEvent, type QuestionDetails, ReportEntryStyle, type ResolvedReferenceFacts, type TableColumn, type TreeNode, type VariableEntry } from "./report-styles.js";
14
+ export { ReportEntryStyle } from "./report-styles.js";
15
+ export type * from "./report-styles.js";
16
+ /** The eight syslog levels, by name. */
17
+ export declare const LogLevel: {
18
+ readonly Debug: "debug";
19
+ readonly Info: "info";
20
+ readonly Notice: "notice";
21
+ readonly Warning: "warning";
22
+ readonly Error: "error";
23
+ readonly Critical: "critical";
24
+ readonly Alert: "alert";
25
+ readonly Emergency: "emergency";
26
+ };
27
+ /** One of the eight syslog levels. */
28
+ export type LogLevel = (typeof LogLevel)[keyof typeof LogLevel];
29
+ /** The eight syslog levels, from least to most severe. */
30
+ export declare const LOG_LEVELS: readonly LogLevel[];
31
+ /** Whether a string names a log level. */
32
+ export declare function isLogLevel(value: unknown): value is LogLevel;
33
+ /**
34
+ * Whether a message at `level` shows when the threshold is `threshold`.
35
+ *
36
+ * ```ts
37
+ * isLevelShown("warning", "notice"); // true
38
+ * isLevelShown("debug", "info"); // false
39
+ * ```
40
+ */
41
+ export declare function isLevelShown(level: LogLevel, threshold: LogLevel): boolean;
42
+ /**
43
+ * What a reported message is, for people: text; an `Error`, shown with its whole cause chain; a
44
+ * payload (a `VariableSet`, a `Table`, ...); or, for a command's result, `IntentOutputs`.
45
+ */
46
+ export type ReportMessage = string | Error | Payload | IntentOutputs<object>;
47
+ /**
48
+ * The text of any message: an error's chain one cause per line, a payload's or outputs' text.
49
+ *
50
+ * ```ts
51
+ * messageText(new Error("Could not download", { cause: new Error("DNS failed") }));
52
+ * // "Could not download\nCaused by: DNS failed"
53
+ * ```
54
+ */
55
+ export declare function messageText(message: ReportMessage): string;
56
+ /** An error and every error under it through `cause`, outermost first (a cycle stops it). */
57
+ export declare function errorChain(error: Error): Error[];
58
+ /**
59
+ * The facts a message carries, which a front end may show or keep (MCP sends them as data). For a
60
+ * styled message, its style's details type (`OperationDetails` for `ReportEntryStyle.Operation`, ...).
61
+ */
62
+ export type ReportDetails = object;
63
+ /**
64
+ * The shared part of every reporter, and the ONE place every reporting method is defined.
65
+ * `Reporter`, the type plugins see, is derived from this class's public members, and the
66
+ * {@link Reporting} mixin copies its convenience methods onto other classes, so a method added here
67
+ * reaches every reporter and every class that mixes it in.
68
+ *
69
+ * A subclass writes a message that passes the threshold (`write`); everything else is shared.
70
+ *
71
+ * ```ts
72
+ * class LinesReporter extends BaseReporter {
73
+ * readonly lines: string[] = [];
74
+ *
75
+ * protected write(level: LogLevel, message: string): void {
76
+ * this.lines.push(`${level}: ${message}`);
77
+ * }
78
+ * }
79
+ *
80
+ * const reporter = new LinesReporter(LogLevel.Warning);
81
+ * reporter.notice("Skipped."); // below the threshold: dropped
82
+ * reporter.warn("Stale."); // lines: ["warning: Stale."]
83
+ * ```
84
+ */
85
+ export declare abstract class BaseReporter {
86
+ private readonly thresholdSource;
87
+ /**
88
+ * @param thresholdSource - The least severe level shown (default `debug`, everything). A function
89
+ * is read on every message, for a threshold that changes while the reporter is in use.
90
+ */
91
+ constructor(thresholdSource?: LogLevel | (() => LogLevel));
92
+ /** The least severe level shown now. */
93
+ protected threshold(): LogLevel;
94
+ /** Whether a message at `level` passes the threshold now. */
95
+ protected shows(level: LogLevel): boolean;
96
+ /** Writes one message that passed the threshold, in the way this reporter shows `style`. */
97
+ protected abstract write(level: LogLevel, message: ReportMessage, details: ReportDetails | undefined, style: ReportEntryStyle): void;
98
+ /**
99
+ * Reports one message. Every other reporting method ends here.
100
+ *
101
+ * @param style - What kind of entry it is (default `ReportEntryStyle.Plain`, a paragraph).
102
+ */
103
+ log(level: LogLevel, message: ReportMessage, details?: ReportDetails, style?: ReportEntryStyle): void;
104
+ /** Reports a message for whoever is debugging sous. */
105
+ debug(message: string | Error, details?: ReportDetails, style?: ReportEntryStyle): void;
106
+ /** Reports an ordinary message. */
107
+ info(message: string | Error, details?: ReportDetails, style?: ReportEntryStyle): void;
108
+ /** Reports a normal but significant event. */
109
+ notice(message: string | Error, details?: ReportDetails, style?: ReportEntryStyle): void;
110
+ /** Reports something that may need the user's attention. */
111
+ warning(message: string | Error, details?: ReportDetails, style?: ReportEntryStyle): void;
112
+ /** Shorthand for {@link warning}. */
113
+ warn(message: string | Error, details?: ReportDetails, style?: ReportEntryStyle): void;
114
+ /** Reports something that failed. */
115
+ error(message: string | Error, details?: ReportDetails, style?: ReportEntryStyle): void;
116
+ /** Reports a failure that stops the run. */
117
+ critical(message: string | Error, details?: ReportDetails, style?: ReportEntryStyle): void;
118
+ /** Reports something that must be acted on at once. */
119
+ alert(message: string | Error, details?: ReportDetails, style?: ReportEntryStyle): void;
120
+ /** Reports that sous cannot work at all. */
121
+ emergency(message: string | Error, details?: ReportDetails, style?: ReportEntryStyle): void;
122
+ /** Shorthand for {@link emergency}. */
123
+ emerg(message: string | Error, details?: ReportDetails, style?: ReportEntryStyle): void;
124
+ /** Text shown exactly as given, such as a value a script reads: no wrapping, no color. */
125
+ text(content: string, level?: LogLevel): void;
126
+ /** An explanation that is neither a warning nor an error. */
127
+ note(text: string, level?: LogLevel): void;
128
+ /** The start of an operation: `Building the project`. */
129
+ operationStart(operationName: string, level?: LogLevel): void;
130
+ /** A step inside the current operation: one target, one recipe, one check. */
131
+ stepStart(stepName: string, level?: LogLevel): void;
132
+ /** One line of detail inside the current operation or step: `Read 12 recipes.` */
133
+ detail(text: string, level?: LogLevel): void;
134
+ /**
135
+ * One action done (or, in a dry run, to be done) to one target. The level defaults to `error`
136
+ * for `failed` and `info` for everything else.
137
+ *
138
+ * ```ts
139
+ * reporter.operation("written", "/work/app/CLAUDE.md", { tokens: 1840 });
140
+ * reporter.operation("skipped", "/work/app/AGENTS.md", { note: 'left out by the transform "strip"' });
141
+ * ```
142
+ */
143
+ operation(action: OperationAction, target: string, facts?: Omit<OperationDetails, "action" | "target">, level?: LogLevel): void;
144
+ /** How an operation ended: `Done.`, or `Done with 2 errors.` The level defaults to `info`, or `error` when it failed. */
145
+ operationEnd(summary: string, ok?: boolean, level?: LogLevel): void;
146
+ /** One label and its value: `Repository: /work/recipes`. */
147
+ variable(label: string, value: unknown, detail?: string, level?: LogLevel): void;
148
+ /**
149
+ * Labels and values, read as one block (a terminal lines their colons up). A record is read as
150
+ * label and value pairs, in its key order.
151
+ */
152
+ variables(entries: readonly VariableEntry[] | Readonly<Record<string, unknown>>, title?: string, level?: LogLevel): void;
153
+ /** The facts a command runs with (its project, its config, what it reads), shown before its output. */
154
+ commandVariables(entries: readonly VariableEntry[] | Readonly<Record<string, unknown>>, level?: LogLevel): void;
155
+ /** A list of items, with an optional title before it and an optional sentence after it. */
156
+ list(items: readonly string[], options?: {
157
+ title?: string;
158
+ footer?: string;
159
+ }, level?: LogLevel): void;
160
+ /**
161
+ * A table of rows. With no rows, a front end shows `options.empty` when it is given;
162
+ * `options.rowNotes[i]` is a note shown under row `i`.
163
+ */
164
+ table(columns: readonly TableColumn[], rows: readonly Readonly<Record<string, unknown>>[], options?: {
165
+ title?: string;
166
+ empty?: string;
167
+ rowNotes?: readonly (string | undefined)[];
168
+ }, level?: LogLevel): void;
169
+ /** Items nested under other items, such as each contributor and the changes it made. */
170
+ tree(nodes: readonly TreeNode[], title?: string, level?: LogLevel): void;
171
+ /** How many things were found: `Found 5 recipes`. `entity` is already in the right number. */
172
+ count(count: number, entity: string, verb?: string, level?: LogLevel): void;
173
+ /** Says that a run is a dry run, or what a dry run left undone: `Nothing was written.` */
174
+ dryRun(text: string, level?: LogLevel): void;
175
+ /**
176
+ * Something wrong, with its cause and what to do about it. A one-sentence warning with nothing
177
+ * more to say is `warn(text)` instead.
178
+ *
179
+ * ```ts
180
+ * reporter.problem("The compilation transform \"strip\" is not registered.", { subject: "CLAUDE.md", hint: "Install the plugin that adds it." });
181
+ * ```
182
+ */
183
+ problem(summary: string, facts?: Omit<ProblemDetails, "summary">, level?: LogLevel): void;
184
+ /** What a name on the command line resolved to, and why (`reason`, one sentence). */
185
+ resolvedReference(reason: string, facts: ResolvedReferenceFacts, level?: LogLevel): void;
186
+ /** The question about to be asked, and where its answer goes. */
187
+ question(facts: QuestionDetails, level?: LogLevel): void;
188
+ /** A command sous runs, or (with `dryRun`) would run. */
189
+ command(command: string, args: readonly string[], facts?: Omit<CommandDetails, "command" | "args">, level?: LogLevel): void;
190
+ /**
191
+ * Progress through a longer step. A terminal draws one bar and redraws it in place; the event
192
+ * with `finished: true` ends it.
193
+ */
194
+ progress(event: ProgressEvent, level?: LogLevel): void;
195
+ /** A structured value shown as data (a config as JSON). */
196
+ data(value: unknown, level?: LogLevel): void;
197
+ /**
198
+ * A command's result: what its intent returned. A terminal sends it to standard output, and
199
+ * everything else to standard error.
200
+ */
201
+ result(outputs: IntentOutputs<object>, level?: LogLevel): void;
202
+ /** The sous banner. */
203
+ banner(level?: LogLevel): void;
204
+ }
205
+ /**
206
+ * Where a chain's output goes: every public method of {@link BaseReporter}. Each front end supplies
207
+ * its own (the CLI writes to the terminal, MCP sends log notifications) by extending `BaseReporter`.
208
+ */
209
+ export type Reporter = {
210
+ [K in keyof BaseReporter]: BaseReporter[K];
211
+ };
212
+ /** The reporting methods {@link Reporting} copies onto a class: every one except `log`. */
213
+ export type ReportingMethods = Omit<Reporter, "log">;
214
+ /** A class, abstract or not. */
215
+ export type AbstractConstructor<T = object> = abstract new (...args: any[]) => T;
216
+ /**
217
+ * Gives a class every reporting method of {@link BaseReporter} (`info`, `warn`, `operationStart`,
218
+ * ...) as a shorthand for the same method on its `reporter`, which the class provides. The methods
219
+ * are copied from `BaseReporter.prototype` when the mixin is applied, so nothing lists them twice.
220
+ * They are public, since a type can only make a member protected inside a class body.
221
+ *
222
+ * ```ts
223
+ * class Compiler extends Reporting(Object) {
224
+ * constructor(private readonly deps: { reporter: Reporter }) {
225
+ * super();
226
+ * }
227
+ *
228
+ * protected get reporter(): Reporter {
229
+ * return this.deps.reporter;
230
+ * }
231
+ *
232
+ * run(): void {
233
+ * this.info("Compiling"); // this.deps.reporter.info("Compiling")
234
+ * }
235
+ * }
236
+ * ```
237
+ */
238
+ export declare function Reporting<TBase extends AbstractConstructor>(Base: TBase): TBase & AbstractConstructor<ReportingMethods>;
239
+ /** One message a {@link BufferedReporter} kept. */
240
+ export interface ReportedMessage {
241
+ level: LogLevel;
242
+ message: ReportMessage;
243
+ details?: ReportDetails;
244
+ style: ReportEntryStyle;
245
+ }
246
+ /**
247
+ * Builds a reporter from a function that receives every message, whatever its level.
248
+ *
249
+ * ```ts
250
+ * const reporter = createReporter((level, message) => lines.push(`${level}: ${message}`));
251
+ * reporter.warn("Recipe X declares a variable no template reads.");
252
+ * ```
253
+ */
254
+ export declare function createReporter(log: (level: LogLevel, message: ReportMessage, details: ReportDetails | undefined, style: ReportEntryStyle) => void): Reporter;
255
+ /** A reporter that drops everything. */
256
+ export declare const nullReporter: Reporter;
257
+ /**
258
+ * A reporter that keeps every message, to hand on later ({@link flushTo}) or to read in tests.
259
+ */
260
+ export declare class BufferedReporter extends BaseReporter {
261
+ /** Every message, in order. */
262
+ readonly messages: ReportedMessage[];
263
+ protected write(level: LogLevel, message: ReportMessage, details: ReportDetails | undefined, style: ReportEntryStyle): void;
264
+ /** The text of the messages at a level ({@link messageText}). */
265
+ at(level: LogLevel): string[];
266
+ /** The messages of a style. */
267
+ styled(style: ReportEntryStyle): ReportedMessage[];
268
+ /** Sends every kept message to another reporter, in order, and forgets them. */
269
+ flushTo(reporter: Reporter): void;
270
+ }