@effect-uai/anthropic 0.1.0
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/LICENSE +21 -0
- package/README.md +43 -0
- package/dist/chunk-CfYAbeIz.mjs +13 -0
- package/dist/index.d.mts +438 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +649 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +54 -0
- package/src/Anthropic.ts +390 -0
- package/src/codec.ts +628 -0
- package/src/index.ts +4 -0
- package/src/models.ts +34 -0
- package/src/streamEvents.ts +221 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Betalyra Sociedade Unipessoal Lda.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @effect-uai/anthropic
|
|
2
|
+
|
|
3
|
+
Anthropic Messages API provider for [`@effect-uai/core`](https://www.npmjs.com/package/@effect-uai/core).
|
|
4
|
+
|
|
5
|
+
Implements the `LanguageModel` contract against Anthropic's Messages
|
|
6
|
+
API with SSE streaming, including extended thinking surfaced as
|
|
7
|
+
`reasoning_delta` events.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
pnpm add @effect-uai/anthropic @effect-uai/core effect
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
ESM-only. Requires `effect@4.x` and `@effect-uai/core` as peers.
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { Config, Effect, Layer } from "effect"
|
|
21
|
+
import { FetchHttpClient } from "effect/unstable/http"
|
|
22
|
+
import { layer as anthropicLayer } from "@effect-uai/anthropic"
|
|
23
|
+
|
|
24
|
+
const provider = Layer.unwrap(
|
|
25
|
+
Effect.gen(function* () {
|
|
26
|
+
const apiKey = yield* Config.redacted("ANTHROPIC_API_KEY")
|
|
27
|
+
return anthropicLayer({ apiKey })
|
|
28
|
+
}),
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
const layer = Layer.provide(provider, FetchHttpClient.layer)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
The layer registers both the provider-typed `Anthropic` tag and the
|
|
35
|
+
generic `LanguageModel` tag.
|
|
36
|
+
|
|
37
|
+
## Docs
|
|
38
|
+
|
|
39
|
+
<https://effect-uai.betalyra.com/providers/anthropic/>
|
|
40
|
+
|
|
41
|
+
## License
|
|
42
|
+
|
|
43
|
+
MIT
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, no_symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
return target;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
export { __exportAll as t };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
import { Context, Effect, Layer, Option, Redacted, Result, Schema, Stream } from "effect";
|
|
2
|
+
import { HttpClient } from "effect/unstable/http";
|
|
3
|
+
import * as AiError from "@effect-uai/core/AiError";
|
|
4
|
+
import { CommonRequest, LanguageModel } from "@effect-uai/core/LanguageModel";
|
|
5
|
+
import { JsonParseError } from "@effect-uai/core/JSONL";
|
|
6
|
+
import { Turn, TurnEvent } from "@effect-uai/core/Turn";
|
|
7
|
+
import * as Items from "@effect-uai/core/Items";
|
|
8
|
+
|
|
9
|
+
//#region src/codec.d.ts
|
|
10
|
+
declare namespace codec_d_exports {
|
|
11
|
+
export { Accumulator, RequestBody, ThinkingConfig, WireContentBlock, WireUsage, accumulatorToTurn, appendInputJsonDelta, appendSignatureDelta, appendTextDelta, appendThinkingDelta, buildRequestBody, emptyAccumulator, mergeUsage, setStopReason, startBlock };
|
|
12
|
+
}
|
|
13
|
+
declare const WireContentBlock: Schema.Union<readonly [Schema.Struct<{
|
|
14
|
+
readonly type: Schema.Literal<"text">;
|
|
15
|
+
readonly text: Schema.String;
|
|
16
|
+
}>, Schema.Struct<{
|
|
17
|
+
readonly type: Schema.Literal<"tool_use">;
|
|
18
|
+
readonly id: Schema.String;
|
|
19
|
+
readonly name: Schema.String;
|
|
20
|
+
readonly input: Schema.Unknown;
|
|
21
|
+
}>, Schema.Struct<{
|
|
22
|
+
readonly type: Schema.Literal<"thinking">;
|
|
23
|
+
readonly thinking: Schema.String;
|
|
24
|
+
readonly signature: Schema.optional<Schema.String>;
|
|
25
|
+
}>, Schema.Struct<{
|
|
26
|
+
readonly type: Schema.Literal<"redacted_thinking">;
|
|
27
|
+
readonly data: Schema.String;
|
|
28
|
+
}>]>;
|
|
29
|
+
type WireContentBlock = typeof WireContentBlock.Type;
|
|
30
|
+
declare const WireUsage: Schema.Struct<{
|
|
31
|
+
readonly input_tokens: Schema.optional<Schema.Number>;
|
|
32
|
+
readonly output_tokens: Schema.optional<Schema.Number>;
|
|
33
|
+
readonly cache_creation_input_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
|
|
34
|
+
readonly cache_read_input_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
|
|
35
|
+
}>;
|
|
36
|
+
type WireUsage = typeof WireUsage.Type;
|
|
37
|
+
interface RequestTextContent {
|
|
38
|
+
readonly type: "text";
|
|
39
|
+
readonly text: string;
|
|
40
|
+
}
|
|
41
|
+
interface RequestToolResultContent {
|
|
42
|
+
readonly type: "tool_result";
|
|
43
|
+
readonly tool_use_id: string;
|
|
44
|
+
readonly content: string;
|
|
45
|
+
}
|
|
46
|
+
interface RequestToolUseContent {
|
|
47
|
+
readonly type: "tool_use";
|
|
48
|
+
readonly id: string;
|
|
49
|
+
readonly name: string;
|
|
50
|
+
readonly input: unknown;
|
|
51
|
+
}
|
|
52
|
+
interface RequestThinkingContent {
|
|
53
|
+
readonly type: "thinking";
|
|
54
|
+
readonly thinking: string;
|
|
55
|
+
readonly signature?: string;
|
|
56
|
+
}
|
|
57
|
+
interface RequestRedactedThinkingContent {
|
|
58
|
+
readonly type: "redacted_thinking";
|
|
59
|
+
readonly data: string;
|
|
60
|
+
}
|
|
61
|
+
interface RequestImageContent {
|
|
62
|
+
readonly type: "image";
|
|
63
|
+
readonly source: {
|
|
64
|
+
readonly type: "url";
|
|
65
|
+
readonly url: string;
|
|
66
|
+
} | {
|
|
67
|
+
readonly type: "base64";
|
|
68
|
+
readonly media_type: string;
|
|
69
|
+
readonly data: string;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
type RequestUserContentBlock = RequestTextContent | RequestToolResultContent | RequestImageContent;
|
|
73
|
+
type RequestAssistantContentBlock = RequestTextContent | RequestToolUseContent | RequestThinkingContent | RequestRedactedThinkingContent;
|
|
74
|
+
interface RequestUserMessage {
|
|
75
|
+
readonly role: "user";
|
|
76
|
+
readonly content: ReadonlyArray<RequestUserContentBlock>;
|
|
77
|
+
}
|
|
78
|
+
interface RequestAssistantMessage {
|
|
79
|
+
readonly role: "assistant";
|
|
80
|
+
readonly content: ReadonlyArray<RequestAssistantContentBlock>;
|
|
81
|
+
}
|
|
82
|
+
type RequestMessage = RequestUserMessage | RequestAssistantMessage;
|
|
83
|
+
interface ThinkingConfig {
|
|
84
|
+
readonly type: "enabled";
|
|
85
|
+
readonly budget_tokens: number;
|
|
86
|
+
}
|
|
87
|
+
interface RequestBody {
|
|
88
|
+
readonly model: string;
|
|
89
|
+
readonly messages: ReadonlyArray<RequestMessage>;
|
|
90
|
+
readonly max_tokens: number;
|
|
91
|
+
readonly system?: string;
|
|
92
|
+
readonly temperature?: number;
|
|
93
|
+
readonly top_p?: number;
|
|
94
|
+
readonly top_k?: number;
|
|
95
|
+
readonly stop_sequences?: ReadonlyArray<string>;
|
|
96
|
+
readonly thinking?: ThinkingConfig;
|
|
97
|
+
readonly tools?: ReadonlyArray<Record<string, unknown>>;
|
|
98
|
+
readonly tool_choice?: Record<string, unknown>;
|
|
99
|
+
readonly metadata?: {
|
|
100
|
+
readonly user_id: string;
|
|
101
|
+
};
|
|
102
|
+
readonly output_config?: Record<string, unknown>;
|
|
103
|
+
readonly stream: true;
|
|
104
|
+
}
|
|
105
|
+
declare const buildRequestBody: (params: {
|
|
106
|
+
readonly model: string;
|
|
107
|
+
readonly history: ReadonlyArray<Items.Item>;
|
|
108
|
+
readonly maxTokens: number;
|
|
109
|
+
readonly temperature: Option.Option<number>;
|
|
110
|
+
readonly topP: Option.Option<number>;
|
|
111
|
+
readonly topK: Option.Option<number>;
|
|
112
|
+
readonly stopSequences: Option.Option<ReadonlyArray<string>>;
|
|
113
|
+
readonly thinking: Option.Option<ThinkingConfig>;
|
|
114
|
+
readonly tools: Option.Option<ReadonlyArray<Record<string, unknown>>>;
|
|
115
|
+
readonly toolChoice: Option.Option<Record<string, unknown>>;
|
|
116
|
+
readonly userId: Option.Option<string>;
|
|
117
|
+
readonly outputConfig: Option.Option<Record<string, unknown>>;
|
|
118
|
+
}) => Result.Result<RequestBody, JsonParseError>;
|
|
119
|
+
interface BlockBuffer {
|
|
120
|
+
readonly type: WireContentBlock["type"];
|
|
121
|
+
readonly text: string;
|
|
122
|
+
readonly inputJson: string;
|
|
123
|
+
readonly thinking: string;
|
|
124
|
+
readonly signature: string;
|
|
125
|
+
readonly id: Option.Option<string>;
|
|
126
|
+
readonly name: Option.Option<string>;
|
|
127
|
+
readonly redactedData: Option.Option<string>;
|
|
128
|
+
}
|
|
129
|
+
interface Accumulator {
|
|
130
|
+
readonly blocks: Readonly<Record<number, BlockBuffer>>;
|
|
131
|
+
readonly stopReason: Option.Option<string>;
|
|
132
|
+
readonly usage: Items.Usage;
|
|
133
|
+
}
|
|
134
|
+
declare const emptyAccumulator: Accumulator;
|
|
135
|
+
declare const startBlock: (acc: Accumulator, index: number, block: WireContentBlock) => Accumulator;
|
|
136
|
+
declare const appendTextDelta: (acc: Accumulator, index: number, text: string) => Accumulator;
|
|
137
|
+
declare const appendInputJsonDelta: (acc: Accumulator, index: number, partial: string) => Accumulator;
|
|
138
|
+
declare const appendThinkingDelta: (acc: Accumulator, index: number, thinking: string) => Accumulator;
|
|
139
|
+
declare const appendSignatureDelta: (acc: Accumulator, index: number, signature: string) => Accumulator;
|
|
140
|
+
declare const setStopReason: (acc: Accumulator, reason: string) => Accumulator;
|
|
141
|
+
declare const mergeUsage: (acc: Accumulator, wire: WireUsage) => Accumulator;
|
|
142
|
+
declare const accumulatorToTurn: (acc: Accumulator) => Turn;
|
|
143
|
+
//#endregion
|
|
144
|
+
//#region src/models.d.ts
|
|
145
|
+
/**
|
|
146
|
+
* Known Anthropic model identifiers usable via the Messages API (as of
|
|
147
|
+
* April 2026). The `(string & {})` tail keeps autocomplete on the literals
|
|
148
|
+
* while still accepting any string, so newly-released models work without
|
|
149
|
+
* an SDK update.
|
|
150
|
+
*
|
|
151
|
+
* Reference: https://platform.claude.com/docs/en/docs/about-claude/models
|
|
152
|
+
*
|
|
153
|
+
* Latest tier:
|
|
154
|
+
* - `claude-opus-4-7` - most capable; agentic coding focus. Adaptive
|
|
155
|
+
* thinking only (no extended thinking).
|
|
156
|
+
* - `claude-sonnet-4-6` - speed + intelligence balance. Extended +
|
|
157
|
+
* adaptive thinking.
|
|
158
|
+
* - `claude-haiku-4-5-20251001` (alias `claude-haiku-4-5`) - fastest;
|
|
159
|
+
* extended thinking.
|
|
160
|
+
*
|
|
161
|
+
* Deprecated and retiring 2026-06-15:
|
|
162
|
+
* `claude-sonnet-4-20250514` (`claude-sonnet-4-0`),
|
|
163
|
+
* `claude-opus-4-20250514` (`claude-opus-4-0`).
|
|
164
|
+
*/
|
|
165
|
+
type AnthropicModel = "claude-opus-4-7" | "claude-sonnet-4-6" | "claude-haiku-4-5" | "claude-haiku-4-5-20251001" | "claude-opus-4-6" | "claude-sonnet-4-5" | "claude-sonnet-4-5-20250929" | "claude-opus-4-5" | "claude-opus-4-5-20251101" | "claude-opus-4-1" | "claude-opus-4-1-20250805" | (string & {});
|
|
166
|
+
declare namespace streamEvents_d_exports {
|
|
167
|
+
export { KnownProviderEvent, ProviderEvent, applyEvent, isErrorEvent, isInputJsonDeltaEvent, isMessageStop, isTextDeltaEvent, isThinkingDeltaEvent, isToolUseStartEvent };
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Internal: union of variants we actually know how to decode from the wire.
|
|
171
|
+
* Used as the decode target; failures are caught and re-emitted as `Unknown`.
|
|
172
|
+
*/
|
|
173
|
+
declare const KnownProviderEvent: Schema.Union<readonly [Schema.Struct<{
|
|
174
|
+
readonly type: Schema.Literal<"message_start">;
|
|
175
|
+
readonly message: Schema.Struct<{
|
|
176
|
+
readonly id: Schema.optional<Schema.String>;
|
|
177
|
+
readonly usage: Schema.optional<Schema.Struct<{
|
|
178
|
+
readonly input_tokens: Schema.optional<Schema.Number>;
|
|
179
|
+
readonly output_tokens: Schema.optional<Schema.Number>;
|
|
180
|
+
readonly cache_creation_input_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
|
|
181
|
+
readonly cache_read_input_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
|
|
182
|
+
}>>;
|
|
183
|
+
}>;
|
|
184
|
+
}>, Schema.Struct<{
|
|
185
|
+
readonly type: Schema.Literal<"content_block_start">;
|
|
186
|
+
readonly index: Schema.Number;
|
|
187
|
+
readonly content_block: Schema.Union<readonly [Schema.Struct<{
|
|
188
|
+
readonly type: Schema.Literal<"text">;
|
|
189
|
+
readonly text: Schema.String;
|
|
190
|
+
}>, Schema.Struct<{
|
|
191
|
+
readonly type: Schema.Literal<"tool_use">;
|
|
192
|
+
readonly id: Schema.String;
|
|
193
|
+
readonly name: Schema.String;
|
|
194
|
+
readonly input: Schema.Unknown;
|
|
195
|
+
}>, Schema.Struct<{
|
|
196
|
+
readonly type: Schema.Literal<"thinking">;
|
|
197
|
+
readonly thinking: Schema.String;
|
|
198
|
+
readonly signature: Schema.optional<Schema.String>;
|
|
199
|
+
}>, Schema.Struct<{
|
|
200
|
+
readonly type: Schema.Literal<"redacted_thinking">;
|
|
201
|
+
readonly data: Schema.String;
|
|
202
|
+
}>]>;
|
|
203
|
+
}>, Schema.Struct<{
|
|
204
|
+
readonly type: Schema.Literal<"content_block_delta">;
|
|
205
|
+
readonly index: Schema.Number;
|
|
206
|
+
readonly delta: Schema.Union<readonly [Schema.Struct<{
|
|
207
|
+
readonly type: Schema.Literal<"text_delta">;
|
|
208
|
+
readonly text: Schema.String;
|
|
209
|
+
}>, Schema.Struct<{
|
|
210
|
+
readonly type: Schema.Literal<"input_json_delta">;
|
|
211
|
+
readonly partial_json: Schema.String;
|
|
212
|
+
}>, Schema.Struct<{
|
|
213
|
+
readonly type: Schema.Literal<"thinking_delta">;
|
|
214
|
+
readonly thinking: Schema.String;
|
|
215
|
+
}>, Schema.Struct<{
|
|
216
|
+
readonly type: Schema.Literal<"signature_delta">;
|
|
217
|
+
readonly signature: Schema.String;
|
|
218
|
+
}>]>;
|
|
219
|
+
}>, Schema.Struct<{
|
|
220
|
+
readonly type: Schema.Literal<"content_block_stop">;
|
|
221
|
+
readonly index: Schema.Number;
|
|
222
|
+
}>, Schema.Struct<{
|
|
223
|
+
readonly type: Schema.Literal<"message_delta">;
|
|
224
|
+
readonly delta: Schema.Struct<{
|
|
225
|
+
readonly stop_reason: Schema.optional<Schema.NullOr<Schema.String>>;
|
|
226
|
+
readonly stop_sequence: Schema.optional<Schema.NullOr<Schema.String>>;
|
|
227
|
+
}>;
|
|
228
|
+
readonly usage: Schema.optional<Schema.Struct<{
|
|
229
|
+
readonly input_tokens: Schema.optional<Schema.Number>;
|
|
230
|
+
readonly output_tokens: Schema.optional<Schema.Number>;
|
|
231
|
+
readonly cache_creation_input_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
|
|
232
|
+
readonly cache_read_input_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
|
|
233
|
+
}>>;
|
|
234
|
+
}>, Schema.Struct<{
|
|
235
|
+
readonly type: Schema.Literal<"message_stop">;
|
|
236
|
+
}>, Schema.Struct<{
|
|
237
|
+
readonly type: Schema.Literal<"ping">;
|
|
238
|
+
}>, Schema.Struct<{
|
|
239
|
+
readonly type: Schema.Literal<"error">;
|
|
240
|
+
readonly error: Schema.optional<Schema.Struct<{
|
|
241
|
+
readonly type: Schema.optional<Schema.String>;
|
|
242
|
+
readonly message: Schema.optional<Schema.String>;
|
|
243
|
+
}>>;
|
|
244
|
+
}>]>;
|
|
245
|
+
/**
|
|
246
|
+
* Public: every event the native stream can emit. Discriminated on `type`.
|
|
247
|
+
* The `_unknown` branch closes the cardinality so downstream `Match.exhaustive`
|
|
248
|
+
* cannot silently miss a wire event we didn't model.
|
|
249
|
+
*/
|
|
250
|
+
declare const ProviderEvent: Schema.Union<readonly [Schema.Struct<{
|
|
251
|
+
readonly type: Schema.Literal<"message_start">;
|
|
252
|
+
readonly message: Schema.Struct<{
|
|
253
|
+
readonly id: Schema.optional<Schema.String>;
|
|
254
|
+
readonly usage: Schema.optional<Schema.Struct<{
|
|
255
|
+
readonly input_tokens: Schema.optional<Schema.Number>;
|
|
256
|
+
readonly output_tokens: Schema.optional<Schema.Number>;
|
|
257
|
+
readonly cache_creation_input_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
|
|
258
|
+
readonly cache_read_input_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
|
|
259
|
+
}>>;
|
|
260
|
+
}>;
|
|
261
|
+
}>, Schema.Struct<{
|
|
262
|
+
readonly type: Schema.Literal<"content_block_start">;
|
|
263
|
+
readonly index: Schema.Number;
|
|
264
|
+
readonly content_block: Schema.Union<readonly [Schema.Struct<{
|
|
265
|
+
readonly type: Schema.Literal<"text">;
|
|
266
|
+
readonly text: Schema.String;
|
|
267
|
+
}>, Schema.Struct<{
|
|
268
|
+
readonly type: Schema.Literal<"tool_use">;
|
|
269
|
+
readonly id: Schema.String;
|
|
270
|
+
readonly name: Schema.String;
|
|
271
|
+
readonly input: Schema.Unknown;
|
|
272
|
+
}>, Schema.Struct<{
|
|
273
|
+
readonly type: Schema.Literal<"thinking">;
|
|
274
|
+
readonly thinking: Schema.String;
|
|
275
|
+
readonly signature: Schema.optional<Schema.String>;
|
|
276
|
+
}>, Schema.Struct<{
|
|
277
|
+
readonly type: Schema.Literal<"redacted_thinking">;
|
|
278
|
+
readonly data: Schema.String;
|
|
279
|
+
}>]>;
|
|
280
|
+
}>, Schema.Struct<{
|
|
281
|
+
readonly type: Schema.Literal<"content_block_delta">;
|
|
282
|
+
readonly index: Schema.Number;
|
|
283
|
+
readonly delta: Schema.Union<readonly [Schema.Struct<{
|
|
284
|
+
readonly type: Schema.Literal<"text_delta">;
|
|
285
|
+
readonly text: Schema.String;
|
|
286
|
+
}>, Schema.Struct<{
|
|
287
|
+
readonly type: Schema.Literal<"input_json_delta">;
|
|
288
|
+
readonly partial_json: Schema.String;
|
|
289
|
+
}>, Schema.Struct<{
|
|
290
|
+
readonly type: Schema.Literal<"thinking_delta">;
|
|
291
|
+
readonly thinking: Schema.String;
|
|
292
|
+
}>, Schema.Struct<{
|
|
293
|
+
readonly type: Schema.Literal<"signature_delta">;
|
|
294
|
+
readonly signature: Schema.String;
|
|
295
|
+
}>]>;
|
|
296
|
+
}>, Schema.Struct<{
|
|
297
|
+
readonly type: Schema.Literal<"content_block_stop">;
|
|
298
|
+
readonly index: Schema.Number;
|
|
299
|
+
}>, Schema.Struct<{
|
|
300
|
+
readonly type: Schema.Literal<"message_delta">;
|
|
301
|
+
readonly delta: Schema.Struct<{
|
|
302
|
+
readonly stop_reason: Schema.optional<Schema.NullOr<Schema.String>>;
|
|
303
|
+
readonly stop_sequence: Schema.optional<Schema.NullOr<Schema.String>>;
|
|
304
|
+
}>;
|
|
305
|
+
readonly usage: Schema.optional<Schema.Struct<{
|
|
306
|
+
readonly input_tokens: Schema.optional<Schema.Number>;
|
|
307
|
+
readonly output_tokens: Schema.optional<Schema.Number>;
|
|
308
|
+
readonly cache_creation_input_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
|
|
309
|
+
readonly cache_read_input_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
|
|
310
|
+
}>>;
|
|
311
|
+
}>, Schema.Struct<{
|
|
312
|
+
readonly type: Schema.Literal<"message_stop">;
|
|
313
|
+
}>, Schema.Struct<{
|
|
314
|
+
readonly type: Schema.Literal<"ping">;
|
|
315
|
+
}>, Schema.Struct<{
|
|
316
|
+
readonly type: Schema.Literal<"error">;
|
|
317
|
+
readonly error: Schema.optional<Schema.Struct<{
|
|
318
|
+
readonly type: Schema.optional<Schema.String>;
|
|
319
|
+
readonly message: Schema.optional<Schema.String>;
|
|
320
|
+
}>>;
|
|
321
|
+
}>, Schema.Struct<{
|
|
322
|
+
readonly type: Schema.Literal<"_unknown">;
|
|
323
|
+
readonly raw: Schema.Unknown;
|
|
324
|
+
}>]>;
|
|
325
|
+
type ProviderEvent = typeof ProviderEvent.Type;
|
|
326
|
+
declare const applyEvent: (acc: Accumulator, event: ProviderEvent) => Accumulator;
|
|
327
|
+
declare const isTextDeltaEvent: (event: ProviderEvent) => event is Extract<ProviderEvent, {
|
|
328
|
+
type: "content_block_delta";
|
|
329
|
+
delta: {
|
|
330
|
+
type: "text_delta";
|
|
331
|
+
};
|
|
332
|
+
}>;
|
|
333
|
+
declare const isThinkingDeltaEvent: (event: ProviderEvent) => event is Extract<ProviderEvent, {
|
|
334
|
+
type: "content_block_delta";
|
|
335
|
+
delta: {
|
|
336
|
+
type: "thinking_delta";
|
|
337
|
+
};
|
|
338
|
+
}>;
|
|
339
|
+
declare const isInputJsonDeltaEvent: (event: ProviderEvent) => event is Extract<ProviderEvent, {
|
|
340
|
+
type: "content_block_delta";
|
|
341
|
+
delta: {
|
|
342
|
+
type: "input_json_delta";
|
|
343
|
+
};
|
|
344
|
+
}>;
|
|
345
|
+
declare const isToolUseStartEvent: (event: ProviderEvent) => event is Extract<ProviderEvent, {
|
|
346
|
+
type: "content_block_start";
|
|
347
|
+
content_block: {
|
|
348
|
+
type: "tool_use";
|
|
349
|
+
};
|
|
350
|
+
}>;
|
|
351
|
+
declare const isMessageStop: (event: ProviderEvent) => boolean;
|
|
352
|
+
declare const isErrorEvent: (event: ProviderEvent) => event is Extract<ProviderEvent, {
|
|
353
|
+
type: "error";
|
|
354
|
+
}>;
|
|
355
|
+
//#endregion
|
|
356
|
+
//#region src/Anthropic.d.ts
|
|
357
|
+
interface AnthropicRequest extends Omit<CommonRequest, "model"> {
|
|
358
|
+
/**
|
|
359
|
+
* Narrows `CommonRequest.model` (`string`) to the typed `AnthropicModel`
|
|
360
|
+
* literal union for autocomplete.
|
|
361
|
+
*/
|
|
362
|
+
readonly model: AnthropicModel;
|
|
363
|
+
/**
|
|
364
|
+
* Top-K nucleus sampling parameter. Anthropic-specific; not exposed on the
|
|
365
|
+
* common surface.
|
|
366
|
+
*/
|
|
367
|
+
readonly topK?: number;
|
|
368
|
+
/** Stop sequences that abort generation when matched. */
|
|
369
|
+
readonly stopSequences?: ReadonlyArray<string>;
|
|
370
|
+
/**
|
|
371
|
+
* Extended thinking configuration. `0` budget is equivalent to disabled.
|
|
372
|
+
* Only the `claude-sonnet-4-x`, `claude-haiku-4-x`, and pre-Opus-4.7
|
|
373
|
+
* model lines support extended thinking.
|
|
374
|
+
*/
|
|
375
|
+
readonly thinking?: ThinkingConfig;
|
|
376
|
+
/**
|
|
377
|
+
* `metadata.user_id` on the wire. End-user tracking identifier.
|
|
378
|
+
*/
|
|
379
|
+
readonly user?: string;
|
|
380
|
+
}
|
|
381
|
+
interface AnthropicService {
|
|
382
|
+
/**
|
|
383
|
+
* Stream the provider's native event vocabulary (post-SSE-decode).
|
|
384
|
+
* Use this when you need full vendor fidelity (e.g. `signature_delta` for
|
|
385
|
+
* encrypted reasoning state). For provider-portable code, use `streamTurn`.
|
|
386
|
+
*/
|
|
387
|
+
readonly streamNative: (request: AnthropicRequest) => Stream.Stream<ProviderEvent, AiError.AiError>;
|
|
388
|
+
/**
|
|
389
|
+
* Stream canonical `TurnEvent`s. Implemented as
|
|
390
|
+
* `streamNative |> toCanonical`.
|
|
391
|
+
*/
|
|
392
|
+
readonly streamTurn: (request: AnthropicRequest) => Stream.Stream<TurnEvent, AiError.AiError>;
|
|
393
|
+
/**
|
|
394
|
+
* Project a stream of native `ProviderEvent`s into canonical `TurnEvent`s.
|
|
395
|
+
* Stateful (threads an `Accumulator` for tool-call lookup and
|
|
396
|
+
* accumulator-to-Turn assembly).
|
|
397
|
+
*/
|
|
398
|
+
readonly toCanonical: <E, R>(s: Stream.Stream<ProviderEvent, E, R>) => Stream.Stream<TurnEvent, E, R>;
|
|
399
|
+
}
|
|
400
|
+
declare const Anthropic_base: Context.ServiceClass<Anthropic, "@betalyra/effect-uai/providers/anthropic/Anthropic", AnthropicService>;
|
|
401
|
+
/**
|
|
402
|
+
* Provider-typed service tag. Yield this when you want Anthropic-specific
|
|
403
|
+
* options (`topK`, `stopSequences`, `thinking`); yield the generic
|
|
404
|
+
* `LanguageModel` tag for provider-portable code. Both are registered by
|
|
405
|
+
* `layer`.
|
|
406
|
+
*/
|
|
407
|
+
declare class Anthropic extends Anthropic_base {}
|
|
408
|
+
interface Config {
|
|
409
|
+
readonly apiKey: Redacted.Redacted;
|
|
410
|
+
readonly baseUrl?: string;
|
|
411
|
+
/**
|
|
412
|
+
* Default `max_tokens` for requests that don't override via
|
|
413
|
+
* `request.maxOutputTokens`. Anthropic requires this field; we default to
|
|
414
|
+
* 4096 if neither is set.
|
|
415
|
+
*/
|
|
416
|
+
readonly defaultMaxTokens?: number;
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Project a stream of native `ProviderEvent`s into canonical `TurnEvent`s.
|
|
420
|
+
* Threads a fresh `Accumulator` per stream so tool-call lookup and
|
|
421
|
+
* `accumulatorToTurn` assembly work correctly across the run.
|
|
422
|
+
*/
|
|
423
|
+
declare const toCanonical: <E, R>(s: Stream.Stream<ProviderEvent, E, R>) => Stream.Stream<TurnEvent, E, R>;
|
|
424
|
+
/**
|
|
425
|
+
* Build an `AnthropicService` value. For Layer-based setup, prefer `layer`.
|
|
426
|
+
*/
|
|
427
|
+
declare const make: (cfg: Config) => Effect.Effect<AnthropicService, never, HttpClient.HttpClient>;
|
|
428
|
+
/**
|
|
429
|
+
* Layer that registers both the provider-specific `Anthropic` tag and the
|
|
430
|
+
* generic `LanguageModel` tag, sharing one underlying implementation.
|
|
431
|
+
*
|
|
432
|
+
* The generic tag accepts `CommonRequest`; the typed tag accepts the full
|
|
433
|
+
* `AnthropicRequest` surface.
|
|
434
|
+
*/
|
|
435
|
+
declare const layer: (cfg: Config) => Layer.Layer<Anthropic | LanguageModel, never, HttpClient.HttpClient>;
|
|
436
|
+
//#endregion
|
|
437
|
+
export { Anthropic, AnthropicModel, AnthropicRequest, AnthropicService, Config, codec_d_exports as codec, layer, make, streamEvents_d_exports as streamEvents, toCanonical };
|
|
438
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/codec.ts","../src/models.ts","../src/streamEvents.ts","../src/Anthropic.ts"],"mappings":";;;;;;;;;;;;cAkCa,gBAAA,EAAgB,MAAA,CAAA,KAAA,WAAA,MAAA,CAAA,MAAA;EAAA;;;;;;;;;;;;;;;KAMjB,gBAAA,UAA0B,gBAAA,CAAiB,IAAA;AAAA,cAEjD,SAAA,EAAS,MAAA,CAAA,MAAA;EAAA;;;;;KAMH,SAAA,UAAmB,SAAA,CAAU,IAAA;AAAA,UAM/B,kBAAA;EAAA,SACC,IAAA;EAAA,SACA,IAAA;AAAA;AAAA,UAGD,wBAAA;EAAA,SACC,IAAA;EAAA,SACA,WAAA;EAAA,SACA,OAAA;AAAA;AAAA,UAGD,qBAAA;EAAA,SACC,IAAA;EAAA,SACA,EAAA;EAAA,SACA,IAAA;EAAA,SACA,KAAA;AAAA;AAAA,UAGD,sBAAA;EAAA,SACC,IAAA;EAAA,SACA,QAAA;EAAA,SACA,SAAA;AAAA;AAAA,UAGD,8BAAA;EAAA,SACC,IAAA;EAAA,SACA,IAAA;AAAA;AAAA,UAGD,mBAAA;EAAA,SACC,IAAA;EAAA,SACA,MAAA;IAAA,SACM,IAAA;IAAA,SAAsB,GAAA;EAAA;IAAA,SACtB,IAAA;IAAA,SAAyB,UAAA;IAAA,SAA6B,IAAA;EAAA;AAAA;AAAA,KAGlE,uBAAA,GAA0B,kBAAA,GAAqB,wBAAA,GAA2B,mBAAA;AAAA,KAE1E,4BAAA,GACD,kBAAA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