@yswgaicx/yswg-img-cli
v0.1.14
Published
CLI wrapper for YSWG Monkey Genius image generation.
Readme
yswg-img-cli
yswg-img wraps the YSWG Monkey Genius image generation page as a CLI.
Install
Install from npm on any machine with Node.js 22 or newer:
npm install -g @yswgaicx/yswg-img-cli@latest
yswg-img --helpUpgrade an existing global install:
npm install -g @yswgaicx/yswg-img-cli@latest
yswg-img version --jsonyswg-img checks npm for newer versions at most once every 12 hours and writes
upgrade hints to stderr, so --json stdout stays parseable. Disable this check
with --no-update-check or YSWG_IMG_CLI_NO_UPDATE_CHECK=1.
Install Locally
npm linkYou can also run it without linking:
node bin/yswg-img.js --helpLogin
Send the enterprise WeChat verification code:
yswg-img auth send-code --email zhangsanLogin with the 6-digit code:
yswg-img auth login --email zhangsan --code 123456The token is saved to ~/.yswg-img-cli/config.json.
Agent Usage
Use these rules when an AI agent calls the CLI:
- First run
yswg-img auth status --json. - If
authenticatedisfalse, runyswg-img auth send-code --email <email> --jsonand thenyswg-img auth login --email <email> --code <6 digits> --json. - Prefer
--jsonfor every command so downstream tools can parse stable output. - Use defaults unless the user explicitly asks otherwise:
groupId=6, modelgemini-3.1-flash-image-preview, ratio1:1, count1. - For one-shot generation, call:
yswg-img generate --prompt "一只可爱的白色兔子,粉色背景" --jsongenerateautomatically checks live model health before submitting. If the selected model is slower than 90 seconds on average or has poor health, the CLI switches to a healthier image model and returnsmodelSwitchin JSON output.If the user explicitly wants the selected model even when it is slow, add
--no-auto-switch.Read the local model cache without calling the backend:
yswg-img models --json- If model validation fails after backend permissions change, refresh the cache:
yswg-img models refresh --jsonIf the access token expires, authenticated backend commands automatically use the saved refresh token once, save the new session, and retry the original request.
Check whether the globally installed CLI is current:
yswg-img version --json- For up to 4 parallel images in one backend request, call:
yswg-img generate --prompt "四张电商产品场景图" --count 4 --json- For long-running jobs, wait for the command result. If it times out, recover it:
yswg-img tasks recover --task-id <task-id-from-json> --out outputs/recovered --json- For generated images, keep only local file paths and short summaries in agent
context. Do not paste image bytes, base64,
data:image/..., or full tool output into the conversation. - To fetch an Amazon product gallery by ASIN, call:
yswg-img amazon asin B0TEST1234 --max 7 --json- To call the backend SSE text stream endpoint, use:
yswg-img stream --prompt "用三句话总结这张图" --image-url https://example.com/a.png --jsonGenerate
Supported Monkey Genius options mirrored by the CLI:
- Model/group selection:
--group-id,--model - Model health protection: automatic live health check with
--no-auto-switchfor user-forced model selection - Aspect ratio:
--ratio 1:1|3:2|2:3|4:3|3:4|16:9|9:16|5:4|4:5|21:9 - Concurrent image output:
--count 1through--count 4when the selected model supports it - Reference images:
--ref ./a.png,https://example.com/b.jpg - Night/off-peak queue submission:
--night - AI tool/template payloads:
--template-code,--template-vars-json - Amazon ASIN product gallery lookup:
amazon asin <asin> --max 7 - Text / vision SSE invocation:
stream --prompt <text> - Generation history lookup and timeout recovery:
tasks search,tasks recover - History tabs/search/night-cancel:
tasks search --tab ...,tasks cancel-night
Known display-name to vendor-model mapping:
groupId=2046043157249933314displays as图灵二代and is sent asgpt-image-2.groupId=2046853405565095939displays as图灵二代高清and is sent asgpt-image-2-vip.groupId=2046853405565095940displays as图灵二代超清and is sent asgpt-image-2-vip.
The backend also overwrites params.model from the selected endpoint model, but
the CLI mirrors the vendor model in dry-run output to avoid misleading agents.
Model Health Switching
Before a real generate request, the CLI calls:
GET /prod-api/web/ai-group-metrics/realtimeThe response is parsed as a permission tree. Each model node is matched by
groupId, and the CLI reads metrics.windows with preference for the 1-minute
window, then 5/10/30/60 minutes.
Switching rules:
- Default threshold: average successful duration above
90000ms. - Health threshold: success rate below
80%. - Only image-generation models are candidates.
- Text models, video models, and exhausted models are excluded.
- Backend
remaining=-1is treated as unlimited capacity; only an explicitremaining=0is treated as exhausted. - Same-size models are preferred when possible.
- If the healthier candidate does not support the requested ratio or image count, the CLI keeps the original model and writes a warning to stderr.
- If the health endpoint fails, generation still proceeds with the originally selected model and writes a warning to stderr.
Force the exact selected model:
yswg-img generate --prompt "..." --group-id 6 --no-auto-switch --jsonWhen switching happens, JSON output includes:
{
"modelSwitch": {
"fromGroupId": "6",
"toGroupId": "5",
"reason": "平均耗时 120000ms 超过 90000ms"
}
}Local reference images are compressed before OSS upload by default, matching the Monkey Genius frontend behavior:
- Images below 50 KB are kept as-is.
- Images are resized to fit within 1280 x 1280.
- Opaque images are converted to JPEG at quality 0.82, then stepped down toward 0.5 until they are near 300 KB.
- Images with transparency are kept as PNG.
Use --no-compress only when the original file must be uploaded unchanged.
Dry-run the backend payload:
yswg-img generate \
--prompt "一张白底产品图,柔和自然光,高级电商摄影" \
--ratio 1:1 \
--count 1 \
--dry-run \
--jsonSubmit and wait for output images:
yswg-img generate \
--prompt "一张白底产品图,柔和自然光,高级电商摄影" \
--ratio 1:1 \
--count 4 \
--out outputs \
--jsonThe CLI caps one generation request at 4 images. When the backend returns multiple task IDs, the CLI waits for those tasks concurrently and downloads the result files by path.
Downloaded file names include the invoke task batch prefix, for example
2065019349651689473-image-01.png. Re-running generation or recovery in the
same --out directory will not overwrite existing files; repeated downloads add
a numeric suffix such as -2.
For image-heavy workflows, keep local file paths and short summaries in agent
context. Do not paste high-resolution image data, base64, data:image/..., or
full tool output into the conversation.
Use reference images:
yswg-img generate \
--prompt "参考产品主体,生成室内生活方式场景图" \
--ref ./product.png,https://example.com/ref.jpg \
--out outputs \
--jsonUse an AI tool/template payload:
yswg-img generate \
--group-id 6 \
--template-code tool_code \
--template-vars-json '{"color":"red"}' \
--display-prompt "AI 工具生成" \
--dry-run \
--jsonIf a long generation times out or the WebSocket disconnects, the CLI now checks generation history for the returned invoke task IDs before failing. This avoids submitting the same prompt again when the image was already produced.
Tune the wait timeout:
yswg-img generate --prompt "..." --timeout 900 --jsonQuery recent task history without creating a new generation task:
yswg-img tasks search --size 10 --keyword "兔子" --tab all --jsonThe CLI intentionally only queries the latest 10 records. If you need older records, open the navigation site and query them in the Monkey Genius page.
Available history tabs: all, expiring1d, expiring2d, nightQueue.
Fetch a history record by record ID:
yswg-img tasks get --id 2064983569742958594 --jsonRecover/download completed images by invoke task ID after a timeout:
yswg-img tasks recover \
--task-id 2065019349651689473 \
--out outputs/recovered \
--jsonCancel an off-peak queued task:
yswg-img tasks cancel-night --id <record-id> --jsonFetch Amazon product title and main image gallery by ASIN:
yswg-img amazon asin B0TEST1234 --max 7 --jsonThe command calls /prod-api/web/amazon/asin/{asin} and returns the backend
AmazonProductVo shape: asin, title, and images.
Stream
yswg-img stream calls the backend SSE endpoint:
POST /prod-api/web/ai/invoke/streamDefault text model: groupId=13 (gemini-3-flash-preview).
Machine-readable call:
yswg-img stream --prompt "写一段短文介绍天才猴子" --jsonContinue an existing conversation:
yswg-img stream \
--prompt "继续上一个回答,改成要点列表" \
--conversation-id <conversation-id> \
--jsonUse image URLs for vision input:
yswg-img stream \
--prompt "分析这两张图片的差异" \
--image-url https://example.com/a.png,https://example.com/b.png \
--jsonPass advanced backend params or prompt templates:
yswg-img stream \
--prompt "输出更短" \
--params-json '{"temperature":0.2}' \
--template-code image_analyze \
--template-vars-json '{"tone":"short"}' \
--jsonWith --json, stdout is a final summary object containing content,
conversationId, message IDs, and usage when the backend returns them. Without
--json, the CLI writes delta text as it arrives and appends one final newline.
Configuration
Runtime flags override environment variables, which override the saved config.
The app ID is the exception: it is fixed to the npm app and cannot be
overridden.
YSWG_TOKENYSWG_REFRESH_TOKENYSWG_BASE_URLYSWG_WS_PATHYSWG_IMG_CLI_NO_UPDATE_CHECK
Default app name: npm.
Default app ID: 2066371323654864898.
The app ID is fixed. --app-id and YSWG_APP_ID are intentionally rejected.
Default ratio: 1:1.
Default generation model: gemini-3.1-flash-image-preview (groupId=6, 三代).
Model Cache
yswg-img models --json reads local metadata from
~/.yswg-img-cli/model-cache.json. If that file does not exist, the CLI falls
back to built-in npm app defaults. This keeps generation validation fast and
stable for agents.
Run this only when backend model permissions or model metadata change:
yswg-img models refresh --jsonIf generation reports a model/group validation mismatch, refresh the cache before submitting the same task again.
Version Updates
Use yswg-img version --json for a machine-readable version check:
{
"currentVersion": "0.1.8",
"latestVersion": "0.1.9",
"updateAvailable": true,
"upgradeCommand": "npm install -g @yswgaicx/yswg-img-cli@latest"
}Use yswg-img upgrade --json to print the canonical upgrade command without
touching npm:
yswg-img upgrade --jsonThe CLI caches npm latest metadata in ~/.yswg-img-cli/update-check.json.
Regular commands emit update hints to stderr only, preserving stdout for JSON
parsers and agents.
Code Structure
bin/yswg-img.js: executable entrypoint only.src/cli.js: command dispatch, dependency injection, JSON output, and agent help text.src/stream.js: stream payload normalization and JSON flag parsing.src/generate.js: generation payloads, validation, history recovery, WebSocket wait, uploads, downloads.src/api.js: YSWG HTTP API client.src/config.js: config loading, env/flag precedence, email normalization.src/model-cache.js: built-in model metadata plus local cache refresh/read helpers.src/version-check.js: npm latest lookup, update cache, version comparison, upgrade command.src/args.js: minimal CLI argument parsing.src/image-compress.js: frontend-compatible reference image compression.
