@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,197 @@
|
|
|
1
|
+
// The Responses format's side of conversion (OpenAI, Fireworks).
|
|
2
|
+
import { isArray, isObject } from '../json.js';
|
|
3
|
+
function texts(value) {
|
|
4
|
+
return isArray(value)
|
|
5
|
+
? value.flatMap((part) => isObject(part) && typeof part['text'] === 'string' ? [part['text']] : [])
|
|
6
|
+
: [];
|
|
7
|
+
}
|
|
8
|
+
/** A reasoning item's readable text: its summary, or else its reasoning text. */
|
|
9
|
+
function reasoningText(item) {
|
|
10
|
+
const summary = texts(item['summary']);
|
|
11
|
+
return (summary.length > 0 ? summary : texts(item['content'])).join('\n\n');
|
|
12
|
+
}
|
|
13
|
+
function message(content) {
|
|
14
|
+
return { type: 'message', role: 'assistant', content };
|
|
15
|
+
}
|
|
16
|
+
function outputText(text) {
|
|
17
|
+
return { type: 'output_text', text, annotations: [] };
|
|
18
|
+
}
|
|
19
|
+
/** Reasoning the target cannot use as it is: dropped, or kept as text. */
|
|
20
|
+
function carryReasoning(text, context) {
|
|
21
|
+
if (context.reasoning === 'drop' || text === '') {
|
|
22
|
+
context.change('dropped', 'reasoning', text === '' ? 'no readable text' : 'bound to its model');
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
if (context.target.plainReasoning) {
|
|
26
|
+
context.change('rewritten', 'reasoning', 'as a plain reasoning item');
|
|
27
|
+
return { type: 'reasoning', summary: [{ type: 'summary_text', text }] };
|
|
28
|
+
}
|
|
29
|
+
context.change('rewritten', 'reasoning', 'as assistant text');
|
|
30
|
+
return message([outputText(text)]);
|
|
31
|
+
}
|
|
32
|
+
/** An item without the IDs and status its provider gave it. */
|
|
33
|
+
function withoutIds(item) {
|
|
34
|
+
const { id: _id, status: _status, ...rest } = item;
|
|
35
|
+
return rest;
|
|
36
|
+
}
|
|
37
|
+
function string(value, otherwise) {
|
|
38
|
+
return typeof value === 'string' ? value : otherwise;
|
|
39
|
+
}
|
|
40
|
+
export const responsesConverter = {
|
|
41
|
+
sameFormat(items, sameProvider, context) {
|
|
42
|
+
const out = [];
|
|
43
|
+
for (const item of items) {
|
|
44
|
+
switch (item['type']) {
|
|
45
|
+
case 'message':
|
|
46
|
+
out.push(sameProvider ? item : withoutIds(item));
|
|
47
|
+
break;
|
|
48
|
+
case 'reasoning': {
|
|
49
|
+
// A provider's own plain reasoning reads the same on any of its models.
|
|
50
|
+
const encrypted = item['encrypted_content'];
|
|
51
|
+
if (sameProvider && context.target.plainReasoning && !encrypted) {
|
|
52
|
+
out.push(item);
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
const carried = carryReasoning(reasoningText(item), context);
|
|
56
|
+
if (carried !== undefined)
|
|
57
|
+
out.push(carried);
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
case 'function_call':
|
|
61
|
+
out.push(sameProvider
|
|
62
|
+
? item
|
|
63
|
+
: {
|
|
64
|
+
type: 'function_call',
|
|
65
|
+
call_id: string(item['call_id'], ''),
|
|
66
|
+
name: string(item['name'], ''),
|
|
67
|
+
arguments: string(item['arguments'], '{}'),
|
|
68
|
+
});
|
|
69
|
+
break;
|
|
70
|
+
case 'custom_tool_call':
|
|
71
|
+
if (context.target.customTools) {
|
|
72
|
+
out.push(sameProvider ? item : withoutIds(item));
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const callId = item['call_id'];
|
|
76
|
+
if (typeof callId === 'string')
|
|
77
|
+
context.droppedCalls.add(callId);
|
|
78
|
+
context.change('dropped', 'tool_call', `custom tool ${string(item['name'], '')}`);
|
|
79
|
+
}
|
|
80
|
+
break;
|
|
81
|
+
default:
|
|
82
|
+
if (sameProvider)
|
|
83
|
+
out.push(item);
|
|
84
|
+
else
|
|
85
|
+
context.change('dropped', 'provider_item', string(item['type'], 'unknown'));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
},
|
|
90
|
+
read(items, context) {
|
|
91
|
+
const common = [];
|
|
92
|
+
for (const item of items) {
|
|
93
|
+
switch (item['type']) {
|
|
94
|
+
case 'message': {
|
|
95
|
+
const content = item['content'];
|
|
96
|
+
for (const part of isArray(content) ? content : []) {
|
|
97
|
+
if (!isObject(part))
|
|
98
|
+
continue;
|
|
99
|
+
const text = part['text'];
|
|
100
|
+
const refusal = part['refusal'];
|
|
101
|
+
if (part['type'] === 'output_text' && typeof text === 'string') {
|
|
102
|
+
const annotations = part['annotations'];
|
|
103
|
+
if (isArray(annotations) && annotations.length > 0) {
|
|
104
|
+
context.change('dropped', 'citations', `${annotations.length} annotations`);
|
|
105
|
+
}
|
|
106
|
+
common.push({ kind: 'text', text });
|
|
107
|
+
}
|
|
108
|
+
else if (part['type'] === 'refusal' && typeof refusal === 'string') {
|
|
109
|
+
common.push({ kind: 'refusal', text: refusal });
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
common.push({ kind: 'other', type: `message ${string(part['type'], 'part')}` });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
case 'reasoning':
|
|
118
|
+
common.push({ kind: 'reasoning', text: reasoningText(item) });
|
|
119
|
+
break;
|
|
120
|
+
case 'function_call':
|
|
121
|
+
common.push({
|
|
122
|
+
kind: 'call',
|
|
123
|
+
id: string(item['call_id'], ''),
|
|
124
|
+
name: string(item['name'], ''),
|
|
125
|
+
arguments: string(item['arguments'], '{}'),
|
|
126
|
+
});
|
|
127
|
+
break;
|
|
128
|
+
case 'custom_tool_call':
|
|
129
|
+
common.push({
|
|
130
|
+
kind: 'custom_call',
|
|
131
|
+
id: string(item['call_id'], ''),
|
|
132
|
+
name: string(item['name'], ''),
|
|
133
|
+
input: string(item['input'], ''),
|
|
134
|
+
});
|
|
135
|
+
break;
|
|
136
|
+
default:
|
|
137
|
+
common.push({ kind: 'other', type: string(item['type'], 'unknown') });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return common;
|
|
141
|
+
},
|
|
142
|
+
write(items, context) {
|
|
143
|
+
const out = [];
|
|
144
|
+
let open = [];
|
|
145
|
+
const close = () => {
|
|
146
|
+
if (open.length > 0)
|
|
147
|
+
out.push(message(open));
|
|
148
|
+
open = [];
|
|
149
|
+
};
|
|
150
|
+
for (const item of items) {
|
|
151
|
+
switch (item.kind) {
|
|
152
|
+
case 'text':
|
|
153
|
+
open.push(outputText(item.text));
|
|
154
|
+
break;
|
|
155
|
+
case 'refusal':
|
|
156
|
+
open.push({ type: 'refusal', refusal: item.text });
|
|
157
|
+
break;
|
|
158
|
+
case 'reasoning': {
|
|
159
|
+
close();
|
|
160
|
+
const carried = carryReasoning(item.text, context);
|
|
161
|
+
if (carried !== undefined)
|
|
162
|
+
out.push(carried);
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
case 'call':
|
|
166
|
+
close();
|
|
167
|
+
out.push({
|
|
168
|
+
type: 'function_call',
|
|
169
|
+
call_id: context.callId(item.id),
|
|
170
|
+
name: item.name,
|
|
171
|
+
arguments: item.arguments,
|
|
172
|
+
});
|
|
173
|
+
break;
|
|
174
|
+
case 'custom_call':
|
|
175
|
+
close();
|
|
176
|
+
if (context.target.customTools) {
|
|
177
|
+
out.push({
|
|
178
|
+
type: 'custom_tool_call',
|
|
179
|
+
call_id: context.callId(item.id),
|
|
180
|
+
name: item.name,
|
|
181
|
+
input: item.input,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
context.droppedCalls.add(item.id);
|
|
186
|
+
context.change('dropped', 'tool_call', `custom tool ${item.name}`);
|
|
187
|
+
}
|
|
188
|
+
break;
|
|
189
|
+
case 'other':
|
|
190
|
+
context.change('dropped', 'provider_item', item.type);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
close();
|
|
194
|
+
return out;
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
//# sourceMappingURL=responses.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"responses.js","sourceRoot":"","sources":["../../src/convert/responses.ts"],"names":[],"mappings":"AAAA,iEAAiE;AAIjE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAG/C,SAAS,KAAK,CAAC,KAAuB;IACpC,OAAO,OAAO,CAAC,KAAK,CAAC;QACnB,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CACrB,QAAQ,CAAC,IAAI,CAAC,IAAI,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CACzE;QACH,CAAC,CAAC,EAAE,CAAC;AACT,CAAC;AAED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AAC,IAAI,CAAC,EAAE,CAAC;4BAChC,IAAI,EAAE,IAAI,CAAC,IAAI;4BACf,KAAK,EAAE,IAAI,CAAC,KAAK;yBAClB,CAAC,CAAC;oBACL,CAAC;yBAAM,CAAC;wBACN,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;wBAClC,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,WAAW,EAAE,eAAe,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;oBACrE,CAAC;oBACD,MAAM;gBACR,KAAK,OAAO;oBACV,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,eAAe,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;YAC1D,CAAC;QACH,CAAC;QACD,KAAK,EAAE,CAAC;QACR,OAAO,GAAG,CAAC;IACb,CAAC;CACF,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { JsonObject } from '@acentric/types';
|
|
2
|
+
import type { ApiFormat, Provider } from '../providers.js';
|
|
3
|
+
/** The provider and model a conversation is converted for. */
|
|
4
|
+
export type ConvertTarget<P extends Provider = Provider> = {
|
|
5
|
+
readonly provider: P;
|
|
6
|
+
readonly model: string;
|
|
7
|
+
};
|
|
8
|
+
export type ConvertOptions = {
|
|
9
|
+
/**
|
|
10
|
+
* Reasoning the target cannot use as it is: another model's signed or
|
|
11
|
+
* encrypted reasoning, and any reasoning sent to another provider. `drop`,
|
|
12
|
+
* the default, leaves it out. `text` keeps its readable text: as a plain
|
|
13
|
+
* reasoning item where the target reads one (Fireworks), otherwise as
|
|
14
|
+
* assistant text. Reasoning with no readable text is dropped either way.
|
|
15
|
+
*/
|
|
16
|
+
readonly reasoning?: 'drop' | 'text';
|
|
17
|
+
};
|
|
18
|
+
/** One thing `convert` dropped or rewrote. */
|
|
19
|
+
export type ConversionChange = {
|
|
20
|
+
/** The message's index in the input. */
|
|
21
|
+
readonly index: number;
|
|
22
|
+
readonly action: 'dropped' | 'rewritten';
|
|
23
|
+
readonly what: 'message' | 'reasoning' | 'tool_call' | 'tool_result' | 'tool_call_id' | 'tool_arguments' | 'provider_item' | 'citations' | 'refusal' | 'part' | 'part_option';
|
|
24
|
+
/** What exactly, for logs, such as `web_search_call` or `cache_control`. */
|
|
25
|
+
readonly detail: string;
|
|
26
|
+
};
|
|
27
|
+
/** The converted messages, and every change made to get them. */
|
|
28
|
+
export type Conversion<M> = {
|
|
29
|
+
readonly messages: M[];
|
|
30
|
+
readonly changes: readonly ConversionChange[];
|
|
31
|
+
};
|
|
32
|
+
/** What a target provider takes, as far as conversion cares. */
|
|
33
|
+
export type Capabilities = {
|
|
34
|
+
readonly api: ApiFormat;
|
|
35
|
+
/** Free-form custom tools and their calls. */
|
|
36
|
+
readonly customTools: boolean;
|
|
37
|
+
/** File parts. */
|
|
38
|
+
readonly files: boolean;
|
|
39
|
+
/** Images and files by a provider's file ID. */
|
|
40
|
+
readonly fileIds: boolean;
|
|
41
|
+
/** Reads reasoning items that hold only text. */
|
|
42
|
+
readonly plainReasoning: boolean;
|
|
43
|
+
};
|
|
44
|
+
export type Target = Capabilities & ConvertTarget;
|
|
45
|
+
/** One assistant item read without its format, on its way to another format. Never stored. */
|
|
46
|
+
export type Common = {
|
|
47
|
+
readonly kind: 'text';
|
|
48
|
+
readonly text: string;
|
|
49
|
+
} | {
|
|
50
|
+
readonly kind: 'refusal';
|
|
51
|
+
readonly text: string;
|
|
52
|
+
} | {
|
|
53
|
+
readonly kind: 'reasoning';
|
|
54
|
+
readonly text: string;
|
|
55
|
+
} | {
|
|
56
|
+
readonly kind: 'call';
|
|
57
|
+
readonly id: string;
|
|
58
|
+
readonly name: string;
|
|
59
|
+
readonly arguments: string;
|
|
60
|
+
} | {
|
|
61
|
+
readonly kind: 'custom_call';
|
|
62
|
+
readonly id: string;
|
|
63
|
+
readonly name: string;
|
|
64
|
+
readonly input: string;
|
|
65
|
+
} | {
|
|
66
|
+
readonly kind: 'other';
|
|
67
|
+
readonly type: string;
|
|
68
|
+
};
|
|
69
|
+
/** What a conversation's conversion shares from message to message. */
|
|
70
|
+
export type Context = {
|
|
71
|
+
readonly target: Target;
|
|
72
|
+
readonly reasoning: 'drop' | 'text';
|
|
73
|
+
/** Call IDs whose calls were dropped; their results are dropped too. */
|
|
74
|
+
readonly droppedCalls: Set<string>;
|
|
75
|
+
change(action: ConversionChange['action'], what: ConversionChange['what'], detail: string): void;
|
|
76
|
+
/** A call ID as the target takes it. Results are mapped the same way. */
|
|
77
|
+
callId(id: string): string;
|
|
78
|
+
};
|
|
79
|
+
/** One API format's side of conversion. */
|
|
80
|
+
export type FormatConverter = {
|
|
81
|
+
/** The items of a message from another provider or model in this same format. */
|
|
82
|
+
sameFormat(items: readonly JsonObject[], sameProvider: boolean, context: Context): JsonObject[];
|
|
83
|
+
/** The items of a message in this format, read for another format. */
|
|
84
|
+
read(items: readonly JsonObject[], context: Context): Common[];
|
|
85
|
+
/** Common items written as this format's. */
|
|
86
|
+
write(items: readonly Common[], context: Context): JsonObject[];
|
|
87
|
+
};
|
|
88
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/convert/types.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAElD,OAAO,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE3D,8DAA8D;AAC9D,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,IAAI;IACzD,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B;;;;;;OAMG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACtC,CAAC;AAEF,8C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AAO,GAAG,MAAM,EAAE,CAAC;IAC/D,6CAA6C;IAC7C,KAAK,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,EAAE,OAAO,EAAE,OAAO,GAAG,UAAU,EAAE,CAAC;CACjE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/convert/types.ts"],"names":[],"mappings":"AAAA,oCAAoC"}
|
package/dist/cost.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ModelPricing } from './model.js';
|
|
2
|
+
import type { Usage, UsageCost } from './usage.js';
|
|
3
|
+
export type CostOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* Scales every price, for where and how the request ran, such as 0.5 for a
|
|
6
|
+
* batch or 1.1 for inference kept in the US. Defaults to 1.
|
|
7
|
+
*/
|
|
8
|
+
readonly multiplier?: number;
|
|
9
|
+
/** The part of `usage.cacheWrite` written for an hour, priced at `cacheWrite1h`. */
|
|
10
|
+
readonly cacheWrite1h?: number;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The estimated cost, or undefined when it cannot be known: no prices, a count
|
|
14
|
+
* that is missing, or tokens in a part the prices do not cover. The prices for
|
|
15
|
+
* the whole request are those of the highest tier its prompt is over.
|
|
16
|
+
*/
|
|
17
|
+
export declare function estimateCost(usage: Usage, pricing: ModelPricing | undefined, { multiplier, cacheWrite1h }?: CostOptions): UsageCost | undefined;
|
|
18
|
+
//# sourceMappingURL=cost.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cost.d.ts","sourceRoot":"","sources":["../src/cost.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAa,YAAY,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEnD,MAAM,MAAM,WAAW,GAAG;IACxB;;;OAGG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,oFAAoF;IACpF,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;CAChC,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,YAAY,GAAG,SAAS,EACjC,EAAE,UAAc,EAAE,YAAgB,EAAE,GAAE,WAAgB,GACrD,SAAS,GAAG,SAAS,CA+BvB"}
|
package/dist/cost.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// What a completion cost, estimated from its tokens and the model's prices,
|
|
2
|
+
// for providers that do not say what they charged.
|
|
3
|
+
/**
|
|
4
|
+
* The estimated cost, or undefined when it cannot be known: no prices, a count
|
|
5
|
+
* that is missing, or tokens in a part the prices do not cover. The prices for
|
|
6
|
+
* the whole request are those of the highest tier its prompt is over.
|
|
7
|
+
*/
|
|
8
|
+
export function estimateCost(usage, pricing, { multiplier = 1, cacheWrite1h = 0 } = {}) {
|
|
9
|
+
const { totalInput, input, output, cacheRead, cacheWrite } = usage;
|
|
10
|
+
if (pricing === undefined || totalInput === undefined || input === undefined)
|
|
11
|
+
return undefined;
|
|
12
|
+
if (output === undefined)
|
|
13
|
+
return undefined;
|
|
14
|
+
if (cacheWrite1h > (cacheWrite ?? 0))
|
|
15
|
+
return undefined;
|
|
16
|
+
let prices = pricing.base;
|
|
17
|
+
for (const tier of pricing.tiers ?? []) {
|
|
18
|
+
if (totalInput > tier.aboveInputTokens)
|
|
19
|
+
prices = tier.cost;
|
|
20
|
+
}
|
|
21
|
+
const read = part(cacheRead, prices.cacheRead);
|
|
22
|
+
const shortWrite = part(cacheWrite === undefined ? undefined : cacheWrite - cacheWrite1h, prices.cacheWrite);
|
|
23
|
+
const hourWrite = part(cacheWrite1h, prices.cacheWrite1h);
|
|
24
|
+
if (read === null || shortWrite === null || hourWrite === null)
|
|
25
|
+
return undefined;
|
|
26
|
+
const written = shortWrite === undefined && hourWrite === undefined
|
|
27
|
+
? undefined
|
|
28
|
+
: (shortWrite ?? 0) + (hourWrite ?? 0);
|
|
29
|
+
const parts = {
|
|
30
|
+
input: price(input, prices.input) * multiplier,
|
|
31
|
+
output: price(output, prices.output) * multiplier,
|
|
32
|
+
...(read === undefined ? {} : { cacheRead: read * multiplier }),
|
|
33
|
+
...(written === undefined ? {} : { cacheWrite: written * multiplier }),
|
|
34
|
+
};
|
|
35
|
+
const total = parts.input + parts.output + (parts.cacheRead ?? 0) + (parts.cacheWrite ?? 0);
|
|
36
|
+
return { source: 'estimated', ...parts, total };
|
|
37
|
+
}
|
|
38
|
+
/** The price of some tokens: undefined when there are none to price, null when they have no price. */
|
|
39
|
+
function part(tokens, perMillion) {
|
|
40
|
+
if (tokens === undefined)
|
|
41
|
+
return undefined;
|
|
42
|
+
if (tokens === 0)
|
|
43
|
+
return perMillion === undefined ? undefined : 0;
|
|
44
|
+
return perMillion === undefined ? null : price(tokens, perMillion);
|
|
45
|
+
}
|
|
46
|
+
function price(tokens, perMillion) {
|
|
47
|
+
return (tokens * perMillion) / 1_000_000;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=cost.js.map
|
package/dist/cost.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cost.js","sourceRoot":"","sources":["../src/cost.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,mDAAmD;AAenD;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAC1B,KAAY,EACZ,OAAiC,EACjC,EAAE,UAAU,GAAG,CAAC,EAAE,YAAY,GAAG,CAAC,KAAkB,EAAE;IAEtD,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,KAAK,CAAC;IACnE,IAAI,OAAO,KAAK,SAAS,IAAI,UAAU,KAAK,SAAS,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC/F,IAAI,MAAM,KAAK,SAAS;QAA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AAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,OAAO,GAAG,UAAU,EAAE,CAAC;KACvE,CAAC;IACF,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,SAAS,IAAI,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,UAAU,IAAI,CAAC,CAAC,CAAC;IAC5F,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,KAAK,EAAE,KAAK,EAAE,CAAC;AAClD,CAAC;AAED,sGAAsG;AACtG,SAAS,IAAI,CACX,MAA0B,EAC1B,UAA8B;IAE9B,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC3C,IAAI,MAAM,KAAK,CAAC;QAAE,OAAO,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;IAClE,OAAO,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AACrE,CAAC;AAED,SAAS,KAAK,CAAC,MAAc,EAAE,UAAkB;IAC/C,OAAO,CAAC,MAAM,GAAG,UAAU,CAAC,GAAG,SAAS,CAAC;AAC3C,CAAC"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { Json, Outcome } from '@acentric/types';
|
|
2
|
+
import type { Usage } from './usage.js';
|
|
3
|
+
/** Whether each kind is worth retrying when the error does not say otherwise. */
|
|
4
|
+
declare const RETRYABLE: {
|
|
5
|
+
/** The client, the gateway or the provider refused the request. */
|
|
6
|
+
readonly invalid_request: false;
|
|
7
|
+
/** Credentials missing, not valid or not allowed. */
|
|
8
|
+
readonly auth: false;
|
|
9
|
+
/** Throttled; see `retryAfterMs`. */
|
|
10
|
+
readonly rate_limited: true;
|
|
11
|
+
/** Out of credits or over a billing limit. */
|
|
12
|
+
readonly quota_exceeded: false;
|
|
13
|
+
/** The prompt is too long for the model. */
|
|
14
|
+
readonly context_overflow: false;
|
|
15
|
+
/** A provider's safety policy refused it; sending it again as it is will not help. */
|
|
16
|
+
readonly policy_violation: false;
|
|
17
|
+
/** An unknown model, or a previous response that expired or is not valid; see `providerParam`. */
|
|
18
|
+
readonly not_found: false;
|
|
19
|
+
/** The provider is out of capacity. */
|
|
20
|
+
readonly overloaded: true;
|
|
21
|
+
/** Any other failure at the provider. */
|
|
22
|
+
readonly provider_error: true;
|
|
23
|
+
/** The provider answered with something that cannot be read. */
|
|
24
|
+
readonly invalid_response: false;
|
|
25
|
+
/** The provider or the gateway could not be reached. */
|
|
26
|
+
readonly network_error: true;
|
|
27
|
+
/** The call took longer than allowed. */
|
|
28
|
+
readonly timeout: true;
|
|
29
|
+
/** The handler's signal fired: the effect was cancelled, aborted or timed out. */
|
|
30
|
+
readonly cancelled: false;
|
|
31
|
+
/** A bug in the gateway. */
|
|
32
|
+
readonly internal_error: false;
|
|
33
|
+
};
|
|
34
|
+
export type LlmFailureKind = keyof typeof RETRYABLE;
|
|
35
|
+
export declare const LLM_FAILURE_KINDS: readonly LlmFailureKind[];
|
|
36
|
+
/** A failed completion as Json: what an LlmError's toJSON() returns, and what step reads. */
|
|
37
|
+
export type LlmFailure = {
|
|
38
|
+
/** Always `LlmError`, which tells an LLM failure from other thrown values. */
|
|
39
|
+
readonly name: 'LlmError';
|
|
40
|
+
readonly message: string;
|
|
41
|
+
readonly kind: LlmFailureKind;
|
|
42
|
+
/** Whether sending the same request again may succeed. */
|
|
43
|
+
readonly retryable: boolean;
|
|
44
|
+
/** Left out for a failure before a provider was chosen. */
|
|
45
|
+
readonly provider?: string;
|
|
46
|
+
readonly providerCode?: string;
|
|
47
|
+
readonly providerType?: string;
|
|
48
|
+
/** The request field the provider blamed, such as `previous_response_id`. */
|
|
49
|
+
readonly providerParam?: string;
|
|
50
|
+
/** The provider's error as it answered, cut short when long. */
|
|
51
|
+
readonly providerError?: Json;
|
|
52
|
+
/** The provider's HTTP status, not the gateway's. */
|
|
53
|
+
readonly httpStatus?: number;
|
|
54
|
+
/** How long the provider asked to wait before trying again, in milliseconds. */
|
|
55
|
+
readonly retryAfterMs?: number;
|
|
56
|
+
/** The provider's request ID, for its support. */
|
|
57
|
+
readonly requestId?: string;
|
|
58
|
+
/** The provider's response ID, when a response was created before it failed. */
|
|
59
|
+
readonly responseId?: string;
|
|
60
|
+
/** Tokens the provider reported, and may bill, for a failed generation. */
|
|
61
|
+
readonly usage?: Usage;
|
|
62
|
+
};
|
|
63
|
+
type Detail = Omit<LlmFailure, 'name' | 'message' | 'kind' | 'retryable'>;
|
|
64
|
+
/** What an LlmError is made from. `retryable` defaults by kind. */
|
|
65
|
+
export type LlmErrorOptions = Detail & {
|
|
66
|
+
readonly kind: LlmFailureKind;
|
|
67
|
+
readonly retryable?: boolean;
|
|
68
|
+
};
|
|
69
|
+
/** A failed completion. The client throws it; a runner reports its toJSON() to step. */
|
|
70
|
+
export declare class LlmError extends Error {
|
|
71
|
+
readonly name = "LlmError";
|
|
72
|
+
readonly failure: LlmFailure;
|
|
73
|
+
constructor(message: string, options: LlmErrorOptions);
|
|
74
|
+
get kind(): LlmFailureKind;
|
|
75
|
+
get retryable(): boolean;
|
|
76
|
+
/** The failure as Json, which a runner reports as the effect's error. */
|
|
77
|
+
toJSON(): LlmFailure;
|
|
78
|
+
/** An LlmError from its Json, such as a gateway's answer. */
|
|
79
|
+
static fromJSON({ message, kind, retryable, ...detail }: LlmFailure): LlmError;
|
|
80
|
+
}
|
|
81
|
+
/** Whether a value is an LlmFailure, such as a failed effect's error. */
|
|
82
|
+
export declare function isLlmFailure(value: Json | undefined): value is LlmFailure;
|
|
83
|
+
/**
|
|
84
|
+
* The LLM failure in an effect's outcome, or null: for a result, for a
|
|
85
|
+
* failure the runner reported itself (a timeout, a crash) and for one thrown
|
|
86
|
+
* by something other than this package.
|
|
87
|
+
*/
|
|
88
|
+
export declare function llmFailure(outcome: Outcome): LlmFailure | null;
|
|
89
|
+
export {};
|
|
90
|
+
//# sourceMappingURL=failure.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"failure.d.ts","sourceRoot":"","sources":["../src/failure.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAGrD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC,iFAAiF;AACjF,QAAA,MAAM,SAAS;IACb,mEAAmE;;IAEnE,qDAAqD;;IAErD,qCAAqC;;IAErC,8CAA8C;;IAE9C,4CAA4C;;IAE5C,sFAAsF;;IAEtF,kGAAkG;;IAElG,uCAAuC;;IAEvC,yCAAyC;;IAEzC,gEAAgE;;IAEhE,wDAAwD;;IAExD,yCAAyC;;IAEzC,kFAAkF;;IAElF,4BAA4B;;CAE2B,CAAC;AAE1D,MAAM,MAAM,cAAc,GAAG,MAAM,OAAO,SAAS,CAAC;AAEpD,eAAO,MAAM,iBAAiB,EAAE,SAAS,cAAc,EAEtD,CAAC;AAEF,6FAA6F;AAC7F,MAAM,MAAM,UAAU,GAAG;IACvB,8EAA8E;IAC9E,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;IAC9B,0DAA0D;IAC1D,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,2DAA2D;IAC3D,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAC/B,6EAA6E;IAC7E,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC,gEAAgE;IAChE,QAAQ,CAA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G,IAAI,CAI9D"}
|
package/dist/failure.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Every way a completion can fail, as an error the client throws and as the
|
|
2
|
+
// Json a step reads. An LlmError's toJSON() is what a runner reports as the
|
|
3
|
+
// failure's error (RUNNERS.md §8), so step learns its kind, whether a retry
|
|
4
|
+
// may help and how long to wait. Retrying is the agent's decision.
|
|
5
|
+
import { isObject } from './json.js';
|
|
6
|
+
/** Whether each kind is worth retrying when the error does not say otherwise. */
|
|
7
|
+
const RETRYABLE = {
|
|
8
|
+
/** The client, the gateway or the provider refused the request. */
|
|
9
|
+
invalid_request: false,
|
|
10
|
+
/** Credentials missing, not valid or not allowed. */
|
|
11
|
+
auth: false,
|
|
12
|
+
/** Throttled; see `retryAfterMs`. */
|
|
13
|
+
rate_limited: true,
|
|
14
|
+
/** Out of credits or over a billing limit. */
|
|
15
|
+
quota_exceeded: false,
|
|
16
|
+
/** The prompt is too long for the model. */
|
|
17
|
+
context_overflow: false,
|
|
18
|
+
/** A provider's safety policy refused it; sending it again as it is will not help. */
|
|
19
|
+
policy_violation: false,
|
|
20
|
+
/** An unknown model, or a previous response that expired or is not valid; see `providerParam`. */
|
|
21
|
+
not_found: false,
|
|
22
|
+
/** The provider is out of capacity. */
|
|
23
|
+
overloaded: true,
|
|
24
|
+
/** Any other failure at the provider. */
|
|
25
|
+
provider_error: true,
|
|
26
|
+
/** The provider answered with something that cannot be read. */
|
|
27
|
+
invalid_response: false,
|
|
28
|
+
/** The provider or the gateway could not be reached. */
|
|
29
|
+
network_error: true,
|
|
30
|
+
/** The call took longer than allowed. */
|
|
31
|
+
timeout: true,
|
|
32
|
+
/** The handler's signal fired: the effect was cancelled, aborted or timed out. */
|
|
33
|
+
cancelled: false,
|
|
34
|
+
/** A bug in the gateway. */
|
|
35
|
+
internal_error: false,
|
|
36
|
+
};
|
|
37
|
+
export const LLM_FAILURE_KINDS = Object.keys(RETRYABLE).filter((kind) => kind in RETRYABLE);
|
|
38
|
+
const DETAIL = [
|
|
39
|
+
'provider',
|
|
40
|
+
'providerCode',
|
|
41
|
+
'providerType',
|
|
42
|
+
'providerParam',
|
|
43
|
+
'providerError',
|
|
44
|
+
'httpStatus',
|
|
45
|
+
'retryAfterMs',
|
|
46
|
+
'requestId',
|
|
47
|
+
'responseId',
|
|
48
|
+
'usage',
|
|
49
|
+
];
|
|
50
|
+
/** A failed completion. The client throws it; a runner reports its toJSON() to step. */
|
|
51
|
+
export class LlmError extends Error {
|
|
52
|
+
name = 'LlmError';
|
|
53
|
+
failure;
|
|
54
|
+
constructor(message, options) {
|
|
55
|
+
super(message);
|
|
56
|
+
const { kind, retryable, ...detail } = options;
|
|
57
|
+
const failure = {
|
|
58
|
+
name: 'LlmError',
|
|
59
|
+
message,
|
|
60
|
+
kind,
|
|
61
|
+
retryable: retryable ?? RETRYABLE[kind],
|
|
62
|
+
};
|
|
63
|
+
for (const key of DETAIL)
|
|
64
|
+
if (detail[key] !== undefined)
|
|
65
|
+
failure[key] = detail[key];
|
|
66
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion
|
|
67
|
+
this.failure = failure;
|
|
68
|
+
}
|
|
69
|
+
get kind() {
|
|
70
|
+
return this.failure.kind;
|
|
71
|
+
}
|
|
72
|
+
get retryable() {
|
|
73
|
+
return this.failure.retryable;
|
|
74
|
+
}
|
|
75
|
+
/** The failure as Json, which a runner reports as the effect's error. */
|
|
76
|
+
toJSON() {
|
|
77
|
+
return this.failure;
|
|
78
|
+
}
|
|
79
|
+
/** An LlmError from its Json, such as a gateway's answer. */
|
|
80
|
+
static fromJSON({ message, kind, retryable, ...detail }) {
|
|
81
|
+
const options = { ...omitName(detail), kind, retryable };
|
|
82
|
+
return new LlmError(message, options);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function omitName(detail) {
|
|
86
|
+
const { name: _name, ...rest } = detail;
|
|
87
|
+
return rest;
|
|
88
|
+
}
|
|
89
|
+
/** Whether a value is an LlmFailure, such as a failed effect's error. */
|
|
90
|
+
export function isLlmFailure(value) {
|
|
91
|
+
if (!isObject(value))
|
|
92
|
+
return false;
|
|
93
|
+
const { name, message, kind, retryable } = value;
|
|
94
|
+
return (name === 'LlmError' &&
|
|
95
|
+
typeof message === 'string' &&
|
|
96
|
+
typeof kind === 'string' &&
|
|
97
|
+
kind in RETRYABLE &&
|
|
98
|
+
typeof retryable === 'boolean');
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The LLM failure in an effect's outcome, or null: for a result, for a
|
|
102
|
+
* failure the runner reported itself (a timeout, a crash) and for one thrown
|
|
103
|
+
* by something other than this package.
|
|
104
|
+
*/
|
|
105
|
+
export function llmFailure(outcome) {
|
|
106
|
+
if (!('failed' in outcome))
|
|
107
|
+
return null;
|
|
108
|
+
const { error } = outcome.failed;
|
|
109
|
+
return isLlmFailure(error) ? error : null;
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=failure.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"failure.js","sourceRoot":"","sources":["../src/failure.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,4EAA4E;AAC5E,4EAA4E;AAC5E,mEAAmE;AAInE,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAGrC,iFAAiF;AACjF,MAAM,SAAS,GAAG;IAChB,mEAAmE;IACnE,eAAe,EAAE,KAAK;IACtB,qDAAqD;IACrD,IAAI,EAAE,KAAK;IACX,qCAAqC;IACrC,YAAY,EAAE,IAAI;IAClB,8CAA8C;IAC9C,cAAc,EAAE,KAAK;IACrB,4CAA4C;IAC5C,gBAAgB,EAAE,KAAK;IACvB,sFAAsF;IACtF,gBAAgB,EAAE,KAAK;IACvB,kGAAkG;IAClG,SAAS,EAAE,KAAK;IAChB,uCAAuC;IACvC,UAAU,EAAE,IAAI;IAChB,yCAAyC;IACzC,cAAc,EAAE,IAAI;IACpB,gEAAgE;IAChE,gBAAgB,EAAE,KAAK;IACvB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json } from '@acentric/types';
|
|
2
|
+
/**
|
|
3
|
+
* Fields merged as they are into the provider's own block for a shared shape,
|
|
4
|
+
* such as Anthropic's `cache_control` on a content part or OpenAI's `detail`
|
|
5
|
+
* on an image. The client never interprets them.
|
|
6
|
+
*/
|
|
7
|
+
export type ProviderFields = {
|
|
8
|
+
readonly [key: string]: Json;
|
|
9
|
+
};
|
|
10
|
+
/** The application's own attributes on a part or message. Never sent to a provider. */
|
|
11
|
+
export type Metadata = {
|
|
12
|
+
readonly [key: string]: Json;
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=fields.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fields.d.ts","sourceRoot":"","sources":["../src/fields.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE5C;;;;GAIG;AACH,MAAM,MAAM,cAAc,GAAG;IAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC;AAE9D,uFAAuF;AACvF,MAAM,MAAM,QAAQ,GAAG;IAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC"}
|
package/dist/fields.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fields.js","sourceRoot":"","sources":["../src/fields.ts"],"names":[],"mappings":"AAAA,4EAA4E"}
|