Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
/**
* Creative Director — produce a video from a pipeline issue (CDO Phase 3, #2185).
*
* The autopilot/pipeline → Creative Director direction of the bridge: take a
* finished pipeline issue (its prose / comic script + the series context) and
* mint a FRESH Creative Director video project — a teaser/trailer — seeded from
* it, then start it. This is the #842 write pattern (createProject + setTreatment
* + link, never clobber an existing project) applied to a pipeline issue instead
* of a writers-room work.
*
* Conductor, not re-implementation:
* - Treatment is generated by the SAME `writers-room-cd-bridge` stage +
* `shapeProposal` shaper the WR→CD send path uses (imported from
* liveDirector.js) — the treatment-from-prose contract lives in one place.
* - Casting reuses `applyAutoCastToProject` so the series' catalog ingredients
* (canon characters/places/objects) are pulled into CD casting AND linked as
* `ref_kind: 'creative-director'` refs, exactly like the manual remix path —
* autonomous and manual paths converge on the same ingredients.
* - The project is STARTED via `startCreativeDirectorProject` (the same entry
* the Start button and the plan-advance loop call).
*
* `startCreativeDirectorProject` is imported dynamically inside the function to
* keep the module graph acyclic: it pulls in the CD plan-advance loop → the
* creative tool registry → the pipeline tools → seriesAutopilot, and this module
* is itself reachable from seriesAutopilot's teaser step.
*/
import { runStagedLLM } from '../stageRunner.js';
import {
CD_BRIDGE_STAGE,
shapeProposal,
bridgeScenesToTreatmentScenes,
} from '../writersRoom/liveDirector.js';
import { getIssue } from '../pipeline/issues.js';
import { getSeries } from '../pipeline/series.js';
import { defaultVideoModelId } from '../videoGen/local.js';
import { createProject, setTreatment, deleteProject } from './local.js';
import { applyAutoCastToProject } from './autoCast.js';
import { deleteCollection } from '../mediaCollections.js';
import { ServerError } from '../../lib/errorHandler.js';
// Cap the source prose fed to the treatment stage — a full 24-page script would
// blow the context window; the CD bridge only needs enough to shape a teaser.
const SOURCE_PROSE_MAX = 12000;
// Prefer the finished prose draft as the teaser's source (it's continuous
// narrative the treatment stage reads best); fall back to the comic script, then
// the beat sheet. Returns '' when the issue carries no usable text.
function issueSourceProse(issue) {
const stages = issue?.stages || {};
const text = stages.prose?.output || stages.comicScript?.output || stages.idea?.output || '';
const trimmed = typeof text === 'string' ? text.trim() : '';
return trimmed.length > SOURCE_PROSE_MAX ? trimmed.slice(0, SOURCE_PROSE_MAX) : trimmed;
}
// Assemble a casting brief from the series + issue context so autoCast's hybrid
// search pulls the right canon ingredients. Series premise + issue title/logline
// name the characters/places the teaser should feature.
function deriveTeaserBrief(series, issue, proposal) {
return [series?.name, series?.premise, issue?.title, proposal?.logline, proposal?.synopsis]
.filter((v) => typeof v === 'string' && v.trim())
.map((v) => v.trim())
.join('\n\n');
}
/**
* Produce a Creative Director teaser/trailer video project from a pipeline issue.
*
* Steps: load the issue + series → generate a treatment proposal from the issue's
* prose via the CD-bridge stage → mint a fresh CD project (seeded with
* `sourceIssueId` so the stitch step mixes in the issue's audio-stage music) →
* apply the treatment → auto-cast the series canon → start it. The multi-step
* write rolls back the orphaned project + its media collection on a failure after
* create (mirrors sendToCreativeDirector's orphan-cleanup).
*
* @param {string} issueId
* @param {{ providerDefault?: string, modelDefault?: string, effortDefault?: string, name?: string,
* aspectRatio?: string, quality?: string, targetDurationSeconds?: number, modelId?: string,
* renderBackend?: object }} [options]
* @returns {Promise<{ project: object, proposal: object }>}
*/
export async function produceVideoFromIssue(issueId, options = {}) {
const issue = await getIssue(issueId);
if (!issue) throw new ServerError('Issue not found', { status: 404, code: 'NOT_FOUND' });
const series = await getSeries(issue.seriesId).catch(() => null);
const before = issueSourceProse(issue);
if (!before) {
throw new ServerError(
'Issue has no prose / script to build a teaser from — generate its text stages first.',
{ status: 400, code: 'NO_SOURCE_PROSE' },
);
}
// Reuse the SAME CD-bridge stage the writers-room send path uses. `work`
// carries the series/issue framing; the issue prose is the cursor context.
const variables = {
work: {
title: issue.title || series?.name || 'Untitled',
kind: 'comic',
status: 'ready',
},
before,
after: '',
selection: '',
returnsJson: true,
};
const stageOpts = { source: 'pipeline-cd-teaser', returnsJson: true };
if (options.providerDefault) stageOpts.providerDefault = options.providerDefault;
if (options.modelDefault) stageOpts.modelDefault = options.modelDefault;
if (options.effortDefault) stageOpts.effortDefault = options.effortDefault;
const { content } = await runStagedLLM(CD_BRIDGE_STAGE, variables, stageOpts);
const proposal = shapeProposal(content);
if (!proposal) {
throw new ServerError(
'The model could not produce a usable teaser treatment from this issue.',
{ status: 422, code: 'NO_TREATMENT' },
);
}
const scenes = bridgeScenesToTreatmentScenes(proposal.scenes);
// Mint the fresh project. sourceIssueId links it back to the issue (and feeds
// the stitch step's music mux). CD render defaults match the New-Project form.
const project = await createProject({
name: options.name || `${issue.title || series?.name || 'Issue'} — Teaser`,
aspectRatio: options.aspectRatio || '16:9',
quality: options.quality || 'standard',
modelId: options.modelId || defaultVideoModelId(),
targetDurationSeconds: options.targetDurationSeconds || 60,
styleSpec: proposal.styleSpec || '',
sourceIssueId: issueId,
// Carry the caller's render-backend pin (#3135) onto the teaser. The teaser
// is a SEPARATE CD project, so a creative commission that reaches this tool
// from its plan would otherwise render the run's actual deliverable on the
// install default while the commission says "Grok" — silently defeating the
// pin at the project boundary. Absent ⇒ null, i.e. today's behavior for the
// manual writers-room / pipeline send paths.
renderBackend: options.renderBackend || null,
// Same boundary problem as the render pin above, for the OTHER half: the
// teaser is a separate project, so without the owning commission's id it is
// invisible to that commission's stop and its live provider resolution — a
// paused or deleted commission would leave this project running. Absent ⇒
// null, i.e. today's behavior for the manual writers-room / pipeline paths.
commissionId: options.commissionId || null,
});
let started;
try {
started = await setTreatment(project.id, {
logline: proposal.logline,
synopsis: proposal.synopsis,
scenes,
});
// Shared cast/canon (#2185): pull the series' catalog ingredients into CD
// casting + write creative-director refs so autonomous and manual paths
// converge on the same ingredients. Best-effort — the treatment already
// landed, and a series with no cataloged canon simply adds nothing.
const brief = deriveTeaserBrief(series, issue, proposal);
if (brief) {
await applyAutoCastToProject(project.id, { brief })
.then((r) => {
if (r?.added?.length) console.log(`🎬 Teaser auto-cast: ${r.added.length} ingredient(s) → CD project ${project.id}`);
})
.catch((err) => console.error(`❌ Teaser auto-cast failed for CD project ${project.id}: ${err.message}`));
}
// Explicitly start it (the #842 rule: a bridged record's owning step starts
// it — nothing auto-starts). Imported here to keep the module graph acyclic.
const { startCreativeDirectorProject } = await import('./completionHook.js');
await startCreativeDirectorProject(project.id);
} catch (err) {
// Roll back the orphaned project + its auto-created media collection so a
// setTreatment/start failure doesn't leave a treatment-less shell behind.
await deleteProject(project.id).catch(() => {});
if (project.collectionId) await deleteCollection(project.collectionId).catch(() => {});
throw err;
}
console.log(`🎬 Produced CD teaser project ${project.id} from issue ${issueId}`);
return { project: started || project, proposal };
}