@arcships/aimux 0.2.1 → 0.3.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/dist/error.d.ts +71 -0
- package/dist/error.js +173 -0
- package/dist/index.d.ts +132 -5
- package/dist/index.js +281 -24
- package/dist/types/AiMuxError.d.ts +29 -15
- package/dist/types/AiMuxError.js +0 -1
- package/dist/types/ApiCallError.d.ts +65 -0
- package/dist/types/ApiCallError.js +3 -0
- package/dist/types/BreakKind.d.ts +1 -0
- package/dist/types/BreakKind.js +3 -0
- package/dist/types/CallOptions.d.ts +27 -1
- package/dist/types/CatalogueSource.d.ts +4 -0
- package/dist/types/CatalogueSource.js +3 -0
- package/dist/types/ChatCompletion.d.ts +16 -0
- package/dist/types/ChatCompletion.js +2 -0
- package/dist/types/ChatCompletionChoice.d.ts +8 -0
- package/dist/types/ChatCompletionChoice.js +2 -0
- package/dist/types/ChatCompletionChunk.d.ts +15 -0
- package/dist/types/ChatCompletionChunk.js +2 -0
- package/dist/types/ChatCompletionChunkChoice.d.ts +8 -0
- package/dist/types/ChatCompletionChunkChoice.js +2 -0
- package/dist/types/ChatCompletionChunkFunction.d.ts +4 -0
- package/dist/types/ChatCompletionChunkFunction.js +3 -0
- package/dist/types/ChatCompletionChunkToolCall.d.ts +7 -0
- package/dist/types/ChatCompletionChunkToolCall.js +2 -0
- package/dist/types/ChatCompletionDelta.d.ts +7 -0
- package/dist/types/ChatCompletionDelta.js +2 -0
- package/dist/types/ChatCompletionFunction.d.ts +4 -0
- package/dist/types/ChatCompletionFunction.js +3 -0
- package/dist/types/ChatCompletionMessage.d.ts +9 -0
- package/dist/types/ChatCompletionMessage.js +2 -0
- package/dist/types/ChatCompletionToolCall.d.ts +6 -0
- package/dist/types/ChatCompletionToolCall.js +2 -0
- package/dist/types/ChatCompletionUsage.d.ts +9 -0
- package/dist/types/ChatCompletionUsage.js +2 -0
- package/dist/types/CompletionTokensDetails.d.ts +3 -0
- package/dist/types/CompletionTokensDetails.js +3 -0
- package/dist/types/FilePart.d.ts +8 -0
- package/dist/types/FilePart.js +2 -0
- package/dist/types/Fingerprint.d.ts +21 -0
- package/dist/types/Fingerprint.js +3 -0
- package/dist/types/GenerateObjectResult.d.ts +48 -0
- package/dist/types/GenerateObjectResult.js +2 -0
- package/dist/types/GenerateTextOptions.d.ts +12 -1
- package/dist/types/GenerateTextResult.d.ts +47 -0
- package/dist/types/HttpExchange.d.ts +23 -0
- package/dist/types/HttpExchange.js +2 -0
- package/dist/types/HttpRecord.d.ts +12 -0
- package/dist/types/HttpRecord.js +3 -0
- package/dist/types/ImageCallOptions.d.ts +1 -1
- package/dist/types/InputRecord.d.ts +16 -0
- package/dist/types/InputRecord.js +2 -0
- package/dist/types/Modality.d.ts +4 -0
- package/dist/types/Modality.js +3 -0
- package/dist/types/ModelCapabilities.d.ts +21 -0
- package/dist/types/ModelCapabilities.js +3 -0
- package/dist/types/ModelCost.d.ts +10 -0
- package/dist/types/ModelCost.js +3 -0
- package/dist/types/ModelLimits.d.ts +17 -0
- package/dist/types/ModelLimits.js +3 -0
- package/dist/types/ModelModalities.d.ts +8 -0
- package/dist/types/ModelModalities.js +2 -0
- package/dist/types/ModelSpec.d.ts +33 -0
- package/dist/types/ModelSpec.js +2 -0
- package/dist/types/ModelType.d.ts +4 -0
- package/dist/types/ModelType.js +3 -0
- package/dist/types/OutcomeRecord.d.ts +14 -0
- package/dist/types/OutcomeRecord.js +2 -0
- package/dist/types/OutcomeStatus.d.ts +4 -0
- package/dist/types/OutcomeStatus.js +3 -0
- package/dist/types/PrefixBreak.d.ts +11 -0
- package/dist/types/PrefixBreak.js +2 -0
- package/dist/types/PromptTokensDetails.d.ts +4 -0
- package/dist/types/PromptTokensDetails.js +3 -0
- package/dist/types/ProviderAuditSpec.d.ts +33 -0
- package/dist/types/ProviderAuditSpec.js +2 -0
- package/dist/types/ProviderFamily.d.ts +5 -0
- package/dist/types/ProviderFamily.js +3 -0
- package/dist/types/ProviderRecord.d.ts +30 -0
- package/dist/types/ProviderRecord.js +2 -0
- package/dist/types/ReasoningMode.d.ts +4 -0
- package/dist/types/ReasoningMode.js +3 -0
- package/dist/types/ReasoningPart.d.ts +9 -0
- package/dist/types/ReasoningPart.js +3 -0
- package/dist/types/ReasoningSpec.d.ts +39 -0
- package/dist/types/ReasoningSpec.js +2 -0
- package/dist/types/ReasoningVisibility.d.ts +4 -0
- package/dist/types/ReasoningVisibility.js +3 -0
- package/dist/types/Recording.d.ts +56 -0
- package/dist/types/Recording.js +2 -0
- package/dist/types/RequestCacheHints.d.ts +9 -0
- package/dist/types/RequestCacheHints.js +3 -0
- package/dist/types/ResponseRecord.d.ts +13 -0
- package/dist/types/ResponseRecord.js +3 -0
- package/dist/types/RuntimeModel.d.ts +22 -0
- package/dist/types/RuntimeModel.js +3 -0
- package/dist/types/SessionCall.d.ts +20 -0
- package/dist/types/SessionCall.js +3 -0
- package/dist/types/SessionChainView.d.ts +10 -0
- package/dist/types/SessionChainView.js +2 -0
- package/dist/types/SessionSource.d.ts +4 -0
- package/dist/types/SessionSource.js +3 -0
- package/dist/types/SessionStepStat.d.ts +27 -0
- package/dist/types/SessionStepStat.js +2 -0
- package/dist/types/SessionView.d.ts +16 -0
- package/dist/types/SessionView.js +2 -0
- package/dist/types/SourcePart.d.ts +9 -0
- package/dist/types/SourcePart.js +3 -0
- package/dist/types/StreamTextResultAggregated.d.ts +74 -0
- package/dist/types/StreamTextResultAggregated.js +2 -0
- package/dist/types/TimeoutConfiguration.d.ts +10 -0
- package/dist/types/TimeoutConfiguration.js +1 -0
- package/dist/types/TimingRecord.d.ts +7 -0
- package/dist/types/TimingRecord.js +3 -0
- package/dist/types/TraceFilter.d.ts +9 -0
- package/dist/types/TraceFilter.js +3 -0
- package/dist/types/TraceRecord.d.ts +54 -0
- package/dist/types/TraceRecord.js +2 -0
- package/dist/types/TraceStats.d.ts +20 -0
- package/dist/types/TraceStats.js +3 -0
- package/dist/types/TranscriptionCallOptions.d.ts +29 -0
- package/dist/types/TranscriptionCallOptions.js +2 -0
- package/dist/types/TranscriptionRequest.d.ts +9 -0
- package/dist/types/TranscriptionRequest.js +3 -0
- package/dist/types/TranscriptionResponse.d.ts +24 -0
- package/dist/types/TranscriptionResponse.js +2 -0
- package/dist/types/TranscriptionResult.d.ts +46 -0
- package/dist/types/TranscriptionResult.js +2 -0
- package/dist/types/TranscriptionSegment.d.ts +17 -0
- package/dist/types/TranscriptionSegment.js +3 -0
- package/dist/types/TranscriptionStreamPart.d.ts +70 -0
- package/dist/types/TranscriptionStreamPart.js +2 -0
- package/dist/types/UsageSnapshot.d.ts +18 -0
- package/dist/types/UsageSnapshot.js +2 -0
- package/dist/types/Verdict.d.ts +23 -0
- package/dist/types/Verdict.js +2 -0
- package/dist/types/VerdictConfidence.d.ts +4 -0
- package/dist/types/VerdictConfidence.js +3 -0
- package/dist/types/VerdictKind.d.ts +4 -0
- package/dist/types/VerdictKind.js +3 -0
- package/dist/types/VideoCallOptions.d.ts +1 -1
- package/dist/types.d.ts +66 -1
- package/dist/types.js +3 -3
- package/index.d.ts +256 -25
- package/index.js +72 -54
- package/package.json +9 -8
package/dist/index.js
CHANGED
|
@@ -7,35 +7,125 @@
|
|
|
7
7
|
// Users would otherwise have to JSON.stringify every input and JSON.parse every
|
|
8
8
|
// output, with no static types. This wrapper erases that JSON boundary: inputs
|
|
9
9
|
// and outputs are typed objects, using the ts-rs generated types from
|
|
10
|
-
// `./types.ts` (
|
|
11
|
-
// the Rust core,
|
|
10
|
+
// `./types.ts` (ts-rs exports directly into `./types/` — single source of
|
|
11
|
+
// truth in the Rust core, packaged with the npm tarball).
|
|
12
12
|
//
|
|
13
13
|
// The raw napi API (`index.js` / `index.d.ts`, auto-generated) is untouched.
|
|
14
14
|
// This is purely an extra TypeScript layer on top.
|
|
15
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
16
|
+
if (k2 === undefined) k2 = k;
|
|
17
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
18
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
19
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
20
|
+
}
|
|
21
|
+
Object.defineProperty(o, k2, desc);
|
|
22
|
+
}) : (function(o, m, k, k2) {
|
|
23
|
+
if (k2 === undefined) k2 = k;
|
|
24
|
+
o[k2] = m[k];
|
|
25
|
+
}));
|
|
26
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
27
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
28
|
+
}) : function(o, v) {
|
|
29
|
+
o["default"] = v;
|
|
30
|
+
});
|
|
31
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
32
|
+
var ownKeys = function(o) {
|
|
33
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
34
|
+
var ar = [];
|
|
35
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
36
|
+
return ar;
|
|
37
|
+
};
|
|
38
|
+
return ownKeys(o);
|
|
39
|
+
};
|
|
40
|
+
return function (mod) {
|
|
41
|
+
if (mod && mod.__esModule) return mod;
|
|
42
|
+
var result = {};
|
|
43
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
44
|
+
__setModuleDefault(result, mod);
|
|
45
|
+
return result;
|
|
46
|
+
};
|
|
47
|
+
})();
|
|
15
48
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.provider = exports.azure = exports.anthropicAws = exports.vertex = exports.bedrock = exports.xai = exports.mistral = exports.cohere = exports.google = exports.deepseek = exports.anthropic = exports.openai = exports.initLogging = exports.AbortBridge = exports.StreamTextGenerator = exports.Model = void 0;
|
|
49
|
+
exports.ProviderHandleTyped = exports.ProviderName = exports.provider = exports.azure = exports.anthropicAws = exports.vertex = exports.bedrock = exports.xai = exports.mistral = exports.cohere = exports.google = exports.deepseek = exports.anthropic = exports.openai = exports.moa = exports.router = exports.mockReplay = exports.listSessions = exports.sessionCalls = exports.initSessionInfer = exports.initSessionStore = exports.initLogging = exports.AbortBridge = exports.StreamTextGenerator = exports.Model = exports.withAimuxError = exports.OtherError = exports.RequestAbortedError = exports.TimeoutError = exports.NoSuchProviderError = exports.NoSuchModelError = exports.UnsupportedFunctionalityError = exports.TokenExpiredError = exports.InvalidPromptError = exports.InvalidArgumentError = exports.ToolError = exports.InvalidResponseDataError = exports.JSONParseError = exports.APICallError = exports.AimuxError = void 0;
|
|
17
50
|
exports.generateText = generateText;
|
|
18
51
|
exports.streamText = streamText;
|
|
52
|
+
exports.getSessionCalls = getSessionCalls;
|
|
53
|
+
exports.getSessions = getSessions;
|
|
54
|
+
exports.generateTextAsOpenai = generateTextAsOpenai;
|
|
55
|
+
exports.streamTextAsOpenai = streamTextAsOpenai;
|
|
56
|
+
exports.createProvider = createProvider;
|
|
57
|
+
exports.getModelSpecs = getModelSpecs;
|
|
58
|
+
const native = __importStar(require("../index.js"));
|
|
19
59
|
const index_js_1 = require("../index.js");
|
|
60
|
+
// Error hierarchy (throw/catch). Wire payload type `AiMuxError` lives under StreamPart only.
|
|
61
|
+
var error_ts_1 = require("./error.js");
|
|
62
|
+
Object.defineProperty(exports, "AimuxError", { enumerable: true, get: function () { return error_ts_1.AimuxError; } });
|
|
63
|
+
Object.defineProperty(exports, "APICallError", { enumerable: true, get: function () { return error_ts_1.APICallError; } });
|
|
64
|
+
Object.defineProperty(exports, "JSONParseError", { enumerable: true, get: function () { return error_ts_1.JSONParseError; } });
|
|
65
|
+
Object.defineProperty(exports, "InvalidResponseDataError", { enumerable: true, get: function () { return error_ts_1.InvalidResponseDataError; } });
|
|
66
|
+
Object.defineProperty(exports, "ToolError", { enumerable: true, get: function () { return error_ts_1.ToolError; } });
|
|
67
|
+
Object.defineProperty(exports, "InvalidArgumentError", { enumerable: true, get: function () { return error_ts_1.InvalidArgumentError; } });
|
|
68
|
+
Object.defineProperty(exports, "InvalidPromptError", { enumerable: true, get: function () { return error_ts_1.InvalidPromptError; } });
|
|
69
|
+
Object.defineProperty(exports, "TokenExpiredError", { enumerable: true, get: function () { return error_ts_1.TokenExpiredError; } });
|
|
70
|
+
Object.defineProperty(exports, "UnsupportedFunctionalityError", { enumerable: true, get: function () { return error_ts_1.UnsupportedFunctionalityError; } });
|
|
71
|
+
Object.defineProperty(exports, "NoSuchModelError", { enumerable: true, get: function () { return error_ts_1.NoSuchModelError; } });
|
|
72
|
+
Object.defineProperty(exports, "NoSuchProviderError", { enumerable: true, get: function () { return error_ts_1.NoSuchProviderError; } });
|
|
73
|
+
Object.defineProperty(exports, "TimeoutError", { enumerable: true, get: function () { return error_ts_1.TimeoutError; } });
|
|
74
|
+
Object.defineProperty(exports, "RequestAbortedError", { enumerable: true, get: function () { return error_ts_1.RequestAbortedError; } });
|
|
75
|
+
Object.defineProperty(exports, "OtherError", { enumerable: true, get: function () { return error_ts_1.OtherError; } });
|
|
76
|
+
Object.defineProperty(exports, "withAimuxError", { enumerable: true, get: function () { return error_ts_1.withAimuxError; } });
|
|
77
|
+
const error_ts_2 = require("./error.js");
|
|
20
78
|
// Re-export the raw napi constructors/factories so consumers can do everything
|
|
21
79
|
// from a single import: `import { openai, generateText } from 'aimux'`.
|
|
80
|
+
// Rust fn names are snake_case; napi-rs exposes them camelCased (like
|
|
81
|
+
// `init_logging` → `initLogging`).
|
|
82
|
+
//
|
|
83
|
+
// Infallible items (classes + void initializers) pass through untouched.
|
|
22
84
|
var index_js_2 = require("../index.js");
|
|
23
85
|
Object.defineProperty(exports, "Model", { enumerable: true, get: function () { return index_js_2.Model; } });
|
|
24
86
|
Object.defineProperty(exports, "StreamTextGenerator", { enumerable: true, get: function () { return index_js_2.StreamTextGenerator; } });
|
|
25
87
|
Object.defineProperty(exports, "AbortBridge", { enumerable: true, get: function () { return index_js_2.AbortBridge; } });
|
|
26
88
|
Object.defineProperty(exports, "initLogging", { enumerable: true, get: function () { return index_js_2.initLogging; } });
|
|
27
|
-
Object.defineProperty(exports, "
|
|
28
|
-
Object.defineProperty(exports, "
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
89
|
+
Object.defineProperty(exports, "initSessionStore", { enumerable: true, get: function () { return index_js_2.initSessionStore; } });
|
|
90
|
+
Object.defineProperty(exports, "initSessionInfer", { enumerable: true, get: function () { return index_js_2.initSessionInfer; } });
|
|
91
|
+
// Fallible raw functions are wrapped so their throws are typed `AimuxError`
|
|
92
|
+
// subclasses (`instanceof` works); generic wrappers preserve the signatures.
|
|
93
|
+
function wrapSync(fn) {
|
|
94
|
+
return (...args) => {
|
|
95
|
+
try {
|
|
96
|
+
return fn(...args);
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
throw error_ts_2.AimuxError.fromNative(e);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function wrapAsync(fn) {
|
|
104
|
+
return (...args) => (0, error_ts_2.withAimuxError)(() => fn(...args));
|
|
105
|
+
}
|
|
106
|
+
exports.sessionCalls = wrapSync(native.sessionCalls);
|
|
107
|
+
exports.listSessions = wrapSync(native.listSessions);
|
|
108
|
+
exports.mockReplay = wrapSync(native.mockReplay);
|
|
109
|
+
exports.router = wrapSync(native.router);
|
|
110
|
+
exports.moa = wrapSync(native.moa);
|
|
111
|
+
exports.openai = wrapAsync(native.openai);
|
|
112
|
+
exports.anthropic = wrapAsync(native.anthropic);
|
|
113
|
+
exports.deepseek = wrapAsync(native.deepseek);
|
|
114
|
+
exports.google = wrapAsync(native.google);
|
|
115
|
+
exports.cohere = wrapAsync(native.cohere);
|
|
116
|
+
exports.mistral = wrapAsync(native.mistral);
|
|
117
|
+
exports.xai = wrapAsync(native.xai);
|
|
118
|
+
exports.bedrock = wrapAsync(native.bedrock);
|
|
119
|
+
exports.vertex = wrapAsync(native.vertex);
|
|
120
|
+
exports.anthropicAws = wrapAsync(native.anthropicAws);
|
|
121
|
+
exports.azure = wrapAsync(native.azure);
|
|
122
|
+
exports.provider = wrapAsync(native.provider);
|
|
123
|
+
// Both meanings: the `ProviderName` const object (runtime, for `ProviderName.groq`)
|
|
124
|
+
// and the derived string-union type. A value export resolves at runtime, so the
|
|
125
|
+
// specifier needs the real `.ts` extension for Node's type-stripping test runs;
|
|
126
|
+
// tsc rewrites it to `.js` on emit (rewriteRelativeImportExtensions).
|
|
127
|
+
var ProviderName_ts_1 = require("./types/ProviderName.js");
|
|
128
|
+
Object.defineProperty(exports, "ProviderName", { enumerable: true, get: function () { return ProviderName_ts_1.ProviderName; } });
|
|
39
129
|
/**
|
|
40
130
|
* Generate text (non-streaming). Returns a typed {@link GenerateTextResult}.
|
|
41
131
|
*
|
|
@@ -58,10 +148,12 @@ Object.defineProperty(exports, "provider", { enumerable: true, get: function ()
|
|
|
58
148
|
* ```
|
|
59
149
|
*/
|
|
60
150
|
async function generateText(model, prompt, options, signal) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
151
|
+
return (0, error_ts_2.withAimuxError)(async () => {
|
|
152
|
+
const optsJson = options ? JSON.stringify(options) : undefined;
|
|
153
|
+
const bridge = signal ? new index_js_1.AbortBridge(signal) : undefined;
|
|
154
|
+
const resultJson = await model.generateText(JSON.stringify(prompt), optsJson, bridge);
|
|
155
|
+
return JSON.parse(resultJson);
|
|
156
|
+
});
|
|
65
157
|
}
|
|
66
158
|
/**
|
|
67
159
|
* Stream text from a model. Yields typed {@link StreamPart}s.
|
|
@@ -85,10 +177,175 @@ async function generateText(model, prompt, options, signal) {
|
|
|
85
177
|
* ```
|
|
86
178
|
*/
|
|
87
179
|
async function* streamText(model, prompt, options, signal) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
180
|
+
try {
|
|
181
|
+
const optsJson = options ? JSON.stringify(options) : undefined;
|
|
182
|
+
const bridge = signal ? new index_js_1.AbortBridge(signal) : undefined;
|
|
183
|
+
const gen = await model.streamText(JSON.stringify(prompt), optsJson, bridge);
|
|
184
|
+
for await (const json of gen) {
|
|
185
|
+
yield JSON.parse(json);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
catch (e) {
|
|
189
|
+
throw error_ts_2.AimuxError.fromNative(e);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* All calls of a session, ordered by step (RFC-0024). Empty if the session
|
|
194
|
+
* is unknown or no store is registered.
|
|
195
|
+
*
|
|
196
|
+
* `initSessionStore()` / `initSessionInfer(enabled)` (raw napi, re-exported
|
|
197
|
+
* above) must be called first to register the store / opt-in inferer.
|
|
198
|
+
*/
|
|
199
|
+
function getSessionCalls(sessionId) {
|
|
200
|
+
try {
|
|
201
|
+
return JSON.parse(native.sessionCalls(sessionId));
|
|
202
|
+
}
|
|
203
|
+
catch (e) {
|
|
204
|
+
throw error_ts_2.AimuxError.fromNative(e);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* All known sessions (RFC-0024).
|
|
209
|
+
*/
|
|
210
|
+
function getSessions() {
|
|
211
|
+
try {
|
|
212
|
+
return JSON.parse(native.listSessions());
|
|
213
|
+
}
|
|
214
|
+
catch (e) {
|
|
215
|
+
throw error_ts_2.AimuxError.fromNative(e);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Generate text and return an OpenAI Chat Completion (non-streaming).
|
|
220
|
+
*
|
|
221
|
+
* Works with **any** provider — the result is always a standard OpenAI
|
|
222
|
+
* `ChatCompletion` object.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```ts
|
|
226
|
+
* import { openai, generateTextAsOpenai } from 'aimux'
|
|
227
|
+
* const model = await openai(apiKey, 'gpt-4o')
|
|
228
|
+
* const completion = await generateTextAsOpenai(model, 'What is Rust?')
|
|
229
|
+
* console.log(completion.choices[0].message.content)
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
async function generateTextAsOpenai(model, prompt, options, signal) {
|
|
233
|
+
return (0, error_ts_2.withAimuxError)(async () => {
|
|
234
|
+
const optsJson = options ? JSON.stringify(options) : undefined;
|
|
235
|
+
const bridge = signal ? new index_js_1.AbortBridge(signal) : undefined;
|
|
236
|
+
const resultJson = await model.generateTextAsOpenai(JSON.stringify(prompt), optsJson, bridge);
|
|
237
|
+
return JSON.parse(resultJson);
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Stream text as OpenAI Chat Completion chunks.
|
|
242
|
+
*
|
|
243
|
+
* Works with **any** provider — yields standard OpenAI `ChatCompletionChunk`
|
|
244
|
+
* objects. Pass `streamOptions` via `providerOptions.openai.stream_options`:
|
|
245
|
+
* `{ include_usage: true, include_reasoning: true }` (both default true).
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```ts
|
|
249
|
+
* import { openai, streamTextAsOpenai } from 'aimux'
|
|
250
|
+
* const model = await openai(apiKey, 'gpt-4o')
|
|
251
|
+
* for await (const chunk of streamTextAsOpenai(model, 'Write a haiku.')) {
|
|
252
|
+
* const delta = chunk.choices[0]?.delta?.content
|
|
253
|
+
* if (delta) process.stdout.write(delta)
|
|
254
|
+
* }
|
|
255
|
+
* ```
|
|
256
|
+
*/
|
|
257
|
+
async function* streamTextAsOpenai(model, prompt, options, signal) {
|
|
258
|
+
try {
|
|
259
|
+
const optsJson = options ? JSON.stringify(options) : undefined;
|
|
260
|
+
const bridge = signal ? new index_js_1.AbortBridge(signal) : undefined;
|
|
261
|
+
const gen = await model.streamTextAsOpenai(JSON.stringify(prompt), optsJson, bridge);
|
|
262
|
+
for await (const json of gen) {
|
|
263
|
+
yield JSON.parse(json);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
catch (e) {
|
|
267
|
+
throw error_ts_2.AimuxError.fromNative(e);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
271
|
+
// Provider handles (RFC-0027) — createProvider / listModels / model
|
|
272
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
273
|
+
/**
|
|
274
|
+
* A typed wrapper around the raw napi `ProviderHandle` class. Created by
|
|
275
|
+
* {@link createProvider}, supports {@link ProviderHandleTyped.listModels} and
|
|
276
|
+
* {@link ProviderHandleTyped.model}.
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ```ts
|
|
280
|
+
* import { createProvider, generateText } from 'aimux'
|
|
281
|
+
* const p = await createProvider('deepseek', apiKey)
|
|
282
|
+
* const models = await p.listModels()
|
|
283
|
+
* const model = await p.model(models[0].id)
|
|
284
|
+
* const result = await generateText(model, 'Hello')
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
class ProviderHandleTyped {
|
|
288
|
+
constructor(raw) {
|
|
289
|
+
this.raw = raw;
|
|
290
|
+
}
|
|
291
|
+
/** List models available on this provider (runtime discovery + anya2a spec). */
|
|
292
|
+
async listModels() {
|
|
293
|
+
return (0, error_ts_2.withAimuxError)(async () => {
|
|
294
|
+
return JSON.parse(await this.raw.listModels());
|
|
295
|
+
});
|
|
93
296
|
}
|
|
297
|
+
/** Build a language model from a discovered model id. */
|
|
298
|
+
async model(modelId) {
|
|
299
|
+
return (0, error_ts_2.withAimuxError)(() => this.raw.model(modelId));
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
exports.ProviderHandleTyped = ProviderHandleTyped;
|
|
303
|
+
/**
|
|
304
|
+
* Create a **provider handle** (RFC-0027) for a registry-backed provider.
|
|
305
|
+
*
|
|
306
|
+
* Unlike `provider()` (which binds to a single modelId), this returns a handle
|
|
307
|
+
* that supports `listModels()` (runtime discovery) and `model()`.
|
|
308
|
+
*
|
|
309
|
+
* @param name - Provider name (e.g. `"deepseek"`, `"groq"`).
|
|
310
|
+
* @param apiKey - API key; omit/null to read the provider's env var.
|
|
311
|
+
* @param config - Optional provider config (baseUrl, headers, …).
|
|
312
|
+
*
|
|
313
|
+
* @example
|
|
314
|
+
* ```ts
|
|
315
|
+
* import { createProvider, generateText } from 'aimux'
|
|
316
|
+
* const p = await createProvider('deepseek', process.env.DEEPSEEK_API_KEY)
|
|
317
|
+
* const models = await p.listModels()
|
|
318
|
+
* const model = await p.model(models[0].id)
|
|
319
|
+
* const result = await generateText(model, 'Hello')
|
|
320
|
+
* ```
|
|
321
|
+
*/
|
|
322
|
+
async function createProvider(name, apiKey, config) {
|
|
323
|
+
return (0, error_ts_2.withAimuxError)(async () => {
|
|
324
|
+
const raw = await (0, index_js_1.createProvider)(name, apiKey ?? null, config ?? null);
|
|
325
|
+
return new ProviderHandleTyped(raw);
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Fetch the community model catalogue (anya2a). Returns a `Catalogue` object
|
|
330
|
+
* with a `lookup(provider, modelId)` method. Thin fetch — no caching; the host
|
|
331
|
+
* decides how to persist/reuse the result.
|
|
332
|
+
*
|
|
333
|
+
* @param sourceUrl - Optional URL override (default = anya2a endpoint).
|
|
334
|
+
*
|
|
335
|
+
* @example
|
|
336
|
+
* ```ts
|
|
337
|
+
* import { createProvider, getModelSpecs, generateText } from 'aimux'
|
|
338
|
+
* const [p, catalogue] = await Promise.all([
|
|
339
|
+
* createProvider('deepseek', apiKey),
|
|
340
|
+
* getModelSpecs(),
|
|
341
|
+
* ])
|
|
342
|
+
* const models = await p.listModels()
|
|
343
|
+
* const model = await p.model(models[0].id)
|
|
344
|
+
* const spec = catalogue.specs?.['deepseek']?.[models[0].id] // community portrait
|
|
345
|
+
* ```
|
|
346
|
+
*/
|
|
347
|
+
async function getModelSpecs(sourceUrl) {
|
|
348
|
+
return (0, error_ts_2.withAimuxError)(async () => {
|
|
349
|
+
return JSON.parse(await (0, index_js_1.getModelSpecs)(sourceUrl ?? null));
|
|
350
|
+
});
|
|
94
351
|
}
|
|
@@ -1,14 +1,23 @@
|
|
|
1
|
+
import type { ApiCallError } from "./ApiCallError";
|
|
1
2
|
/**
|
|
2
3
|
* Unified error type for all aimux operations.
|
|
4
|
+
*
|
|
5
|
+
* Variants are cut by *what the caller does about it*, not by where the
|
|
6
|
+
* failure came from. `ApiCall` carries the [`ApiCallError`] detail inline
|
|
7
|
+
* (unboxed, like async-openai's `ApiError(ApiError)`); the size guard in
|
|
8
|
+
* `error_value_golden_test` keeps the enum under clippy's
|
|
9
|
+
* `result_large_err` threshold.
|
|
10
|
+
*
|
|
11
|
+
* Constructed as a named struct literal everywhere
|
|
12
|
+
* (`ApiCallError { status_code: .., ..Default::default() }`), the same
|
|
13
|
+
* shape as the AI SDK's named-options constructor.
|
|
3
14
|
*/
|
|
4
15
|
export type AiMuxError = {
|
|
5
|
-
"
|
|
16
|
+
"ApiCall": ApiCallError;
|
|
6
17
|
} | {
|
|
7
|
-
"
|
|
18
|
+
"JsonParse": string;
|
|
8
19
|
} | {
|
|
9
|
-
"
|
|
10
|
-
} | {
|
|
11
|
-
"Stream": string;
|
|
20
|
+
"InvalidResponseData": string;
|
|
12
21
|
} | {
|
|
13
22
|
"Tool": string;
|
|
14
23
|
} | {
|
|
@@ -16,19 +25,24 @@ export type AiMuxError = {
|
|
|
16
25
|
} | {
|
|
17
26
|
"InvalidPrompt": string;
|
|
18
27
|
} | {
|
|
19
|
-
"
|
|
20
|
-
retry_after_ms: bigint;
|
|
21
|
-
};
|
|
22
|
-
} | {
|
|
23
|
-
"Auth": string;
|
|
28
|
+
"TokenExpired": string;
|
|
24
29
|
} | {
|
|
25
|
-
"
|
|
30
|
+
"UnsupportedFunctionality": string;
|
|
26
31
|
} | {
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
32
|
+
"NoSuchModel": {
|
|
33
|
+
model_id: string;
|
|
34
|
+
/**
|
|
35
|
+
* What kind of model was requested (`"languageModel"`,
|
|
36
|
+
* `"imageModel"`, …), the AI SDK's `modelType`.
|
|
37
|
+
*/
|
|
38
|
+
model_type: string;
|
|
39
|
+
};
|
|
30
40
|
} | {
|
|
31
|
-
"
|
|
41
|
+
"NoSuchProvider": {
|
|
42
|
+
provider_id: string;
|
|
43
|
+
};
|
|
32
44
|
} | {
|
|
45
|
+
"Timeout": string;
|
|
46
|
+
} | "Aborted" | {
|
|
33
47
|
"Other": string;
|
|
34
48
|
};
|
package/dist/types/AiMuxError.js
CHANGED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a failed API call observed — the field set of the AI SDK's
|
|
3
|
+
* `APICallError` (`statusCode` / `responseBody`), which this project's error
|
|
4
|
+
* taxonomy mirrors (docs/internal/vercel-research/07-kernel-infrastructure.md).
|
|
5
|
+
*
|
|
6
|
+
* Carried by [`AiMuxError::ApiCall`] alone. There are no per-status
|
|
7
|
+
* variants: a 401, 404 or 429 is an `ApiCall` error whose classification is
|
|
8
|
+
* read from `status_code`, exactly as the AI SDK reads
|
|
9
|
+
* `APICallError.statusCode`. Transport failures (connection reset, DNS,
|
|
10
|
+
* body read) are `ApiCall` errors too — no response arrived, so
|
|
11
|
+
* `status_code` is `None` and `is_retryable` is `true`, exactly as the AI
|
|
12
|
+
* SDK's `handleFetchError` builds an `APICallError` with no `statusCode`
|
|
13
|
+
* and `isRetryable: true`. `message` holds the provider's text **only** —
|
|
14
|
+
* the status is a field, never baked into the string; `Display` composes
|
|
15
|
+
* the human-readable form from the fields at print time, so nothing
|
|
16
|
+
* downstream has to parse it back out.
|
|
17
|
+
*
|
|
18
|
+
* `APICallError` fields not carried in this round: `url` /
|
|
19
|
+
* `requestBodyValues` / `responseHeaders` (the request context is not
|
|
20
|
+
* available at the error-construction sites today; all fields are
|
|
21
|
+
* `#[serde(default)]`, so adding them later is not a breaking change).
|
|
22
|
+
*/
|
|
23
|
+
export type ApiCallError = {
|
|
24
|
+
/**
|
|
25
|
+
* HTTP status of the response, when it came from one
|
|
26
|
+
* (`APICallError.statusCode`). Always the *observed* status: the HTTP
|
|
27
|
+
* layer fills it for every response-derived error; errors built without
|
|
28
|
+
* an HTTP exchange leave it `None`.
|
|
29
|
+
*/
|
|
30
|
+
status_code: number | null;
|
|
31
|
+
/**
|
|
32
|
+
* Provider's machine-readable code (OpenAI's `code`/`type`,
|
|
33
|
+
* e.g. `"rate_limit_exceeded"` vs `"insufficient_quota"`). Never an HTTP
|
|
34
|
+
* status. Our normalized take on `APICallError.data`.
|
|
35
|
+
*/
|
|
36
|
+
provider_code: string | null;
|
|
37
|
+
/**
|
|
38
|
+
* The provider's message, verbatim. No status prefix.
|
|
39
|
+
*/
|
|
40
|
+
message: string;
|
|
41
|
+
/**
|
|
42
|
+
* The raw response body, verbatim (`APICallError.responseBody`) — the
|
|
43
|
+
* lossless evidence when `message`/`provider_code` are extractions.
|
|
44
|
+
* `None` when the error did not come from a response body.
|
|
45
|
+
*/
|
|
46
|
+
response_body: string | null;
|
|
47
|
+
/**
|
|
48
|
+
* Provider-assigned request id, from the `x-request-id` / `request-id`
|
|
49
|
+
* response header when one was sent. Filled by the shared HTTP layer.
|
|
50
|
+
*/
|
|
51
|
+
request_id: string | null;
|
|
52
|
+
/**
|
|
53
|
+
* Retry hint in milliseconds, distilled from the `retry-after-ms` /
|
|
54
|
+
* `retry-after` response headers on a 429. The classification lives in
|
|
55
|
+
* `status_code`; this is the matching action input.
|
|
56
|
+
*/
|
|
57
|
+
retry_after_ms: number | null;
|
|
58
|
+
/**
|
|
59
|
+
* Whether retrying can help (`APICallError.isRetryable`) — stored at
|
|
60
|
+
* construction, exactly like the AI SDK: the response path computes it
|
|
61
|
+
* from the status (408/409/429 or 5xx), and the transport path (no response, so
|
|
62
|
+
* no status to compute from) sets it to `true` explicitly.
|
|
63
|
+
*/
|
|
64
|
+
is_retryable: boolean;
|
|
65
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type BreakKind = "SystemChanged" | "ToolsChanged" | "ConversationReset" | "Unknown";
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { LanguageModelPromptMessage } from "./LanguageModelPromptMessage";
|
|
2
2
|
import type { ReasoningEffort } from "./ReasoningEffort";
|
|
3
3
|
import type { ResponseFormat } from "./ResponseFormat";
|
|
4
|
+
import type { TimeoutConfiguration } from "./TimeoutConfiguration";
|
|
4
5
|
import type { Tool } from "./Tool";
|
|
5
6
|
import type { ToolChoice } from "./ToolChoice";
|
|
6
7
|
import type { JsonValue } from "./serde_json/JsonValue";
|
|
@@ -51,7 +52,7 @@ export type CallOptions = {
|
|
|
51
52
|
/**
|
|
52
53
|
* Seed for reproducibility.
|
|
53
54
|
*/
|
|
54
|
-
seed:
|
|
55
|
+
seed: number | null;
|
|
55
56
|
/**
|
|
56
57
|
* Tools available to the model (function tools and/or provider-defined tools).
|
|
57
58
|
*/
|
|
@@ -88,4 +89,29 @@ export type CallOptions = {
|
|
|
88
89
|
* `RetryConfig.max_retries`. `Some(0)` disables retries.
|
|
89
90
|
*/
|
|
90
91
|
max_retries: number | null;
|
|
92
|
+
/**
|
|
93
|
+
* Per-call timeout configuration (total / first-chunk / chunk idle).
|
|
94
|
+
* `None` = no timeouts (provider defaults still apply at the HTTP layer).
|
|
95
|
+
*/
|
|
96
|
+
timeout: TimeoutConfiguration | null;
|
|
97
|
+
/**
|
|
98
|
+
* Session identifier, for grouping consecutive calls into a session
|
|
99
|
+
* (observability, see RFC-0024). Explicit values take precedence; when
|
|
100
|
+
* `None` and the optional session inferer is enabled, one may be inferred.
|
|
101
|
+
* Orthogonal to RFC-0019 session-affinity headers: this field is for
|
|
102
|
+
* local grouping, while session headers in `headers` are for upstream
|
|
103
|
+
* routing — both may share an id value but travel different paths.
|
|
104
|
+
*/
|
|
105
|
+
session_id: string | null;
|
|
106
|
+
/**
|
|
107
|
+
* Emit raw provider stream chunks as `StreamPart::Raw` (debugging aid).
|
|
108
|
+
* When `Some(true)`, streaming providers yield one `Raw` part per JSON
|
|
109
|
+
* SSE event, carrying the **parsed JSON payload** of the event, emitted
|
|
110
|
+
* before the parsed parts. Excludes the `[DONE]` sentinel; unparsable
|
|
111
|
+
* chunks emit only `Error`. `null`/`Some(false)` = off.
|
|
112
|
+
* Currently honored by the OpenAI-compatible family (openai / azure /
|
|
113
|
+
* openai-compatible registry providers); other provider families ignore
|
|
114
|
+
* it for now (RFC-0016 M2).
|
|
115
|
+
*/
|
|
116
|
+
include_raw_chunks: boolean | null;
|
|
91
117
|
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ChatCompletionChoice } from "./ChatCompletionChoice";
|
|
2
|
+
import type { ChatCompletionUsage } from "./ChatCompletionUsage";
|
|
3
|
+
/**
|
|
4
|
+
* A complete Chat Completion response (non-streaming).
|
|
5
|
+
*
|
|
6
|
+
* Mirrors the OpenAI `chat.completion` object.
|
|
7
|
+
*/
|
|
8
|
+
export type ChatCompletion = {
|
|
9
|
+
id: string;
|
|
10
|
+
object: string;
|
|
11
|
+
created: bigint;
|
|
12
|
+
model: string;
|
|
13
|
+
choices: Array<ChatCompletionChoice>;
|
|
14
|
+
usage: ChatCompletionUsage;
|
|
15
|
+
system_fingerprint: string | null;
|
|
16
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ChatCompletionMessage } from "./ChatCompletionMessage";
|
|
2
|
+
import type { JsonValue } from "./serde_json/JsonValue";
|
|
3
|
+
export type ChatCompletionChoice = {
|
|
4
|
+
index: number;
|
|
5
|
+
message: ChatCompletionMessage;
|
|
6
|
+
finish_reason: string | null;
|
|
7
|
+
logprobs: JsonValue | null;
|
|
8
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ChatCompletionChunkChoice } from "./ChatCompletionChunkChoice";
|
|
2
|
+
import type { ChatCompletionUsage } from "./ChatCompletionUsage";
|
|
3
|
+
/**
|
|
4
|
+
* A single Chat Completion chunk (streaming).
|
|
5
|
+
*
|
|
6
|
+
* Mirrors the OpenAI `chat.completion.chunk` object.
|
|
7
|
+
*/
|
|
8
|
+
export type ChatCompletionChunk = {
|
|
9
|
+
id: string;
|
|
10
|
+
object: string;
|
|
11
|
+
created: bigint;
|
|
12
|
+
model: string;
|
|
13
|
+
choices: Array<ChatCompletionChunkChoice>;
|
|
14
|
+
usage: ChatCompletionUsage | null;
|
|
15
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ChatCompletionDelta } from "./ChatCompletionDelta";
|
|
2
|
+
import type { JsonValue } from "./serde_json/JsonValue";
|
|
3
|
+
export type ChatCompletionChunkChoice = {
|
|
4
|
+
index: number;
|
|
5
|
+
delta: ChatCompletionDelta;
|
|
6
|
+
finish_reason: string | null;
|
|
7
|
+
logprobs: JsonValue | null;
|
|
8
|
+
};
|