@andrueandersoncs/lint-anything 0.6.0 → 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/dist/lib.js CHANGED
@@ -6,6 +6,35 @@ import { join } from "path";
6
6
  import { Cause, Config, Context, Effect, flow, Layer, Option, Redacted, Ref, Result, Runtime, Schedule, Schema, Semaphore } from "effect";
7
7
  import { FetchHttpClient, HttpClient, HttpClientError, HttpClientRequest } from "effect/http";
8
8
 
9
+ // src/media.ts
10
+ import { extname } from "path";
11
+ var mediaKinds = ["image", "audio", "video"];
12
+ var mediaNoun = { image: "images", audio: "audio clips", video: "videos" };
13
+ var types = {
14
+ ".png": { type: "image/png", kind: "image" },
15
+ ".jpg": { type: "image/jpeg", kind: "image" },
16
+ ".jpeg": { type: "image/jpeg", kind: "image" },
17
+ ".webp": { type: "image/webp", kind: "image" },
18
+ ".wav": { type: "audio/wav", kind: "audio" },
19
+ ".mp3": { type: "audio/mpeg", kind: "audio" },
20
+ ".flac": { type: "audio/flac", kind: "audio" },
21
+ ".ogg": { type: "audio/ogg", kind: "audio" },
22
+ ".mp4": { type: "video/mp4", kind: "video" },
23
+ ".webm": { type: "video/webm", kind: "video" }
24
+ };
25
+ var mediaExtensions = Object.keys(types);
26
+ var mediaTypeOf = (path) => types[extname(path).toLowerCase()];
27
+ var maxMediaFiles = { image: 4, audio: 4, video: 2 };
28
+ var maxImageBytes = 4 * 1024 * 1024;
29
+ var maxMediaBytes = 8 * 1024 * 1024;
30
+ var readMedium = async (path) => {
31
+ const { type, kind } = mediaTypeOf(path);
32
+ const data = await Bun.file(path).bytes();
33
+ return { path, type, kind, bytes: data.length, digest: new Bun.CryptoHasher("sha256").update(data).digest("hex") };
34
+ };
35
+ var mediaBase64 = async (medium) => Buffer.from(await Bun.file(medium.path).bytes()).toString("base64");
36
+
37
+ // src/decision-model.ts
9
38
  class DecisionModelError extends Schema.TaggedError()("DecisionModelError", {
10
39
  message: Schema.String
11
40
  }) {
@@ -32,6 +61,7 @@ var attemptRetries = 3;
32
61
  var cacheMaxAgeMs = 30 * 24 * 60 * 60 * 1000;
33
62
  var cacheFile = "answers.sqlite";
34
63
  var cacheBusyTimeoutMs = 1e4;
64
+ var dataUrl = (medium) => `data:${medium.type};base64,${medium.base64}`;
35
65
  var bearer = (key) => ["authorization", `Bearer ${key}`];
36
66
  var never = () => false;
37
67
  var Probability = Schema.Number.check(Schema.isBetween({ minimum: 0, maximum: 1 }));
@@ -41,8 +71,18 @@ var SystemOneResponse = Schema.Struct({
41
71
  answers: Schema.Record(Schema.String, Schema.Struct({ noul: Probability })),
42
72
  usage: ReportedUsage
43
73
  });
74
+ var systemOneMedia = (media) => {
75
+ const of = (kind) => media.filter((medium) => medium.kind === kind).map(dataUrl);
76
+ const [images, audio, videos] = [of("image"), of("audio"), of("video")];
77
+ return {
78
+ ...images.length === 0 ? {} : { images },
79
+ ...audio.length === 0 ? {} : { audio },
80
+ ...videos.length === 0 ? {} : { videos }
81
+ };
82
+ };
44
83
  var systemOne = {
45
- head: (model, state) => `${JSON.stringify({ model, state }).slice(0, -1)},"questions":{`,
84
+ head: (model, state, media) => `${JSON.stringify({ model, ...systemOneMedia(media), state }).slice(0, -1)},"questions":{`,
85
+ takes: mediaKinds,
46
86
  tail: "}}",
47
87
  entry: (id, question) => `${JSON.stringify(id)}:${JSON.stringify(noul(question))}`,
48
88
  decode: (body) => Schema.decodeUnknownEffect(SystemOneResponse)(typeof body === "object" && body !== null && "result" in body ? body.result : body).pipe(Effect.map(({ answers, usage }) => ({
@@ -67,8 +107,16 @@ var DecisionsResponse = Schema.Struct({
67
107
  ])),
68
108
  usage: ReportedUsage
69
109
  });
110
+ var decisionsInput = (state, media) => media.length === 0 ? state : [{
111
+ role: "user",
112
+ content: [
113
+ ...media.map((medium) => ({ type: "input_image", image_url: dataUrl(medium) })),
114
+ { type: "input_text", text: state }
115
+ ]
116
+ }];
70
117
  var decisions = {
71
- head: (model, state) => `${JSON.stringify({ model, input: state }).slice(0, -1)},"questions":[`,
118
+ head: (model, state, media) => `${JSON.stringify({ model, input: decisionsInput(state, media) }).slice(0, -1)},"questions":[`,
119
+ takes: ["image"],
72
120
  tail: "]}",
73
121
  entry: (id, question) => JSON.stringify(predicate(id, question)),
74
122
  decode: (body) => Schema.decodeUnknownEffect(DecisionsResponse)(body).pipe(Effect.map(({ answers, usage }) => ({
@@ -90,8 +138,13 @@ var GeminiResponse = Schema.Struct({
90
138
  var GeminiBlocked = Schema.fromJsonString(Schema.Struct({
91
139
  error: Schema.Struct({ code: Schema.Literals(["safety", "prohibited_content", "blocklist", "image_safety", "content_blocked"]) })
92
140
  }));
141
+ var geminiInput = (state, media) => media.length === 0 ? state : [
142
+ ...media.map((medium) => ({ type: medium.kind, data: medium.base64, mime_type: medium.type })),
143
+ { type: "text", text: state }
144
+ ];
93
145
  var gemini = {
94
- head: (model, state) => `${JSON.stringify({ model, input: state }).slice(0, -1)},"questions":{`,
146
+ head: (model, state, media) => `${JSON.stringify({ model, input: geminiInput(state, media) }).slice(0, -1)},"questions":{`,
147
+ takes: mediaKinds,
95
148
  tail: "}}",
96
149
  entry: (id, question) => `${JSON.stringify(id)}:${JSON.stringify(geminiNoul(question))}`,
97
150
  decode: (body) => Schema.decodeUnknownEffect(GeminiResponse)(body).pipe(Effect.map(({ answers, usage }) => ({
@@ -105,6 +158,11 @@ var wires = { "system-one": systemOne, decisions, gemini };
105
158
  var decodeReply = (format, body) => wires[format].decode(body);
106
159
  var keyHeader = (format, key) => wires[format].keyHeader(key);
107
160
  var declinesAll = (format, status, body) => wires[format].declinesAll(status, body);
161
+ var workersAiMedia = {
162
+ clef: ["image"],
163
+ "clef-flash": ["image"],
164
+ "clef-omni": mediaKinds
165
+ };
108
166
  var clef = workersAiModel("clef");
109
167
  function workersAiModel(model) {
110
168
  return {
@@ -112,7 +170,8 @@ function workersAiModel(model) {
112
170
  model,
113
171
  name: model === "clef" ? "Clef" : model,
114
172
  cacheKey: model,
115
- inputTokenPrice: model === "clef" ? clefInputTokenPrice : undefined
173
+ inputTokenPrice: model === "clef" ? clefInputTokenPrice : undefined,
174
+ media: workersAiMedia[model]
116
175
  };
117
176
  }
118
177
  var workersAiTarget = (options) => {
@@ -135,7 +194,8 @@ var endpointModel = (url, model) => ({
135
194
  model,
136
195
  name: model,
137
196
  cacheKey: `${url} ${model}`,
138
- inputTokenPrice: undefined
197
+ inputTokenPrice: undefined,
198
+ media: undefined
139
199
  });
140
200
  var endpointTarget = (options) => ({ ...endpointModel(options.url, options.model), url: options.url, token: options.token });
141
201
  var configError = (message) => new DecisionModelError({ message });
@@ -174,9 +234,9 @@ var configuredTarget = Effect.gen(function* () {
174
234
  const baseUrl = yield* Config.String("CLOUDFLARE_API_BASE_URL").pipe(Config.withDefault(defaultCloudflareBaseUrl), Effect.mapError((error) => configError(`CLOUDFLARE_API_BASE_URL: ${error.message}`)));
175
235
  return workersAiTarget({ accountId, token, model: chosen.model, baseUrl });
176
236
  });
177
- var pack = (model, state, values, question) => {
237
+ var pack = (model, state, media, values, question) => {
178
238
  const wire = wires[model.format];
179
- const head = wire.head(model.model, state);
239
+ const head = wire.head(model.model, state, []);
180
240
  const fixedBytes = Buffer.byteLength(head) + Buffer.byteLength(wire.tail);
181
241
  const entry = (value, index) => {
182
242
  const id = `i${index}`;
@@ -190,7 +250,7 @@ var pack = (model, state, values, question) => {
190
250
  let bytes = fixedBytes;
191
251
  const flush = () => {
192
252
  if (questions.length > 0)
193
- requests.push({ body: `${head}${entries.join(",")}${wire.tail}`, questions });
253
+ requests.push({ body: `${head}${entries.join(",")}${wire.tail}`, media, questions });
194
254
  entries = [];
195
255
  questions = [];
196
256
  bytes = fixedBytes;
@@ -213,7 +273,31 @@ var pack = (model, state, values, question) => {
213
273
  flush();
214
274
  return { requests, oversized };
215
275
  };
216
- var planRequests = (state, questions, model = clef) => pack(model, state, questions.map((_, index) => index), (index) => questions[index]);
276
+ var planRequests = (state, questions, model = clef) => {
277
+ const groups = new Map;
278
+ questions.forEach((question, index) => {
279
+ const key = JSON.stringify(question.media ?? []);
280
+ const group = groups.get(key);
281
+ if (group === undefined)
282
+ groups.set(key, [index]);
283
+ else
284
+ group.push(index);
285
+ });
286
+ const requests = [];
287
+ const oversized = [];
288
+ for (const indexes of groups.values()) {
289
+ const media = questions[indexes[0]].media ?? [];
290
+ const planned = pack(model, state, media, indexes, (index) => questions[index]);
291
+ requests.push(...planned.requests);
292
+ oversized.push(...planned.oversized);
293
+ }
294
+ return { requests, oversized };
295
+ };
296
+ var withMedia = (model, state, body, media) => {
297
+ const head = wires[model.format].head;
298
+ return `${head(model.model, state, media)}${body.slice(head(model.model, state, []).length)}`;
299
+ };
300
+ var mediaPrint = (media) => media.map((medium) => `${medium.type} ${medium.digest}`).join("\x00");
217
301
  var openAnswerCache = (directory, now, model) => {
218
302
  mkdirSync(directory, { recursive: true });
219
303
  const db = new Database(join(directory, cacheFile));
@@ -228,15 +312,15 @@ var openAnswerCache = (directory, now, model) => {
228
312
  db.run("DELETE FROM answers WHERE answered <= ?", [now - cacheMaxAgeMs]);
229
313
  const select = db.query("SELECT noul FROM answers WHERE key = ?");
230
314
  const upsert = db.query("INSERT OR REPLACE INTO answers (key, noul, answered) VALUES (?, ?, ?)");
231
- const key = (state, question) => new Bun.CryptoHasher("sha256").update(`${model.cacheKey}\x00${state}\x00${JSON.stringify(noul(question))}`).digest();
315
+ const key = (state, question, print) => new Bun.CryptoHasher("sha256").update(`${model.cacheKey}\x00${state}\x00${JSON.stringify(noul(question))}${print === "" ? "" : `\x00${print}`}`).digest();
232
316
  const insert = db.transaction((state, answers) => {
233
317
  const answered = Date.now();
234
- for (const { question, noul } of answers)
235
- upsert.run(key(state, question), noul, answered);
318
+ for (const { question, print, noul } of answers)
319
+ upsert.run(key(state, question, print), noul, answered);
236
320
  });
237
321
  const failed = (error) => new DecisionModelError({ message: `answer cache ${join(directory, cacheFile)}: ${error instanceof Error ? error.message : String(error)}` });
238
322
  return {
239
- get: (state, questions) => Effect.try({ try: () => questions.map((question) => select.get(key(state, question))?.noul), catch: failed }),
323
+ get: (state, questions, prints) => Effect.try({ try: () => questions.map((question, index) => select.get(key(state, question, prints[index]))?.noul), catch: failed }),
240
324
  set: (state, answers) => Effect.try({ try: () => insert(state, answers), catch: failed }),
241
325
  close: () => db.close()
242
326
  };
@@ -298,8 +382,38 @@ var make = Effect.fnUntraced(function* (target, cacheDirectory) {
298
382
  const { response } = error.reason;
299
383
  return response.text.pipe(Effect.timeout("5 seconds"), Effect.map((text) => wire.declinesAll(response.status, text)), Effect.orElseSucceed(() => false));
300
384
  };
301
- const send = Effect.fn("DecisionModel.send")(function* (source, request) {
302
- const reply = yield* record(source, { requests: 1 }).pipe(Effect.andThen(HttpClientRequest.post(target.url).pipe(HttpClientRequest.bodyText(request.body, "application/json"), client.execute)), Effect.flatMap((response) => response.json), Effect.flatMap(wire.decode), permits.withPermit, Effect.catch((error) => Effect.flatMap(declinesAll(error), (all) => all ? Effect.succeed({ answers: new Map(request.questions.map(({ id }) => [id, "declined"])), inputTokens: undefined }) : Effect.flatMap(failureMessage(target.name, error), (message) => Effect.fail(new DecisionModelError({ message }))))));
385
+ const readMedia = (questions) => Effect.forEach([...new Set(questions.flatMap((question) => question.media ?? []))], (path) => Effect.tryPromise({
386
+ try: () => readMedium(path),
387
+ catch: (error) => new DecisionModelError({ message: `cannot read media ${path}: ${error instanceof Error ? error.message : String(error)}` })
388
+ })).pipe(Effect.map((media) => new Map(media.map((medium) => [medium.path, medium]))));
389
+ const takes = (kind) => wire.takes.includes(kind) && (target.media?.includes(kind) ?? true);
390
+ const taken = ["text", ...mediaKinds.filter(takes).map((kind) => mediaNoun[kind])];
391
+ const takenList = taken.length <= 2 ? taken.join(" and ") : `${taken.slice(0, -1).join(", ")}, and ${taken.at(-1)}`;
392
+ const notTaken = (medium) => new DecisionModelError({ message: `${target.name} takes ${takenList}, not ${mediaNoun[medium.kind]}: ${medium.path}` });
393
+ const mediaTooLarge = (media) => {
394
+ const image = media.find((medium) => medium.kind === "image" && medium.bytes > maxImageBytes);
395
+ if (image !== undefined) {
396
+ return new RequestTooLarge({
397
+ bytes: image.bytes,
398
+ message: `image ${image.path} is ${image.bytes} bytes, over the ${maxImageBytes}-byte image limit`
399
+ });
400
+ }
401
+ const bytes = media.reduce((sum, medium) => sum + medium.bytes, 0);
402
+ return bytes > maxMediaBytes ? new RequestTooLarge({ bytes, message: `media are ${bytes} bytes, over the ${maxMediaBytes}-byte media limit` }) : undefined;
403
+ };
404
+ const bodyOf = (state, request, media) => request.media.length === 0 ? Effect.succeed(request.body) : Effect.tryPromise({
405
+ try: async () => {
406
+ const encoded = await Promise.all(request.media.map(async (path) => {
407
+ const medium = media.get(path);
408
+ return { ...medium, base64: await mediaBase64(medium) };
409
+ }));
410
+ return withMedia(target, state, request.body, encoded);
411
+ },
412
+ catch: (error) => new DecisionModelError({ message: `cannot read media: ${error instanceof Error ? error.message : String(error)}` })
413
+ });
414
+ const send = Effect.fn("DecisionModel.send")(function* (source, state, request, media) {
415
+ const post = (body) => HttpClientRequest.post(target.url).pipe(HttpClientRequest.bodyText(body, "application/json"), client.execute, Effect.flatMap((response) => response.json), Effect.flatMap(wire.decode), Effect.catch((error) => Effect.flatMap(declinesAll(error), (all) => all ? Effect.succeed({ answers: new Map(request.questions.map(({ id }) => [id, "declined"])), inputTokens: undefined }) : Effect.flatMap(failureMessage(target.name, error), (message) => Effect.fail(new DecisionModelError({ message }))))));
416
+ const reply = yield* record(source, { requests: 1 }).pipe(Effect.andThen(bodyOf(state, request, media)), Effect.flatMap(post), permits.withPermit);
303
417
  yield* record(source, reply.inputTokens === undefined ? { unmetered: 1 } : { inputTokens: reply.inputTokens });
304
418
  const answers = [];
305
419
  for (const { id, value } of request.questions) {
@@ -312,9 +426,12 @@ var make = Effect.fnUntraced(function* (target, cacheDirectory) {
312
426
  return answers;
313
427
  });
314
428
  const ask = Effect.fn("DecisionModel.ask")(function* (source, state, questions) {
429
+ const media = yield* readMedia(questions);
430
+ const mediaOf = (question) => (question.media ?? []).map((path) => media.get(path));
431
+ const prints = questions.map((question) => mediaPrint(mediaOf(question)));
315
432
  const results = [];
316
433
  const misses = [];
317
- const cached = cache === undefined ? [] : yield* cache.get(state, questions);
434
+ const cached = cache === undefined ? [] : yield* cache.get(state, questions, prints);
318
435
  questions.forEach((_, index) => {
319
436
  const answer = cached[index];
320
437
  if (answer === undefined)
@@ -324,18 +441,30 @@ var make = Effect.fnUntraced(function* (target, cacheDirectory) {
324
441
  });
325
442
  if (misses.length < questions.length)
326
443
  yield* record(source, { cached: questions.length - misses.length });
327
- const { requests, oversized } = planRequests(state, misses.map((index) => questions[index]), target);
444
+ const sendable = [];
445
+ for (const index of misses) {
446
+ const shown = mediaOf(questions[index]);
447
+ const untaken = shown.find((medium) => !takes(medium.kind));
448
+ if (untaken !== undefined)
449
+ return yield* notTaken(untaken);
450
+ const tooMuch = mediaTooLarge(shown);
451
+ if (tooMuch === undefined)
452
+ sendable.push(index);
453
+ else
454
+ results[index] = Result.fail(tooMuch);
455
+ }
456
+ const { requests, oversized } = planRequests(state, sendable.map((index) => questions[index]), target);
328
457
  for (const { value, bytes } of oversized)
329
- results[misses[value]] = Result.fail(tooLarge(bytes));
330
- yield* Effect.forEach(requests, (request) => send(source, request).pipe(Effect.tap((received) => {
458
+ results[sendable[value]] = Result.fail(tooLarge(bytes));
459
+ yield* Effect.forEach(requests, (request) => send(source, state, request, media).pipe(Effect.tap((received) => {
331
460
  const answered = [];
332
461
  for (const { value, answer } of received) {
333
- const index = misses[value];
462
+ const index = sendable[value];
334
463
  if (answer === "declined") {
335
464
  results[index] = Result.fail(declined);
336
465
  } else {
337
466
  results[index] = Result.succeed(answer);
338
- answered.push({ question: questions[index], noul: answer });
467
+ answered.push({ question: questions[index], print: prints[index], noul: answer });
339
468
  }
340
469
  }
341
470
  return cache === undefined ? Effect.void : cache.set(state, answered);
@@ -350,9 +479,22 @@ var ItemFields = Schema2.Struct({
350
479
  id: Schema2.NonEmptyString,
351
480
  text: Schema2.NonEmptyString,
352
481
  path: Schema2.optional(Schema2.NonEmptyString),
353
- lines: Schema2.optional(Schema2.Tuple([Schema2.Int, Schema2.Int]).check(Schema2.makeFilter(([start, end]) => 1 <= start && start <= end || "lines must be [start, end] with 1 <= start <= end")))
482
+ lines: Schema2.optional(Schema2.Tuple([Schema2.Int, Schema2.Int]).check(Schema2.makeFilter(([start, end]) => 1 <= start && start <= end || "lines must be [start, end] with 1 <= start <= end"))),
483
+ media: Schema2.optional(Schema2.Array(Schema2.NonEmptyString))
354
484
  });
355
485
  var formatIssues = SchemaIssue.makeFormatterStandardSchemaV1();
486
+ var mediaProblem = (media) => {
487
+ const untyped = media.find((file) => mediaTypeOf(file) === undefined);
488
+ if (untyped !== undefined)
489
+ return `media ${untyped} is not a file type decision models take (${mediaExtensions.join(", ")})`;
490
+ for (const kind of mediaKinds) {
491
+ const count = media.filter((file) => mediaTypeOf(file).kind === kind).length;
492
+ if (count > maxMediaFiles[kind]) {
493
+ return `media lists ${count} ${mediaNoun[kind]}; an item shows the model at most ${maxMediaFiles[kind]}`;
494
+ }
495
+ }
496
+ return;
497
+ };
356
498
  var parseItem = Effect2.fn("parseItem")(function* (line) {
357
499
  const value = yield* Effect2.try({ try: () => JSON.parse(line), catch: () => "not valid JSON" });
358
500
  if (typeof value !== "object" || value === null || Array.isArray(value))
@@ -361,12 +503,16 @@ var parseItem = Effect2.fn("parseItem")(function* (line) {
361
503
  if (unknown.length > 0) {
362
504
  return yield* Effect2.fail(`unknown field ${unknown.join(", ")}; put what the model should read in text`);
363
505
  }
364
- return yield* Schema2.decodeUnknownEffect(ItemFields)(value).pipe(Effect2.mapError((error) => formatIssues(error.issue).issues.map(({ path, message }) => `${path?.length ? `${path.join(".")}: ` : ""}${message}`).join("; ")));
506
+ const item = yield* Schema2.decodeUnknownEffect(ItemFields)(value).pipe(Effect2.mapError((error) => formatIssues(error.issue).issues.map(({ path, message }) => `${path?.length ? `${path.join(".")}: ` : ""}${message}`).join("; ")));
507
+ const problem = item.media === undefined ? undefined : mediaProblem(item.media);
508
+ return problem === undefined ? item : yield* Effect2.fail(problem);
365
509
  });
366
510
  // src/lint.ts
367
511
  import { Array as Arr, Effect as Effect4, Result as Result2 } from "effect";
368
512
 
369
513
  // src/select.ts
514
+ import { stat } from "fs/promises";
515
+ import { resolve } from "path";
370
516
  import { Effect as Effect3, Runtime as Runtime2, Schema as Schema3 } from "effect";
371
517
  class SelectorError extends Schema3.TaggedError()("SelectorError", {
372
518
  message: Schema3.String
@@ -416,6 +562,15 @@ var start = async (select, cwd) => {
416
562
  running.delete(child.pid);
417
563
  }
418
564
  };
565
+ var findMedia = (item, cwd) => {
566
+ if (item.media === undefined)
567
+ return Effect3.succeed(item);
568
+ const media = cwd === undefined ? item.media : item.media.map((file) => resolve(cwd, file));
569
+ return Effect3.forEach(media, (file, index) => Effect3.tryPromise({
570
+ try: () => stat(file),
571
+ catch: () => `media ${item.media[index]}: no such file`
572
+ }).pipe(Effect3.filterOrFail((stats) => stats.isFile(), () => `media ${item.media[index]} is not a file`))).pipe(Effect3.as({ ...item, media }));
573
+ };
419
574
  var runSelector = Effect3.fn("runSelector")(function* (policy, cwd, runs) {
420
575
  const result = yield* Effect3.promise(() => {
421
576
  if (runs === undefined)
@@ -440,14 +595,18 @@ var runSelector = Effect3.fn("runSelector")(function* (policy, cwd, runs) {
440
595
  }
441
596
  const lines = result.stdout.split(`
442
597
  `);
443
- return yield* Effect3.forEach(lines.flatMap((line, index) => line.trim() === "" ? [] : [{ line, number: index + 1 }]), ({ line, number }) => parseItem(line).pipe(Effect3.mapError((reason) => new SelectorError({ message: `${policy.name}: selector output line ${number} is not a valid item: ${reason}` }))));
598
+ return yield* Effect3.forEach(lines.flatMap((line, index) => line.trim() === "" ? [] : [{ line, number: index + 1 }]), ({ line, number }) => parseItem(line).pipe(Effect3.flatMap((item) => findMedia(item, cwd)), Effect3.mapError((reason) => new SelectorError({ message: `${policy.name}: selector output line ${number} is not a valid item: ${reason}` }))));
444
599
  });
445
600
 
446
601
  // src/lint.ts
447
602
  var reviewFloor = 0.5;
448
603
  var defaultThreshold = 0.65;
449
604
  var verdictOf = (violates, floor, threshold) => violates <= floor ? "pass" : violates >= threshold ? "violation" : "review";
450
- var itemQuestion = (policy, item) => ({ instructions: item.text, criteria: policy.criteria });
605
+ var itemQuestion = (policy, item) => ({
606
+ instructions: item.text,
607
+ criteria: policy.criteria,
608
+ ...item.media === undefined || item.media.length === 0 ? {} : { media: item.media }
609
+ });
451
610
  var toFinding = (violates, base, policy, threshold) => Result2.match(violates, {
452
611
  onSuccess: (violates) => {
453
612
  const verdict = verdictOf(violates, reviewFloor, threshold);
@@ -1,7 +1,11 @@
1
1
  import { Cause, Context, Effect, Layer, Redacted, Result, Runtime, Schema } from "effect";
2
+ import { type MediaKind } from "./media.ts";
2
3
  /**
3
4
  * One yes/no question: `instructions` is what the decision model judges, and `criteria` say what a yes
4
- * (`true`) and a no (`false`) mean. The model answers with the probability of yes.
5
+ * (`true`) and a no (`false`) mean. The model answers with the probability of yes. `media` lists image,
6
+ * audio, and video files the model is shown with it, by path from the current directory (types by
7
+ * extension, `media.ts`); a request shows its media for every question in it, so only questions with the
8
+ * same media share one.
5
9
  */
6
10
  export interface Question {
7
11
  readonly instructions: string;
@@ -9,6 +13,7 @@ export interface Question {
9
13
  readonly true: string;
10
14
  readonly false: string;
11
15
  };
16
+ readonly media?: ReadonlyArray<string>;
12
17
  }
13
18
  declare const DecisionModelError_base: Schema.Class<DecisionModelError, Schema.TaggedStruct<"DecisionModelError", {
14
19
  readonly message: Schema.String;
@@ -44,13 +49,18 @@ export declare const cacheMaxAgeMs: number;
44
49
  * - `system-one`: the System One API (`POST /v1/systemone`) that Jev introduced and Clef, LiteLLM, LLM
45
50
  * Gateway, Ollama, llama.cpp, SGLang, and most decision-model servers speak. A request has a `state` and
46
51
  * `noul` questions keyed by id; each answer is the probability of yes. Workers AI wraps the response in
47
- * `result`, which is read the same way.
52
+ * `result`, which is read the same way. Media go before the state as data URLs, in `images`, `audio`,
53
+ * and `videos`, as Clef, clef-omni, and LiteLLM take them.
48
54
  * - `decisions`: OpenAI's Decisions API (`POST /v1/decisions`). A request has an `input` and a list of
49
- * `predicate` questions; answers come back as a list, each a probability or a refusal.
55
+ * `predicate` questions; answers come back as a list, each a probability or a refusal. With images, the
56
+ * input is one user message: an `input_image` part per image, then the state as `input_text`. It takes
57
+ * no audio or video.
50
58
  * - `gemini`: Google's Decisions API for Gemini Decision (`POST /v1beta/decisions` on
51
59
  * `generativelanguage.googleapis.com`). A request has an `input` and `noul` questions keyed by id, whose
52
60
  * criteria are `true_description` and `false_description`; the key goes in `x-goog-api-key`. A request
53
- * its safety filters block is answered with HTTP 400 instead, which declines every question in it.
61
+ * its safety filters block is answered with HTTP 400 instead, which declines every question in it. With
62
+ * media, the input is a list of content blocks: an `image`, `audio`, or `video` block per file, in base64,
63
+ * then the state as `text`.
54
64
  */
55
65
  export type Format = "system-one" | "decisions" | "gemini";
56
66
  /** A response, read: each question's probability of yes, or `declined`, by id, and the input tokens it reports. */
@@ -79,6 +89,11 @@ export interface Model {
79
89
  readonly cacheKey: string;
80
90
  /** USD per input token, when known. */
81
91
  readonly inputTokenPrice: number | undefined;
92
+ /**
93
+ * The kinds of media it takes, when known; otherwise any its format can carry is sent, and the endpoint
94
+ * says whether it takes them.
95
+ */
96
+ readonly media?: ReadonlyArray<MediaKind> | undefined;
82
97
  }
83
98
  /**
84
99
  * A decision model and how to reach it: the endpoint requests are POSTed to, and the API key or token, if
@@ -123,9 +138,14 @@ export declare const configuredModel: Effect.Effect<Model, DecisionModelError>;
123
138
  * `CLOUDFLARE_AUTH_TOKEN`, and optionally `CLOUDFLARE_API_BASE_URL`.
124
139
  */
125
140
  export declare const configuredTarget: Effect.Effect<Target, DecisionModelError>;
126
- /** One request: its exact body and what each question id answers. */
141
+ /**
142
+ * One request: its exact body, the media its questions show (by path), and what each question id answers.
143
+ * The body of a request with media leaves them out; they are read, and put where its format takes them,
144
+ * when it is sent.
145
+ */
127
146
  export interface Planned<A> {
128
147
  readonly body: string;
148
+ readonly media: ReadonlyArray<string>;
129
149
  readonly questions: ReadonlyArray<{
130
150
  readonly id: string;
131
151
  readonly value: A;
@@ -133,7 +153,9 @@ export interface Planned<A> {
133
153
  }
134
154
  /**
135
155
  * Plans the requests that ask `questions` under `state`: the state once per request and as many questions
136
- * as fit, laid out for `model` (default: Clef). Planned questions answer by index into `questions`.
156
+ * as fit, laid out for `model` (default: Clef). A request shows its media for every question in it, so
157
+ * questions share one only with questions showing the same media; groups are planned in order of their
158
+ * first question. Planned questions answer by index into `questions`.
137
159
  */
138
160
  export declare const planRequests: (state: string, questions: ReadonlyArray<Question>, model?: Pick<Model, "format" | "model">) => {
139
161
  requests: Planned<number>[];
@@ -4,10 +4,12 @@ declare const ItemFields: Schema.Struct<{
4
4
  readonly text: Schema.NonEmptyString;
5
5
  readonly path: Schema.optional<Schema.NonEmptyString>;
6
6
  readonly lines: Schema.optional<Schema.Tuple<readonly [Schema.Int, Schema.Int]>>;
7
+ readonly media: Schema.optional<Schema.$Array<Schema.NonEmptyString>>;
7
8
  }>;
8
9
  /**
9
10
  * One thing a selector picked out to judge. `text` is everything the decision model reads for it, context included; `id`
10
- * names it in findings; `path` and `lines` (1-based, inclusive) say where findings point.
11
+ * names it in findings; `path` and `lines` (1-based, inclusive) say where findings point. `media` lists the image, audio,
12
+ * and video files the model is shown with it, by path from where the selector ran.
11
13
  */
12
14
  export type Item = Schema.Schema.Type<typeof ItemFields>;
13
15
  /** Parses one JSONL line into an item; fails with a message saying what is wrong with it. */
@@ -16,5 +18,6 @@ export declare const parseItem: (line: string) => Effect.Effect<{
16
18
  readonly text: string;
17
19
  readonly path?: string | undefined;
18
20
  readonly lines?: readonly [number, number] | undefined;
21
+ readonly media?: readonly string[] | undefined;
19
22
  }, string, never>;
20
23
  export {};
@@ -29,6 +29,7 @@ export declare const select: (policy: Policy, cwd?: string | undefined, runs?: S
29
29
  readonly text: string;
30
30
  readonly path?: string | undefined;
31
31
  readonly lines?: readonly [number, number] | undefined;
32
+ readonly media?: readonly string[] | undefined;
32
33
  }[], import("./select.ts").SelectorError, never>;
33
34
  /**
34
35
  * Judge stage: asks the decision model whether each item violates `policy`, under the policy body. Results
@@ -40,6 +41,7 @@ export declare const judge: (policy: Policy, items: readonly {
40
41
  readonly text: string;
41
42
  readonly path?: string | undefined;
42
43
  readonly lines?: readonly [number, number] | undefined;
44
+ readonly media?: readonly string[] | undefined;
43
45
  }[]) => Effect.Effect<readonly Result.Result<number, Unanswered>[], import("./decision-model.ts").DecisionModelError, DecisionModel>;
44
46
  /**
45
47
  * Decide stage: one finding per item of `items`, given their `answers` (from `judge`, in the same order),
@@ -0,0 +1,30 @@
1
+ /** What a decision model can be shown besides text. */
2
+ export type MediaKind = "image" | "audio" | "video";
3
+ export declare const mediaKinds: ReadonlyArray<MediaKind>;
4
+ /** What a message calls several files of a kind. */
5
+ export declare const mediaNoun: Record<MediaKind, string>;
6
+ /** The extensions a media file may have, for messages. */
7
+ export declare const mediaExtensions: string[];
8
+ /** A media file's MIME type and kind, from its extension; `undefined` when it isn't one the models take. */
9
+ export declare const mediaTypeOf: (path: string) => {
10
+ readonly type: string;
11
+ readonly kind: MediaKind;
12
+ } | undefined;
13
+ /** Most files of each kind one item may show the model. */
14
+ export declare const maxMediaFiles: Record<MediaKind, number>;
15
+ /** Largest image an item may show the model. */
16
+ export declare const maxImageBytes: number;
17
+ /** Largest total of the media one item shows the model. */
18
+ export declare const maxMediaBytes: number;
19
+ /** A media file as read for a request: its type and kind, its size, and the SHA-256 of its contents. */
20
+ export interface Medium {
21
+ readonly path: string;
22
+ readonly type: string;
23
+ readonly kind: MediaKind;
24
+ readonly bytes: number;
25
+ readonly digest: string;
26
+ }
27
+ /** Reads the media file at `path` (which must have a media type) and fingerprints it; rejects if it can't. */
28
+ export declare const readMedium: (path: string) => Promise<Medium>;
29
+ /** A media file's contents in base64, read when its request is sent. */
30
+ export declare const mediaBase64: (medium: Medium) => Promise<string>;
@@ -69,6 +69,7 @@ export declare const itemsOf: (record: LabelRecord, items: ReadonlyArray<Item>)
69
69
  readonly text: string;
70
70
  readonly path?: string | undefined;
71
71
  readonly lines?: readonly [number, number] | undefined;
72
+ readonly media?: readonly string[] | undefined;
72
73
  }[];
73
74
  /**
74
75
  * A labeled unit with a policy's violation probability (`null`: too large to send). `group` is the
@@ -176,6 +177,7 @@ export declare const scorePolicy: (policy: Policy, labels: readonly {
176
177
  readonly text: string;
177
178
  readonly path?: string | undefined;
178
179
  readonly lines?: readonly [number, number] | undefined;
180
+ readonly media?: readonly string[] | undefined;
179
181
  };
180
182
  label: Label;
181
183
  group: string;
@@ -24,13 +24,14 @@ export declare const killRunningSelectors: () => void;
24
24
  /**
25
25
  * Runs `policy.select` with `sh -c` from `cwd`, with no arguments and an empty stdin: the selector finds
26
26
  * what it judges itself. Its stdout must be JSONL: one item per non-empty line. A non-zero exit, an invalid
27
- * line, or running longer than `selectorTimeoutMinutes` fails the run. With `runs`, a selector already run
28
- * with the same command and directory reuses that run's output.
27
+ * line (media that name no file included), or running longer than `selectorTimeoutMinutes` fails the run.
28
+ * With `runs`, a selector already run with the same command and directory reuses that run's output.
29
29
  */
30
30
  export declare const runSelector: (policy: Policy, cwd?: string | undefined, runs?: SelectorRuns | undefined) => Effect.Effect<{
31
31
  readonly id: string;
32
32
  readonly text: string;
33
33
  readonly path?: string | undefined;
34
34
  readonly lines?: readonly [number, number] | undefined;
35
+ readonly media?: readonly string[] | undefined;
35
36
  }[], SelectorError, never>;
36
37
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andrueandersoncs/lint-anything",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Check anything against plain-English policies, judged by the decision model of your choice: any System One, OpenAI Decisions, or Gemini Decisions endpoint, or Workers AI",
5
5
  "license": "MIT",
6
6
  "author": "Andrue Anderson",
@@ -5,7 +5,7 @@ description: Run lint-anything, act on its findings, and write or tune its polic
5
5
 
6
6
  # lint-anything
7
7
 
8
- lint-anything checks a codebase against rules written in English. Each rule is a **policy**: a Markdown file whose shell **selector** cuts out the items to judge (functions, comments, commits, anything), and whose body and **criteria** tell a decision model what a violation is: whichever model the project configures. The model returns a violation probability for each item; the CLI turns it into a verdict.
8
+ lint-anything checks a codebase against rules written in English. Each rule is a **policy**: a Markdown file whose shell **selector** cuts out the items to judge (functions, comments, commits, screenshots, recordings, anything), and whose body and **criteria** tell a decision model what a violation is: whichever model the project configures. The model returns a violation probability for each item; the CLI turns it into a verdict.
9
9
 
10
10
  It is a guide more than a gate: semantic rules are judged probabilistically. On the project's eval corpus, well-written policies catch about 69% of real violations outright, surface about 85% (counting `review`), and raise false alarms on about 13% of clean cases. Read findings critically; don't assume every `violation` is real or every `pass` is clean.
11
11
 
@@ -24,7 +24,8 @@ If `lint-anything` isn't found, install it with `bun add -d @andrueandersoncs/li
24
24
  - **The working directory matters.** Policies are read from `./.lint-anything/rules`, and selectors run in the current directory. Run from a subdirectory and you get `error: cannot read policies in .lint-anything/rules`: there is no upward search.
25
25
  - **The decision model** is the project's choice, set in the environment or in `.env` / `.env.local` in the current directory. Don't change it to make findings go away. With `LINT_ANYTHING_ENDPOINT` (a full URL), the model named by `LINT_ANYTHING_MODEL` is asked there, and `LINT_ANYTHING_API_KEY` is its key if it needs one. An endpoint on `generativelanguage.googleapis.com` speaks Google's Decisions API (`gemini-decision-1`, keyed by a Gemini API key); any other whose path ends in `/decisions` speaks OpenAI's; any other speaks System One (`/v1/systemone`: Jev, LiteLLM, LLM Gateway, and most decision-model servers).
26
26
  - **Without an endpoint**, a Cloudflare model on Workers AI is asked: `clef`, or another that `LINT_ANYTHING_MODEL` names (`clef-flash`). `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_AUTH_TOKEN` are then required on every run, even when every answer would come from the cache. `CLOUDFLARE_API_BASE_URL` is optional.
27
- - **Accuracy numbers in this skill were measured on Clef.** A project on another model should score it on its labels before trusting it.
27
+ - **What the model can see** besides text depends on it: Gemini Decision 1 and `clef-omni` take images, audio, and video; OpenAI Decisions, `clef`, and `clef-flash` take images only; another System One server takes what it takes. An item whose media the model can't take stops the run before anything is sent (`Clef takes text and images, not audio clips: …`).
28
+ - **Accuracy numbers in this skill were measured on Clef**, on text. A project on another model, or with media, should score its policies on its labels before trusting them.
28
29
  - **Add `.lint-anything/cache` to `.gitignore`.** The answer cache (`answers.sqlite` plus `-wal`/`-shm` files) is written there.
29
30
 
30
31
  ### Output
@@ -50,7 +51,7 @@ Columns: verdict, violation probability (`-` when none), location, policy name,
50
51
  | `pass` | probability ≤ 0.5 | 0 |
51
52
  | `review` | between 0.5 and 0.65: uncertain, often a case reasonable reviewers would disagree on | 0 |
52
53
  | `violation` | probability ≥ 0.65 | **1** |
53
- | `inconclusive` | the item wasn't judged: too large to send (`request is N bytes; Clef would truncate it`, or `…, over the 64000-byte request limit` on another model), or refused by the model (`<model> declined to answer`: an OpenAI Decisions refusal, or a request Gemini's safety filters blocked) | 0 |
54
+ | `inconclusive` | the item wasn't judged: too large to send (`request is N bytes; Clef would truncate it`, or `…, over the 64000-byte request limit` on another model; `image <path> is N bytes, over the 4194304-byte image limit`; `media are N bytes, over the 8388608-byte media limit`), or refused by the model (`<model> declined to answer`: an OpenAI Decisions refusal, or a request Gemini's safety filters blocked) | 0 |
54
55
 
55
56
  | Exit code | Meaning |
56
57
  | --- | --- |
@@ -149,8 +150,20 @@ The selector is a shell command that finds what to judge and prints **JSONL**, o
149
150
  | `text` | yes | non-empty string: **everything the model reads for this item**, including its context |
150
151
  | `path` | no | file path, for the location |
151
152
  | `lines` | no | `[start, end]`, integers, **1-based**, inclusive, `1 ≤ start ≤ end` |
153
+ | `media` | no | array of file paths, from the project root: images, audio, and video the model is shown with the text |
152
154
 
153
- Any other field, a non-JSON line, or a wrong type stops the whole run with `selector output line N is not a valid item: …`. Blank lines are skipped.
155
+ Any other field, a non-JSON line, or a wrong type stops the whole run with `selector output line N is not a valid item: …`. Blank lines are skipped. So does a `media` file that doesn't exist, has a type the models don't take, or is one too many of its kind.
156
+
157
+ **Media items.** `media` files are typed by extension: `.png`, `.jpg`, `.jpeg`, `.webp`; `.wav`, `.mp3`, `.flac`, `.ogg`; `.mp4`, `.webm`. An item shows at most 4 images, 4 audio clips, and 2 videos, at most 4 MiB per image and 8 MiB in all (larger is `inconclusive`). Keep `text` short and say what the media is: it is the question asked about them.
158
+
159
+ ```yaml
160
+ select: |
161
+ for f in docs/screenshots/*.png; do
162
+ jq -cn --arg f "$f" '{id: $f, path: $f, text: "screenshot \($f)", media: [$f]}'
163
+ done
164
+ ```
165
+
166
+ Items share a request only with items showing the same files, so a policy over 200 screenshots sends 200 requests on its first run. Several questions about one file (a heading rule and a button rule, as separate items with the same `media`) do share one.
154
167
 
155
168
  How a selector runs:
156
169
 
@@ -256,7 +269,7 @@ The lint-anything repository has worked examples: 21 policies in `evals/corpus/p
256
269
 
257
270
  ## Cache and cost
258
271
 
259
- - Each answer is cached per question, keyed by model (and endpoint), policy body, item text, and criteria. Switching models never reuses another model's answers. Re-running with nothing changed sends no requests, and an edit re-asks only the items whose text changed. Editing a policy's body or criteria re-asks all of that policy's items.
272
+ - Each answer is cached per question, keyed by model (and endpoint), policy body, item text, criteria, and the contents of the item's media. Switching models never reuses another model's answers. Re-running with nothing changed sends no requests, and an edit re-asks only the items whose text or media changed. Editing a policy's body or criteria re-asks all of that policy's items.
260
273
  - Answers expire after 30 days, so everything is asked again about once a month, and verdicts can shift slightly if the model has been updated.
261
274
  - A machine without the cache (a fresh CI runner) pays for every item on every run, unless CI keeps `.lint-anything/cache` between runs.
262
275
  - Cost depends on the model. `--debug` prices only Clef ($0.24 per million input tokens; output is free); on another model its cost column is `-`, and the project's provider bills it. On the evals, with Clef, rule-specific selectors cost about $14–16 per 1,000 files across 17 rules. Nothing caps spending: a selector that emits every line of a large repository sends all of them, so check the item count by hand first.