@r_masseater/oxlint-plugin-decision 0.1.6 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +53 -29
- package/dist/index.d.mts +5 -5
- package/dist/index.mjs +164 -101
- package/dist/index.mjs.map +1 -1
- package/dist/openrouter-app.d.mts +6 -0
- package/dist/openrouter-app.mjs +79 -0
- package/dist/openrouter-app.mjs.map +1 -0
- package/dist/openrouter-worker.mjs +48 -127
- package/dist/openrouter-worker.mjs.map +1 -1
- package/dist/{wire-XOgoBBuu.mjs → wire-XbLEwd0i.mjs} +166 -198
- package/dist/wire-XbLEwd0i.mjs.map +1 -0
- package/package.json +17 -11
- package/skills/write-decision-rules/SKILL.md +18 -18
- package/skills/write-decision-rules/references/providers.md +13 -9
- package/skills/write-decision-rules/references/tuning.md +7 -7
- package/dist/wire-XOgoBBuu.mjs.map +0 -1
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { Array, Boolean, Cause, Config, Console, Effect, Exit, Number as Number$1, Option, Order, Predicate, Result, Schema, String as String$1, pipe } from "effect";
|
|
1
|
+
import { Array, Boolean, Cause, Config, Console, DateTime, Effect, Exit, Function, Number as Number$1, Option, Order, Predicate, Record, Result, Schema, String as String$1, pipe } from "effect";
|
|
2
2
|
// src/schema.ts
|
|
3
3
|
const NONE = 0;
|
|
4
4
|
const MINIMUM_LABELS = 2;
|
|
5
5
|
const NonNegativeInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(NONE));
|
|
6
|
-
const Probability = Schema.
|
|
6
|
+
const Probability = Schema.Finite.check(Schema.isBetween({
|
|
7
7
|
minimum: 0,
|
|
8
8
|
maximum: 1
|
|
9
9
|
}));
|
|
10
10
|
const CONFIDENT = .8;
|
|
11
|
-
const Cutoff = Schema.
|
|
11
|
+
const Cutoff = Schema.Finite.check(Schema.isBetween({
|
|
12
12
|
minimum: CONFIDENT,
|
|
13
13
|
maximum: 1
|
|
14
14
|
}, { message: `cutoff must be between ${CONFIDENT} and 1` }));
|
|
@@ -52,7 +52,7 @@ const ClassifyAnswer = Schema.Struct({
|
|
|
52
52
|
kind: Schema.Literal("classify"),
|
|
53
53
|
labels: Schema.Record(Label, Schema.String).check(Schema.isMinProperties(MINIMUM_LABELS)),
|
|
54
54
|
report: Schema.NonEmptyArray(Label)
|
|
55
|
-
}).check(Schema.makeFilter(({ labels, report }) => report.every((label) =>
|
|
55
|
+
}).check(Schema.makeFilter(({ labels, report }) => pipe(report, Array.every((label) => Record.has(labels, label))), { message: "every label in report must be one of labels" }));
|
|
56
56
|
const SeverityAnswer = Schema.Struct({
|
|
57
57
|
kind: Schema.Literal("severity"),
|
|
58
58
|
minimum: Schema.optionalKey(SeveritySchema)
|
|
@@ -63,6 +63,7 @@ const RuleAnswerSchema = Schema.Union([
|
|
|
63
63
|
SeverityAnswer
|
|
64
64
|
]);
|
|
65
65
|
// src/answer.ts
|
|
66
|
+
const DATA_FIRST_ARITY$6 = 2;
|
|
66
67
|
const YES = "yes";
|
|
67
68
|
const DEFAULT_MINIMUM = "warn";
|
|
68
69
|
const SEVERITY_CRITERIA = {
|
|
@@ -71,7 +72,7 @@ const SEVERITY_CRITERIA = {
|
|
|
71
72
|
warn: "Likely a problem",
|
|
72
73
|
error: "Clearly a problem"
|
|
73
74
|
};
|
|
74
|
-
const SEVERITY_ORDER =
|
|
75
|
+
const SEVERITY_ORDER = Record.keys(SEVERITY_CRITERIA);
|
|
75
76
|
const BOOLEAN_ANSWER = { kind: "boolean" };
|
|
76
77
|
const decodeAnswer = Schema.decodeUnknownResult(RuleAnswerSchema);
|
|
77
78
|
const decodeRuleAnswer = (answer) => {
|
|
@@ -96,7 +97,7 @@ const isChoice = (answer) => answer.type === "choice";
|
|
|
96
97
|
const noulOf = (answer) => Option.fromUndefinedOr(answer).pipe(Option.filter(isNoul), Option.flatMap(decodeNoul));
|
|
97
98
|
const choiceOf = (answer) => Option.fromUndefinedOr(answer).pipe(Option.filter(isChoice), Option.flatMap(decodeChoice));
|
|
98
99
|
const probabilityOfChoice = ({ choice, probabilities }) => Option.fromUndefinedOr(probabilities).pipe(Option.flatMap((byLabel) => Option.fromUndefinedOr(byLabel[choice])));
|
|
99
|
-
const questionFor = (answer, instructions) => {
|
|
100
|
+
const questionFor = Function.dual(DATA_FIRST_ARITY$6, (answer, instructions) => {
|
|
100
101
|
switch (answer.kind) {
|
|
101
102
|
case "classify": return {
|
|
102
103
|
type: "choice",
|
|
@@ -117,18 +118,18 @@ const questionFor = (answer, instructions) => {
|
|
|
117
118
|
instructions
|
|
118
119
|
};
|
|
119
120
|
}
|
|
120
|
-
};
|
|
121
|
-
const choiceVerdictOf = (criteria, answer) => choiceOf(answer).pipe(Option.filter(({ choice }) =>
|
|
121
|
+
});
|
|
122
|
+
const choiceVerdictOf = (criteria, answer) => choiceOf(answer).pipe(Option.filter(({ choice }) => Record.has(criteria, choice)), Option.flatMap((chosen) => Option.map(probabilityOfChoice(chosen), (probability) => ({
|
|
122
123
|
answer: chosen.choice,
|
|
123
124
|
probability
|
|
124
125
|
}))));
|
|
125
|
-
const verdictOf = (question, answer) => {
|
|
126
|
+
const verdictOf = Function.dual(DATA_FIRST_ARITY$6, (question, answer) => {
|
|
126
127
|
if (question.type === "choice") return choiceVerdictOf(question.criteria, answer);
|
|
127
128
|
return noulOf(answer).pipe(Option.map(({ noul }) => ({
|
|
128
129
|
answer: YES,
|
|
129
130
|
probability: noul
|
|
130
131
|
})));
|
|
131
|
-
};
|
|
132
|
+
});
|
|
132
133
|
const severityMatches = ({ minimum = DEFAULT_MINIMUM }, label) => SEVERITY_ORDER.indexOf(label) >= SEVERITY_ORDER.indexOf(minimum);
|
|
133
134
|
const answerMatches = (answer, label) => {
|
|
134
135
|
switch (answer.kind) {
|
|
@@ -138,37 +139,13 @@ const answerMatches = (answer, label) => {
|
|
|
138
139
|
default: return false;
|
|
139
140
|
}
|
|
140
141
|
};
|
|
141
|
-
const isReported = (rule, verdict) => verdict.probability >= rule.cutoff && answerMatches(rule.answer, verdict.answer);
|
|
142
|
+
const isReported = Function.dual(DATA_FIRST_ARITY$6, (rule, verdict) => verdict.probability >= rule.cutoff && answerMatches(rule.answer, verdict.answer));
|
|
142
143
|
// src/options.ts
|
|
144
|
+
const DATA_FIRST_ARITY_3$1 = 3;
|
|
145
|
+
const DATA_FIRST_ARITY$5 = 2;
|
|
143
146
|
const PREFIX = "oxlint-plugin-decision";
|
|
144
147
|
const PROVIDER_SEPARATOR = "/";
|
|
145
|
-
const FIRST_LINE$1 = 1;
|
|
146
148
|
const ONE_RULE = 1;
|
|
147
|
-
const FIRST_COLUMN = 0;
|
|
148
|
-
const TARGET_NODE_TYPES = {
|
|
149
|
-
function: [
|
|
150
|
-
"FunctionDeclaration",
|
|
151
|
-
"FunctionExpression",
|
|
152
|
-
"ArrowFunctionExpression"
|
|
153
|
-
],
|
|
154
|
-
call: ["CallExpression"],
|
|
155
|
-
jsx: ["JSXElement"],
|
|
156
|
-
file: ["Program"]
|
|
157
|
-
};
|
|
158
|
-
const NAMED_PARENTS = {
|
|
159
|
-
VariableDeclarator: "init",
|
|
160
|
-
Property: "value",
|
|
161
|
-
MethodDefinition: "value",
|
|
162
|
-
PropertyDefinition: "value"
|
|
163
|
-
};
|
|
164
|
-
const isNamedParentType = (type) => Object.hasOwn(NAMED_PARENTS, type);
|
|
165
|
-
const ANONYMOUS_FUNCTIONS = /* @__PURE__ */ new Set(["FunctionExpression", "ArrowFunctionExpression"]);
|
|
166
|
-
const holdsAsName = (parent, node) => {
|
|
167
|
-
if (!isNamedParentType(parent.type)) return false;
|
|
168
|
-
const field = NAMED_PARENTS[parent.type];
|
|
169
|
-
return ANONYMOUS_FUNCTIONS.has(node.type) && parent[field] === node;
|
|
170
|
-
};
|
|
171
|
-
const snippetNodeFor = (node) => Option.fromNullishOr(node.parent).pipe(Option.filter((parent) => holdsAsName(parent, node)), Option.getOrElse(() => node));
|
|
172
149
|
const DEFAULTS = {
|
|
173
150
|
ci: "fail",
|
|
174
151
|
timeoutMs: 1e4,
|
|
@@ -208,52 +185,49 @@ const RuleSchema = Schema.Struct({
|
|
|
208
185
|
const STRICT = { onExcessProperty: "error" };
|
|
209
186
|
const decodeOptions = Schema.decodeUnknownResult(OptionsSchema, STRICT);
|
|
210
187
|
const decodeRule = Schema.decodeUnknownResult(RuleSchema, STRICT);
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
this.name = "OptionsError";
|
|
215
|
-
}
|
|
216
|
-
};
|
|
188
|
+
const taggedError = Schema.TaggedError;
|
|
189
|
+
var OptionsError = class extends taggedError()("OptionsError", { message: Schema.String }) {};
|
|
190
|
+
const optionsError = (message) => OptionsError.make({ message: `${PREFIX}: ${message}` });
|
|
217
191
|
const modelIdsOf = (spec) => Array.dedupe(Array.ensure(spec));
|
|
218
192
|
const splitModelId = (id) => {
|
|
219
193
|
const [provider, ...rest] = String$1.split(id, PROVIDER_SEPARATOR);
|
|
220
|
-
const model = rest.join(PROVIDER_SEPARATOR);
|
|
194
|
+
const model = pipe(rest, Array.join(PROVIDER_SEPARATOR));
|
|
221
195
|
return Option.liftPredicate([provider, model], ([left, right]) => String$1.isNonEmpty(left) && String$1.isNonEmpty(right));
|
|
222
196
|
};
|
|
223
|
-
const resolveModel = (id, providers) => splitModelId(id).pipe(Option.flatMap(([name, model]) => Option.map(Option.fromUndefinedOr(providers[name]), (provider) => ({
|
|
197
|
+
const resolveModel = Function.dual(DATA_FIRST_ARITY$5, (id, providers) => splitModelId(id).pipe(Option.flatMap(([name, model]) => Option.map(Option.fromUndefinedOr(providers[name]), (provider) => ({
|
|
224
198
|
id,
|
|
225
199
|
provider,
|
|
226
200
|
model
|
|
227
|
-
}))), Option.getOrThrowWith(() =>
|
|
228
|
-
const duplicateIdOf = (rules) => Array.findFirst(
|
|
201
|
+
}))), Option.getOrThrowWith(() => optionsError(`model "${id}" must be "<provider>/<model>" with a provider from: ${pipe(Record.keys(providers), Array.join(", "))}`))));
|
|
202
|
+
const duplicateIdOf = (rules) => Array.findFirst(Record.toEntries(Array.groupBy(rules, ({ id }) => id)), ([, same]) => same.length > ONE_RULE).pipe(Option.map(([id]) => id));
|
|
229
203
|
const answerOf = (rule) => {
|
|
230
204
|
if (Predicate.isUndefined(rule.answer)) return BOOLEAN_ANSWER;
|
|
231
|
-
return Result.getOrThrowWith(decodeRuleAnswer(rule.answer), (issue) =>
|
|
205
|
+
return Result.getOrThrowWith(decodeRuleAnswer(rule.answer), (issue) => optionsError(`rule "${rule.id}" has an invalid answer: ${issue}`));
|
|
232
206
|
};
|
|
233
|
-
const checked = (decode, input, what) => Result.getOrThrowWith(decode(input), (issue) =>
|
|
207
|
+
const checked = Function.dual(DATA_FIRST_ARITY_3$1, (decode, input, what) => Result.getOrThrowWith(decode(input), (issue) => optionsError(`invalid ${what}: ${issue.message}`)));
|
|
234
208
|
const checkRule = (rule) => {
|
|
235
209
|
checked(decodeRule, rule, `rule "${rule.id}"`);
|
|
236
|
-
if (Predicate.isNotUndefined(rule.location) && rule.target !== "file") throw
|
|
210
|
+
if (Predicate.isNotUndefined(rule.location) && rule.target !== "file") throw optionsError("location is supported only for file rules");
|
|
237
211
|
answerOf(rule);
|
|
238
212
|
return rule;
|
|
239
213
|
};
|
|
240
214
|
const checkRules = (rules) => {
|
|
241
|
-
if (!Array.isReadonlyArrayNonEmpty(rules)) throw
|
|
215
|
+
if (!Array.isReadonlyArrayNonEmpty(rules)) throw optionsError("rules must list at least one rule");
|
|
242
216
|
Option.map(duplicateIdOf(rules), (duplicate) => {
|
|
243
|
-
throw
|
|
217
|
+
throw optionsError(`rule id "${duplicate}" is used more than once`);
|
|
244
218
|
});
|
|
245
|
-
return rules.map((rule) => checkRule(rule));
|
|
219
|
+
return pipe(rules, Array.map((rule) => checkRule(rule)));
|
|
246
220
|
};
|
|
247
221
|
const resolveRule = (fallback, providers) => (rule) => ({
|
|
248
222
|
...rule,
|
|
249
223
|
answer: answerOf(rule),
|
|
250
|
-
models: modelIdsOf(rule.model ?? fallback).map((id) => resolveModel(id, providers))
|
|
224
|
+
models: pipe(modelIdsOf(rule.model ?? fallback), Array.map((id) => resolveModel(id, providers)))
|
|
251
225
|
});
|
|
252
226
|
const settingsFrom = (settings) => {
|
|
253
227
|
if (Predicate.isUndefined(settings)) return {};
|
|
254
228
|
return checked(decodeOptions, settings, "settings");
|
|
255
229
|
};
|
|
256
|
-
const resolveOptions = ({ options, settings }, rules, providers) => {
|
|
230
|
+
const resolveOptions = Function.dual(DATA_FIRST_ARITY_3$1, ({ options, settings }, rules, providers) => {
|
|
257
231
|
const fromCode = checked(decodeOptions, options, "createDecisionPlugin options");
|
|
258
232
|
const { model, ...merged } = {
|
|
259
233
|
...DEFAULTS,
|
|
@@ -266,67 +240,42 @@ const resolveOptions = ({ options, settings }, rules, providers) => {
|
|
|
266
240
|
maxSnippetChars: merged.maxSnippetChars,
|
|
267
241
|
env: merged.env,
|
|
268
242
|
explain: merged.explain,
|
|
269
|
-
rules: checkRules(rules).map((rule) => resolveRule(model, providers)(rule))
|
|
243
|
+
rules: pipe(checkRules(rules), Array.map((rule) => resolveRule(model, providers)(rule)))
|
|
270
244
|
};
|
|
271
|
-
};
|
|
272
|
-
const headOf = (text, line, column) => {
|
|
273
|
-
const [first] = String$1.split(text, "\n");
|
|
274
|
-
return {
|
|
275
|
-
start: {
|
|
276
|
-
line,
|
|
277
|
-
column
|
|
278
|
-
},
|
|
279
|
-
end: {
|
|
280
|
-
line,
|
|
281
|
-
column: column + first.length
|
|
282
|
-
}
|
|
283
|
-
};
|
|
284
|
-
};
|
|
285
|
-
const REPORT_LOC = {
|
|
286
|
-
function: (node, text) => headOf(text, node.loc.start.line, node.loc.start.column),
|
|
287
|
-
call: (node) => node.loc,
|
|
288
|
-
jsx: (node) => Option.getOrElse(Option.fromNullishOr(node.openingElement), () => node).loc,
|
|
289
|
-
file: (_node, text) => headOf(text, FIRST_LINE$1, FIRST_COLUMN)
|
|
290
|
-
};
|
|
245
|
+
});
|
|
291
246
|
// src/runtime.ts
|
|
292
|
-
const
|
|
247
|
+
const DATA_FIRST_ARITY$4 = 2;
|
|
248
|
+
const encodeJson = Schema.encodeUnknownOption(Schema.fromJsonString(Schema.Unknown));
|
|
249
|
+
const toJson = (value) => Option.getOrElse(encodeJson(value), () => String(value));
|
|
250
|
+
const nowMs = () => DateTime.toEpochMillis(DateTime.nowUnsafe());
|
|
251
|
+
const runOrElse = Function.dual(DATA_FIRST_ARITY$4, (effect, fallback) => Exit.match(Effect.runSyncExit(effect), {
|
|
293
252
|
onSuccess: (value) => value,
|
|
294
253
|
onFailure: (cause) => fallback(Cause.squash(cause))
|
|
295
|
-
});
|
|
296
|
-
const envOf = (name) => runOrElse(Config.option(Config.NonEmptyString(name)), () => Option.none()).pipe(Option.map((value) =>
|
|
297
|
-
const envNameOf = (env, name) => env[name] ?? name;
|
|
254
|
+
}));
|
|
255
|
+
const envOf = (name) => runOrElse(Config.option(Config.NonEmptyString(name)), () => Option.none()).pipe(Option.map((value) => String$1.trim(value)), Option.filter(String$1.isNonEmpty));
|
|
256
|
+
const envNameOf = Function.dual(DATA_FIRST_ARITY$4, (env, name) => env[name] ?? name);
|
|
298
257
|
const failsInCi = (options) => options.ci === "fail" && Option.isSome(envOf("CI"));
|
|
299
258
|
const warned = /* @__PURE__ */ new Set();
|
|
300
|
-
const warnOnce = (reason, message) => {
|
|
259
|
+
const warnOnce = Function.dual(DATA_FIRST_ARITY$4, (reason, message) => {
|
|
301
260
|
if (warned.has(reason)) return;
|
|
302
261
|
warned.add(reason);
|
|
303
262
|
Effect.runFork(Console.warn(`${PREFIX}: ${message}`));
|
|
304
|
-
};
|
|
263
|
+
});
|
|
305
264
|
const messageOf = (error) => {
|
|
306
265
|
if (error instanceof Error) return error.message;
|
|
307
266
|
return String(error);
|
|
308
267
|
};
|
|
309
|
-
// src/
|
|
268
|
+
// src/location.ts
|
|
269
|
+
const DATA_FIRST_ARITY$3 = 2;
|
|
270
|
+
const DATA_FIRST_ARITY_3 = 3;
|
|
310
271
|
const CHOICE_OPTION_LIMIT = 254;
|
|
311
|
-
const FIRST_LINE = 1;
|
|
272
|
+
const FIRST_LINE$1 = 1;
|
|
312
273
|
const MINIMUM_WIDTH = 1;
|
|
313
274
|
const UNKNOWN_CHOICE = "unknown";
|
|
314
|
-
const
|
|
315
|
-
const TRUNCATION_MARKER = "/* ...truncated */";
|
|
316
|
-
const refAt = (index) => `s${String(index)}`;
|
|
317
|
-
const locationRefOf = (ref) => `${ref}${LOCATION_SUFFIX}`;
|
|
318
|
-
const snippetRefOf = (questionId) => Boolean.match(questionId.endsWith(LOCATION_SUFFIX), {
|
|
319
|
-
onTrue: () => questionId.slice(Number$1.decrement(FIRST_LINE), -9),
|
|
320
|
-
onFalse: () => questionId
|
|
321
|
-
});
|
|
322
|
-
const truncateSnippet = (text, maxChars) => Boolean.match(text.length <= maxChars, {
|
|
323
|
-
onTrue: () => text,
|
|
324
|
-
onFalse: () => `${text.slice(Number$1.decrement(FIRST_LINE), maxChars)}${TRUNCATION_MARKER}`
|
|
325
|
-
});
|
|
326
|
-
const sourceLines = (text) => text.split(/\r\n|[\n\r\u2028\u2029]/u);
|
|
275
|
+
const sourceLines = (text) => String$1.split(text, /\r\n|[\n\r\u2028\u2029]/u);
|
|
327
276
|
const locates = (match) => Predicate.isNotUndefined(match.rule.location) && !match.truncated;
|
|
328
277
|
const EVERY_LINE = {
|
|
329
|
-
start: FIRST_LINE,
|
|
278
|
+
start: FIRST_LINE$1,
|
|
330
279
|
end: Number.POSITIVE_INFINITY
|
|
331
280
|
};
|
|
332
281
|
const isWithin = (line, within) => Number$1.between(line, {
|
|
@@ -345,42 +294,57 @@ const rangeOf = (group) => ({
|
|
|
345
294
|
start: Array.headNonEmpty(group).line,
|
|
346
295
|
end: Array.lastNonEmpty(group).line
|
|
347
296
|
});
|
|
348
|
-
const numberedLines = (snippet) => sourceLines(snippet).map((text, index) => ({
|
|
297
|
+
const numberedLines = (snippet) => pipe(sourceLines(snippet), Array.map((text, index) => ({
|
|
349
298
|
text,
|
|
350
|
-
line: Number$1.sum(index, FIRST_LINE)
|
|
351
|
-
}));
|
|
352
|
-
const locationCandidates = (snippet, within
|
|
353
|
-
const lines = numberedLines(snippet).filter(({ text, line }) =>
|
|
299
|
+
line: Number$1.sum(index, FIRST_LINE$1)
|
|
300
|
+
})));
|
|
301
|
+
const locationCandidates = Function.dual(DATA_FIRST_ARITY$3, (snippet, within) => {
|
|
302
|
+
const lines = pipe(numberedLines(snippet), Array.filter(({ text, line }) => String$1.trim(text).length >= FIRST_LINE$1 && isWithin(line, within)));
|
|
354
303
|
const width = Math.max(MINIMUM_WIDTH, Math.ceil(lines.length / CHOICE_OPTION_LIMIT));
|
|
355
|
-
return
|
|
304
|
+
return Record.fromEntries(pipe(Array.chunksOf(lines, width), Array.map((group) => {
|
|
356
305
|
const range = rangeOf(group);
|
|
357
306
|
return [rangeKey(range), range];
|
|
358
|
-
}));
|
|
359
|
-
};
|
|
360
|
-
const locationQuestion = (match, ref, candidates) => ({
|
|
307
|
+
})));
|
|
308
|
+
});
|
|
309
|
+
const locationQuestion = Function.dual(DATA_FIRST_ARITY_3, (match, ref, candidates) => ({
|
|
361
310
|
type: "choice",
|
|
362
311
|
instructions: `Consider only the numbered source in state.snippets.${ref}. If it violates this rule, select the line (or range containing that line) to annotate. Rule: ${match.rule.question} Location: ${match.rule.location.question} If multiple violations exist, select the earliest one. Select ${UNKNOWN_CHOICE} if no violation exists or its location is unclear.`,
|
|
363
312
|
criteria: {
|
|
364
|
-
...
|
|
313
|
+
...Record.fromEntries(pipe(Record.toEntries(candidates), Array.map(([key, range]) => [key, rangeLabel(range)]))),
|
|
365
314
|
[UNKNOWN_CHOICE]: "No clear source location for this violation"
|
|
366
315
|
}
|
|
367
|
-
});
|
|
368
|
-
const validChoice = (answer, criteria) => Option.exists(choiceOf(answer), (chosen) =>
|
|
369
|
-
const selectedLocation = (answer, candidates, cutoff) => choiceOf(answer).pipe(Option.filter((chosen) => Option.exists(probabilityOfChoice(chosen), (probability) => Number$1.isGreaterThanOrEqualTo(probability, cutoff))), Option.flatMap(({ choice }) => Option.fromUndefinedOr(candidates[choice])));
|
|
370
|
-
const locationOfLine = (snippet, line) => {
|
|
316
|
+
}));
|
|
317
|
+
const validChoice = Function.dual(DATA_FIRST_ARITY$3, (answer, criteria) => Option.exists(choiceOf(answer), (chosen) => Record.has(criteria, chosen.choice) && Option.isSome(probabilityOfChoice(chosen))));
|
|
318
|
+
const selectedLocation = Function.dual(DATA_FIRST_ARITY_3, (answer, candidates, cutoff) => choiceOf(answer).pipe(Option.filter((chosen) => Option.exists(probabilityOfChoice(chosen), (probability) => Number$1.isGreaterThanOrEqualTo(probability, cutoff))), Option.flatMap(({ choice }) => Option.fromUndefinedOr(candidates[choice]))));
|
|
319
|
+
const locationOfLine = Function.dual(DATA_FIRST_ARITY$3, (snippet, line) => {
|
|
371
320
|
const text = Option.getOrElse(Array.get(sourceLines(snippet), Number$1.decrement(line)), () => "");
|
|
372
321
|
return {
|
|
373
322
|
start: {
|
|
374
323
|
line,
|
|
375
|
-
column:
|
|
324
|
+
column: Option.getOrElse(String$1.search(/\S/u)(text), () => Number$1.decrement(FIRST_LINE$1))
|
|
376
325
|
},
|
|
377
326
|
end: {
|
|
378
327
|
line,
|
|
379
|
-
column:
|
|
328
|
+
column: String$1.trimEnd(text).length
|
|
380
329
|
}
|
|
381
330
|
};
|
|
382
|
-
};
|
|
383
|
-
|
|
331
|
+
});
|
|
332
|
+
// src/request.ts
|
|
333
|
+
const DATA_FIRST_ARITY$2 = 2;
|
|
334
|
+
const FIRST_LINE = 1;
|
|
335
|
+
const LOCATION_SUFFIX = "_location";
|
|
336
|
+
const TRUNCATION_MARKER = "/* ...truncated */";
|
|
337
|
+
const refAt = (index) => `s${String(index)}`;
|
|
338
|
+
const locationRefOf = (ref) => `${ref}${LOCATION_SUFFIX}`;
|
|
339
|
+
const snippetRefOf = (questionId) => Boolean.match(String$1.endsWith(LOCATION_SUFFIX)(questionId), {
|
|
340
|
+
onTrue: () => String$1.slice(Number$1.decrement(FIRST_LINE), -9)(questionId),
|
|
341
|
+
onFalse: () => questionId
|
|
342
|
+
});
|
|
343
|
+
const truncateSnippet = Function.dual(DATA_FIRST_ARITY$2, (text, maxChars) => Boolean.match(text.length <= maxChars, {
|
|
344
|
+
onTrue: () => text,
|
|
345
|
+
onFalse: () => `${String$1.slice(Number$1.decrement(FIRST_LINE), maxChars)(text)}${TRUNCATION_MARKER}`
|
|
346
|
+
}));
|
|
347
|
+
const numbered = (snippet) => pipe(numberedLines(snippet), Array.map(({ text, line }) => `L${String(line)}| ${text}`), Array.join("\n"));
|
|
384
348
|
const contextNote = (match, ref) => {
|
|
385
349
|
if (Predicate.isUndefined(match.context)) return "";
|
|
386
350
|
return ` state.contexts.${ref} shows the source around it; use it only to understand the snippet.`;
|
|
@@ -388,105 +352,57 @@ const contextNote = (match, ref) => {
|
|
|
388
352
|
const mainQuestionFor = (match, ref) => [ref, questionFor(match.rule.answer, `Consider only snippet "${ref}" in state.snippets.${contextNote(match, ref)} ${match.rule.question}`)];
|
|
389
353
|
const questionsFor = (match, ref) => {
|
|
390
354
|
if (!locates(match)) return [mainQuestionFor(match, ref)];
|
|
391
|
-
return [mainQuestionFor(match, ref), [locationRefOf(ref), locationQuestion(match, ref, locationCandidates(match.snippet))]];
|
|
355
|
+
return [mainQuestionFor(match, ref), [locationRefOf(ref), locationQuestion(match, ref, locationCandidates(match.snippet, EVERY_LINE))]];
|
|
392
356
|
};
|
|
393
357
|
const snippetFor = (match) => {
|
|
394
358
|
if (locates(match)) return numbered(match.snippet);
|
|
395
359
|
return match.snippet;
|
|
396
360
|
};
|
|
397
361
|
const contextsOf = (matches) => {
|
|
398
|
-
const contexts = matches.flatMap((match, index) => Option.toArray(Option.map(Option.fromUndefinedOr(match.context), (text) => [refAt(index), text])));
|
|
362
|
+
const contexts = pipe(matches, Array.flatMap((match, index) => Option.toArray(Option.map(Option.fromUndefinedOr(match.context), (text) => [refAt(index), text]))));
|
|
399
363
|
if (Array.isReadonlyArrayEmpty(contexts)) return {};
|
|
400
|
-
return { contexts:
|
|
364
|
+
return { contexts: Record.fromEntries(contexts) };
|
|
401
365
|
};
|
|
402
|
-
const buildRequest = (model, matches) => ({
|
|
366
|
+
const buildRequest = Function.dual(DATA_FIRST_ARITY$2, (model, matches) => ({
|
|
403
367
|
model,
|
|
404
368
|
state: {
|
|
405
|
-
snippets:
|
|
369
|
+
snippets: Record.fromEntries(pipe(matches, Array.map((match, index) => [refAt(index), snippetFor(match)]))),
|
|
406
370
|
...contextsOf(matches)
|
|
407
371
|
},
|
|
408
|
-
questions:
|
|
409
|
-
});
|
|
410
|
-
const choiceQuestionsOf$1 = (request) =>
|
|
372
|
+
questions: Record.fromEntries(pipe(matches, Array.flatMap((match, index) => questionsFor(match, refAt(index)))))
|
|
373
|
+
}));
|
|
374
|
+
const choiceQuestionsOf$1 = (request) => pipe(Record.toEntries(request.questions), Array.flatMap(([id, question]) => {
|
|
411
375
|
if (question.type === "choice") return [[id, question.criteria]];
|
|
412
376
|
return [];
|
|
413
|
-
});
|
|
414
|
-
const mainQuestionsOf = (request) =>
|
|
377
|
+
}));
|
|
378
|
+
const mainQuestionsOf = (request) => pipe(Record.toEntries(request.questions), Array.filter(([id]) => !String$1.endsWith(LOCATION_SUFFIX)(id)));
|
|
415
379
|
const resultOf = (answers, [ref, question]) => [ref, Option.getOrThrowWith(verdictOf(question, answers[ref]), () => /* @__PURE__ */ new Error(`response has no valid answer for "${ref}"`))];
|
|
416
|
-
const parseVerdicts = (response, request) => {
|
|
380
|
+
const parseVerdicts = Function.dual(DATA_FIRST_ARITY$2, (response, request) => {
|
|
417
381
|
const { model, answers } = Option.getOrThrowWith(Option.liftPredicate(response, ({ model: served }) => String$1.isNonEmpty(served)), () => /* @__PURE__ */ new Error("response has no model id"));
|
|
418
382
|
return {
|
|
419
383
|
model,
|
|
420
|
-
results:
|
|
384
|
+
results: Record.fromEntries(pipe(mainQuestionsOf(request), Array.map((entry) => resultOf(answers, entry)))),
|
|
421
385
|
answers,
|
|
422
|
-
complete: choiceQuestionsOf$1(request).every(([id, criteria]) => validChoice(answers[id], criteria))
|
|
386
|
+
complete: pipe(choiceQuestionsOf$1(request), Array.every(([id, criteria]) => validChoice(answers[id], criteria)))
|
|
423
387
|
};
|
|
424
|
-
};
|
|
425
|
-
// src/wire.ts
|
|
388
|
+
});
|
|
389
|
+
// src/wire-choice.ts
|
|
390
|
+
const DATA_FIRST_ARITY$1 = 2;
|
|
426
391
|
const RESPAN_MODELS = ["respan/"];
|
|
427
392
|
const OPTION_SEPARATOR = "__";
|
|
428
393
|
const CERTAIN = 1;
|
|
429
394
|
const NO_SEVERITY = "none";
|
|
430
|
-
const
|
|
431
|
-
const
|
|
432
|
-
const
|
|
433
|
-
const
|
|
434
|
-
onNone: () => ({}),
|
|
435
|
-
onSome: (text) => ({ [ref]: text })
|
|
436
|
-
});
|
|
437
|
-
const stateOf = ({ snippets, contexts }, ref) => {
|
|
438
|
-
if (Predicate.isUndefined(contexts)) return { snippets: only(snippets, ref) };
|
|
439
|
-
return {
|
|
440
|
-
snippets: only(snippets, ref),
|
|
441
|
-
contexts: only(contexts, ref)
|
|
442
|
-
};
|
|
443
|
-
};
|
|
444
|
-
const optionIdOf = (id, label) => `${id}${OPTION_SEPARATOR}${label}`;
|
|
445
|
-
const isSeverityScale = (criteria) => Object.keys(criteria).join(",") === SEVERITY_ORDER.join(",");
|
|
446
|
-
const optionText = (label, description) => Option.match(Option.fromNullishOr(description), {
|
|
447
|
-
onNone: () => `"${label}"`,
|
|
448
|
-
onSome: (text) => `"${label}" (${text})`
|
|
449
|
-
});
|
|
450
|
-
const labelQuestion = (instructions, label, description) => ({
|
|
451
|
-
type: "noul",
|
|
452
|
-
instructions: `${instructions} Does the label ${optionText(label, description)} fit?`
|
|
453
|
-
});
|
|
454
|
-
const levelQuestion = (instructions, label, description) => ({
|
|
455
|
-
type: "noul",
|
|
456
|
-
instructions: `${instructions} Is it at least as serious as ${optionText(label, description)}?`
|
|
457
|
-
});
|
|
458
|
-
const levelsAboveNone = (criteria) => Object.entries(criteria).filter(([label]) => label !== NO_SEVERITY);
|
|
459
|
-
const yesNoQuestionsOf = (id, { instructions, criteria }) => {
|
|
460
|
-
if (isSeverityScale(criteria)) return levelsAboveNone(criteria).map(([label, description]) => [optionIdOf(id, label), levelQuestion(instructions, label, description)]);
|
|
461
|
-
return Object.entries(criteria).map(([label, description]) => [optionIdOf(id, label), labelQuestion(instructions, label, description)]);
|
|
462
|
-
};
|
|
463
|
-
const asYesNo = ([id, question]) => {
|
|
464
|
-
if (question.type === "choice") return yesNoQuestionsOf(id, question);
|
|
465
|
-
return [[id, question]];
|
|
466
|
-
};
|
|
467
|
-
const perSnippet = (request) => {
|
|
468
|
-
const wires = Object.values(Array.groupBy(Object.entries(request.questions), ([id]) => snippetRefOf(id))).map((questions) => {
|
|
469
|
-
const [[id]] = questions;
|
|
470
|
-
return {
|
|
471
|
-
model: request.model,
|
|
472
|
-
state: stateAsText(stateOf(request.state, snippetRefOf(id))),
|
|
473
|
-
questions: Object.fromEntries(questions.flatMap((entry) => asYesNo(entry)))
|
|
474
|
-
};
|
|
475
|
-
});
|
|
476
|
-
if (Array.isReadonlyArrayNonEmpty(wires)) return wires;
|
|
477
|
-
return [request];
|
|
478
|
-
};
|
|
479
|
-
const wireRequestsFor = (request) => {
|
|
480
|
-
if (isRespan(request.model)) return perSnippet(request);
|
|
481
|
-
return [request];
|
|
482
|
-
};
|
|
395
|
+
const isRespan = (model) => pipe(RESPAN_MODELS, Array.some((prefix) => String$1.startsWith(prefix)(model)));
|
|
396
|
+
const optionIdOf = Function.dual(DATA_FIRST_ARITY$1, (id, label) => `${id}${OPTION_SEPARATOR}${label}`);
|
|
397
|
+
const isSeverityScale = (criteria) => pipe(Record.keys(criteria), Array.join(",")) === pipe(SEVERITY_ORDER, Array.join(","));
|
|
398
|
+
const levelsAboveNone = (criteria) => pipe(Record.toEntries(criteria), Array.filter(([label]) => label !== NO_SEVERITY));
|
|
483
399
|
const byYes = Order.mapInput(Order.Number, ({ yes }) => yes);
|
|
484
400
|
const scoreOf = (answers, id, label) => Option.map(noulOf(answers[optionIdOf(id, label)]), ({ noul }) => ({
|
|
485
401
|
label,
|
|
486
402
|
yes: noul
|
|
487
403
|
}));
|
|
488
|
-
const scoresOf = (answers, id, labels) =>
|
|
489
|
-
const probabilitiesOf = (scores) =>
|
|
404
|
+
const scoresOf = (answers, id, labels) => pipe(labels, Array.map((label) => scoreOf(answers, id, label)), Option.all, Option.flatMap((scores) => Option.liftPredicate(scores, Array.isArrayNonEmpty)));
|
|
405
|
+
const probabilitiesOf = (scores) => Record.fromEntries(pipe(scores, Array.map(({ label, yes }) => [label, yes])));
|
|
490
406
|
const choiceAnswer = (choice, probabilities) => ({
|
|
491
407
|
type: "choice",
|
|
492
408
|
choice,
|
|
@@ -495,7 +411,7 @@ const choiceAnswer = (choice, probabilities) => ({
|
|
|
495
411
|
const bestLabel = (scores) => choiceAnswer(Array.max(scores, byYes).label, probabilitiesOf(scores));
|
|
496
412
|
const atLeast = (scores) => pipe(scores, Array.map(({ label }, index) => ({
|
|
497
413
|
label,
|
|
498
|
-
yes: Math.max(...scores.slice(index).map(({ yes }) => yes))
|
|
414
|
+
yes: Math.max(...pipe(scores.slice(index), Array.map(({ yes }) => yes)))
|
|
499
415
|
})));
|
|
500
416
|
const severityLevel = (scores) => {
|
|
501
417
|
const cumulative = atLeast(scores);
|
|
@@ -510,8 +426,8 @@ const severityLevel = (scores) => {
|
|
|
510
426
|
}), probabilities);
|
|
511
427
|
};
|
|
512
428
|
const labelsAsked = (criteria) => {
|
|
513
|
-
if (isSeverityScale(criteria)) return levelsAboveNone(criteria).map(([label]) => label);
|
|
514
|
-
return
|
|
429
|
+
if (isSeverityScale(criteria)) return pipe(levelsAboveNone(criteria), Array.map(([label]) => label));
|
|
430
|
+
return Record.keys(criteria);
|
|
515
431
|
};
|
|
516
432
|
const choiceFromYesNo = (answers, [id, { criteria }]) => {
|
|
517
433
|
const collapse = Boolean.match(isSeverityScale(criteria), {
|
|
@@ -520,21 +436,73 @@ const choiceFromYesNo = (answers, [id, { criteria }]) => {
|
|
|
520
436
|
});
|
|
521
437
|
return scoresOf(answers, id, labelsAsked(criteria)).pipe(Option.map((scores) => [id, collapse(scores)]), Option.toArray);
|
|
522
438
|
};
|
|
523
|
-
const choiceQuestionsOf = (request) =>
|
|
439
|
+
const choiceQuestionsOf = (request) => pipe(Record.toEntries(request.questions), Array.flatMap(([id, question]) => {
|
|
524
440
|
if (question.type === "choice") return [[id, question]];
|
|
525
441
|
return [];
|
|
526
|
-
});
|
|
527
|
-
const withChoices = (request, answers) => {
|
|
442
|
+
}));
|
|
443
|
+
const withChoices = Function.dual(DATA_FIRST_ARITY$1, (request, answers) => {
|
|
528
444
|
if (!isRespan(request.model)) return answers;
|
|
529
445
|
return {
|
|
530
446
|
...answers,
|
|
531
|
-
...
|
|
447
|
+
...Record.fromEntries(pipe(choiceQuestionsOf(request), Array.flatMap((entry) => choiceFromYesNo(answers, entry))))
|
|
448
|
+
};
|
|
449
|
+
});
|
|
450
|
+
// src/wire.ts
|
|
451
|
+
const DATA_FIRST_ARITY = 2;
|
|
452
|
+
const sectionsOf = (name, entries) => pipe(Record.toEntries(entries), Array.map(([ref, text]) => `state.${name}.${ref}:\n${text}`));
|
|
453
|
+
const stateAsText = ({ snippets, contexts = {} }) => pipe([...sectionsOf("snippets", snippets), ...sectionsOf("contexts", contexts)], Array.join("\n\n"));
|
|
454
|
+
const only = (entries, ref) => Option.match(Option.fromUndefinedOr(entries[ref]), {
|
|
455
|
+
onNone: () => ({}),
|
|
456
|
+
onSome: (text) => ({ [ref]: text })
|
|
457
|
+
});
|
|
458
|
+
const stateOf = ({ snippets, contexts }, ref) => {
|
|
459
|
+
if (Predicate.isUndefined(contexts)) return { snippets: only(snippets, ref) };
|
|
460
|
+
return {
|
|
461
|
+
snippets: only(snippets, ref),
|
|
462
|
+
contexts: only(contexts, ref)
|
|
532
463
|
};
|
|
533
464
|
};
|
|
534
|
-
const
|
|
535
|
-
|
|
536
|
-
|
|
465
|
+
const optionText = (label, description) => Option.match(Option.fromNullishOr(description), {
|
|
466
|
+
onNone: () => `"${label}"`,
|
|
467
|
+
onSome: (text) => `"${label}" (${text})`
|
|
537
468
|
});
|
|
538
|
-
|
|
469
|
+
const labelQuestion = (instructions, label, description) => ({
|
|
470
|
+
type: "noul",
|
|
471
|
+
instructions: `${instructions} Does the label ${optionText(label, description)} fit?`
|
|
472
|
+
});
|
|
473
|
+
const levelQuestion = (instructions, label, description) => ({
|
|
474
|
+
type: "noul",
|
|
475
|
+
instructions: `${instructions} Is it at least as serious as ${optionText(label, description)}?`
|
|
476
|
+
});
|
|
477
|
+
const yesNoQuestionsOf = (id, { instructions, criteria }) => {
|
|
478
|
+
if (isSeverityScale(criteria)) return pipe(levelsAboveNone(criteria), Array.map(([label, description]) => [optionIdOf(id, label), levelQuestion(instructions, label, description)]));
|
|
479
|
+
return pipe(Record.toEntries(criteria), Array.map(([label, description]) => [optionIdOf(id, label), labelQuestion(instructions, label, description)]));
|
|
480
|
+
};
|
|
481
|
+
const asYesNo = ([id, question]) => {
|
|
482
|
+
if (question.type === "choice") return yesNoQuestionsOf(id, question);
|
|
483
|
+
return [[id, question]];
|
|
484
|
+
};
|
|
485
|
+
const perSnippet = (request) => {
|
|
486
|
+
const bySnippet = Array.groupBy(Record.toEntries(request.questions), ([id]) => snippetRefOf(id));
|
|
487
|
+
const wires = pipe(Record.values(bySnippet), Array.map((questions) => {
|
|
488
|
+
const [[id]] = questions;
|
|
489
|
+
return {
|
|
490
|
+
model: request.model,
|
|
491
|
+
state: stateAsText(stateOf(request.state, snippetRefOf(id))),
|
|
492
|
+
questions: Record.fromEntries(pipe(questions, Array.flatMap((entry) => asYesNo(entry))))
|
|
493
|
+
};
|
|
494
|
+
}));
|
|
495
|
+
if (Array.isReadonlyArrayNonEmpty(wires)) return wires;
|
|
496
|
+
return [request];
|
|
497
|
+
};
|
|
498
|
+
const wireRequestsFor = (request) => {
|
|
499
|
+
if (isRespan(request.model)) return perSnippet(request);
|
|
500
|
+
return [request];
|
|
501
|
+
};
|
|
502
|
+
const mergeResponses = Function.dual(DATA_FIRST_ARITY, (request, responses) => ({
|
|
503
|
+
...Array.headNonEmpty(responses),
|
|
504
|
+
answers: withChoices(request, pipe(responses, Array.flatMap(({ answers }) => Record.toEntries(answers)), Record.fromEntries))
|
|
505
|
+
}));
|
|
506
|
+
export { resolveModel as A, ModelSpecSchema as C, checked as D, checkRules as E, AnswerSchema as M, isReported as N, modelIdsOf as O, warnOnce as S, STRICT as T, failsInCi as _, parseVerdicts as a, runOrElse as b, EVERY_LINE as c, locationOfLine as d, locationQuestion as f, envOf as g, envNameOf as h, locationRefOf as i, resolveOptions as j, optionsError as k, locates as l, sourceLines as m, wireRequestsFor as n, refAt as o, selectedLocation as p, buildRequest as r, truncateSnippet as s, mergeResponses as t, locationCandidates as u, messageOf as v, PREFIX as w, toJson as x, nowMs as y };
|
|
539
507
|
|
|
540
|
-
//# sourceMappingURL=wire-
|
|
508
|
+
//# sourceMappingURL=wire-XbLEwd0i.mjs.map
|