@r_masseater/oxlint-plugin-decision 0.0.0-stage → 0.0.1
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/LICENSE +21 -0
- package/README.md +286 -2
- package/dist/index.d.mts +112 -0
- package/dist/index.mjs +344 -0
- package/dist/openrouter-worker.d.mts +1 -0
- package/dist/openrouter-worker.mjs +147 -0
- package/dist/request-Dg8fyV-u.mjs +512 -0
- package/package.json +64 -3
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
import { Array, Boolean, Cause, Config, Console, Effect, Exit, Number as Number$1, Option, Predicate, Result, Schema, String as String$1 } from "effect";
|
|
2
|
+
//#region src/answer.ts
|
|
3
|
+
const YES = "yes";
|
|
4
|
+
const DEFAULT_MINIMUM = "warn";
|
|
5
|
+
const MINIMUM_LABELS = 2;
|
|
6
|
+
const SEVERITY_CRITERIA = {
|
|
7
|
+
none: "No problem",
|
|
8
|
+
info: "Worth noting, but not a problem",
|
|
9
|
+
warn: "Likely a problem",
|
|
10
|
+
error: "Clearly a problem"
|
|
11
|
+
};
|
|
12
|
+
const SEVERITY_ORDER = Object.keys(SEVERITY_CRITERIA);
|
|
13
|
+
const Probability$1 = Schema.Number.check(Schema.isBetween({
|
|
14
|
+
minimum: 0,
|
|
15
|
+
maximum: 1
|
|
16
|
+
}));
|
|
17
|
+
const Label = Schema.NonEmptyString;
|
|
18
|
+
const BooleanAnswer = Schema.Struct({ kind: Schema.Literal("boolean") });
|
|
19
|
+
const ClassifyAnswer = Schema.Struct({
|
|
20
|
+
kind: Schema.Literal("classify"),
|
|
21
|
+
labels: Schema.Record(Label, Schema.String).check(Schema.isMinProperties(MINIMUM_LABELS)),
|
|
22
|
+
report: Schema.NonEmptyArray(Label)
|
|
23
|
+
}).check(Schema.makeFilter(({ labels, report }) => report.every((label) => Object.hasOwn(labels, label)), { message: "every label in report must be one of labels" }));
|
|
24
|
+
const SeverityAnswer = Schema.Struct({
|
|
25
|
+
kind: Schema.Literal("severity"),
|
|
26
|
+
minimum: Schema.optionalKey(Schema.Literals([
|
|
27
|
+
"info",
|
|
28
|
+
"warn",
|
|
29
|
+
"error"
|
|
30
|
+
]))
|
|
31
|
+
});
|
|
32
|
+
const RuleAnswerSchema = Schema.Union([
|
|
33
|
+
BooleanAnswer,
|
|
34
|
+
ClassifyAnswer,
|
|
35
|
+
SeverityAnswer
|
|
36
|
+
]);
|
|
37
|
+
const BOOLEAN_ANSWER = { kind: "boolean" };
|
|
38
|
+
const decodeAnswer = Schema.decodeUnknownResult(RuleAnswerSchema);
|
|
39
|
+
const decodeRuleAnswer = (answer) => {
|
|
40
|
+
const decoded = decodeAnswer(answer);
|
|
41
|
+
if (Result.isSuccess(decoded)) return Result.succeed(decoded.success);
|
|
42
|
+
return Result.fail(decoded.failure.message);
|
|
43
|
+
};
|
|
44
|
+
const NoulAnswer = Schema.Struct({ noul: Probability$1 });
|
|
45
|
+
const choiceAnswerOf = (labels) => Schema.Struct({
|
|
46
|
+
type: Schema.Literal("choice"),
|
|
47
|
+
choice: Schema.Literals(labels),
|
|
48
|
+
probabilities: Schema.Record(Schema.String, Probability$1)
|
|
49
|
+
});
|
|
50
|
+
const questionFor = (answer, instructions) => {
|
|
51
|
+
switch (answer.kind) {
|
|
52
|
+
case "classify": return {
|
|
53
|
+
type: "choice",
|
|
54
|
+
instructions: `${instructions} Select the label that fits best.`,
|
|
55
|
+
criteria: answer.labels
|
|
56
|
+
};
|
|
57
|
+
case "severity": return {
|
|
58
|
+
type: "choice",
|
|
59
|
+
instructions: `${instructions} Rate how serious the problem is, and select none if there is no problem.`,
|
|
60
|
+
criteria: SEVERITY_CRITERIA
|
|
61
|
+
};
|
|
62
|
+
case "boolean": return {
|
|
63
|
+
type: "noul",
|
|
64
|
+
instructions
|
|
65
|
+
};
|
|
66
|
+
default: return {
|
|
67
|
+
type: "noul",
|
|
68
|
+
instructions
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const choiceVerdictOf = (labels, raw) => Schema.decodeUnknownOption(choiceAnswerOf(labels))(raw).pipe(Option.flatMap(({ choice, probabilities }) => Option.fromUndefinedOr(probabilities[choice]).pipe(Option.map((probability) => ({
|
|
73
|
+
answer: choice,
|
|
74
|
+
probability
|
|
75
|
+
})))));
|
|
76
|
+
const verdictOf = (question, raw) => {
|
|
77
|
+
if (question.type === "choice") return choiceVerdictOf(Object.keys(question.criteria), raw);
|
|
78
|
+
return Schema.decodeUnknownOption(NoulAnswer)(raw).pipe(Option.map(({ noul }) => ({
|
|
79
|
+
answer: YES,
|
|
80
|
+
probability: noul
|
|
81
|
+
})));
|
|
82
|
+
};
|
|
83
|
+
const rankOf = (label) => SEVERITY_ORDER.indexOf(label);
|
|
84
|
+
const severityMatches = ({ minimum = DEFAULT_MINIMUM }, label) => rankOf(label) >= rankOf(minimum);
|
|
85
|
+
const answerMatches = (answer, label) => {
|
|
86
|
+
switch (answer.kind) {
|
|
87
|
+
case "classify": return answer.report.includes(label);
|
|
88
|
+
case "severity": return severityMatches(answer, label);
|
|
89
|
+
case "boolean": return label === YES;
|
|
90
|
+
default: return false;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const isReported = (rule, verdict) => verdict.probability >= rule.cutoff && answerMatches(rule.answer, verdict.answer);
|
|
94
|
+
//#endregion
|
|
95
|
+
//#region src/options.ts
|
|
96
|
+
const PREFIX = "oxlint-plugin-decision";
|
|
97
|
+
const PROVIDER_SEPARATOR = "/";
|
|
98
|
+
const FIRST_LINE$1 = 1;
|
|
99
|
+
const FIRST_COLUMN = 0;
|
|
100
|
+
const TARGET_NODE_TYPES = {
|
|
101
|
+
function: [
|
|
102
|
+
"FunctionDeclaration",
|
|
103
|
+
"FunctionExpression",
|
|
104
|
+
"ArrowFunctionExpression"
|
|
105
|
+
],
|
|
106
|
+
call: ["CallExpression"],
|
|
107
|
+
jsx: ["JSXElement"],
|
|
108
|
+
file: ["Program"]
|
|
109
|
+
};
|
|
110
|
+
const NAMED_PARENTS = {
|
|
111
|
+
VariableDeclarator: "init",
|
|
112
|
+
Property: "value",
|
|
113
|
+
MethodDefinition: "value",
|
|
114
|
+
PropertyDefinition: "value"
|
|
115
|
+
};
|
|
116
|
+
const isNamedParentType = (type) => Object.hasOwn(NAMED_PARENTS, type);
|
|
117
|
+
const ANONYMOUS_FUNCTIONS = /* @__PURE__ */ new Set(["FunctionExpression", "ArrowFunctionExpression"]);
|
|
118
|
+
const holdsAsName = (parent, node) => {
|
|
119
|
+
if (!isNamedParentType(parent.type)) return false;
|
|
120
|
+
const field = NAMED_PARENTS[parent.type];
|
|
121
|
+
return ANONYMOUS_FUNCTIONS.has(node.type) && parent[field] === node;
|
|
122
|
+
};
|
|
123
|
+
const snippetNodeFor = (node) => Option.fromNullishOr(node.parent).pipe(Option.filter((parent) => holdsAsName(parent, node)), Option.getOrElse(() => node));
|
|
124
|
+
const DEFAULTS = {
|
|
125
|
+
ci: "skip",
|
|
126
|
+
timeoutMs: 1e4,
|
|
127
|
+
maxMatchesPerFile: 25,
|
|
128
|
+
maxSnippetChars: 4e3,
|
|
129
|
+
model: "openrouter/~typesafe/jev-latest"
|
|
130
|
+
};
|
|
131
|
+
const MODEL_SCHEMA = { oneOf: [{
|
|
132
|
+
type: "string",
|
|
133
|
+
minLength: 1
|
|
134
|
+
}, {
|
|
135
|
+
type: "array",
|
|
136
|
+
minItems: 1,
|
|
137
|
+
items: {
|
|
138
|
+
type: "string",
|
|
139
|
+
minLength: 1
|
|
140
|
+
}
|
|
141
|
+
}] };
|
|
142
|
+
const LINE_COUNT = {
|
|
143
|
+
type: "integer",
|
|
144
|
+
minimum: 0
|
|
145
|
+
};
|
|
146
|
+
const CONTEXT_SCHEMA = { oneOf: [
|
|
147
|
+
{
|
|
148
|
+
type: "object",
|
|
149
|
+
additionalProperties: false,
|
|
150
|
+
required: ["scope"],
|
|
151
|
+
properties: { scope: { enum: ["node", "file"] } }
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
type: "object",
|
|
155
|
+
additionalProperties: false,
|
|
156
|
+
required: ["scope"],
|
|
157
|
+
properties: {
|
|
158
|
+
scope: { enum: ["lines"] },
|
|
159
|
+
before: LINE_COUNT,
|
|
160
|
+
after: LINE_COUNT
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
type: "object",
|
|
165
|
+
additionalProperties: false,
|
|
166
|
+
required: ["scope", "kind"],
|
|
167
|
+
properties: {
|
|
168
|
+
scope: { enum: ["enclosing"] },
|
|
169
|
+
kind: { enum: [
|
|
170
|
+
"function",
|
|
171
|
+
"class",
|
|
172
|
+
"block"
|
|
173
|
+
] }
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
] };
|
|
177
|
+
const SCHEMA = {
|
|
178
|
+
type: "object",
|
|
179
|
+
additionalProperties: false,
|
|
180
|
+
properties: {
|
|
181
|
+
ci: { enum: ["skip", "fail"] },
|
|
182
|
+
timeoutMs: {
|
|
183
|
+
type: "integer",
|
|
184
|
+
minimum: 1
|
|
185
|
+
},
|
|
186
|
+
maxMatchesPerFile: {
|
|
187
|
+
type: "integer",
|
|
188
|
+
minimum: 1
|
|
189
|
+
},
|
|
190
|
+
maxSnippetChars: {
|
|
191
|
+
type: "integer",
|
|
192
|
+
minimum: 1
|
|
193
|
+
},
|
|
194
|
+
model: MODEL_SCHEMA,
|
|
195
|
+
rules: {
|
|
196
|
+
type: "array",
|
|
197
|
+
items: {
|
|
198
|
+
type: "object",
|
|
199
|
+
additionalProperties: false,
|
|
200
|
+
required: [
|
|
201
|
+
"id",
|
|
202
|
+
"target",
|
|
203
|
+
"question",
|
|
204
|
+
"cutoff"
|
|
205
|
+
],
|
|
206
|
+
properties: {
|
|
207
|
+
id: {
|
|
208
|
+
type: "string",
|
|
209
|
+
minLength: 1
|
|
210
|
+
},
|
|
211
|
+
target: { enum: Object.keys(TARGET_NODE_TYPES) },
|
|
212
|
+
question: {
|
|
213
|
+
type: "string",
|
|
214
|
+
minLength: 1
|
|
215
|
+
},
|
|
216
|
+
location: {
|
|
217
|
+
type: "object",
|
|
218
|
+
additionalProperties: false,
|
|
219
|
+
required: ["question", "cutoff"],
|
|
220
|
+
properties: {
|
|
221
|
+
question: {
|
|
222
|
+
type: "string",
|
|
223
|
+
minLength: 1
|
|
224
|
+
},
|
|
225
|
+
cutoff: {
|
|
226
|
+
type: "number",
|
|
227
|
+
minimum: .5,
|
|
228
|
+
exclusiveMinimum: true,
|
|
229
|
+
maximum: 1
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
cutoff: {
|
|
234
|
+
type: "number",
|
|
235
|
+
minimum: 0,
|
|
236
|
+
maximum: 1
|
|
237
|
+
},
|
|
238
|
+
model: MODEL_SCHEMA,
|
|
239
|
+
context: CONTEXT_SCHEMA,
|
|
240
|
+
answer: { oneOf: [
|
|
241
|
+
{
|
|
242
|
+
type: "object",
|
|
243
|
+
additionalProperties: false,
|
|
244
|
+
required: ["kind"],
|
|
245
|
+
properties: { kind: { enum: ["boolean"] } }
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
type: "object",
|
|
249
|
+
additionalProperties: false,
|
|
250
|
+
required: [
|
|
251
|
+
"kind",
|
|
252
|
+
"labels",
|
|
253
|
+
"report"
|
|
254
|
+
],
|
|
255
|
+
properties: {
|
|
256
|
+
kind: { enum: ["classify"] },
|
|
257
|
+
labels: {
|
|
258
|
+
type: "object",
|
|
259
|
+
minProperties: 2,
|
|
260
|
+
additionalProperties: { type: "string" }
|
|
261
|
+
},
|
|
262
|
+
report: {
|
|
263
|
+
type: "array",
|
|
264
|
+
minItems: 1,
|
|
265
|
+
items: {
|
|
266
|
+
type: "string",
|
|
267
|
+
minLength: 1
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
type: "object",
|
|
274
|
+
additionalProperties: false,
|
|
275
|
+
required: ["kind"],
|
|
276
|
+
properties: {
|
|
277
|
+
kind: { enum: ["severity"] },
|
|
278
|
+
minimum: { enum: [
|
|
279
|
+
"info",
|
|
280
|
+
"warn",
|
|
281
|
+
"error"
|
|
282
|
+
] }
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
] }
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
var OptionsError = class extends Error {
|
|
292
|
+
constructor(message) {
|
|
293
|
+
super(`${PREFIX}: ${message}`);
|
|
294
|
+
this.name = "OptionsError";
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
const modelIdsOf = (spec) => Array.dedupe(Array.ensure(spec));
|
|
298
|
+
const splitModelId = (id) => {
|
|
299
|
+
const [provider = "", ...rest] = id.split(PROVIDER_SEPARATOR);
|
|
300
|
+
const model = rest.join(PROVIDER_SEPARATOR);
|
|
301
|
+
return Option.liftPredicate([provider, model], ([left, right]) => String$1.isNonEmpty(left) && String$1.isNonEmpty(right));
|
|
302
|
+
};
|
|
303
|
+
const resolveModel = (id, providers) => splitModelId(id).pipe(Option.flatMap(([name, model]) => Option.map(Option.fromUndefinedOr(providers[name]), (provider) => ({
|
|
304
|
+
id,
|
|
305
|
+
provider,
|
|
306
|
+
model
|
|
307
|
+
}))), Option.getOrThrowWith(() => new OptionsError(`model "${id}" must be "<provider>/<model>" with a provider from: ${Object.keys(providers).join(", ")}`)));
|
|
308
|
+
const duplicateIdOf = (rules) => {
|
|
309
|
+
const ids = rules.map(({ id }) => id);
|
|
310
|
+
return Array.findFirst(ids, (id, index) => ids.indexOf(id) !== index);
|
|
311
|
+
};
|
|
312
|
+
const answerOf = (rule) => {
|
|
313
|
+
if (Predicate.isUndefined(rule.answer)) return BOOLEAN_ANSWER;
|
|
314
|
+
return Result.getOrThrowWith(decodeRuleAnswer(rule.answer), (issue) => new OptionsError(`rule "${rule.id}" has an invalid answer: ${issue}`));
|
|
315
|
+
};
|
|
316
|
+
const resolveRule = (fallback, providers) => (rule) => {
|
|
317
|
+
if (Predicate.isNotUndefined(rule.location) && rule.target !== "file") throw new OptionsError("location is supported only for file rules");
|
|
318
|
+
return {
|
|
319
|
+
...rule,
|
|
320
|
+
answer: answerOf(rule),
|
|
321
|
+
models: modelIdsOf(rule.model ?? fallback).map((id) => resolveModel(id, providers))
|
|
322
|
+
};
|
|
323
|
+
};
|
|
324
|
+
const resolveOptions = (options, codeRules, providers) => {
|
|
325
|
+
const rules = [...codeRules, ...options.rules ?? []];
|
|
326
|
+
if (!Array.isReadonlyArrayNonEmpty(rules)) throw new OptionsError("options.rules must list at least one rule");
|
|
327
|
+
Option.map(duplicateIdOf(rules), (duplicate) => {
|
|
328
|
+
throw new OptionsError(`rule id "${duplicate}" is used more than once`);
|
|
329
|
+
});
|
|
330
|
+
const { model, ...settings } = {
|
|
331
|
+
...DEFAULTS,
|
|
332
|
+
...options
|
|
333
|
+
};
|
|
334
|
+
return {
|
|
335
|
+
ci: settings.ci,
|
|
336
|
+
timeoutMs: settings.timeoutMs,
|
|
337
|
+
maxMatchesPerFile: settings.maxMatchesPerFile,
|
|
338
|
+
maxSnippetChars: settings.maxSnippetChars,
|
|
339
|
+
rules: rules.map((rule) => resolveRule(model, providers)(rule))
|
|
340
|
+
};
|
|
341
|
+
};
|
|
342
|
+
const headOf = (text, line, column) => {
|
|
343
|
+
const [first = ""] = text.split("\n", FIRST_LINE$1);
|
|
344
|
+
return {
|
|
345
|
+
start: {
|
|
346
|
+
line,
|
|
347
|
+
column
|
|
348
|
+
},
|
|
349
|
+
end: {
|
|
350
|
+
line,
|
|
351
|
+
column: column + first.length
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
};
|
|
355
|
+
const REPORT_LOC = {
|
|
356
|
+
function: (node, text) => headOf(text, node.loc.start.line, node.loc.start.column),
|
|
357
|
+
call: (node) => node.loc,
|
|
358
|
+
jsx: (node) => Option.getOrElse(Option.fromNullishOr(node.openingElement), () => node).loc,
|
|
359
|
+
file: (_node, text) => headOf(text, FIRST_LINE$1, FIRST_COLUMN)
|
|
360
|
+
};
|
|
361
|
+
//#endregion
|
|
362
|
+
//#region src/runtime.ts
|
|
363
|
+
const runOrElse = (effect, fallback) => Exit.match(Effect.runSyncExit(effect), {
|
|
364
|
+
onSuccess: (value) => value,
|
|
365
|
+
onFailure: (cause) => fallback(Cause.squash(cause))
|
|
366
|
+
});
|
|
367
|
+
const envOf = (name) => runOrElse(Config.option(Config.NonEmptyString(name)), () => Option.none()).pipe(Option.map((value) => value.trim()), Option.filter(String$1.isNonEmpty));
|
|
368
|
+
const failsInCi = (options) => options.ci === "fail" && Option.isSome(envOf("CI"));
|
|
369
|
+
const warned = /* @__PURE__ */ new Set();
|
|
370
|
+
const warnOnce = (reason, message) => {
|
|
371
|
+
if (warned.has(reason)) return;
|
|
372
|
+
warned.add(reason);
|
|
373
|
+
Effect.runFork(Console.warn(`${PREFIX}: ${message}`));
|
|
374
|
+
};
|
|
375
|
+
const messageOf = (error) => {
|
|
376
|
+
if (error instanceof Error) return error.message;
|
|
377
|
+
return String(error);
|
|
378
|
+
};
|
|
379
|
+
//#endregion
|
|
380
|
+
//#region src/request.ts
|
|
381
|
+
const CHOICE_OPTION_LIMIT = 254;
|
|
382
|
+
const FIRST_LINE = 1;
|
|
383
|
+
const MINIMUM_WIDTH = 1;
|
|
384
|
+
const UNKNOWN_CHOICE = "unknown";
|
|
385
|
+
const LOCATION_SUFFIX = "_location";
|
|
386
|
+
const TRUNCATION_MARKER = "/* ...truncated */";
|
|
387
|
+
const refAt = (index) => `s${String(index)}`;
|
|
388
|
+
const locationRefOf = (ref) => `${ref}${LOCATION_SUFFIX}`;
|
|
389
|
+
const snippetRefOf = (questionId) => Boolean.match(questionId.endsWith(LOCATION_SUFFIX), {
|
|
390
|
+
onTrue: () => questionId.slice(Number$1.decrement(FIRST_LINE), -9),
|
|
391
|
+
onFalse: () => questionId
|
|
392
|
+
});
|
|
393
|
+
const truncateSnippet = (text, maxChars) => Boolean.match(text.length <= maxChars, {
|
|
394
|
+
onTrue: () => text,
|
|
395
|
+
onFalse: () => `${text.slice(Number$1.decrement(FIRST_LINE), maxChars)}${TRUNCATION_MARKER}`
|
|
396
|
+
});
|
|
397
|
+
const sourceLines = (text) => text.split(/\r\n|[\n\r\u2028\u2029]/u);
|
|
398
|
+
const locates = (match) => Predicate.isNotUndefined(match.rule.location) && !match.truncated;
|
|
399
|
+
const EVERY_LINE = {
|
|
400
|
+
start: FIRST_LINE,
|
|
401
|
+
end: Number.POSITIVE_INFINITY
|
|
402
|
+
};
|
|
403
|
+
const isWithin = (line, within) => Number$1.between(line, {
|
|
404
|
+
minimum: within.start,
|
|
405
|
+
maximum: within.end
|
|
406
|
+
});
|
|
407
|
+
const rangeKey = ({ start, end }) => Boolean.match(start === end, {
|
|
408
|
+
onTrue: () => `L${String(start)}`,
|
|
409
|
+
onFalse: () => `L${String(start)}-L${String(end)}`
|
|
410
|
+
});
|
|
411
|
+
const rangeLabel = ({ start, end }) => Boolean.match(start === end, {
|
|
412
|
+
onTrue: () => `Line ${String(start)}`,
|
|
413
|
+
onFalse: () => `Lines ${String(start)} to ${String(end)}`
|
|
414
|
+
});
|
|
415
|
+
const rangeOf = (group) => ({
|
|
416
|
+
start: Array.headNonEmpty(group).line,
|
|
417
|
+
end: Array.lastNonEmpty(group).line
|
|
418
|
+
});
|
|
419
|
+
const numberedLines = (snippet) => sourceLines(snippet).map((text, index) => ({
|
|
420
|
+
text,
|
|
421
|
+
line: Number$1.sum(index, FIRST_LINE)
|
|
422
|
+
}));
|
|
423
|
+
const locationCandidates = (snippet, within = EVERY_LINE) => {
|
|
424
|
+
const lines = numberedLines(snippet).filter(({ text, line }) => text.trim().length >= FIRST_LINE && isWithin(line, within));
|
|
425
|
+
const width = Math.max(MINIMUM_WIDTH, Math.ceil(lines.length / CHOICE_OPTION_LIMIT));
|
|
426
|
+
return Object.fromEntries(Array.chunksOf(lines, width).map((group) => {
|
|
427
|
+
const range = rangeOf(group);
|
|
428
|
+
return [rangeKey(range), range];
|
|
429
|
+
}));
|
|
430
|
+
};
|
|
431
|
+
const locationQuestion = (match, ref, candidates) => ({
|
|
432
|
+
type: "choice",
|
|
433
|
+
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.`,
|
|
434
|
+
criteria: {
|
|
435
|
+
...Object.fromEntries(Object.entries(candidates).map(([key, range]) => [key, rangeLabel(range)])),
|
|
436
|
+
[UNKNOWN_CHOICE]: "No clear source location for this violation"
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
const Probability = Schema.Number.check(Schema.isBetween({
|
|
440
|
+
minimum: 0,
|
|
441
|
+
maximum: 1
|
|
442
|
+
}));
|
|
443
|
+
const ChoiceAnswer = Schema.Struct({
|
|
444
|
+
type: Schema.Literal("choice"),
|
|
445
|
+
choice: Schema.String,
|
|
446
|
+
probabilities: Schema.Record(Schema.String, Probability)
|
|
447
|
+
});
|
|
448
|
+
const Response = Schema.Struct({
|
|
449
|
+
model: Schema.NonEmptyString,
|
|
450
|
+
answers: Schema.Record(Schema.String, Schema.Unknown)
|
|
451
|
+
});
|
|
452
|
+
const decodeChoice = Schema.decodeUnknownOption(ChoiceAnswer);
|
|
453
|
+
const decodeResponse = Schema.decodeUnknownOption(Response);
|
|
454
|
+
const validChoice = (answer, criteria) => Option.exists(decodeChoice(answer), ({ choice, probabilities }) => Object.hasOwn(criteria, choice) && Object.hasOwn(probabilities, choice));
|
|
455
|
+
const selectedLocation = (answer, candidates, cutoff) => decodeChoice(answer).pipe(Option.filter(({ choice, probabilities }) => Option.exists(Option.fromUndefinedOr(probabilities[choice]), (probability) => Number$1.isGreaterThanOrEqualTo(probability, cutoff))), Option.flatMap(({ choice }) => Option.fromUndefinedOr(candidates[choice])));
|
|
456
|
+
const locationOfLine = (snippet, line) => {
|
|
457
|
+
const text = Option.getOrElse(Array.get(sourceLines(snippet), Number$1.decrement(line)), () => "");
|
|
458
|
+
return {
|
|
459
|
+
start: {
|
|
460
|
+
line,
|
|
461
|
+
column: Math.max(Number$1.decrement(FIRST_LINE), text.search(/\S/u))
|
|
462
|
+
},
|
|
463
|
+
end: {
|
|
464
|
+
line,
|
|
465
|
+
column: text.trimEnd().length
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
};
|
|
469
|
+
const numbered = (snippet) => numberedLines(snippet).map(({ text, line }) => `L${String(line)}| ${text}`).join("\n");
|
|
470
|
+
const contextNote = (match, ref) => {
|
|
471
|
+
if (Predicate.isUndefined(match.context)) return "";
|
|
472
|
+
return ` state.contexts.${ref} shows the source around it; use it only to understand the snippet.`;
|
|
473
|
+
};
|
|
474
|
+
const mainQuestionFor = (match, ref) => [ref, questionFor(match.rule.answer, `Consider only snippet "${ref}" in state.snippets.${contextNote(match, ref)} ${match.rule.question}`)];
|
|
475
|
+
const questionsFor = (match, ref) => {
|
|
476
|
+
if (!locates(match)) return [mainQuestionFor(match, ref)];
|
|
477
|
+
return [mainQuestionFor(match, ref), [locationRefOf(ref), locationQuestion(match, ref, locationCandidates(match.snippet))]];
|
|
478
|
+
};
|
|
479
|
+
const snippetFor = (match) => {
|
|
480
|
+
if (locates(match)) return numbered(match.snippet);
|
|
481
|
+
return match.snippet;
|
|
482
|
+
};
|
|
483
|
+
const contextsOf = (matches) => {
|
|
484
|
+
const contexts = matches.flatMap((match, index) => Option.toArray(Option.map(Option.fromUndefinedOr(match.context), (text) => [refAt(index), text])));
|
|
485
|
+
if (Array.isReadonlyArrayEmpty(contexts)) return {};
|
|
486
|
+
return { contexts: Object.fromEntries(contexts) };
|
|
487
|
+
};
|
|
488
|
+
const buildRequest = (model, matches) => ({
|
|
489
|
+
model,
|
|
490
|
+
state: {
|
|
491
|
+
snippets: Object.fromEntries(matches.map((match, index) => [refAt(index), snippetFor(match)])),
|
|
492
|
+
...contextsOf(matches)
|
|
493
|
+
},
|
|
494
|
+
questions: Object.fromEntries(matches.flatMap((match, index) => questionsFor(match, refAt(index))))
|
|
495
|
+
});
|
|
496
|
+
const choiceQuestionsOf = (request) => Object.entries(request.questions).flatMap(([id, question]) => {
|
|
497
|
+
if (question.type === "choice") return [[id, question.criteria]];
|
|
498
|
+
return [];
|
|
499
|
+
});
|
|
500
|
+
const mainQuestionsOf = (request) => Object.entries(request.questions).filter(([id]) => !id.endsWith(LOCATION_SUFFIX));
|
|
501
|
+
const resultOf = (answers, [ref, question]) => [ref, Option.getOrThrowWith(verdictOf(question, answers[ref]), () => /* @__PURE__ */ new Error(`response has no valid answer for "${ref}"`))];
|
|
502
|
+
const parseVerdicts = (json, request) => {
|
|
503
|
+
const { model, answers } = Option.getOrThrowWith(decodeResponse(json), () => /* @__PURE__ */ new Error("response has no model id or answers object"));
|
|
504
|
+
return {
|
|
505
|
+
model,
|
|
506
|
+
results: Object.fromEntries(mainQuestionsOf(request).map((entry) => resultOf(answers, entry))),
|
|
507
|
+
answers,
|
|
508
|
+
complete: choiceQuestionsOf(request).every(([id, criteria]) => validChoice(answers[id], criteria))
|
|
509
|
+
};
|
|
510
|
+
};
|
|
511
|
+
//#endregion
|
|
512
|
+
export { TARGET_NODE_TYPES as C, isReported as E, SCHEMA as S, snippetNodeFor as T, warnOnce as _, locationQuestion as a, PREFIX as b, refAt as c, sourceLines as d, truncateSnippet as f, runOrElse as g, messageOf as h, locationOfLine as i, selectedLocation as l, failsInCi as m, locates as n, locationRefOf as o, envOf as p, locationCandidates as r, parseVerdicts as s, buildRequest as t, snippetRefOf as u, DEFAULTS as v, resolveOptions as w, REPORT_LOC as x, OptionsError as y };
|
package/package.json
CHANGED
|
@@ -1,6 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@r_masseater/oxlint-plugin-decision",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Oxlint JS plugin that lints code with plain-English rules answered by decision models on OpenRouter.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"decision-model",
|
|
7
|
+
"lint",
|
|
8
|
+
"openrouter",
|
|
9
|
+
"oxlint",
|
|
10
|
+
"oxlint-plugin"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/masseater/oxlint-plugin-decision#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/masseater/oxlint-plugin-decision/issues"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/masseater/oxlint-plugin-decision.git",
|
|
20
|
+
"directory": "libs/oxlint-plugin-decision"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"type": "module",
|
|
26
|
+
"imports": {
|
|
27
|
+
"#src/*": "./src/*"
|
|
28
|
+
},
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.mts",
|
|
32
|
+
"default": "./dist/index.mjs"
|
|
33
|
+
},
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public",
|
|
38
|
+
"provenance": true
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@effect/platform-node": "4.0.0",
|
|
42
|
+
"@openrouter/sdk": "1.4.25",
|
|
43
|
+
"effect": "4.0.0",
|
|
44
|
+
"synckit": "0.12.1"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@effect/tsgo": "0.50.0",
|
|
48
|
+
"@effect/vitest": "4.0.0",
|
|
49
|
+
"@oxlint/plugins": "1.87.0",
|
|
50
|
+
"@types/node": "26.6.5",
|
|
51
|
+
"oxlint": "1.87.0",
|
|
52
|
+
"typescript": "7.0.2",
|
|
53
|
+
"vite-plus": "1.1.0"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"oxlint": ">=1.87.0"
|
|
57
|
+
},
|
|
58
|
+
"engines": {
|
|
59
|
+
"node": ">=20"
|
|
60
|
+
},
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "vp pack",
|
|
63
|
+
"dev": "vp pack --watch",
|
|
64
|
+
"test": "vp test --project=mock",
|
|
65
|
+
"example": "vp run build && ./node_modules/oxlint/bin/oxlint -c example/.oxlintrc.json example/"
|
|
66
|
+
}
|
|
6
67
|
}
|