Files
jackwener__opencli/clis/yollomi/upscale.js
jakevin d2974a9ff6 refactor(adapters): convert adapter layer from TypeScript to JavaScript (#928)
* refactor(adapters): convert adapter layer from TypeScript to JavaScript

Core framework stays TypeScript; adapter layer moves to JS-first.
Adapters are essentially "executable config + browser scripts" that
barely use TS features — this simplifies the build/distribution pipeline
by removing the dist/clis/ intermediate compilation step.

Changes:
- Convert all 753 adapter files in clis/ from .ts to .js
- Update tsconfig to exclude clis/ from compilation
- Simplify build-manifest to scan clis/*.js directly (no dist/clis/)
- Update discovery, main, fetch-adapters to load JS adapters from clis/
- Update generate-verified to output .js artifacts
- Update package.json files field: dist/clis/ → clis/
- Fix all test files for the .ts → .js transition

* fix(main): use findPackageRoot for BUILTIN_CLIS path

The previous relative path (../../clis from __dirname) only worked for
dist/src/main.js but broke dev mode (tsx src/main.ts) where __dirname
is <repo>/src — resolving to /clis instead of <repo>/clis.

Use findPackageRoot() which works for both dev and prod paths.
2026-04-10 14:52:18 +08:00

54 lines
2.4 KiB
JavaScript

/**
* Yollomi image upscaling — POST /api/ai/image-upscaler
*/
import * as path from 'node:path';
import { cli, Strategy } from '@jackwener/opencli/registry';
import { CliError } from '@jackwener/opencli/errors';
import { log } from '@jackwener/opencli/logger';
import { YOLLOMI_DOMAIN, yollomiPost, downloadOutput, fmtBytes } from './utils.js';
cli({
site: 'yollomi',
name: 'upscale',
description: 'Upscale image resolution with AI (1 credit)',
domain: YOLLOMI_DOMAIN,
strategy: Strategy.COOKIE,
args: [
{ name: 'image', positional: true, required: true, help: 'Image URL to upscale' },
{ name: 'scale', default: '2', choices: ['2', '4'], help: 'Upscale factor (2 or 4)' },
{ name: 'output', default: './yollomi-output', help: 'Output directory' },
{ name: 'no-download', type: 'boolean', default: false, help: 'Only show URL' },
],
columns: ['status', 'file', 'size', 'scale', 'url'],
func: async (page, kwargs) => {
const scale = parseInt(kwargs.scale, 10);
log.status(`Upscaling ${scale}x...`);
const data = await yollomiPost(page, '/api/ai/image-upscaler', {
imageUrl: kwargs.image,
scale,
face_enhance: false,
});
const url = data.image || (data.images?.[0]);
if (!url)
throw new CliError('EMPTY_RESPONSE', 'No result', 'Check the input image');
if (kwargs['no-download'])
return [{ status: 'upscaled', file: '-', size: '-', scale: `${scale}x`, url }];
try {
const urlPath = (() => { try {
return new URL(url).pathname;
}
catch {
return url;
} })();
const ext = urlPath.endsWith('.png') || urlPath.endsWith('.webp') ? urlPath.slice(urlPath.lastIndexOf('.')) : '.jpg';
const filename = `yollomi_upscale_${scale}x_${Date.now()}${ext}`;
const { path: fp, size } = await downloadOutput(url, kwargs.output, filename);
if (data.remainingCredits !== undefined)
log.status(`Credits remaining: ${data.remainingCredits}`);
return [{ status: 'saved', file: path.relative('.', fp), size: fmtBytes(size), scale: `${scale}x`, url }];
}
catch {
return [{ status: 'download-failed', file: '-', size: '-', scale: `${scale}x`, url }];
}
},
});