@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.
- package/README.md +97 -2
- package/dist/command-variables.d.ts +13 -0
- package/dist/command-variables.js +28 -0
- package/dist/config.d.ts +235 -0
- package/dist/config.js +1 -0
- package/dist/context.d.ts +159 -0
- package/dist/context.js +1 -0
- package/dist/entities.d.ts +60 -0
- package/dist/entities.js +1 -0
- package/dist/errors.d.ts +111 -0
- package/dist/errors.js +138 -0
- package/dist/events.d.ts +90 -0
- package/dist/events.js +1 -0
- package/dist/handlers.d.ts +47 -0
- package/dist/handlers.js +16 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +15 -0
- package/dist/intents.d.ts +158 -0
- package/dist/intents.js +1 -0
- package/dist/json.d.ts +84 -0
- package/dist/json.js +7 -0
- package/dist/node.d.ts +81 -0
- package/dist/node.js +1 -0
- package/dist/outputs.d.ts +199 -0
- package/dist/outputs.js +292 -0
- package/dist/plugin.d.ts +181 -0
- package/dist/plugin.js +18 -0
- package/dist/prompter.d.ts +66 -0
- package/dist/prompter.js +32 -0
- package/dist/registry.d.ts +91 -0
- package/dist/registry.js +1 -0
- package/dist/report-styles.d.ts +221 -0
- package/dist/report-styles.js +60 -0
- package/dist/reporter.d.ts +270 -0
- package/dist/reporter.js +392 -0
- package/dist/schema.d.ts +131 -0
- package/dist/schema.js +211 -0
- package/dist/services.d.ts +68 -0
- package/dist/services.js +1 -0
- package/dist/timing.d.ts +31 -0
- package/dist/timing.js +22 -0
- package/package.json +34 -4
package/README.md
CHANGED
|
@@ -1,3 +1,98 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @sous-io/sous-plugin-api
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The interface for writing a sous plugin, and the one package of sous that outside plugins depend on. It holds `definePlugin`, the types, the `Reporter` and `Prompter` interfaces and the error classes. It has no dependencies.
|
|
4
|
+
|
|
5
|
+
## Write your first plugin
|
|
6
|
+
|
|
7
|
+
A plugin is a module whose default export is `definePlugin({...})`. Its `register(sous)` runs once, when the plugin loads. Everything below goes inside it.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { definePlugin } from "@sous-io/sous-plugin-api";
|
|
11
|
+
|
|
12
|
+
export default definePlugin({
|
|
13
|
+
name: "sous-plugin-diagrams",
|
|
14
|
+
version: "1.0.0",
|
|
15
|
+
dependsOn: { "@sous-io/sous-plugin-skills": "^1.0.0" },
|
|
16
|
+
|
|
17
|
+
register(sous) {
|
|
18
|
+
// 1. A config contributor: adds to the config while it is built.
|
|
19
|
+
sous.config.contribute({
|
|
20
|
+
id: "diagrams",
|
|
21
|
+
contribute(config) {
|
|
22
|
+
if (config.atPath("diagrams.enabled").valueOr(true)) {
|
|
23
|
+
config.atPath("compilation.targets").appendValue({ id: "diagrams" });
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// 2. An intent: a step with typed input. The CLI command and the MCP tool are generated from it.
|
|
29
|
+
sous.intents.add({
|
|
30
|
+
intent: "diagrams.render",
|
|
31
|
+
summary: "Render every diagram",
|
|
32
|
+
handle: async (input, ctx) => {
|
|
33
|
+
ctx.reporter.info("Rendering");
|
|
34
|
+
return { rendered: 0 };
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// 3. A listener: runs when an event fires.
|
|
39
|
+
sous.on("config.built", (event, ctx) => {
|
|
40
|
+
ctx.reporter.debug("Config built");
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
A contributor re-runs when something it read changes, so it must give the same result for the same input.
|
|
47
|
+
|
|
48
|
+
### Which contributor wins
|
|
49
|
+
|
|
50
|
+
When two contributors set the same key, the one whose patch applies last wins. Patches apply by `applyOrder`, a whole number from 0 to 99999 (default 2000): lower numbers apply first, and equal numbers apply in id order. The user's config files use 89900 and up, so they apply after every plugin and win.
|
|
51
|
+
|
|
52
|
+
`before` and `after` name contributors, plugins or wildcards (`"*"` means every other contributor). They win over the number: a contributor declared `after` another is moved to one past it, and the move is logged at the `debug` level. `sous config show --layers` shows each contributor's number, and the one it moved to.
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
sous.config.contribute({
|
|
56
|
+
id: "diagrams/fixups",
|
|
57
|
+
applyOrder: 3000, // after plugins left at the default (2000)
|
|
58
|
+
after: ["@sous-io/sous-plugin-skills"], // and after every contributor of sous-plugin-skills, whatever its number
|
|
59
|
+
contribute(config) {
|
|
60
|
+
config.atPath("diagrams.enabled").setValue(true);
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Typed access to another plugin
|
|
66
|
+
|
|
67
|
+
Add your plugin's names to the interfaces in this package (declaration merging), and another plugin gets them typed. To use a plugin's names, import its types file with no runtime cost and list it in `dependsOn`:
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
// In sous-plugin-diagrams: describe what it provides.
|
|
71
|
+
declare module "@sous-io/sous-plugin-api" {
|
|
72
|
+
interface SousIntents {
|
|
73
|
+
"diagrams.render": { input: { only?: string }; result: { rendered: number } };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// In another plugin: the intent is now typed.
|
|
78
|
+
import type {} from "sous-plugin-diagrams/types";
|
|
79
|
+
|
|
80
|
+
const { rendered } = await ctx.run("diagrams.render", { only: "intro" });
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
`SousServices`, `SousEvents`, `SousEntities` and `SousConfig` work the same way, for `sous.services.get(name)`, event payloads, entity kinds and config keys.
|
|
84
|
+
|
|
85
|
+
## Where things are
|
|
86
|
+
|
|
87
|
+
- `src/plugin.ts`: `definePlugin` and the `sous` object `register` receives (everything a plugin can add).
|
|
88
|
+
- `src/config.ts`: contributors and the config builder they use.
|
|
89
|
+
- `src/intents.ts`, `src/events.ts`, `src/services.ts`, `src/entities.ts`: what each registry takes.
|
|
90
|
+
- `src/registry.ts`: the interfaces plugins add types to.
|
|
91
|
+
- `src/errors.ts`, `src/reporter.ts`, `src/prompter.ts`: the error classes, and how a plugin reports and asks.
|
|
92
|
+
- Real examples: any `packages/sous-plugin-*` directory, for instance `packages/sous-plugin-inspect`.
|
|
93
|
+
|
|
94
|
+
## Testing
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
pnpm --filter @sous-io/sous-plugin-api test
|
|
98
|
+
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Context } from "./context.js";
|
|
2
|
+
import { IntentOutput } from "./outputs.js";
|
|
3
|
+
/**
|
|
4
|
+
* The "Command Variables" output most commands open their result with: the project's name (the
|
|
5
|
+
* config's `name`, else its directory's name), its config file, then `extra` in its key order.
|
|
6
|
+
* Outside a project it holds only `extra`.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* return new IntentOutputs([commandVariables(ctx, { Reading: "the cached indexes" }), listing]);
|
|
10
|
+
* // Project: my-app / Config: /work/my-app/.sous/sous.config.js / Reading: the cached indexes
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare function commandVariables(ctx: Pick<Context, "project" | "config">, extra?: Readonly<Record<string, unknown>>): IntentOutput;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { IntentOutput, VariableSet } from "./outputs.js";
|
|
2
|
+
import { ReportEntryStyle } from "./report-styles.js";
|
|
3
|
+
/**
|
|
4
|
+
* The "Command Variables" output most commands open their result with: the project's name (the
|
|
5
|
+
* config's `name`, else its directory's name), its config file, then `extra` in its key order.
|
|
6
|
+
* Outside a project it holds only `extra`.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* return new IntentOutputs([commandVariables(ctx, { Reading: "the cached indexes" }), listing]);
|
|
10
|
+
* // Project: my-app / Config: /work/my-app/.sous/sous.config.js / Reading: the cached indexes
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export function commandVariables(ctx, extra = {}) {
|
|
14
|
+
const entries = {};
|
|
15
|
+
if (ctx.project !== undefined) {
|
|
16
|
+
const name = ctx.config.get(["name"]);
|
|
17
|
+
entries.Project = typeof name === "string" && name !== "" ? name : baseName(ctx.project.root);
|
|
18
|
+
if (ctx.project.configPath !== undefined) {
|
|
19
|
+
entries.Config = ctx.project.configPath;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return new IntentOutput({ title: "Command Variables", result: new VariableSet({ ...entries, ...extra }), style: ReportEntryStyle.CommandVariables });
|
|
23
|
+
}
|
|
24
|
+
/** The last segment of a path, on any platform. */
|
|
25
|
+
function baseName(directory) {
|
|
26
|
+
const parts = directory.split(/[\\/]/).filter((part) => part !== "");
|
|
27
|
+
return parts[parts.length - 1] ?? directory;
|
|
28
|
+
}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import type { Environment, ProjectInfo } from "./context.js";
|
|
2
|
+
import type { IntentDefinition } from "./intents.js";
|
|
3
|
+
import type { EntityReader } from "./entities.js";
|
|
4
|
+
import type { JsonObject, JsonValue, Patch, PatchSet, PathInput } from "./json.js";
|
|
5
|
+
import type { DocumentNode } from "./node.js";
|
|
6
|
+
import type { ConfigKeyType, EntityKind, EntityType } from "./registry.js";
|
|
7
|
+
import type { Reporter } from "./reporter.js";
|
|
8
|
+
import type { ServiceReader } from "./services.js";
|
|
9
|
+
/** Reading a built config. Values are plain JSON, frozen. */
|
|
10
|
+
export interface ConfigReader {
|
|
11
|
+
/** The value at a path, or `undefined`. */
|
|
12
|
+
get<K extends string>(path: K): ConfigKeyType<K> | undefined;
|
|
13
|
+
get<T = JsonValue>(path: PathInput): T | undefined;
|
|
14
|
+
/** Whether a value exists at a path. */
|
|
15
|
+
has(path: PathInput): boolean;
|
|
16
|
+
/** The whole config. */
|
|
17
|
+
value(): Readonly<JsonObject>;
|
|
18
|
+
}
|
|
19
|
+
/** One primitive change to a value, and who made it. */
|
|
20
|
+
export interface ConfigLayer {
|
|
21
|
+
/** The contributor that made it. */
|
|
22
|
+
readonly contributor: string;
|
|
23
|
+
/** The plugin the contributor belongs to. */
|
|
24
|
+
readonly plugin: string;
|
|
25
|
+
readonly patch: Patch;
|
|
26
|
+
/** The patch sets around it, outermost first, after the contributor's own: `["removeWhere"]`. */
|
|
27
|
+
readonly helpers: readonly string[];
|
|
28
|
+
/** The contributor's declared apply order. */
|
|
29
|
+
readonly applyOrder: number;
|
|
30
|
+
/** Its apply order once `before` and `after` are met; it may pass 99999. */
|
|
31
|
+
readonly actualApplyOrder: number;
|
|
32
|
+
/**
|
|
33
|
+
* The config build the change was made in: `""` for the build with no harness, else the
|
|
34
|
+
* harness's id (`"codex"`). See {@link BuiltConfig.harnesses}.
|
|
35
|
+
*/
|
|
36
|
+
readonly build: string;
|
|
37
|
+
}
|
|
38
|
+
/** One contributor's part in a build. */
|
|
39
|
+
export interface Contribution {
|
|
40
|
+
readonly contributor: string;
|
|
41
|
+
readonly plugin: string;
|
|
42
|
+
/** The config build it ran in: `""` for the build with no harness, else the harness's id. */
|
|
43
|
+
readonly build: string;
|
|
44
|
+
readonly stage: number;
|
|
45
|
+
/** Its declared apply order, 0 to 99999 (default 2000). */
|
|
46
|
+
readonly applyOrder: number;
|
|
47
|
+
/**
|
|
48
|
+
* Its apply order once `before` and `after` are met: the declared one, or moved to one past
|
|
49
|
+
* (or one less than) a contributor it must apply after (or before). It may pass 99999.
|
|
50
|
+
*/
|
|
51
|
+
readonly actualApplyOrder: number;
|
|
52
|
+
/** Its latest patch set. */
|
|
53
|
+
readonly patches: PatchSet;
|
|
54
|
+
/** How many times it ran in this build. */
|
|
55
|
+
readonly runs: number;
|
|
56
|
+
}
|
|
57
|
+
/** A config the engine built: the values, and the entities and services that belong to it. */
|
|
58
|
+
export interface BuiltConfig extends ConfigReader {
|
|
59
|
+
/** Counts builds in this process, "what if" builds included. */
|
|
60
|
+
readonly buildNumber: number;
|
|
61
|
+
/** True for a "what if" build, which never replaces the real config. */
|
|
62
|
+
readonly whatIf: boolean;
|
|
63
|
+
/** Passes the build took, over every stage, counting only passes in which something ran. */
|
|
64
|
+
readonly passes: number;
|
|
65
|
+
/** A hash of the values and the entities (ids, sources and data), to tell configs apart. */
|
|
66
|
+
readonly hash: string;
|
|
67
|
+
/** The entities of this build. */
|
|
68
|
+
readonly entities: EntityReader;
|
|
69
|
+
/** The services of this build: `config` services belong to it. */
|
|
70
|
+
readonly services: ServiceReader;
|
|
71
|
+
/** Every change to a path, or anything inside it or around it, in the order it was applied. */
|
|
72
|
+
layers(path: PathInput): ConfigLayer[];
|
|
73
|
+
/** Every contributor's part, in the order their patches apply: earlier stages first, then by actual apply order and id. */
|
|
74
|
+
contributions(): Contribution[];
|
|
75
|
+
/**
|
|
76
|
+
* The harness this config was built for: `""` for the config every intent reads (the build
|
|
77
|
+
* with no harness, merged with what belongs to each harness), else the harness's id.
|
|
78
|
+
*/
|
|
79
|
+
readonly harness: string;
|
|
80
|
+
/** The ids of every harness that had its own config build, sorted. Empty when no harness is registered. */
|
|
81
|
+
readonly harnesses: readonly string[];
|
|
82
|
+
/**
|
|
83
|
+
* One harness's full config, as its own build made it, for code that acts for that harness;
|
|
84
|
+
* `undefined` when no build was made for it. `forHarness("")` gives the build with no harness,
|
|
85
|
+
* before the merge.
|
|
86
|
+
*
|
|
87
|
+
* ```ts
|
|
88
|
+
* const codex = ctx.config.forHarness("codex");
|
|
89
|
+
* const comments = codex?.get<boolean>("compilation.includeSourceComments");
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
forHarness(id: string): BuiltConfig | undefined;
|
|
93
|
+
}
|
|
94
|
+
/** The harness a config build is for, as contributors and config expressions read it (`harness.id`). */
|
|
95
|
+
export interface BuildHarness {
|
|
96
|
+
/** The harness's id (`"codex"`); `""` in the build with no harness, never `undefined`. */
|
|
97
|
+
readonly id: string;
|
|
98
|
+
}
|
|
99
|
+
/** Watching more than a contributor reads: a path, or an entity kind. */
|
|
100
|
+
export type Monitor = PathInput | {
|
|
101
|
+
entities: string;
|
|
102
|
+
};
|
|
103
|
+
/** Who is running, as the builder tells a contributor. */
|
|
104
|
+
export interface ContributorInfo {
|
|
105
|
+
readonly id: string;
|
|
106
|
+
readonly plugin: string;
|
|
107
|
+
/** The stage it runs in, after any shift. */
|
|
108
|
+
readonly stage: number;
|
|
109
|
+
/** The pass of the current stage, from 1. */
|
|
110
|
+
readonly pass: number;
|
|
111
|
+
/** How many times it has run in this build, this run included. */
|
|
112
|
+
readonly run: number;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* What a contributor is given. It shows the config made from every other
|
|
116
|
+
* contributor's latest patches, without this contributor's own; every read is recorded, and every
|
|
117
|
+
* change becomes this run's patch set. It also reports like a reporter (`config.warn(...)`,
|
|
118
|
+
* `config.log(...)`), through the engine's reporter, with the contributor named in each message
|
|
119
|
+
* (contributors never print).
|
|
120
|
+
*/
|
|
121
|
+
export interface ConfigBuilder extends Reporter {
|
|
122
|
+
/** The root, to merge a whole config file: `config.root().merge(data)`. Reading it monitors everything. */
|
|
123
|
+
root(): DocumentNode;
|
|
124
|
+
/** A node at a path. */
|
|
125
|
+
atPath(path: PathInput): DocumentNode;
|
|
126
|
+
/** Every entity of a kind. Recorded as a read of the list. */
|
|
127
|
+
entities<K extends EntityKind>(kind: K): EntityType<K>[];
|
|
128
|
+
/** One entity. Recorded as a read of that entity alone. */
|
|
129
|
+
entity<K extends EntityKind>(kind: K, id: string): EntityType<K> | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* Adds an entity the config creates. It is part of this run's patch, so a re-run replaces it.
|
|
132
|
+
* Its methods must not use `this` (see `sous.entities.add`).
|
|
133
|
+
*/
|
|
134
|
+
addEntity<K extends EntityKind>(kind: K, entry: EntityType<K>): void;
|
|
135
|
+
/** This contributor's patch set from its previous run, if it ran before. */
|
|
136
|
+
previousPatches(): PatchSet | undefined;
|
|
137
|
+
/** Applies raw patches, as written in a patch file. */
|
|
138
|
+
apply(patches: Patch | PatchSet | readonly Patch[]): void;
|
|
139
|
+
/** This plugin's own options from `plugins.options`. Recorded as a read. */
|
|
140
|
+
options<T = JsonObject>(): T | undefined;
|
|
141
|
+
/** Who is running. */
|
|
142
|
+
readonly contributor: ContributorInfo;
|
|
143
|
+
readonly project: ProjectInfo | undefined;
|
|
144
|
+
readonly env: Environment;
|
|
145
|
+
/**
|
|
146
|
+
* What the front end is about to run, when it said (`BuildOptions.plan`); `undefined` when
|
|
147
|
+
* nothing is known, as for a library caller or a command only a plugin loaded later adds.
|
|
148
|
+
*/
|
|
149
|
+
readonly plan: RunPlan | undefined;
|
|
150
|
+
/**
|
|
151
|
+
* The harness this build is for. The engine builds the config once with no harness
|
|
152
|
+
* (`id: ""`), then once more for each registered harness; it never changes within a build,
|
|
153
|
+
* so reading it is not recorded.
|
|
154
|
+
*/
|
|
155
|
+
readonly harness: BuildHarness;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* A contributor: anything that adds to the config.
|
|
159
|
+
*
|
|
160
|
+
* ```ts
|
|
161
|
+
* sous.config.contribute({
|
|
162
|
+
* id: "diagrams",
|
|
163
|
+
* after: ["@sous-io/sous-plugin-skills"],
|
|
164
|
+
*
|
|
165
|
+
* contribute(config) {
|
|
166
|
+
* if (config.atPath("diagrams.enabled").valueOr(true)) {
|
|
167
|
+
* config.atPath("compilation.targets").appendValue({ id: "diagrams" });
|
|
168
|
+
* }
|
|
169
|
+
* },
|
|
170
|
+
* });
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
export interface ContributorDefinition {
|
|
174
|
+
/** Unique in the engine: `diagrams`, `confd/610-mac.jsonc`, `claude-code/defaults`. */
|
|
175
|
+
id: string;
|
|
176
|
+
/**
|
|
177
|
+
* The file it reads, for a contributor that applies one config file. Messages about a value it
|
|
178
|
+
* set name this file instead of the id: `/work/app/.sous/sous.config.js`.
|
|
179
|
+
*/
|
|
180
|
+
source?: string;
|
|
181
|
+
/** Its config stage; stages run lowest first. Default `0`, shifted for plugins loaded during a stage. */
|
|
182
|
+
stage?: number;
|
|
183
|
+
/**
|
|
184
|
+
* Where its patch applies within its stage: a whole number from 0 to 99999, default `2000`. Higher
|
|
185
|
+
* numbers apply later, so when two contributors set one key, the one with the higher number
|
|
186
|
+
* applies after the other and wins; equal numbers apply in id order. The user's files use
|
|
187
|
+
* 89900 and up, so they apply after every plugin's contributor. `before` and `after` win over
|
|
188
|
+
* the number: a contributor that must apply after another is moved to one past it (see
|
|
189
|
+
* `Contribution.actualApplyOrder`). Any other number is refused when the contributor is added.
|
|
190
|
+
*/
|
|
191
|
+
applyOrder?: number;
|
|
192
|
+
/**
|
|
193
|
+
* Plugins, contributor ids or wildcards this one runs and applies before. `"*"` means every
|
|
194
|
+
* contributor that does not itself declare `before: ["*"]`. Default: its plugin's `before`.
|
|
195
|
+
*/
|
|
196
|
+
before?: readonly string[];
|
|
197
|
+
/**
|
|
198
|
+
* Plugins, contributor ids or wildcards this one runs and applies after. `"*"` means every
|
|
199
|
+
* contributor that does not itself declare `after: ["*"]`. Default: its plugin's `after`.
|
|
200
|
+
*/
|
|
201
|
+
after?: readonly string[];
|
|
202
|
+
/** More to watch than it reads; a change to any re-runs it. */
|
|
203
|
+
monitors?: readonly Monitor[];
|
|
204
|
+
/** Builds this contributor's patch. Must give the same result for the same input. */
|
|
205
|
+
contribute(config: ConfigBuilder): void | Promise<void>;
|
|
206
|
+
}
|
|
207
|
+
/** Options for building a config. */
|
|
208
|
+
export interface BuildOptions {
|
|
209
|
+
/** More contributors for this build only ("what if" builds). */
|
|
210
|
+
extraContributors?: readonly ContributorDefinition[];
|
|
211
|
+
/**
|
|
212
|
+
* `reportBudgets` (`boolean = true`): `false` keeps the build's over-budget warning from being
|
|
213
|
+
* printed. A front end passes it for a command that only prints text (help, version, completion),
|
|
214
|
+
* where the warning is noise above the answer. The build is still timed (`--timings`).
|
|
215
|
+
*/
|
|
216
|
+
reportBudgets?: boolean;
|
|
217
|
+
/**
|
|
218
|
+
* What the front end is about to run, when it read its command line before the config was built.
|
|
219
|
+
* The engine keeps it for every later build in the same run, and contributors read it as
|
|
220
|
+
* `config.plan`.
|
|
221
|
+
*/
|
|
222
|
+
plan?: RunPlan;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* What a front end is about to run, read from its command line before the config was built.
|
|
226
|
+
*
|
|
227
|
+
* ```ts
|
|
228
|
+
* { intent: buildDefinition } // `sous build`
|
|
229
|
+
* { intent: undefined } // `sous --help`, `sous --version`: no intent runs
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
export interface RunPlan {
|
|
233
|
+
/** The intent it will submit, or `undefined` when it runs none (help, the version). */
|
|
234
|
+
readonly intent: IntentDefinition | undefined;
|
|
235
|
+
}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import type { BuildOptions, BuiltConfig } from "./config.js";
|
|
2
|
+
import type { EntityReader } from "./entities.js";
|
|
3
|
+
import type { EventFor, EventPayload } from "./events.js";
|
|
4
|
+
import type { IntentCatalog, IntentRequest, SubmitOptions } from "./intents.js";
|
|
5
|
+
import type { JsonObject } from "./json.js";
|
|
6
|
+
import type { Prompter } from "./prompter.js";
|
|
7
|
+
import type { IntentInput, IntentName, IntentReturn } from "./registry.js";
|
|
8
|
+
import type { Reporter } from "./reporter.js";
|
|
9
|
+
import type { ServiceReader } from "./services.js";
|
|
10
|
+
import type { Timings } from "./timing.js";
|
|
11
|
+
/**
|
|
12
|
+
* The project a sous process serves: the one found from its working directory. Its root is the
|
|
13
|
+
* directory holding the `.sous/` directory the config file was found in.
|
|
14
|
+
*/
|
|
15
|
+
export interface ProjectInfo {
|
|
16
|
+
/** The project root. */
|
|
17
|
+
readonly root: string;
|
|
18
|
+
/** The `.sous/` directory. */
|
|
19
|
+
readonly sousDir: string;
|
|
20
|
+
/** The config file, when one was found. */
|
|
21
|
+
readonly configPath: string | undefined;
|
|
22
|
+
/** The `conf.d` directory. */
|
|
23
|
+
readonly confDir: string;
|
|
24
|
+
}
|
|
25
|
+
/** The environment variables sous was started with. Read them here, never from `process.env`. */
|
|
26
|
+
export type Environment = Readonly<Record<string, string | undefined>>;
|
|
27
|
+
/** The application the engine runs: sous itself, or another tool built on the engine. */
|
|
28
|
+
export interface AppInfo {
|
|
29
|
+
readonly name: string;
|
|
30
|
+
readonly version: string;
|
|
31
|
+
}
|
|
32
|
+
/** The chain a context belongs to. */
|
|
33
|
+
export interface ChainInfo {
|
|
34
|
+
/** Counts chains in this process. */
|
|
35
|
+
readonly id: number;
|
|
36
|
+
/** The intent the chain was submitted with. */
|
|
37
|
+
readonly intent: string;
|
|
38
|
+
readonly parallel: boolean;
|
|
39
|
+
}
|
|
40
|
+
/** One loaded plugin, as front ends and `plugins show` see it. */
|
|
41
|
+
export interface PluginInfo {
|
|
42
|
+
readonly name: string;
|
|
43
|
+
readonly version: string | undefined;
|
|
44
|
+
/** Whether it is active in the current config. */
|
|
45
|
+
readonly active: boolean;
|
|
46
|
+
readonly builtIn: boolean;
|
|
47
|
+
/** The plugin that brought it in, or `plugins.load`. */
|
|
48
|
+
readonly loadedBy: string | undefined;
|
|
49
|
+
/** What it registered, for `plugins show`. */
|
|
50
|
+
readonly registrations: ReadonlyArray<{
|
|
51
|
+
readonly kind: string;
|
|
52
|
+
readonly name: string;
|
|
53
|
+
}>;
|
|
54
|
+
/** Its options from `plugins.options` in the current config, if any. */
|
|
55
|
+
readonly options: JsonObject | undefined;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The engine, as plugins see it: what front ends and background intents use.
|
|
59
|
+
*/
|
|
60
|
+
export interface EngineHandle {
|
|
61
|
+
/** The config as of the latest rebuild. */
|
|
62
|
+
readonly config: BuiltConfig;
|
|
63
|
+
/** The application name and version. */
|
|
64
|
+
readonly app: AppInfo;
|
|
65
|
+
/** The intents front ends generate commands and tools from. */
|
|
66
|
+
readonly intents: IntentCatalog;
|
|
67
|
+
/** Every timing so far (`--timings`). */
|
|
68
|
+
readonly timings: Timings;
|
|
69
|
+
/** Where engine-level messages go (budget warnings, build warnings). */
|
|
70
|
+
readonly reporter: Reporter;
|
|
71
|
+
/**
|
|
72
|
+
* The error of the latest config build, when it failed (`undefined` once one succeeds). The
|
|
73
|
+
* config is then the partial one that build reached; only intents that declare
|
|
74
|
+
* `toleratesConfigErrors` run, and a front end should report this error for anything else.
|
|
75
|
+
*/
|
|
76
|
+
readonly configError: unknown;
|
|
77
|
+
/** Sets where engine-level messages go; messages kept so far are sent there first. */
|
|
78
|
+
setReporter(reporter: Reporter): void;
|
|
79
|
+
/** Submits a chain; resolves with the intent's outputs. */
|
|
80
|
+
submit<N extends IntentName>(request: IntentRequest<N>, options?: SubmitOptions): Promise<IntentReturn<N>>;
|
|
81
|
+
/** Builds a separate "what if" config, never replacing the real one. */
|
|
82
|
+
buildConfig(options?: BuildOptions): Promise<BuiltConfig>;
|
|
83
|
+
/** Every loaded plugin. */
|
|
84
|
+
plugins(): PluginInfo[];
|
|
85
|
+
/** Resolves when every background intent has returned. */
|
|
86
|
+
whenBackgroundIntentsReturn(): Promise<void>;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* What a handler, a listener or an entity method gets (`ctx`). Inside a chain, `run` starts another
|
|
90
|
+
* intent as part of the same chain.
|
|
91
|
+
*/
|
|
92
|
+
export interface Context {
|
|
93
|
+
/** The plugin whose code was given this context. */
|
|
94
|
+
readonly plugin: string;
|
|
95
|
+
/** The config this chain started with (for a background intent, the latest). */
|
|
96
|
+
readonly config: BuiltConfig;
|
|
97
|
+
readonly entities: EntityReader;
|
|
98
|
+
readonly services: ServiceReader;
|
|
99
|
+
/** Where this chain's output goes: the front end that submitted it. */
|
|
100
|
+
readonly reporter: Reporter;
|
|
101
|
+
/** Where this chain's questions go: the front end that submitted it. */
|
|
102
|
+
readonly prompter: Prompter;
|
|
103
|
+
readonly project: ProjectInfo | undefined;
|
|
104
|
+
readonly env: Environment;
|
|
105
|
+
/** The front end's global argument values (`debug`, `logLevel`, plugin-added ones). */
|
|
106
|
+
readonly globals: Readonly<Record<string, unknown>>;
|
|
107
|
+
/**
|
|
108
|
+
* The working directory the front end gave the chain: the CLI gives the process's, the MCP
|
|
109
|
+
* server the project root. Use it instead of `process.cwd()`. Default: the engine's.
|
|
110
|
+
*/
|
|
111
|
+
readonly cwd: string;
|
|
112
|
+
/** The chain, or `undefined` outside one (a background intent). */
|
|
113
|
+
readonly chain: ChainInfo | undefined;
|
|
114
|
+
readonly engine: EngineHandle;
|
|
115
|
+
/** This plugin's own options from `plugins.options`. */
|
|
116
|
+
options<T = JsonObject>(): T | undefined;
|
|
117
|
+
/**
|
|
118
|
+
* Runs an intent as part of this chain, immediately, and returns its outputs; `toPlain()` gives
|
|
119
|
+
* its result as a plain object. Outside a chain it submits a new chain instead.
|
|
120
|
+
*/
|
|
121
|
+
run<N extends IntentName>(intent: N, input?: IntentInput<N> | Record<string, unknown>): Promise<IntentReturn<N>>;
|
|
122
|
+
/** Fires an event and waits for every listener. */
|
|
123
|
+
emit<N extends string>(name: N, payload?: EventPayload<N>): Promise<EventFor<N>>;
|
|
124
|
+
/** Says this chain changed the config, so it is rebuilt before the next chain starts. */
|
|
125
|
+
configChanged(): void;
|
|
126
|
+
/**
|
|
127
|
+
* Rebuilds the config now, for the rest of this chain: later `ctx.config`, `ctx.services`
|
|
128
|
+
* (`config` services are made again), `ctx.entities` and `ctx.run` calls see the new one. The
|
|
129
|
+
* new config becomes the engine's, and `config.built` fires, as for any rebuild. Resolves with
|
|
130
|
+
* it; a failed build rejects, and the chain keeps its old config.
|
|
131
|
+
*
|
|
132
|
+
* Only a series chain may do this: a parallel chain, a background intent, a `config.built`
|
|
133
|
+
* listener or code outside any chain gets a `SousError`.
|
|
134
|
+
*
|
|
135
|
+
* ```ts
|
|
136
|
+
* async handle(input, ctx) {
|
|
137
|
+
* await writeSubscription(ctx, input); // changes a config file
|
|
138
|
+
* await ctx.refreshConfig(); // the build below sees it
|
|
139
|
+
*
|
|
140
|
+
* return ctx.run("build", {}); // the build's outputs become this intent's
|
|
141
|
+
* }
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
refreshConfig(options?: RefreshConfigOptions): Promise<BuiltConfig>;
|
|
145
|
+
}
|
|
146
|
+
/** Options for `ctx.refreshConfig()`. */
|
|
147
|
+
export interface RefreshConfigOptions {
|
|
148
|
+
/**
|
|
149
|
+
* Where to look for the project before rebuilding, for an intent that has just set one up
|
|
150
|
+
* (`sous init`). The engine takes the project found at or above this directory when it differs
|
|
151
|
+
* from the one it serves; without a project there, nothing changes.
|
|
152
|
+
*
|
|
153
|
+
* ```ts
|
|
154
|
+
* await writeProjectFiles(root);
|
|
155
|
+
* await ctx.refreshConfig({ locateProjectFrom: root }); // the build below serves `root`
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
locateProjectFrom?: string;
|
|
159
|
+
}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { ProjectInfo } from "./context.js";
|
|
2
|
+
import type { PluginDefinition } from "./plugin.js";
|
|
3
|
+
import type { EntityKind, EntityType } from "./registry.js";
|
|
4
|
+
import type { SousSchema } from "./schema.js";
|
|
5
|
+
/** Options for defining an entity kind. */
|
|
6
|
+
export interface EntityKindOptions {
|
|
7
|
+
/** What the kind is, for `plugins show`. */
|
|
8
|
+
description?: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* One entity, with where it came from.
|
|
12
|
+
*
|
|
13
|
+
* The engine compares entities by id, source and a hash of their fields that are not functions,
|
|
14
|
+
* so a contributor that read an entity re-runs when it appears, disappears or its data changes.
|
|
15
|
+
*/
|
|
16
|
+
export interface EntityRecord<E = unknown> {
|
|
17
|
+
readonly kind: string;
|
|
18
|
+
readonly id: string;
|
|
19
|
+
readonly entry: E;
|
|
20
|
+
/** The plugin that added it. */
|
|
21
|
+
readonly plugin: string;
|
|
22
|
+
/** The contributor that added it, when the config created it. */
|
|
23
|
+
readonly contributor?: string;
|
|
24
|
+
}
|
|
25
|
+
/** Reading entities, after the config is built. */
|
|
26
|
+
export interface EntityReader {
|
|
27
|
+
/** One entity, or `undefined`. */
|
|
28
|
+
get<K extends EntityKind>(kind: K, id: string): EntityType<K> | undefined;
|
|
29
|
+
/** Every entity of a kind, in the order they were added. */
|
|
30
|
+
list<K extends EntityKind>(kind: K): EntityType<K>[];
|
|
31
|
+
/** Whether an entity exists. */
|
|
32
|
+
has(kind: string, id: string): boolean;
|
|
33
|
+
/** Every entity of a kind, with where each came from. */
|
|
34
|
+
describe<K extends EntityKind>(kind: K): EntityRecord<EntityType<K>>[];
|
|
35
|
+
/** Every kind defined so far. */
|
|
36
|
+
kinds(): string[];
|
|
37
|
+
}
|
|
38
|
+
/** A `config-key` entity: a key the config may hold, with its schema and defaults. */
|
|
39
|
+
export interface ConfigKeyEntity {
|
|
40
|
+
/** The key, as a path: `diagrams`, or `tools.claude` for a key inside another plugin's. */
|
|
41
|
+
id: string;
|
|
42
|
+
/** Checks the value and fills in its defaults when the finished config is checked. */
|
|
43
|
+
schema: SousSchema;
|
|
44
|
+
/** What the key is for. */
|
|
45
|
+
description?: string;
|
|
46
|
+
}
|
|
47
|
+
/** What a plugin locator is told. */
|
|
48
|
+
export interface PluginLocateContext {
|
|
49
|
+
readonly project: ProjectInfo | undefined;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* A `plugin-locator` entity: turns a specifier from `plugins.load` into a module to import, such as
|
|
53
|
+
* a recipe's downloaded bundle, or a Yarn Plug'n'Play location. Locators are asked in plugin order;
|
|
54
|
+
* the first answer wins, and when none answers, Node's own resolution from the project is used.
|
|
55
|
+
*/
|
|
56
|
+
export interface PluginLocatorEntity {
|
|
57
|
+
id: string;
|
|
58
|
+
/** A path or URL to import, a plugin definition, or `undefined` to pass. */
|
|
59
|
+
locate(specifier: string, context: PluginLocateContext): string | URL | PluginDefinition | undefined | Promise<string | URL | PluginDefinition | undefined>;
|
|
60
|
+
}
|
package/dist/entities.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|