The best way to control Claude Code, Codex and more from any computer or phone

main
ts 154 lines 5.57 KB
Raw
1 /**
2 * Cloudflare Clef implementation of DecisionModel over Workers AI REST.
3 *
4 * @stability unstable
5 * @since 4.0.0
6 */
7 import * as AiError from "effect/ai/AiError"
8 import * as DecisionModel from "effect/ai/DecisionModel"
9 import * as AiModel from "effect/ai/Model"
10 import * as Effect from "effect/Effect"
11 import * as Layer from "effect/Layer"
12 import type * as Schema from "effect/Schema"
13 import { CloudflareClient } from "./CloudflareClient.ts"
14 import type * as CloudflareSchema from "./CloudflareSchema.ts"
15
16 /**
17 * Cloudflare Clef model identifiers.
18 *
19 * @stability unstable
20 * @category models
21 * @since 4.0.0
22 */
23 export type Model = typeof CloudflareSchema.DecisionsRequest.Encoded["model"]
24
25 /**
26 * Creates a decision model with Cloudflare provider metadata.
27 *
28 * @stability unstable
29 * @category constructors
30 * @since 4.0.0
31 */
32 export const model = (
33 model: Model
34 ): AiModel.Model<"cloudflare", DecisionModel.DecisionModel, CloudflareClient> =>
35 AiModel.make("cloudflare", model, layer({ model }))
36
37 /**
38 * Builds a decision service with tolerance for four-decimal probability rounding.
39 *
40 * **Details**
41 *
42 * Base64 data is sent with its media type, and `data:` URLs are sent
43 * unchanged. Clef shows images before the state, so with images the state is
44 * sent as text that first lists each image's reference in order. The API
45 * enforces image limits.
46 *
47 * **Gotchas**
48 *
49 * - Remote URLs fail with `AiError.InvalidUserInputError` because Clef does
50 * not fetch them.
51 *
52 * @stability unstable
53 * @category constructors
54 * @since 4.0.0
55 */
56 export const make = Effect.fnUntraced(
57 function*(
58 options: { readonly model: Model }
59 ): Effect.fn.Return<DecisionModel.DecisionModel, never, CloudflareClient> {
60 const client = yield* CloudflareClient
61 return yield* DecisionModel.make({
62 probabilityPrecision: 4,
63 supportsImages: true,
64 decide: Effect.fnUntraced(function*({ state, decisions, images }) {
65 const questions: Record<string, typeof CloudflareSchema.Question.Encoded> = Object.create(null)
66 for (const [key, decision] of Object.entries(decisions)) {
67 switch (decision._tag) {
68 case "Classify":
69 questions[key] = { type: "choice", instructions: decision.instructions, criteria: decision.criteria }
70 break
71 case "Rate":
72 questions[key] = {
73 type: "score",
74 instructions: decision.instructions,
75 criteria: decision.criteria.map((level) => level.description)
76 }
77 break
78 case "Probability":
79 questions[key] = { type: "noul", instructions: decision.instructions, criteria: decision.criteria }
80 break
81 }
82 }
83 if (images?.some(({ image }) => image.data instanceof URL && image.data.protocol !== "data:")) {
84 return yield* Effect.fail(AiError.make({
85 module: "CloudflareDecisionModel",
86 method: "decide",
87 reason: new AiError.InvalidUserInputError({
88 description: "Cloudflare Clef accepts base64 or data URL images, not remote URLs"
89 })
90 }))
91 }
92 const response = yield* client.createDecisions({
93 model: options.model,
94 state: images === undefined ? state : labelImages(state, images),
95 questions,
96 ...(images === undefined ? undefined : { images: images.map(({ image }) => encodeImage(image)) })
97 })
98 const answers: Record<string, DecisionModel.ProviderAnswer> = Object.create(null)
99 for (const [key, decision] of Object.entries(decisions)) {
100 const answer = response.answers[key]
101 switch (answer?.type) {
102 case "choice":
103 answers[key] = {
104 _tag: "Classify",
105 label: answer.choice,
106 probabilities: answer.probabilities,
107 confidence: answer.confidence
108 }
109 break
110 case "score": {
111 const levels = decision._tag === "Rate" ? decision.criteria : []
112 const probabilities: Record<string, number> = Object.create(null)
113 for (let index = 0; index < levels.length; index++) {
114 const probability = answer.probabilities[String(index)]
115 if (probability !== undefined) probabilities[levels[index].label] = probability
116 }
117 answers[key] = { _tag: "Rate", rating: answer.score, probabilities, confidence: answer.confidence }
118 break
119 }
120 case "noul":
121 answers[key] = { _tag: "Probability", probability: answer.noul }
122 break
123 }
124 }
125 return {
126 answers,
127 usage: { inputTokens: response.usage.input_tokens, outputTokens: response.usage.output_tokens }
128 }
129 })
130 })
131 }
132 )
133
134 const encodeImage = (image: DecisionModel.ProviderImage["image"]): typeof CloudflareSchema.Image.Encoded =>
135 image.data instanceof URL ? image.data.href : { content_type: image.mediaType, base64: image.data }
136
137 const labelImages = (state: Schema.Json, images: ReadonlyArray<DecisionModel.ProviderImage>): string =>
138 [
139 ...images.map(({ reference }, index) => `Image ${index + 1}: ${reference}`),
140 "",
141 typeof state === "string" ? state : JSON.stringify(state)
142 ].join("\n")
143
144 /**
145 * Provides DecisionModel using an existing CloudflareClient.
146 *
147 * @stability unstable
148 * @category layers
149 * @since 4.0.0
150 */
151 export const layer = (
152 options: { readonly model: Model }
153 ): Layer.Layer<DecisionModel.DecisionModel, never, CloudflareClient> =>
154 Layer.effect(DecisionModel.DecisionModel, make(options))