@jini-ai/integrations 0.4.2 → 0.6.10

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.
Files changed (132) hide show
  1. package/CHANGELOG.md +99 -1
  2. package/README.md +1 -0
  3. package/dist/api-connections/access-token.d.ts +9 -0
  4. package/dist/api-connections/access-token.js +92 -0
  5. package/dist/api-connections/account-actions.d.ts +40 -0
  6. package/dist/api-connections/account-actions.js +110 -0
  7. package/dist/api-connections/attempt-state.d.ts +28 -0
  8. package/dist/api-connections/attempt-state.js +73 -0
  9. package/dist/api-connections/authorization.d.ts +45 -0
  10. package/dist/api-connections/authorization.js +120 -0
  11. package/dist/api-connections/deps.d.ts +41 -0
  12. package/dist/api-connections/deps.js +13 -0
  13. package/dist/api-connections/descriptor.d.ts +6 -0
  14. package/dist/api-connections/descriptor.js +111 -0
  15. package/dist/api-connections/errors.d.ts +23 -0
  16. package/dist/api-connections/errors.js +24 -0
  17. package/dist/api-connections/finish-authorization.d.ts +24 -0
  18. package/dist/api-connections/finish-authorization.js +117 -0
  19. package/dist/api-connections/index.d.ts +18 -0
  20. package/dist/api-connections/index.js +10 -0
  21. package/dist/api-connections/install-credential.d.ts +19 -0
  22. package/dist/api-connections/install-credential.js +12 -0
  23. package/dist/api-connections/keys.d.ts +27 -0
  24. package/dist/api-connections/keys.js +22 -0
  25. package/dist/api-connections/ports.d.ts +73 -0
  26. package/dist/api-connections/ports.js +2 -0
  27. package/dist/api-connections/records.d.ts +27 -0
  28. package/dist/api-connections/records.js +52 -0
  29. package/dist/api-connections/request-checks.d.ts +18 -0
  30. package/dist/api-connections/request-checks.js +67 -0
  31. package/dist/api-connections/service.d.ts +25 -0
  32. package/dist/api-connections/service.js +26 -0
  33. package/dist/api-connections/status.d.ts +28 -0
  34. package/dist/api-connections/status.js +36 -0
  35. package/dist/api-connections/token-store/errors.d.ts +20 -0
  36. package/dist/api-connections/token-store/errors.js +38 -0
  37. package/dist/api-connections/token-store/index.d.ts +6 -0
  38. package/dist/api-connections/token-store/index.js +6 -0
  39. package/dist/api-connections/token-store/record.d.ts +69 -0
  40. package/dist/api-connections/token-store/record.js +97 -0
  41. package/dist/api-connections/token-store/vault-token-store.d.ts +26 -0
  42. package/dist/api-connections/token-store/vault-token-store.js +215 -0
  43. package/dist/api-connections/types.d.ts +101 -0
  44. package/dist/api-connections/types.js +9 -0
  45. package/dist/credentialed-http/auth-schemes.js +8 -4
  46. package/dist/credentialed-http/credentialed-request.d.ts +4 -0
  47. package/dist/credentialed-http/credentialed-request.js +61 -77
  48. package/dist/credentialed-http/response-redaction.d.ts +15 -0
  49. package/dist/credentialed-http/response-redaction.js +82 -0
  50. package/dist/media-providers/catalog.d.ts +3 -1
  51. package/dist/media-providers/catalog.js +3 -1
  52. package/dist/media-providers/dispatch/async-operation-store.d.ts +31 -0
  53. package/dist/media-providers/dispatch/async-operation-store.js +75 -37
  54. package/dist/media-providers/dispatch/builtin-vendors.d.ts +1 -1
  55. package/dist/media-providers/dispatch/context.js +10 -2
  56. package/dist/media-providers/dispatch/engine.d.ts +6 -3
  57. package/dist/media-providers/dispatch/engine.js +99 -109
  58. package/dist/media-providers/dispatch/index.d.ts +4 -2
  59. package/dist/media-providers/dispatch/index.js +3 -1
  60. package/dist/media-providers/dispatch/media-provider-registry.d.ts +62 -0
  61. package/dist/media-providers/dispatch/media-provider-registry.js +141 -0
  62. package/dist/media-providers/dispatch/node.d.ts +13 -0
  63. package/dist/media-providers/dispatch/node.js +24 -0
  64. package/dist/media-providers/dispatch/openai-compatible.d.ts +1 -1
  65. package/dist/media-providers/dispatch/openai-compatible.js +90 -87
  66. package/dist/media-providers/dispatch/operation-runtime.js +116 -89
  67. package/dist/media-providers/dispatch/outbound.d.ts +8 -4
  68. package/dist/media-providers/dispatch/outbound.js +27 -26
  69. package/dist/media-providers/dispatch/providers/aihubmix.d.ts +1 -1
  70. package/dist/media-providers/dispatch/providers/aihubmix.js +61 -56
  71. package/dist/media-providers/dispatch/providers/custom-image.d.ts +1 -1
  72. package/dist/media-providers/dispatch/providers/elevenlabs.d.ts +1 -1
  73. package/dist/media-providers/dispatch/providers/fishaudio.d.ts +1 -1
  74. package/dist/media-providers/dispatch/providers/grok.d.ts +1 -1
  75. package/dist/media-providers/dispatch/providers/imagerouter-video-async.d.ts +1 -1
  76. package/dist/media-providers/dispatch/providers/imagerouter-video-async.js +53 -49
  77. package/dist/media-providers/dispatch/providers/imagerouter.d.ts +1 -1
  78. package/dist/media-providers/dispatch/providers/imagerouter.js +11 -24
  79. package/dist/media-providers/dispatch/providers/minimax.d.ts +1 -1
  80. package/dist/media-providers/dispatch/providers/nanobanana.d.ts +1 -1
  81. package/dist/media-providers/dispatch/providers/nanobanana.js +22 -30
  82. package/dist/media-providers/dispatch/providers/openai.d.ts +1 -1
  83. package/dist/media-providers/dispatch/providers/openai.js +51 -47
  84. package/dist/media-providers/dispatch/providers/openrouter.d.ts +1 -1
  85. package/dist/media-providers/dispatch/providers/openrouter.js +52 -37
  86. package/dist/media-providers/dispatch/providers/senseaudio.d.ts +1 -1
  87. package/dist/media-providers/dispatch/providers/senseaudio.js +47 -28
  88. package/dist/media-providers/dispatch/providers/volcengine.d.ts +1 -1
  89. package/dist/media-providers/dispatch/response-parsers.js +37 -21
  90. package/dist/media-providers/dispatch/sqlite-async-operation-store.js +9 -29
  91. package/dist/media-providers/dispatch/ssrf-guard.d.ts +3 -2
  92. package/dist/media-providers/dispatch/ssrf-guard.js +6 -5
  93. package/dist/media-providers/dispatch/types.d.ts +76 -2
  94. package/dist/media-providers/dispatch/vendor-adapter.d.ts +2 -43
  95. package/dist/media-providers/dispatch/vendor-adapter.js +2 -2
  96. package/dist/media-providers/dispatch/vendor-registry-core.d.ts +24 -0
  97. package/dist/media-providers/dispatch/vendor-registry-core.js +34 -0
  98. package/dist/media-providers/dispatch/vendor-registry.d.ts +10 -36
  99. package/dist/media-providers/dispatch/vendor-registry.js +14 -42
  100. package/dist/media-providers/policy.js +25 -17
  101. package/dist/media-providers/providers.d.ts +5 -0
  102. package/dist/media-providers/providers.js +2 -0
  103. package/dist/media-providers/sqlite-task-store.js +12 -80
  104. package/dist/media-providers/staging.js +77 -58
  105. package/dist/media-providers/task-lifecycle.d.ts +33 -0
  106. package/dist/media-providers/task-lifecycle.js +124 -0
  107. package/dist/media-providers/task-store.js +8 -86
  108. package/dist/media-providers/ui-defaults.d.ts +10 -0
  109. package/dist/media-providers/ui-defaults.js +167 -0
  110. package/dist/media-providers/video-request.js +112 -90
  111. package/dist/saved-requests/definition.d.ts +17 -0
  112. package/dist/saved-requests/definition.js +84 -0
  113. package/dist/saved-requests/execute.d.ts +17 -0
  114. package/dist/saved-requests/execute.js +95 -0
  115. package/dist/saved-requests/index.d.ts +6 -0
  116. package/dist/saved-requests/index.js +4 -0
  117. package/dist/saved-requests/projection.d.ts +9 -0
  118. package/dist/saved-requests/projection.js +48 -0
  119. package/dist/saved-requests/schema.d.ts +7 -0
  120. package/dist/saved-requests/schema.js +73 -0
  121. package/dist/saved-requests/types.d.ts +58 -0
  122. package/dist/saved-requests/types.js +2 -0
  123. package/dist/saved-requests/validation.d.ts +46 -0
  124. package/dist/saved-requests/validation.js +66 -0
  125. package/dist/webhooks/delivery.d.ts +14 -0
  126. package/dist/webhooks/delivery.js +41 -10
  127. package/dist/webhooks/index.d.ts +1 -0
  128. package/dist/webhooks/index.js +1 -0
  129. package/dist/webhooks/ports.d.ts +23 -0
  130. package/dist/webhooks/worker.d.ts +83 -0
  131. package/dist/webhooks/worker.js +105 -0
  132. package/package.json +46 -9
@@ -58,6 +58,7 @@ import { createRawBytesParser } from '../response-parsers.js';
58
58
  import { assertAndFetchExternalAsset } from '../ssrf-guard.js';
59
59
  import { dispatchVendorRequest, requireApiKey } from '../vendor-adapter.js';
60
60
  import { aihubmixAppCodeHeader, aihubmixGeminiImageUrl, aihubmixHeaders, AIHUBMIX_DEFAULT_BASE_URL, aihubmixWireModel, classifyAIHubMixModel } from './aihubmix-shared.js';
61
+ const DEFAULT_IMAGE_PROMPT = 'A high-quality reference image.';
61
62
  const NO_CREDENTIAL_MESSAGE = 'no AIHubMix credential — configure an API key or set AIHUBMIX_API_KEY.';
62
63
  const aihubmixImageAdapter = {
63
64
  requireCredential: requireApiKey({ message: NO_CREDENTIAL_MESSAGE }),
@@ -78,7 +79,7 @@ const aihubmixImageAdapter = {
78
79
  ...aihubmixAppCodeHeader(),
79
80
  },
80
81
  body: JSON.stringify({
81
- contents: [{ role: 'user', parts: [{ text: ctx.prompt || 'A high-quality reference image.' }] }],
82
+ contents: [{ role: 'user', parts: [{ text: ctx.prompt || DEFAULT_IMAGE_PROMPT }] }],
82
83
  generationConfig: {
83
84
  responseModalities: ['TEXT', 'IMAGE'],
84
85
  imageConfig: { aspectRatio: aspect },
@@ -90,7 +91,7 @@ const aihubmixImageAdapter = {
90
91
  }
91
92
  const body = {
92
93
  model: wireModel,
93
- prompt: ctx.prompt || 'A high-quality reference image.',
94
+ prompt: ctx.prompt || DEFAULT_IMAGE_PROMPT,
94
95
  n: 1,
95
96
  size: openaiSizeFor(wireModel, ctx.aspect),
96
97
  };
@@ -112,62 +113,66 @@ const aihubmixImageAdapter = {
112
113
  };
113
114
  },
114
115
  async parseResponse({ resp, ctx, request }) {
115
- if (request.meta.kind === 'gemini') {
116
- const { wireModel, aspect } = request.meta;
117
- if (!resp.ok) {
118
- const text = await resp.text().catch(() => '');
119
- throw new Error(`aihubmix image (gemini) ${resp.status}: ${text.slice(0, 240)}`);
120
- }
121
- const data = (await resp.json());
122
- const parts = data.candidates?.[0]?.content?.parts ?? [];
123
- const b64 = parts.map((p) => p.inlineData?.data ?? p.inline_data?.data).find((d) => typeof d === 'string' && d.length > 0);
124
- if (!b64) {
125
- throw new Error(`aihubmix gemini image response had no inline image data: ${JSON.stringify(data).slice(0, 200)}`);
126
- }
127
- const bytes = Buffer.from(b64, 'base64');
128
- return {
129
- bytes,
130
- providerNote: `aihubmix/${wireModel} · ${aspect} · ${bytes.length} bytes (gemini-native)`,
131
- suggestedExt: '.png',
132
- };
133
- }
134
- const { wireModel } = request.meta;
135
- const text = await resp.text();
136
- if (!resp.ok) {
137
- throw new Error(`aihubmix ${resp.status}: ${truncate(text, 240)}`);
138
- }
139
- let data;
140
- try {
141
- data = JSON.parse(text);
142
- }
143
- catch {
144
- throw new Error(`aihubmix non-JSON response: ${truncate(text, 200)}`);
145
- }
146
- const entry = Array.isArray(data.data) ? data.data[0] : undefined;
147
- if (!entry)
148
- throw new Error('aihubmix response had no data[0]');
149
- let bytes;
150
- if (entry.b64_json) {
151
- bytes = Buffer.from(entry.b64_json, 'base64');
152
- }
153
- else if (entry.url) {
154
- const imgResp = await assertAndFetchExternalAsset({ url: entry.url }, { ...ctx, init: withRequestInit(ctx) });
155
- if (!imgResp.ok)
156
- throw new Error(`aihubmix image fetch ${imgResp.status}`);
157
- bytes = Buffer.from(await imgResp.arrayBuffer());
158
- }
159
- else {
160
- throw new Error('aihubmix response had neither b64_json nor url');
161
- }
162
- return {
163
- bytes,
164
- providerNote: `aihubmix/${wireModel} · ${ctx.aspect} · ${bytes.length} bytes`,
165
- suggestedExt: '.png',
166
- };
116
+ const { meta } = request;
117
+ return meta.kind === 'gemini' ? parseGeminiImageResponse({ resp, meta }) : parseOpenAIImageResponse({ resp, ctx, meta });
167
118
  },
168
119
  };
120
+ /** First non-empty base64 string across the parts; camelCase `inlineData` wins over `inline_data` within one part. */
121
+ function firstInlineImageData(parts) {
122
+ return parts.map((p) => p.inlineData?.data ?? p.inline_data?.data).find((d) => typeof d === 'string' && d.length > 0);
123
+ }
124
+ async function parseGeminiImageResponse({ resp, meta }) {
125
+ if (!resp.ok) {
126
+ const text = await resp.text().catch(() => '');
127
+ throw new Error(`aihubmix image (gemini) ${resp.status}: ${text.slice(0, 240)}`);
128
+ }
129
+ const data = (await resp.json());
130
+ const b64 = firstInlineImageData(data.candidates?.[0]?.content?.parts ?? []);
131
+ if (!b64) {
132
+ throw new Error(`aihubmix gemini image response had no inline image data: ${JSON.stringify(data).slice(0, 200)}`);
133
+ }
134
+ const bytes = Buffer.from(b64, 'base64');
135
+ return {
136
+ bytes,
137
+ providerNote: `aihubmix/${meta.wireModel} · ${meta.aspect} · ${bytes.length} bytes (gemini-native)`,
138
+ suggestedExt: '.png',
139
+ };
140
+ }
141
+ async function parseOpenAIImageResponse({ resp, ctx, meta }) {
142
+ const text = await resp.text();
143
+ if (!resp.ok) {
144
+ throw new Error(`aihubmix ${resp.status}: ${truncate(text, 240)}`);
145
+ }
146
+ let data;
147
+ try {
148
+ data = JSON.parse(text);
149
+ }
150
+ catch {
151
+ throw new Error(`aihubmix non-JSON response: ${truncate(text, 200)}`);
152
+ }
153
+ const entry = Array.isArray(data.data) ? data.data[0] : undefined;
154
+ if (!entry)
155
+ throw new Error('aihubmix response had no data[0]');
156
+ const bytes = await openAIEntryBytes({ entry, ctx });
157
+ return {
158
+ bytes,
159
+ providerNote: `aihubmix/${meta.wireModel} · ${ctx.aspect} · ${bytes.length} bytes`,
160
+ suggestedExt: '.png',
161
+ };
162
+ }
163
+ /** Inline `b64_json` first; otherwise the `url`, downloaded through the SSRF guard (it is attacker-controllable). */
164
+ async function openAIEntryBytes({ entry, ctx }) {
165
+ if (entry.b64_json)
166
+ return Buffer.from(entry.b64_json, 'base64');
167
+ if (!entry.url)
168
+ throw new Error('aihubmix response had neither b64_json nor url');
169
+ const imgResp = await assertAndFetchExternalAsset({ url: entry.url }, { ...ctx, init: withRequestInit(ctx) });
170
+ if (!imgResp.ok)
171
+ throw new Error(`aihubmix image fetch ${imgResp.status}`);
172
+ return Buffer.from(await imgResp.arrayBuffer());
173
+ }
169
174
  export async function renderAIHubMixImage({ ctx, credentials }) {
170
- return dispatchVendorRequest({ adapter: aihubmixImageAdapter, ctx: ctx, credentials: credentials });
175
+ return dispatchVendorRequest({ adapter: aihubmixImageAdapter, ctx, credentials });
171
176
  }
172
177
  const aihubmixTTSAdapter = {
173
178
  requireCredential: requireApiKey({ message: NO_CREDENTIAL_MESSAGE }),
@@ -197,7 +202,7 @@ const aihubmixTTSAdapter = {
197
202
  }),
198
203
  };
199
204
  export async function renderAIHubMixTTS({ ctx, credentials }) {
200
- return dispatchVendorRequest({ adapter: aihubmixTTSAdapter, ctx: ctx, credentials: credentials });
205
+ return dispatchVendorRequest({ adapter: aihubmixTTSAdapter, ctx, credentials });
201
206
  }
202
207
  /** Explicit composition: importing a renderer never changes another module's registry. */
203
208
  export function registerVendorAdapters({ registry }, _optional = {}) {
@@ -1,5 +1,5 @@
1
1
  import type { ProviderCredentials, RenderContext, RenderResult } from '../types.js';
2
- import type { VendorAdapterRegistry } from '../vendor-registry.js';
2
+ import type { VendorAdapterRegistry } from '../vendor-registry-core.js';
3
3
  export declare const CUSTOM_IMAGE_MODEL_ID = "custom-image";
4
4
  export declare function renderCustomOpenAIImage({ ctx, credentials }: {
5
5
  ctx: RenderContext;
@@ -1,5 +1,5 @@
1
1
  import type { ProviderCredentials, RenderContext, RenderResult } from '../types.js';
2
- import type { VendorAdapterRegistry } from '../vendor-registry.js';
2
+ import type { VendorAdapterRegistry } from '../vendor-registry-core.js';
3
3
  export declare function renderElevenLabsTTS({ ctx, credentials }: {
4
4
  ctx: RenderContext;
5
5
  credentials: ProviderCredentials;
@@ -1,5 +1,5 @@
1
1
  import type { ProviderCredentials, RenderContext, RenderResult } from '../types.js';
2
- import type { VendorAdapterRegistry } from '../vendor-registry.js';
2
+ import type { VendorAdapterRegistry } from '../vendor-registry-core.js';
3
3
  export declare function renderFishAudioTTS({ ctx, credentials }: {
4
4
  ctx: RenderContext;
5
5
  credentials: ProviderCredentials;
@@ -1,5 +1,5 @@
1
1
  import type { ProviderCredentials, RenderContext, RenderResult } from '../types.js';
2
- import type { VendorAdapterRegistry } from '../vendor-registry.js';
2
+ import type { VendorAdapterRegistry } from '../vendor-registry-core.js';
3
3
  /**
4
4
  * xAI's Imagine API accepts a wide list of aspect ratios (1:1, 16:9, 9:16,
5
5
  * 4:3, 3:4, 3:2, 2:3, 2:1, 1:2, 19.5:9, 9:19.5, 20:9, 9:20, auto) —
@@ -17,5 +17,5 @@ export interface ImageRouterVideoMeta {
17
17
  * context's own `wireModel`.
18
18
  * @complexity O(1) per built request; one vendor round trip per parse.
19
19
  */
20
- export declare function createImageRouterVideoPollingAdapter(_required: Record<string, never>, optional?: ImageRouterVideoConfig): PollingVendorAdapter<ImageRouterVideoMeta>;
20
+ export declare function createImageRouterVideoPollingAdapter(_required: Record<string, never>, config?: ImageRouterVideoConfig): PollingVendorAdapter<ImageRouterVideoMeta>;
21
21
  //# sourceMappingURL=imagerouter-video-async.d.ts.map
@@ -45,6 +45,40 @@ function retryAfterMsFrom(resp) {
45
45
  const seconds = Number(raw);
46
46
  return Number.isFinite(seconds) && seconds > 0 ? seconds * 1_000 : undefined;
47
47
  }
48
+ /** The wire model, size and length one request is for — derived from the context, so submit and poll agree. */
49
+ function videoMetaFor({ ctx, config }) {
50
+ return {
51
+ wireModel: (config.wireModel || ctx.wireModel).trim(),
52
+ size: imageRouterSizeFor({ aspect: ctx.aspect, surface: 'video' }),
53
+ seconds: typeof ctx.length === 'number' ? ctx.length : 'auto',
54
+ };
55
+ }
56
+ /** Decodes an OpenAI-shaped `{ data: [...] }` body into the completed render result. */
57
+ async function completedVideo({ data, ctx, meta }) {
58
+ const bytes = await bytesFromOpenAICompatibleData({ data, providerTag: 'imagerouter video' }, ctx);
59
+ const { wireModel, size, seconds } = meta;
60
+ return {
61
+ kind: 'complete',
62
+ result: {
63
+ bytes,
64
+ providerNote: `imagerouter/${wireModel} · ${size} · ${seconds === 'auto' ? 'auto' : `${seconds}s`} · ${bytes.length} bytes`,
65
+ suggestedExt: '.mp4',
66
+ },
67
+ };
68
+ }
69
+ /** A vendor-reported terminal failure, or `undefined` while the job is not in a failed state. */
70
+ function failedOutcome(data) {
71
+ const status = typeof data.status === 'string' ? data.status.toLowerCase() : '';
72
+ if (!TERMINAL_FAILED_STATUSES.has(status))
73
+ return undefined;
74
+ const error = data.error;
75
+ const detail = typeof error?.message === 'string' && error.message ? error.message : status;
76
+ return { kind: 'failed', message: `imagerouter video job failed: ${detail}` };
77
+ }
78
+ function retryAfterField(resp) {
79
+ const retryAfterMs = retryAfterMsFrom(resp);
80
+ return retryAfterMs !== undefined ? { retryAfterMs } : {};
81
+ }
48
82
  /**
49
83
  * Builds the ImageRouter video polling adapter for a given endpoint configuration.
50
84
  *
@@ -52,8 +86,7 @@ function retryAfterMsFrom(resp) {
52
86
  * context's own `wireModel`.
53
87
  * @complexity O(1) per built request; one vendor round trip per parse.
54
88
  */
55
- export function createImageRouterVideoPollingAdapter(_required, optional = {}) {
56
- const config = optional;
89
+ export function createImageRouterVideoPollingAdapter(_required, config = {}) {
57
90
  const baseUrl = trimTrailingSlash((config.baseUrl || DEFAULT_BASE_URL).trim());
58
91
  return {
59
92
  expectedLatencyClass: 'slow',
@@ -61,43 +94,30 @@ export function createImageRouterVideoPollingAdapter(_required, optional = {}) {
61
94
  // not have landed cannot be safely re-issued — a duplicate submit is a duplicate charge.
62
95
  submitIsIdempotent: false,
63
96
  buildSubmitRequest({ ctx }) {
64
- const wireModel = (config.wireModel || ctx.wireModel).trim();
65
- const seconds = typeof ctx.length === 'number' ? ctx.length : 'auto';
66
- const size = imageRouterSizeFor({ aspect: ctx.aspect, surface: 'video' });
97
+ const meta = videoMetaFor({ ctx, config });
67
98
  return {
68
99
  url: `${baseUrl}/videos/generations`,
69
- init: withUnsignedRequestInit({ ctx: ctx, init: {
100
+ init: withUnsignedRequestInit({ ctx, init: {
70
101
  method: 'POST',
71
102
  headers: { 'content-type': 'application/json' },
72
103
  body: JSON.stringify({
73
104
  prompt: ctx.prompt || 'A short cinematic clip.',
74
- model: wireModel,
75
- size,
76
- seconds,
105
+ model: meta.wireModel,
106
+ size: meta.size,
107
+ seconds: meta.seconds,
77
108
  response_format: 'b64_json',
78
109
  }),
79
110
  } }),
80
- meta: { wireModel, size, seconds },
111
+ meta,
81
112
  };
82
113
  },
83
114
  async parseSubmitResponse({ resp, ctx, request }) {
84
115
  const data = (await parseOpenAICompatibleJson(resp, 'imagerouter video'));
85
- if (Array.isArray(data.data)) {
86
- const bytes = await bytesFromOpenAICompatibleData({ data, providerTag: 'imagerouter video' }, ctx);
87
- const { wireModel, size, seconds } = request.meta;
88
- return {
89
- kind: 'complete',
90
- result: {
91
- bytes,
92
- providerNote: `imagerouter/${wireModel} · ${size} · ${seconds === 'auto' ? 'auto' : `${seconds}s`} · ${bytes.length} bytes`,
93
- suggestedExt: '.mp4',
94
- },
95
- };
96
- }
116
+ if (Array.isArray(data.data))
117
+ return completedVideo({ data, ctx, meta: request.meta });
97
118
  if (typeof data.id === 'string' && data.id) {
98
119
  // Only the job handle is persisted — no credential, no request body.
99
- const retryAfterMs = retryAfterMsFrom(resp);
100
- return { kind: 'pending', state: { jobId: data.id }, ...(retryAfterMs !== undefined ? { retryAfterMs } : {}) };
120
+ return { kind: 'pending', state: { jobId: data.id }, ...retryAfterField(resp) };
101
121
  }
102
122
  throw new Error('imagerouter video submit returned neither generated data nor a job id');
103
123
  },
@@ -108,34 +128,18 @@ export function createImageRouterVideoPollingAdapter(_required, optional = {}) {
108
128
  }
109
129
  return {
110
130
  url: `${baseUrl}/videos/${encodeURIComponent(jobId)}`,
111
- init: withUnsignedRequestInit({ ctx: ctx, init: { method: 'GET', headers: { accept: 'application/json' } } }),
112
- meta: { wireModel: (config.wireModel || ctx.wireModel).trim(), size: imageRouterSizeFor({ aspect: ctx.aspect, surface: 'video' }), seconds: typeof ctx.length === 'number' ? ctx.length : 'auto' },
131
+ init: withUnsignedRequestInit({ ctx, init: { method: 'GET', headers: { accept: 'application/json' } } }),
132
+ meta: videoMetaFor({ ctx, config }),
113
133
  };
114
134
  },
115
- async parsePollResponse({ resp, ctx, state: _state }) {
135
+ async parsePollResponse({ resp, ctx }) {
116
136
  const data = (await parseOpenAICompatibleJson(resp, 'imagerouter video'));
117
- const status = typeof data.status === 'string' ? data.status.toLowerCase() : '';
118
- if (TERMINAL_FAILED_STATUSES.has(status)) {
119
- const error = data.error;
120
- const detail = typeof error?.message === 'string' && error.message ? error.message : status;
121
- return { kind: 'failed', message: `imagerouter video job failed: ${detail}` };
122
- }
123
- if (Array.isArray(data.data)) {
124
- const bytes = await bytesFromOpenAICompatibleData({ data, providerTag: 'imagerouter video' }, ctx);
125
- const wireModel = (config.wireModel || ctx.wireModel).trim();
126
- const size = imageRouterSizeFor({ aspect: ctx.aspect, surface: 'video' });
127
- const seconds = typeof ctx.length === 'number' ? ctx.length : 'auto';
128
- return {
129
- kind: 'complete',
130
- result: {
131
- bytes,
132
- providerNote: `imagerouter/${wireModel} · ${size} · ${seconds === 'auto' ? 'auto' : `${seconds}s`} · ${bytes.length} bytes`,
133
- suggestedExt: '.mp4',
134
- },
135
- };
136
- }
137
- const retryAfterMs = retryAfterMsFrom(resp);
138
- return { kind: 'pending', ...(retryAfterMs !== undefined ? { retryAfterMs } : {}) };
137
+ const failed = failedOutcome(data);
138
+ if (failed)
139
+ return failed;
140
+ if (Array.isArray(data.data))
141
+ return completedVideo({ data, ctx, meta: videoMetaFor({ ctx, config }) });
142
+ return { kind: 'pending', ...retryAfterField(resp) };
139
143
  },
140
144
  };
141
145
  }
@@ -1,5 +1,5 @@
1
1
  import type { ProviderCredentials, RenderContext, RenderResult } from '../types.js';
2
- import type { VendorAdapterRegistry } from '../vendor-registry.js';
2
+ import type { VendorAdapterRegistry } from '../vendor-registry-core.js';
3
3
  /** ImageRouter's image/video generation `size` string for a given aspect ratio. */
4
4
  export declare function imageRouterSizeFor({ aspect, surface }: {
5
5
  aspect: string | undefined;
@@ -18,35 +18,22 @@
18
18
  */
19
19
  import { buildOpenAIImageUrl, buildOpenAIVideoUrl, bytesFromOpenAICompatibleData, parseOpenAICompatibleJson, sniffImageExt, withRequestInit, } from '../openai-compatible.js';
20
20
  import { dispatchVendorRequest, requireApiKey } from '../vendor-adapter.js';
21
+ // Maps (not object literals) so an inherited key such as `constructor` can never resolve as an aspect.
22
+ const PORTRAIT_AND_4_3_SIZES = [['9:16', '576x1024'], ['4:3', '1024x768'], ['3:4', '768x1024']];
23
+ const IMAGE_SIZES = new Map([['16:9', '1024x576'], ...PORTRAIT_AND_4_3_SIZES]);
24
+ const VIDEO_SIZES = new Map([['1:1', '1024x1024'], ...PORTRAIT_AND_4_3_SIZES]);
21
25
  /** ImageRouter's image/video generation `size` string for a given aspect ratio. */
22
26
  export function imageRouterSizeFor({ aspect, surface }) {
23
- if (surface === 'video') {
24
- if (aspect === '1:1')
25
- return '1024x1024';
26
- if (aspect === '9:16')
27
- return '576x1024';
28
- if (aspect === '4:3')
29
- return '1024x768';
30
- if (aspect === '3:4')
31
- return '768x1024';
32
- return '1024x576';
33
- }
34
- if (aspect === '16:9')
35
- return '1024x576';
36
- if (aspect === '9:16')
37
- return '576x1024';
38
- if (aspect === '4:3')
39
- return '1024x768';
40
- if (aspect === '3:4')
41
- return '768x1024';
42
- return '1024x1024';
27
+ const [sizes, fallback] = surface === 'video' ? [VIDEO_SIZES, '1024x576'] : [IMAGE_SIZES, '1024x1024'];
28
+ return sizes.get(aspect ?? '') ?? fallback;
43
29
  }
30
+ const DEFAULT_BASE_URL = 'https://api.imagerouter.io/v1/openai';
44
31
  const NO_CREDENTIAL_MESSAGE = 'no ImageRouter API key — configure it or set IMAGEROUTER_API_KEY';
45
32
  const imageRouterImageAdapter = {
46
33
  requireCredential: requireApiKey({ message: NO_CREDENTIAL_MESSAGE }),
47
34
  buildRequest({ ctx, credentials }) {
48
35
  const apiKey = credentials.apiKey; // requireCredential already validated this.
49
- const baseUrl = (credentials.baseUrl || 'https://api.imagerouter.io/v1/openai').trim();
36
+ const baseUrl = (credentials.baseUrl || DEFAULT_BASE_URL).trim();
50
37
  const wireModel = (credentials.model || ctx.wireModel).trim();
51
38
  const size = imageRouterSizeFor({ aspect: ctx.aspect, surface: 'image' });
52
39
  const body = {
@@ -81,13 +68,13 @@ const imageRouterImageAdapter = {
81
68
  },
82
69
  };
83
70
  export async function renderImageRouterImage({ ctx, credentials }) {
84
- return dispatchVendorRequest({ adapter: imageRouterImageAdapter, ctx: ctx, credentials: credentials });
71
+ return dispatchVendorRequest({ adapter: imageRouterImageAdapter, ctx, credentials });
85
72
  }
86
73
  const imageRouterVideoAdapter = {
87
74
  requireCredential: requireApiKey({ message: NO_CREDENTIAL_MESSAGE }),
88
75
  buildRequest({ ctx, credentials }) {
89
76
  const apiKey = credentials.apiKey; // requireCredential already validated this.
90
- const baseUrl = (credentials.baseUrl || 'https://api.imagerouter.io/v1/openai').trim();
77
+ const baseUrl = (credentials.baseUrl || DEFAULT_BASE_URL).trim();
91
78
  const wireModel = (credentials.model || ctx.wireModel).trim();
92
79
  const seconds = typeof ctx.length === 'number' ? ctx.length : 'auto';
93
80
  const size = imageRouterSizeFor({ aspect: ctx.aspect, surface: 'video' });
@@ -123,7 +110,7 @@ const imageRouterVideoAdapter = {
123
110
  },
124
111
  };
125
112
  export async function renderImageRouterVideo({ ctx, credentials }) {
126
- return dispatchVendorRequest({ adapter: imageRouterVideoAdapter, ctx: ctx, credentials: credentials });
113
+ return dispatchVendorRequest({ adapter: imageRouterVideoAdapter, ctx, credentials });
127
114
  }
128
115
  /** Explicit composition: importing a renderer never changes another module's registry. */
129
116
  export function registerVendorAdapters({ registry }, _optional = {}) {
@@ -1,5 +1,5 @@
1
1
  import type { ProviderCredentials, RenderContext, RenderResult } from '../types.js';
2
- import type { VendorAdapterRegistry } from '../vendor-registry.js';
2
+ import type { VendorAdapterRegistry } from '../vendor-registry-core.js';
3
3
  export declare function renderMinimaxTTS({ ctx, credentials }: {
4
4
  ctx: RenderContext;
5
5
  credentials: ProviderCredentials;
@@ -1,5 +1,5 @@
1
1
  import type { ProviderCredentials, RenderContext, RenderResult } from '../types.js';
2
- import type { VendorAdapterRegistry } from '../vendor-registry.js';
2
+ import type { VendorAdapterRegistry } from '../vendor-registry-core.js';
3
3
  export declare function renderNanoBananaImage({ ctx, credentials }: {
4
4
  ctx: RenderContext;
5
5
  credentials: ProviderCredentials;
@@ -51,40 +51,32 @@ function nanoBananaHeaders(baseUrl, apiKey) {
51
51
  /**
52
52
  * `providers.ts`'s `MEDIA_ASPECTS` are all natively supported by Nano
53
53
  * Banana's `imageConfig.aspectRatio`; anything else defaults to `'1:1'`.
54
- * Written as independent `if` returns (rather than the origin's one
55
- * `||`-chained condition) to match this package's established
54
+ * Written as a `Set` membership test (rather than the origin's one
55
+ * `||`-chained condition) to match this package's table-driven
56
56
  * aspect-mapping style (see `imagerouter.ts`'s `imageRouterSizeFor`) — same
57
- * input -> output mapping for every case, not a behavior change.
57
+ * input -> output mapping for every case, not a behavior change. A `Set`,
58
+ * not an object literal, so an inherited key like `constructor` can never match.
58
59
  */
60
+ const NANOBANANA_ASPECTS = new Set(['1:1', '16:9', '9:16', '4:3', '3:4']);
59
61
  function nanoBananaAspectFor(aspect) {
60
- if (aspect === '1:1')
61
- return '1:1';
62
- if (aspect === '16:9')
63
- return '16:9';
64
- if (aspect === '9:16')
65
- return '9:16';
66
- if (aspect === '4:3')
67
- return '4:3';
68
- if (aspect === '3:4')
69
- return '3:4';
70
- return '1:1';
62
+ return aspect !== undefined && NANOBANANA_ASPECTS.has(aspect) ? aspect : '1:1';
71
63
  }
64
+ function asArray(value) {
65
+ return Array.isArray(value) ? value : [];
66
+ }
67
+ /** A part's non-empty `inlineData.data` string, else `undefined` (camelCase only — Gemini's documented field). */
68
+ function inlineDataOf(part) {
69
+ const data = part?.inlineData?.data;
70
+ return typeof data === 'string' && data ? data : undefined;
71
+ }
72
+ /** The first inline image across every candidate's parts, in order. */
72
73
  function inlineImageBytesFromGenerateContent(data) {
73
- const candidates = data?.candidates;
74
- if (Array.isArray(candidates)) {
75
- for (const candidate of candidates) {
76
- const parts = candidate?.content?.parts;
77
- if (Array.isArray(parts)) {
78
- for (const part of parts) {
79
- const inline = part?.inlineData;
80
- if (typeof inline?.data === 'string' && inline.data) {
81
- return Buffer.from(inline.data, 'base64');
82
- }
83
- }
84
- }
85
- }
86
- }
87
- throw new Error('nano-banana image response missing candidates[].content.parts[].inlineData.data');
74
+ const candidates = asArray(data?.candidates);
75
+ const parts = candidates.flatMap((candidate) => asArray(candidate?.content?.parts));
76
+ const b64 = parts.map(inlineDataOf).find((d) => d !== undefined);
77
+ if (!b64)
78
+ throw new Error('nano-banana image response missing candidates[].content.parts[].inlineData.data');
79
+ return Buffer.from(b64, 'base64');
88
80
  }
89
81
  const NO_CREDENTIAL_MESSAGE = 'no Nano Banana credential — configure an API key or set GOOGLE_API_KEY / GEMINI_API_KEY.';
90
82
  const nanoBananaImageAdapter = {
@@ -135,7 +127,7 @@ const nanoBananaImageAdapter = {
135
127
  },
136
128
  };
137
129
  export async function renderNanoBananaImage({ ctx, credentials }) {
138
- return dispatchVendorRequest({ adapter: nanoBananaImageAdapter, ctx: ctx, credentials: credentials });
130
+ return dispatchVendorRequest({ adapter: nanoBananaImageAdapter, ctx, credentials });
139
131
  }
140
132
  /** Explicit composition: importing a renderer never changes another module's registry. */
141
133
  export function registerVendorAdapters({ registry }, _optional = {}) {
@@ -1,5 +1,5 @@
1
1
  import type { ProviderCredentials, RenderContext, RenderResult } from '../types.js';
2
- import type { VendorAdapterRegistry } from '../vendor-registry.js';
2
+ import type { VendorAdapterRegistry } from '../vendor-registry-core.js';
3
3
  export declare function renderOpenAIImage({ ctx, credentials }: {
4
4
  ctx: RenderContext;
5
5
  credentials: ProviderCredentials;