@effect/ai-cloudflare 0.0.0 → 4.0.2
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/AGENTS.md +410 -0
- package/CLAUDE.md +410 -0
- package/LICENSE +21 -0
- package/ai-docs/README.md +44 -0
- package/ai-docs/package.json +36 -0
- package/ai-docs/src/01_effect/01_basics/01_effect-gen.ts +30 -0
- package/ai-docs/src/01_effect/01_basics/02_effect-fn.ts +52 -0
- package/ai-docs/src/01_effect/01_basics/10_creating-effects.ts +74 -0
- package/ai-docs/src/01_effect/01_basics/index.md +7 -0
- package/ai-docs/src/01_effect/02_schema/10_schema-basics.ts +43 -0
- package/ai-docs/src/01_effect/02_schema/index.md +7 -0
- package/ai-docs/src/01_effect/03_services/01_service.ts +45 -0
- package/ai-docs/src/01_effect/03_services/10_reference.ts +10 -0
- package/ai-docs/src/01_effect/03_services/20_layer-composition.ts +70 -0
- package/ai-docs/src/01_effect/03_services/20_layer-unwrap.ts +66 -0
- package/ai-docs/src/01_effect/03_services/index.md +5 -0
- package/ai-docs/src/01_effect/04_errors/01_error-handling.ts +30 -0
- package/ai-docs/src/01_effect/04_errors/10_catch-tags.ts +24 -0
- package/ai-docs/src/01_effect/04_errors/20_reason-errors.ts +64 -0
- package/ai-docs/src/01_effect/04_errors/index.md +1 -0
- package/ai-docs/src/01_effect/05_resources/10_acquire-release.ts +105 -0
- package/ai-docs/src/01_effect/05_resources/20_layer-side-effects.ts +31 -0
- package/ai-docs/src/01_effect/05_resources/30_layer-map.ts +86 -0
- package/ai-docs/src/01_effect/05_resources/index.md +3 -0
- package/ai-docs/src/01_effect/06_running/10_run-main.ts +30 -0
- package/ai-docs/src/01_effect/06_running/20_layer-launch.ts +27 -0
- package/ai-docs/src/01_effect/06_running/index.md +1 -0
- package/ai-docs/src/01_effect/07_pubsub/10_pubsub.ts +56 -0
- package/ai-docs/src/01_effect/07_pubsub/index.md +3 -0
- package/ai-docs/src/03_stream/10_creating-streams.ts +103 -0
- package/ai-docs/src/03_stream/20_consuming-streams.ts +137 -0
- package/ai-docs/src/03_stream/30_encoding.ts +163 -0
- package/ai-docs/src/03_stream/index.md +4 -0
- package/ai-docs/src/04_integration/10_managed-runtime.ts +129 -0
- package/ai-docs/src/04_integration/index.md +5 -0
- package/ai-docs/src/05_batching/10_request-resolver.ts +89 -0
- package/ai-docs/src/05_batching/index.md +3 -0
- package/ai-docs/src/06_schedule/10_schedules.ts +110 -0
- package/ai-docs/src/06_schedule/index.md +3 -0
- package/ai-docs/src/07_datetime/10_creating-and-formatting.ts +30 -0
- package/ai-docs/src/07_datetime/20_time-zones.ts +44 -0
- package/ai-docs/src/07_datetime/index.md +5 -0
- package/ai-docs/src/08_observability/10_logging.ts +66 -0
- package/ai-docs/src/08_observability/20_otlp-tracing.ts +95 -0
- package/ai-docs/src/08_observability/index.md +7 -0
- package/ai-docs/src/09_testing/10_effect-tests.ts +55 -0
- package/ai-docs/src/09_testing/20_layer-tests.ts +138 -0
- package/ai-docs/src/09_testing/index.md +1 -0
- package/ai-docs/src/10_predicate/01_basics.ts +14 -0
- package/ai-docs/src/10_predicate/index.md +9 -0
- package/ai-docs/src/40_sql/10_basics.ts +175 -0
- package/ai-docs/src/40_sql/index.md +6 -0
- package/ai-docs/src/50_http-client/10_basics.ts +102 -0
- package/ai-docs/src/50_http-client/index.md +3 -0
- package/ai-docs/src/51_http-server/10_basics.ts +118 -0
- package/ai-docs/src/51_http-server/20_testing.ts +100 -0
- package/ai-docs/src/51_http-server/fixtures/api/Api.ts +14 -0
- package/ai-docs/src/51_http-server/fixtures/api/Authorization.ts +36 -0
- package/ai-docs/src/51_http-server/fixtures/api/System.ts +10 -0
- package/ai-docs/src/51_http-server/fixtures/api/Users.ts +107 -0
- package/ai-docs/src/51_http-server/fixtures/domain/User.ts +21 -0
- package/ai-docs/src/51_http-server/fixtures/domain/UserErrors.ts +22 -0
- package/ai-docs/src/51_http-server/fixtures/server/Authorization.ts +37 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users/http.ts +80 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users.ts +183 -0
- package/ai-docs/src/51_http-server/index.md +3 -0
- package/ai-docs/src/60_child-process/10_working-with-child-processes.ts +117 -0
- package/ai-docs/src/60_child-process/index.md +3 -0
- package/ai-docs/src/70_cli/10_basics.ts +162 -0
- package/ai-docs/src/70_cli/index.md +5 -0
- package/ai-docs/src/71_ai/10_language-model.ts +156 -0
- package/ai-docs/src/71_ai/20_tools.ts +226 -0
- package/ai-docs/src/71_ai/30_chat.ts +158 -0
- package/ai-docs/src/71_ai/fixtures/domain/LaunchPlan.ts +9 -0
- package/ai-docs/src/71_ai/index.md +5 -0
- package/ai-docs/src/80_cluster/10_entities.ts +97 -0
- package/ai-docs/src/80_cluster/index.md +4 -0
- package/ai-docs/src/index.md +10 -0
- package/ai-docs/tsconfig.json +25 -0
- package/dist/CloudflareClient.d.ts +79 -0
- package/dist/CloudflareClient.d.ts.map +1 -0
- package/dist/CloudflareClient.js +86 -0
- package/dist/CloudflareClient.js.map +1 -0
- package/dist/CloudflareDecisionModel.d.ts +49 -0
- package/dist/CloudflareDecisionModel.d.ts.map +1 -0
- package/dist/CloudflareDecisionModel.js +122 -0
- package/dist/CloudflareDecisionModel.js.map +1 -0
- package/dist/CloudflareSchema.d.ts +186 -0
- package/dist/CloudflareSchema.d.ts.map +1 -0
- package/dist/CloudflareSchema.js +128 -0
- package/dist/CloudflareSchema.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/errors.d.ts +7 -0
- package/dist/internal/errors.d.ts.map +1 -0
- package/dist/internal/errors.js +109 -0
- package/dist/internal/errors.js.map +1 -0
- package/package.json +47 -9
- package/src/CloudflareClient.ts +149 -0
- package/src/CloudflareDecisionModel.ts +111 -0
- package/src/CloudflareSchema.ts +131 -0
- package/src/index.ts +24 -0
- package/src/internal/errors.ts +101 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codecs for text and JSON decisions from Cloudflare Clef models.
|
|
3
|
+
*
|
|
4
|
+
* @stability unstable
|
|
5
|
+
* @since 4.0.0
|
|
6
|
+
*/
|
|
7
|
+
import * as Schema from "effect/Schema"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A classification question.
|
|
11
|
+
*
|
|
12
|
+
* @stability unstable
|
|
13
|
+
* @category schemas
|
|
14
|
+
* @since 4.0.0
|
|
15
|
+
*/
|
|
16
|
+
export const ChoiceQuestion = Schema.Struct({
|
|
17
|
+
type: Schema.Literal("choice"),
|
|
18
|
+
instructions: Schema.String,
|
|
19
|
+
criteria: Schema.Record(Schema.String, Schema.String)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* An ordered rating question.
|
|
24
|
+
*
|
|
25
|
+
* @stability unstable
|
|
26
|
+
* @category schemas
|
|
27
|
+
* @since 4.0.0
|
|
28
|
+
*/
|
|
29
|
+
export const ScoreQuestion = Schema.Struct({
|
|
30
|
+
type: Schema.Literal("score"),
|
|
31
|
+
instructions: Schema.String,
|
|
32
|
+
criteria: Schema.Array(Schema.String)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A probability question.
|
|
37
|
+
*
|
|
38
|
+
* @stability unstable
|
|
39
|
+
* @category schemas
|
|
40
|
+
* @since 4.0.0
|
|
41
|
+
*/
|
|
42
|
+
export const NoulQuestion = Schema.Struct({
|
|
43
|
+
type: Schema.Literal("noul"),
|
|
44
|
+
instructions: Schema.String,
|
|
45
|
+
criteria: Schema.optional(Schema.Struct({ false: Schema.String, true: Schema.String }))
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A Clef classification, rating, or probability question.
|
|
50
|
+
*
|
|
51
|
+
* @stability unstable
|
|
52
|
+
* @category schemas
|
|
53
|
+
* @since 4.0.0
|
|
54
|
+
*/
|
|
55
|
+
export const Question = Schema.Union([ChoiceQuestion, ScoreQuestion, NoulQuestion])
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* A batch of questions about text or JSON state.
|
|
59
|
+
*
|
|
60
|
+
* @stability unstable
|
|
61
|
+
* @category schemas
|
|
62
|
+
* @since 4.0.0
|
|
63
|
+
*/
|
|
64
|
+
export const DecisionsRequest = Schema.Struct({
|
|
65
|
+
model: Schema.Union([Schema.Literals(["clef", "clef-flash"]), Schema.StringForLiteralAutocomplete]),
|
|
66
|
+
state: Schema.Json,
|
|
67
|
+
questions: Schema.Record(Schema.String, Question)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A classification distribution.
|
|
72
|
+
*
|
|
73
|
+
* @stability unstable
|
|
74
|
+
* @category schemas
|
|
75
|
+
* @since 4.0.0
|
|
76
|
+
*/
|
|
77
|
+
export const ChoiceAnswer = Schema.Struct({
|
|
78
|
+
type: Schema.Literal("choice"),
|
|
79
|
+
choice: Schema.String,
|
|
80
|
+
probabilities: Schema.Record(Schema.String, Schema.Number),
|
|
81
|
+
confidence: Schema.Number
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* A zero-based rating and index-keyed distribution.
|
|
86
|
+
*
|
|
87
|
+
* @stability unstable
|
|
88
|
+
* @category schemas
|
|
89
|
+
* @since 4.0.0
|
|
90
|
+
*/
|
|
91
|
+
export const ScoreAnswer = Schema.Struct({
|
|
92
|
+
type: Schema.Literal("score"),
|
|
93
|
+
score: Schema.Number,
|
|
94
|
+
probabilities: Schema.Record(Schema.String, Schema.Number),
|
|
95
|
+
confidence: Schema.Number,
|
|
96
|
+
legend: Schema.Record(Schema.String, Schema.String)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* A Bernoulli probability.
|
|
101
|
+
*
|
|
102
|
+
* @stability unstable
|
|
103
|
+
* @category schemas
|
|
104
|
+
* @since 4.0.0
|
|
105
|
+
*/
|
|
106
|
+
export const NoulAnswer = Schema.Struct({ type: Schema.Literal("noul"), noul: Schema.Number })
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A Clef classification, rating, or probability answer.
|
|
110
|
+
*
|
|
111
|
+
* @stability unstable
|
|
112
|
+
* @category schemas
|
|
113
|
+
* @since 4.0.0
|
|
114
|
+
*/
|
|
115
|
+
export const Answer = Schema.Union([ChoiceAnswer, ScoreAnswer, NoulAnswer])
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Answers and token usage for a batch.
|
|
119
|
+
*
|
|
120
|
+
* @stability unstable
|
|
121
|
+
* @category schemas
|
|
122
|
+
* @since 4.0.0
|
|
123
|
+
*/
|
|
124
|
+
export const DecisionsResponse = Schema.Struct({
|
|
125
|
+
model: Schema.String,
|
|
126
|
+
answers: Schema.Record(Schema.String, Answer),
|
|
127
|
+
usage: Schema.Struct({
|
|
128
|
+
input_tokens: Schema.Int,
|
|
129
|
+
output_tokens: Schema.Int
|
|
130
|
+
})
|
|
131
|
+
})
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @stability unstable
|
|
3
|
+
* @since 4.0.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// @barrel
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @stability unstable
|
|
10
|
+
* @since 4.0.0
|
|
11
|
+
*/
|
|
12
|
+
export * as CloudflareClient from "./CloudflareClient.ts"
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @stability unstable
|
|
16
|
+
* @since 4.0.0
|
|
17
|
+
*/
|
|
18
|
+
export * as CloudflareDecisionModel from "./CloudflareDecisionModel.ts"
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @stability unstable
|
|
22
|
+
* @since 4.0.0
|
|
23
|
+
*/
|
|
24
|
+
export * as CloudflareSchema from "./CloudflareSchema.ts"
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import * as AiError from "effect/ai/AiError"
|
|
2
|
+
import * as Duration from "effect/Duration"
|
|
3
|
+
import * as Effect from "effect/Effect"
|
|
4
|
+
import type * as HttpClientError from "effect/http/HttpClientError"
|
|
5
|
+
import type * as HttpClientRequest from "effect/http/HttpClientRequest"
|
|
6
|
+
import type * as HttpClientResponse from "effect/http/HttpClientResponse"
|
|
7
|
+
import * as Num from "effect/Number"
|
|
8
|
+
import * as Option from "effect/Option"
|
|
9
|
+
import * as Redactable from "effect/Redactable"
|
|
10
|
+
import * as Schema from "effect/Schema"
|
|
11
|
+
|
|
12
|
+
const requestDetails = (request: HttpClientRequest.HttpClientRequest) => ({
|
|
13
|
+
method: request.method,
|
|
14
|
+
url: request.url,
|
|
15
|
+
urlParams: Array.from(request.urlParams),
|
|
16
|
+
hash: Option.getOrUndefined(request.hash),
|
|
17
|
+
headers: Redactable.redact(request.headers) as Record<string, string>
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const ErrorBody = Schema.Struct({
|
|
21
|
+
errors: Schema.Array(Schema.Struct({ code: Schema.Number, message: Schema.String }))
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
const decodeErrorBody = Schema.decodeUnknownOption(ErrorBody)
|
|
25
|
+
|
|
26
|
+
const retryAfter = (raw: string | undefined): Duration.Duration | undefined => {
|
|
27
|
+
if (raw === undefined || raw.trim() === "") return undefined
|
|
28
|
+
const seconds = Option.getOrUndefined(Num.parse(raw))
|
|
29
|
+
if (seconds !== undefined) return Number.isFinite(seconds) && seconds >= 0 ? Duration.seconds(seconds) : undefined
|
|
30
|
+
const date = Date.parse(raw)
|
|
31
|
+
return Number.isNaN(date) ? undefined : Duration.millis(Math.max(0, date - Date.now()))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const fail = (reason: AiError.AiErrorReason) =>
|
|
35
|
+
Effect.fail(AiError.make({ module: "CloudflareClient", method: "createDecisions", reason }))
|
|
36
|
+
|
|
37
|
+
export const mapResponseError = Effect.fnUntraced(
|
|
38
|
+
function*(response: HttpClientResponse.HttpClientResponse): Effect.fn.Return<never, AiError.AiError> {
|
|
39
|
+
const { request, status } = response
|
|
40
|
+
const body = yield* response.text.pipe(Effect.orElseSucceed(() => undefined))
|
|
41
|
+
const parsed = yield* Effect.try(() => JSON.parse(body ?? "")).pipe(Effect.orElseSucceed(() => undefined))
|
|
42
|
+
const details = Option.getOrUndefined(decodeErrorBody(parsed))
|
|
43
|
+
const http = {
|
|
44
|
+
request: requestDetails(request),
|
|
45
|
+
response: { status, headers: Redactable.redact(response.headers) as Record<string, string> },
|
|
46
|
+
body
|
|
47
|
+
}
|
|
48
|
+
const description = AiError.buildErrorDescription({
|
|
49
|
+
status,
|
|
50
|
+
method: request.method,
|
|
51
|
+
url: request.url,
|
|
52
|
+
body,
|
|
53
|
+
message: details?.errors.map((error) => error.message).join("; "),
|
|
54
|
+
errorCode: details?.errors[0]?.code,
|
|
55
|
+
requestId: response.headers["cf-ray"]
|
|
56
|
+
})
|
|
57
|
+
if (status === 429) {
|
|
58
|
+
return yield* fail(
|
|
59
|
+
new AiError.RateLimitError({
|
|
60
|
+
retryAfter: retryAfter(response.headers["retry-after"]),
|
|
61
|
+
http,
|
|
62
|
+
metadata: { cloudflare: { rayId: response.headers["cf-ray"] ?? null, errors: details?.errors ?? [] } }
|
|
63
|
+
})
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
if (status === 404 || status === 422) {
|
|
67
|
+
return yield* fail(new AiError.InvalidRequestError({ description, http }))
|
|
68
|
+
}
|
|
69
|
+
if (status >= 200 && status < 300) {
|
|
70
|
+
return yield* fail(new AiError.InternalProviderError({ description, http }))
|
|
71
|
+
}
|
|
72
|
+
return yield* fail(AiError.reasonFromHttpStatus({ status, description, http }))
|
|
73
|
+
}
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
export const mapHttpClientError = Effect.fnUntraced(
|
|
77
|
+
function*(error: HttpClientError.HttpClientError): Effect.fn.Return<never, AiError.AiError> {
|
|
78
|
+
const source = error.reason
|
|
79
|
+
switch (source._tag) {
|
|
80
|
+
case "TransportError":
|
|
81
|
+
case "EncodeError":
|
|
82
|
+
case "InvalidUrlError":
|
|
83
|
+
return yield* fail(
|
|
84
|
+
new AiError.NetworkError({
|
|
85
|
+
reason: source._tag,
|
|
86
|
+
description: source.description,
|
|
87
|
+
request: requestDetails(source.request)
|
|
88
|
+
})
|
|
89
|
+
)
|
|
90
|
+
case "DecodeError":
|
|
91
|
+
case "EmptyBodyError":
|
|
92
|
+
return yield* fail(
|
|
93
|
+
new AiError.InvalidOutputError({
|
|
94
|
+
description: source.description ?? "Failed to decode Cloudflare response"
|
|
95
|
+
})
|
|
96
|
+
)
|
|
97
|
+
case "StatusCodeError":
|
|
98
|
+
return yield* mapResponseError(source.response)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
)
|