@agentforge-io/connectors-higgsfield 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/dist/api.d.ts +38 -1
- package/dist/api.js +137 -8
- package/dist/index.d.ts +1 -1
- package/dist/mcp.d.ts +6 -0
- package/dist/mcp.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -53,7 +53,11 @@ envelopes of `balance` (`{credits, subscription_plan_type}`) and `estimate_image
|
|
|
53
53
|
NOT verified against a real grant. Parsed tolerantly; anything else throws `unexpected_response`:
|
|
54
54
|
- `media_import_url`: media id in `media_id` | `id` | `media.id` | `results[0].(media_id|id)`.
|
|
55
55
|
- `remove_background`: a `job_id` (also in `job`/`jobs[]`/`results[]`) and/or a `media_id`; whether it costs credits and
|
|
56
|
-
where `use_unlim` goes (we put it inside `params`).
|
|
56
|
+
where `use_unlim` goes (we put it inside `params`). The real success envelope was NEVER captured (PDK-HIGGSFIELD-UNKNOWN: a paid,
|
|
57
|
+
completed call surfaced as an unreadable reply). If the reply cannot be read, `removeBackground` does not guess: it looks the job up
|
|
58
|
+
for free in the generation history (`findBackgroundRemoval`, verified `show_generations` shape: model `image_background_remover`,
|
|
59
|
+
`params.medias[].data.id` = the source job/media id, `results.rawUrl`) and only throws `unexpected_response` (with the reply's SHAPE,
|
|
60
|
+
never its values) when nothing matches.
|
|
57
61
|
- `generate_image`: job ids in `job_id` | `id` | `job_ids[]` | `jobs[]` | `results[]`; the `variant` parameter name for
|
|
58
62
|
flux_2 `pro`; whether `quote_ref` can be passed back.
|
|
59
63
|
- `jobs_wait`: `all_terminal` boolean plus a `jobs[]` (or `results[]`) list with `job_id`|`id`, `status`, and result URLs
|
package/dist/api.d.ts
CHANGED
|
@@ -35,9 +35,29 @@ export interface GenerateImageParams {
|
|
|
35
35
|
medias?: MediaRef[];
|
|
36
36
|
[extra: string]: unknown;
|
|
37
37
|
}
|
|
38
|
+
/** Structure only (keys and primitive types, never values): safe to log and to store for diagnosis. */
|
|
39
|
+
export declare function shapeOf(v: unknown, depth?: number): string;
|
|
40
|
+
export interface BackgroundRemoval {
|
|
41
|
+
jobId: string;
|
|
42
|
+
status: JobStatus;
|
|
43
|
+
url?: string;
|
|
44
|
+
}
|
|
45
|
+
export interface HiggsfieldApiOptions {
|
|
46
|
+
/** Injectable for tests. */
|
|
47
|
+
sleep?: (ms: number) => Promise<void>;
|
|
48
|
+
now?: () => number;
|
|
49
|
+
/** Lookups attempted after an unreadable remove_background reply (default 3). */
|
|
50
|
+
recoverTries?: number;
|
|
51
|
+
/** Delay between those lookups (default 2000 ms). */
|
|
52
|
+
recoverDelayMs?: number;
|
|
53
|
+
}
|
|
38
54
|
export declare class HiggsfieldApi {
|
|
39
55
|
private readonly mcp;
|
|
40
|
-
|
|
56
|
+
private readonly sleep;
|
|
57
|
+
private readonly now;
|
|
58
|
+
private readonly recoverTries;
|
|
59
|
+
private readonly recoverDelayMs;
|
|
60
|
+
constructor(mcp: HiggsfieldMcpClient, opts?: HiggsfieldApiOptions);
|
|
41
61
|
/** Verified envelope: { credits, subscription_plan_type }. */
|
|
42
62
|
balance(): Promise<Balance>;
|
|
43
63
|
/** Returns a confirmed media_id (envelope parsed tolerantly). */
|
|
@@ -47,11 +67,28 @@ export declare class HiggsfieldApi {
|
|
|
47
67
|
/**
|
|
48
68
|
* Treated as spending (its cost is not published). Result may be a job or a media id.
|
|
49
69
|
* The tool's params schema is closed: only media_id and media_type are accepted.
|
|
70
|
+
*
|
|
71
|
+
* The real success envelope was never captured (PDK-HIGGSFIELD-UNKNOWN). When the call was ACCEPTED but its reply is
|
|
72
|
+
* unreadable (`unexpected_response`: no JSON content, or no job/media id), the job is looked up for free in the
|
|
73
|
+
* generation history (`findBackgroundRemoval`) before giving up. Timeouts / `outcome_unknown` are NOT recovered here
|
|
74
|
+
* (the call may never have been made); the platform reconciles those with the same free lookup.
|
|
50
75
|
*/
|
|
51
76
|
removeBackground(mediaId: string): Promise<{
|
|
52
77
|
jobId?: string;
|
|
53
78
|
mediaId?: string;
|
|
54
79
|
}>;
|
|
80
|
+
/**
|
|
81
|
+
* FREE, read-only. Finds the background removal of `sourceId` (a generation job id or media id) in the account's
|
|
82
|
+
* generation history: `show_generations` items with model `image_background_remover` whose `params.medias[].data.id`
|
|
83
|
+
* is `sourceId`. Newest match wins. Verified shape (2026-12-01): `{ items: [{ id, status, model, params:{medias:
|
|
84
|
+
* [{role, data:{id,type,url}}]}, results:{rawUrl,minUrl}, createdAt }], next_cursor }`.
|
|
85
|
+
* Never throws on items it does not understand; returns undefined when nothing matches.
|
|
86
|
+
*/
|
|
87
|
+
findBackgroundRemoval(sourceId: string, opts?: {
|
|
88
|
+
createdAfter?: number;
|
|
89
|
+
pageSize?: number;
|
|
90
|
+
maxPages?: number;
|
|
91
|
+
}): Promise<BackgroundRemoval | undefined>;
|
|
55
92
|
/** Verified envelope: { cost: { credits, credits_exact }, quote_ref }. Free call. */
|
|
56
93
|
estimateImageCost(params: GenerateImageParams): Promise<CostEstimate>;
|
|
57
94
|
/** SPENDING. Never retried; ambiguous failures surface as providerCode 'outcome_unknown'. */
|
package/dist/api.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.HiggsfieldApi = exports.TERMINAL_JOB_STATUSES = void 0;
|
|
4
|
+
exports.shapeOf = shapeOf;
|
|
4
5
|
const mcp_1 = require("./mcp");
|
|
5
6
|
exports.TERMINAL_JOB_STATUSES = ['completed', 'failed', 'nsfw', 'ip_detected'];
|
|
6
7
|
function unexpected(tool, what) {
|
|
@@ -41,7 +42,7 @@ function findId(o, keys, listKeys, nestKeys = []) {
|
|
|
41
42
|
for (const k of listKeys) {
|
|
42
43
|
const l = o[k];
|
|
43
44
|
if (Array.isArray(l) && l.length) {
|
|
44
|
-
const e = firstId(l[0], keys);
|
|
45
|
+
const e = typeof l[0] === 'string' && l[0] ? l[0] : firstId(l[0], keys);
|
|
45
46
|
if (e)
|
|
46
47
|
return e;
|
|
47
48
|
}
|
|
@@ -67,6 +68,44 @@ function findIds(o, keys, listKeys) {
|
|
|
67
68
|
}
|
|
68
69
|
return [...new Set(out)];
|
|
69
70
|
}
|
|
71
|
+
/** Last resort: first string under any of `keys`, at any depth (bounded). */
|
|
72
|
+
function deepFindId(o, keys, depth = 0) {
|
|
73
|
+
if (depth > 4)
|
|
74
|
+
return undefined;
|
|
75
|
+
if (Array.isArray(o)) {
|
|
76
|
+
for (const e of o.slice(0, 10)) {
|
|
77
|
+
const f = deepFindId(e, keys, depth + 1);
|
|
78
|
+
if (f)
|
|
79
|
+
return f;
|
|
80
|
+
}
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
if (!isObj(o))
|
|
84
|
+
return undefined;
|
|
85
|
+
const direct = firstId(o, keys);
|
|
86
|
+
if (direct)
|
|
87
|
+
return direct;
|
|
88
|
+
for (const v of Object.values(o)) {
|
|
89
|
+
const f = deepFindId(v, keys, depth + 1);
|
|
90
|
+
if (f)
|
|
91
|
+
return f;
|
|
92
|
+
}
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
/** Structure only (keys and primitive types, never values): safe to log and to store for diagnosis. */
|
|
96
|
+
function shapeOf(v, depth = 0) {
|
|
97
|
+
if (Array.isArray(v))
|
|
98
|
+
return depth >= 3 ? 'array' : `[${v.length ? shapeOf(v[0], depth + 1) : ''}]`;
|
|
99
|
+
if (isObj(v)) {
|
|
100
|
+
if (depth >= 3)
|
|
101
|
+
return 'object';
|
|
102
|
+
const parts = Object.keys(v)
|
|
103
|
+
.slice(0, 12)
|
|
104
|
+
.map((k) => `${k.slice(0, 40)}:${shapeOf(v[k], depth + 1)}`);
|
|
105
|
+
return `{${parts.join(',')}}`;
|
|
106
|
+
}
|
|
107
|
+
return v === null ? 'null' : typeof v;
|
|
108
|
+
}
|
|
70
109
|
function urlsOf(v) {
|
|
71
110
|
const out = [];
|
|
72
111
|
const push = (e) => {
|
|
@@ -85,8 +124,12 @@ function urlsOf(v) {
|
|
|
85
124
|
return out;
|
|
86
125
|
}
|
|
87
126
|
class HiggsfieldApi {
|
|
88
|
-
constructor(mcp) {
|
|
127
|
+
constructor(mcp, opts = {}) {
|
|
89
128
|
this.mcp = mcp;
|
|
129
|
+
this.sleep = opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
130
|
+
this.now = opts.now ?? Date.now;
|
|
131
|
+
this.recoverTries = opts.recoverTries ?? 3;
|
|
132
|
+
this.recoverDelayMs = opts.recoverDelayMs ?? 2000;
|
|
90
133
|
}
|
|
91
134
|
/** Verified envelope: { credits, subscription_plan_type }. */
|
|
92
135
|
async balance() {
|
|
@@ -109,15 +152,101 @@ class HiggsfieldApi {
|
|
|
109
152
|
/**
|
|
110
153
|
* Treated as spending (its cost is not published). Result may be a job or a media id.
|
|
111
154
|
* The tool's params schema is closed: only media_id and media_type are accepted.
|
|
155
|
+
*
|
|
156
|
+
* The real success envelope was never captured (PDK-HIGGSFIELD-UNKNOWN). When the call was ACCEPTED but its reply is
|
|
157
|
+
* unreadable (`unexpected_response`: no JSON content, or no job/media id), the job is looked up for free in the
|
|
158
|
+
* generation history (`findBackgroundRemoval`) before giving up. Timeouts / `outcome_unknown` are NOT recovered here
|
|
159
|
+
* (the call may never have been made); the platform reconciles those with the same free lookup.
|
|
112
160
|
*/
|
|
113
161
|
async removeBackground(mediaId) {
|
|
114
162
|
const params = { media_id: mediaId, media_type: 'image' };
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
163
|
+
const startedAt = this.now() / 1000;
|
|
164
|
+
let failure;
|
|
165
|
+
try {
|
|
166
|
+
const r = await this.mcp.callTool('remove_background', { params }, { spending: true });
|
|
167
|
+
// Same tolerance as generate_image (`id`, `job_ids[]`, ...).
|
|
168
|
+
const jobId = findId(r, ['job_id', 'jobId', 'id'], ['job_ids', 'jobs', 'results', 'result'], ['job', 'result', 'data']) ??
|
|
169
|
+
deepFindId(r, ['job_id', 'jobId']);
|
|
170
|
+
const outMedia = findId(r, ['media_id', 'mediaId'], ['medias', 'results'], ['media']);
|
|
171
|
+
if (jobId || outMedia)
|
|
172
|
+
return { ...(jobId ? { jobId } : {}), ...(outMedia ? { mediaId: outMedia } : {}) };
|
|
173
|
+
failure = new mcp_1.HiggsfieldApiError(200, 'unexpected_response', `remove_background: neither a job id nor a media id in response (${shapeOf(r)})`);
|
|
174
|
+
}
|
|
175
|
+
catch (e) {
|
|
176
|
+
if (!(e instanceof mcp_1.HiggsfieldApiError) || e.providerCode !== 'unexpected_response')
|
|
177
|
+
throw e;
|
|
178
|
+
failure = e;
|
|
179
|
+
}
|
|
180
|
+
for (let i = 0; i < this.recoverTries; i++) {
|
|
181
|
+
if (i > 0)
|
|
182
|
+
await this.sleep(this.recoverDelayMs);
|
|
183
|
+
let found;
|
|
184
|
+
try {
|
|
185
|
+
found = await this.findBackgroundRemoval(mediaId, { createdAfter: startedAt - 60 });
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
found = undefined; // the lookup is best effort; the original failure is what gets reported
|
|
189
|
+
}
|
|
190
|
+
if (found)
|
|
191
|
+
return { jobId: found.jobId };
|
|
192
|
+
}
|
|
193
|
+
throw failure;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* FREE, read-only. Finds the background removal of `sourceId` (a generation job id or media id) in the account's
|
|
197
|
+
* generation history: `show_generations` items with model `image_background_remover` whose `params.medias[].data.id`
|
|
198
|
+
* is `sourceId`. Newest match wins. Verified shape (2026-12-01): `{ items: [{ id, status, model, params:{medias:
|
|
199
|
+
* [{role, data:{id,type,url}}]}, results:{rawUrl,minUrl}, createdAt }], next_cursor }`.
|
|
200
|
+
* Never throws on items it does not understand; returns undefined when nothing matches.
|
|
201
|
+
*/
|
|
202
|
+
async findBackgroundRemoval(sourceId, opts = {}) {
|
|
203
|
+
const size = opts.pageSize ?? 30;
|
|
204
|
+
const maxPages = opts.maxPages ?? 2;
|
|
205
|
+
let cursor;
|
|
206
|
+
for (let page = 0; page < maxPages; page++) {
|
|
207
|
+
const r = await this.mcp.callTool('show_generations', {
|
|
208
|
+
only_completed: false,
|
|
209
|
+
size,
|
|
210
|
+
type: 'image',
|
|
211
|
+
...(cursor !== undefined ? { cursor } : {}),
|
|
212
|
+
});
|
|
213
|
+
if (!isObj(r) || !Array.isArray(r.items))
|
|
214
|
+
return undefined;
|
|
215
|
+
let best;
|
|
216
|
+
for (const it of r.items) {
|
|
217
|
+
if (!isObj(it) || it.model !== 'image_background_remover')
|
|
218
|
+
continue;
|
|
219
|
+
const id = str(it.id);
|
|
220
|
+
if (!id)
|
|
221
|
+
continue;
|
|
222
|
+
const medias = isObj(it.params) && Array.isArray(it.params.medias) ? it.params.medias : [];
|
|
223
|
+
const matches = medias.some((m) => isObj(m) && isObj(m.data) && str(m.data.id) === sourceId);
|
|
224
|
+
if (!matches)
|
|
225
|
+
continue;
|
|
226
|
+
const at = typeof it.createdAt === 'number' ? it.createdAt : 0;
|
|
227
|
+
if (opts.createdAfter !== undefined && at < opts.createdAfter)
|
|
228
|
+
continue;
|
|
229
|
+
if (best && best.at >= at)
|
|
230
|
+
continue;
|
|
231
|
+
const url = isObj(it.results) ? str(it.results.rawUrl) : undefined;
|
|
232
|
+
best = { jobId: id, status: str(it.status) ?? 'unknown', ...(url ? { url } : {}), at };
|
|
233
|
+
}
|
|
234
|
+
if (best) {
|
|
235
|
+
const { at: _at, ...out } = best;
|
|
236
|
+
return out;
|
|
237
|
+
}
|
|
238
|
+
const next = r.next_cursor;
|
|
239
|
+
if (typeof next !== 'number' || !r.items.length)
|
|
240
|
+
return undefined;
|
|
241
|
+
// Stop paging once the page is entirely older than the window.
|
|
242
|
+
if (opts.createdAfter !== undefined) {
|
|
243
|
+
const times = r.items.map((i) => (isObj(i) && typeof i.createdAt === 'number' ? i.createdAt : Infinity));
|
|
244
|
+
if (Math.max(...times) < opts.createdAfter)
|
|
245
|
+
return undefined;
|
|
246
|
+
}
|
|
247
|
+
cursor = next;
|
|
248
|
+
}
|
|
249
|
+
return undefined;
|
|
121
250
|
}
|
|
122
251
|
/** Verified envelope: { cost: { credits, credits_exact }, quote_ref }. Free call. */
|
|
123
252
|
async estimateImageCost(params) {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { higgsfieldConnector, HIGGSFIELD_MCP_URL, type HiggsfieldConnectorOptions } from './connector';
|
|
2
2
|
export { higgsfieldClientMetadata, type HiggsfieldClientMetadata, type HiggsfieldClientMetadataInput, } from './client-metadata';
|
|
3
3
|
export { HiggsfieldMcpClient, HiggsfieldApiError, type McpClientOptions, type CallToolOptions } from './mcp';
|
|
4
|
-
export { HiggsfieldApi, TERMINAL_JOB_STATUSES, type Balance, type CostEstimate, type GenerateImageParams, type JobRef, type JobState, type JobStatus, type JobsWaitResult, type MediaRef, } from './api';
|
|
4
|
+
export { HiggsfieldApi, TERMINAL_JOB_STATUSES, type Balance, type BackgroundRemoval, type HiggsfieldApiOptions, type CostEstimate, type GenerateImageParams, type JobRef, type JobState, type JobStatus, type JobsWaitResult, type MediaRef, } from './api';
|
|
5
5
|
export * from './rules';
|
|
6
6
|
export { higgsfieldGetBalanceTool } from './tools/get-balance';
|
|
7
7
|
export { higgsfieldGetJobTool } from './tools/get-job';
|
package/dist/mcp.d.ts
CHANGED
|
@@ -16,6 +16,11 @@ export interface McpClientOptions {
|
|
|
16
16
|
endpoint?: string;
|
|
17
17
|
fetchImpl?: typeof fetch;
|
|
18
18
|
timeoutMs?: number;
|
|
19
|
+
/**
|
|
20
|
+
* Timeout of a SPENDING call. Longer than `timeoutMs`: a paid tool may do work before answering, and timing out
|
|
21
|
+
* turns an accepted (charged) call into an unknown outcome. Default 90 s.
|
|
22
|
+
*/
|
|
23
|
+
spendingTimeoutMs?: number;
|
|
19
24
|
/** Extra attempts for read-only calls on 429/5xx/network/timeout. */
|
|
20
25
|
maxRetries?: number;
|
|
21
26
|
sleep?: (ms: number) => Promise<void>;
|
|
@@ -35,6 +40,7 @@ export declare class HiggsfieldMcpClient {
|
|
|
35
40
|
private readonly fetchImpl;
|
|
36
41
|
private readonly endpoint;
|
|
37
42
|
private readonly timeoutMs;
|
|
43
|
+
private readonly spendingTimeoutMs;
|
|
38
44
|
private readonly maxRetries;
|
|
39
45
|
private readonly sleep;
|
|
40
46
|
constructor(opts: McpClientOptions);
|
package/dist/mcp.js
CHANGED
|
@@ -30,6 +30,7 @@ class HiggsfieldMcpClient {
|
|
|
30
30
|
this.fetchImpl = opts.fetchImpl ?? fetch;
|
|
31
31
|
this.endpoint = opts.endpoint ?? exports.HIGGSFIELD_MCP_ENDPOINT;
|
|
32
32
|
this.timeoutMs = opts.timeoutMs ?? 30_000;
|
|
33
|
+
this.spendingTimeoutMs = opts.spendingTimeoutMs ?? 90_000;
|
|
33
34
|
this.maxRetries = opts.maxRetries ?? 2;
|
|
34
35
|
this.sleep = opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
35
36
|
this.sessionId = opts.sessionId;
|
|
@@ -95,7 +96,7 @@ class HiggsfieldMcpClient {
|
|
|
95
96
|
if (this.sessionId)
|
|
96
97
|
headers['mcp-session-id'] = this.sessionId;
|
|
97
98
|
const ctrl = new AbortController();
|
|
98
|
-
const timer = setTimeout(() => ctrl.abort(), this.timeoutMs);
|
|
99
|
+
const timer = setTimeout(() => ctrl.abort(), spending ? this.spendingTimeoutMs : this.timeoutMs);
|
|
99
100
|
let res;
|
|
100
101
|
try {
|
|
101
102
|
res = await this.fetchImpl(this.endpoint, {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentforge-io/connectors-higgsfield",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Higgsfield connector for AgentForge — OAuth public client (CIMD + PKCE) over the Higgsfield MCP server, with a typed MCP client, versioned catalog-photo rules, and read-only agent tools (balance, job status). Nothing here spends credits on its own.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|