/** Node 22+, no dependencies. ACTION=me|search|download|image */ import fs from 'node:fs/promises'; const base=(process.env.MOTIONGIT_BASE_URL??'http://127.0.0.1:8095').replace(/\/$/,''); const key=process.env.MOTIONGIT_API_KEY;if(!key)throw Error('Set MOTIONGIT_API_KEY'); const action=process.env.ACTION??'me'; const routes={me:'/api/v1/me',search:'/api/v1/stickers/search?q='+encodeURIComponent(process.env.QUERY??'增长')+'&limit=5',download:'/api/v1/stickers/'+encodeURIComponent(process.env.PRESET??'growth'),image:'/api/v1/images'}; if(!Object.hasOwn(routes,action))throw Error('Unknown ACTION'); const res=await fetch(base+routes[action],{headers:{authorization:'Bearer '+key,'content-type':'application/json'},...(action==='image'?{method:'POST',body:JSON.stringify({prompt:process.env.PROMPT??'清晨城市天际线',size:'1536x1024'})}:{})}); const json=await res.json();console.log(JSON.stringify({status:res.status,body:json},null,2)); if(!res.ok){if(res.status===503&&json.error?.code==='not_enabled')console.log('生图内测未启用,本次没有扣点。');else process.exitCode=1;} else if(action==='download')await fs.writeFile(process.env.OUTPUT??'sticker.json',JSON.stringify(json)); else if(action==='image'){ const asset=new URL(json.url);if(asset.origin!==new URL(base).origin)throw Error('Unexpected asset origin'); const image=await fetch(asset,{headers:{authorization:'Bearer '+key}});if(!image.ok)throw Error('Asset download failed'); await fs.writeFile(process.env.OUTPUT??'generated.png',Buffer.from(await image.arrayBuffer())); }