-
Notifications
You must be signed in to change notification settings - Fork 9
feat: thread reply edit vs generate logic (REC-68) #439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
recoup-coding-agent
wants to merge
1
commit into
test
Choose a base branch
from
feature/rec-68-thread-reply-edit-logic
base: test
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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), | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"), | ||
| 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), | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2:
cropoperations can validate without any crop parameters, allowing invalid edit payloads to be sent to the ffmpeg edit task.Prompt for AI agents