| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- /**
- * @node sdcpp-txt2vid
- * @name SD.cpp Text to Video
- * @category sdcpp
- * @version 1.0.0
- * @description Queue a video generation, for video models such as Wan, LTXAV and HunyuanVideo
- * @icon clapperboard
- */
- // The credential this node wants, named so it can be found. It is stored as a
- // plain basic credential - that is what decides how it is encrypted - and this
- // only says which basic credential is the SD.cpp one. Anything that accepts a
- // basic credential still accepts this, and this node still accepts a plain
- // basic credential, because the shape is identical.
- const credentialTypes = [
- {
- id: 'sdcpp',
- label: 'SD.cpp Server',
- baseType: 'basic',
- description: 'The username and password you sign in to sdcpp-restapi with. The node exchanges them for a token before every call',
- usernameLabel: 'Username',
- passwordLabel: 'Password'
- }
- ];
- const configSchema = {
- type: 'object',
- properties: {
- serverUrl: {
- type: 'string', title: 'Server URL',
- description: 'Base address of the sdcpp-restapi server',
- default: 'http://localhost:8077'
- },
- credentialId: {
- type: 'string', title: 'Credential',
- description: 'A basic credential holding the sdcpp-restapi username and password',
- dynamicOptions: { source: 'credentials', filter: { type: ['sdcpp', 'basic'] } }
- },
- prompt: {
- type: 'string', title: 'Prompt',
- description: 'What to generate. Supports {{variable}} interpolation, <lora:name:weight>, and {a|b|c} dynamic prompts which the server expands into one queue item per variation',
- format: 'textarea'
- },
- negativePrompt: {
- type: 'string', title: 'Negative Prompt',
- description: 'Concepts to exclude. Has no effect on LCM and one-step SDXS models',
- format: 'textarea'
- },
- initImageBase64: {
- type: 'string', title: 'First Frame (base64)',
- description: 'Optional image to start the video from'
- },
- videoFrames: { type: 'number', title: 'Frames', description: 'How many frames to render' },
- fps: { type: 'number', title: 'FPS', description: 'Frames per second of the output' },
- strength: { type: 'number', title: 'Strength', description: 'How far the result may move from the first frame, 0 to 1' },
- width: { type: 'number', title: 'Width', description: 'Leave empty to use the loaded architecture default' },
- height: { type: 'number', title: 'Height', description: 'Leave empty to use the loaded architecture default' },
- steps: { type: 'number', title: 'Steps', description: 'Leave empty for the architecture default - 4 for Flux Schnell and LCM, 20 for most others' },
- cfgScale: { type: 'number', title: 'CFG Scale', description: 'Leave empty for the architecture default. Distilled models such as Flux Schnell, LCM and Z-Image want 1.0' },
- sampler: {
- type: 'string', title: 'Sampler',
- enum: ['', 'euler', 'euler_a', 'heun', 'dpm2', 'dpm++2s_a', 'dpm++2m', 'dpm++2mv2',
- 'ipndm', 'ipndm_v', 'lcm', 'ddim_trailing', 'tcd', 'res_multistep', 'res_2s',
- 'er_sde', 'euler_cfg_pp', 'euler_a_cfg_pp', 'euler_ge'],
- default: '', description: 'Leave empty to use the architecture default'
- },
- scheduler: {
- type: 'string', title: 'Scheduler',
- enum: ['', 'discrete', 'karras', 'exponential', 'ays', 'gits', 'sgm_uniform',
- 'simple', 'smoothstep', 'kl_optimal', 'lcm', 'bong_tangent', 'ltx2'],
- default: '', description: 'Leave empty to use the architecture default'
- },
- seed: { type: 'number', title: 'Seed', description: 'Use -1 for a random seed. Any other value reproduces the same image' },
- batchCount: { type: 'number', title: 'Batch Count', description: 'How many to generate in this one job, all sharing the prompt' },
- clipSkip: { type: 'number', title: 'CLIP Skip', description: 'Skip the last N CLIP layers. Negative means the model default' },
- title: { type: 'string', title: 'Job Title', description: 'Optional label stored with the job, useful for finding it again in the queue' },
- extraOptions: {
- type: 'object', title: 'Extra Options',
- description: 'Any other generation field passed straight through, such as slg_scale, cache_mode or vae_tiling. See /options/generation on the server for the full list'
- },
- timeout: {
- type: 'number', title: 'Timeout (ms)',
- description: 'Applies to queueing the job, not to the render. The call returns as soon as the job is accepted',
- default: 30000
- }
- },
- required: ['credentialId']
- };
- const inputSchema = { type: 'object', properties: { data: { type: 'any' } } };
- const outputSchema = {
- type: 'object',
- properties: {
- jobId: { type: 'string', description: 'Id of the queued job, to pass to SD.cpp Wait For Job' },
- status: { type: 'string', description: 'Queue status when the job was accepted, normally pending' },
- position: { type: 'number', description: 'Place in the queue' },
- request: { type: 'object', description: 'The body actually sent, useful for seeing which defaults were left to the server' }
- }
- };
- function normalizeServer(url) {
- const value = String(url || '').trim();
- if (!value) {
- throw new Error('SD.cpp: a server URL is required, such as http://localhost:8077');
- }
- return value.replace(/\/+$/, '');
- }
- function readCredential(credentialId) {
- const auth = smartbotic.credentials.get(credentialId);
- if (!auth || auth.success !== true) {
- throw new Error('SD.cpp: could not read the credential: ' +
- ((auth && auth.error) || 'unknown error'));
- }
- const value = auth.headerValue || '';
- if (value.indexOf('Basic ') !== 0) {
- throw new Error('SD.cpp: the credential must be a basic one, holding the sdcpp-restapi ' +
- 'username and password');
- }
- const decoded = smartbotic.utils.base64Decode(value.substring(6));
- const separator = decoded.indexOf(':');
- if (separator < 1) {
- throw new Error('SD.cpp: the credential is malformed, expected a username and a password');
- }
- return {
- username: decoded.substring(0, separator),
- password: decoded.substring(separator + 1)
- };
- }
- function call(options) {
- const response = smartbotic.http.request(options);
- let body = response.data;
- if (typeof body === 'string' && body.length > 0) {
- try {
- body = JSON.parse(body);
- } catch (e) {
- const snippet = body.substring(0, 200).replace(/\s+/g, ' ');
- throw new Error('SD.cpp: ' + options.what + ' returned HTTP ' + response.status +
- ' with a body that is not JSON: ' + snippet);
- }
- }
- if (response.status < 200 || response.status >= 300) {
- const detail = (body && (body.message || body.error)) || ('HTTP ' + response.status);
- throw new Error('SD.cpp: ' + options.what + ' failed: ' + detail);
- }
- return body || {};
- }
- function login(server, credential, timeout) {
- const session = call({
- method: 'POST',
- url: server + '/auth/login',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({
- username: credential.username,
- password: credential.password
- }),
- timeout: timeout,
- what: 'signing in'
- });
- if (!session.token) {
- throw new Error('SD.cpp: the server accepted the login but returned no token');
- }
- return session.token;
- }
- // /health is unauthenticated, and it is the only way to find out what is
- // already loaded without asking for a token first.
- function readHealth(server, timeout) {
- return call({
- method: 'GET',
- url: server + '/health',
- timeout: timeout,
- what: 'reading server health'
- });
- }
- function putIfSet(target, key, value) {
- if (value === undefined || value === null || value === '') {
- return;
- }
- target[key] = value;
- }
- // Width, steps, cfg_scale and the rest are architecture defaults: the server
- // fills them from the loaded model's preset when the field is absent. Sending an
- // empty box as 0 would override that preset with nonsense, so a field the user
- // did not fill is left out of the body entirely - and the test is emptiness,
- // never truthiness, because seed 0 and clip_skip 0 are legitimate values.
- async function execute(config, input, context) {
- const server = normalizeServer(config.serverUrl);
- const timeout = config.timeout || 30000;
- const credential = readCredential(config.credentialId);
- const token = login(server, credential, timeout);
- const body = {};
- putIfSet(body, 'prompt', config.prompt);
- if (!body.prompt) {
- throw new Error('SD.cpp: txt2vid needs a prompt');
- }
- putIfSet(body, 'negative_prompt', config.negativePrompt);
- putIfSet(body, 'width', config.width);
- putIfSet(body, 'height', config.height);
- putIfSet(body, 'steps', config.steps);
- putIfSet(body, 'cfg_scale', config.cfgScale);
- putIfSet(body, 'sampler', config.sampler);
- putIfSet(body, 'scheduler', config.scheduler);
- putIfSet(body, 'seed', config.seed);
- putIfSet(body, 'batch_count', config.batchCount);
- putIfSet(body, 'clip_skip', config.clipSkip);
- putIfSet(body, 'init_image_base64', config.initImageBase64);
- putIfSet(body, 'strength', config.strength);
- putIfSet(body, 'video_frames', config.videoFrames);
- putIfSet(body, 'fps', config.fps);
- putIfSet(body, 'title', config.title);
- // Anything else the API accepts, passed through, so a new server field does
- // not need a node change to be reachable.
- const extra = config.extraOptions;
- if (extra && typeof extra === 'object') {
- const keys = Object.keys(extra);
- for (let i = 0; i < keys.length; i++) {
- putIfSet(body, keys[i], extra[keys[i]]);
- }
- }
- const queued = call({
- method: 'POST',
- url: server + '/txt2vid',
- headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
- body: JSON.stringify(body),
- timeout: timeout,
- what: 'queueing the txt2vid job'
- });
- if (!queued.job_id) {
- throw new Error('SD.cpp: the job was accepted but no job id came back');
- }
- smartbotic.log.info('SD.cpp: queued txt2vid job ' + queued.job_id);
- return {
- jobId: queued.job_id,
- status: queued.status || 'pending',
- position: queued.position !== undefined ? queued.position : -1,
- request: body
- };
- }
- module.exports = { configSchema, inputSchema, outputSchema, execute };
|