@acentric/llm 0.0.0-stage → 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/README.md +128 -2
- package/dist/catalog/anthropic.d.ts +77 -0
- package/dist/catalog/anthropic.d.ts.map +1 -0
- package/dist/catalog/anthropic.js +73 -0
- package/dist/catalog/anthropic.js.map +1 -0
- package/dist/catalog/fireworks.d.ts +93 -0
- package/dist/catalog/fireworks.d.ts.map +1 -0
- package/dist/catalog/fireworks.js +117 -0
- package/dist/catalog/fireworks.js.map +1 -0
- package/dist/catalog/openai.d.ts +103 -0
- package/dist/catalog/openai.d.ts.map +1 -0
- package/dist/catalog/openai.js +102 -0
- package/dist/catalog/openai.js.map +1 -0
- package/dist/client/anthropic/errors.d.ts +4 -0
- package/dist/client/anthropic/errors.d.ts.map +1 -0
- package/dist/client/anthropic/errors.js +80 -0
- package/dist/client/anthropic/errors.js.map +1 -0
- package/dist/client/anthropic/request.d.ts +9 -0
- package/dist/client/anthropic/request.d.ts.map +1 -0
- package/dist/client/anthropic/request.js +232 -0
- package/dist/client/anthropic/request.js.map +1 -0
- package/dist/client/anthropic/response.d.ts +13 -0
- package/dist/client/anthropic/response.d.ts.map +1 -0
- package/dist/client/anthropic/response.js +207 -0
- package/dist/client/anthropic/response.js.map +1 -0
- package/dist/client/direct.d.ts +18 -0
- package/dist/client/direct.d.ts.map +1 -0
- package/dist/client/direct.js +104 -0
- package/dist/client/direct.js.map +1 -0
- package/dist/client/gateway.d.ts +43 -0
- package/dist/client/gateway.d.ts.map +1 -0
- package/dist/client/gateway.js +72 -0
- package/dist/client/gateway.js.map +1 -0
- package/dist/client/http.d.ts +41 -0
- package/dist/client/http.d.ts.map +1 -0
- package/dist/client/http.js +190 -0
- package/dist/client/http.js.map +1 -0
- package/dist/client/llm.d.ts +41 -0
- package/dist/client/llm.d.ts.map +1 -0
- package/dist/client/llm.js +100 -0
- package/dist/client/llm.js.map +1 -0
- package/dist/client/responses/errors.d.ts +13 -0
- package/dist/client/responses/errors.d.ts.map +1 -0
- package/dist/client/responses/errors.js +144 -0
- package/dist/client/responses/errors.js.map +1 -0
- package/dist/client/responses/profile.d.ts +50 -0
- package/dist/client/responses/profile.d.ts.map +1 -0
- package/dist/client/responses/profile.js +4 -0
- package/dist/client/responses/profile.js.map +1 -0
- package/dist/client/responses/providers.d.ts +4 -0
- package/dist/client/responses/providers.d.ts.map +1 -0
- package/dist/client/responses/providers.js +75 -0
- package/dist/client/responses/providers.js.map +1 -0
- package/dist/client/responses/request.d.ts +5 -0
- package/dist/client/responses/request.d.ts.map +1 -0
- package/dist/client/responses/request.js +248 -0
- package/dist/client/responses/request.js.map +1 -0
- package/dist/client/responses/response.d.ts +10 -0
- package/dist/client/responses/response.d.ts.map +1 -0
- package/dist/client/responses/response.js +176 -0
- package/dist/client/responses/response.js.map +1 -0
- package/dist/client/util.d.ts +21 -0
- package/dist/client/util.d.ts.map +1 -0
- package/dist/client/util.js +63 -0
- package/dist/client/util.js.map +1 -0
- package/dist/client.d.ts +9 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +10 -0
- package/dist/client.js.map +1 -0
- package/dist/content.d.ts +38 -0
- package/dist/content.d.ts.map +1 -0
- package/dist/content.js +5 -0
- package/dist/content.js.map +1 -0
- package/dist/convert/anthropic.d.ts +3 -0
- package/dist/convert/anthropic.d.ts.map +1 -0
- package/dist/convert/anthropic.js +114 -0
- package/dist/convert/anthropic.js.map +1 -0
- package/dist/convert/index.d.ts +12 -0
- package/dist/convert/index.d.ts.map +1 -0
- package/dist/convert/index.js +221 -0
- package/dist/convert/index.js.map +1 -0
- package/dist/convert/responses.d.ts +3 -0
- package/dist/convert/responses.d.ts.map +1 -0
- package/dist/convert/responses.js +197 -0
- package/dist/convert/responses.js.map +1 -0
- package/dist/convert/types.d.ts +88 -0
- package/dist/convert/types.d.ts.map +1 -0
- package/dist/convert/types.js +3 -0
- package/dist/convert/types.js.map +1 -0
- package/dist/cost.d.ts +18 -0
- package/dist/cost.d.ts.map +1 -0
- package/dist/cost.js +49 -0
- package/dist/cost.js.map +1 -0
- package/dist/failure.d.ts +90 -0
- package/dist/failure.d.ts.map +1 -0
- package/dist/failure.js +111 -0
- package/dist/failure.js.map +1 -0
- package/dist/fields.d.ts +14 -0
- package/dist/fields.d.ts.map +1 -0
- package/dist/fields.js +3 -0
- package/dist/fields.js.map +1 -0
- package/dist/formats/anthropic.d.ts +152 -0
- package/dist/formats/anthropic.d.ts.map +1 -0
- package/dist/formats/anthropic.js +7 -0
- package/dist/formats/anthropic.js.map +1 -0
- package/dist/formats/responses.d.ts +179 -0
- package/dist/formats/responses.d.ts.map +1 -0
- package/dist/formats/responses.js +9 -0
- package/dist/formats/responses.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/json.d.ts +6 -0
- package/dist/json.d.ts.map +1 -0
- package/dist/json.js +10 -0
- package/dist/json.js.map +1 -0
- package/dist/messages.d.ts +82 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +4 -0
- package/dist/messages.js.map +1 -0
- package/dist/model.d.ts +36 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +4 -0
- package/dist/model.js.map +1 -0
- package/dist/models.d.ts +9 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +19 -0
- package/dist/models.js.map +1 -0
- package/dist/providers/anthropic.d.ts +130 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +5 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/fireworks.d.ts +108 -0
- package/dist/providers/fireworks.d.ts.map +1 -0
- package/dist/providers/fireworks.js +6 -0
- package/dist/providers/fireworks.js.map +1 -0
- package/dist/providers/openai.d.ts +193 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +4 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers.d.ts +50 -0
- package/dist/providers.d.ts.map +1 -0
- package/dist/providers.js +5 -0
- package/dist/providers.js.map +1 -0
- package/dist/read/anthropic.d.ts +15 -0
- package/dist/read/anthropic.d.ts.map +1 -0
- package/dist/read/anthropic.js +56 -0
- package/dist/read/anthropic.js.map +1 -0
- package/dist/read/responses.d.ts +26 -0
- package/dist/read/responses.d.ts.map +1 -0
- package/dist/read/responses.js +121 -0
- package/dist/read/responses.js.map +1 -0
- package/dist/read.d.ts +32 -0
- package/dist/read.d.ts.map +1 -0
- package/dist/read.js +73 -0
- package/dist/read.js.map +1 -0
- package/dist/request.d.ts +23 -0
- package/dist/request.d.ts.map +1 -0
- package/dist/request.js +4 -0
- package/dist/request.js.map +1 -0
- package/dist/response.d.ts +38 -0
- package/dist/response.d.ts.map +1 -0
- package/dist/response.js +4 -0
- package/dist/response.js.map +1 -0
- package/dist/secrets.d.ts +22 -0
- package/dist/secrets.d.ts.map +1 -0
- package/dist/secrets.js +35 -0
- package/dist/secrets.js.map +1 -0
- package/dist/tool-calls.d.ts +28 -0
- package/dist/tool-calls.d.ts.map +1 -0
- package/dist/tool-calls.js +4 -0
- package/dist/tool-calls.js.map +1 -0
- package/dist/tools.d.ts +33 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +4 -0
- package/dist/tools.js.map +1 -0
- package/dist/usage.d.ts +31 -0
- package/dist/usage.d.ts.map +1 -0
- package/dist/usage.js +3 -0
- package/dist/usage.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +6 -0
- package/dist/version.js.map +1 -0
- package/package.json +33 -3
- package/src/catalog/anthropic.ts +83 -0
- package/src/catalog/fireworks.ts +121 -0
- package/src/catalog/openai.ts +133 -0
- package/src/client/anthropic/errors.ts +88 -0
- package/src/client/anthropic/request.ts +272 -0
- package/src/client/anthropic/response.ts +236 -0
- package/src/client/direct.ts +157 -0
- package/src/client/gateway.ts +120 -0
- package/src/client/http.ts +231 -0
- package/src/client/llm.ts +151 -0
- package/src/client/responses/errors.ts +166 -0
- package/src/client/responses/profile.ts +55 -0
- package/src/client/responses/providers.ts +87 -0
- package/src/client/responses/request.ts +326 -0
- package/src/client/responses/response.ts +203 -0
- package/src/client/util.ts +81 -0
- package/src/client.ts +20 -0
- package/src/content.ts +46 -0
- package/src/convert/anthropic.ts +115 -0
- package/src/convert/index.ts +275 -0
- package/src/convert/responses.ts +203 -0
- package/src/convert/types.ts +104 -0
- package/src/cost.ts +71 -0
- package/src/failure.ts +164 -0
- package/src/fields.ts +13 -0
- package/src/formats/anthropic.ts +191 -0
- package/src/formats/responses.ts +207 -0
- package/src/index.ts +44 -0
- package/src/json.ts +13 -0
- package/src/messages.ts +99 -0
- package/src/model.ts +41 -0
- package/src/models.ts +23 -0
- package/src/providers/anthropic.ts +137 -0
- package/src/providers/fireworks.ts +140 -0
- package/src/providers/openai.ts +275 -0
- package/src/providers.ts +59 -0
- package/src/read/anthropic.ts +64 -0
- package/src/read/responses.ts +159 -0
- package/src/read.ts +108 -0
- package/src/request.ts +28 -0
- package/src/response.ts +44 -0
- package/src/secrets.ts +40 -0
- package/src/tool-calls.ts +33 -0
- package/src/tools.ts +38 -0
- package/src/usage.ts +33 -0
- package/src/version.ts +6 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// An Anthropic answer read into an LlmResponse.
|
|
2
|
+
import { estimateCost } from '../../cost.js';
|
|
3
|
+
import { isArray, isObject } from '../../json.js';
|
|
4
|
+
import { findModel } from '../../models.js';
|
|
5
|
+
import { invalidResponse, omit, tokens } from '../util.js';
|
|
6
|
+
const PROVIDER = 'anthropic';
|
|
7
|
+
/** Response fields the response already holds elsewhere. */
|
|
8
|
+
const ENVELOPE_FIELDS = ['id', 'type', 'role', 'content', 'model', 'stop_reason', 'usage'];
|
|
9
|
+
/** Usage fields mapped to `Usage`; the rest of `usage` stays in `native`. */
|
|
10
|
+
const USAGE_FIELDS = [
|
|
11
|
+
'input_tokens',
|
|
12
|
+
'output_tokens',
|
|
13
|
+
'cache_creation_input_tokens',
|
|
14
|
+
'cache_read_input_tokens',
|
|
15
|
+
'output_tokens_details',
|
|
16
|
+
];
|
|
17
|
+
const STOP_REASONS = {
|
|
18
|
+
end_turn: 'stop',
|
|
19
|
+
stop_sequence: 'stop',
|
|
20
|
+
max_tokens: 'length',
|
|
21
|
+
model_context_window_exceeded: 'length',
|
|
22
|
+
tool_use: 'tool_use',
|
|
23
|
+
pause_turn: 'pause_turn',
|
|
24
|
+
// Compaction paused after summarizing; sending the message back goes on.
|
|
25
|
+
compaction: 'pause_turn',
|
|
26
|
+
refusal: 'refusal',
|
|
27
|
+
};
|
|
28
|
+
/** The only categories in which a refusal before any output is billed. */
|
|
29
|
+
const BILLED_REFUSALS = new Set([
|
|
30
|
+
'bio',
|
|
31
|
+
'frontier_llm',
|
|
32
|
+
'reasoning_extraction',
|
|
33
|
+
]);
|
|
34
|
+
export function parseResponse(request, answer) {
|
|
35
|
+
const body = answer.body;
|
|
36
|
+
if (!isObject(body) || body['type'] !== 'message' || !isArray(body['content'])) {
|
|
37
|
+
throw invalidResponse(PROVIDER, 'Anthropic did not answer with a message', body);
|
|
38
|
+
}
|
|
39
|
+
const content = body['content'];
|
|
40
|
+
const id = typeof body['id'] === 'string' && body['id'] !== '' ? body['id'] : undefined;
|
|
41
|
+
const stop = typeof body['stop_reason'] === 'string' ? body['stop_reason'] : undefined;
|
|
42
|
+
const stopReason = (stop === undefined ? undefined : STOP_REASONS[stop]) ?? 'stop';
|
|
43
|
+
const served = typeof body['model'] === 'string' ? body['model'] : request.model;
|
|
44
|
+
const rawUsage = isObject(body['usage']) ? body['usage'] : undefined;
|
|
45
|
+
const usage = rawUsage === undefined ? undefined : readUsage(rawUsage);
|
|
46
|
+
const cost = rawUsage === undefined || freeRefusal(body) ? undefined : estimate(rawUsage, served, request);
|
|
47
|
+
// How cache writes split by time to live says nothing when nothing was written.
|
|
48
|
+
const extras = rawUsage === undefined
|
|
49
|
+
? undefined
|
|
50
|
+
: omit(rawUsage, usage?.cacheWrite === 0 ? [...USAGE_FIELDS, 'cache_creation'] : USAGE_FIELDS);
|
|
51
|
+
const native = {
|
|
52
|
+
...omit(body, ENVELOPE_FIELDS),
|
|
53
|
+
...(extras === undefined || Object.keys(extras).length === 0 ? {} : { usage: extras }),
|
|
54
|
+
};
|
|
55
|
+
const response = {
|
|
56
|
+
...(id === undefined ? {} : { id }),
|
|
57
|
+
provider: PROVIDER,
|
|
58
|
+
model: request.model,
|
|
59
|
+
...(served === request.model ? {} : { responseModel: served }),
|
|
60
|
+
message: {
|
|
61
|
+
role: 'assistant',
|
|
62
|
+
provider: PROVIDER,
|
|
63
|
+
api: 'anthropic-messages',
|
|
64
|
+
model: request.model,
|
|
65
|
+
...(id === undefined ? {} : { id }),
|
|
66
|
+
timestamp: answer.timestamp,
|
|
67
|
+
content,
|
|
68
|
+
},
|
|
69
|
+
stopReason,
|
|
70
|
+
...(stop === undefined || stop === stopReason ? {} : { providerStopReason: stop }),
|
|
71
|
+
...(usage === undefined ? {} : { usage: cost === undefined ? usage : { ...usage, cost } }),
|
|
72
|
+
native,
|
|
73
|
+
durationMs: answer.durationMs,
|
|
74
|
+
timestamp: answer.timestamp,
|
|
75
|
+
};
|
|
76
|
+
// The content is Anthropic's own blocks, which the format's types describe.
|
|
77
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion
|
|
78
|
+
return response;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Usage as the format reports it: `input_tokens` is only the uncached part.
|
|
82
|
+
* Where a request sampled more than once (compaction, a fallback model), the
|
|
83
|
+
* iterations hold the whole bill and the top-level counts leave some out.
|
|
84
|
+
*/
|
|
85
|
+
export function readUsage(raw) {
|
|
86
|
+
const iterations = samplings(raw);
|
|
87
|
+
const counted = iterations === undefined ? counts(raw) : sum(iterations.map((one) => one.usage));
|
|
88
|
+
const details = isObject(raw['output_tokens_details']) ? raw['output_tokens_details'] : {};
|
|
89
|
+
const reasoning = tokens(details['thinking_tokens']);
|
|
90
|
+
return { ...counted, ...(reasoning === undefined ? {} : { reasoning }) };
|
|
91
|
+
}
|
|
92
|
+
/** One sampling's tokens. */
|
|
93
|
+
function counts(raw) {
|
|
94
|
+
const input = tokens(raw['input_tokens']);
|
|
95
|
+
const output = tokens(raw['output_tokens']);
|
|
96
|
+
// Anthropic leaves the cache counts out when nothing was cached.
|
|
97
|
+
const read = raw['cache_read_input_tokens'];
|
|
98
|
+
const written = raw['cache_creation_input_tokens'];
|
|
99
|
+
const cacheRead = read === undefined || read === null ? 0 : tokens(read);
|
|
100
|
+
const cacheWrite = written === undefined || written === null ? 0 : tokens(written);
|
|
101
|
+
const totalInput = input === undefined || cacheRead === undefined || cacheWrite === undefined
|
|
102
|
+
? undefined
|
|
103
|
+
: input + cacheRead + cacheWrite;
|
|
104
|
+
return {
|
|
105
|
+
...(totalInput === undefined ? {} : { totalInput }),
|
|
106
|
+
...(input === undefined ? {} : { input }),
|
|
107
|
+
...(output === undefined ? {} : { output }),
|
|
108
|
+
...(cacheRead === undefined ? {} : { cacheRead }),
|
|
109
|
+
...(cacheWrite === undefined ? {} : { cacheWrite }),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function samplings(raw) {
|
|
113
|
+
const iterations = raw['iterations'];
|
|
114
|
+
if (!isArray(iterations) || iterations.length === 0)
|
|
115
|
+
return undefined;
|
|
116
|
+
return iterations.filter(isObject).map((iteration) => {
|
|
117
|
+
const model = iteration['model'];
|
|
118
|
+
return {
|
|
119
|
+
model: typeof model === 'string' ? model : undefined,
|
|
120
|
+
usage: counts(iteration),
|
|
121
|
+
cacheWrite1h: hourWrites(iteration),
|
|
122
|
+
};
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
function hourWrites(raw) {
|
|
126
|
+
if (!raw['cache_creation_input_tokens'])
|
|
127
|
+
return 0;
|
|
128
|
+
const split = raw['cache_creation'];
|
|
129
|
+
return isObject(split) ? tokens(split['ephemeral_1h_input_tokens'] ?? 0) : undefined;
|
|
130
|
+
}
|
|
131
|
+
function sum(usages) {
|
|
132
|
+
const keys = ['totalInput', 'input', 'output', 'cacheRead', 'cacheWrite'];
|
|
133
|
+
const total = {};
|
|
134
|
+
for (const key of keys) {
|
|
135
|
+
if (usages.every((usage) => usage[key] !== undefined)) {
|
|
136
|
+
total[key] = usages.reduce((all, usage) => all + (usage[key] ?? 0), 0);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return total;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* The cost at standard prices, each sampling at its own model's prices and
|
|
143
|
+
* long-context tier, times the multiplier for where and how it ran. Undefined
|
|
144
|
+
* when any part cannot be priced.
|
|
145
|
+
*/
|
|
146
|
+
function estimate(raw, served, request) {
|
|
147
|
+
const multiplier = priceMultiplier(raw, request);
|
|
148
|
+
if (multiplier === undefined)
|
|
149
|
+
return undefined;
|
|
150
|
+
const each = samplings(raw) ?? [
|
|
151
|
+
{ model: served, usage: counts(raw), cacheWrite1h: hourWrites(raw) },
|
|
152
|
+
];
|
|
153
|
+
const costs = [];
|
|
154
|
+
for (const { model, usage, cacheWrite1h } of each) {
|
|
155
|
+
if (cacheWrite1h === undefined)
|
|
156
|
+
return undefined;
|
|
157
|
+
const pricing = findModel(PROVIDER, model ?? served)?.pricing;
|
|
158
|
+
const cost = estimateCost(usage, pricing, { multiplier, cacheWrite1h });
|
|
159
|
+
if (cost === undefined)
|
|
160
|
+
return undefined;
|
|
161
|
+
costs.push(cost);
|
|
162
|
+
}
|
|
163
|
+
const [first] = costs;
|
|
164
|
+
if (costs.length === 1 && first !== undefined)
|
|
165
|
+
return first;
|
|
166
|
+
const parts = ['input', 'output', 'cacheRead', 'cacheWrite', 'total'];
|
|
167
|
+
const total = {};
|
|
168
|
+
for (const cost of costs) {
|
|
169
|
+
for (const part of parts) {
|
|
170
|
+
const value = cost[part];
|
|
171
|
+
if (value !== undefined)
|
|
172
|
+
total[part] = (total[part] ?? 0) + value;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return { source: 'estimated', total: 0, ...total };
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Standard prices hold unless the response says otherwise: inference kept in
|
|
179
|
+
* the US costs 1.1 times as much, fast mode twice, a batch half. Priority Tier
|
|
180
|
+
* has no published price.
|
|
181
|
+
*/
|
|
182
|
+
function priceMultiplier(raw, request) {
|
|
183
|
+
let multiplier = 1;
|
|
184
|
+
const tier = raw['service_tier'];
|
|
185
|
+
if (tier === 'batch')
|
|
186
|
+
multiplier *= 0.5;
|
|
187
|
+
else if (tier !== undefined && tier !== null && tier !== 'standard')
|
|
188
|
+
return undefined;
|
|
189
|
+
if (raw['inference_geo'] === 'us')
|
|
190
|
+
multiplier *= 1.1;
|
|
191
|
+
const speed = raw['speed'];
|
|
192
|
+
if (speed === 'fast')
|
|
193
|
+
multiplier *= 2;
|
|
194
|
+
else if ((speed === undefined || speed === null) && request.options?.speed === 'fast')
|
|
195
|
+
return undefined;
|
|
196
|
+
return multiplier;
|
|
197
|
+
}
|
|
198
|
+
/** A refusal before any output, in a category Anthropic does not bill. */
|
|
199
|
+
function freeRefusal(body) {
|
|
200
|
+
const content = body['content'];
|
|
201
|
+
if (body['stop_reason'] !== 'refusal' || !isArray(content) || content.length > 0)
|
|
202
|
+
return false;
|
|
203
|
+
const details = body['stop_details'];
|
|
204
|
+
const category = isObject(details) ? details['category'] : undefined;
|
|
205
|
+
return typeof category !== 'string' || !BILLED_REFUSALS.has(category);
|
|
206
|
+
}
|
|
207
|
+
//# sourceMappingURL=response.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response.js","sourceRoot":"","sources":["../../../src/client/anthropic/response.ts"],"names":[],"mappings":"AAAA,gDAAgD;AAIhD,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,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OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACrE,OAAO,OAAO,QAAQ,KAAK,QAAQ,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;AACxE,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Json } from '@acentric/types';
|
|
2
|
+
import type { Credentials, Provider } from '../providers.js';
|
|
3
|
+
import type { LlmRequest } from '../request.js';
|
|
4
|
+
import type { LlmResponse } from '../response.js';
|
|
5
|
+
export type CallOptions = {
|
|
6
|
+
/** Stops the call; it then fails with `cancelled`. */
|
|
7
|
+
readonly signal?: AbortSignal;
|
|
8
|
+
/** A fetch to use instead of the global one, such as in tests. */
|
|
9
|
+
readonly fetch?: typeof fetch;
|
|
10
|
+
};
|
|
11
|
+
/** One completion from the request's provider. Never retried. */
|
|
12
|
+
export declare function callProvider<P extends Provider>(request: LlmRequest<P>, credentials: Credentials<P>, options?: CallOptions): Promise<LlmResponse<P>>;
|
|
13
|
+
/**
|
|
14
|
+
* Credentials checked before they are sent, such as the keys a gateway reads
|
|
15
|
+
* from its configuration. A message names the field at fault, never its value.
|
|
16
|
+
*/
|
|
17
|
+
export declare function readCredentials<P extends Provider>(provider: P, value: Json | undefined): Credentials<P>;
|
|
18
|
+
//# sourceMappingURL=direct.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"direct.d.ts","sourceRoot":"","sources":["../../src/client/direct.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAI5C,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC7D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAgBlD,MAAM,MAAM,WAAW,GAAG;IACxB,sDAAsD;IACtD,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;IAC9B,kEAAkE;IAClE,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CAC/B,CAAC;AAEF,iEAAiE;AACjE,wBAAsB,YAAY,CAAC,CAAC,SAAS,QAAQ,EACnD,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,EACtB,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,EAC3B,OAAO,GAAE,WAAgB,GACxB,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CA+BzB;AAkED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,QAAQ,EAChD,QAAQ,EAAE,CAAC,EACX,KAAK,EAAE,IAAI,GAAG,SAAS,GACtB,WAAW,CAAC,CAAC,CAAC,CAehB"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// One call straight to a provider, with credentials in hand. What the client
|
|
2
|
+
// does when it has a provider's key, and what a gateway runs for each request.
|
|
3
|
+
import { LlmError } from '../failure.js';
|
|
4
|
+
import { isObject } from '../json.js';
|
|
5
|
+
import { httpFailure as anthropicFailure } from './anthropic/errors.js';
|
|
6
|
+
import { buildRequest as anthropicRequest } from './anthropic/request.js';
|
|
7
|
+
import { parseResponse as anthropicResponse } from './anthropic/response.js';
|
|
8
|
+
import { postJson, withRequestId } from './http.js';
|
|
9
|
+
import { httpFailure as responsesFailure } from './responses/errors.js';
|
|
10
|
+
import { fireworksProfile, openAiProfile } from './responses/providers.js';
|
|
11
|
+
import { buildRequest as responsesRequest } from './responses/request.js';
|
|
12
|
+
import { parseResponse as responsesResponse } from './responses/response.js';
|
|
13
|
+
import { invalidRequest } from './util.js';
|
|
14
|
+
/** The only Anthropic Messages API version. */
|
|
15
|
+
const ANTHROPIC_VERSION = '2023-06-01';
|
|
16
|
+
/** One completion from the request's provider. Never retried. */
|
|
17
|
+
export async function callProvider(request, credentials, options = {}) {
|
|
18
|
+
// Each branch narrows the request to its provider; the result is that provider's.
|
|
19
|
+
const any = request;
|
|
20
|
+
const name = request.provider;
|
|
21
|
+
switch (any.provider) {
|
|
22
|
+
case 'anthropic':
|
|
23
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion
|
|
24
|
+
return (await callAnthropic(any, readCredentials('anthropic', credentials), options));
|
|
25
|
+
case 'openai':
|
|
26
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion
|
|
27
|
+
return (await callResponses(any, readCredentials('openai', credentials), openAiProfile, options));
|
|
28
|
+
case 'fireworks':
|
|
29
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion
|
|
30
|
+
return (await callResponses(any, readCredentials('fireworks', credentials), fireworksProfile, options));
|
|
31
|
+
default:
|
|
32
|
+
throw invalidRequest(undefined, `there is no provider ${name}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function callAnthropic(request, credentials, options) {
|
|
36
|
+
const { body, betas } = anthropicRequest(request);
|
|
37
|
+
const answer = await postJson({
|
|
38
|
+
provider: 'anthropic',
|
|
39
|
+
label: 'Anthropic',
|
|
40
|
+
url: 'https://api.anthropic.com/v1/messages',
|
|
41
|
+
headers: {
|
|
42
|
+
'x-api-key': credentials.apiKey,
|
|
43
|
+
...(credentials.workspaceId === undefined
|
|
44
|
+
? {}
|
|
45
|
+
: { 'anthropic-workspace-id': credentials.workspaceId }),
|
|
46
|
+
'anthropic-version': ANTHROPIC_VERSION,
|
|
47
|
+
...(betas.length > 0 ? { 'anthropic-beta': betas.join(',') } : {}),
|
|
48
|
+
},
|
|
49
|
+
body,
|
|
50
|
+
...common(request, options),
|
|
51
|
+
failed: anthropicFailure,
|
|
52
|
+
});
|
|
53
|
+
return read(answer, () => anthropicResponse(request, answer));
|
|
54
|
+
}
|
|
55
|
+
async function callResponses(request, credentials, profile, options) {
|
|
56
|
+
const body = responsesRequest(request, profile);
|
|
57
|
+
const answer = await postJson({
|
|
58
|
+
provider: profile.provider,
|
|
59
|
+
label: profile.label,
|
|
60
|
+
url: profile.url,
|
|
61
|
+
headers: profile.headers(credentials),
|
|
62
|
+
body,
|
|
63
|
+
...common(request, options),
|
|
64
|
+
failed: (http) => responsesFailure(profile, http),
|
|
65
|
+
});
|
|
66
|
+
return read(answer, () => responsesResponse(request, answer, profile));
|
|
67
|
+
}
|
|
68
|
+
function common(request, { signal, fetch }) {
|
|
69
|
+
return {
|
|
70
|
+
...(request.timeoutMs === undefined ? {} : { timeoutMs: request.timeoutMs }),
|
|
71
|
+
...(signal === undefined ? {} : { signal }),
|
|
72
|
+
...(fetch === undefined ? {} : { fetch }),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** Reads an answer; a failure while reading names the provider's request ID. */
|
|
76
|
+
function read(answer, parse) {
|
|
77
|
+
try {
|
|
78
|
+
return parse();
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
throw error instanceof LlmError ? withRequestId(error, answer.requestId) : error;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Credentials checked before they are sent, such as the keys a gateway reads
|
|
86
|
+
* from its configuration. A message names the field at fault, never its value.
|
|
87
|
+
*/
|
|
88
|
+
export function readCredentials(provider, value) {
|
|
89
|
+
if (!isObject(value))
|
|
90
|
+
throw invalidRequest(provider, `the ${provider} credentials must be an object with an apiKey`);
|
|
91
|
+
const fields = {};
|
|
92
|
+
for (const [name, field] of Object.entries(value)) {
|
|
93
|
+
if (typeof field !== 'string' || field.trim() === '') {
|
|
94
|
+
throw invalidRequest(provider, `credentials.${name} must be a string that is not empty`);
|
|
95
|
+
}
|
|
96
|
+
fields[name] = field.trim();
|
|
97
|
+
}
|
|
98
|
+
if (fields['apiKey'] === undefined)
|
|
99
|
+
throw invalidRequest(provider, 'credentials.apiKey is missing');
|
|
100
|
+
// Every provider's credentials are an apiKey and optional string fields.
|
|
101
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion
|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,CAAC;AAED,gFAAgF;AAChF,SAAS,IAAI,CAAI,MAAkB,EAAE,KAAc;IACjD,IAAI,CAAC;QACH,OAAO,KAAK,EAAE,CAAC;IACjB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,KAAK,YAAY,QAAQ,CAAC,CAAC,CAAC,aAAa,CAAC,KAAK,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IACnF,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,eAAe,CAC7B,QAAW,EACX,KAAuB;IAEvB,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;QAClB,MAAM,cAAc,CAAC,QAAQ,EAAE,OAAO,QAAQ,+CAA+C,CAAC,CAAC;IACjG,MAAM,MAAM,GAA+B,EAAE,CAAC;IAC9C,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAClD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;YACrD,MAAM,cAAc,CAAC,QAAQ,EAAE,eAAe,IAAI,qCAAqC,CAAC,CAAC;QAC3F,CAAC;QACD,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAC9B,CAAC;IACD,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,SAAS;QAChC,MAAM,cAAc,CAAC,QAAQ,EAAE,+BAA+B,CAAC,CAAC;IAClE,yEAAyE;IACzE,+DAA+D;IAC/D,OAAO,MAAmC,CAAC;AAC7C,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { LlmFailure } from '../failure.js';
|
|
2
|
+
import type { Provider } from '../providers.js';
|
|
3
|
+
import type { LlmRequest } from '../request.js';
|
|
4
|
+
import type { LlmResponse } from '../response.js';
|
|
5
|
+
import type { CallOptions } from './direct.js';
|
|
6
|
+
/** Where a gateway takes completions, under its URL. */
|
|
7
|
+
export declare const GATEWAY_ROUTE = "/v1/complete";
|
|
8
|
+
/** The header that carries the client's LLM_VERSION to the gateway. */
|
|
9
|
+
export declare const VERSION_HEADER = "acentric-llm-version";
|
|
10
|
+
/**
|
|
11
|
+
* The effect a call is made for, as its handler's context names it. A gateway
|
|
12
|
+
* shows each call under it, but it is the caller's word: never a credential,
|
|
13
|
+
* and never what a call is charged to.
|
|
14
|
+
*/
|
|
15
|
+
export type GatewayEffect = {
|
|
16
|
+
readonly key?: string;
|
|
17
|
+
readonly kind?: string;
|
|
18
|
+
readonly attempt?: number;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The body of a call to the gateway: the request, and the effect that made
|
|
22
|
+
* it. It carries no credentials, since the gateway calls providers with its
|
|
23
|
+
* own keys.
|
|
24
|
+
*/
|
|
25
|
+
export type GatewayRequest<P extends Provider = Provider> = P extends Provider ? LlmRequest<P> & {
|
|
26
|
+
readonly effect?: GatewayEffect;
|
|
27
|
+
} : never;
|
|
28
|
+
/** The body of a gateway's answer that is not 2xx. */
|
|
29
|
+
export type GatewayFailure = {
|
|
30
|
+
readonly error: LlmFailure;
|
|
31
|
+
};
|
|
32
|
+
export type Gateway = {
|
|
33
|
+
/** The gateway's address, such as `https://llm.cloud.acentric.dev`. */
|
|
34
|
+
readonly url: string;
|
|
35
|
+
/** Sent as `Authorization: Bearer`. */
|
|
36
|
+
readonly token: string;
|
|
37
|
+
};
|
|
38
|
+
export type GatewayCallOptions = CallOptions & {
|
|
39
|
+
/** The effect the call is made for, sent for the gateway to show. */
|
|
40
|
+
readonly effect?: GatewayEffect;
|
|
41
|
+
};
|
|
42
|
+
export declare function callGateway<P extends Provider>(request: LlmRequest<P>, gateway: Gateway, { signal, fetch, effect }?: GatewayCallOptions): Promise<LlmResponse<P>>;
|
|
43
|
+
//# sourceMappingURL=gateway.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
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C,EAAE,aAAa,CAAC;CACjC,CAAC;AAEF,wBAAsB,WAAW,CAAC,CAAC,SAAS,QAAQ,EAClD,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,EACtB,OAAO,EAAE,OAAO,EAChB,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAE,kBAAuB,GACjD,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAuBzB"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// One call through the acentric gateway, which acentric-cloud runs with
|
|
2
|
+
// provider keys of its own. The request goes as it is, with the token and the
|
|
3
|
+
// client's version in headers and the effect that made the call in the body;
|
|
4
|
+
// the response comes back as it is.
|
|
5
|
+
import { isLlmFailure, LlmError } from '../failure.js';
|
|
6
|
+
import { isObject } from '../json.js';
|
|
7
|
+
import { LLM_VERSION } from '../version.js';
|
|
8
|
+
import { postJson, retryAfter } from './http.js';
|
|
9
|
+
import { kindForStatus } from './responses/errors.js';
|
|
10
|
+
import { cut, invalidResponse } from './util.js';
|
|
11
|
+
/** Where a gateway takes completions, under its URL. */
|
|
12
|
+
export const GATEWAY_ROUTE = '/v1/complete';
|
|
13
|
+
/** The header that carries the client's LLM_VERSION to the gateway. */
|
|
14
|
+
export const VERSION_HEADER = 'acentric-llm-version';
|
|
15
|
+
export async function callGateway(request, gateway, { signal, fetch, effect } = {}) {
|
|
16
|
+
const provider = request.provider;
|
|
17
|
+
const answer = await postJson({
|
|
18
|
+
provider,
|
|
19
|
+
label: 'the acentric gateway',
|
|
20
|
+
url: `${base(provider, gateway.url)}${GATEWAY_ROUTE}`,
|
|
21
|
+
headers: { authorization: `Bearer ${gateway.token}`, [VERSION_HEADER]: LLM_VERSION },
|
|
22
|
+
body: effect === undefined ? request : { ...request, effect },
|
|
23
|
+
...(request.timeoutMs === undefined ? {} : { timeoutMs: request.timeoutMs }),
|
|
24
|
+
...(signal === undefined ? {} : { signal }),
|
|
25
|
+
...(fetch === undefined ? {} : { fetch }),
|
|
26
|
+
failed: (http) => gatewayFailure(provider, http),
|
|
27
|
+
// The gateway's request ID is not the provider's; its failures carry that.
|
|
28
|
+
providerAnswers: false,
|
|
29
|
+
});
|
|
30
|
+
const body = answer.body;
|
|
31
|
+
const message = isObject(body) ? body['message'] : undefined;
|
|
32
|
+
if (!isObject(body) || body['provider'] !== provider || !isObject(message)) {
|
|
33
|
+
throw invalidResponse(provider, 'the acentric gateway did not answer with a response', body);
|
|
34
|
+
}
|
|
35
|
+
// The gateway answers what callProvider returned, for this provider.
|
|
36
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion
|
|
37
|
+
return body;
|
|
38
|
+
}
|
|
39
|
+
/** The gateway's own failure as it sent it, or one read from the HTTP status. */
|
|
40
|
+
function gatewayFailure(provider, answer) {
|
|
41
|
+
const error = isObject(answer.body) ? answer.body['error'] : undefined;
|
|
42
|
+
if (isLlmFailure(error))
|
|
43
|
+
return LlmError.fromJSON(error);
|
|
44
|
+
const after = retryAfter(answer.headers);
|
|
45
|
+
return new LlmError(`the acentric gateway answered HTTP ${answer.status}`, {
|
|
46
|
+
kind: kindForStatus(answer.status),
|
|
47
|
+
provider,
|
|
48
|
+
httpStatus: answer.status,
|
|
49
|
+
providerError: cut(answer.body),
|
|
50
|
+
...(after === undefined ? {} : { retryAfterMs: after }),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
function base(provider, value) {
|
|
54
|
+
let url;
|
|
55
|
+
try {
|
|
56
|
+
url = new URL(value);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
throw new LlmError(`the gateway's URL is not a URL: ${value}`, {
|
|
60
|
+
kind: 'invalid_request',
|
|
61
|
+
provider,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
if (url.protocol !== 'https:' && url.protocol !== 'http:') {
|
|
65
|
+
throw new LlmError('the gateway must be reached over https, or http for local development', {
|
|
66
|
+
kind: 'invalid_request',
|
|
67
|
+
provider,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return url.href.replace(/\/+$/, '');
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=gateway.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateway.js","sourceRoot":"","sources":["../../src/client/gateway.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,8EAA8E;AAC9E,6EAA6E;AAC7E,oCAAoC;AAEpC,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEvD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAItC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,GAAG,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAEjD,wDAAwD;AACxD,MAAM,CAAC,MAAM,aAAa,GAAG,cAAc,CAAC;AAE5C,uEAAuE;AACvE,MAAM,CAAC,MAAM,cAAc,GAAG,sBAAsB,CAAC;AAqCrD,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,OAAsB,EACtB,OAAgB,EAChB,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAyB,EAAE;IAElD,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;IAClC,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC;QAC5B,QAAQ;QACR,KAAK,EAAE,sBAAsB;QAC7B,GAAG,EAAE,GAAG,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,GAAG,aAAa,EAAE;QACrD,OAAO,EAAE,EAAE,aAAa,EAAE,UAAU,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,cAAc,CAAC,EAAE,WAAW,EAAE;QACpF,IAAI,EAAE,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,OA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,KAAK,CAAC;QAAE,OAAO,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACzD,MAAM,KAAK,GAAG,UAAU,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACzC,OAAO,IAAI,QAAQ,CAAC,sCAAsC,MAAM,CAAC,MAAM,EAAE,EAAE;QACzE,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC;QAClC,QAAQ;QACR,UAAU,EAAE,MAAM,CAAC,MAAM;QACzB,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC;QAC/B,GAAG,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,KAAK,EAAE,CAAC;KACxD,CAAC,CAAC;AACL,CAAC;AAED,SAAS,IAAI,CAAC,QAAkB,EAAE,KAAa;IAC7C,IAAI,GAAQ,CAAC;IACb,IAAI,CAAC;QACH,GAAG,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,QAAQ,CAAC,mCAAmC,KAAK,EAAE,EAAE;YAC7D,IAAI,EAAE,iBAAiB;YACvB,QAAQ;SACT,CAAC,CAAC;IACL,CAAC;IACD,IAAI,GAAG,CAAC,QAAQ,KAAK,QAAQ,IAAI,GAAG,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;QAC1D,MAAM,IAAI,QAAQ,CAAC,uEAAuE,EAAE;YAC1F,IAAI,EAAE,iBAAiB;YACvB,QAAQ;SACT,CAAC,CAAC;IACL,CAAC;IACD,OAAO,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Json } from '@acentric/types';
|
|
2
|
+
import { LlmError } from '../failure.js';
|
|
3
|
+
/** The longest one call may take, and the default, in milliseconds. */
|
|
4
|
+
export declare const MAX_TIMEOUT_MS: number;
|
|
5
|
+
export type Post = {
|
|
6
|
+
readonly provider: string;
|
|
7
|
+
/** Names the other end in messages, such as "Anthropic". */
|
|
8
|
+
readonly label: string;
|
|
9
|
+
readonly url: string;
|
|
10
|
+
readonly headers: {
|
|
11
|
+
readonly [name: string]: string;
|
|
12
|
+
};
|
|
13
|
+
readonly body: Json;
|
|
14
|
+
readonly timeoutMs?: number;
|
|
15
|
+
readonly signal?: AbortSignal;
|
|
16
|
+
readonly fetch?: typeof fetch;
|
|
17
|
+
/** Turns an answer that is not 2xx into an error. */
|
|
18
|
+
readonly failed: (answer: HttpAnswer) => LlmError;
|
|
19
|
+
/** Whether the answer's request ID is the provider's, for its failures to name. Defaults to true. */
|
|
20
|
+
readonly providerAnswers?: boolean;
|
|
21
|
+
};
|
|
22
|
+
/** An answer that is not 2xx. */
|
|
23
|
+
export type HttpAnswer = {
|
|
24
|
+
readonly status: number;
|
|
25
|
+
readonly headers: Headers;
|
|
26
|
+
/** The JSON body, or the text body cut short. */
|
|
27
|
+
readonly body: Json;
|
|
28
|
+
};
|
|
29
|
+
export type JsonAnswer = {
|
|
30
|
+
readonly body: Json;
|
|
31
|
+
readonly durationMs: number;
|
|
32
|
+
/** When the answer arrived, in milliseconds since the Unix epoch. */
|
|
33
|
+
readonly timestamp: number;
|
|
34
|
+
readonly requestId?: string;
|
|
35
|
+
};
|
|
36
|
+
export declare function postJson(post: Post): Promise<JsonAnswer>;
|
|
37
|
+
/** `Retry-After` in milliseconds, given in seconds or as an HTTP date. */
|
|
38
|
+
export declare function retryAfter(headers: Headers): number | undefined;
|
|
39
|
+
/** A failure from after the provider answered names its request ID, for its support. */
|
|
40
|
+
export declare function withRequestId(error: LlmError, requestId: string | undefined): LlmError;
|
|
41
|
+
//# sourceMappingURL=http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/client/http.ts"],"names":[],"m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