@opencode/ai 0.0.0-dev-21908 → 0.0.0-dev-21920
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 +32 -0
- package/dist/decision.d.ts +4 -3
- package/dist/decision.js +4 -3
- package/dist/experimental/cloudflare-run-extraction.d.ts +13 -0
- package/dist/experimental/cloudflare-run-extraction.js +83 -0
- package/dist/experimental/extraction.d.ts +183 -0
- package/dist/experimental/extraction.js +102 -0
- package/dist/experimental.d.ts +2 -0
- package/dist/experimental.js +1 -0
- package/dist/protocols/cloudflare-run-decisions.js +1 -1
- package/dist/protocols/system-one.d.ts +5 -5
- package/dist/protocols/system-one.js +6 -6
- package/dist/providers/baseten.js +1 -1
- package/dist/providers/cloudflare-workers-ai.d.ts +5 -0
- package/dist/providers/cloudflare-workers-ai.js +6 -4
- package/dist/providers/openrouter.js +1 -1
- package/dist/utils/media-type.js +4 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -222,6 +222,38 @@ CloudflareAIGateway.configure({ accountId }).decision("typesafe/jev")
|
|
|
222
222
|
OpenAI and Vercel AI Gateway use OpenAI's `/v1/decisions` API. TypeSafe, OpenCode, OpenRouter, Baseten and
|
|
223
223
|
DigitalOcean use System One (`/systemone`), and Cloudflare sends the System One body through `/ai/run`.
|
|
224
224
|
|
|
225
|
+
### Extraction (experimental)
|
|
226
|
+
|
|
227
|
+
Extraction models read typed values out of text instead of answering questions with probabilities. A field of type
|
|
228
|
+
`text`, `number`, `date`, `time`, `datetime`, `duration`, `list`, `estimate` or `forecast` gets an answer typed to
|
|
229
|
+
match; other types are sent and answered as-is. Only Cloudflare's Clairic serves it today, so it lives under
|
|
230
|
+
`@opencode/ai/experimental`.
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
import { ai } from "@opencode/ai"
|
|
234
|
+
import { Extraction } from "@opencode/ai/experimental"
|
|
235
|
+
import { CloudflareWorkersAI } from "@opencode/ai/providers"
|
|
236
|
+
|
|
237
|
+
const response = await ai.run(
|
|
238
|
+
Extraction.generate({
|
|
239
|
+
model: CloudflareWorkersAI.configure({ accountId }).experimental.extraction("@cf/cloudflare/clairic"),
|
|
240
|
+
input: "Invoice INV-2291, issued 2026-09-02, net 30. Total $1,080.00. Contact: Dana Ruiz, Priya Shah.",
|
|
241
|
+
fields: {
|
|
242
|
+
invoiceNumber: { type: "text" },
|
|
243
|
+
dueDate: { type: "date", instructions: "Issue date plus the net payment days" },
|
|
244
|
+
total: { type: "number", decimals: 2, unit: "USD" },
|
|
245
|
+
contacts: { type: "list", items: "text" },
|
|
246
|
+
},
|
|
247
|
+
}),
|
|
248
|
+
)
|
|
249
|
+
response.answers.total.value // 1080, or null when the input does not say
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
Value answers carry `guess`, `nullProbability` and `confidence`; text answers carry a `span` into `input`. Estimates and
|
|
253
|
+
forecasts return `median`, `p10` and `p90`. Field options other than `type`, `instructions` and `nullThreshold` go to
|
|
254
|
+
Clairic as written. Effect users provide `RequestExecutor.fetchLayer` (or `AIClient.layer`). Images are not supported
|
|
255
|
+
yet.
|
|
256
|
+
|
|
225
257
|
## Alibaba Cloud Model Studio
|
|
226
258
|
|
|
227
259
|
`Alibaba` provides standard Model Studio inference. Configure a region explicitly, then select
|
package/dist/decision.d.ts
CHANGED
|
@@ -244,10 +244,11 @@ export interface DecisionModelRoute<Options extends DecisionOptions = DecisionOp
|
|
|
244
244
|
}
|
|
245
245
|
declare const DecisionModelCompatibility_base: Schema.Class<DecisionModelCompatibility, Schema.Struct<{
|
|
246
246
|
/**
|
|
247
|
-
* Where a System One host reads media, from models.dev `[api.systemone] compatibility.
|
|
248
|
-
* `assets` (TypeSafe's assets map
|
|
247
|
+
* Where a System One host reads media, from models.dev `[api.systemone] compatibility.media_input`, named after
|
|
248
|
+
* where the media goes in the body: `assets` (TypeSafe's assets map, referenced from the state), `state` (items
|
|
249
|
+
* inside a state list) or `root` (one top-level list per kind: `images`, `audio`, `videos`).
|
|
249
250
|
*/
|
|
250
|
-
readonly
|
|
251
|
+
readonly mediaInput: Schema.optional<Schema.Literals<readonly ["assets", "state", "root"]>>;
|
|
251
252
|
}>, {}>;
|
|
252
253
|
/** Host quirks a protocol adapts to, like `LanguageModelCompatibility` for language models. */
|
|
253
254
|
export declare class DecisionModelCompatibility extends DecisionModelCompatibility_base {
|
package/dist/decision.js
CHANGED
|
@@ -75,10 +75,11 @@ export const DecisionAnswer = Schema.Union([PredicateAnswer, ChoiceAnswer, Score
|
|
|
75
75
|
/** Host quirks a protocol adapts to, like `LanguageModelCompatibility` for language models. */
|
|
76
76
|
export class DecisionModelCompatibility extends Schema.Class("Decision.ModelCompatibility")({
|
|
77
77
|
/**
|
|
78
|
-
* Where a System One host reads media, from models.dev `[api.systemone] compatibility.
|
|
79
|
-
* `assets` (TypeSafe's assets map
|
|
78
|
+
* Where a System One host reads media, from models.dev `[api.systemone] compatibility.media_input`, named after
|
|
79
|
+
* where the media goes in the body: `assets` (TypeSafe's assets map, referenced from the state), `state` (items
|
|
80
|
+
* inside a state list) or `root` (one top-level list per kind: `images`, `audio`, `videos`).
|
|
80
81
|
*/
|
|
81
|
-
|
|
82
|
+
mediaInput: Schema.optional(Schema.Literals(["assets", "state", "root"])),
|
|
82
83
|
}) {
|
|
83
84
|
}
|
|
84
85
|
(function (DecisionModelCompatibility) {
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ExtractionModel, ExtractionResponse, type ExtractionRequestFor } from "./extraction.js";
|
|
2
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
3
|
+
import { MediaRoute } from "../route/media.js";
|
|
4
|
+
/** Posts Clairic's `{ model, state, fields }` body to Cloudflare's `/ai/run/{model}`. */
|
|
5
|
+
export declare const protocol: MediaProtocol.Inline<ExtractionRequestFor, ExtractionResponse>;
|
|
6
|
+
/** `baseURL` is the Run API base, `.../client/v4/accounts/{account}/ai/run`. */
|
|
7
|
+
export declare const model: (input: MediaRoute.ModelInput) => ExtractionModel<{
|
|
8
|
+
readonly [x: string]: unknown;
|
|
9
|
+
}>;
|
|
10
|
+
export declare const CloudflareRunExtraction: {
|
|
11
|
+
readonly protocol: MediaProtocol.Inline<ExtractionRequestFor, ExtractionResponse>;
|
|
12
|
+
readonly model: typeof model;
|
|
13
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { ExtractionAnswers, ExtractionModel, ExtractionResponse } from "./extraction.js";
|
|
3
|
+
import { JsonObject, ProviderShared } from "../protocols/shared.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 { Usage, mergeJsonRecords } from "../schema/index.js";
|
|
8
|
+
const route = MediaProtocol.identity({
|
|
9
|
+
id: "cloudflare-run-extraction",
|
|
10
|
+
name: "Cloudflare Run",
|
|
11
|
+
provider: "cloudflare-workers-ai",
|
|
12
|
+
});
|
|
13
|
+
const decodeEnvelope = route.decodeJson(Schema.Struct({
|
|
14
|
+
result: Schema.Struct({
|
|
15
|
+
model: Schema.String,
|
|
16
|
+
answers: Schema.Record(Schema.String, JsonObject),
|
|
17
|
+
truncated: Schema.optional(Schema.Boolean),
|
|
18
|
+
state_text: Schema.optional(Schema.String),
|
|
19
|
+
usage: Schema.optional(Schema.StructWithRest(Schema.Struct({ input_tokens: Schema.optional(Schema.Number), output_tokens: Schema.optional(Schema.Number) }), [JsonObject])),
|
|
20
|
+
}),
|
|
21
|
+
}));
|
|
22
|
+
/** Posts Clairic's `{ model, state, fields }` body to Cloudflare's `/ai/run/{model}`. */
|
|
23
|
+
export const protocol = MediaProtocol.inline(route, {
|
|
24
|
+
body: {
|
|
25
|
+
from: (request) => Effect.succeed(MediaProtocol.json({
|
|
26
|
+
...mergeJsonRecords(request.providerOptions, request.http?.body),
|
|
27
|
+
model: request.model.id,
|
|
28
|
+
state: request.input,
|
|
29
|
+
fields: Object.fromEntries(Object.entries(request.fields).map(([name, field]) => {
|
|
30
|
+
const { nullThreshold, ...rest } = field;
|
|
31
|
+
return [name, nullThreshold === undefined ? rest : { ...rest, null_threshold: nullThreshold }];
|
|
32
|
+
})),
|
|
33
|
+
...(request.referenceDate === undefined ? {} : { reference_date: request.referenceDate }),
|
|
34
|
+
})),
|
|
35
|
+
},
|
|
36
|
+
response: {
|
|
37
|
+
decode: Effect.fn("CloudflareRunExtraction.decodeResponse")(function* (response, context) {
|
|
38
|
+
const output = yield* decodeEnvelope(response);
|
|
39
|
+
const result = output.value.result;
|
|
40
|
+
// Each answer decodes as the field it answers; extra answers are dropped.
|
|
41
|
+
const answers = yield* Effect.forEach(Object.entries(context.request.fields), ([name, field]) => {
|
|
42
|
+
if (!Object.hasOwn(result.answers, name))
|
|
43
|
+
return Effect.fail(output.invalid(`${route.name} returned no answer for ${name}`));
|
|
44
|
+
if (!Object.hasOwn(ExtractionAnswers, field.type))
|
|
45
|
+
return Effect.succeed([name, result.answers[name]]);
|
|
46
|
+
return Schema.decodeUnknownEffect(ExtractionAnswers[field.type])(result.answers[name]).pipe(Effect.map((answer) => [name, answer]), Effect.mapError((cause) => output.invalid(`${route.name} returned an invalid answer for ${name}`, cause)));
|
|
47
|
+
});
|
|
48
|
+
return new ExtractionResponse({
|
|
49
|
+
answers: Object.fromEntries(answers),
|
|
50
|
+
usage: result.usage === undefined
|
|
51
|
+
? undefined
|
|
52
|
+
: new Usage({
|
|
53
|
+
inputTokens: result.usage.input_tokens,
|
|
54
|
+
outputTokens: result.usage.output_tokens,
|
|
55
|
+
totalTokens: ProviderShared.totalTokens(result.usage.input_tokens, result.usage.output_tokens, undefined),
|
|
56
|
+
providerMetadata: { [route.provider]: result.usage },
|
|
57
|
+
}),
|
|
58
|
+
providerMetadata: {
|
|
59
|
+
[route.provider]: {
|
|
60
|
+
model: result.model,
|
|
61
|
+
...(result.truncated === undefined ? {} : { truncated: result.truncated }),
|
|
62
|
+
...(result.state_text === undefined ? {} : { stateText: result.state_text }),
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}),
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
/** `baseURL` is the Run API base, `.../client/v4/accounts/{account}/ai/run`. */
|
|
70
|
+
export const model = (input) => new ExtractionModel({
|
|
71
|
+
id: input.id,
|
|
72
|
+
provider: route.provider,
|
|
73
|
+
http: input.http,
|
|
74
|
+
route: MediaRoute.inline({
|
|
75
|
+
protocol,
|
|
76
|
+
endpoint: Endpoint.path(({ request }) => `/${request.model.id}`, {
|
|
77
|
+
baseURL: input.baseURL,
|
|
78
|
+
}),
|
|
79
|
+
auth: input.auth,
|
|
80
|
+
headers: input.headers,
|
|
81
|
+
}),
|
|
82
|
+
});
|
|
83
|
+
export const CloudflareRunExtraction = { protocol, model };
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { MediaModel } from "../media-model.js";
|
|
3
|
+
import { RequestExecutor } from "../route/executor.js";
|
|
4
|
+
import type { MediaRoute } from "../route/media.js";
|
|
5
|
+
import { AIError, HttpOptions, Usage, type OpenString } from "../schema/index.js";
|
|
6
|
+
/**
|
|
7
|
+
* The answer schema for each field type Clairic documents, decoding its wire keys (`p_null` and friends). Answers to
|
|
8
|
+
* other field types pass through as sent.
|
|
9
|
+
*/
|
|
10
|
+
export declare const ExtractionAnswers: {
|
|
11
|
+
text: Schema.encodeKeys<Schema.Struct<{
|
|
12
|
+
readonly value: Schema.NullOr<Schema.String>;
|
|
13
|
+
readonly guess: Schema.optional<Schema.String>;
|
|
14
|
+
readonly nullProbability: Schema.optional<Schema.Number>;
|
|
15
|
+
readonly confidence: Schema.optional<Schema.Number>;
|
|
16
|
+
readonly span: Schema.optional<Schema.Tuple<readonly [Schema.Int, Schema.Int]>>;
|
|
17
|
+
}>, {
|
|
18
|
+
readonly nullProbability: "p_null";
|
|
19
|
+
}>;
|
|
20
|
+
number: Schema.encodeKeys<Schema.Struct<{
|
|
21
|
+
readonly value: Schema.NullOr<Schema.Number>;
|
|
22
|
+
readonly guess: Schema.optional<Schema.Number>;
|
|
23
|
+
readonly nullProbability: Schema.optional<Schema.Number>;
|
|
24
|
+
readonly confidence: Schema.optional<Schema.Number>;
|
|
25
|
+
readonly span: Schema.optional<Schema.Tuple<readonly [Schema.Int, Schema.Int]>>;
|
|
26
|
+
}>, {
|
|
27
|
+
readonly nullProbability: "p_null";
|
|
28
|
+
}>;
|
|
29
|
+
date: Schema.encodeKeys<Schema.Struct<{
|
|
30
|
+
readonly value: Schema.NullOr<Schema.String>;
|
|
31
|
+
readonly guess: Schema.optional<Schema.String>;
|
|
32
|
+
readonly nullProbability: Schema.optional<Schema.Number>;
|
|
33
|
+
readonly confidence: Schema.optional<Schema.Number>;
|
|
34
|
+
readonly span: Schema.optional<Schema.Tuple<readonly [Schema.Int, Schema.Int]>>;
|
|
35
|
+
}>, {
|
|
36
|
+
readonly nullProbability: "p_null";
|
|
37
|
+
}>;
|
|
38
|
+
time: Schema.encodeKeys<Schema.Struct<{
|
|
39
|
+
readonly value: Schema.NullOr<Schema.String>;
|
|
40
|
+
readonly guess: Schema.optional<Schema.String>;
|
|
41
|
+
readonly nullProbability: Schema.optional<Schema.Number>;
|
|
42
|
+
readonly confidence: Schema.optional<Schema.Number>;
|
|
43
|
+
readonly span: Schema.optional<Schema.Tuple<readonly [Schema.Int, Schema.Int]>>;
|
|
44
|
+
}>, {
|
|
45
|
+
readonly nullProbability: "p_null";
|
|
46
|
+
}>;
|
|
47
|
+
datetime: Schema.encodeKeys<Schema.Struct<{
|
|
48
|
+
readonly value: Schema.NullOr<Schema.String>;
|
|
49
|
+
readonly guess: Schema.optional<Schema.String>;
|
|
50
|
+
readonly nullProbability: Schema.optional<Schema.Number>;
|
|
51
|
+
readonly confidence: Schema.optional<Schema.Number>;
|
|
52
|
+
readonly span: Schema.optional<Schema.Tuple<readonly [Schema.Int, Schema.Int]>>;
|
|
53
|
+
}>, {
|
|
54
|
+
readonly nullProbability: "p_null";
|
|
55
|
+
}>;
|
|
56
|
+
duration: Schema.encodeKeys<Schema.Struct<{
|
|
57
|
+
readonly value: Schema.NullOr<Schema.Number>;
|
|
58
|
+
readonly guess: Schema.optional<Schema.Number>;
|
|
59
|
+
readonly nullProbability: Schema.optional<Schema.Number>;
|
|
60
|
+
readonly confidence: Schema.optional<Schema.Number>;
|
|
61
|
+
readonly span: Schema.optional<Schema.Tuple<readonly [Schema.Int, Schema.Int]>>;
|
|
62
|
+
}>, {
|
|
63
|
+
readonly nullProbability: "p_null";
|
|
64
|
+
}>;
|
|
65
|
+
list: Schema.Struct<{
|
|
66
|
+
readonly value: Schema.NullOr<Schema.$Array<Schema.Union<readonly [Schema.String, Schema.Number]>>>;
|
|
67
|
+
readonly spans: Schema.optional<Schema.$Array<Schema.Tuple<readonly [Schema.Int, Schema.Int]>>>;
|
|
68
|
+
readonly confidence: Schema.optional<Schema.Number>;
|
|
69
|
+
}>;
|
|
70
|
+
estimate: Schema.encodeKeys<Schema.Struct<{
|
|
71
|
+
readonly value: Schema.Union<readonly [Schema.Number, Schema.String]>;
|
|
72
|
+
readonly mean: Schema.optional<Schema.Union<readonly [Schema.Number, Schema.String]>>;
|
|
73
|
+
readonly median: Schema.Union<readonly [Schema.Number, Schema.String]>;
|
|
74
|
+
readonly p10: Schema.Union<readonly [Schema.Number, Schema.String]>;
|
|
75
|
+
readonly p90: Schema.Union<readonly [Schema.Number, Schema.String]>;
|
|
76
|
+
readonly belowProbability: Schema.optional<Schema.Number>;
|
|
77
|
+
readonly aboveProbability: Schema.optional<Schema.Number>;
|
|
78
|
+
readonly quantiles: Schema.optional<Schema.$Array<Schema.Union<readonly [Schema.Number, Schema.String]>>>;
|
|
79
|
+
}>, {
|
|
80
|
+
readonly belowProbability: "p_below";
|
|
81
|
+
readonly aboveProbability: "p_above";
|
|
82
|
+
}>;
|
|
83
|
+
/** One estimate per step of the series. */
|
|
84
|
+
forecast: Schema.Struct<{
|
|
85
|
+
readonly value: Schema.$Array<Schema.Union<readonly [Schema.Number, Schema.String]>>;
|
|
86
|
+
readonly steps: Schema.$Array<Schema.encodeKeys<Schema.Struct<{
|
|
87
|
+
readonly value: Schema.Union<readonly [Schema.Number, Schema.String]>;
|
|
88
|
+
readonly mean: Schema.optional<Schema.Union<readonly [Schema.Number, Schema.String]>>;
|
|
89
|
+
readonly median: Schema.Union<readonly [Schema.Number, Schema.String]>;
|
|
90
|
+
readonly p10: Schema.Union<readonly [Schema.Number, Schema.String]>;
|
|
91
|
+
readonly p90: Schema.Union<readonly [Schema.Number, Schema.String]>;
|
|
92
|
+
readonly belowProbability: Schema.optional<Schema.Number>;
|
|
93
|
+
readonly aboveProbability: Schema.optional<Schema.Number>;
|
|
94
|
+
readonly quantiles: Schema.optional<Schema.$Array<Schema.Union<readonly [Schema.Number, Schema.String]>>>;
|
|
95
|
+
}>, {
|
|
96
|
+
readonly belowProbability: "p_below";
|
|
97
|
+
readonly aboveProbability: "p_above";
|
|
98
|
+
}>>;
|
|
99
|
+
}>;
|
|
100
|
+
};
|
|
101
|
+
type Answers = typeof ExtractionAnswers;
|
|
102
|
+
export type ExtractionAnswerFor<Field extends ExtractionField> = Field["type"] extends keyof Answers ? Answers[Field["type"]]["Type"] : Readonly<Record<string, unknown>>;
|
|
103
|
+
/** One answer per field, keyed by field name. */
|
|
104
|
+
export type ExtractionAnswersFor<Fields extends ExtractionFields> = {
|
|
105
|
+
readonly [Name in keyof Fields]: ExtractionAnswerFor<Fields[Name]>;
|
|
106
|
+
};
|
|
107
|
+
/** Options beyond these, such as `unit`, `decimals`, `items`, `min` or `max`, go to the host as written. */
|
|
108
|
+
declare const ExtractionFieldSchema: Schema.StructWithRest<Schema.Struct<{
|
|
109
|
+
readonly type: Schema.String;
|
|
110
|
+
readonly instructions: Schema.optional<Schema.String>;
|
|
111
|
+
/** Probability of null above which the value is null; the host's default is 0.5. */
|
|
112
|
+
readonly nullThreshold: Schema.optional<Schema.Number>;
|
|
113
|
+
}>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>;
|
|
114
|
+
export type ExtractionField = typeof ExtractionFieldSchema.Type & {
|
|
115
|
+
readonly type: OpenString<keyof Answers>;
|
|
116
|
+
};
|
|
117
|
+
export type ExtractionFields = Readonly<Record<string, ExtractionField>>;
|
|
118
|
+
export type ExtractionOptions = MediaModel.Options;
|
|
119
|
+
export type ExtractionRoute = MediaRoute.InlineRoute<ExtractionRequestFor, ExtractionResponse>;
|
|
120
|
+
export declare class ExtractionModel<Options extends ExtractionOptions = ExtractionOptions> extends MediaModel<ExtractionRoute, Options> {
|
|
121
|
+
protected readonly _ExtractionModel: void;
|
|
122
|
+
}
|
|
123
|
+
export type ExtractionModelOptions<Model> = Model extends ExtractionModel<infer Options> ? Options : never;
|
|
124
|
+
declare const ExtractionRequest_base: Schema.Class<ExtractionRequest, Schema.Struct<{
|
|
125
|
+
readonly model: Schema.declare<ExtractionModel<{
|
|
126
|
+
readonly [x: string]: unknown;
|
|
127
|
+
}>, unknown>;
|
|
128
|
+
/** The text to read; text answers carry spans into it. */
|
|
129
|
+
readonly input: Schema.String;
|
|
130
|
+
readonly fields: Schema.refine<{
|
|
131
|
+
readonly [x: string]: {
|
|
132
|
+
readonly [x: string]: unknown;
|
|
133
|
+
readonly type: string;
|
|
134
|
+
readonly instructions?: string | undefined;
|
|
135
|
+
readonly nullThreshold?: number | undefined;
|
|
136
|
+
};
|
|
137
|
+
}, Schema.$Record<Schema.String, Schema.StructWithRest<Schema.Struct<{
|
|
138
|
+
readonly type: Schema.String;
|
|
139
|
+
readonly instructions: Schema.optional<Schema.String>;
|
|
140
|
+
/** Probability of null above which the value is null; the host's default is 0.5. */
|
|
141
|
+
readonly nullThreshold: Schema.optional<Schema.Number>;
|
|
142
|
+
}>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>>>;
|
|
143
|
+
/** ISO date that anchors dates written without a year. */
|
|
144
|
+
readonly referenceDate: Schema.optional<Schema.String>;
|
|
145
|
+
readonly providerOptions: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
|
|
146
|
+
readonly http: Schema.optional<typeof HttpOptions>;
|
|
147
|
+
}>, {}>;
|
|
148
|
+
export declare class ExtractionRequest extends ExtractionRequest_base {
|
|
149
|
+
protected readonly _ExtractionRequest: void;
|
|
150
|
+
}
|
|
151
|
+
export type ExtractionRequestFor<Options extends ExtractionOptions = ExtractionOptions, Fields extends ExtractionFields = ExtractionFields> = Omit<ExtractionRequest, "model" | "fields" | "providerOptions"> & {
|
|
152
|
+
readonly model: ExtractionModel<Options>;
|
|
153
|
+
readonly fields: Fields;
|
|
154
|
+
readonly providerOptions?: Options;
|
|
155
|
+
};
|
|
156
|
+
export type ExtractionRequestInput<Model extends ExtractionModel = ExtractionModel, Fields extends ExtractionFields = ExtractionFields> = {
|
|
157
|
+
readonly model: Model;
|
|
158
|
+
readonly input: string;
|
|
159
|
+
readonly fields: Fields;
|
|
160
|
+
readonly referenceDate?: string;
|
|
161
|
+
readonly providerOptions?: NoInfer<ExtractionModelOptions<Model>>;
|
|
162
|
+
readonly http?: HttpOptions.Input;
|
|
163
|
+
};
|
|
164
|
+
declare const ExtractionResponse_base: Schema.Class<ExtractionResponse, Schema.Struct<{
|
|
165
|
+
readonly answers: Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>;
|
|
166
|
+
readonly usage: Schema.optional<typeof Usage>;
|
|
167
|
+
readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
|
|
168
|
+
}>, {}>;
|
|
169
|
+
export declare class ExtractionResponse extends ExtractionResponse_base {
|
|
170
|
+
}
|
|
171
|
+
export type ExtractionResponseFor<Fields extends ExtractionFields> = Omit<ExtractionResponse, "answers"> & {
|
|
172
|
+
readonly answers: ExtractionAnswersFor<Fields>;
|
|
173
|
+
};
|
|
174
|
+
export declare function request<const Model extends ExtractionModel, const Fields extends ExtractionFields>(input: ExtractionRequestInput<Model, Fields>): ExtractionRequestFor<ExtractionModelOptions<Model>, Fields>;
|
|
175
|
+
export declare function request(input: ExtractionRequest): ExtractionRequest;
|
|
176
|
+
export declare function request(input: ExtractionRequest | ExtractionRequestInput): ExtractionRequest;
|
|
177
|
+
export declare function generate<const Model extends ExtractionModel, const Fields extends ExtractionFields>(input: ExtractionRequestInput<Model, Fields>): Effect.Effect<ExtractionResponseFor<Fields>, AIError, RequestExecutor.Service>;
|
|
178
|
+
export declare function generate(input: ExtractionRequest): Effect.Effect<ExtractionResponse, AIError, RequestExecutor.Service>;
|
|
179
|
+
export declare const Extraction: {
|
|
180
|
+
readonly request: typeof request;
|
|
181
|
+
readonly generate: typeof generate;
|
|
182
|
+
};
|
|
183
|
+
export {};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { MediaModel, tryRequest } from "../media-model.js";
|
|
3
|
+
import { RequestExecutor } from "../route/executor.js";
|
|
4
|
+
import { AIError, HttpOptions, ProviderMetadata, ProviderOptions, Usage } from "../schema/index.js";
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// Answers
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
/** `[start, end)` character offsets into `input`. */
|
|
9
|
+
const Span = Schema.Tuple([Schema.Int, Schema.Int]);
|
|
10
|
+
/** An ISO date for date estimates; anything else is numeric. */
|
|
11
|
+
const NumberOrDate = Schema.Union([Schema.Number, Schema.String]);
|
|
12
|
+
/** `value` is null when the input does not determine it; `guess` is the most likely value either way. */
|
|
13
|
+
const valueAnswer = (value) => Schema.Struct({
|
|
14
|
+
value: Schema.NullOr(value),
|
|
15
|
+
guess: Schema.optional(value),
|
|
16
|
+
nullProbability: Schema.optional(Schema.Number),
|
|
17
|
+
confidence: Schema.optional(Schema.Number),
|
|
18
|
+
span: Schema.optional(Span),
|
|
19
|
+
}).pipe(Schema.encodeKeys({ nullProbability: "p_null" }));
|
|
20
|
+
const StringAnswer = valueAnswer(Schema.String);
|
|
21
|
+
const NumberAnswer = valueAnswer(Schema.Number);
|
|
22
|
+
/** `value` is the most likely point; `p10` to `p90` is the 80% interval. */
|
|
23
|
+
const EstimateAnswer = Schema.Struct({
|
|
24
|
+
value: NumberOrDate,
|
|
25
|
+
mean: Schema.optional(NumberOrDate),
|
|
26
|
+
median: NumberOrDate,
|
|
27
|
+
p10: NumberOrDate,
|
|
28
|
+
p90: NumberOrDate,
|
|
29
|
+
belowProbability: Schema.optional(Schema.Number),
|
|
30
|
+
aboveProbability: Schema.optional(Schema.Number),
|
|
31
|
+
quantiles: Schema.optional(Schema.Array(NumberOrDate)),
|
|
32
|
+
}).pipe(Schema.encodeKeys({ belowProbability: "p_below", aboveProbability: "p_above" }));
|
|
33
|
+
/**
|
|
34
|
+
* The answer schema for each field type Clairic documents, decoding its wire keys (`p_null` and friends). Answers to
|
|
35
|
+
* other field types pass through as sent.
|
|
36
|
+
*/
|
|
37
|
+
export const ExtractionAnswers = {
|
|
38
|
+
text: StringAnswer,
|
|
39
|
+
number: NumberAnswer,
|
|
40
|
+
date: StringAnswer,
|
|
41
|
+
time: StringAnswer,
|
|
42
|
+
datetime: StringAnswer,
|
|
43
|
+
duration: NumberAnswer,
|
|
44
|
+
list: Schema.Struct({
|
|
45
|
+
value: Schema.NullOr(Schema.Array(Schema.Union([Schema.String, Schema.Number]))),
|
|
46
|
+
spans: Schema.optional(Schema.Array(Span)),
|
|
47
|
+
confidence: Schema.optional(Schema.Number),
|
|
48
|
+
}),
|
|
49
|
+
estimate: EstimateAnswer,
|
|
50
|
+
/** One estimate per step of the series. */
|
|
51
|
+
forecast: Schema.Struct({ value: Schema.Array(NumberOrDate), steps: Schema.Array(EstimateAnswer) }),
|
|
52
|
+
};
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
// Fields
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
/** Options beyond these, such as `unit`, `decimals`, `items`, `min` or `max`, go to the host as written. */
|
|
57
|
+
const ExtractionFieldSchema = Schema.StructWithRest(Schema.Struct({
|
|
58
|
+
type: Schema.String,
|
|
59
|
+
instructions: Schema.optional(Schema.String),
|
|
60
|
+
/** Probability of null above which the value is null; the host's default is 0.5. */
|
|
61
|
+
nullThreshold: Schema.optional(Schema.Number),
|
|
62
|
+
}), [Schema.Record(Schema.String, Schema.Unknown)]);
|
|
63
|
+
export class ExtractionModel extends MediaModel {
|
|
64
|
+
}
|
|
65
|
+
// ---------------------------------------------------------------------------
|
|
66
|
+
// Request and response
|
|
67
|
+
// ---------------------------------------------------------------------------
|
|
68
|
+
export class ExtractionRequest extends Schema.Class("Extraction.Request")({
|
|
69
|
+
model: Schema.declare((value) => value instanceof ExtractionModel, {
|
|
70
|
+
expected: "Extraction.Model",
|
|
71
|
+
}),
|
|
72
|
+
/** The text to read; text answers carry spans into it. */
|
|
73
|
+
input: Schema.String,
|
|
74
|
+
fields: Schema.Record(Schema.String, ExtractionFieldSchema).pipe(Schema.refine((x) => Object.keys(x).length > 0, { message: "Extraction fields must not be empty" })),
|
|
75
|
+
/** ISO date that anchors dates written without a year. */
|
|
76
|
+
referenceDate: Schema.optional(Schema.String),
|
|
77
|
+
providerOptions: Schema.optional(ProviderOptions),
|
|
78
|
+
http: Schema.optional(HttpOptions),
|
|
79
|
+
}) {
|
|
80
|
+
}
|
|
81
|
+
export class ExtractionResponse extends Schema.Class("Extraction.Response")({
|
|
82
|
+
answers: Schema.Record(Schema.String, Schema.Record(Schema.String, Schema.Unknown)),
|
|
83
|
+
usage: Schema.optional(Usage),
|
|
84
|
+
providerMetadata: Schema.optional(ProviderMetadata),
|
|
85
|
+
}) {
|
|
86
|
+
}
|
|
87
|
+
export function request(input) {
|
|
88
|
+
if (input instanceof ExtractionRequest)
|
|
89
|
+
return input;
|
|
90
|
+
return new ExtractionRequest({ ...input, http: HttpOptions.make(input.http) });
|
|
91
|
+
}
|
|
92
|
+
export function generate(input) {
|
|
93
|
+
return Effect.gen(function* () {
|
|
94
|
+
const executor = yield* RequestExecutor.Service;
|
|
95
|
+
const built = yield* tryRequest(() => request(input));
|
|
96
|
+
return yield* built.model.route.generate(built, executor.execute);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
export const Extraction = {
|
|
100
|
+
request,
|
|
101
|
+
generate,
|
|
102
|
+
};
|
package/dist/experimental.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
+
export { Extraction, ExtractionModel, ExtractionRequest, ExtractionResponse } from "./experimental/extraction.js";
|
|
2
|
+
export type { ExtractionAnswerFor, ExtractionAnswersFor, ExtractionField, ExtractionFields, ExtractionModelOptions, ExtractionOptions, ExtractionRequestFor, ExtractionRequestInput, ExtractionResponseFor, ExtractionRoute, } from "./experimental/extraction.js";
|
|
1
3
|
export { Realtime } from "./experimental/realtime.js";
|
|
2
4
|
export type { RealtimeConnectInput, RealtimeSession, RealtimeTransport, RealtimeTransportInput, RealtimeTransportType, } from "./experimental/realtime.js";
|
package/dist/experimental.js
CHANGED
|
@@ -20,7 +20,7 @@ export const protocol = DecisionRoute.protocol({
|
|
|
20
20
|
name: NAME,
|
|
21
21
|
inlineMedia: true,
|
|
22
22
|
body: {
|
|
23
|
-
from: (request) => SystemOne.encodeBody(request, "
|
|
23
|
+
from: (request) => SystemOne.encodeBody(request, "root").pipe(Effect.map((input) => ({ model: request.model.id, input }))),
|
|
24
24
|
},
|
|
25
25
|
response: {
|
|
26
26
|
decode: (response, context) => decodeEnvelope(response).pipe(Effect.flatMap((output) => SystemOne.toResponse("answers" in output.value.result ? output.value.result : output.value.result.result, context.request, context.providerMetadataKey, output.invalid))),
|
|
@@ -4,8 +4,8 @@ import { DecisionRoute } from "../route/decision.js";
|
|
|
4
4
|
import { AIError } from "../schema/index.js";
|
|
5
5
|
export declare const DEFAULT_PATH = "/systemone";
|
|
6
6
|
export declare const DEFAULT_BASE_URL = "https://api.typesafe.ai/v1";
|
|
7
|
-
/** `messages` is Baseten's own placement (parts in a user message inside `state.messages`), set by its provider. */
|
|
8
|
-
type
|
|
7
|
+
/** `state.messages` is Baseten's own placement (parts in a user message inside `state.messages`), set by its provider. */
|
|
8
|
+
type MediaInput = NonNullable<DecisionModelCompatibility["mediaInput"]> | "state.messages";
|
|
9
9
|
export declare const Response: Schema.Struct<{
|
|
10
10
|
readonly model: Schema.String;
|
|
11
11
|
readonly answers: Schema.$Record<Schema.String, Schema.toTaggedUnion<"type", readonly [Schema.Struct<{
|
|
@@ -31,8 +31,8 @@ export declare const Response: Schema.Struct<{
|
|
|
31
31
|
readonly provider_metadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
|
|
32
32
|
}>;
|
|
33
33
|
export type Response = Schema.Schema.Type<typeof Response>;
|
|
34
|
-
/** The System One body without `model`; media goes where the model's `
|
|
35
|
-
export declare const encodeBody: (request: DecisionRequestFor, fallback:
|
|
34
|
+
/** The System One body without `model`; media goes where the model's `mediaInput` says, else `fallback`. */
|
|
35
|
+
export declare const encodeBody: (request: DecisionRequestFor, fallback: MediaInput) => Effect.Effect<{
|
|
36
36
|
questions: any;
|
|
37
37
|
state: unknown;
|
|
38
38
|
assets?: undefined;
|
|
@@ -77,7 +77,7 @@ export declare const SystemOne: {
|
|
|
77
77
|
readonly provider: Schema.optional<Schema.String>;
|
|
78
78
|
readonly provider_metadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
|
|
79
79
|
}>;
|
|
80
|
-
readonly encodeBody: (request: DecisionRequestFor, fallback:
|
|
80
|
+
readonly encodeBody: (request: DecisionRequestFor, fallback: MediaInput) => Effect.Effect<{
|
|
81
81
|
questions: any;
|
|
82
82
|
state: unknown;
|
|
83
83
|
assets?: undefined;
|
|
@@ -38,10 +38,10 @@ export const Response = Schema.Struct({
|
|
|
38
38
|
});
|
|
39
39
|
const ID = "system-one";
|
|
40
40
|
const NAME = "System One";
|
|
41
|
-
/** The System One body without `model`; media goes where the model's `
|
|
41
|
+
/** The System One body without `model`; media goes where the model's `mediaInput` says, else `fallback`. */
|
|
42
42
|
export const encodeBody = Effect.fn("SystemOne.encodeBody")(function* (request, fallback) {
|
|
43
43
|
const questions = yield* encodeQuestions(request.questions);
|
|
44
|
-
const placed = yield* placeMedia(request, request.model.compatibility?.
|
|
44
|
+
const placed = yield* placeMedia(request, request.model.compatibility?.mediaInput ?? fallback);
|
|
45
45
|
return { ...mergeJsonRecords(request.providerOptions, request.http?.body), ...placed, questions };
|
|
46
46
|
});
|
|
47
47
|
const encodeQuestions = (questions) => Effect.forEach(questions, (question) => {
|
|
@@ -69,7 +69,7 @@ const encodeQuestions = (questions) => Effect.forEach(questions, (question) => {
|
|
|
69
69
|
},
|
|
70
70
|
]);
|
|
71
71
|
}).pipe(Effect.map(Object.fromEntries));
|
|
72
|
-
const placeMedia = Effect.fnUntraced(function* (request,
|
|
72
|
+
const placeMedia = Effect.fnUntraced(function* (request, mediaInput) {
|
|
73
73
|
const parts = request.input;
|
|
74
74
|
const unsupported = (asset, accepted) => new AIError({
|
|
75
75
|
reason: new UnsupportedOperationError({
|
|
@@ -82,10 +82,10 @@ const placeMedia = Effect.fnUntraced(function* (request, imageInput) {
|
|
|
82
82
|
const media = parts.flatMap((part) => (part.type === "media" ? [part.media] : []));
|
|
83
83
|
if (media.length === 0)
|
|
84
84
|
return { state: state(parts.map(plain)) };
|
|
85
|
-
if (
|
|
85
|
+
if (mediaInput === "state")
|
|
86
86
|
return { state: yield* Effect.forEach(parts, (part) => stateItem(part, unsupported)) };
|
|
87
87
|
// Chat-style content in one user message: text and JSON as text parts, media as in `state`.
|
|
88
|
-
if (
|
|
88
|
+
if (mediaInput === "state.messages")
|
|
89
89
|
return {
|
|
90
90
|
state: {
|
|
91
91
|
messages: [
|
|
@@ -98,7 +98,7 @@ const placeMedia = Effect.fnUntraced(function* (request, imageInput) {
|
|
|
98
98
|
],
|
|
99
99
|
},
|
|
100
100
|
};
|
|
101
|
-
if (
|
|
101
|
+
if (mediaInput === "root") {
|
|
102
102
|
const lists = { image: "images", audio: "audio", video: "videos" };
|
|
103
103
|
const unplaced = media.find((asset) => !Object.hasOwn(lists, asset.kind));
|
|
104
104
|
if (unplaced !== undefined)
|
|
@@ -23,7 +23,7 @@ const decisionProtocol = DecisionRoute.protocol({
|
|
|
23
23
|
...SystemOne.protocol,
|
|
24
24
|
id: "baseten-system-one",
|
|
25
25
|
body: {
|
|
26
|
-
from: (request) => SystemOne.encodeBody(request, "messages").pipe(Effect.map((body) => ({ ...body, model: request.model.id }))),
|
|
26
|
+
from: (request) => SystemOne.encodeBody(request, "state.messages").pipe(Effect.map((body) => ({ ...body, model: request.model.id }))),
|
|
27
27
|
},
|
|
28
28
|
});
|
|
29
29
|
export const configure = (input = {}) => {
|
|
@@ -302,6 +302,11 @@ export declare const configure: (input: LanguageModelOptions) => {
|
|
|
302
302
|
model: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./openai-options.js").OpenAIOptionsInput, import("../route/client.js").CompactionOperations | undefined>;
|
|
303
303
|
decision: (modelID: string | ModelID) => import("../decision.js").DecisionModel<import("../decision.js").DecisionOptions>;
|
|
304
304
|
image: (modelID: string | ModelID) => import("../image.js").ImageModel<import("./cloudflare-workers-ai.js").CloudflareWorkersAIImageOptions>;
|
|
305
|
+
experimental: {
|
|
306
|
+
extraction: (modelID: string | ModelID) => import("../experimental.js").ExtractionModel<{
|
|
307
|
+
readonly [x: string]: unknown;
|
|
308
|
+
}>;
|
|
309
|
+
};
|
|
305
310
|
configure: typeof configure;
|
|
306
311
|
};
|
|
307
312
|
export declare const provider: {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CloudflareRunExtraction } from "../experimental/cloudflare-run-extraction.js";
|
|
1
2
|
import { CloudflareRunDecisions } from "../protocols/cloudflare-run-decisions.js";
|
|
2
3
|
import { CloudflareWorkersAIImages } from "../protocols/cloudflare-workers-ai-images.js";
|
|
3
4
|
import { OpenAIChat } from "../protocols/openai-chat.js";
|
|
@@ -42,6 +43,7 @@ export const configure = (input) => {
|
|
|
42
43
|
const auth = AuthOptions.bearer(input, authEnvVars);
|
|
43
44
|
const configured = route.with({ ...defaults, endpoint: { baseURL: baseURL(input) }, auth });
|
|
44
45
|
// The Run API base is derived per call so a chat-only configuration without an account still constructs.
|
|
46
|
+
const runDeployment = () => MediaRoute.deployment({ ...input, baseURL: runBaseURL(input) }, auth);
|
|
45
47
|
const decision = (modelID) => CloudflareRunDecisions.route
|
|
46
48
|
.with({
|
|
47
49
|
provider: id,
|
|
@@ -55,10 +57,10 @@ export const configure = (input) => {
|
|
|
55
57
|
id,
|
|
56
58
|
model: (modelID) => configured.model({ id: modelID }),
|
|
57
59
|
decision,
|
|
58
|
-
image: (modelID) => CloudflareWorkersAIImages.model({
|
|
59
|
-
|
|
60
|
-
id: modelID,
|
|
61
|
-
}
|
|
60
|
+
image: (modelID) => CloudflareWorkersAIImages.model({ ...runDeployment(), id: modelID }),
|
|
61
|
+
experimental: {
|
|
62
|
+
extraction: (modelID) => CloudflareRunExtraction.model({ ...runDeployment(), id: modelID }),
|
|
63
|
+
},
|
|
62
64
|
configure,
|
|
63
65
|
};
|
|
64
66
|
};
|
|
@@ -107,7 +107,7 @@ export const configure = (input = {}) => {
|
|
|
107
107
|
headers: input.headers,
|
|
108
108
|
http: input.http,
|
|
109
109
|
});
|
|
110
|
-
const decision = (modelID) => decisionRoute.model({ id: modelID, compatibility: {
|
|
110
|
+
const decision = (modelID) => decisionRoute.model({ id: modelID, compatibility: { mediaInput: "state" } });
|
|
111
111
|
return {
|
|
112
112
|
id,
|
|
113
113
|
model: (modelID) => route.model({ id: modelID, compatibility: { supportsPromptCacheKey: true } }),
|
package/dist/utils/media-type.js
CHANGED
|
@@ -58,8 +58,12 @@ const EXTENSIONS = {
|
|
|
58
58
|
jpeg: "image/jpeg",
|
|
59
59
|
gif: "image/gif",
|
|
60
60
|
webp: "image/webp",
|
|
61
|
+
avif: "image/avif",
|
|
62
|
+
heic: "image/heic",
|
|
63
|
+
heif: "image/heif",
|
|
61
64
|
pdf: "application/pdf",
|
|
62
65
|
mp4: "video/mp4",
|
|
66
|
+
mov: "video/quicktime",
|
|
63
67
|
webm: "video/webm",
|
|
64
68
|
mp3: "audio/mpeg",
|
|
65
69
|
m4a: "audio/mp4",
|
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-21920",
|
|
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-21920",
|
|
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-21920",
|
|
49
49
|
"aws4fetch": "1.0.20",
|
|
50
50
|
"effect": "4.0.1",
|
|
51
51
|
"google-auth-library": "10.5.0"
|