@opencode/ai 2.1.3 → 2.2.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.
Files changed (288) hide show
  1. package/README.md +283 -114
  2. package/dist/ai-client.d.ts +2 -2
  3. package/dist/ai-client.js +2 -1
  4. package/dist/cache-policy.d.ts +4 -0
  5. package/dist/cache-policy.js +20 -6
  6. package/dist/decision-client.d.ts +21 -0
  7. package/dist/decision-client.js +55 -0
  8. package/dist/decision.d.ts +472 -0
  9. package/dist/decision.js +158 -0
  10. package/dist/effect.d.ts +38 -0
  11. package/dist/effect.js +27 -0
  12. package/dist/effort-updates.d.ts +2 -2
  13. package/dist/experimental/cloudflare-run-extraction.d.ts +13 -0
  14. package/dist/experimental/cloudflare-run-extraction.js +83 -0
  15. package/dist/experimental/extraction.d.ts +183 -0
  16. package/dist/experimental/extraction.js +102 -0
  17. package/dist/experimental/realtime.d.ts +7 -2
  18. package/dist/experimental/realtime.js +5 -5
  19. package/dist/experimental.d.ts +2 -3
  20. package/dist/experimental.js +1 -2
  21. package/dist/image-client.d.ts +13 -22
  22. package/dist/image-client.js +0 -1
  23. package/dist/image.d.ts +154 -324
  24. package/dist/image.js +4 -11
  25. package/dist/index.d.ts +1426 -34
  26. package/dist/index.js +127 -24
  27. package/dist/llm.d.ts +16 -1
  28. package/dist/llm.js +34 -0
  29. package/dist/media-client.d.ts +2 -1
  30. package/dist/media-model.d.ts +3 -0
  31. package/dist/media-model.js +4 -1
  32. package/dist/media.d.ts +0 -7
  33. package/dist/media.js +0 -6
  34. package/dist/protocols/alibaba-chat.d.ts +32 -12
  35. package/dist/protocols/alibaba-images.d.ts +22 -0
  36. package/dist/protocols/alibaba-images.js +77 -0
  37. package/dist/protocols/alibaba-messages.d.ts +62 -27
  38. package/dist/protocols/alibaba-responses.d.ts +41 -38
  39. package/dist/protocols/anthropic-messages.d.ts +196 -94
  40. package/dist/protocols/anthropic-messages.js +47 -16
  41. package/dist/protocols/bedrock-converse.d.ts +140 -47
  42. package/dist/protocols/bedrock-converse.js +114 -18
  43. package/dist/protocols/bedrock-event-stream.js +1 -0
  44. package/dist/protocols/bfl-images.d.ts +9 -0
  45. package/dist/protocols/bfl-images.js +17 -15
  46. package/dist/protocols/cartesia-speech.d.ts +32 -50
  47. package/dist/protocols/cloudflare-run-decisions.d.ts +9 -0
  48. package/dist/protocols/cloudflare-run-decisions.js +36 -0
  49. package/dist/protocols/cloudflare-workers-ai-images.d.ts +23 -0
  50. package/dist/protocols/cloudflare-workers-ai-images.js +80 -0
  51. package/dist/protocols/cohere-chat.d.ts +7 -8
  52. package/dist/protocols/deepgram-speech.d.ts +32 -50
  53. package/dist/protocols/elevenlabs-speech.d.ts +32 -50
  54. package/dist/protocols/fal-images.d.ts +10 -1
  55. package/dist/protocols/fal-images.js +16 -25
  56. package/dist/protocols/fal-video.js +1 -1
  57. package/dist/protocols/gemini.d.ts +37 -34
  58. package/dist/protocols/gemini.js +13 -8
  59. package/dist/protocols/google-images.d.ts +2 -2
  60. package/dist/protocols/google-images.js +8 -37
  61. package/dist/protocols/google-interactions.d.ts +18 -14
  62. package/dist/protocols/google-interactions.js +14 -7
  63. package/dist/protocols/google-speech.d.ts +32 -50
  64. package/dist/protocols/google-speech.js +0 -1
  65. package/dist/protocols/google-transcription.d.ts +32 -50
  66. package/dist/protocols/google-transcription.js +0 -1
  67. package/dist/protocols/google-video.d.ts +1 -1
  68. package/dist/protocols/google-video.js +2 -7
  69. package/dist/protocols/index.d.ts +3 -0
  70. package/dist/protocols/index.js +3 -0
  71. package/dist/protocols/meta-images.d.ts +7 -1
  72. package/dist/protocols/meta-images.js +10 -8
  73. package/dist/protocols/meta-messages.d.ts +73 -38
  74. package/dist/protocols/meta-messages.js +14 -1
  75. package/dist/protocols/meta-responses.d.ts +84 -78
  76. package/dist/protocols/meta-responses.js +29 -1
  77. package/dist/protocols/mistral-chat.d.ts +81 -78
  78. package/dist/protocols/mistral-chat.js +2 -1
  79. package/dist/protocols/open-responses-channel.js +47 -46
  80. package/dist/protocols/open-responses-continuation.d.ts +8 -2
  81. package/dist/protocols/open-responses-continuation.js +3 -1
  82. package/dist/protocols/open-responses.d.ts +426 -396
  83. package/dist/protocols/open-responses.js +16 -25
  84. package/dist/protocols/openai-chat.d.ts +764 -52
  85. package/dist/protocols/openai-chat.js +38 -25
  86. package/dist/protocols/openai-compatible-chat.d.ts +35 -15
  87. package/dist/protocols/openai-compatible-responses.d.ts +44 -42
  88. package/dist/protocols/openai-decisions.d.ts +22 -0
  89. package/dist/protocols/openai-decisions.js +128 -0
  90. package/dist/protocols/openai-images.d.ts +53 -56
  91. package/dist/protocols/openai-images.js +100 -34
  92. package/dist/protocols/openai-responses.d.ts +322 -264
  93. package/dist/protocols/openai-responses.js +33 -3
  94. package/dist/protocols/openai-speech.d.ts +32 -50
  95. package/dist/protocols/openai-speech.js +15 -5
  96. package/dist/protocols/openai-transcription.d.ts +32 -50
  97. package/dist/protocols/openai-transcription.js +15 -11
  98. package/dist/protocols/replicate-images.js +1 -1
  99. package/dist/protocols/runway-video.d.ts +1 -1
  100. package/dist/protocols/runway-video.js +1 -1
  101. package/dist/protocols/shared.d.ts +9 -1
  102. package/dist/protocols/shared.js +13 -2
  103. package/dist/protocols/stability-images.d.ts +5 -0
  104. package/dist/protocols/stability-images.js +4 -9
  105. package/dist/protocols/system-one.d.ts +101 -0
  106. package/dist/protocols/system-one.js +230 -0
  107. package/dist/protocols/utils/bedrock-cache.d.ts +2 -2
  108. package/dist/protocols/utils/bedrock-cache.js +7 -6
  109. package/dist/protocols/utils/cache.d.ts +14 -3
  110. package/dist/protocols/utils/cache.js +11 -4
  111. package/dist/protocols/utils/gemini-generate-content.d.ts +3 -3
  112. package/dist/protocols/utils/gemini-generate-content.js +10 -16
  113. package/dist/protocols/utils/gemini-json-schema.d.ts +5 -0
  114. package/dist/protocols/utils/gemini-json-schema.js +3 -3
  115. package/dist/protocols/utils/image-sizing.d.ts +31 -0
  116. package/dist/protocols/utils/image-sizing.js +73 -0
  117. package/dist/protocols/utils/media-input.d.ts +4 -5
  118. package/dist/protocols/utils/media-input.js +8 -5
  119. package/dist/protocols/utils/partial-json-options.d.ts +0 -1
  120. package/dist/protocols/utils/partial-json-options.js +0 -1
  121. package/dist/protocols/utils/speech-stream.d.ts +0 -1
  122. package/dist/protocols/utils/speech-stream.js +0 -1
  123. package/dist/protocols/utils/tool-schema.js +15 -5
  124. package/dist/protocols/utils/tool-stream.d.ts +10 -1
  125. package/dist/protocols/venice-chat.d.ts +71 -53
  126. package/dist/protocols/venice-chat.js +7 -6
  127. package/dist/protocols/xai-images.d.ts +1 -2
  128. package/dist/protocols/xai-images.js +9 -2
  129. package/dist/protocols/xai-responses.d.ts +88 -78
  130. package/dist/protocols/xai-responses.js +28 -2
  131. package/dist/protocols/xai-video.d.ts +1 -1
  132. package/dist/protocols/xai-video.js +11 -8
  133. package/dist/protocols/zai-chat.d.ts +54 -30
  134. package/dist/protocols/zai-chat.js +8 -3
  135. package/dist/protocols/zai-images.d.ts +3 -1
  136. package/dist/protocols/zai-images.js +12 -11
  137. package/dist/protocols/zai-messages.d.ts +72 -35
  138. package/dist/protocols/zai-messages.js +3 -2
  139. package/dist/provider-error.js +24 -7
  140. package/dist/providers/alibaba/images.d.ts +2 -0
  141. package/dist/providers/alibaba/images.js +1 -0
  142. package/dist/providers/alibaba.d.ts +146 -85
  143. package/dist/providers/alibaba.js +16 -8
  144. package/dist/providers/amazon-bedrock-mantle.d.ts +277 -218
  145. package/dist/providers/amazon-bedrock.d.ts +48 -28
  146. package/dist/providers/amazon-bedrock.js +11 -11
  147. package/dist/providers/anthropic-compatible.d.ts +64 -29
  148. package/dist/providers/anthropic.d.ts +64 -29
  149. package/dist/providers/azure/images.d.ts +2 -0
  150. package/dist/providers/azure/images.js +1 -0
  151. package/dist/providers/azure.d.ts +156 -115
  152. package/dist/providers/azure.js +19 -1
  153. package/dist/providers/baseten/decisions.d.ts +2 -0
  154. package/dist/providers/baseten/decisions.js +1 -0
  155. package/dist/providers/baseten.d.ts +75 -25
  156. package/dist/providers/baseten.js +41 -8
  157. package/dist/providers/cerebras.d.ts +66 -24
  158. package/dist/providers/cloudflare-ai-gateway/decisions.d.ts +2 -0
  159. package/dist/providers/cloudflare-ai-gateway/decisions.js +1 -0
  160. package/dist/providers/cloudflare-ai-gateway.d.ts +409 -272
  161. package/dist/providers/cloudflare-ai-gateway.js +33 -6
  162. package/dist/providers/cloudflare-workers-ai/decisions.d.ts +2 -0
  163. package/dist/providers/cloudflare-workers-ai/decisions.js +1 -0
  164. package/dist/providers/cloudflare-workers-ai/images.d.ts +2 -0
  165. package/dist/providers/cloudflare-workers-ai/images.js +1 -0
  166. package/dist/providers/cloudflare-workers-ai.d.ts +79 -25
  167. package/dist/providers/cloudflare-workers-ai.js +45 -9
  168. package/dist/providers/cohere.d.ts +117 -75
  169. package/dist/providers/deepinfra.d.ts +66 -24
  170. package/dist/providers/deepseek.d.ts +66 -24
  171. package/dist/providers/digitalocean/decisions.d.ts +2 -0
  172. package/dist/providers/digitalocean/decisions.js +1 -0
  173. package/dist/providers/digitalocean/images.d.ts +2 -0
  174. package/dist/providers/digitalocean/images.js +1 -0
  175. package/dist/providers/digitalocean.d.ts +108 -37
  176. package/dist/providers/digitalocean.js +39 -8
  177. package/dist/providers/fireworks.d.ts +66 -24
  178. package/dist/providers/google/images.d.ts +2 -0
  179. package/dist/providers/google/images.js +1 -0
  180. package/dist/providers/google-vertex/images.d.ts +2 -0
  181. package/dist/providers/google-vertex/images.js +1 -0
  182. package/dist/providers/google-vertex-chat.d.ts +33 -12
  183. package/dist/providers/google-vertex-messages.d.ts +64 -29
  184. package/dist/providers/google-vertex-mistral.d.ts +17 -17
  185. package/dist/providers/google-vertex-responses.d.ts +38 -35
  186. package/dist/providers/google-vertex.d.ts +20 -18
  187. package/dist/providers/google-vertex.js +32 -15
  188. package/dist/providers/google.d.ts +22 -21
  189. package/dist/providers/google.js +15 -8
  190. package/dist/providers/groq.d.ts +66 -28
  191. package/dist/providers/groq.js +0 -6
  192. package/dist/providers/meta.d.ts +144 -85
  193. package/dist/providers/minimax.d.ts +145 -86
  194. package/dist/providers/mistral.d.ts +28 -28
  195. package/dist/providers/moonshot.d.ts +277 -218
  196. package/dist/providers/open-responses-options.d.ts +0 -1
  197. package/dist/providers/open-responses-options.js +1 -1
  198. package/dist/providers/openai/images.d.ts +2 -0
  199. package/dist/providers/openai/images.js +1 -0
  200. package/dist/providers/openai-compatible/images.d.ts +2 -0
  201. package/dist/providers/openai-compatible/images.js +1 -0
  202. package/dist/providers/openai-compatible-responses.d.ts +38 -35
  203. package/dist/providers/openai-compatible.d.ts +36 -13
  204. package/dist/providers/openai-compatible.js +22 -10
  205. package/dist/providers/openai-options.d.ts +3 -2
  206. package/dist/providers/openai-options.js +0 -1
  207. package/dist/providers/openai.d.ts +146 -109
  208. package/dist/providers/openai.js +20 -4
  209. package/dist/providers/opencode/decisions.d.ts +2 -0
  210. package/dist/providers/opencode/decisions.js +1 -0
  211. package/dist/providers/opencode.d.ts +9 -10
  212. package/dist/providers/opencode.js +10 -8
  213. package/dist/providers/openrouter/decisions.d.ts +2 -0
  214. package/dist/providers/openrouter/decisions.js +1 -0
  215. package/dist/providers/openrouter/images.d.ts +2 -0
  216. package/dist/providers/openrouter/images.js +1 -0
  217. package/dist/providers/openrouter.d.ts +128 -47
  218. package/dist/providers/openrouter.js +38 -16
  219. package/dist/providers/togetherai.d.ts +66 -24
  220. package/dist/providers/typesafe-ai/decisions.d.ts +2 -0
  221. package/dist/providers/typesafe-ai/decisions.js +1 -0
  222. package/dist/providers/typesafe-ai.d.ts +9 -10
  223. package/dist/providers/typesafe-ai.js +10 -8
  224. package/dist/providers/venice.d.ts +214 -20
  225. package/dist/providers/vercel-ai-gateway/decisions.d.ts +2 -0
  226. package/dist/providers/vercel-ai-gateway/decisions.js +1 -0
  227. package/dist/providers/vercel-ai-gateway/images.d.ts +2 -0
  228. package/dist/providers/vercel-ai-gateway/images.js +1 -0
  229. package/dist/providers/vercel-ai-gateway.d.ts +10 -16
  230. package/dist/providers/vercel-ai-gateway.js +31 -85
  231. package/dist/providers/xai/images.d.ts +2 -0
  232. package/dist/providers/xai/images.js +1 -0
  233. package/dist/providers/xai.d.ts +40 -17
  234. package/dist/providers/xai.js +15 -8
  235. package/dist/providers/zai/images.d.ts +2 -0
  236. package/dist/providers/zai/images.js +1 -0
  237. package/dist/providers/zai-coding-plan.d.ts +224 -85
  238. package/dist/providers/zai.d.ts +110 -23
  239. package/dist/providers/zai.js +15 -8
  240. package/dist/route/auth-options.d.ts +0 -3
  241. package/dist/route/auth.d.ts +0 -2
  242. package/dist/route/auth.js +0 -2
  243. package/dist/route/client.d.ts +2 -4
  244. package/dist/route/client.js +29 -8
  245. package/dist/route/decision.d.ts +58 -0
  246. package/dist/route/decision.js +75 -0
  247. package/dist/route/executor-service.js +1 -1
  248. package/dist/route/executor.d.ts +1 -1
  249. package/dist/route/framing.d.ts +2 -0
  250. package/dist/route/index.d.ts +3 -0
  251. package/dist/route/index.js +3 -0
  252. package/dist/route/media-protocol.d.ts +1 -0
  253. package/dist/route/media-protocol.js +12 -8
  254. package/dist/route/media.d.ts +5 -1
  255. package/dist/route/media.js +6 -2
  256. package/dist/route/transport/http.js +1 -0
  257. package/dist/route/transport/index.d.ts +1 -0
  258. package/dist/schema/events.d.ts +148 -6
  259. package/dist/schema/events.js +20 -0
  260. package/dist/schema/messages.d.ts +6 -2
  261. package/dist/schema/messages.js +3 -0
  262. package/dist/schema/options.d.ts +5 -3
  263. package/dist/schema/options.js +8 -3
  264. package/dist/speech-client.d.ts +13 -22
  265. package/dist/speech-client.js +0 -1
  266. package/dist/speech.d.ts +148 -283
  267. package/dist/speech.js +2 -4
  268. package/dist/testing.d.ts +25 -24
  269. package/dist/testing.js +3 -21
  270. package/dist/transcription-client.d.ts +13 -22
  271. package/dist/transcription.d.ts +148 -283
  272. package/dist/transcription.js +1 -1
  273. package/dist/utils/media-type.js +22 -1
  274. package/dist/video-client.d.ts +13 -22
  275. package/dist/video-client.js +0 -1
  276. package/dist/video.d.ts +115 -250
  277. package/dist/video.js +3 -5
  278. package/package.json +516 -13
  279. package/dist/experimental/evaluation-client.d.ts +0 -21
  280. package/dist/experimental/evaluation-client.js +0 -64
  281. package/dist/experimental/evaluation.d.ts +0 -242
  282. package/dist/experimental/evaluation.js +0 -115
  283. package/dist/experimental/system-one.d.ts +0 -16
  284. package/dist/experimental/system-one.js +0 -135
  285. package/dist/promise.d.ts +0 -1021
  286. package/dist/promise.js +0 -101
  287. package/dist/provider.d.ts +0 -23
  288. package/dist/provider.js +0 -2
@@ -1,64 +0,0 @@
1
- import { Context, Effect, Layer } from "effect";
2
- import { RequestExecutor } from "../route/executor.js";
3
- import { AIError, InvalidProviderOutputError, mergeHttpOptions } from "../schema/index.js";
4
- import { sanitizeSurrogates } from "../utils/sanitize.js";
5
- import {} from "./evaluation.js";
6
- export class Service extends Context.Service()("@opencode/AI/Experimental/EvaluationClient") {
7
- }
8
- export const evaluate = (request) => Effect.flatMap(Service, (client) => client.evaluate(request));
9
- export const layer = Layer.effect(Service, Effect.gen(function* () {
10
- const executor = yield* RequestExecutor.Service;
11
- return Service.of({
12
- evaluate: (request) => request.model.route
13
- .evaluate({
14
- ...sanitizeSurrogates({
15
- ...request,
16
- model: undefined,
17
- http: mergeHttpOptions(request.model.http, request.http),
18
- }),
19
- model: request.model,
20
- }, executor.execute)
21
- .pipe(Effect.flatMap((response) => {
22
- const questions = Object.entries(request.questions);
23
- if (questions.length === Object.keys(response.answers).length &&
24
- questions.every(([id, question]) => {
25
- const answer = response.answers[id];
26
- if (question.type === "boolean")
27
- return answer?.type === "boolean";
28
- if (question.type === "choice") {
29
- if (answer?.type !== "choice" || !Object.hasOwn(question.criteria, answer.choice))
30
- return false;
31
- if (answer.probabilities === undefined)
32
- return true;
33
- const keys = Object.keys(question.criteria);
34
- const probabilities = answer.probabilities;
35
- return (Object.keys(probabilities).length === keys.length &&
36
- keys.every((key) => Object.hasOwn(probabilities, key)));
37
- }
38
- if (answer?.type !== "score" || answer.score < 0 || answer.score > question.criteria.length - 1)
39
- return false;
40
- if (answer.probabilities === undefined)
41
- return true;
42
- const keys = question.criteria.map((_, index) => String(index));
43
- const probabilities = answer.probabilities;
44
- return (Object.keys(probabilities).length === keys.length &&
45
- keys.every((key) => Object.hasOwn(probabilities, key)));
46
- }))
47
- return Effect.succeed(response);
48
- return Effect.fail(new AIError({
49
- reason: new InvalidProviderOutputError({
50
- route: request.model.route.id,
51
- message: "Evaluation answers do not match the requested questions",
52
- cause: response.answers,
53
- }),
54
- }));
55
- })),
56
- });
57
- }));
58
- export const fetchLayer = layer.pipe(Layer.provideMerge(RequestExecutor.fetchLayer));
59
- export const EvaluationClient = {
60
- Service,
61
- layer,
62
- fetchLayer,
63
- evaluate,
64
- };
@@ -1,242 +0,0 @@
1
- import { Effect, Schema } from "effect";
2
- import { AIError, HttpOptions, ModelID, ProviderID, Usage } from "../schema/index.js";
3
- import { Service, type Execute } from "./evaluation-client.js";
4
- export declare const EvaluationInput: Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>;
5
- export type EvaluationInput = Schema.Schema.Type<typeof EvaluationInput>;
6
- export declare const ChoiceQuestion: Schema.Struct<{
7
- readonly type: Schema.Literal<"choice">;
8
- readonly instructions: Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>;
9
- readonly criteria: Schema.refine<{
10
- readonly [x: string]: string | readonly Schema.Json[] | {
11
- readonly [x: string]: Schema.Json;
12
- } | null;
13
- }, Schema.$Record<Schema.String, Schema.NullOr<Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>>>>;
14
- }>;
15
- export type ChoiceQuestion = Schema.Schema.Type<typeof ChoiceQuestion>;
16
- export declare const ScoreQuestion: Schema.Struct<{
17
- readonly type: Schema.Literal<"score">;
18
- readonly instructions: Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>;
19
- readonly criteria: Schema.$Array<Schema.NullOr<Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>>>;
20
- }>;
21
- export type ScoreQuestion = Schema.Schema.Type<typeof ScoreQuestion>;
22
- export declare const BooleanQuestion: Schema.Struct<{
23
- readonly type: Schema.Literal<"boolean">;
24
- readonly instructions: Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>;
25
- readonly criteria: Schema.optional<Schema.Struct<{
26
- readonly true: Schema.optional<Schema.NullOr<Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>>>;
27
- readonly false: Schema.optional<Schema.NullOr<Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>>>;
28
- }>>;
29
- }>;
30
- export type BooleanQuestion = Schema.Schema.Type<typeof BooleanQuestion>;
31
- export declare const EvaluationQuestion: Schema.toTaggedUnion<"type", readonly [Schema.Struct<{
32
- readonly type: Schema.Literal<"choice">;
33
- readonly instructions: Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>;
34
- readonly criteria: Schema.refine<{
35
- readonly [x: string]: string | readonly Schema.Json[] | {
36
- readonly [x: string]: Schema.Json;
37
- } | null;
38
- }, Schema.$Record<Schema.String, Schema.NullOr<Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>>>>;
39
- }>, Schema.Struct<{
40
- readonly type: Schema.Literal<"score">;
41
- readonly instructions: Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>;
42
- readonly criteria: Schema.$Array<Schema.NullOr<Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>>>;
43
- }>, Schema.Struct<{
44
- readonly type: Schema.Literal<"boolean">;
45
- readonly instructions: Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>;
46
- readonly criteria: Schema.optional<Schema.Struct<{
47
- readonly true: Schema.optional<Schema.NullOr<Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>>>;
48
- readonly false: Schema.optional<Schema.NullOr<Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>>>;
49
- }>>;
50
- }>]>;
51
- export type EvaluationQuestion = Schema.Schema.Type<typeof EvaluationQuestion>;
52
- export type EvaluationQuestions = Readonly<Record<string, EvaluationQuestion>>;
53
- export declare const ChoiceAnswer: Schema.Struct<{
54
- readonly type: Schema.Literal<"choice">;
55
- readonly choice: Schema.String;
56
- readonly probabilities: Schema.optional<Schema.$Record<Schema.String, Schema.Number>>;
57
- readonly confidence: Schema.optional<Schema.Number>;
58
- }>;
59
- export type ChoiceAnswer = Schema.Schema.Type<typeof ChoiceAnswer>;
60
- export declare const ScoreAnswer: Schema.Struct<{
61
- readonly type: Schema.Literal<"score">;
62
- readonly score: Schema.Number;
63
- readonly probabilities: Schema.optional<Schema.$Record<Schema.String, Schema.Number>>;
64
- readonly confidence: Schema.optional<Schema.Number>;
65
- }>;
66
- export type ScoreAnswer = Schema.Schema.Type<typeof ScoreAnswer>;
67
- export declare const BooleanAnswer: Schema.Struct<{
68
- readonly type: Schema.Literal<"boolean">;
69
- readonly probability: Schema.Number;
70
- }>;
71
- export type BooleanAnswer = Schema.Schema.Type<typeof BooleanAnswer>;
72
- export declare const EvaluationAnswer: Schema.toTaggedUnion<"type", readonly [Schema.Struct<{
73
- readonly type: Schema.Literal<"choice">;
74
- readonly choice: Schema.String;
75
- readonly probabilities: Schema.optional<Schema.$Record<Schema.String, Schema.Number>>;
76
- readonly confidence: Schema.optional<Schema.Number>;
77
- }>, Schema.Struct<{
78
- readonly type: Schema.Literal<"score">;
79
- readonly score: Schema.Number;
80
- readonly probabilities: Schema.optional<Schema.$Record<Schema.String, Schema.Number>>;
81
- readonly confidence: Schema.optional<Schema.Number>;
82
- }>, Schema.Struct<{
83
- readonly type: Schema.Literal<"boolean">;
84
- readonly probability: Schema.Number;
85
- }>]>;
86
- export type EvaluationAnswer = Schema.Schema.Type<typeof EvaluationAnswer>;
87
- export type AnswerFor<Question extends EvaluationQuestion> = Question extends {
88
- readonly type: "choice";
89
- readonly criteria: infer Criteria;
90
- } ? {
91
- readonly type: "choice";
92
- readonly choice: Extract<keyof Criteria, string>;
93
- readonly probabilities?: Readonly<Record<Extract<keyof Criteria, string>, number>>;
94
- readonly confidence?: number;
95
- } : Question extends {
96
- readonly type: "score";
97
- } ? ScoreAnswer : BooleanAnswer;
98
- export type AnswersFor<Questions extends EvaluationQuestions> = {
99
- readonly [ID in keyof Questions]: AnswerFor<Questions[ID]>;
100
- };
101
- export type EvaluationOptions = Record<string, unknown>;
102
- export interface EvaluationRoute<Options extends EvaluationOptions = EvaluationOptions> {
103
- readonly id: string;
104
- evaluate(request: EvaluationRequestFor<Options>, execute: Execute): Effect.Effect<EvaluationResponse, AIError>;
105
- }
106
- export declare class EvaluationModel<Options extends EvaluationOptions = EvaluationOptions> {
107
- protected readonly _Options: Options;
108
- readonly id: ModelID;
109
- readonly provider: ProviderID;
110
- readonly route: EvaluationRoute<Options>;
111
- readonly http?: HttpOptions;
112
- constructor(input: EvaluationModel.Input<Options>);
113
- static make<Options extends EvaluationOptions = EvaluationOptions>(input: EvaluationModel.MakeInput<Options>): EvaluationModel<Options>;
114
- }
115
- export declare namespace EvaluationModel {
116
- interface Input<Options extends EvaluationOptions = EvaluationOptions> {
117
- readonly id: ModelID;
118
- readonly provider: ProviderID;
119
- readonly route: EvaluationRoute<Options>;
120
- readonly http?: HttpOptions;
121
- }
122
- interface MakeInput<Options extends EvaluationOptions = EvaluationOptions> extends Omit<Input<Options>, "id" | "provider"> {
123
- readonly id: string | ModelID;
124
- readonly provider: string | ProviderID;
125
- }
126
- }
127
- export declare const EvaluationModelSchema: Schema.declare<EvaluationModel<EvaluationOptions>, EvaluationModel<EvaluationOptions>>;
128
- declare const EvaluationRequest_base: Schema.Class<EvaluationRequest, Schema.Struct<{
129
- readonly model: Schema.declare<EvaluationModel<EvaluationOptions>, EvaluationModel<EvaluationOptions>>;
130
- readonly state: Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>;
131
- readonly questions: Schema.refine<{
132
- readonly [x: string]: {
133
- readonly type: "choice";
134
- readonly instructions: string | readonly Schema.Json[] | {
135
- readonly [x: string]: Schema.Json;
136
- };
137
- readonly criteria: {
138
- readonly [x: string]: string | readonly Schema.Json[] | {
139
- readonly [x: string]: Schema.Json;
140
- } | null;
141
- };
142
- } | {
143
- readonly type: "score";
144
- readonly instructions: string | readonly Schema.Json[] | {
145
- readonly [x: string]: Schema.Json;
146
- };
147
- readonly criteria: readonly (string | readonly Schema.Json[] | {
148
- readonly [x: string]: Schema.Json;
149
- } | null)[];
150
- } | {
151
- readonly type: "boolean";
152
- readonly instructions: string | readonly Schema.Json[] | {
153
- readonly [x: string]: Schema.Json;
154
- };
155
- readonly criteria?: {
156
- readonly true?: string | readonly Schema.Json[] | {
157
- readonly [x: string]: Schema.Json;
158
- } | null | undefined;
159
- readonly false?: string | readonly Schema.Json[] | {
160
- readonly [x: string]: Schema.Json;
161
- } | null | undefined;
162
- } | undefined;
163
- };
164
- }, Schema.$Record<Schema.String, Schema.toTaggedUnion<"type", readonly [Schema.Struct<{
165
- readonly type: Schema.Literal<"choice">;
166
- readonly instructions: Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>;
167
- readonly criteria: Schema.refine<{
168
- readonly [x: string]: string | readonly Schema.Json[] | {
169
- readonly [x: string]: Schema.Json;
170
- } | null;
171
- }, Schema.$Record<Schema.String, Schema.NullOr<Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>>>>;
172
- }>, Schema.Struct<{
173
- readonly type: Schema.Literal<"score">;
174
- readonly instructions: Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>;
175
- readonly criteria: Schema.$Array<Schema.NullOr<Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>>>;
176
- }>, Schema.Struct<{
177
- readonly type: Schema.Literal<"boolean">;
178
- readonly instructions: Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>;
179
- readonly criteria: Schema.optional<Schema.Struct<{
180
- readonly true: Schema.optional<Schema.NullOr<Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>>>;
181
- readonly false: Schema.optional<Schema.NullOr<Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>, Schema.$Array<Schema.Codec<Schema.Json, Schema.Json, never, never>>]>>>;
182
- }>>;
183
- }>]>>>;
184
- readonly options: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
185
- readonly http: Schema.optional<typeof HttpOptions>;
186
- }>, {}>;
187
- export declare class EvaluationRequest extends EvaluationRequest_base {
188
- protected readonly _EvaluationRequest: void;
189
- }
190
- export type EvaluationModelOptions<Model> = Model extends EvaluationModel<infer Options> ? Options : never;
191
- export type EvaluationRequestFor<Options extends EvaluationOptions = EvaluationOptions, Questions extends EvaluationQuestions = EvaluationQuestions> = Omit<EvaluationRequest, "model" | "questions" | "options"> & {
192
- readonly model: EvaluationModel<Options>;
193
- readonly questions: Questions;
194
- readonly options?: Options;
195
- };
196
- export type EvaluationRequestInput<Model extends EvaluationModel = EvaluationModel, Questions extends EvaluationQuestions = EvaluationQuestions> = Omit<ConstructorParameters<typeof EvaluationRequest>[0], "model" | "questions" | "options" | "http"> & {
197
- readonly model: Model;
198
- readonly questions: Questions;
199
- readonly options?: NoInfer<EvaluationModelOptions<Model>>;
200
- readonly http?: HttpOptions.Input;
201
- };
202
- declare const EvaluationRounding_base: Schema.Class<EvaluationRounding, Schema.Struct<{
203
- readonly probabilityDecimals: Schema.optional<Schema.Int>;
204
- readonly scoreDecimals: Schema.optional<Schema.Int>;
205
- }>, {}>;
206
- export declare class EvaluationRounding extends EvaluationRounding_base {
207
- }
208
- declare const EvaluationResponse_base: Schema.Class<EvaluationResponse, Schema.Struct<{
209
- readonly model: Schema.brand<Schema.String, "AI.ModelID">;
210
- readonly answers: Schema.$Record<Schema.String, Schema.toTaggedUnion<"type", readonly [Schema.Struct<{
211
- readonly type: Schema.Literal<"choice">;
212
- readonly choice: Schema.String;
213
- readonly probabilities: Schema.optional<Schema.$Record<Schema.String, Schema.Number>>;
214
- readonly confidence: Schema.optional<Schema.Number>;
215
- }>, Schema.Struct<{
216
- readonly type: Schema.Literal<"score">;
217
- readonly score: Schema.Number;
218
- readonly probabilities: Schema.optional<Schema.$Record<Schema.String, Schema.Number>>;
219
- readonly confidence: Schema.optional<Schema.Number>;
220
- }>, Schema.Struct<{
221
- readonly type: Schema.Literal<"boolean">;
222
- readonly probability: Schema.Number;
223
- }>]>>;
224
- readonly usage: Schema.optional<typeof Usage>;
225
- readonly rounding: Schema.optional<typeof EvaluationRounding>;
226
- readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
227
- }>, {}>;
228
- export declare class EvaluationResponse extends EvaluationResponse_base {
229
- }
230
- export type EvaluationResponseFor<Questions extends EvaluationQuestions> = Omit<EvaluationResponse, "answers"> & {
231
- readonly answers: AnswersFor<Questions>;
232
- };
233
- export declare function request<const Model extends EvaluationModel, const Questions extends EvaluationQuestions>(input: EvaluationRequestInput<Model, Questions>): EvaluationRequestFor<EvaluationModelOptions<Model>, Questions>;
234
- export declare function request(input: EvaluationRequest): EvaluationRequest;
235
- export declare function request(input: EvaluationRequest | EvaluationRequestInput): EvaluationRequest;
236
- export declare function run<const Model extends EvaluationModel, const Questions extends EvaluationQuestions>(input: EvaluationRequestInput<Model, Questions>): Effect.Effect<EvaluationResponseFor<Questions>, AIError, Service>;
237
- export declare function run(input: EvaluationRequest): Effect.Effect<EvaluationResponse, AIError, Service>;
238
- export declare const Evaluation: {
239
- readonly request: typeof request;
240
- readonly run: typeof run;
241
- };
242
- export {};
@@ -1,115 +0,0 @@
1
- import { Effect, Schema } from "effect";
2
- import { AIError, HttpOptions, InvalidRequestError, ModelID, ProviderID, ProviderMetadata, Usage, } from "../schema/index.js";
3
- import { EvaluationClient, Service } from "./evaluation-client.js";
4
- export const EvaluationInput = Schema.Union([Schema.String, Schema.JsonObject, Schema.Array(Schema.Json)]);
5
- const EvaluationCriterion = Schema.NullOr(EvaluationInput);
6
- const ChoiceCriteria = Schema.Record(Schema.String, EvaluationCriterion).pipe(Schema.refine((x) => Object.keys(x).length > 0, {
7
- message: "Choice criteria must be a nonempty option map",
8
- }));
9
- export const ChoiceQuestion = Schema.Struct({
10
- type: Schema.Literal("choice"),
11
- instructions: EvaluationInput,
12
- criteria: ChoiceCriteria,
13
- });
14
- export const ScoreQuestion = Schema.Struct({
15
- type: Schema.Literal("score"),
16
- instructions: EvaluationInput,
17
- criteria: Schema.Array(EvaluationCriterion).check(Schema.isMinLength(2)),
18
- });
19
- export const BooleanQuestion = Schema.Struct({
20
- type: Schema.Literal("boolean"),
21
- instructions: EvaluationInput,
22
- criteria: Schema.optional(Schema.Struct({
23
- true: Schema.optional(EvaluationCriterion),
24
- false: Schema.optional(EvaluationCriterion),
25
- })),
26
- });
27
- export const EvaluationQuestion = Schema.Union([ChoiceQuestion, ScoreQuestion, BooleanQuestion]).pipe(Schema.toTaggedUnion("type"));
28
- const EvaluationQuestions = Schema.Record(Schema.String, EvaluationQuestion).pipe(Schema.refine((x) => Object.keys(x).length > 0, {
29
- message: "Evaluation questions must be a nonempty map",
30
- }));
31
- const Probability = Schema.Number.check(Schema.isBetween({ minimum: 0, maximum: 1 }));
32
- export const ChoiceAnswer = Schema.Struct({
33
- type: Schema.Literal("choice"),
34
- choice: Schema.String,
35
- probabilities: Schema.optional(Schema.Record(Schema.String, Probability)),
36
- confidence: Schema.optional(Probability),
37
- });
38
- export const ScoreAnswer = Schema.Struct({
39
- type: Schema.Literal("score"),
40
- score: Schema.Number,
41
- probabilities: Schema.optional(Schema.Record(Schema.String, Probability)),
42
- confidence: Schema.optional(Probability),
43
- });
44
- export const BooleanAnswer = Schema.Struct({
45
- type: Schema.Literal("boolean"),
46
- probability: Probability,
47
- });
48
- export const EvaluationAnswer = Schema.Union([ChoiceAnswer, ScoreAnswer, BooleanAnswer]).pipe(Schema.toTaggedUnion("type"));
49
- export class EvaluationModel {
50
- id;
51
- provider;
52
- route;
53
- http;
54
- constructor(input) {
55
- this.id = input.id;
56
- this.provider = input.provider;
57
- this.route = input.route;
58
- this.http = input.http;
59
- }
60
- static make(input) {
61
- return new EvaluationModel({
62
- id: ModelID.make(input.id),
63
- provider: ProviderID.make(input.provider),
64
- route: input.route,
65
- http: input.http,
66
- });
67
- }
68
- }
69
- export const EvaluationModelSchema = Schema.declare((value) => value instanceof EvaluationModel, {
70
- expected: "Evaluation.Model",
71
- });
72
- export class EvaluationRequest extends Schema.Class("Evaluation.Request")({
73
- model: EvaluationModelSchema,
74
- state: EvaluationInput,
75
- questions: EvaluationQuestions,
76
- options: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
77
- http: Schema.optional(HttpOptions),
78
- }) {
79
- }
80
- export class EvaluationRounding extends Schema.Class("Evaluation.Rounding")({
81
- probabilityDecimals: Schema.optional(Schema.Int),
82
- scoreDecimals: Schema.optional(Schema.Int),
83
- }) {
84
- }
85
- export class EvaluationResponse extends Schema.Class("Evaluation.Response")({
86
- model: ModelID,
87
- answers: Schema.Record(Schema.String, EvaluationAnswer),
88
- usage: Schema.optional(Usage),
89
- rounding: Schema.optional(EvaluationRounding),
90
- providerMetadata: Schema.optional(ProviderMetadata),
91
- }) {
92
- }
93
- export function request(input) {
94
- if (input instanceof EvaluationRequest)
95
- return input;
96
- return new EvaluationRequest({
97
- ...input,
98
- http: HttpOptions.make(input.http),
99
- });
100
- }
101
- export function run(input) {
102
- return Effect.try({
103
- try: () => request(input),
104
- catch: (cause) => new AIError({
105
- reason: new InvalidRequestError({
106
- message: cause instanceof Error ? cause.message : String(cause),
107
- cause,
108
- }),
109
- }),
110
- }).pipe(Effect.flatMap(EvaluationClient.evaluate));
111
- }
112
- export const Evaluation = {
113
- request,
114
- run,
115
- };
@@ -1,16 +0,0 @@
1
- import { EvaluationModel, type EvaluationOptions } from "./evaluation.js";
2
- import { type Definition as AuthDefinition } from "../route/auth.js";
3
- import { HttpOptions, ModelID } from "../schema/index.js";
4
- export interface ModelInput {
5
- readonly id: string | ModelID;
6
- readonly provider: string;
7
- readonly providerMetadataKey: string;
8
- readonly auth: AuthDefinition;
9
- readonly baseURL: string;
10
- readonly headers?: Record<string, string>;
11
- readonly http?: HttpOptions;
12
- }
13
- export declare const model: <Options extends EvaluationOptions = EvaluationOptions>(cfg: ModelInput) => EvaluationModel<Options>;
14
- export declare const SystemOne: {
15
- readonly model: typeof model;
16
- };
@@ -1,135 +0,0 @@
1
- import { Effect, Schema } from "effect";
2
- import { Headers, HttpClientRequest } from "effect/http";
3
- import { ChoiceQuestion, EvaluationInput, EvaluationModel, EvaluationResponse, EvaluationRounding, ScoreQuestion, } from "./evaluation.js";
4
- import { Auth } from "../route/auth.js";
5
- import { RequestExecutor } from "../route/executor.js";
6
- import { JsonObject, ProviderShared } from "../protocols/shared.js";
7
- import { AIError, HttpOptions, InvalidProviderOutputError, InvalidRequestError, ModelID, ProviderMetadata, Usage, mergeJsonRecords, } from "../schema/index.js";
8
- const Noul = Schema.Struct({
9
- type: Schema.Literal("noul"),
10
- instructions: EvaluationInput,
11
- criteria: Schema.optional(Schema.Struct({
12
- true: Schema.optional(Schema.NullOr(EvaluationInput)),
13
- false: Schema.optional(Schema.NullOr(EvaluationInput)),
14
- })),
15
- });
16
- const Question = Schema.Union([
17
- ChoiceQuestion.pipe(Schema.refine((x) => Object.keys(x.criteria).length <= 255, {
18
- message: "System One Choice questions support at most 255 options",
19
- })),
20
- ScoreQuestion.pipe(Schema.refine((x) => x.criteria.length <= 10, {
21
- message: "System One Score questions support at most 10 levels",
22
- })),
23
- Noul,
24
- ]);
25
- const Request = Schema.StructWithRest(Schema.Struct({
26
- model: Schema.String,
27
- state: EvaluationInput,
28
- questions: Schema.Record(Schema.String, Question),
29
- }), [JsonObject]);
30
- const Probability = Schema.Number.check(Schema.isBetween({ minimum: 0, maximum: 1 }));
31
- const NoulAnswer = Schema.Struct({ type: Schema.Literal("noul"), noul: Probability });
32
- const Choice = Schema.Struct({
33
- type: Schema.Literal("choice"),
34
- choice: Schema.String,
35
- probabilities: Schema.Record(Schema.String, Probability),
36
- confidence: Schema.optional(Probability),
37
- });
38
- const Score = Schema.Struct({
39
- type: Schema.Literal("score"),
40
- score: Schema.Number,
41
- probabilities: Schema.Record(Schema.String, Probability),
42
- legend: Schema.optional(Schema.Record(Schema.String, Schema.Json)),
43
- confidence: Schema.optional(Probability),
44
- });
45
- const Answer = Schema.Union([NoulAnswer, Choice, Score]).pipe(Schema.toTaggedUnion("type"));
46
- const NativeUsage = Schema.StructWithRest(Schema.Struct({
47
- input_tokens: Schema.optional(Schema.Number),
48
- output_tokens: Schema.optional(Schema.Number),
49
- }), [JsonObject]);
50
- const Response = Schema.Struct({
51
- model: Schema.String,
52
- answers: Schema.Record(Schema.String, Answer),
53
- usage: Schema.optional(NativeUsage),
54
- id: Schema.optional(Schema.String),
55
- provider: Schema.optional(Schema.String),
56
- provider_metadata: Schema.optional(ProviderMetadata),
57
- });
58
- export const model = (cfg) => EvaluationModel.make({
59
- id: cfg.id,
60
- provider: cfg.provider,
61
- http: cfg.http,
62
- route: {
63
- id: "system-one",
64
- evaluate: (req, send) => Effect.gen(function* () {
65
- const url = new URL(`${cfg.baseURL.replace(/\/$/, "")}/systemone`);
66
- Object.entries(req.http?.query ?? {}).forEach(([key, value]) => url.searchParams.set(key, value));
67
- const body = yield* Schema.encodeUnknownEffect(Schema.fromJsonString(Request))({
68
- ...mergeJsonRecords(req.options, req.http?.body),
69
- model: req.model.id,
70
- state: req.state,
71
- questions: Object.fromEntries(Object.entries(req.questions).map(([id, x]) => [id, x.type === "boolean" ? { ...x, type: "noul" } : x])),
72
- }).pipe(Effect.mapError((cause) => new AIError({ reason: new InvalidRequestError({ message: cause.message, cause }) })));
73
- const headers = yield* Auth.toEffect(cfg.auth)({
74
- request: req,
75
- method: "POST",
76
- url: url.toString(),
77
- body,
78
- headers: Headers.fromInput({ ...cfg.headers, ...req.http?.headers }),
79
- });
80
- const res = yield* send(HttpClientRequest.post(url).pipe(HttpClientRequest.setHeaders(headers), HttpClientRequest.bodyText(body, "application/json")));
81
- const http = RequestExecutor.responseHttp(res);
82
- const fail = (message, cause, body) => new AIError({ reason: new InvalidProviderOutputError({ route: "system-one", message, body, http, cause }) });
83
- const text = yield* res.text.pipe(Effect.mapError((cause) => fail("Failed to read the System One response", cause)));
84
- const data = yield* Schema.decodeUnknownEffect(Schema.fromJsonString(Response))(text).pipe(Effect.mapError((cause) => fail("System One returned an invalid response", cause, text)));
85
- const legend = {};
86
- const answers = Object.fromEntries(Object.entries(data.answers).map(([id, answer]) => {
87
- if (answer.type === "noul")
88
- return [id, { type: "boolean", probability: answer.noul }];
89
- if (answer.type === "choice") {
90
- return [
91
- id,
92
- {
93
- type: "choice",
94
- choice: answer.choice,
95
- probabilities: answer.probabilities,
96
- ...(answer.confidence === undefined ? {} : { confidence: answer.confidence }),
97
- },
98
- ];
99
- }
100
- if (answer.legend !== undefined)
101
- legend[id] = answer.legend;
102
- return [
103
- id,
104
- {
105
- type: "score",
106
- score: answer.score,
107
- probabilities: answer.probabilities,
108
- ...(answer.confidence === undefined ? {} : { confidence: answer.confidence }),
109
- },
110
- ];
111
- }));
112
- const meta = {
113
- ...(data.id === undefined ? {} : { responseId: data.id }),
114
- ...(data.provider === undefined ? {} : { provider: data.provider }),
115
- ...data.provider_metadata?.[cfg.providerMetadataKey],
116
- ...(Object.keys(legend).length === 0 ? {} : { legend }),
117
- };
118
- return new EvaluationResponse({
119
- model: ModelID.make(data.model),
120
- answers,
121
- usage: data.usage
122
- ? new Usage({
123
- inputTokens: data.usage.input_tokens,
124
- outputTokens: data.usage.output_tokens,
125
- totalTokens: ProviderShared.totalTokens(data.usage.input_tokens, data.usage.output_tokens, undefined),
126
- providerMetadata: { [cfg.providerMetadataKey]: data.usage },
127
- })
128
- : undefined,
129
- rounding: new EvaluationRounding({ probabilityDecimals: 2, scoreDecimals: 2 }),
130
- providerMetadata: Object.keys(meta).length === 0 ? undefined : { [cfg.providerMetadataKey]: meta },
131
- });
132
- }),
133
- },
134
- });
135
- export const SystemOne = { model };