@r_masseater/oxlint-plugin-decision 0.5.1 → 0.7.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 +23 -395
- package/dist/app-Bso1uwY5.mjs +662 -0
- package/dist/app-Bso1uwY5.mjs.map +1 -0
- package/dist/cloudflare-worker.mjs +6 -179
- package/dist/cloudflare-worker.mjs.map +1 -1
- package/dist/decision-BtTZ52be.mjs +643 -0
- package/dist/decision-BtTZ52be.mjs.map +1 -0
- package/dist/define-presets-clyqLZxo.mjs +550 -0
- package/dist/define-presets-clyqLZxo.mjs.map +1 -0
- package/dist/index-BOrpK3Iv.d.mts +89 -0
- package/dist/index-Cgg-w0hb.d.mts +3370 -0
- package/dist/index-D7VrRpTx.d.mts +6 -0
- package/dist/index.d.mts +3 -3372
- package/dist/index.mjs +1 -786
- package/dist/openrouter-app.d.mts +12 -4
- package/dist/openrouter-app.mjs +1 -87
- package/dist/openrouter-worker.mjs +5 -35
- package/dist/openrouter-worker.mjs.map +1 -1
- package/dist/preset-plugin-AAwedMeS.mjs +10 -0
- package/dist/preset-plugin-AAwedMeS.mjs.map +1 -0
- package/dist/presets/ai-slop.d.mts +6 -0
- package/dist/presets/ai-slop.mjs +10 -0
- package/dist/presets/ai-slop.mjs.map +1 -0
- package/dist/presets/index.d.mts +3 -0
- package/dist/presets/index.mjs +3 -0
- package/dist/presets/recommended.d.mts +5 -0
- package/dist/presets/recommended.mjs +7 -0
- package/dist/presets/recommended.mjs.map +1 -0
- package/dist/presets/review-only.d.mts +5 -0
- package/dist/presets/review-only.mjs +7 -0
- package/dist/presets/review-only.mjs.map +1 -0
- package/dist/presets/strict.d.mts +5 -0
- package/dist/presets/strict.mjs +7 -0
- package/dist/presets/strict.mjs.map +1 -0
- package/dist/serve-decisions-CjlhvYm7.mjs +290 -0
- package/dist/serve-decisions-CjlhvYm7.mjs.map +1 -0
- package/package.json +25 -3
- package/skills/write-decision-rules/SKILL.md +18 -17
- package/skills/write-decision-rules/references/tuning.md +1 -1
- package/dist/index.mjs.map +0 -1
- package/dist/openrouter-app.mjs.map +0 -1
- package/dist/runtime-CICpEidL.mjs +0 -271
- package/dist/runtime-CICpEidL.mjs.map +0 -1
- package/dist/serve-decisions-cQpGj_Fq.mjs +0 -112
- package/dist/serve-decisions-cQpGj_Fq.mjs.map +0 -1
- package/dist/wire-CRD1mSAo.mjs +0 -251
- package/dist/wire-CRD1mSAo.mjs.map +0 -1
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
import { A as contextOf, B as selectedLocation, C as checked, D as resolveOptions, E as resolveModel, F as EVERY_LINE, H as isReported, I as locates, L as locationCandidates, M as REPORT_LOC, N as TARGET_NODE_TYPES, O as isSelected, P as snippetNodeFor, R as locationOfLine, S as checkRules, T as optionsError, _ as refAt, b as PREFIX, c as failsInCi, d as runOrElse, f as toJson, g as parseVerdicts, h as locationRefOf, i as yesNoRequestsFor, j as lacksComment, k as commentedSnippetOf, l as messageOf, m as buildRequest, o as envNameOf, p as warnOnce, r as wireRequestsFor, s as envOf, u as nowMs, v as truncateSnippet, w as modelIdsOf, x as STRICT, y as ModelSpecSchema, z as locationQuestion } from "./decision-BtTZ52be.mjs";
|
|
2
|
+
import { Array, Boolean, Console, Effect, Function, Match, Option, Predicate, Record, Schema, String as String$1, Struct, pipe } from "effect";
|
|
3
|
+
import { createSyncFn } from "synckit";
|
|
4
|
+
// src/entities/decision/model/budget.ts
|
|
5
|
+
const OUT_OF_BUDGET = /^http (?:402|403): .*(?:credit|budget)/iu;
|
|
6
|
+
// src/entities/decision/model/define.ts
|
|
7
|
+
const defineDecisionRule = (rule) => rule;
|
|
8
|
+
const defineProvider = (provider) => provider;
|
|
9
|
+
// src/app/sync-provider.ts
|
|
10
|
+
const BACKSTOP_MS = 6e4;
|
|
11
|
+
const SYNCKIT_TIMEOUT = "Internal error: Atomics.wait() failed: timed-out";
|
|
12
|
+
const BUILT_EXTENSION = ".mjs";
|
|
13
|
+
const MODULE_EXTENSION = /\.[cm]?[jt]s$/u;
|
|
14
|
+
const workers = /* @__PURE__ */ new Map();
|
|
15
|
+
const workerUrl = (worker) => {
|
|
16
|
+
const extension = pipe(String$1.match(MODULE_EXTENSION)(import.meta.url), Option.flatMap(Array.head), Option.getOrElse(() => BUILT_EXTENSION));
|
|
17
|
+
return new URL(`${worker}${extension}`, import.meta.url);
|
|
18
|
+
};
|
|
19
|
+
const workerCall = (worker) => {
|
|
20
|
+
const existing = workers.get(worker);
|
|
21
|
+
if (Predicate.isNotUndefined(existing)) return existing;
|
|
22
|
+
const created = createSyncFn(workerUrl(worker), { timeout: BACKSTOP_MS });
|
|
23
|
+
workers.set(worker, created);
|
|
24
|
+
return created;
|
|
25
|
+
};
|
|
26
|
+
const reasonOf = (error) => {
|
|
27
|
+
const message = messageOf(error);
|
|
28
|
+
if (message === SYNCKIT_TIMEOUT) return "timeout";
|
|
29
|
+
return `network: ${message}`;
|
|
30
|
+
};
|
|
31
|
+
const askWorker = (worker, inputs) => runOrElse(Effect.sync(() => workerCall(worker)(inputs)), (cause) => pipe(inputs, Array.map(() => ({
|
|
32
|
+
ok: false,
|
|
33
|
+
reason: reasonOf(cause)
|
|
34
|
+
}))));
|
|
35
|
+
/** @internal */
|
|
36
|
+
const syncProvider = ({ worker, ...provider }) => ({
|
|
37
|
+
...provider,
|
|
38
|
+
decide: (input) => Array.headNonEmpty(askWorker(worker, [input])),
|
|
39
|
+
decideAll: (inputs) => Array.match(inputs, {
|
|
40
|
+
onEmpty: () => [],
|
|
41
|
+
onNonEmpty: (some) => askWorker(worker, some)
|
|
42
|
+
})
|
|
43
|
+
});
|
|
44
|
+
// src/app/cloudflare.ts
|
|
45
|
+
const cloudflare = syncProvider({
|
|
46
|
+
name: "cloudflare",
|
|
47
|
+
worker: "cloudflare-worker",
|
|
48
|
+
requiredEnv: ["CLOUDFLARE_API_TOKEN", "CLOUDFLARE_ACCOUNT_ID"],
|
|
49
|
+
models: /^@cf\/cloudflare\/clef(?:-[a-z]+)?$/u
|
|
50
|
+
});
|
|
51
|
+
// src/app/openrouter.ts
|
|
52
|
+
const openRouter = syncProvider({
|
|
53
|
+
name: "openrouter",
|
|
54
|
+
worker: "openrouter-worker",
|
|
55
|
+
requiredEnv: ["OPENROUTER_API_KEY"]
|
|
56
|
+
});
|
|
57
|
+
// src/features/judge-code/model/override.ts
|
|
58
|
+
const DATA_FIRST_ARITY_3$1 = 3;
|
|
59
|
+
const RuleOverrideSchema = Schema.Struct({ model: Schema.optionalKey(ModelSpecSchema) });
|
|
60
|
+
const RULE_OPTIONS_SCHEMA = [{
|
|
61
|
+
type: "object",
|
|
62
|
+
additionalProperties: false,
|
|
63
|
+
properties: { model: { oneOf: [{
|
|
64
|
+
type: "string",
|
|
65
|
+
minLength: 1
|
|
66
|
+
}, {
|
|
67
|
+
type: "array",
|
|
68
|
+
minItems: 1,
|
|
69
|
+
items: {
|
|
70
|
+
type: "string",
|
|
71
|
+
minLength: 1
|
|
72
|
+
}
|
|
73
|
+
}] } }
|
|
74
|
+
}];
|
|
75
|
+
const decodeOverride = Schema.decodeUnknownResult(RuleOverrideSchema, STRICT);
|
|
76
|
+
const overrideRule = Function.dual(DATA_FIRST_ARITY_3$1, (rule, raw, providers) => {
|
|
77
|
+
if (Predicate.isUndefined(raw)) return rule;
|
|
78
|
+
const { model } = checked(decodeOverride, raw, `options for rule "${rule.id}"`);
|
|
79
|
+
return {
|
|
80
|
+
...rule,
|
|
81
|
+
models: Option.match(Option.fromUndefinedOr(model), {
|
|
82
|
+
onNone: () => rule.models,
|
|
83
|
+
onSome: (spec) => pipe(modelIdsOf(spec), Array.map((id) => resolveModel(id, providers)))
|
|
84
|
+
})
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
// src/features/judge-code/model/explain-text.ts
|
|
88
|
+
const DECIMALS$1 = 2;
|
|
89
|
+
const fixed = (value) => value.toFixed(DECIMALS$1);
|
|
90
|
+
const cachedText = (cached) => Option.match(Option.fromUndefinedOr(cached), {
|
|
91
|
+
onNone: () => "unknown",
|
|
92
|
+
onSome: (hit) => Boolean.match(hit, {
|
|
93
|
+
onTrue: () => "yes",
|
|
94
|
+
onFalse: () => "no"
|
|
95
|
+
})
|
|
96
|
+
});
|
|
97
|
+
const probabilitiesText = (probabilities) => Option.match(Array.match(Record.toEntries(probabilities), {
|
|
98
|
+
onEmpty: () => Option.none(),
|
|
99
|
+
onNonEmpty: (entries) => Option.some(pipe(entries, Array.map(([label, value]) => `${label}=${fixed(value)}`), Array.join(" ")))
|
|
100
|
+
}), {
|
|
101
|
+
onNone: () => "(no answer)",
|
|
102
|
+
onSome: (text) => text
|
|
103
|
+
});
|
|
104
|
+
const verdictText = (explanation) => {
|
|
105
|
+
if (Predicate.isNotUndefined(explanation.error)) return `error: ${explanation.error}`;
|
|
106
|
+
const outcome = Boolean.match(explanation.reported, {
|
|
107
|
+
onTrue: () => "reported",
|
|
108
|
+
onFalse: () => "not reported"
|
|
109
|
+
});
|
|
110
|
+
const cutoff = `cutoff ${fixed(explanation.cutoff)}`;
|
|
111
|
+
return Option.match(Option.fromUndefinedOr(explanation.verdict), {
|
|
112
|
+
onNone: () => `no verdict, ${cutoff}, ${outcome}`,
|
|
113
|
+
onSome: ({ answer, probability }) => `${answer} ${fixed(probability)}, ${cutoff}, ${outcome}`
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
const servedText = (served) => Option.match(Option.fromUndefinedOr(served), {
|
|
117
|
+
onNone: () => "",
|
|
118
|
+
onSome: (id) => ` (served ${id})`
|
|
119
|
+
});
|
|
120
|
+
const asText = (explanation) => pipe([
|
|
121
|
+
`${PREFIX} explain: ${explanation.file}:${String(explanation.line)}:${String(explanation.column)} [${explanation.rule}] ${explanation.model}${servedText(explanation.served)} cached=${cachedText(explanation.cached)} ${String(explanation.durationMs)}ms`,
|
|
122
|
+
` state: ${String(explanation.state.chars)} chars ${toJson(explanation.state.head)}`,
|
|
123
|
+
...pipe(explanation.questions, Array.map(({ id, question, probabilities }) => ` ${id}: ${probabilitiesText(probabilities)}\n ${question}`)),
|
|
124
|
+
` verdict: ${verdictText(explanation)}`
|
|
125
|
+
], Array.join("\n"));
|
|
126
|
+
// src/features/judge-code/model/explain.ts
|
|
127
|
+
const EXPLAIN_ENV = "OXLINT_DECISION_EXPLAIN";
|
|
128
|
+
const OFF_VALUES = /* @__PURE__ */ new Set([
|
|
129
|
+
"0",
|
|
130
|
+
"false",
|
|
131
|
+
"off",
|
|
132
|
+
"no"
|
|
133
|
+
]);
|
|
134
|
+
const STATE_HEAD_CHARS = 60;
|
|
135
|
+
const OPTION_SEPARATOR = "__";
|
|
136
|
+
const FIRST = 1;
|
|
137
|
+
const START = 0;
|
|
138
|
+
const COLUMN_BASE = 1;
|
|
139
|
+
const SEPARATOR = /^[/\\]/u;
|
|
140
|
+
const relativeTo = (root, filename) => Boolean.match(String$1.isNonEmpty(root) && String$1.startsWith(root)(filename) && SEPARATOR.test(String$1.slice(root.length)(filename)), {
|
|
141
|
+
onTrue: () => String$1.slice(root.length + FIRST)(filename),
|
|
142
|
+
onFalse: () => filename
|
|
143
|
+
});
|
|
144
|
+
const fromSetting = (setting) => Match.value(setting).pipe(Match.when("json", () => "json"), Match.when(true, () => "text"), Match.when("text", () => "text"), Match.orElse(() => "off"));
|
|
145
|
+
const fromEnv = (value) => Match.value(String$1.toLowerCase(value)).pipe(Match.when("json", () => "json"), Match.when((lower) => OFF_VALUES.has(lower), () => "off"), Match.orElse(() => "text"));
|
|
146
|
+
const explainModeOf = (setting) => Option.match(envOf(EXPLAIN_ENV), {
|
|
147
|
+
onNone: () => fromSetting(setting),
|
|
148
|
+
onSome: fromEnv
|
|
149
|
+
});
|
|
150
|
+
const probabilitiesIn = (answer) => {
|
|
151
|
+
if (answer.type === "noul") return Option.some({ yes: answer.noul });
|
|
152
|
+
if (answer.type === "choice") return Option.fromUndefinedOr(answer.probabilities);
|
|
153
|
+
return Option.none();
|
|
154
|
+
};
|
|
155
|
+
const probabilitiesOf = (answer) => Option.fromUndefinedOr(answer).pipe(Option.flatMap(probabilitiesIn), Option.getOrElse(() => ({})));
|
|
156
|
+
const asksAbout = (ref, id) => pipe(String$1.split(id, OPTION_SEPARATOR), Array.take(FIRST), Array.contains(ref));
|
|
157
|
+
const sentFor = (sent, ref) => pipe(sent, Array.flatMap((wire) => pipe(Record.toEntries(wire.questions), Array.filter(([id]) => asksAbout(ref, id)))));
|
|
158
|
+
const stateTextOf = (sent, ref, snippet) => Array.findFirst(sent, (wire) => pipe(Record.keys(wire.questions), Array.some((id) => asksAbout(ref, id)))).pipe(Option.map(({ state }) => state), Option.filter(Predicate.isString), Option.getOrElse(() => snippet));
|
|
159
|
+
const summaryOf = (text) => ({
|
|
160
|
+
chars: text.length,
|
|
161
|
+
head: String$1.slice(START, STATE_HEAD_CHARS)(String$1.trim(String$1.replaceAll(/\s+/gu, " ")(text)))
|
|
162
|
+
});
|
|
163
|
+
const baseOf = ({ pass, sent }, match, { ref, loc }) => {
|
|
164
|
+
const questions = sentFor(sent, ref);
|
|
165
|
+
return (answers) => ({
|
|
166
|
+
file: relativeTo(pass.cwd, pass.filename),
|
|
167
|
+
line: loc.start.line,
|
|
168
|
+
column: loc.start.column + COLUMN_BASE,
|
|
169
|
+
rule: match.rule.id,
|
|
170
|
+
model: pass.model,
|
|
171
|
+
cutoff: match.rule.cutoff,
|
|
172
|
+
durationMs: pass.durationMs,
|
|
173
|
+
state: summaryOf(stateTextOf(sent, ref, match.snippet)),
|
|
174
|
+
questions: pipe(questions, Array.map(([id, question]) => ({
|
|
175
|
+
id,
|
|
176
|
+
question: question.instructions,
|
|
177
|
+
probabilities: probabilitiesOf(answers[id])
|
|
178
|
+
})))
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
const printerFor = (mode) => (explanation) => {
|
|
182
|
+
const line = Boolean.match(mode === "json", {
|
|
183
|
+
onTrue: () => toJson(explanation),
|
|
184
|
+
onFalse: () => asText(explanation)
|
|
185
|
+
});
|
|
186
|
+
Effect.runFork(Console.error(line));
|
|
187
|
+
};
|
|
188
|
+
const explaining = (pass, mode) => {
|
|
189
|
+
const sent = Boolean.match(pass.yesNo, {
|
|
190
|
+
onTrue: () => yesNoRequestsFor(pass.request),
|
|
191
|
+
onFalse: () => wireRequestsFor(pass.request)
|
|
192
|
+
});
|
|
193
|
+
const print = printerFor(mode);
|
|
194
|
+
return {
|
|
195
|
+
outcome: (match, at, { served, answers, cached, verdict, reported }) => {
|
|
196
|
+
print({
|
|
197
|
+
...baseOf({
|
|
198
|
+
pass,
|
|
199
|
+
sent
|
|
200
|
+
}, match, at)(answers),
|
|
201
|
+
served,
|
|
202
|
+
...Option.match(Option.fromUndefinedOr(cached), {
|
|
203
|
+
onNone: () => ({}),
|
|
204
|
+
onSome: (hit) => ({ cached: hit })
|
|
205
|
+
}),
|
|
206
|
+
...Option.match(verdict, {
|
|
207
|
+
onNone: () => ({}),
|
|
208
|
+
onSome: (found) => ({ verdict: found })
|
|
209
|
+
}),
|
|
210
|
+
reported
|
|
211
|
+
});
|
|
212
|
+
},
|
|
213
|
+
failure: (matches, reason) => {
|
|
214
|
+
for (const [index, match] of matches.entries()) {
|
|
215
|
+
const at = {
|
|
216
|
+
ref: refAt(index),
|
|
217
|
+
loc: match.loc
|
|
218
|
+
};
|
|
219
|
+
print({
|
|
220
|
+
...baseOf({
|
|
221
|
+
pass,
|
|
222
|
+
sent
|
|
223
|
+
}, match, at)({}),
|
|
224
|
+
reported: false,
|
|
225
|
+
error: reason
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
const SILENT = {
|
|
232
|
+
outcome: Function.constVoid,
|
|
233
|
+
failure: Function.constVoid
|
|
234
|
+
};
|
|
235
|
+
const explainerFor = (pass) => Match.value(explainModeOf(pass.setting)).pipe(Match.when("off", () => SILENT), Match.orElse((mode) => explaining(pass, mode)));
|
|
236
|
+
// src/features/judge-code/model/locate.ts
|
|
237
|
+
const DATA_FIRST_ARITY_3 = 3;
|
|
238
|
+
const DATA_FIRST_ARITY$1 = 2;
|
|
239
|
+
const MINIMUM_TIMEOUT_MS = 1;
|
|
240
|
+
const remainingMs = (deadline) => Math.max(MINIMUM_TIMEOUT_MS, deadline - nowMs());
|
|
241
|
+
const verdictsOf = Function.dual(DATA_FIRST_ARITY$1, (result, request) => {
|
|
242
|
+
if (!result.ok) return result;
|
|
243
|
+
return runOrElse(Effect.sync(() => ({
|
|
244
|
+
ok: true,
|
|
245
|
+
verdicts: parseVerdicts(result.response, request),
|
|
246
|
+
cached: result.cached
|
|
247
|
+
})), (cause) => ({
|
|
248
|
+
ok: false,
|
|
249
|
+
reason: messageOf(cause)
|
|
250
|
+
}));
|
|
251
|
+
});
|
|
252
|
+
const inputOf = Function.dual(DATA_FIRST_ARITY$1, (asking, request) => ({
|
|
253
|
+
request,
|
|
254
|
+
timeoutMs: remainingMs(asking.deadline),
|
|
255
|
+
env: asking.env
|
|
256
|
+
}));
|
|
257
|
+
const fetchVerdicts = (asking, request) => verdictsOf(asking.model.provider.decide(inputOf(asking, request)), request);
|
|
258
|
+
const isRange = (location) => Option.exists(location, ({ start, end }) => start !== end);
|
|
259
|
+
const refineOnce = ({ asking, match, ref, state }, within) => {
|
|
260
|
+
const questionId = locationRefOf(ref);
|
|
261
|
+
const candidates = locationCandidates(match.snippet, within);
|
|
262
|
+
const refined = fetchVerdicts(asking, {
|
|
263
|
+
model: asking.model.model,
|
|
264
|
+
state,
|
|
265
|
+
questions: { [questionId]: locationQuestion(match, ref, candidates) }
|
|
266
|
+
});
|
|
267
|
+
if (!refined.ok) return refined;
|
|
268
|
+
return {
|
|
269
|
+
...refined,
|
|
270
|
+
next: selectedLocation(refined.verdicts.answers[questionId], candidates, match.rule.location.cutoff)
|
|
271
|
+
};
|
|
272
|
+
};
|
|
273
|
+
const refine = (refinement, first) => {
|
|
274
|
+
let location = first;
|
|
275
|
+
while (isRange(location)) {
|
|
276
|
+
const step = refineOnce(refinement, location.value);
|
|
277
|
+
if (!step.ok) return {
|
|
278
|
+
loc: refinement.match.loc,
|
|
279
|
+
failure: Option.some(step.reason)
|
|
280
|
+
};
|
|
281
|
+
location = step.next ?? Option.none();
|
|
282
|
+
}
|
|
283
|
+
return {
|
|
284
|
+
loc: Option.match(location, {
|
|
285
|
+
onNone: () => refinement.match.loc,
|
|
286
|
+
onSome: ({ start }) => locationOfLine(refinement.match.snippet, start)
|
|
287
|
+
}),
|
|
288
|
+
failure: Option.none()
|
|
289
|
+
};
|
|
290
|
+
};
|
|
291
|
+
const locateMatch = Function.dual(DATA_FIRST_ARITY_3, (asking, match, { ref, verdicts, request }) => {
|
|
292
|
+
if (!locates(match)) return {
|
|
293
|
+
loc: match.loc,
|
|
294
|
+
failure: Option.none()
|
|
295
|
+
};
|
|
296
|
+
const first = selectedLocation(verdicts.answers[locationRefOf(ref)], locationCandidates(match.snippet, EVERY_LINE), match.rule.location.cutoff);
|
|
297
|
+
return refine({
|
|
298
|
+
asking,
|
|
299
|
+
match,
|
|
300
|
+
ref,
|
|
301
|
+
state: request.state
|
|
302
|
+
}, first);
|
|
303
|
+
});
|
|
304
|
+
// src/features/judge-code/model/report.ts
|
|
305
|
+
const DECIMALS = 2;
|
|
306
|
+
const failsHere = (pass, reason) => failsInCi(pass.options) && !OUT_OF_BUDGET.test(reason);
|
|
307
|
+
const degrade = (pass, reason) => {
|
|
308
|
+
if (failsHere(pass, reason)) throw optionsError(`${reason} (${pass.filename})`);
|
|
309
|
+
warnOnce(reason, `${reason} (${pass.filename})`);
|
|
310
|
+
};
|
|
311
|
+
const reportFailure = (pass, failure) => {
|
|
312
|
+
if (failsHere(pass, failure.reason)) {
|
|
313
|
+
pass.contextOf(failure.match).report({
|
|
314
|
+
loc: failure.loc,
|
|
315
|
+
messageId: "unlocated",
|
|
316
|
+
data: {
|
|
317
|
+
id: failure.match.rule.id,
|
|
318
|
+
reason: failure.reason
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
warnOnce(failure.reason, `${failure.reason} (${pass.filename})`);
|
|
324
|
+
};
|
|
325
|
+
const locate = (answered, match, { ref, failed }) => {
|
|
326
|
+
if (failed) return {
|
|
327
|
+
loc: match.loc,
|
|
328
|
+
failure: Option.none()
|
|
329
|
+
};
|
|
330
|
+
return locateMatch(answered.asking, match, {
|
|
331
|
+
...answered,
|
|
332
|
+
ref
|
|
333
|
+
});
|
|
334
|
+
};
|
|
335
|
+
const outcomeOf = (answered, match, ref) => {
|
|
336
|
+
const verdict = Option.fromUndefinedOr(answered.verdicts.results[ref]);
|
|
337
|
+
return {
|
|
338
|
+
served: `${answered.asking.model.provider.name}/${answered.verdicts.model}`,
|
|
339
|
+
cached: answered.cached,
|
|
340
|
+
answers: answered.verdicts.answers,
|
|
341
|
+
verdict,
|
|
342
|
+
reported: Option.exists(verdict, (found) => isReported(match.rule, found))
|
|
343
|
+
};
|
|
344
|
+
};
|
|
345
|
+
const reportMatch = (pass, answered, { match, ref, failed }) => {
|
|
346
|
+
const outcome = outcomeOf(answered, match, ref);
|
|
347
|
+
const { verdict } = outcome;
|
|
348
|
+
if (!outcome.reported || Option.isNone(verdict)) {
|
|
349
|
+
answered.explainer.outcome(match, {
|
|
350
|
+
ref,
|
|
351
|
+
loc: match.loc
|
|
352
|
+
}, outcome);
|
|
353
|
+
return Option.none();
|
|
354
|
+
}
|
|
355
|
+
const located = locate(answered, match, {
|
|
356
|
+
ref,
|
|
357
|
+
failed
|
|
358
|
+
});
|
|
359
|
+
const at = {
|
|
360
|
+
ref,
|
|
361
|
+
loc: located.loc
|
|
362
|
+
};
|
|
363
|
+
answered.explainer.outcome(match, at, outcome);
|
|
364
|
+
pass.contextOf(match).report({
|
|
365
|
+
loc: located.loc,
|
|
366
|
+
messageId: "yes",
|
|
367
|
+
data: {
|
|
368
|
+
id: match.rule.id,
|
|
369
|
+
model: `${pass.model.provider.name}/${answered.verdicts.model}`,
|
|
370
|
+
answer: verdict.value.answer,
|
|
371
|
+
score: verdict.value.probability.toFixed(DECIMALS),
|
|
372
|
+
cutoff: match.rule.cutoff.toFixed(DECIMALS),
|
|
373
|
+
question: match.rule.message ?? match.rule.question
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
return located.failure;
|
|
377
|
+
};
|
|
378
|
+
const reportMatches = (pass, answered) => {
|
|
379
|
+
let failure = Option.none();
|
|
380
|
+
for (const [index, match] of pass.matches.entries()) {
|
|
381
|
+
const located = reportMatch(pass, answered, {
|
|
382
|
+
match,
|
|
383
|
+
ref: refAt(index),
|
|
384
|
+
failed: Option.isSome(failure)
|
|
385
|
+
});
|
|
386
|
+
failure = Option.orElse(failure, () => located.pipe(Option.map((reason) => ({
|
|
387
|
+
match,
|
|
388
|
+
loc: match.loc,
|
|
389
|
+
reason
|
|
390
|
+
}))));
|
|
391
|
+
}
|
|
392
|
+
return failure;
|
|
393
|
+
};
|
|
394
|
+
const explainPassOf = (pass, { request, durationMs }) => ({
|
|
395
|
+
setting: pass.options.explain,
|
|
396
|
+
cwd: Option.match(Array.head(pass.matches), {
|
|
397
|
+
onNone: () => "",
|
|
398
|
+
onSome: (match) => pass.contextOf(match).cwd
|
|
399
|
+
}),
|
|
400
|
+
filename: pass.filename,
|
|
401
|
+
model: pass.model.id,
|
|
402
|
+
yesNo: pass.model.provider.yesNo === true,
|
|
403
|
+
request,
|
|
404
|
+
durationMs
|
|
405
|
+
});
|
|
406
|
+
const explainerOf = (pass, timed) => explainerFor(explainPassOf(pass, timed));
|
|
407
|
+
const prepare = (pass) => ({
|
|
408
|
+
pass,
|
|
409
|
+
asking: {
|
|
410
|
+
model: pass.model,
|
|
411
|
+
deadline: pass.deadline,
|
|
412
|
+
env: pass.options.env
|
|
413
|
+
},
|
|
414
|
+
request: buildRequest(pass.model.model, pass.matches)
|
|
415
|
+
});
|
|
416
|
+
const reportAnswer = ({ pass, asking, request }, { decided, durationMs }) => {
|
|
417
|
+
const explainer = explainerOf(pass, {
|
|
418
|
+
request,
|
|
419
|
+
durationMs
|
|
420
|
+
});
|
|
421
|
+
const result = verdictsOf(decided, request);
|
|
422
|
+
if (!result.ok) {
|
|
423
|
+
explainer.failure(pass.matches, result.reason);
|
|
424
|
+
degrade(pass, result.reason);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
Option.map(reportMatches(pass, {
|
|
428
|
+
...result,
|
|
429
|
+
asking,
|
|
430
|
+
request,
|
|
431
|
+
explainer
|
|
432
|
+
}), (failure) => {
|
|
433
|
+
reportFailure(pass, failure);
|
|
434
|
+
});
|
|
435
|
+
};
|
|
436
|
+
const decideWith = (provider, prepared) => {
|
|
437
|
+
const inputs = pipe(prepared, Array.map(({ asking, request }) => inputOf(asking, request)));
|
|
438
|
+
return Option.match(Option.fromUndefinedOr(provider.decideAll), {
|
|
439
|
+
onNone: () => pipe(inputs, Array.map((input) => provider.decide(input))),
|
|
440
|
+
onSome: (decideAll) => decideAll(inputs)
|
|
441
|
+
});
|
|
442
|
+
};
|
|
443
|
+
const reportProvider = (prepared) => {
|
|
444
|
+
const { provider } = Array.headNonEmpty(prepared).pass.model;
|
|
445
|
+
const started = performance.now();
|
|
446
|
+
const results = decideWith(provider, prepared);
|
|
447
|
+
const durationMs = Math.round(performance.now() - started);
|
|
448
|
+
const answers = Boolean.match(results.length === prepared.length, {
|
|
449
|
+
onTrue: () => results,
|
|
450
|
+
onFalse: () => pipe(prepared, Array.map(() => ({
|
|
451
|
+
ok: false,
|
|
452
|
+
reason: `provider ${provider.name} returned ${String(results.length)} results for ${String(prepared.length)} requests`
|
|
453
|
+
})))
|
|
454
|
+
});
|
|
455
|
+
for (const [one, decided] of Array.zip(prepared, answers)) reportAnswer(one, {
|
|
456
|
+
decided,
|
|
457
|
+
durationMs
|
|
458
|
+
});
|
|
459
|
+
};
|
|
460
|
+
const reportModels = (passes) => {
|
|
461
|
+
const byProvider = Array.groupBy(pipe(passes, Array.map((pass) => prepare(pass))), ({ pass }) => pass.model.provider.name);
|
|
462
|
+
for (const prepared of Record.values(byProvider)) reportProvider(prepared);
|
|
463
|
+
};
|
|
464
|
+
// src/features/judge-code/model/visitor.ts
|
|
465
|
+
const DATA_FIRST_ARITY = 2;
|
|
466
|
+
const raisedMissing = /* @__PURE__ */ new Set();
|
|
467
|
+
const missingEnvOf = (options, provider) => pipe(provider.requiredEnv, Array.map((name) => envNameOf(options.env, name)), Array.filter((name) => Option.isNone(envOf(name))));
|
|
468
|
+
const isAvailable = (options, { provider }) => {
|
|
469
|
+
const missingNames = missingEnvOf(options, provider);
|
|
470
|
+
if (Array.isReadonlyArrayEmpty(missingNames)) return true;
|
|
471
|
+
const missing = pipe(missingNames, Array.join(", "));
|
|
472
|
+
if (!failsInCi(options)) warnOnce(`missing:${provider.name}`, `${missing} is not set, skipping ${provider.name} models`);
|
|
473
|
+
else if (!raisedMissing.has(provider.name)) {
|
|
474
|
+
raisedMissing.add(provider.name);
|
|
475
|
+
throw optionsError(`${missing} is not set`);
|
|
476
|
+
}
|
|
477
|
+
return false;
|
|
478
|
+
};
|
|
479
|
+
const availableModels = (batch) => {
|
|
480
|
+
const models = pipe(Record.values(batch.rules), Array.flatMap((rule) => rule.models), Array.map((model) => [model.id, model]));
|
|
481
|
+
return pipe(Array.fromIterable(new Map(models).values()), Array.filter((model) => isAvailable(batch.options, model)));
|
|
482
|
+
};
|
|
483
|
+
const snippetOf = (context, node) => {
|
|
484
|
+
if (node.type === "Program") return context.sourceCode.text;
|
|
485
|
+
return context.sourceCode.getText(node);
|
|
486
|
+
};
|
|
487
|
+
const namedSnippetOf = (context, node) => {
|
|
488
|
+
const named = snippetNodeFor(node);
|
|
489
|
+
if (named === node) return snippetOf(context, node);
|
|
490
|
+
return context.sourceCode.getText(named);
|
|
491
|
+
};
|
|
492
|
+
const matchOf = (context, node, { rule, options }) => {
|
|
493
|
+
const text = Option.getOrElse(commentedSnippetOf(context, node, rule), () => namedSnippetOf(context, node));
|
|
494
|
+
const surrounding = contextOf(context, node, rule.context ?? { scope: "node" });
|
|
495
|
+
return {
|
|
496
|
+
...Option.match(surrounding, {
|
|
497
|
+
onNone: () => ({}),
|
|
498
|
+
onSome: (value) => ({ context: value })
|
|
499
|
+
}),
|
|
500
|
+
rule,
|
|
501
|
+
loc: REPORT_LOC[rule.target](node, snippetOf(context, node)),
|
|
502
|
+
snippet: truncateSnippet(text, options.maxSnippetChars),
|
|
503
|
+
truncated: text.length > options.maxSnippetChars
|
|
504
|
+
};
|
|
505
|
+
};
|
|
506
|
+
const contextOfRule = (batch, id) => Option.getOrThrow(Option.fromUndefinedOr(batch.contexts[id]));
|
|
507
|
+
const finish = (batch) => {
|
|
508
|
+
const [first] = Record.values(batch.contexts);
|
|
509
|
+
if (Predicate.isUndefined(first)) return;
|
|
510
|
+
const deadline = nowMs() + batch.options.timeoutMs;
|
|
511
|
+
const passes = pipe(availableModels(batch), Array.flatMap((model) => {
|
|
512
|
+
const matches = pipe(batch.matches, Array.filter(({ rule }) => pipe(rule.models, Array.some(({ id }) => id === model.id))));
|
|
513
|
+
return Array.match(matches, {
|
|
514
|
+
onEmpty: () => [],
|
|
515
|
+
onNonEmpty: (some) => [{
|
|
516
|
+
contextOf: (match) => contextOfRule(batch, match.rule.id),
|
|
517
|
+
filename: first.filename,
|
|
518
|
+
options: batch.options,
|
|
519
|
+
model,
|
|
520
|
+
matches: some,
|
|
521
|
+
deadline
|
|
522
|
+
}]
|
|
523
|
+
});
|
|
524
|
+
}));
|
|
525
|
+
reportModels(passes);
|
|
526
|
+
};
|
|
527
|
+
const ruleIn = (batch, id) => Option.getOrThrow(Array.findFirst(batch.options.rules, (rule) => rule.id === id));
|
|
528
|
+
const sharedFile = Function.dual(DATA_FIRST_ARITY, (optionsFor, ruleFor) => {
|
|
529
|
+
let current = Option.none();
|
|
530
|
+
const start = (context) => {
|
|
531
|
+
const batch = {
|
|
532
|
+
options: optionsFor(context.settings),
|
|
533
|
+
contexts: {},
|
|
534
|
+
rules: {},
|
|
535
|
+
matches: [],
|
|
536
|
+
exiting: false,
|
|
537
|
+
exits: 0
|
|
538
|
+
};
|
|
539
|
+
current = Option.some(batch);
|
|
540
|
+
return batch;
|
|
541
|
+
};
|
|
542
|
+
return {
|
|
543
|
+
join: (context, id) => {
|
|
544
|
+
const batch = Option.match(current, {
|
|
545
|
+
onNone: () => start(context),
|
|
546
|
+
onSome: (open) => {
|
|
547
|
+
if (open.exiting) return start(context);
|
|
548
|
+
return open;
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
const [options] = context.options;
|
|
552
|
+
const rule = ruleFor(ruleIn(batch, id), options);
|
|
553
|
+
batch.contexts[id] = context;
|
|
554
|
+
batch.rules[id] = rule;
|
|
555
|
+
return {
|
|
556
|
+
batch,
|
|
557
|
+
rule
|
|
558
|
+
};
|
|
559
|
+
},
|
|
560
|
+
leave: () => {
|
|
561
|
+
if (Option.isNone(current)) return;
|
|
562
|
+
const batch = current.value;
|
|
563
|
+
batch.exits += 1;
|
|
564
|
+
if (batch.exits >= Record.keys(batch.contexts).length) current = Option.none();
|
|
565
|
+
if (!batch.exiting) {
|
|
566
|
+
batch.exiting = true;
|
|
567
|
+
finish(batch);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
});
|
|
572
|
+
const isSkipped = (context, node, rule) => !isSelected(context, node, rule) || lacksComment(context, node, rule);
|
|
573
|
+
const visitorFor = Function.dual(DATA_FIRST_ARITY, (shared, { id, target }) => (context) => {
|
|
574
|
+
let file = Option.none();
|
|
575
|
+
const collect = (node) => {
|
|
576
|
+
if (Option.isNone(file)) return;
|
|
577
|
+
const { batch, rule } = file.value;
|
|
578
|
+
if (isSkipped(context, node, rule)) return;
|
|
579
|
+
batch.matches.push(matchOf(context, node, {
|
|
580
|
+
rule,
|
|
581
|
+
options: batch.options
|
|
582
|
+
}));
|
|
583
|
+
};
|
|
584
|
+
const nodeTypes = TARGET_NODE_TYPES[target];
|
|
585
|
+
return {
|
|
586
|
+
...Record.fromEntries(pipe(nodeTypes, Array.map((type) => [type, collect]))),
|
|
587
|
+
Program: (node) => {
|
|
588
|
+
file = Option.some(shared.join(context, id));
|
|
589
|
+
if (target === "file") collect(node);
|
|
590
|
+
},
|
|
591
|
+
"Program:exit": () => {
|
|
592
|
+
file = Option.none();
|
|
593
|
+
shared.leave();
|
|
594
|
+
}
|
|
595
|
+
};
|
|
596
|
+
});
|
|
597
|
+
// src/app/plugin.ts
|
|
598
|
+
const DEFAULT_NAME = "decision";
|
|
599
|
+
const metaOf = (rule) => ({
|
|
600
|
+
type: "problem",
|
|
601
|
+
docs: { description: rule.question },
|
|
602
|
+
schema: RULE_OPTIONS_SCHEMA,
|
|
603
|
+
messages: {
|
|
604
|
+
yes: "[{{id}}] {{model}} answered {{answer}} ({{score}} >= {{cutoff}}): {{question}}",
|
|
605
|
+
unlocated: `${PREFIX}: {{reason}} while refining the location of [{{id}}]; the finding is reported at the file level`
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
const optionsOf = (setup) => Struct.omit(setup, [
|
|
609
|
+
"name",
|
|
610
|
+
"rules",
|
|
611
|
+
"providers"
|
|
612
|
+
]);
|
|
613
|
+
const memoized = (resolve) => {
|
|
614
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
615
|
+
return (settings) => {
|
|
616
|
+
const key = toJson([settings]);
|
|
617
|
+
const cached = resolved.get(key);
|
|
618
|
+
if (Predicate.isNotUndefined(cached)) return cached;
|
|
619
|
+
const fresh = resolve(settings);
|
|
620
|
+
resolved.set(key, fresh);
|
|
621
|
+
return fresh;
|
|
622
|
+
};
|
|
623
|
+
};
|
|
624
|
+
const memoizedRules = (providers) => {
|
|
625
|
+
const resolved = /* @__PURE__ */ new WeakMap();
|
|
626
|
+
return (rule, options) => {
|
|
627
|
+
const known = resolved.get(rule) ?? /* @__PURE__ */ new Map();
|
|
628
|
+
resolved.set(rule, known);
|
|
629
|
+
const key = toJson([options]);
|
|
630
|
+
const cached = known.get(key);
|
|
631
|
+
if (Predicate.isNotUndefined(cached)) return cached;
|
|
632
|
+
const fresh = overrideRule(rule, options, providers);
|
|
633
|
+
known.set(key, fresh);
|
|
634
|
+
return fresh;
|
|
635
|
+
};
|
|
636
|
+
};
|
|
637
|
+
const createDecisionPlugin = (setup) => {
|
|
638
|
+
const name = setup.name ?? DEFAULT_NAME;
|
|
639
|
+
const providers = Record.fromEntries(pipe([
|
|
640
|
+
cloudflare,
|
|
641
|
+
openRouter,
|
|
642
|
+
...setup.providers ?? []
|
|
643
|
+
], Array.map((provider) => [provider.name, provider])));
|
|
644
|
+
const rules = checkRules(setup.rules);
|
|
645
|
+
const options = optionsOf(setup);
|
|
646
|
+
const resolve = memoized((settings) => resolveOptions({
|
|
647
|
+
options,
|
|
648
|
+
settings
|
|
649
|
+
}, rules, providers));
|
|
650
|
+
resolve({});
|
|
651
|
+
const shared = sharedFile((settings) => resolve(settings[name]), memoizedRules(providers));
|
|
652
|
+
return {
|
|
653
|
+
meta: { name },
|
|
654
|
+
rules: Record.fromEntries(pipe(rules, Array.map((rule) => [rule.id, {
|
|
655
|
+
meta: metaOf(rule),
|
|
656
|
+
createOnce: visitorFor(shared, rule)
|
|
657
|
+
}])))
|
|
658
|
+
};
|
|
659
|
+
};
|
|
660
|
+
export { defineProvider as a, defineDecisionRule as i, openRouter as n, cloudflare as r, createDecisionPlugin as t };
|
|
661
|
+
|
|
662
|
+
//# sourceMappingURL=app-Bso1uwY5.mjs.map
|