@drkmttr/adhere 0.18.1 → 0.21.2
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 +1099 -1088
- package/package.json +15 -15
- package/presets/README.md +132 -0
- package/skills/adhere/SKILL.md +4 -3
- package/skills/adhere-setup/SKILL.md +11 -10
- package/src/cli.ts +121 -14
- package/src/config.ts +147 -139
- package/src/create/agents.ts +149 -0
- package/src/create/ask.ts +147 -0
- package/src/create/look.ts +140 -0
- package/src/create/wizard.ts +432 -0
- package/src/index.ts +2 -0
- package/src/init.ts +2 -1
- package/src/install.ts +1 -1
- package/src/instructions.ts +76 -0
- package/src/markdown.ts +3 -9
- package/src/rules.ts +42 -45
- package/src/services/AdhereConfig.ts +31 -18
- package/src/services/Credentials.ts +14 -8
- package/src/services/Jev.reads.test.ts +22 -0
- package/src/services/Jev.ts +35 -13
- package/src/wording.ts +1 -1
- package/src/workflows/audit.ts +82 -14
- package/src/workflows/format.ts +97 -0
- package/src/workspaces.test.ts +37 -1
- package/src/workspaces.ts +121 -12
package/src/config.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type PresetName, presetNames } from "#presets.ts";
|
|
2
2
|
import { type Provider, SAVED_KEYS } from "#providers/provider.ts";
|
|
3
3
|
import type { RuleSet } from "#rules.ts";
|
|
4
|
-
import { Effect, Predicate, Schema
|
|
4
|
+
import { Effect, Predicate, Schema } from "effect";
|
|
5
5
|
|
|
6
6
|
export type RuleId = string;
|
|
7
7
|
|
|
@@ -32,7 +32,10 @@ const ProviderSchema = Schema.declare(
|
|
|
32
32
|
Predicate.hasProperty(value, "ask") &&
|
|
33
33
|
Predicate.isFunction(value.ask) &&
|
|
34
34
|
(!Predicate.hasProperty(value, "savedKey") || SAVED_KEYS.some((key) => key === value.savedKey)),
|
|
35
|
-
{
|
|
35
|
+
{
|
|
36
|
+
expected: "a provider: a model, and a function to ask it",
|
|
37
|
+
toCode: () => ({ runtime: "Provider", Type: "Provider" }),
|
|
38
|
+
},
|
|
36
39
|
);
|
|
37
40
|
|
|
38
41
|
/**
|
|
@@ -56,12 +59,16 @@ export type AppendState = (
|
|
|
56
59
|
* What the config can have Jev read beside each file's code, shared by all
|
|
57
60
|
* its rules. Each name is the key the state carries it under, which a rule's
|
|
58
61
|
* description can name in backticks. `workspacePackages` maps only workspace
|
|
59
|
-
* packages imported by this file to their directories. `
|
|
62
|
+
* packages imported by this file to their directories. `dependents` lists what
|
|
63
|
+
* depends on this file: workspace packages whose package.json names its package,
|
|
64
|
+
* and files that import it by a relative path. `symbols` maps names
|
|
60
65
|
* to native compiler types, with documentation and JSDoc tags shared across variants.
|
|
61
66
|
* `references` maps used names to complete AST declaration scopes by
|
|
62
67
|
* repo-relative path, rather than fixed line windows or full related files.
|
|
63
68
|
*/
|
|
64
|
-
export const Reads = Schema.Array(
|
|
69
|
+
export const Reads = Schema.Array(
|
|
70
|
+
Schema.Literals(["workspacePackages", "dependents", "symbols", "references"]),
|
|
71
|
+
);
|
|
65
72
|
|
|
66
73
|
/**
|
|
67
74
|
* A rule's examples, named in RFC 2119's words, which Jev reads as written. A
|
|
@@ -69,91 +76,84 @@ export const Reads = Schema.Array(Schema.Literals(["workspacePackages", "symbols
|
|
|
69
76
|
* be. A guideline, which asks for less, shows code that `should` be written
|
|
70
77
|
* and code that `shouldNot` be.
|
|
71
78
|
*/
|
|
72
|
-
const
|
|
73
|
-
description: Schema.String
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
*/
|
|
79
|
+
export const Rule = Schema.Struct({
|
|
80
|
+
description: Schema.String.annotate({
|
|
81
|
+
description: "What the rule says, in one sentence. The report shows it with each finding.",
|
|
82
|
+
}),
|
|
83
|
+
details: Schema.optionalKey(Schema.String).annotate({
|
|
84
|
+
description:
|
|
85
|
+
"What the description leaves out, such as why the rule holds or where it does not: in a `RULE.md`, the prose around its code. Jev reads it after the description, as part of the rule; the report shows the description alone.",
|
|
86
|
+
}),
|
|
87
|
+
must: Schema.optionalKey(Schema.String).annotate({
|
|
88
|
+
description:
|
|
89
|
+
"Code the rule requires. A rule has `must` and `never`, or, as a guideline, `should` and `shouldNot`.",
|
|
90
|
+
}),
|
|
91
|
+
never: Schema.optionalKey(Schema.String).annotate({
|
|
92
|
+
description: "Code the rule forbids.",
|
|
93
|
+
}),
|
|
94
|
+
should: Schema.optionalKey(Schema.String).annotate({
|
|
95
|
+
description: "A guideline's code to write.",
|
|
96
|
+
}),
|
|
97
|
+
shouldNot: Schema.optionalKey(Schema.String).annotate({
|
|
98
|
+
description: "A guideline's code not to write.",
|
|
99
|
+
}),
|
|
100
|
+
threshold: Schema.optionalKey(Schema.Finite).annotate({
|
|
101
|
+
description: "The rule's own confidence threshold, 0 to 1, in place of the config's.",
|
|
102
|
+
}),
|
|
103
|
+
tests: Schema.optionalKey(Schema.Literals(["only", "include"])).annotate({
|
|
104
|
+
description:
|
|
105
|
+
"Test files, which a rule skips by default: `only` for a rule about tests, which judges nothing else, and `include` for one that holds in tests too.",
|
|
106
|
+
}),
|
|
107
|
+
level: Schema.optionalKey(Schema.Literals(["error", "warning"])).annotate({
|
|
108
|
+
description:
|
|
109
|
+
"How a finding under the rule is reported: an `error`, the default, which fails the run, or a `warning`, for a nit or a rule that errs toward false findings, which does not.",
|
|
110
|
+
}),
|
|
111
|
+
appliesTo: Schema.optionalKey(Schema.Array(Schema.String)).annotate({
|
|
112
|
+
description:
|
|
113
|
+
"Where the rule applies, each a description of code that breaks it: Jev is asked of each whether any code that breaks the rule is as it describes, and a finding stands only when it says yes to every one.",
|
|
114
|
+
}),
|
|
115
|
+
excludeIf: Schema.optionalKey(Schema.Array(Schema.String)).annotate({
|
|
116
|
+
description:
|
|
117
|
+
"Where the rule does not apply: Jev is asked of each whether all the code that breaks the rule is as it describes, and a yes to any drops the finding.",
|
|
118
|
+
}),
|
|
113
119
|
appendState: Schema.optionalKey(
|
|
114
120
|
Schema.declare((value: unknown): value is AppendState => typeof value === "function", {
|
|
115
121
|
expected: "a function",
|
|
122
|
+
toCode: () => ({ runtime: "AppendState", Type: "AppendState" }),
|
|
116
123
|
}),
|
|
117
|
-
)
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
// Guard the removed key instead of silently dropping it as an excess property.
|
|
126
|
-
reads: Schema.optionalKey(
|
|
127
|
-
Schema.declare((_value: unknown): _value is never => false, {
|
|
128
|
-
expected: "no rule-level reads; move reads to the top level of defineConfig({...})",
|
|
124
|
+
).annotate({
|
|
125
|
+
description:
|
|
126
|
+
"A hook on the state Jev reads for the rule, in a rule written in TypeScript (see `AppendState`). The rule's questions go in requests of their own, so only they read what it adds.",
|
|
127
|
+
}),
|
|
128
|
+
options: Schema.optionalKey(
|
|
129
|
+
Schema.declare((value: unknown): value is Schema.Decoder<unknown> => Schema.isSchema(value), {
|
|
130
|
+
expected: "an Effect Schema",
|
|
131
|
+
toCode: () => ({ runtime: "Schema", Type: "Schema.Decoder<unknown>" }),
|
|
129
132
|
}),
|
|
130
|
-
)
|
|
133
|
+
).annotate({
|
|
134
|
+
description:
|
|
135
|
+
"The settings a rule written in TypeScript takes, as an Effect Schema, set under the rule's id in a config's `overrides`, as `options`. A key the schema does not make optional must be set. Jev reads the decoded settings as `options`, in requests of the rule's own.",
|
|
136
|
+
}),
|
|
131
137
|
})
|
|
132
|
-
.
|
|
133
|
-
Schema.
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
138
|
+
.check(
|
|
139
|
+
Schema.makeFilter(
|
|
140
|
+
(rule) => {
|
|
141
|
+
const requirement = rule.must !== undefined || rule.never !== undefined;
|
|
142
|
+
const guideline = rule.should !== undefined || rule.shouldNot !== undefined;
|
|
143
|
+
return requirement && guideline
|
|
144
|
+
? "a rule is a requirement (must, never) or a guideline (should, shouldNot), not both"
|
|
145
|
+
: requirement || guideline
|
|
146
|
+
? undefined
|
|
147
|
+
: "a rule needs an example: must, never, should, or shouldNot";
|
|
148
|
+
},
|
|
149
|
+
{ toCode: () => ({ runtime: "Schema.makeFilter(isRequirementOrGuideline)" }) },
|
|
143
150
|
),
|
|
144
151
|
)
|
|
145
|
-
.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
? "a rule is a requirement (must, never) or a guideline (should, shouldNot), not both"
|
|
151
|
-
: requirement || guideline
|
|
152
|
-
? undefined
|
|
153
|
-
: "a rule needs an example: must, never, should, or shouldNot";
|
|
154
|
-
}),
|
|
155
|
-
);
|
|
156
|
-
export interface Rule extends Schema.Schema.Type<typeof Rule> {}
|
|
152
|
+
.annotate({ identifier: "Rule" });
|
|
153
|
+
export interface Rule extends Schema.Schema.Type<typeof Rule> {
|
|
154
|
+
/** The settings `options` decoded from the config's override, set as the config is loaded. */
|
|
155
|
+
readonly optionValues?: unknown;
|
|
156
|
+
}
|
|
157
157
|
|
|
158
158
|
/** How a finding is reported: an error fails the run, a warning does not. */
|
|
159
159
|
export type Level = NonNullable<Rule["level"]>;
|
|
@@ -186,72 +186,76 @@ export type Rules = Readonly<Record<RuleId, Rule>>;
|
|
|
186
186
|
/** Rules inline, or a directory of Markdown rule files (see `markdown.ts`). */
|
|
187
187
|
export type RuleSource = Rules | string | URL;
|
|
188
188
|
|
|
189
|
-
/** A rule's setting in a config's `overrides`: `off`, a level, or a level and
|
|
189
|
+
/** A rule's setting in a config's `overrides`: `off`, a level, or a level, a threshold, and its options. */
|
|
190
190
|
const Override = Schema.Union([
|
|
191
191
|
Schema.Literals(["off", "error", "warning"]),
|
|
192
192
|
Schema.Struct({
|
|
193
193
|
level: Schema.optionalKey(Schema.Literals(["off", "error", "warning"])),
|
|
194
194
|
threshold: Schema.optionalKey(Schema.Finite),
|
|
195
|
+
options: Schema.optionalKey(Schema.Unknown),
|
|
195
196
|
}),
|
|
196
197
|
]);
|
|
197
198
|
export type Override = typeof Override.Type;
|
|
198
199
|
|
|
200
|
+
/** A config file's keys. Each `description` is what the docs' Config file reference shows. */
|
|
199
201
|
export const AdhereConfig = Schema.Struct({
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
maxSymbolLength: Schema.optionalKey(Schema.Natural)
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
202
|
+
model: Schema.optionalKey(Schema.String).annotate({
|
|
203
|
+
description: `The model id sent to TypeSafe AI. Defaults to "jev-latest".`,
|
|
204
|
+
}),
|
|
205
|
+
provider: Schema.optionalKey(ProviderSchema).annotate({
|
|
206
|
+
description:
|
|
207
|
+
"Where the questions go in place of TypeSafe's Jev, with its own model (see `Provider`).",
|
|
208
|
+
}),
|
|
209
|
+
threshold: Schema.optionalKey(Schema.Finite).annotate({
|
|
210
|
+
description: "The minimum confidence threshold for a positive match. Defaults to 0.8.",
|
|
211
|
+
}),
|
|
212
|
+
sufficiencyThreshold: Schema.optionalKey(Schema.Finite).annotate({
|
|
213
|
+
description:
|
|
214
|
+
"Below this, Jev's probability that a file shows enough to decide a finding's rule adds a warning to the finding: check what the code relies on outside the file. Defaults to 0.6.",
|
|
215
|
+
}),
|
|
216
|
+
presets: Schema.Array(Schema.Literals(presetNames))
|
|
217
|
+
.pipe(Schema.withDecodingDefaultKey(Effect.succeed([])))
|
|
218
|
+
.annotate({
|
|
219
|
+
description:
|
|
220
|
+
"The built-in rule sets to use, such as `effect` or `alchemy`. Append `/<topic>` for one topic, as in `effect/basics`. Defaults to none.",
|
|
221
|
+
}),
|
|
222
|
+
reads: Schema.optionalKey(Reads).annotate({
|
|
223
|
+
description:
|
|
224
|
+
"Workspace context shared by every rule: `workspacePackages` (the default) and `dependents`; `[]` disables it. Native read names do not enable type awareness.",
|
|
225
|
+
}),
|
|
226
|
+
typeAware: Schema.optionalKey(Schema.Boolean).annotate({
|
|
227
|
+
description:
|
|
228
|
+
"Include both compiler-resolved symbols and declaration excerpts. Defaults to false.",
|
|
229
|
+
}),
|
|
230
|
+
maxSymbolLength: Schema.optionalKey(Schema.Natural).annotate({
|
|
231
|
+
description: "Maximum characters of each symbol's complete resolved content. Defaults to 256.",
|
|
232
|
+
}),
|
|
233
|
+
maxSymbolDocumentationLength: Schema.optionalKey(Schema.Natural).annotate({
|
|
234
|
+
description:
|
|
235
|
+
"Maximum characters of documentation and JSDoc tags per symbol. Defaults to 256; 0 omits them.",
|
|
236
|
+
}),
|
|
237
|
+
rules: Schema.optionalKey(
|
|
238
|
+
Schema.Union([Schema.Record(Schema.String, Rule), Schema.String]),
|
|
239
|
+
).annotate({
|
|
240
|
+
description:
|
|
241
|
+
"A record of rules (see Rule keys), or a directory path relative to the config file, which holds its rules as `.adhere/rules/` does. Unset: the rules in `.adhere/rules/`, and in any nested `.adhere/rules/`.",
|
|
242
|
+
}),
|
|
243
|
+
instructions: Schema.optionalKey(Schema.NonEmptyString).annotate({
|
|
244
|
+
description:
|
|
245
|
+
"Instructions directory relative to the config file. Unset: `.adhere/instructions`, then `.agents/instructions`.",
|
|
246
|
+
}),
|
|
247
|
+
exclude: Schema.optionalKey(Schema.Array(Schema.String)).annotate({
|
|
248
|
+
description:
|
|
249
|
+
"Globs, relative to the working directory, of files no rule judges: what `--filter '!<glob>'` leaves out of one run, left out of every run.",
|
|
250
|
+
}),
|
|
251
|
+
overrides: Schema.optionalKey(Schema.Record(Schema.String, Override)).annotate({
|
|
252
|
+
description:
|
|
253
|
+
"Settings for rules by the id a report names them with, a preset's with its preset first, as in `alchemy/providers/idempotent-delete`: `off`, a level, or `{ level, threshold, options }`. A preset's rule changes without its text being copied into `rules`.",
|
|
254
|
+
}),
|
|
255
|
+
includeComments: Schema.optionalKey(Schema.Boolean).annotate({
|
|
256
|
+
description:
|
|
257
|
+
"Whether Jev reads every comment, for rules about comments. By default comments are taken out, since a comment changes nothing a file does, but for the notes that say `@adhere`.",
|
|
258
|
+
}),
|
|
255
259
|
});
|
|
256
260
|
|
|
257
261
|
/** Where a repo keeps what adhere reads and writes: its config, its rules, and the cache. */
|
|
@@ -366,9 +370,11 @@ export interface ResolvedConfig {
|
|
|
366
370
|
/**
|
|
367
371
|
* The workspace's packages, each name with its directory from the working
|
|
368
372
|
* directory, read from its manifests on every run when the config's reads
|
|
369
|
-
* names
|
|
373
|
+
* names `workspacePackages` or `dependents`.
|
|
370
374
|
*/
|
|
371
375
|
readonly workspacePackages?: Readonly<Record<string, string>>;
|
|
376
|
+
/** Per workspace package, the workspace packages that depend on it, when reads names `dependents`. */
|
|
377
|
+
readonly workspaceDependents?: Readonly<Record<string, Readonly<Record<string, string>>>>;
|
|
372
378
|
}
|
|
373
379
|
|
|
374
380
|
/** Command-line values that apply on top of the config file. */
|
|
@@ -429,7 +435,9 @@ export const resolveConfig = (
|
|
|
429
435
|
...(config.includeComments === undefined ? {} : { includeComments: config.includeComments }),
|
|
430
436
|
typeAware,
|
|
431
437
|
reads: [
|
|
432
|
-
...(config.reads ?? DEFAULT_READS).filter(
|
|
438
|
+
...(config.reads ?? DEFAULT_READS).filter(
|
|
439
|
+
(read) => read === "workspacePackages" || read === "dependents",
|
|
440
|
+
),
|
|
433
441
|
...(typeAware ? (["symbols", "references"] as const) : []),
|
|
434
442
|
],
|
|
435
443
|
maxSymbolLength: config.maxSymbolLength ?? DEFAULT_MAX_SYMBOL_LENGTH,
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The coding agents installed beside adhere, asked one question at a time
|
|
3
|
+
* without a session: `claude -p`, `codex exec`, and `omp -p`. The prompt goes
|
|
4
|
+
* on stdin, the agent may read the repo it runs in but not change it, and
|
|
5
|
+
* what comes back is the text of its last message. Given a JSON schema, the
|
|
6
|
+
* agent is held to it where its CLI can, and the text is that JSON.
|
|
7
|
+
*/
|
|
8
|
+
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { Option, Schema } from "effect";
|
|
12
|
+
|
|
13
|
+
export const AGENTS = ["claude", "codex", "omp"] as const;
|
|
14
|
+
export type Agent = (typeof AGENTS)[number];
|
|
15
|
+
|
|
16
|
+
/** The agents on PATH, in the order adhere prefers them. */
|
|
17
|
+
export const installedAgents = (): ReadonlyArray<Agent> =>
|
|
18
|
+
AGENTS.filter((agent) => Bun.which(agent) !== null);
|
|
19
|
+
|
|
20
|
+
/** `claude -p --output-format json` prints its events, the last its result. */
|
|
21
|
+
const ClaudeResult = Schema.fromJsonString(
|
|
22
|
+
Schema.Array(
|
|
23
|
+
Schema.Struct({
|
|
24
|
+
type: Schema.String,
|
|
25
|
+
is_error: Schema.optionalKey(Schema.Boolean),
|
|
26
|
+
result: Schema.optionalKey(Schema.String),
|
|
27
|
+
structured_output: Schema.optionalKey(Schema.Unknown),
|
|
28
|
+
}),
|
|
29
|
+
),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
/** The line of `omp --mode json` that ends an assistant's message; every other line is skipped. */
|
|
33
|
+
const OmpAnswer = Schema.fromJsonString(
|
|
34
|
+
Schema.Struct({
|
|
35
|
+
type: Schema.Literal("message_end"),
|
|
36
|
+
message: Schema.Struct({
|
|
37
|
+
role: Schema.Literal("assistant"),
|
|
38
|
+
content: Schema.Array(
|
|
39
|
+
Schema.Struct({ type: Schema.String, text: Schema.optionalKey(Schema.String) }),
|
|
40
|
+
),
|
|
41
|
+
}),
|
|
42
|
+
}),
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
/** Runs `command` in `cwd` with `input` on stdin, and returns its stdout once it exits 0. */
|
|
46
|
+
export const run = async (
|
|
47
|
+
command: string,
|
|
48
|
+
args: ReadonlyArray<string>,
|
|
49
|
+
cwd: string,
|
|
50
|
+
input: string,
|
|
51
|
+
) => {
|
|
52
|
+
const child = Bun.spawn([command, ...args], {
|
|
53
|
+
cwd,
|
|
54
|
+
stdin: new Blob([input]),
|
|
55
|
+
stdout: "pipe",
|
|
56
|
+
stderr: "pipe",
|
|
57
|
+
});
|
|
58
|
+
const [stdout, stderr, code] = await Promise.all([
|
|
59
|
+
new Response(child.stdout).text(),
|
|
60
|
+
new Response(child.stderr).text(),
|
|
61
|
+
child.exited,
|
|
62
|
+
]);
|
|
63
|
+
if (code !== 0) throw new Error(`${command} exited ${code}: ${stderr.trim().slice(-500)}`);
|
|
64
|
+
return stdout;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/** The text of `agent`'s answer to `prompt`, asked in `cwd`; with `schema`, its JSON. */
|
|
68
|
+
export const askAgent = async (
|
|
69
|
+
agent: Agent,
|
|
70
|
+
prompt: string,
|
|
71
|
+
cwd: string,
|
|
72
|
+
schema?: object,
|
|
73
|
+
): Promise<string> => {
|
|
74
|
+
switch (agent) {
|
|
75
|
+
case "claude": {
|
|
76
|
+
const events = Schema.decodeUnknownSync(ClaudeResult)(
|
|
77
|
+
await run(
|
|
78
|
+
"claude",
|
|
79
|
+
[
|
|
80
|
+
"-p",
|
|
81
|
+
"--output-format=json",
|
|
82
|
+
"--tools=Read,Grep,Glob",
|
|
83
|
+
"--no-session-persistence",
|
|
84
|
+
...(schema === undefined ? [] : [`--json-schema=${JSON.stringify(schema)}`]),
|
|
85
|
+
],
|
|
86
|
+
cwd,
|
|
87
|
+
prompt,
|
|
88
|
+
),
|
|
89
|
+
);
|
|
90
|
+
const result = events.findLast((event) => event.type === "result");
|
|
91
|
+
if (result === undefined || result.is_error === true) {
|
|
92
|
+
throw new Error(`claude did not answer: ${result?.result ?? "no result"}`);
|
|
93
|
+
}
|
|
94
|
+
return schema === undefined
|
|
95
|
+
? (result.result ?? "")
|
|
96
|
+
: JSON.stringify(result.structured_output);
|
|
97
|
+
}
|
|
98
|
+
case "codex": {
|
|
99
|
+
const dir = await mkdtemp(join(tmpdir(), "adhere-codex-"));
|
|
100
|
+
try {
|
|
101
|
+
const out = join(dir, "answer");
|
|
102
|
+
const schemaFile = join(dir, "schema.json");
|
|
103
|
+
if (schema !== undefined) await writeFile(schemaFile, JSON.stringify(schema));
|
|
104
|
+
await run(
|
|
105
|
+
"codex",
|
|
106
|
+
[
|
|
107
|
+
"exec",
|
|
108
|
+
"--sandbox=read-only",
|
|
109
|
+
"--ephemeral",
|
|
110
|
+
"--skip-git-repo-check",
|
|
111
|
+
"--color=never",
|
|
112
|
+
`--output-last-message=${out}`,
|
|
113
|
+
...(schema === undefined ? [] : [`--output-schema=${schemaFile}`]),
|
|
114
|
+
"-",
|
|
115
|
+
],
|
|
116
|
+
cwd,
|
|
117
|
+
prompt,
|
|
118
|
+
);
|
|
119
|
+
return await readFile(out, "utf8");
|
|
120
|
+
} finally {
|
|
121
|
+
await rm(dir, { recursive: true, force: true });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
case "omp": {
|
|
125
|
+
// omp holds no answer to a schema, so the schema goes in the prompt and
|
|
126
|
+
// the JSON is cut out of whatever surrounds it.
|
|
127
|
+
const asked =
|
|
128
|
+
schema === undefined
|
|
129
|
+
? prompt
|
|
130
|
+
: `${prompt}\n\nReply with only JSON matching this schema:\n${JSON.stringify(schema)}`;
|
|
131
|
+
const lines = (
|
|
132
|
+
await run(
|
|
133
|
+
"omp",
|
|
134
|
+
["-p", "--mode=json", "--tools=read,grep,glob", "--no-session", "--no-title"],
|
|
135
|
+
cwd,
|
|
136
|
+
asked,
|
|
137
|
+
)
|
|
138
|
+
).split("\n");
|
|
139
|
+
const answer = lines
|
|
140
|
+
.flatMap((line) => Option.toArray(Schema.decodeUnknownOption(OmpAnswer)(line)))
|
|
141
|
+
.at(-1);
|
|
142
|
+
if (answer === undefined) throw new Error("omp did not answer");
|
|
143
|
+
const text = answer.message.content
|
|
144
|
+
.flatMap((part) => (part.type === "text" && part.text !== undefined ? [part.text] : []))
|
|
145
|
+
.join("");
|
|
146
|
+
return schema === undefined ? text : text.slice(text.indexOf("{"), text.lastIndexOf("}") + 1);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|