Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions js/polliLib/src/image.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { getDefaultClient } from './client.js';

function modelSupportsJson(info) {
if (!info || typeof info !== 'object') return false;
if (info.json === true) return true;
const fields = [info.output, info.outputs, info.formats, info.format];
for (const f of fields) {
if (Array.isArray(f) && f.some(v => String(v).toLowerCase() === 'json')) return true;
}
return false;
}

const bool = v => (v == null ? undefined : (v ? 'true' : 'false'));
const sleep = ms => new Promise(res => setTimeout(res, ms));

Expand All @@ -19,17 +29,29 @@ export async function image(prompt, {
if (enhance != null) params.enhance = bool(enhance);
if (safe != null) params.safe = bool(safe);
if (referrer) params.referrer = referrer;
if (json) params.json = 'true';

const headers = json ? { Accept: 'application/json' } : {};
let expectJson = false;
const headers = {};
if (json && model) {
try {
const models = await imageModels(client);
if (modelSupportsJson(models?.[model])) {
params.json = 'true';
headers.Accept = 'application/json';
expectJson = true;
}
} catch {
// ignore capability errors and fall back to blob response
}
}

const r = await client.get(url, { params, headers });
if (!r.ok) throw new Error(`image error ${r.status}`);

const ct = r.headers.get('content-type') ?? '';
if (ct.includes('application/json')) {
const data = await r.json();
if (json) return data;
if (expectJson) return data;
if (data?.url) {
const ir = await client.get(data.url);
if (ir.ok) return await ir.blob();
Expand Down
17 changes: 16 additions & 1 deletion js/polliLib/src/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@ import { getDefaultClient } from './client.js';
import { imageModels } from './image.js';
import { textModels } from './text.js';

// Return true if a model description indicates JSON support
export function imageModelSupportsJson(info) {
if (!info || typeof info !== 'object') return false;
if (info.json === true) return true;
const fields = [info.output, info.outputs, info.formats, info.format];
for (const f of fields) {
if (Array.isArray(f) && f.some(v => String(v).toLowerCase() === 'json')) return true;
}
return false;
}

export async function modelCapabilities(client = getDefaultClient()) {
const [image, text] = await Promise.all([
const [img, text] = await Promise.all([
imageModels(client).catch(() => ({})),
textModels(client).catch(() => ({})),
]);
const image = {};
for (const [name, info] of Object.entries(img ?? {})) {
image[name] = { ...(info || {}), json: imageModelSupportsJson(info) };
}
return { image, text, audio: text?.['openai-audio'] ?? {} };
}

42 changes: 42 additions & 0 deletions tests/pollilib-image-json-capability.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import assert from 'assert/strict';
import { image } from '../js/polliLib/src/image.js';
import { modelCapabilities } from '../js/polliLib/src/models.js';

const client = {
imageBase: 'https://example.com',
async get(url, { params, headers } = {}) {
if (url.endsWith('/models')) {
return {
ok: true,
headers: { get: () => 'application/json' },
async json() { return { foo: { json: true }, bar: {} }; }
};
}
if (params.model === 'foo') {
assert.equal(params.json, 'true');
assert.equal(headers.Accept, 'application/json');
return {
ok: true,
headers: { get: () => 'application/json' },
async json() { return { model: params.model }; }
};
}
assert.equal(params.json, undefined);
assert.equal(headers?.Accept, undefined);
return {
ok: true,
headers: { get: () => 'image/png' },
async blob() { return { blob: true }; }
};
}
};

const caps = await modelCapabilities(client);
assert.equal(caps.image.foo.json, true);
assert.equal(caps.image.bar.json, false);

const data = await image('test', { model: 'foo', json: true }, client);
assert.equal(data.model, 'foo');

const blob = await image('test', { model: 'bar', json: true }, client);
assert.deepEqual(blob, { blob: true });
8 changes: 8 additions & 0 deletions tests/pollilib-image-json.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import { image } from '../js/polliLib/src/image.js';
const client = {
imageBase: 'https://example.com',
async get(url, { params, headers } = {}) {
if (url.endsWith('/models')) {
return {
ok: true,
headers: { get: () => 'application/json' },
async json() { return { foo: { json: true } }; }
};
}
assert.equal(params.json, 'true');
assert.equal(headers.Accept, 'application/json');
return {
ok: true,
Expand Down