@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 198 lines 6.98 KB
Raw
1 /**
2 * OpenAI implementation of DecisionModel over the Decisions API.
3 *
4 * @stability unstable
5 * @since 4.0.5
6 */
7 import * as AiError from "effect/ai/AiError"
8 import type * as Decision from "effect/ai/Decision"
9 import * as DecisionModel from "effect/ai/DecisionModel"
10 import * as AiModel from "effect/ai/Model"
11 import * as Effect from "effect/Effect"
12 import * as Layer from "effect/Layer"
13 import * as Record from "effect/Record"
14 import { OpenAiClient } from "./OpenAiClient.ts"
15 import type * as OpenAiSchema from "./OpenAiSchema.ts"
16
17 /**
18 * OpenAI decision model identifiers.
19 *
20 * @stability unstable
21 * @category models
22 * @since 4.0.5
23 */
24 export type Model = typeof OpenAiSchema.CreateDecisionsRequest.Encoded["model"]
25
26 /**
27 * Creates a decision model with OpenAI provider metadata.
28 *
29 * @stability unstable
30 * @category constructors
31 * @since 4.0.5
32 */
33 export const model = (
34 model: Model
35 ): AiModel.Model<"openai", DecisionModel.DecisionModel, OpenAiClient> => AiModel.make("openai", model, layer({ model }))
36
37 /**
38 * Builds a decision service with tolerance for two-decimal probability rounding.
39 *
40 * **Details**
41 *
42 * String state is sent as `input` unchanged, and other state is sent as JSON.
43 * With images, `input` becomes one user message with the state as text,
44 * followed by each image after a label naming the reference that replaces it
45 * in the state. Base64 data is sent as `data:` URLs, and `data:` URLs are sent
46 * unchanged.
47 *
48 * Probability criteria are appended to the question instructions, and each
49 * rating level is sent with its label and description.
50 * String instructions and descriptions are unchanged; structured values and
51 * null descriptions are serialized with `JSON.stringify`.
52 *
53 * **Gotchas**
54 *
55 * - Remote URLs fail with `AiError.InvalidUserInputError` because OpenAI only
56 * accepts inline images.
57 * - Refusals yield `"Refused"` with `decideSettled`; `decide` fails with
58 * `AiError.ContentPolicyError` naming the refused decisions.
59 *
60 * @stability unstable
61 * @category constructors
62 * @since 4.0.5
63 */
64 export const make = Effect.fnUntraced(
65 function*(
66 options: { readonly model: Model }
67 ): Effect.fn.Return<DecisionModel.DecisionModel, never, OpenAiClient> {
68 const client = yield* OpenAiClient
69 return yield* DecisionModel.make({
70 probabilityPrecision: 2,
71 supportsImages: true,
72 decide: Effect.fnUntraced(function*({ state, decisions, images }) {
73 if (images?.some(({ image }) => image.data instanceof URL && image.data.protocol !== "data:")) {
74 return yield* invalidUserInput("OpenAI decisions accept base64 or data URL images, not remote URLs")
75 }
76 const questions: Array<typeof OpenAiSchema.DecisionQuestion.Encoded> = []
77 for (const [name, decision] of Object.entries(decisions)) {
78 questions.push(encodeQuestion(name, decision))
79 }
80 const text = typeof state === "string" ? state : JSON.stringify(state)
81 const response = yield* client.createDecisions({
82 model: options.model,
83 input: images === undefined ? text : [{
84 role: "user",
85 content: [
86 { type: "input_text", text },
87 ...images.flatMap(({ reference, image }) => [
88 { type: "input_text" as const, text: `Image ${reference}:` },
89 { type: "input_image" as const, image_url: encodeImage(image) }
90 ])
91 ]
92 }],
93 questions
94 })
95 const answers: Record<string, DecisionModel.ProviderAnswer> = Object.create(null)
96 for (const answer of response.answers) {
97 if (answers[answer.name]?._tag === "Refusal") continue
98 switch (answer.type) {
99 case "refusal":
100 answers[answer.name] = { _tag: "Refusal" }
101 break
102 case "choice":
103 answers[answer.name] = {
104 _tag: "Classify",
105 label: answer.choice,
106 probabilities: Record.fromIterableWith(answer.probabilities, (p) => [p.value, p.probability]),
107 confidence: answer.confidence
108 }
109 break
110 case "score": {
111 const decision = decisions[answer.name]
112 const levels = decision?._tag === "Rate" ? decision.criteria : []
113 const probabilities: Record<string, number> = Object.create(null)
114 for (const { value, probability } of answer.probabilities) {
115 const level = levels[value]
116 if (level !== undefined) probabilities[level.label] = probability
117 }
118 answers[answer.name] = {
119 _tag: "Rate",
120 rating: answer.score,
121 probabilities,
122 confidence: answer.confidence
123 }
124 break
125 }
126 case "predicate":
127 answers[answer.name] = { _tag: "Probability", probability: answer.probability }
128 break
129 }
130 }
131 return {
132 answers,
133 usage: { inputTokens: response.usage?.input_tokens, outputTokens: response.usage?.output_tokens }
134 }
135 })
136 })
137 }
138 )
139
140 const invalidUserInput = (description: string) =>
141 Effect.fail(AiError.make({
142 module: "OpenAiDecisionModel",
143 method: "decide",
144 reason: new AiError.InvalidUserInputError({ description })
145 }))
146
147 const encodeQuestion = (
148 name: string,
149 decision: Exclude<Decision.Any, Decision.Locate>
150 ): typeof OpenAiSchema.DecisionQuestion.Encoded => {
151 const instructions = encodeDescription(decision.instructions)
152 switch (decision._tag) {
153 case "Classify": {
154 const choices = Object.entries(decision.criteria).map(([value, description]) => ({
155 value,
156 description: encodeDescription(description)
157 }))
158 return { type: "choice", name, instructions, choices }
159 }
160 case "Rate":
161 return {
162 type: "score",
163 name,
164 instructions,
165 levels: decision.criteria.map(({ label, description }) => ({
166 label,
167 description: encodeDescription(description)
168 }))
169 }
170 case "Probability": {
171 if (decision.criteria === undefined) return { type: "predicate", name, instructions }
172 const { false: no, true: yes } = decision.criteria
173 return {
174 type: "predicate",
175 name,
176 instructions: `${instructions}\n\nfalse: ${encodeDescription(no)}\ntrue: ${encodeDescription(yes)}`
177 }
178 }
179 }
180 }
181
182 const encodeDescription = (description: Decision.Description | null): string =>
183 typeof description === "string" ? description : JSON.stringify(description)
184
185 const encodeImage = (image: DecisionModel.ProviderImage["image"]): string =>
186 image.data instanceof URL ? image.data.href : `data:${image.mediaType};base64,${image.data}`
187
188 /**
189 * Provides DecisionModel using an existing OpenAiClient.
190 *
191 * @stability unstable
192 * @category layers
193 * @since 4.0.5
194 */
195 export const layer = (
196 options: { readonly model: Model }
197 ): Layer.Layer<DecisionModel.DecisionModel, never, OpenAiClient> =>
198 Layer.effect(DecisionModel.DecisionModel, make(options))