@opencode/ai 0.0.0-dev-21709 → 0.0.0-dev-21713
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/README.md
CHANGED
|
@@ -183,17 +183,21 @@ console.log(response.answers.refund.probability)
|
|
|
183
183
|
```
|
|
184
184
|
|
|
185
185
|
`TypeSafeAI` reads `TYPESAFE_API_KEY`. `OpenCode` exposes the same selector and reads
|
|
186
|
-
`OPENCODE_API_KEY`. OpenRouter
|
|
186
|
+
`OPENCODE_API_KEY`. OpenRouter, Vercel AI Gateway, and OpenAI use the same provider shape:
|
|
187
187
|
|
|
188
188
|
```ts
|
|
189
|
-
import { OpenRouter, VercelAIGateway } from "@opencode/ai/providers"
|
|
189
|
+
import { OpenAI, OpenRouter, VercelAIGateway } from "@opencode/ai/providers"
|
|
190
190
|
|
|
191
191
|
OpenRouter.configure().experimental.evaluation("typesafe/jev-1.13")
|
|
192
192
|
VercelAIGateway.configure().experimental.evaluation("typesafe-ai/jev")
|
|
193
|
+
OpenAI.configure().experimental.evaluation("gpt-6-luna")
|
|
193
194
|
```
|
|
194
195
|
|
|
195
196
|
OpenRouter reads `OPENROUTER_API_KEY`. Vercel reads `AI_GATEWAY_API_KEY`, then `VERCEL_OIDC_TOKEN`.
|
|
196
|
-
|
|
197
|
+
OpenAI reads `OPENAI_API_KEY` and calls `/v1/decisions`.
|
|
198
|
+
The common API uses `boolean`; System One routes lower it to native `noul`, and OpenAI lowers it to
|
|
199
|
+
`predicate`. OpenAI predicates take no criteria, so boolean criteria fail before the request, and a
|
|
200
|
+
refused question fails the call with a `ContentPolicy` error.
|
|
197
201
|
Choice and score answers include `confidence` when the provider returns it, such as
|
|
198
202
|
`response.answers.department.confidence`. Score legends remain available in provider metadata, and
|
|
199
203
|
the provider's rounded probabilities are returned unchanged.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { EvaluationModel, EvaluationResponse, type EvaluationRequestFor } from "./evaluation.js";
|
|
2
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
3
|
+
import { MediaRoute } from "../route/media.js";
|
|
4
|
+
import { type OpenString } from "../schema/index.js";
|
|
5
|
+
export declare const DEFAULT_BASE_URL = "https://api.openai.com/v1";
|
|
6
|
+
export declare const PATH = "/decisions";
|
|
7
|
+
/** Native body fields, sent as given. */
|
|
8
|
+
export type OpenAIDecisionOptions = {
|
|
9
|
+
readonly prompt_cache_key?: string;
|
|
10
|
+
readonly prompt_cache_options?: Readonly<{
|
|
11
|
+
mode?: OpenString<"implicit" | "explicit">;
|
|
12
|
+
ttl?: OpenString<"30m">;
|
|
13
|
+
}>;
|
|
14
|
+
readonly safety_identifier?: string;
|
|
15
|
+
} & Record<string, unknown>;
|
|
16
|
+
type Request = EvaluationRequestFor<OpenAIDecisionOptions>;
|
|
17
|
+
export declare const protocol: MediaProtocol.Inline<Request, EvaluationResponse>;
|
|
18
|
+
export declare const model: (input: MediaRoute.ModelInput) => EvaluationModel<OpenAIDecisionOptions>;
|
|
19
|
+
export declare const OpenAIDecisions: {
|
|
20
|
+
readonly protocol: MediaProtocol.Inline<Request, EvaluationResponse>;
|
|
21
|
+
readonly model: typeof model;
|
|
22
|
+
};
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { EvaluationModel, EvaluationResponse, EvaluationRounding, } from "./evaluation.js";
|
|
3
|
+
import { OpenResponses } from "../protocols/open-responses.js";
|
|
4
|
+
import { Endpoint } from "../route/endpoint.js";
|
|
5
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
6
|
+
import { MediaRoute } from "../route/media.js";
|
|
7
|
+
import { ModelID, mergeJsonRecords } from "../schema/index.js";
|
|
8
|
+
import { encodeJson } from "../utils/json.js";
|
|
9
|
+
const route = MediaProtocol.identity({ id: "openai-decisions", name: "OpenAI Decisions", provider: "openai" });
|
|
10
|
+
export const DEFAULT_BASE_URL = "https://api.openai.com/v1";
|
|
11
|
+
export const PATH = "/decisions";
|
|
12
|
+
const Probability = Schema.Number.check(Schema.isBetween({ minimum: 0, maximum: 1 }));
|
|
13
|
+
const Answer = Schema.Union([
|
|
14
|
+
Schema.Struct({ type: Schema.Literal("predicate"), name: Schema.String, probability: Probability }),
|
|
15
|
+
Schema.Struct({
|
|
16
|
+
type: Schema.Literal("choice"),
|
|
17
|
+
name: Schema.String,
|
|
18
|
+
choice: Schema.String,
|
|
19
|
+
probabilities: Schema.Array(Schema.Struct({ value: Schema.String, probability: Probability })),
|
|
20
|
+
confidence: Schema.optional(Probability),
|
|
21
|
+
}),
|
|
22
|
+
Schema.Struct({
|
|
23
|
+
type: Schema.Literal("score"),
|
|
24
|
+
name: Schema.String,
|
|
25
|
+
score: Schema.Number,
|
|
26
|
+
probabilities: Schema.Array(Schema.Struct({ value: Schema.Int, probability: Probability })),
|
|
27
|
+
confidence: Schema.optional(Probability),
|
|
28
|
+
}),
|
|
29
|
+
Schema.Struct({ type: Schema.Literal("refusal"), name: Schema.String }),
|
|
30
|
+
]);
|
|
31
|
+
const Response = Schema.Struct({
|
|
32
|
+
model: Schema.String,
|
|
33
|
+
answers: Schema.Array(Answer),
|
|
34
|
+
usage: Schema.optional(OpenResponses.OpenResponsesUsage),
|
|
35
|
+
});
|
|
36
|
+
const fromRequest = Effect.fn("OpenAIDecisions.fromRequest")(function* (request) {
|
|
37
|
+
const criteria = Object.entries(request.questions).flatMap(([name, question]) => question.type === "boolean" && question.criteria !== undefined ? [name] : []);
|
|
38
|
+
if (criteria.length > 0)
|
|
39
|
+
return yield* route.unsupported("evaluation.criteria", `${route.name} boolean questions take instructions only; remove criteria from ${criteria.join(", ")}`);
|
|
40
|
+
return MediaProtocol.json({
|
|
41
|
+
...mergeJsonRecords(request.options, request.http?.body),
|
|
42
|
+
model: request.model.id,
|
|
43
|
+
input: typeof request.state === "string" ? request.state : [{ role: "user", content: json(request.state) }],
|
|
44
|
+
questions: Object.entries(request.questions).map(([name, question]) => {
|
|
45
|
+
const instructions = typeof question.instructions === "string" ? question.instructions : json(question.instructions);
|
|
46
|
+
if (question.type === "boolean")
|
|
47
|
+
return { type: "predicate", name, instructions };
|
|
48
|
+
if (question.type === "choice")
|
|
49
|
+
return {
|
|
50
|
+
type: "choice",
|
|
51
|
+
name,
|
|
52
|
+
instructions,
|
|
53
|
+
choices: Object.entries(question.criteria).map(([value, description]) => ({
|
|
54
|
+
value,
|
|
55
|
+
...(description === null ? {} : { description: text(description) }),
|
|
56
|
+
})),
|
|
57
|
+
};
|
|
58
|
+
return {
|
|
59
|
+
type: "score",
|
|
60
|
+
name,
|
|
61
|
+
instructions,
|
|
62
|
+
levels: question.criteria.map((level, index) => typeof level === "string"
|
|
63
|
+
? { label: level }
|
|
64
|
+
: { label: String(index), ...(level === null ? {} : { description: text(level) }) }),
|
|
65
|
+
};
|
|
66
|
+
}),
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
const json = (data) => [{ type: "input_json", data }];
|
|
70
|
+
const text = (value) => (typeof value === "string" ? value : encodeJson(value));
|
|
71
|
+
const decodeDocument = route.decodeJson(Response);
|
|
72
|
+
const decodeResponse = Effect.fn("OpenAIDecisions.decodeResponse")(function* (response) {
|
|
73
|
+
const output = yield* decodeDocument(response);
|
|
74
|
+
const refused = output.value.answers.flatMap((answer) => (answer.type === "refusal" ? [answer.name] : []));
|
|
75
|
+
if (refused.length > 0)
|
|
76
|
+
return yield* output.contentPolicy(`${route.name} refused to answer ${refused.join(", ")}`);
|
|
77
|
+
return new EvaluationResponse({
|
|
78
|
+
model: ModelID.make(output.value.model),
|
|
79
|
+
answers: Object.fromEntries(output.value.answers.flatMap((answer) => {
|
|
80
|
+
if (answer.type === "refusal")
|
|
81
|
+
return [];
|
|
82
|
+
if (answer.type === "predicate")
|
|
83
|
+
return [[answer.name, { type: "boolean", probability: answer.probability }]];
|
|
84
|
+
const probabilities = Object.fromEntries(answer.probabilities.map((item) => [item.value, item.probability]));
|
|
85
|
+
const confidence = answer.confidence === undefined ? {} : { confidence: answer.confidence };
|
|
86
|
+
if (answer.type === "choice")
|
|
87
|
+
return [[answer.name, { type: "choice", choice: answer.choice, probabilities, ...confidence }]];
|
|
88
|
+
return [[answer.name, { type: "score", score: answer.score, probabilities, ...confidence }]];
|
|
89
|
+
})),
|
|
90
|
+
usage: OpenResponses.mapUsage(output.value.usage, "openai"),
|
|
91
|
+
rounding: new EvaluationRounding({ probabilityDecimals: 2, scoreDecimals: 2 }),
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
export const protocol = MediaProtocol.inline(route, {
|
|
95
|
+
body: { from: fromRequest },
|
|
96
|
+
response: { decode: decodeResponse },
|
|
97
|
+
});
|
|
98
|
+
export const model = (input) => {
|
|
99
|
+
const composed = MediaRoute.inline({
|
|
100
|
+
protocol,
|
|
101
|
+
endpoint: Endpoint.path(PATH, { baseURL: input.baseURL ?? DEFAULT_BASE_URL }),
|
|
102
|
+
auth: input.auth,
|
|
103
|
+
headers: input.headers,
|
|
104
|
+
});
|
|
105
|
+
return EvaluationModel.make({
|
|
106
|
+
id: input.id,
|
|
107
|
+
provider: protocol.provider,
|
|
108
|
+
http: input.http,
|
|
109
|
+
route: { id: composed.id, evaluate: composed.generate },
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
export const OpenAIDecisions = {
|
|
113
|
+
protocol,
|
|
114
|
+
model,
|
|
115
|
+
};
|
|
@@ -8,6 +8,7 @@ export type { OpenAIOptionsInput, OpenAIResponseIncludable } from "./openai-opti
|
|
|
8
8
|
export type { OpenAIImageOptions } from "../protocols/openai-images.js";
|
|
9
9
|
export type { OpenAISpeechOptions } from "../protocols/openai-speech.js";
|
|
10
10
|
export type { OpenAITranscriptionOptions } from "../protocols/openai-transcription.js";
|
|
11
|
+
export type { OpenAIDecisionOptions } from "../experimental/openai-decisions.js";
|
|
11
12
|
export declare const id: string & import("effect/Brand").Brand<"AI.ProviderID">;
|
|
12
13
|
export declare const routes: (Route<{
|
|
13
14
|
readonly model: string;
|
|
@@ -374,6 +375,9 @@ export declare const configure: (input?: Config) => {
|
|
|
374
375
|
image: (modelID: string | ModelID) => import("../image.js").ImageModel<import("../protocols/openai-images.js").OpenAIImageOptions>;
|
|
375
376
|
speech: (modelID: string | ModelID) => import("../speech.js").SpeechModel<import("../protocols/openai-speech.js").OpenAISpeechOptions>;
|
|
376
377
|
transcription: (modelID: string | ModelID) => import("../transcription.js").TranscriptionModel<import("../protocols/openai-transcription.js").OpenAITranscriptionOptions>;
|
|
378
|
+
experimental: {
|
|
379
|
+
evaluation: (modelID: string | ModelID) => import("../experimental.js").EvaluationModel<import("../experimental/openai-decisions.js").OpenAIDecisionOptions>;
|
|
380
|
+
};
|
|
377
381
|
configure: typeof configure;
|
|
378
382
|
};
|
|
379
383
|
export declare const provider: {
|
|
@@ -390,6 +394,9 @@ export declare const provider: {
|
|
|
390
394
|
image: (modelID: string | ModelID) => import("../image.js").ImageModel<import("../protocols/openai-images.js").OpenAIImageOptions>;
|
|
391
395
|
speech: (modelID: string | ModelID) => import("../speech.js").SpeechModel<import("../protocols/openai-speech.js").OpenAISpeechOptions>;
|
|
392
396
|
transcription: (modelID: string | ModelID) => import("../transcription.js").TranscriptionModel<import("../protocols/openai-transcription.js").OpenAITranscriptionOptions>;
|
|
397
|
+
experimental: {
|
|
398
|
+
evaluation: (modelID: string | ModelID) => import("../experimental.js").EvaluationModel<import("../experimental/openai-decisions.js").OpenAIDecisionOptions>;
|
|
399
|
+
};
|
|
393
400
|
configure: typeof configure;
|
|
394
401
|
};
|
|
395
402
|
export declare const model: ProviderPackage.Definition<Settings, OpenAIProviderOptionsInput, typeof OpenAIResponses.route.compact>["model"];
|
|
@@ -402,3 +409,6 @@ export declare const chat: (id: string | ModelID) => import("../schema/options.j
|
|
|
402
409
|
export declare const image: (modelID: string | ModelID) => import("../image.js").ImageModel<import("../protocols/openai-images.js").OpenAIImageOptions>;
|
|
403
410
|
export declare const speech: (modelID: string | ModelID) => import("../speech.js").SpeechModel<import("../protocols/openai-speech.js").OpenAISpeechOptions>;
|
|
404
411
|
export declare const transcription: (modelID: string | ModelID) => import("../transcription.js").TranscriptionModel<import("../protocols/openai-transcription.js").OpenAITranscriptionOptions>;
|
|
412
|
+
export declare const experimental: {
|
|
413
|
+
evaluation: (modelID: string | ModelID) => import("../experimental.js").EvaluationModel<import("../experimental/openai-decisions.js").OpenAIDecisionOptions>;
|
|
414
|
+
};
|
package/dist/providers/openai.js
CHANGED
|
@@ -7,6 +7,7 @@ import { withOpenAIOptions } from "./openai-options.js";
|
|
|
7
7
|
import { OpenAIImages } from "../protocols/openai-images.js";
|
|
8
8
|
import { OpenAISpeech } from "../protocols/openai-speech.js";
|
|
9
9
|
import { OpenAITranscription } from "../protocols/openai-transcription.js";
|
|
10
|
+
import { OpenAIDecisions } from "../experimental/openai-decisions.js";
|
|
10
11
|
export const id = ProviderID.make("openai");
|
|
11
12
|
export const routes = [OpenAIResponses.route, OpenAIChat.route];
|
|
12
13
|
export const imageGeneration = (options = {}) => ToolDefinition.make({
|
|
@@ -55,6 +56,7 @@ export const configure = (input = {}) => {
|
|
|
55
56
|
const image = (modelID) => OpenAIImages.model({ ...media, id: modelID });
|
|
56
57
|
const speech = (modelID) => OpenAISpeech.model({ ...media, id: modelID });
|
|
57
58
|
const transcription = (modelID) => OpenAITranscription.model({ ...media, id: modelID });
|
|
59
|
+
const evaluation = (modelID) => OpenAIDecisions.model({ ...media, id: modelID });
|
|
58
60
|
return {
|
|
59
61
|
id,
|
|
60
62
|
model: responses,
|
|
@@ -63,6 +65,7 @@ export const configure = (input = {}) => {
|
|
|
63
65
|
image,
|
|
64
66
|
speech,
|
|
65
67
|
transcription,
|
|
68
|
+
experimental: { evaluation },
|
|
66
69
|
configure,
|
|
67
70
|
};
|
|
68
71
|
};
|
|
@@ -91,3 +94,4 @@ export const chat = provider.chat;
|
|
|
91
94
|
export const image = provider.image;
|
|
92
95
|
export const speech = provider.speech;
|
|
93
96
|
export const transcription = provider.transcription;
|
|
97
|
+
export const experimental = provider.experimental;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
|
-
"version": "0.0.0-dev-
|
|
3
|
+
"version": "0.0.0-dev-21713",
|
|
4
4
|
"name": "@opencode/ai",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@clack/prompts": "1.0.0-alpha.1",
|
|
37
37
|
"@effect/platform-node": "4.0.1",
|
|
38
|
-
"@opencode/http-recorder": "0.0.0-dev-
|
|
38
|
+
"@opencode/http-recorder": "0.0.0-dev-21713",
|
|
39
39
|
"@tsconfig/bun": "1.0.9",
|
|
40
40
|
"@types/bun": "1.4.3",
|
|
41
41
|
"@typescript/native": "npm:typescript@7.1.0-beta",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"@aws-sdk/credential-providers": "3.1057.0",
|
|
46
46
|
"@smithy/eventstream-codec": "4.2.14",
|
|
47
47
|
"@smithy/util-utf8": "4.2.2",
|
|
48
|
-
"@opencode/schema": "0.0.0-dev-
|
|
48
|
+
"@opencode/schema": "0.0.0-dev-21713",
|
|
49
49
|
"aws4fetch": "1.0.20",
|
|
50
50
|
"effect": "4.0.1",
|
|
51
51
|
"google-auth-library": "10.5.0"
|