@jini-ai/integrations 0.3.6 → 0.4.1
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/CHANGELOG.md +90 -0
- package/README.md +72 -24
- package/dist/args.d.ts +8 -0
- package/dist/args.js +10 -0
- package/dist/argument-error.d.ts +9 -0
- package/dist/argument-error.js +7 -0
- package/dist/credentialed-http/auth-schemes.d.ts +29 -0
- package/dist/credentialed-http/auth-schemes.js +60 -0
- package/dist/credentialed-http/credentialed-request.d.ts +132 -0
- package/dist/credentialed-http/credentialed-request.js +365 -0
- package/dist/credentialed-http/index.d.ts +4 -0
- package/dist/credentialed-http/index.js +4 -0
- package/dist/credentialed-http/ports.d.ts +42 -0
- package/dist/credentialed-http/ports.js +2 -0
- package/dist/media-providers/capability-registry.d.ts +13 -7
- package/dist/media-providers/capability-registry.js +6 -6
- package/dist/media-providers/dispatch/async-operation-store.d.ts +25 -8
- package/dist/media-providers/dispatch/async-operation-store.js +12 -32
- package/dist/media-providers/dispatch/builtin-vendors.d.ts +6 -0
- package/dist/media-providers/dispatch/builtin-vendors.js +30 -0
- package/dist/media-providers/dispatch/context.d.ts +7 -2
- package/dist/media-providers/dispatch/context.js +1 -1
- package/dist/media-providers/dispatch/credentials.d.ts +4 -1
- package/dist/media-providers/dispatch/credentials.js +1 -1
- package/dist/media-providers/dispatch/engine.d.ts +5 -13
- package/dist/media-providers/dispatch/engine.js +25 -26
- package/dist/media-providers/dispatch/index.d.ts +4 -1
- package/dist/media-providers/dispatch/index.js +4 -2
- package/dist/media-providers/dispatch/openai-compatible.d.ts +12 -2
- package/dist/media-providers/dispatch/openai-compatible.js +9 -5
- package/dist/media-providers/dispatch/operation-runtime.d.ts +16 -8
- package/dist/media-providers/dispatch/operation-runtime.js +93 -76
- package/dist/media-providers/dispatch/outbound.d.ts +31 -0
- package/dist/media-providers/dispatch/outbound.js +54 -0
- package/dist/media-providers/dispatch/polling-adapter.d.ts +26 -8
- package/dist/media-providers/dispatch/polling-adapter.js +2 -2
- package/dist/media-providers/dispatch/providers/aihubmix-shared.d.ts +2 -2
- package/dist/media-providers/dispatch/providers/aihubmix-shared.js +2 -2
- package/dist/media-providers/dispatch/providers/aihubmix.d.ts +13 -2
- package/dist/media-providers/dispatch/providers/aihubmix.js +18 -16
- package/dist/media-providers/dispatch/providers/custom-image.d.ts +16 -2
- package/dist/media-providers/dispatch/providers/custom-image.js +12 -9
- package/dist/media-providers/dispatch/providers/elevenlabs.d.ts +13 -2
- package/dist/media-providers/dispatch/providers/elevenlabs.js +16 -14
- package/dist/media-providers/dispatch/providers/fishaudio.d.ts +9 -1
- package/dist/media-providers/dispatch/providers/fishaudio.js +10 -8
- package/dist/media-providers/dispatch/providers/grok.d.ts +16 -3
- package/dist/media-providers/dispatch/providers/grok.js +19 -17
- package/dist/media-providers/dispatch/providers/imagerouter-video-async.d.ts +1 -1
- package/dist/media-providers/dispatch/providers/imagerouter-video-async.js +23 -22
- package/dist/media-providers/dispatch/providers/imagerouter.d.ts +17 -3
- package/dist/media-providers/dispatch/providers/imagerouter.js +21 -19
- package/dist/media-providers/dispatch/providers/minimax.d.ts +9 -1
- package/dist/media-providers/dispatch/providers/minimax.js +9 -7
- package/dist/media-providers/dispatch/providers/nanobanana.d.ts +9 -1
- package/dist/media-providers/dispatch/providers/nanobanana.js +10 -8
- package/dist/media-providers/dispatch/providers/openai.d.ts +13 -2
- package/dist/media-providers/dispatch/providers/openai.js +23 -28
- package/dist/media-providers/dispatch/providers/openrouter.d.ts +12 -2
- package/dist/media-providers/dispatch/providers/openrouter.js +14 -12
- package/dist/media-providers/dispatch/providers/senseaudio.d.ts +13 -2
- package/dist/media-providers/dispatch/providers/senseaudio.js +16 -14
- package/dist/media-providers/dispatch/providers/volcengine.d.ts +9 -1
- package/dist/media-providers/dispatch/providers/volcengine.js +11 -9
- package/dist/media-providers/dispatch/response-parsers.d.ts +21 -7
- package/dist/media-providers/dispatch/response-parsers.js +15 -11
- package/dist/media-providers/dispatch/sqlite-async-operation-store.d.ts +3 -1
- package/dist/media-providers/dispatch/sqlite-async-operation-store.js +30 -28
- package/dist/media-providers/dispatch/ssrf-guard.d.ts +27 -14
- package/dist/media-providers/dispatch/ssrf-guard.js +46 -105
- package/dist/media-providers/dispatch/stub.d.ts +7 -2
- package/dist/media-providers/dispatch/stub.js +4 -3
- package/dist/media-providers/dispatch/types.d.ts +17 -13
- package/dist/media-providers/dispatch/vendor-adapter.d.ts +22 -5
- package/dist/media-providers/dispatch/vendor-adapter.js +13 -10
- package/dist/media-providers/dispatch/vendor-registry.d.ts +14 -11
- package/dist/media-providers/dispatch/vendor-registry.js +33 -4
- package/dist/media-providers/index.d.ts +1 -1
- package/dist/media-providers/index.js +1 -1
- package/dist/media-providers/policy.d.ts +3 -2
- package/dist/media-providers/policy.js +4 -2
- package/dist/media-providers/providers.d.ts +12 -4
- package/dist/media-providers/providers.js +3 -3
- package/dist/media-providers/staging.d.ts +8 -2
- package/dist/media-providers/staging.js +13 -4
- package/dist/media-providers/task-store.d.ts +2 -2
- package/dist/media-providers/task-store.js +2 -2
- package/dist/media-providers/tokens.js +3 -3
- package/dist/media-providers/types.d.ts +4 -4
- package/dist/media-providers/types.js +1 -1
- package/dist/media-providers/video-request.d.ts +35 -15
- package/dist/media-providers/video-request.js +15 -15
- package/dist/webhooks/delivery.d.ts +76 -0
- package/dist/webhooks/delivery.js +222 -0
- package/dist/webhooks/index.d.ts +7 -0
- package/dist/webhooks/index.js +7 -0
- package/dist/webhooks/ports.d.ts +113 -0
- package/dist/webhooks/ports.js +2 -0
- package/dist/webhooks/signing.d.ts +38 -0
- package/dist/webhooks/signing.js +83 -0
- package/dist/webhooks/signing.keyring.d.ts +8 -0
- package/dist/webhooks/signing.keyring.js +18 -0
- package/dist/webhooks/subscriptions.d.ts +78 -0
- package/dist/webhooks/subscriptions.js +139 -0
- package/dist/webhooks/types.d.ts +103 -0
- package/dist/webhooks/types.js +2 -0
- package/package.json +36 -22
- package/dist/composio/bounded-data.d.ts.map +0 -1
- package/dist/composio/bounded-data.js.map +0 -1
- package/dist/composio/catalog.d.ts.map +0 -1
- package/dist/composio/catalog.js.map +0 -1
- package/dist/composio/composio-config.d.ts.map +0 -1
- package/dist/composio/composio-config.js.map +0 -1
- package/dist/composio/composio-descriptions.d.ts.map +0 -1
- package/dist/composio/composio-descriptions.js.map +0 -1
- package/dist/composio/composio.d.ts.map +0 -1
- package/dist/composio/composio.js.map +0 -1
- package/dist/composio/errors.d.ts.map +0 -1
- package/dist/composio/errors.js.map +0 -1
- package/dist/composio/file-lock.d.ts.map +0 -1
- package/dist/composio/file-lock.js.map +0 -1
- package/dist/composio/index.d.ts.map +0 -1
- package/dist/composio/index.js.map +0 -1
- package/dist/composio/json-schema.d.ts.map +0 -1
- package/dist/composio/json-schema.js.map +0 -1
- package/dist/composio/output-protection.d.ts.map +0 -1
- package/dist/composio/output-protection.js.map +0 -1
- package/dist/composio/service.d.ts.map +0 -1
- package/dist/composio/service.js.map +0 -1
- package/dist/media-providers/capability-registry.d.ts.map +0 -1
- package/dist/media-providers/capability-registry.js.map +0 -1
- package/dist/media-providers/catalog.d.ts.map +0 -1
- package/dist/media-providers/catalog.js.map +0 -1
- package/dist/media-providers/dispatch/async-operation-store.d.ts.map +0 -1
- package/dist/media-providers/dispatch/async-operation-store.js.map +0 -1
- package/dist/media-providers/dispatch/context.d.ts.map +0 -1
- package/dist/media-providers/dispatch/context.js.map +0 -1
- package/dist/media-providers/dispatch/credentials.d.ts.map +0 -1
- package/dist/media-providers/dispatch/credentials.js.map +0 -1
- package/dist/media-providers/dispatch/engine.d.ts.map +0 -1
- package/dist/media-providers/dispatch/engine.js.map +0 -1
- package/dist/media-providers/dispatch/index.d.ts.map +0 -1
- package/dist/media-providers/dispatch/index.js.map +0 -1
- package/dist/media-providers/dispatch/openai-compatible.d.ts.map +0 -1
- package/dist/media-providers/dispatch/openai-compatible.js.map +0 -1
- package/dist/media-providers/dispatch/operation-runtime.d.ts.map +0 -1
- package/dist/media-providers/dispatch/operation-runtime.js.map +0 -1
- package/dist/media-providers/dispatch/polling-adapter.d.ts.map +0 -1
- package/dist/media-providers/dispatch/polling-adapter.js.map +0 -1
- package/dist/media-providers/dispatch/providers/aihubmix-shared.d.ts.map +0 -1
- package/dist/media-providers/dispatch/providers/aihubmix-shared.js.map +0 -1
- package/dist/media-providers/dispatch/providers/aihubmix.d.ts.map +0 -1
- package/dist/media-providers/dispatch/providers/aihubmix.js.map +0 -1
- package/dist/media-providers/dispatch/providers/custom-image.d.ts.map +0 -1
- package/dist/media-providers/dispatch/providers/custom-image.js.map +0 -1
- package/dist/media-providers/dispatch/providers/elevenlabs.d.ts.map +0 -1
- package/dist/media-providers/dispatch/providers/elevenlabs.js.map +0 -1
- package/dist/media-providers/dispatch/providers/fishaudio.d.ts.map +0 -1
- package/dist/media-providers/dispatch/providers/fishaudio.js.map +0 -1
- package/dist/media-providers/dispatch/providers/grok.d.ts.map +0 -1
- package/dist/media-providers/dispatch/providers/grok.js.map +0 -1
- package/dist/media-providers/dispatch/providers/imagerouter-video-async.d.ts.map +0 -1
- package/dist/media-providers/dispatch/providers/imagerouter-video-async.js.map +0 -1
- package/dist/media-providers/dispatch/providers/imagerouter.d.ts.map +0 -1
- package/dist/media-providers/dispatch/providers/imagerouter.js.map +0 -1
- package/dist/media-providers/dispatch/providers/minimax.d.ts.map +0 -1
- package/dist/media-providers/dispatch/providers/minimax.js.map +0 -1
- package/dist/media-providers/dispatch/providers/nanobanana.d.ts.map +0 -1
- package/dist/media-providers/dispatch/providers/nanobanana.js.map +0 -1
- package/dist/media-providers/dispatch/providers/openai.d.ts.map +0 -1
- package/dist/media-providers/dispatch/providers/openai.js.map +0 -1
- package/dist/media-providers/dispatch/providers/openrouter.d.ts.map +0 -1
- package/dist/media-providers/dispatch/providers/openrouter.js.map +0 -1
- package/dist/media-providers/dispatch/providers/senseaudio.d.ts.map +0 -1
- package/dist/media-providers/dispatch/providers/senseaudio.js.map +0 -1
- package/dist/media-providers/dispatch/providers/volcengine.d.ts.map +0 -1
- package/dist/media-providers/dispatch/providers/volcengine.js.map +0 -1
- package/dist/media-providers/dispatch/response-parsers.d.ts.map +0 -1
- package/dist/media-providers/dispatch/response-parsers.js.map +0 -1
- package/dist/media-providers/dispatch/sqlite-async-operation-store.d.ts.map +0 -1
- package/dist/media-providers/dispatch/sqlite-async-operation-store.js.map +0 -1
- package/dist/media-providers/dispatch/ssrf-guard.d.ts.map +0 -1
- package/dist/media-providers/dispatch/ssrf-guard.js.map +0 -1
- package/dist/media-providers/dispatch/stub.d.ts.map +0 -1
- package/dist/media-providers/dispatch/stub.js.map +0 -1
- package/dist/media-providers/dispatch/types.d.ts.map +0 -1
- package/dist/media-providers/dispatch/types.js.map +0 -1
- package/dist/media-providers/dispatch/vendor-adapter.d.ts.map +0 -1
- package/dist/media-providers/dispatch/vendor-adapter.js.map +0 -1
- package/dist/media-providers/dispatch/vendor-registry.d.ts.map +0 -1
- package/dist/media-providers/dispatch/vendor-registry.js.map +0 -1
- package/dist/media-providers/index.d.ts.map +0 -1
- package/dist/media-providers/index.js.map +0 -1
- package/dist/media-providers/policy.d.ts.map +0 -1
- package/dist/media-providers/policy.js.map +0 -1
- package/dist/media-providers/providers.d.ts.map +0 -1
- package/dist/media-providers/providers.js.map +0 -1
- package/dist/media-providers/seed.d.ts.map +0 -1
- package/dist/media-providers/seed.js.map +0 -1
- package/dist/media-providers/sqlite-task-store.d.ts.map +0 -1
- package/dist/media-providers/sqlite-task-store.js.map +0 -1
- package/dist/media-providers/staging.d.ts.map +0 -1
- package/dist/media-providers/staging.js.map +0 -1
- package/dist/media-providers/task-store.d.ts.map +0 -1
- package/dist/media-providers/task-store.js.map +0 -1
- package/dist/media-providers/tokens.d.ts.map +0 -1
- package/dist/media-providers/tokens.js.map +0 -1
- package/dist/media-providers/types.d.ts.map +0 -1
- package/dist/media-providers/types.js.map +0 -1
- package/dist/media-providers/video-request.d.ts.map +0 -1
- package/dist/media-providers/video-request.js.map +0 -1
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { partitionArgs } from '../../args.js';
|
|
1
2
|
/**
|
|
2
3
|
* The submit-then-poll runtime: the piece that turns a `PollingVendorAdapter` plus an
|
|
3
4
|
* `AsyncOperationStore` into an operation that survives a restart.
|
|
@@ -21,9 +22,10 @@
|
|
|
21
22
|
*
|
|
22
23
|
* No scheduler. `pollDueOperations` is a single leased tick a host drives from whatever timer or
|
|
23
24
|
* queue it already runs — this package performs no I/O of its own beyond the vendor calls it is
|
|
24
|
-
* handed a `
|
|
25
|
+
* handed a guarded `httpClient` for, a standing invariant across `@jini-ai/integrations/media-providers`.
|
|
25
26
|
*/
|
|
26
|
-
import { FETCH_TIMEOUT_MS
|
|
27
|
+
import { FETCH_TIMEOUT_MS } from '@jini-ai/platform/fetch-with-timeout';
|
|
28
|
+
import { fetchMediaOutbound } from './outbound.js';
|
|
27
29
|
/** Interactive budget for the inline attempt. Deliberately NOT `FETCH_TIMEOUT_MS.GENERATE` (10min) — that is the backstop for the *request*, this is the ceiling on making a human wait. */
|
|
28
30
|
export const DEFAULT_GRACE_MS = 4_000;
|
|
29
31
|
export const DEFAULT_MAX_ATTEMPTS = 60;
|
|
@@ -48,18 +50,24 @@ function toOperationError(error) {
|
|
|
48
50
|
return { message: error instanceof Error ? error.message : String(error) };
|
|
49
51
|
}
|
|
50
52
|
async function performSigned(deps, request, timeoutMs) {
|
|
51
|
-
const signed = await deps.signer(request);
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
const signed = await deps.signer({ request: request });
|
|
54
|
+
return fetchMediaOutbound({ url: signed.url, timeoutMs }, { ...deps, init: signed.init });
|
|
55
|
+
}
|
|
56
|
+
/** Thread only the outbound capability into parsers; stores and signers remain runtime-private. */
|
|
57
|
+
function withRuntimeOutbound({ ctx, deps }) {
|
|
58
|
+
return {
|
|
59
|
+
...ctx,
|
|
60
|
+
...(deps.httpClient === undefined ? {} : { httpClient: deps.httpClient }),
|
|
61
|
+
...(deps.allowPrivateNetwork === undefined ? {} : { allowPrivateNetwork: deps.allowPrivateNetwork }),
|
|
62
|
+
...(deps.outboundMessages === undefined ? {} : { outboundMessages: deps.outboundMessages }),
|
|
63
|
+
};
|
|
56
64
|
}
|
|
57
65
|
/** Issues the submit call and parses it. Isolated from persistence so callers can tell "nothing was obtained from the vendor" apart from "a handle was obtained but recording it failed" — only the former is safe to treat as terminal. */
|
|
58
66
|
async function submitAndParse(deps, params) {
|
|
59
67
|
try {
|
|
60
|
-
const request = params.adapter.buildSubmitRequest(params.ctx);
|
|
68
|
+
const request = params.adapter.buildSubmitRequest({ ctx: params.ctx });
|
|
61
69
|
const resp = await performSigned(deps, request, FETCH_TIMEOUT_MS.GENERATE);
|
|
62
|
-
const outcome = await params.adapter.parseSubmitResponse(resp, params.ctx, request);
|
|
70
|
+
const outcome = await params.adapter.parseSubmitResponse({ resp: resp, ctx: withRuntimeOutbound({ ctx: params.ctx, deps }), request: request });
|
|
63
71
|
return { ok: true, outcome };
|
|
64
72
|
}
|
|
65
73
|
catch (error) {
|
|
@@ -80,7 +88,7 @@ async function submitAndParse(deps, params) {
|
|
|
80
88
|
async function persistWithRetry(store, operationId, patch, retryDelaysMs) {
|
|
81
89
|
for (let attempt = 0; attempt <= retryDelaysMs.length; attempt += 1) {
|
|
82
90
|
try {
|
|
83
|
-
await store.update(operationId, patch);
|
|
91
|
+
await store.update({ id: operationId, patch: patch });
|
|
84
92
|
return true;
|
|
85
93
|
}
|
|
86
94
|
catch {
|
|
@@ -98,13 +106,15 @@ async function persistWithRetry(store, operationId, patch, retryDelaysMs) {
|
|
|
98
106
|
* `{done: false, operationId}` — the row is already durable in both cases.
|
|
99
107
|
* @complexity O(1) plus one vendor round trip.
|
|
100
108
|
*/
|
|
101
|
-
export async function startOperation(
|
|
102
|
-
const
|
|
109
|
+
export async function startOperation(required, optional = {}) {
|
|
110
|
+
const deps = { ...required, ...optional };
|
|
111
|
+
const params = { ...required, ...optional };
|
|
112
|
+
const now = deps.clock ?? Date.now;
|
|
103
113
|
const newId = deps.newId ?? (() => `op_${Math.random().toString(36).slice(2, 10)}_${now()}`);
|
|
104
114
|
const startedAt = now();
|
|
105
115
|
const operationId = newId();
|
|
106
116
|
// Before any HTTP call — this ordering IS the crash-safety mechanism.
|
|
107
|
-
await deps.store.create({
|
|
117
|
+
await deps.store.create(...partitionArgs({
|
|
108
118
|
id: operationId,
|
|
109
119
|
providerId: params.providerId,
|
|
110
120
|
routeKey: params.routeKey,
|
|
@@ -112,7 +122,7 @@ export async function startOperation(deps, params) {
|
|
|
112
122
|
maxAttempts: params.maxAttempts ?? DEFAULT_MAX_ATTEMPTS,
|
|
113
123
|
deadlineAt: startedAt + (params.deadlineMs ?? DEFAULT_DEADLINE_MS),
|
|
114
124
|
nextPollAt: startedAt,
|
|
115
|
-
});
|
|
125
|
+
}, ["id", "providerId", "routeKey", "ownerRef", "maxAttempts", "deadlineAt"]));
|
|
116
126
|
const settle = (async () => {
|
|
117
127
|
const submitted = await submitAndParse(deps, params);
|
|
118
128
|
if (!submitted.ok) {
|
|
@@ -120,7 +130,7 @@ export async function startOperation(deps, params) {
|
|
|
120
130
|
// terminal write is safe. Best-effort even here: a write failure at this point must not
|
|
121
131
|
// reject `settle` (see the `finally` below) and leaves the row `'submitted'`, which is at
|
|
122
132
|
// least as safe as `'failed'` would have been.
|
|
123
|
-
await deps.store.update(operationId, { status: 'failed', error: toOperationError(submitted.error) }).catch(() => undefined);
|
|
133
|
+
await deps.store.update({ id: operationId, patch: { status: 'failed', error: toOperationError(submitted.error) } }).catch(() => undefined);
|
|
124
134
|
return { done: false, operationId };
|
|
125
135
|
}
|
|
126
136
|
const { outcome } = submitted;
|
|
@@ -169,14 +179,16 @@ export async function startOperation(deps, params) {
|
|
|
169
179
|
*
|
|
170
180
|
* @complexity O(k) vendor round trips for k claimed operations.
|
|
171
181
|
*/
|
|
172
|
-
export async function pollDueOperations(
|
|
173
|
-
const
|
|
174
|
-
const
|
|
182
|
+
export async function pollDueOperations(required, optional = {}) {
|
|
183
|
+
const deps = { ...required, ...optional };
|
|
184
|
+
const params = { ...required, ...optional };
|
|
185
|
+
const now = deps.clock ?? Date.now;
|
|
186
|
+
const claimed = await deps.store.claimDue(...partitionArgs({
|
|
175
187
|
now: now(),
|
|
176
188
|
leaseOwner: params.leaseOwner,
|
|
177
189
|
leaseMs: params.leaseMs,
|
|
178
190
|
...(params.limit !== undefined ? { limit: params.limit } : {}),
|
|
179
|
-
});
|
|
191
|
+
}, ["now", "leaseOwner", "leaseMs"]));
|
|
180
192
|
let completed = 0;
|
|
181
193
|
let failed = 0;
|
|
182
194
|
let pending = 0;
|
|
@@ -185,82 +197,82 @@ export async function pollDueOperations(deps, params) {
|
|
|
185
197
|
try {
|
|
186
198
|
const at = now();
|
|
187
199
|
if (row.deadlineAt <= at) {
|
|
188
|
-
await deps.store.update(row.id, {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
200
|
+
await deps.store.update({ id: row.id, patch: {
|
|
201
|
+
status: 'unknown',
|
|
202
|
+
error: {
|
|
203
|
+
message: 'async operation passed its absolute deadline — vendor-side effect is undetermined',
|
|
204
|
+
code: 'DEADLINE_EXPIRED',
|
|
205
|
+
},
|
|
206
|
+
} }, { options: { leaseOwner: params.leaseOwner } });
|
|
195
207
|
unknown += 1;
|
|
196
208
|
continue;
|
|
197
209
|
}
|
|
198
210
|
if (row.attempts >= row.maxAttempts) {
|
|
199
|
-
await deps.store.update(row.id, {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
211
|
+
await deps.store.update({ id: row.id, patch: {
|
|
212
|
+
status: 'failed',
|
|
213
|
+
error: {
|
|
214
|
+
message: `async operation exhausted its ${row.maxAttempts} poll attempts without a terminal answer`,
|
|
215
|
+
code: 'ATTEMPTS_EXHAUSTED',
|
|
216
|
+
},
|
|
217
|
+
} }, { options: { leaseOwner: params.leaseOwner } });
|
|
206
218
|
failed += 1;
|
|
207
219
|
continue;
|
|
208
220
|
}
|
|
209
|
-
const adapter = params.adapters(row.providerId, row.routeKey);
|
|
221
|
+
const adapter = params.adapters({ providerId: row.providerId, routeKey: row.routeKey });
|
|
210
222
|
if (!adapter) {
|
|
211
|
-
await deps.store.update(row.id, {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
223
|
+
await deps.store.update({ id: row.id, patch: {
|
|
224
|
+
status: 'failed',
|
|
225
|
+
error: { message: `no polling adapter registered for "${row.providerId}" / "${row.routeKey}"`, code: 'NO_ADAPTER' },
|
|
226
|
+
} }, { options: { leaseOwner: params.leaseOwner } });
|
|
215
227
|
failed += 1;
|
|
216
228
|
continue;
|
|
217
229
|
}
|
|
218
230
|
const ctx = params.resolveContext(row);
|
|
219
|
-
const request = adapter.buildPollRequest(row.state ?? {}, ctx);
|
|
231
|
+
const request = adapter.buildPollRequest({ state: row.state ?? {}, ctx: ctx });
|
|
220
232
|
// Credentials are resolved here, on this tick — never read from the row.
|
|
221
233
|
const resp = await performSigned(deps, request, FETCH_TIMEOUT_MS.QUICK);
|
|
222
|
-
const outcome = await adapter.parsePollResponse(resp, ctx, row.state ?? {});
|
|
234
|
+
const outcome = await adapter.parsePollResponse({ resp: resp, ctx: withRuntimeOutbound({ ctx, deps }), state: row.state ?? {} });
|
|
223
235
|
// Every write below is fenced on the lease this tick claimed: if it expired and another
|
|
224
236
|
// worker has since reclaimed (or finished) the row, this write must not clobber that newer
|
|
225
237
|
// outcome — see `AsyncOperationUpdateOptions`.
|
|
226
238
|
if (outcome.kind === 'complete') {
|
|
227
|
-
await deps.store.update(row.id, {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
239
|
+
await deps.store.update({ id: row.id, patch: {
|
|
240
|
+
status: 'succeeded',
|
|
241
|
+
attempts: row.attempts + 1,
|
|
242
|
+
result: resultToPayload(outcome.result),
|
|
243
|
+
} }, { options: { leaseOwner: params.leaseOwner } });
|
|
232
244
|
completed += 1;
|
|
233
245
|
}
|
|
234
246
|
else if (outcome.kind === 'failed') {
|
|
235
|
-
await deps.store.update(row.id, {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
247
|
+
await deps.store.update({ id: row.id, patch: {
|
|
248
|
+
status: 'failed',
|
|
249
|
+
attempts: row.attempts + 1,
|
|
250
|
+
error: { message: outcome.message, ...(outcome.code !== undefined ? { code: outcome.code } : {}) },
|
|
251
|
+
} }, { options: { leaseOwner: params.leaseOwner } });
|
|
240
252
|
failed += 1;
|
|
241
253
|
}
|
|
242
254
|
else {
|
|
243
|
-
await deps.store.update(row.id, {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
255
|
+
await deps.store.update({ id: row.id, patch: {
|
|
256
|
+
status: 'polling',
|
|
257
|
+
attempts: row.attempts + 1,
|
|
258
|
+
nextPollAt: at + (outcome.retryAfterMs ?? DEFAULT_POLL_INTERVAL_MS),
|
|
259
|
+
} }, { options: { leaseOwner: params.leaseOwner } });
|
|
248
260
|
pending += 1;
|
|
249
261
|
}
|
|
250
262
|
}
|
|
251
263
|
catch (error) {
|
|
252
264
|
// A transport failure is not a vendor verdict: keep the row pollable and let the attempt cap
|
|
253
265
|
// or the deadline end it, rather than reporting a definitive failure we cannot support.
|
|
254
|
-
await deps.store.update(row.id, {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
266
|
+
await deps.store.update({ id: row.id, patch: {
|
|
267
|
+
status: 'polling',
|
|
268
|
+
attempts: row.attempts + 1,
|
|
269
|
+
nextPollAt: now() + DEFAULT_POLL_INTERVAL_MS,
|
|
270
|
+
error: toOperationError(error),
|
|
271
|
+
} }, { options: { leaseOwner: params.leaseOwner } });
|
|
260
272
|
pending += 1;
|
|
261
273
|
}
|
|
262
274
|
finally {
|
|
263
|
-
await deps.store.releaseLease(row.id, params.leaseOwner);
|
|
275
|
+
await deps.store.releaseLease({ id: row.id, leaseOwner: params.leaseOwner });
|
|
264
276
|
}
|
|
265
277
|
}
|
|
266
278
|
return { claimed: claimed.length, completed, failed, pending, unknown };
|
|
@@ -269,6 +281,7 @@ export async function pollDueOperations(deps, params) {
|
|
|
269
281
|
* Boot-time recovery. Releases dead leases and expires blown deadlines via the store, then handles
|
|
270
282
|
* the one case the store cannot decide alone: a row still at `submitted`, meaning the process died
|
|
271
283
|
* between persisting the row and learning whether the vendor accepted the submit.
|
|
284
|
+
* Run before polling workers start: recovery enumerates through claims, not a concurrent read.
|
|
272
285
|
*
|
|
273
286
|
* Retry safety is gated on idempotency, not assumed. An adapter that has not declared
|
|
274
287
|
* `submitIsIdempotent` sends that row to `unknown` for reconciliation — media generation is billed
|
|
@@ -276,38 +289,42 @@ export async function pollDueOperations(deps, params) {
|
|
|
276
289
|
*
|
|
277
290
|
* @complexity O(n) in stored operations.
|
|
278
291
|
*/
|
|
279
|
-
export async function recoverAfterRestart(
|
|
280
|
-
const
|
|
292
|
+
export async function recoverAfterRestart(required, optional = {}) {
|
|
293
|
+
const deps = { ...required, ...optional };
|
|
294
|
+
const params = { ...required, ...optional };
|
|
295
|
+
const at = params.now ?? (deps.clock ?? Date.now)();
|
|
281
296
|
const reconciled = await deps.store.reconcileOnBoot({ now: at });
|
|
282
297
|
const resubmittable = [];
|
|
283
298
|
let unknownCrashGap = 0;
|
|
284
299
|
for (const row of await collectSubmitted(deps.store)) {
|
|
285
|
-
const adapter = params.adapters(row.providerId, row.routeKey);
|
|
300
|
+
const adapter = params.adapters({ providerId: row.providerId, routeKey: row.routeKey });
|
|
286
301
|
if (adapter?.submitIsIdempotent === true) {
|
|
287
302
|
resubmittable.push(row.id);
|
|
288
303
|
continue;
|
|
289
304
|
}
|
|
290
|
-
await deps.store.update(row.id, {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
305
|
+
await deps.store.update({ id: row.id, patch: {
|
|
306
|
+
status: 'unknown',
|
|
307
|
+
error: {
|
|
308
|
+
message: 'process died before the vendor submit was confirmed, and this adapter does not declare the submit idempotent — reconcile manually rather than risk a duplicate charge',
|
|
309
|
+
code: 'CRASH_GAP_NOT_IDEMPOTENT',
|
|
310
|
+
},
|
|
311
|
+
} });
|
|
297
312
|
unknownCrashGap += 1;
|
|
298
313
|
}
|
|
299
314
|
return { ...reconciled, unknownCrashGap, resubmittable };
|
|
300
315
|
}
|
|
301
316
|
/**
|
|
302
|
-
* The `submitted` rows a restart orphaned.
|
|
303
|
-
*
|
|
317
|
+
* The `submitted` rows a restart orphaned. The store has no claim-free, all-owner read path,
|
|
318
|
+
* so recovery enumerates non-terminal rows through zero-duration `claimDue` leases at the
|
|
319
|
+
* maximum time horizon and releases every claim, including rows already polling. This can
|
|
320
|
+
* supersede live leases; the host must run boot recovery before starting polling workers.
|
|
304
321
|
*/
|
|
305
322
|
async function collectSubmitted(store) {
|
|
306
323
|
const seen = new Map();
|
|
307
|
-
for (const row of await store.claimDue({ now: Number.MAX_SAFE_INTEGER, leaseOwner: RECOVERY_LEASE_OWNER, leaseMs: 0, limit: Number.MAX_SAFE_INTEGER })) {
|
|
324
|
+
for (const row of await store.claimDue(...partitionArgs({ now: Number.MAX_SAFE_INTEGER, leaseOwner: RECOVERY_LEASE_OWNER, leaseMs: 0, limit: Number.MAX_SAFE_INTEGER }, ["now", "leaseOwner", "leaseMs"]))) {
|
|
308
325
|
if (row.status === 'submitted')
|
|
309
326
|
seen.set(row.id, row);
|
|
310
|
-
await store.releaseLease(row.id, RECOVERY_LEASE_OWNER);
|
|
327
|
+
await store.releaseLease({ id: row.id, leaseOwner: RECOVERY_LEASE_OWNER });
|
|
311
328
|
}
|
|
312
329
|
return [...seen.values()];
|
|
313
330
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { HttpClientPort } from '@jini-ai/core/primitives';
|
|
2
|
+
import type { DnsResolver } from '@jini-ai/platform/http/guarded';
|
|
3
|
+
/** The host supplies a policy-enforcing client, never a raw fetch implementation. */
|
|
4
|
+
export interface MediaOutboundOptions {
|
|
5
|
+
/** Override caller-facing transport diagnostics without changing policy. */
|
|
6
|
+
readonly outboundMessages?: Partial<typeof defaultMediaOutboundMessages>;
|
|
7
|
+
readonly httpClient?: HttpClientPort;
|
|
8
|
+
/** Explicit local-development capability. Applies only to the default client. */
|
|
9
|
+
readonly allowPrivateNetwork?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare const defaultMediaOutboundMessages: {
|
|
12
|
+
unsupportedMethod: string;
|
|
13
|
+
unsupportedBody: string;
|
|
14
|
+
dispatcherRefused: string;
|
|
15
|
+
dnsResolutionFailed: string;
|
|
16
|
+
urlCredentialsBlocked: string;
|
|
17
|
+
};
|
|
18
|
+
/** Adapt the fetch-shaped vendor wire request without allowing it to override peer pinning.
|
|
19
|
+
* DNS and decoded-body work share the original timeout backstop. Redirects are refused,
|
|
20
|
+
* including for explicitly enabled private-network development clients.
|
|
21
|
+
* @throws Policy refusals, DNS/transport failures, unsupported bodies or methods.
|
|
22
|
+
* @complexity O(h + b) for h headers and b bounded decoded response bytes.
|
|
23
|
+
*/
|
|
24
|
+
export declare function fetchMediaOutbound({ url, timeoutMs }: {
|
|
25
|
+
url: string;
|
|
26
|
+
timeoutMs: number;
|
|
27
|
+
}, { init, httpClient, allowPrivateNetwork, dns, outboundMessages }?: MediaOutboundOptions & {
|
|
28
|
+
init?: RequestInit;
|
|
29
|
+
dns?: DnsResolver;
|
|
30
|
+
}): Promise<Response>;
|
|
31
|
+
//# sourceMappingURL=outbound.d.ts.map
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { FETCH_TIMEOUT_MS } from '@jini-ai/platform/fetch-with-timeout';
|
|
2
|
+
import { createHttpClient, createNodeGuardedHttpPorts, guardedFetch } from '@jini-ai/platform/http/guarded';
|
|
3
|
+
export const defaultMediaOutboundMessages = {
|
|
4
|
+
unsupportedMethod: 'Unsupported media HTTP method',
|
|
5
|
+
unsupportedBody: 'Media HTTP body must be a string or URLSearchParams',
|
|
6
|
+
dispatcherRefused: 'Configure media connection pooling on the guarded HTTP client',
|
|
7
|
+
dnsResolutionFailed: 'DNS resolution failed',
|
|
8
|
+
urlCredentialsBlocked: 'URL credentials blocked',
|
|
9
|
+
};
|
|
10
|
+
/** Adapt the fetch-shaped vendor wire request without allowing it to override peer pinning.
|
|
11
|
+
* DNS and decoded-body work share the original timeout backstop. Redirects are refused,
|
|
12
|
+
* including for explicitly enabled private-network development clients.
|
|
13
|
+
* @throws Policy refusals, DNS/transport failures, unsupported bodies or methods.
|
|
14
|
+
* @complexity O(h + b) for h headers and b bounded decoded response bytes.
|
|
15
|
+
*/
|
|
16
|
+
export async function fetchMediaOutbound({ url, timeoutMs }, { init = {}, httpClient, allowPrivateNetwork, dns, outboundMessages } = {}) {
|
|
17
|
+
const messages = { ...defaultMediaOutboundMessages, ...outboundMessages };
|
|
18
|
+
if (init.dispatcher !== undefined)
|
|
19
|
+
throw new TypeError(messages.dispatcherRefused);
|
|
20
|
+
const method = (init.method ?? 'GET').toUpperCase();
|
|
21
|
+
if (!isMediaMethod(method))
|
|
22
|
+
throw new TypeError(messages.unsupportedMethod);
|
|
23
|
+
const body = init.body;
|
|
24
|
+
if (body != null && typeof body !== 'string' && !(body instanceof URLSearchParams)) {
|
|
25
|
+
throw new TypeError(messages.unsupportedBody);
|
|
26
|
+
}
|
|
27
|
+
const client = httpClient ?? createHttpClient({
|
|
28
|
+
...createNodeGuardedHttpPorts({}),
|
|
29
|
+
...(dns === undefined ? {} : { dns }),
|
|
30
|
+
userAgent: 'media-provider',
|
|
31
|
+
policy: {
|
|
32
|
+
allowedSchemes: ['http', 'https'],
|
|
33
|
+
denyPrivateAddresses: allowPrivateNetwork !== true,
|
|
34
|
+
devHostAllowlist: [],
|
|
35
|
+
maxRedirects: 0,
|
|
36
|
+
connectTimeoutMs: FETCH_TIMEOUT_MS.GENERATE,
|
|
37
|
+
// Media can contain video; bound buffering at 96 MiB. Hosts may inject a stricter policy.
|
|
38
|
+
maxResponseBytes: 96 * 1024 * 1024,
|
|
39
|
+
maxDecompressedBytes: 96 * 1024 * 1024,
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
return guardedFetch({ client, url, timeoutMs }, {
|
|
43
|
+
method,
|
|
44
|
+
...(init.headers === undefined ? {} : { headers: init.headers }),
|
|
45
|
+
...(body == null ? {} : { body }),
|
|
46
|
+
...(init.signal == null ? {} : { signal: init.signal }),
|
|
47
|
+
totalDeadlineMs: timeoutMs,
|
|
48
|
+
redirect: 'error',
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function isMediaMethod(method) {
|
|
52
|
+
return method === 'GET' || method === 'HEAD' || method === 'POST' || method === 'PUT' || method === 'PATCH' || method === 'DELETE';
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=outbound.js.map
|
|
@@ -60,10 +60,23 @@ export interface PollingVendorAdapter<Meta = undefined> {
|
|
|
60
60
|
* money per call, so a retry must be *proven* safe, not assumed.
|
|
61
61
|
*/
|
|
62
62
|
readonly submitIsIdempotent?: boolean;
|
|
63
|
-
buildSubmitRequest(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
buildSubmitRequest(required: {
|
|
64
|
+
ctx: RenderContext;
|
|
65
|
+
}): UnsignedVendorRequest<Meta>;
|
|
66
|
+
parseSubmitResponse(required: {
|
|
67
|
+
resp: Response;
|
|
68
|
+
ctx: RenderContext;
|
|
69
|
+
request: UnsignedVendorRequest<Meta>;
|
|
70
|
+
}): Promise<SubmitOutcome>;
|
|
71
|
+
buildPollRequest(required: {
|
|
72
|
+
state: Readonly<Record<string, unknown>>;
|
|
73
|
+
ctx: RenderContext;
|
|
74
|
+
}): UnsignedVendorRequest<Meta>;
|
|
75
|
+
parsePollResponse(required: {
|
|
76
|
+
resp: Response;
|
|
77
|
+
ctx: RenderContext;
|
|
78
|
+
state: Readonly<Record<string, unknown>>;
|
|
79
|
+
}): Promise<PollOutcome>;
|
|
67
80
|
}
|
|
68
81
|
/**
|
|
69
82
|
* A `PollingVendorAdapter` with its `Meta` erased, for heterogeneous lookup by
|
|
@@ -81,7 +94,9 @@ export interface SignedVendorRequest {
|
|
|
81
94
|
* The broker seam: attaches auth to an unsigned request. Async because a real broker resolves from
|
|
82
95
|
* a vault / refreshes an OAuth token, and is called once per tick rather than once per operation.
|
|
83
96
|
*/
|
|
84
|
-
export type RequestSigner = (
|
|
97
|
+
export type RequestSigner = (required: {
|
|
98
|
+
request: UnsignedVendorRequest<unknown>;
|
|
99
|
+
}) => Promise<SignedVendorRequest> | SignedVendorRequest;
|
|
85
100
|
/**
|
|
86
101
|
* Reference signer for the `Authorization: Bearer <key>` scheme every vendor in this package uses.
|
|
87
102
|
*
|
|
@@ -97,7 +112,10 @@ export declare function createBearerSigner(required: {
|
|
|
97
112
|
missingCredentialMessage: string;
|
|
98
113
|
}): RequestSigner;
|
|
99
114
|
/** Carries a caller-supplied `dispatcher` onto an unsigned request, matching `withRequestInit`'s role on the sync tier. */
|
|
100
|
-
export declare function withUnsignedRequestInit(ctx: {
|
|
101
|
-
|
|
102
|
-
|
|
115
|
+
export declare function withUnsignedRequestInit({ ctx, init }: {
|
|
116
|
+
ctx: {
|
|
117
|
+
readonly requestInit: MediaGenerationRequestInit;
|
|
118
|
+
};
|
|
119
|
+
init: RequestInit;
|
|
120
|
+
}): RequestInit;
|
|
103
121
|
//# sourceMappingURL=polling-adapter.d.ts.map
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* @complexity O(1) plus whatever `resolve` costs.
|
|
10
10
|
*/
|
|
11
11
|
export function createBearerSigner(required) {
|
|
12
|
-
return async (request) => {
|
|
12
|
+
return async ({ request }) => {
|
|
13
13
|
const credentials = await required.resolve();
|
|
14
14
|
if (!credentials.apiKey) {
|
|
15
15
|
throw new Error(required.missingCredentialMessage);
|
|
@@ -24,7 +24,7 @@ export function createBearerSigner(required) {
|
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
26
|
/** Carries a caller-supplied `dispatcher` onto an unsigned request, matching `withRequestInit`'s role on the sync tier. */
|
|
27
|
-
export function withUnsignedRequestInit(ctx, init) {
|
|
27
|
+
export function withUnsignedRequestInit({ ctx, init }) {
|
|
28
28
|
return { ...init, ...(ctx.requestInit.dispatcher ? { dispatcher: ctx.requestInit.dispatcher } : {}) };
|
|
29
29
|
}
|
|
30
30
|
//# sourceMappingURL=polling-adapter.js.map
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Shared plumbing for the AIHubMix aggregator gateway (an OpenAI-wire-
|
|
3
3
|
* compatible aggregator: one API key fronts OpenAI/Anthropic/Gemini
|
|
4
4
|
* models, routed by model name). Ported near-verbatim from Open Design's
|
|
5
|
-
* `apps/daemon/src/integrations/aihubmix.ts` — see `
|
|
5
|
+
* `apps/daemon/src/integrations/aihubmix.ts` — see `archived provenance ledger`. Only
|
|
6
6
|
* the pieces `providers/aihubmix.ts`'s image/TTS renderers need are
|
|
7
7
|
* ported; see "Not ported" below for what's deliberately left out.
|
|
8
8
|
*
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
* covered with a contrived test that fakes an empty app code.
|
|
24
24
|
*
|
|
25
25
|
* Not ported: `aihubmixVideoSeconds` (video is deferred — see
|
|
26
|
-
* `
|
|
26
|
+
* `archived provenance ledger`'s async-polling bucket), `aihubmixCatalogUrl`/
|
|
27
27
|
* `parseAIHubMixCatalog`/`AIHubMixCatalogModel` (a model-catalogue-
|
|
28
28
|
* discovery HTTP call, not part of the render path), and
|
|
29
29
|
* `AIHUBMIX_IMAGE_ASPECT_TO_SIZE` (used by OD's in-chat `generate_image`
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Shared plumbing for the AIHubMix aggregator gateway (an OpenAI-wire-
|
|
3
3
|
* compatible aggregator: one API key fronts OpenAI/Anthropic/Gemini
|
|
4
4
|
* models, routed by model name). Ported near-verbatim from Open Design's
|
|
5
|
-
* `apps/daemon/src/integrations/aihubmix.ts` — see `
|
|
5
|
+
* `apps/daemon/src/integrations/aihubmix.ts` — see `archived provenance ledger`. Only
|
|
6
6
|
* the pieces `providers/aihubmix.ts`'s image/TTS renderers need are
|
|
7
7
|
* ported; see "Not ported" below for what's deliberately left out.
|
|
8
8
|
*
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
* covered with a contrived test that fakes an empty app code.
|
|
24
24
|
*
|
|
25
25
|
* Not ported: `aihubmixVideoSeconds` (video is deferred — see
|
|
26
|
-
* `
|
|
26
|
+
* `archived provenance ledger`'s async-polling bucket), `aihubmixCatalogUrl`/
|
|
27
27
|
* `parseAIHubMixCatalog`/`AIHubMixCatalogModel` (a model-catalogue-
|
|
28
28
|
* discovery HTTP call, not part of the render path), and
|
|
29
29
|
* `AIHUBMIX_IMAGE_ASPECT_TO_SIZE` (used by OD's in-chat `generate_image`
|
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
import type { ProviderCredentials, RenderContext, RenderResult } from '../types.js';
|
|
2
|
-
|
|
3
|
-
export declare function
|
|
2
|
+
import type { VendorAdapterRegistry } from '../vendor-registry.js';
|
|
3
|
+
export declare function renderAIHubMixImage({ ctx, credentials }: {
|
|
4
|
+
ctx: RenderContext;
|
|
5
|
+
credentials: ProviderCredentials;
|
|
6
|
+
}): Promise<RenderResult>;
|
|
7
|
+
export declare function renderAIHubMixTTS({ ctx, credentials }: {
|
|
8
|
+
ctx: RenderContext;
|
|
9
|
+
credentials: ProviderCredentials;
|
|
10
|
+
}): Promise<RenderResult>;
|
|
11
|
+
/** Explicit composition: importing a renderer never changes another module's registry. */
|
|
12
|
+
export declare function registerVendorAdapters({ registry }: {
|
|
13
|
+
registry: Pick<VendorAdapterRegistry, 'register'>;
|
|
14
|
+
}, _optional?: Record<string, never>): void;
|
|
4
15
|
//# sourceMappingURL=aihubmix.d.ts.map
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* catalog ids, and `renderAIHubMixTTS` (`POST /audio/speech`, OpenAI
|
|
6
6
|
* shape). Ported near-verbatim from Open Design's
|
|
7
7
|
* `apps/daemon/src/media/index.ts` `renderAIHubMixImage`/
|
|
8
|
-
* `renderAIHubMixGeminiImage`/`renderAIHubMixTTS` — see `
|
|
8
|
+
* `renderAIHubMixGeminiImage`/`renderAIHubMixTTS` — see `archived provenance ledger`.
|
|
9
9
|
*
|
|
10
10
|
* The non-Gemini image path and the TTS path route through
|
|
11
11
|
* `openai-compatible.ts`'s shared helpers since AIHubMix's default wire
|
|
@@ -57,12 +57,11 @@ import { buildOpenAIImageUrl, buildOpenAISpeechUrl, openaiSizeFor, OPENAI_TTS_VO
|
|
|
57
57
|
import { createRawBytesParser } from '../response-parsers.js';
|
|
58
58
|
import { assertAndFetchExternalAsset } from '../ssrf-guard.js';
|
|
59
59
|
import { dispatchVendorRequest, requireApiKey } from '../vendor-adapter.js';
|
|
60
|
-
import { mediaVendorRegistry } from '../vendor-registry.js';
|
|
61
60
|
import { aihubmixAppCodeHeader, aihubmixGeminiImageUrl, aihubmixHeaders, AIHUBMIX_DEFAULT_BASE_URL, aihubmixWireModel, classifyAIHubMixModel } from './aihubmix-shared.js';
|
|
62
61
|
const NO_CREDENTIAL_MESSAGE = 'no AIHubMix credential — configure an API key or set AIHUBMIX_API_KEY.';
|
|
63
62
|
const aihubmixImageAdapter = {
|
|
64
|
-
requireCredential: requireApiKey(NO_CREDENTIAL_MESSAGE),
|
|
65
|
-
buildRequest(ctx, credentials) {
|
|
63
|
+
requireCredential: requireApiKey({ message: NO_CREDENTIAL_MESSAGE }),
|
|
64
|
+
buildRequest({ ctx, credentials }) {
|
|
66
65
|
const apiKey = credentials.apiKey; // requireCredential already validated this.
|
|
67
66
|
const baseUrl = credentials.baseUrl || AIHUBMIX_DEFAULT_BASE_URL;
|
|
68
67
|
const wireModel = aihubmixWireModel(credentials.model || ctx.wireModel);
|
|
@@ -112,7 +111,7 @@ const aihubmixImageAdapter = {
|
|
|
112
111
|
meta: { kind: 'openai', wireModel },
|
|
113
112
|
};
|
|
114
113
|
},
|
|
115
|
-
async parseResponse(resp, ctx, request) {
|
|
114
|
+
async parseResponse({ resp, ctx, request }) {
|
|
116
115
|
if (request.meta.kind === 'gemini') {
|
|
117
116
|
const { wireModel, aspect } = request.meta;
|
|
118
117
|
if (!resp.ok) {
|
|
@@ -152,7 +151,7 @@ const aihubmixImageAdapter = {
|
|
|
152
151
|
bytes = Buffer.from(entry.b64_json, 'base64');
|
|
153
152
|
}
|
|
154
153
|
else if (entry.url) {
|
|
155
|
-
const imgResp = await assertAndFetchExternalAsset(entry.url, withRequestInit(ctx));
|
|
154
|
+
const imgResp = await assertAndFetchExternalAsset({ url: entry.url }, { ...ctx, init: withRequestInit(ctx) });
|
|
156
155
|
if (!imgResp.ok)
|
|
157
156
|
throw new Error(`aihubmix image fetch ${imgResp.status}`);
|
|
158
157
|
bytes = Buffer.from(await imgResp.arrayBuffer());
|
|
@@ -167,13 +166,12 @@ const aihubmixImageAdapter = {
|
|
|
167
166
|
};
|
|
168
167
|
},
|
|
169
168
|
};
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
return dispatchVendorRequest(aihubmixImageAdapter, ctx, credentials);
|
|
169
|
+
export async function renderAIHubMixImage({ ctx, credentials }) {
|
|
170
|
+
return dispatchVendorRequest({ adapter: aihubmixImageAdapter, ctx: ctx, credentials: credentials });
|
|
173
171
|
}
|
|
174
172
|
const aihubmixTTSAdapter = {
|
|
175
|
-
requireCredential: requireApiKey(NO_CREDENTIAL_MESSAGE),
|
|
176
|
-
buildRequest(ctx, credentials) {
|
|
173
|
+
requireCredential: requireApiKey({ message: NO_CREDENTIAL_MESSAGE }),
|
|
174
|
+
buildRequest({ ctx, credentials }) {
|
|
177
175
|
const apiKey = credentials.apiKey; // requireCredential already validated this.
|
|
178
176
|
const baseUrl = credentials.baseUrl || AIHUBMIX_DEFAULT_BASE_URL;
|
|
179
177
|
const wireModel = aihubmixWireModel(credentials.model || ctx.wireModel);
|
|
@@ -194,12 +192,16 @@ const aihubmixTTSAdapter = {
|
|
|
194
192
|
parseResponse: createRawBytesParser({
|
|
195
193
|
errorTag: 'aihubmix speech',
|
|
196
194
|
zeroBytesMessage: 'aihubmix speech returned zero bytes',
|
|
197
|
-
note: (bytes, meta) => `aihubmix/${meta.wireModel} · ${meta.voice} · ${meta.format} · ${bytes.length} bytes`,
|
|
198
|
-
suggestedExt: (_bytes, meta) => (meta.format === 'opus' ? '.ogg' : `.${meta.format}`),
|
|
195
|
+
note: ({ bytes, meta }) => `aihubmix/${meta.wireModel} · ${meta.voice} · ${meta.format} · ${bytes.length} bytes`,
|
|
196
|
+
suggestedExt: ({ bytes: _bytes, meta }) => (meta.format === 'opus' ? '.ogg' : `.${meta.format}`),
|
|
199
197
|
}),
|
|
200
198
|
};
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
199
|
+
export async function renderAIHubMixTTS({ ctx, credentials }) {
|
|
200
|
+
return dispatchVendorRequest({ adapter: aihubmixTTSAdapter, ctx: ctx, credentials: credentials });
|
|
201
|
+
}
|
|
202
|
+
/** Explicit composition: importing a renderer never changes another module's registry. */
|
|
203
|
+
export function registerVendorAdapters({ registry }, _optional = {}) {
|
|
204
|
+
registry.register({ providerId: 'aihubmix', routeKey: 'image', adapter: aihubmixImageAdapter });
|
|
205
|
+
registry.register({ providerId: 'aihubmix', routeKey: 'audio:speech', adapter: aihubmixTTSAdapter });
|
|
204
206
|
}
|
|
205
207
|
//# sourceMappingURL=aihubmix.js.map
|
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
import type { ProviderCredentials, RenderContext, RenderResult } from '../types.js';
|
|
2
|
+
import type { VendorAdapterRegistry } from '../vendor-registry.js';
|
|
2
3
|
export declare const CUSTOM_IMAGE_MODEL_ID = "custom-image";
|
|
3
|
-
export declare function renderCustomOpenAIImage(ctx
|
|
4
|
+
export declare function renderCustomOpenAIImage({ ctx, credentials }: {
|
|
5
|
+
ctx: RenderContext;
|
|
6
|
+
credentials: ProviderCredentials;
|
|
7
|
+
}): Promise<RenderResult>;
|
|
4
8
|
/** Whether the caller's `custom-image` credentials should override an `openai`-provider request (same model configured on both). */
|
|
5
|
-
export declare function customImageOverridesOpenAIModel(
|
|
9
|
+
export declare function customImageOverridesOpenAIModel(required: {
|
|
10
|
+
ctx: RenderContext;
|
|
11
|
+
credentials: ProviderCredentials | null;
|
|
12
|
+
}): required is {
|
|
13
|
+
ctx: RenderContext;
|
|
14
|
+
credentials: ProviderCredentials;
|
|
15
|
+
};
|
|
16
|
+
/** Explicit composition: importing a renderer never changes another module's registry. */
|
|
17
|
+
export declare function registerVendorAdapters({ registry }: {
|
|
18
|
+
registry: Pick<VendorAdapterRegistry, 'register'>;
|
|
19
|
+
}, _optional?: Record<string, never>): void;
|
|
6
20
|
//# sourceMappingURL=custom-image.d.ts.map
|