@andrueandersoncs/lint-anything 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/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,9 @@ 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}`;
65
+ var bearer = (key) => ["authorization", `Bearer ${key}`];
66
+ var never = () => false;
35
67
  var Probability = Schema.Number.check(Schema.isBetween({ minimum: 0, maximum: 1 }));
36
68
  var ReportedUsage = Schema.optional(Schema.Struct({ input_tokens: Schema.Number }));
37
69
  var noul = (question) => ({ type: "noul", instructions: question.instructions, criteria: question.criteria });
@@ -39,14 +71,26 @@ var SystemOneResponse = Schema.Struct({
39
71
  answers: Schema.Record(Schema.String, Schema.Struct({ noul: Probability })),
40
72
  usage: ReportedUsage
41
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
+ };
42
83
  var systemOne = {
43
- 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,
44
86
  tail: "}}",
45
87
  entry: (id, question) => `${JSON.stringify(id)}:${JSON.stringify(noul(question))}`,
46
88
  decode: (body) => Schema.decodeUnknownEffect(SystemOneResponse)(typeof body === "object" && body !== null && "result" in body ? body.result : body).pipe(Effect.map(({ answers, usage }) => ({
47
89
  answers: new Map(Object.entries(answers).map(([id, answer]) => [id, answer.noul])),
48
90
  inputTokens: usage?.input_tokens
49
- })))
91
+ }))),
92
+ keyHeader: bearer,
93
+ declinesAll: never
50
94
  };
51
95
  var predicate = (id, question) => ({
52
96
  type: "predicate",
@@ -63,17 +107,62 @@ var DecisionsResponse = Schema.Struct({
63
107
  ])),
64
108
  usage: ReportedUsage
65
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
+ }];
66
117
  var decisions = {
67
- 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"],
68
120
  tail: "]}",
69
121
  entry: (id, question) => JSON.stringify(predicate(id, question)),
70
122
  decode: (body) => Schema.decodeUnknownEffect(DecisionsResponse)(body).pipe(Effect.map(({ answers, usage }) => ({
71
123
  answers: new Map(answers.map((answer) => [answer.name, answer.type === "refusal" ? "declined" : answer.probability])),
72
124
  inputTokens: usage?.input_tokens
73
- })))
125
+ }))),
126
+ keyHeader: bearer,
127
+ declinesAll: never
74
128
  };
75
- var wires = { "system-one": systemOne, decisions };
129
+ var geminiNoul = (question) => ({
130
+ type: "noul",
131
+ instructions: question.instructions,
132
+ criteria: { true_description: question.criteria.true, false_description: question.criteria.false }
133
+ });
134
+ var GeminiResponse = Schema.Struct({
135
+ answers: Schema.Record(Schema.String, Schema.Struct({ noul: Probability })),
136
+ usage: Schema.optional(Schema.Struct({ total_input_tokens: Schema.Number }))
137
+ });
138
+ var GeminiBlocked = Schema.fromJsonString(Schema.Struct({
139
+ error: Schema.Struct({ code: Schema.Literals(["safety", "prohibited_content", "blocklist", "image_safety", "content_blocked"]) })
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
+ ];
145
+ var gemini = {
146
+ head: (model, state, media) => `${JSON.stringify({ model, input: geminiInput(state, media) }).slice(0, -1)},"questions":{`,
147
+ takes: mediaKinds,
148
+ tail: "}}",
149
+ entry: (id, question) => `${JSON.stringify(id)}:${JSON.stringify(geminiNoul(question))}`,
150
+ decode: (body) => Schema.decodeUnknownEffect(GeminiResponse)(body).pipe(Effect.map(({ answers, usage }) => ({
151
+ answers: new Map(Object.entries(answers).map(([id, answer]) => [id, answer.noul])),
152
+ inputTokens: usage?.total_input_tokens
153
+ }))),
154
+ keyHeader: (key) => ["x-goog-api-key", key],
155
+ declinesAll: (status, body) => status === 400 && Option.isSome(Schema.decodeUnknownOption(GeminiBlocked)(body))
156
+ };
157
+ var wires = { "system-one": systemOne, decisions, gemini };
76
158
  var decodeReply = (format, body) => wires[format].decode(body);
159
+ var keyHeader = (format, key) => wires[format].keyHeader(key);
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
+ };
77
166
  var clef = workersAiModel("clef");
78
167
  function workersAiModel(model) {
79
168
  return {
@@ -81,7 +170,8 @@ function workersAiModel(model) {
81
170
  model,
82
171
  name: model === "clef" ? "Clef" : model,
83
172
  cacheKey: model,
84
- inputTokenPrice: model === "clef" ? clefInputTokenPrice : undefined
173
+ inputTokenPrice: model === "clef" ? clefInputTokenPrice : undefined,
174
+ media: workersAiMedia[model]
85
175
  };
86
176
  }
87
177
  var workersAiTarget = (options) => {
@@ -92,13 +182,20 @@ var workersAiTarget = (options) => {
92
182
  token: options.token
93
183
  };
94
184
  };
95
- var formatOf = (url) => new URL(url).pathname.replace(/\/+$/, "").endsWith("/decisions") ? "decisions" : "system-one";
185
+ var geminiHost = "generativelanguage.googleapis.com";
186
+ var formatOf = (url) => {
187
+ const { hostname, pathname } = new URL(url);
188
+ if (hostname === geminiHost)
189
+ return "gemini";
190
+ return pathname.replace(/\/+$/, "").endsWith("/decisions") ? "decisions" : "system-one";
191
+ };
96
192
  var endpointModel = (url, model) => ({
97
193
  format: formatOf(url),
98
194
  model,
99
195
  name: model,
100
196
  cacheKey: `${url} ${model}`,
101
- inputTokenPrice: undefined
197
+ inputTokenPrice: undefined,
198
+ media: undefined
102
199
  });
103
200
  var endpointTarget = (options) => ({ ...endpointModel(options.url, options.model), url: options.url, token: options.token });
104
201
  var configError = (message) => new DecisionModelError({ message });
@@ -137,9 +234,9 @@ var configuredTarget = Effect.gen(function* () {
137
234
  const baseUrl = yield* Config.String("CLOUDFLARE_API_BASE_URL").pipe(Config.withDefault(defaultCloudflareBaseUrl), Effect.mapError((error) => configError(`CLOUDFLARE_API_BASE_URL: ${error.message}`)));
138
235
  return workersAiTarget({ accountId, token, model: chosen.model, baseUrl });
139
236
  });
140
- var pack = (model, state, values, question) => {
237
+ var pack = (model, state, media, values, question) => {
141
238
  const wire = wires[model.format];
142
- const head = wire.head(model.model, state);
239
+ const head = wire.head(model.model, state, []);
143
240
  const fixedBytes = Buffer.byteLength(head) + Buffer.byteLength(wire.tail);
144
241
  const entry = (value, index) => {
145
242
  const id = `i${index}`;
@@ -153,7 +250,7 @@ var pack = (model, state, values, question) => {
153
250
  let bytes = fixedBytes;
154
251
  const flush = () => {
155
252
  if (questions.length > 0)
156
- requests.push({ body: `${head}${entries.join(",")}${wire.tail}`, questions });
253
+ requests.push({ body: `${head}${entries.join(",")}${wire.tail}`, media, questions });
157
254
  entries = [];
158
255
  questions = [];
159
256
  bytes = fixedBytes;
@@ -176,7 +273,31 @@ var pack = (model, state, values, question) => {
176
273
  flush();
177
274
  return { requests, oversized };
178
275
  };
179
- 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");
180
301
  var openAnswerCache = (directory, now, model) => {
181
302
  mkdirSync(directory, { recursive: true });
182
303
  const db = new Database(join(directory, cacheFile));
@@ -191,15 +312,15 @@ var openAnswerCache = (directory, now, model) => {
191
312
  db.run("DELETE FROM answers WHERE answered <= ?", [now - cacheMaxAgeMs]);
192
313
  const select = db.query("SELECT noul FROM answers WHERE key = ?");
193
314
  const upsert = db.query("INSERT OR REPLACE INTO answers (key, noul, answered) VALUES (?, ?, ?)");
194
- 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();
195
316
  const insert = db.transaction((state, answers) => {
196
317
  const answered = Date.now();
197
- for (const { question, noul } of answers)
198
- upsert.run(key(state, question), noul, answered);
318
+ for (const { question, print, noul } of answers)
319
+ upsert.run(key(state, question, print), noul, answered);
199
320
  });
200
321
  const failed = (error) => new DecisionModelError({ message: `answer cache ${join(directory, cacheFile)}: ${error instanceof Error ? error.message : String(error)}` });
201
322
  return {
202
- 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 }),
203
324
  set: (state, answers) => Effect.try({ try: () => insert(state, answers), catch: failed }),
204
325
  close: () => db.close()
205
326
  };
@@ -239,7 +360,8 @@ class DecisionModel extends Context.Service()("lint-anything/DecisionModel") {
239
360
  }
240
361
  var make = Effect.fnUntraced(function* (target, cacheDirectory) {
241
362
  const wire = wires[target.format];
242
- const client = (yield* HttpClient.HttpClient).pipe(HttpClient.mapRequest(flow(target.token === undefined ? (request) => request : HttpClientRequest.bearerToken(target.token), HttpClientRequest.acceptJson)), HttpClient.filterStatusOk, HttpClient.transformResponse(flow(Effect.tap((response) => response.text), Effect.timeout(attemptTimeout))), HttpClient.retryTransient({ schedule: Schedule.exponential(500), times: attemptRetries }));
363
+ const { token } = target;
364
+ const client = (yield* HttpClient.HttpClient).pipe(HttpClient.mapRequest(flow(token === undefined ? (request) => request : HttpClientRequest.setHeader(...wire.keyHeader(Redacted.value(token))), HttpClientRequest.acceptJson)), HttpClient.filterStatusOk, HttpClient.transformResponse(flow(Effect.tap((response) => response.text), Effect.timeout(attemptTimeout))), HttpClient.retryTransient({ schedule: Schedule.exponential(500), times: attemptRetries }));
243
365
  const cache = cacheDirectory === undefined ? undefined : yield* Effect.acquireRelease(Effect.try({
244
366
  try: () => openAnswerCache(cacheDirectory, Date.now(), target),
245
367
  catch: (error) => new DecisionModelError({
@@ -254,8 +376,44 @@ var make = Effect.fnUntraced(function* (target, cacheDirectory) {
254
376
  message: target.name === "Clef" ? `request is ${bytes} bytes; Clef would truncate it` : `request is ${bytes} bytes, over the ${maxRequestBytes}-byte request limit`
255
377
  });
256
378
  const declined = new Declined({ message: `${target.name} declined to answer` });
257
- const send = Effect.fn("DecisionModel.send")(function* (source, request) {
258
- 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(failureMessage(target.name, error), (message) => Effect.fail(new DecisionModelError({ message })))));
379
+ const declinesAll = (error) => {
380
+ if (!HttpClientError.isHttpClientError(error) || error.reason._tag !== "StatusCodeError")
381
+ return Effect.succeed(false);
382
+ const { response } = error.reason;
383
+ return response.text.pipe(Effect.timeout("5 seconds"), Effect.map((text) => wire.declinesAll(response.status, text)), Effect.orElseSucceed(() => false));
384
+ };
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);
259
417
  yield* record(source, reply.inputTokens === undefined ? { unmetered: 1 } : { inputTokens: reply.inputTokens });
260
418
  const answers = [];
261
419
  for (const { id, value } of request.questions) {
@@ -268,9 +426,12 @@ var make = Effect.fnUntraced(function* (target, cacheDirectory) {
268
426
  return answers;
269
427
  });
270
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)));
271
432
  const results = [];
272
433
  const misses = [];
273
- const cached = cache === undefined ? [] : yield* cache.get(state, questions);
434
+ const cached = cache === undefined ? [] : yield* cache.get(state, questions, prints);
274
435
  questions.forEach((_, index) => {
275
436
  const answer = cached[index];
276
437
  if (answer === undefined)
@@ -280,18 +441,30 @@ var make = Effect.fnUntraced(function* (target, cacheDirectory) {
280
441
  });
281
442
  if (misses.length < questions.length)
282
443
  yield* record(source, { cached: questions.length - misses.length });
283
- 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);
284
457
  for (const { value, bytes } of oversized)
285
- results[misses[value]] = Result.fail(tooLarge(bytes));
286
- 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) => {
287
460
  const answered = [];
288
461
  for (const { value, answer } of received) {
289
- const index = misses[value];
462
+ const index = sendable[value];
290
463
  if (answer === "declined") {
291
464
  results[index] = Result.fail(declined);
292
465
  } else {
293
466
  results[index] = Result.succeed(answer);
294
- answered.push({ question: questions[index], noul: answer });
467
+ answered.push({ question: questions[index], print: prints[index], noul: answer });
295
468
  }
296
469
  }
297
470
  return cache === undefined ? Effect.void : cache.set(state, answered);
@@ -306,9 +479,22 @@ var ItemFields = Schema2.Struct({
306
479
  id: Schema2.NonEmptyString,
307
480
  text: Schema2.NonEmptyString,
308
481
  path: Schema2.optional(Schema2.NonEmptyString),
309
- 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))
310
484
  });
311
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
+ };
312
498
  var parseItem = Effect2.fn("parseItem")(function* (line) {
313
499
  const value = yield* Effect2.try({ try: () => JSON.parse(line), catch: () => "not valid JSON" });
314
500
  if (typeof value !== "object" || value === null || Array.isArray(value))
@@ -317,12 +503,16 @@ var parseItem = Effect2.fn("parseItem")(function* (line) {
317
503
  if (unknown.length > 0) {
318
504
  return yield* Effect2.fail(`unknown field ${unknown.join(", ")}; put what the model should read in text`);
319
505
  }
320
- 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);
321
509
  });
322
510
  // src/lint.ts
323
511
  import { Array as Arr, Effect as Effect4, Result as Result2 } from "effect";
324
512
 
325
513
  // src/select.ts
514
+ import { stat } from "fs/promises";
515
+ import { resolve } from "path";
326
516
  import { Effect as Effect3, Runtime as Runtime2, Schema as Schema3 } from "effect";
327
517
  class SelectorError extends Schema3.TaggedError()("SelectorError", {
328
518
  message: Schema3.String
@@ -372,6 +562,15 @@ var start = async (select, cwd) => {
372
562
  running.delete(child.pid);
373
563
  }
374
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
+ };
375
574
  var runSelector = Effect3.fn("runSelector")(function* (policy, cwd, runs) {
376
575
  const result = yield* Effect3.promise(() => {
377
576
  if (runs === undefined)
@@ -396,14 +595,18 @@ var runSelector = Effect3.fn("runSelector")(function* (policy, cwd, runs) {
396
595
  }
397
596
  const lines = result.stdout.split(`
398
597
  `);
399
- 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}` }))));
400
599
  });
401
600
 
402
601
  // src/lint.ts
403
602
  var reviewFloor = 0.5;
404
603
  var defaultThreshold = 0.65;
405
604
  var verdictOf = (violates, floor, threshold) => violates <= floor ? "pass" : violates >= threshold ? "violation" : "review";
406
- 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
+ });
407
610
  var toFinding = (violates, base, policy, threshold) => Result2.match(violates, {
408
611
  onSuccess: (violates) => {
409
612
  const verdict = verdictOf(violates, reviewFloor, threshold);
@@ -745,6 +948,7 @@ export {
745
948
  configuredModel,
746
949
  configuredTarget,
747
950
  decide,
951
+ declinesAll,
748
952
  decodeReply,
749
953
  defaultThreshold,
750
954
  endpointTarget,
@@ -753,6 +957,7 @@ export {
753
957
  itemQuestion,
754
958
  itemsOf,
755
959
  judge,
960
+ keyHeader,
756
961
  killRunningSelectors,
757
962
  lint,
758
963
  loadLabels,
@@ -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;
@@ -26,7 +31,7 @@ export declare class RequestTooLarge extends RequestTooLarge_base {
26
31
  declare const Declined_base: Schema.Class<Declined, Schema.TaggedStruct<"Declined", {
27
32
  readonly message: Schema.String;
28
33
  }>, Cause.YieldableError>;
29
- /** The model declined to answer the question (an OpenAI Decisions `refusal`). */
34
+ /** The model declined to answer the question (an OpenAI Decisions `refusal`, or a request Gemini's safety filters blocked). */
30
35
  export declare class Declined extends Declined_base {
31
36
  }
32
37
  /** Why a question has no answer. */
@@ -44,11 +49,20 @@ 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.
58
+ * - `gemini`: Google's Decisions API for Gemini Decision (`POST /v1beta/decisions` on
59
+ * `generativelanguage.googleapis.com`). A request has an `input` and `noul` questions keyed by id, whose
60
+ * criteria are `true_description` and `false_description`; the key goes in `x-goog-api-key`. A request
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`.
50
64
  */
51
- export type Format = "system-one" | "decisions";
65
+ export type Format = "system-one" | "decisions" | "gemini";
52
66
  /** A response, read: each question's probability of yes, or `declined`, by id, and the input tokens it reports. */
53
67
  interface Reply {
54
68
  readonly answers: ReadonlyMap<string, number | "declined">;
@@ -56,6 +70,13 @@ interface Reply {
56
70
  }
57
71
  /** Reads a decision endpoint's response body in `format`. */
58
72
  export declare const decodeReply: (format: Format, body: unknown) => Effect.Effect<Reply, Schema.SchemaError, never>;
73
+ /** The header that carries `key` to an endpoint in `format`: a bearer token, or Gemini's `x-goog-api-key`. */
74
+ export declare const keyHeader: (format: Format, key: string) => readonly [name: string, value: string];
75
+ /**
76
+ * Whether an endpoint in `format` declined every question of a request by answering with this error status
77
+ * and body (a request Gemini's safety filters blocked).
78
+ */
79
+ export declare const declinesAll: (format: Format, status: number, body: string) => boolean;
59
80
  /** A decision model: which one to ask, the format its endpoint speaks, and what is known about it. */
60
81
  export interface Model {
61
82
  /** The wire format of its endpoint. */
@@ -68,8 +89,16 @@ export interface Model {
68
89
  readonly cacheKey: string;
69
90
  /** USD per input token, when known. */
70
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;
71
97
  }
72
- /** A decision model and how to reach it: the endpoint requests are POSTed to, and the bearer token, if any. */
98
+ /**
99
+ * A decision model and how to reach it: the endpoint requests are POSTed to, and the API key or token, if
100
+ * any (a bearer token, or for Gemini `x-goog-api-key`).
101
+ */
73
102
  export interface Target extends Model {
74
103
  readonly url: string;
75
104
  readonly token: Redacted.Redacted<string> | undefined;
@@ -84,8 +113,10 @@ export declare const workersAiTarget: (options: {
84
113
  readonly baseUrl?: string;
85
114
  }) => Target;
86
115
  /**
87
- * A decision model behind any endpoint: OpenAI Decisions when the URL's path ends in `/decisions` (such as
88
- * `https://api.openai.com/v1/decisions`), System One otherwise (such as `http://localhost:4000/v1/systemone`).
116
+ * A decision model behind any endpoint: Gemini Decisions on `generativelanguage.googleapis.com` (such as
117
+ * `https://generativelanguage.googleapis.com/v1beta/decisions`), OpenAI Decisions when the URL's path ends
118
+ * in `/decisions` (such as `https://api.openai.com/v1/decisions`), System One otherwise (such as
119
+ * `http://localhost:4000/v1/systemone`).
89
120
  */
90
121
  export declare const endpointTarget: (options: {
91
122
  readonly url: string;
@@ -100,15 +131,21 @@ export declare const configuredModel: Effect.Effect<Model, DecisionModelError>;
100
131
  /**
101
132
  * The decision model the environment configures, and how to reach it.
102
133
  *
103
- * - With `LINT_ANYTHING_ENDPOINT`: that endpoint (System One, or OpenAI Decisions when its path ends in
104
- * `/decisions`), asking `LINT_ANYTHING_MODEL`, with `LINT_ANYTHING_API_KEY` as the bearer token if set.
134
+ * - With `LINT_ANYTHING_ENDPOINT`: that endpoint (Gemini Decisions on `generativelanguage.googleapis.com`,
135
+ * OpenAI Decisions when its path ends in `/decisions`, System One otherwise), asking `LINT_ANYTHING_MODEL`,
136
+ * with `LINT_ANYTHING_API_KEY` as the key if set (a bearer token, or for Gemini `x-goog-api-key`).
105
137
  * - Without it: `LINT_ANYTHING_MODEL` (default `clef`) on Workers AI, with `CLOUDFLARE_ACCOUNT_ID`,
106
138
  * `CLOUDFLARE_AUTH_TOKEN`, and optionally `CLOUDFLARE_API_BASE_URL`.
107
139
  */
108
140
  export declare const configuredTarget: Effect.Effect<Target, DecisionModelError>;
109
- /** 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
+ */
110
146
  export interface Planned<A> {
111
147
  readonly body: string;
148
+ readonly media: ReadonlyArray<string>;
112
149
  readonly questions: ReadonlyArray<{
113
150
  readonly id: string;
114
151
  readonly value: A;
@@ -116,7 +153,9 @@ export interface Planned<A> {
116
153
  }
117
154
  /**
118
155
  * Plans the requests that ask `questions` under `state`: the state once per request and as many questions
119
- * 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`.
120
159
  */
121
160
  export declare const planRequests: (state: string, questions: ReadonlyArray<Question>, model?: Pick<Model, "format" | "model">) => {
122
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 {};
@@ -10,7 +10,7 @@
10
10
  * and retried. `DecisionModel.layer(cacheDirectory?)` asks the model the environment configures
11
11
  * (`configuredTarget`): Clef on Workers AI with `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_AUTH_TOKEN` (and
12
12
  * optionally `CLOUDFLARE_API_BASE_URL`) by default, or `LINT_ANYTHING_MODEL` at `LINT_ANYTHING_ENDPOINT`, any
13
- * System One or OpenAI Decisions endpoint, with `LINT_ANYTHING_API_KEY`. `DecisionModel.layerFor(target)`
13
+ * System One, OpenAI Decisions, or Gemini Decisions endpoint, with `LINT_ANYTHING_API_KEY`. `DecisionModel.layerFor(target)`
14
14
  * takes the model from code instead (`workersAiTarget`, `endpointTarget`). `loadPolicies` needs `FileSystem`
15
15
  * and `Path`, for example from `BunServices.layer` in `@effect/platform-bun`.
16
16
  *
@@ -18,7 +18,7 @@
18
18
  * with the metrics and bootstrap intervals the evals use; the skill's score script is built on it.
19
19
  */
20
20
  import { DecisionModel, DecisionModelError } from "./decision-model.ts";
21
- export { cacheMaxAgeMs, clef, clefInputTokenPrice, configuredModel, configuredTarget, Declined, decodeReply, DecisionModel, DecisionModelError, endpointTarget, type Format, type Model, noUsage, planRequests, type Planned, type Question, RequestTooLarge, sumUsage, type Target, type Unanswered, type Usage, workersAiTarget } from "./decision-model.ts";
21
+ export { cacheMaxAgeMs, clef, clefInputTokenPrice, configuredModel, configuredTarget, Declined, declinesAll, decodeReply, DecisionModel, DecisionModelError, endpointTarget, type Format, keyHeader, type Model, noUsage, planRequests, type Planned, type Question, RequestTooLarge, sumUsage, type Target, type Unanswered, type Usage, workersAiTarget } from "./decision-model.ts";
22
22
  /** @deprecated Renamed `DecisionModel`: it asks whichever decision model is configured, Clef by default. */
23
23
  export declare const Clef: typeof DecisionModel;
24
24
  /** @deprecated Renamed `DecisionModel`. */
@@ -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),