Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions lib/agents/content/createContentIntentAgent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Output, ToolLoopAgent, stepCountIs } from "ai";
import { z } from "zod";
import { LIGHTWEIGHT_MODEL } from "@/lib/const";

export const contentIntentSchema = z.object({
action: z
.enum(["edit", "generate"])
.describe(
'Whether the user wants to edit/modify the existing content ("edit") or create entirely new content from scratch ("generate"). Use "edit" when the user references changing, adjusting, trimming, cropping, resizing, or adding overlays to the existing video. Use "generate" when the user wants something completely new or different.',
),
});

export type ContentIntent = z.infer<typeof contentIntentSchema>;

const instructions = `You classify whether a user's message in a content thread is requesting an edit to existing content or a brand new generation.

Context: The user previously generated content (videos/images) in this Slack thread. They are now replying with a new request. You must decide if they want to modify what was already created or start fresh.

Classify as "edit" when the user wants to:
- Trim, shorten, or change the duration
- Crop or change the aspect ratio
- Resize the video
- Add or change text overlays / captions
- Make adjustments to the existing content
- Phrases like "make it shorter", "add text", "crop to square", "resize to 1080x1920"

Classify as "generate" when the user wants to:
- Create entirely new content with a different template, artist, or style
- Generate more videos
- Phrases like "make another one", "try a different template", "generate 3 more"

When in doubt, prefer "edit" since the user is replying in an existing content thread.`;

/**
* Creates a ToolLoopAgent that classifies user intent as "edit" or "generate"
* based on a message in an existing content thread.
*
* @returns A configured ToolLoopAgent for intent classification.
*/
export function createContentIntentAgent() {
return new ToolLoopAgent({
model: LIGHTWEIGHT_MODEL,
instructions,
output: Output.object({ schema: contentIntentSchema }),
stopWhen: stepCountIs(1),
});
}
87 changes: 87 additions & 0 deletions lib/agents/content/createEditOperationsAgent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { Output, ToolLoopAgent, stepCountIs } from "ai";
import { z } from "zod";
import { LIGHTWEIGHT_MODEL } from "@/lib/const";
import { TEMPLATE_IDS } from "@/lib/content/templates";

const editOperationSchema = z.discriminatedUnion("type", [
z.object({
type: z.literal("trim"),
start: z.number().nonnegative().describe("Start time in seconds."),
duration: z.number().positive().describe("Duration in seconds."),
}),
z.object({
type: z.literal("crop"),
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: crop operations can validate without any crop parameters, allowing invalid edit payloads to be sent to the ffmpeg edit task.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At lib/agents/content/createEditOperationsAgent.ts, line 13:

<comment>`crop` operations can validate without any crop parameters, allowing invalid edit payloads to be sent to the ffmpeg edit task.</comment>

<file context>
@@ -0,0 +1,87 @@
+    duration: z.number().positive().describe("Duration in seconds."),
+  }),
+  z.object({
+    type: z.literal("crop"),
+    aspect: z.string().optional().describe('Aspect ratio like "9:16", "1:1", "16:9".'),
+    width: z.number().int().positive().optional(),
</file context>
Fix with Cubic

aspect: z.string().optional().describe('Aspect ratio like "9:16", "1:1", "16:9".'),
width: z.number().int().positive().optional(),
height: z.number().int().positive().optional(),
}),
z.object({
type: z.literal("resize"),
width: z.number().int().positive().optional(),
height: z.number().int().positive().optional(),
}),
z.object({
type: z.literal("overlay_text"),
content: z.string().min(1).describe("The text to overlay on the video."),
color: z.string().optional().default("white"),
stroke_color: z.string().optional().default("black"),
max_font_size: z.number().positive().optional().default(42),
position: z.enum(["top", "center", "bottom"]).optional().default("bottom"),
}),
]);

export const editOperationsResultSchema = z.object({
template: z
.enum(TEMPLATE_IDS)
.optional()
.describe(
"If the user wants to apply a named template instead of explicit operations, set this. Otherwise omit.",
),
operations: z
.array(editOperationSchema)
.describe(
"Ordered list of edit operations to apply. Empty array if a template is used instead.",
),
});

export type EditOperationsResult = z.infer<typeof editOperationsResultSchema>;

const templateList = TEMPLATE_IDS.map(id => `- "${id}"`).join("\n");

const instructions = `You extract video edit operations from a natural-language request.

The user wants to modify an existing video. Parse their request into a list of edit operations.

Available operations:
- "trim": Cut the video. Requires start (seconds) and duration (seconds).
- "crop": Change aspect ratio or dimensions. Use aspect (e.g. "9:16") or width/height.
- "resize": Change output dimensions. Provide width and/or height.
- "overlay_text": Add text on top of the video. Provide the text content, position (top/center/bottom), and optionally color.

Available templates (use instead of operations when user references a template name):
${templateList}

If the user mentions a template name, set "template" and leave "operations" empty.
Otherwise, extract explicit operations from the request.

Examples:
- "make it 10 seconds" → trim with start=0, duration=10
- "crop to square" → crop with aspect="1:1"
- "add 'New Release' text at the top" → overlay_text with content="New Release", position="top"
- "resize to 1080x1920" → resize with width=1080, height=1920
- "apply the bedroom template" → template="artist-caption-bedroom"`;

/**
* Creates a ToolLoopAgent that parses natural-language edit instructions
* into structured ffmpeg operations.
*
* @returns A configured ToolLoopAgent for edit operations parsing.
*/
export function createEditOperationsAgent() {
return new ToolLoopAgent({
model: LIGHTWEIGHT_MODEL,
instructions,
output: Output.object({ schema: editOperationsResultSchema }),
stopWhen: stepCountIs(1),
});
}
8 changes: 7 additions & 1 deletion lib/agents/content/handleContentAgentCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ export async function handleContentAgentCallback(request: Request): Promise<Next
await thread.post("Content generation finished but no videos were produced.");
}

await thread.setState({ status: "completed" });
// Persist generated URLs so thread replies can reference them for edits
const videoUrls = videos.map(v => v.videoUrl).filter(Boolean) as string[];

await thread.setState({
status: "completed",
...(videoUrls.length > 0 && { videoUrls }),
});
break;
}

Expand Down
Loading
Loading