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
78 changes: 78 additions & 0 deletions lib/agents/content/__tests__/postVideoResults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,82 @@ describe("postVideoResults", () => {
}),
);
});

it("posts static image before video when imageUrl is present", async () => {
const imgBuf = Buffer.from([0x10]);
const vidBuf = Buffer.from([0x20]);
mockedDownload
.mockResolvedValueOnce(imgBuf) // image download
.mockResolvedValueOnce(vidBuf); // video download

const videos = [
{
runId: "r1",
status: "completed" as const,
videoUrl: "https://cdn.example.com/v.mp4",
imageUrl: "https://cdn.example.com/static.png",
},
];

await postVideoResults(thread as never, videos, 0);

expect(thread.post).toHaveBeenCalledTimes(2);

// First call: image
expect(thread.post).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
markdown: "**Editorial Image**",
files: [expect.objectContaining({ mimeType: "image/png" })],
}),
);

// Second call: video
expect(thread.post).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
files: [expect.objectContaining({ mimeType: "video/mp4" })],
}),
);
});

it("falls back to URL text when image download fails", async () => {
mockedDownload
.mockResolvedValueOnce(null) // image download fails
.mockResolvedValueOnce(Buffer.from([0x20])); // video download

const videos = [
{
runId: "r1",
status: "completed" as const,
videoUrl: "https://cdn.example.com/v.mp4",
imageUrl: "https://cdn.example.com/static.png",
},
];

await postVideoResults(thread as never, videos, 0);

expect(thread.post).toHaveBeenNthCalledWith(
1,
expect.stringContaining("https://cdn.example.com/static.png"),
);
});

it("skips image posting when no imageUrl is present", async () => {
mockedDownload.mockResolvedValue(Buffer.from([0x01]));

const videos = [
{ runId: "r1", status: "completed" as const, videoUrl: "https://cdn.example.com/v.mp4" },
];

await postVideoResults(thread as never, videos, 0);

// Only the video post, no image
expect(thread.post).toHaveBeenCalledTimes(1);
expect(thread.post).toHaveBeenCalledWith(
expect.objectContaining({
files: [expect.objectContaining({ mimeType: "video/mp4" })],
}),
);
});
});
40 changes: 35 additions & 5 deletions lib/agents/content/postVideoResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface VideoResult {
runId: string;
status: string;
videoUrl?: string;
imageUrl?: string;
captionText?: string;
}

Expand All @@ -17,25 +18,54 @@ interface Thread {
}

/**
* Downloads completed videos in parallel and posts each to the thread.
* Downloads completed videos and static images in parallel and posts each to the thread.
* Falls back to posting the URL as text if a download fails.
*
* @param thread - The thread to post results to
* @param videos - Array of completed video results
* @param videos - Array of completed video results (may also contain imageUrl)
* @param failedCount - Number of failed runs to report
*/
export async function postVideoResults(
thread: Thread,
videos: VideoResult[],
failedCount: number,
): Promise<void> {
// Download all videos in parallel
const buffers = await Promise.all(videos.map(v => downloadVideoBuffer(v.videoUrl!)));
// Collect all URLs to download in parallel
const imageUrls = videos.map(v => v.imageUrl).filter(Boolean) as string[];
const videoUrls = videos.map(v => v.videoUrl!);
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.

P1: Custom agent: Enforce Clear Code Style and Maintainability Practices

Non-null assertion v.videoUrl! applied to every video, but videoUrl is optional. Now that imageUrl exists as an alternative, a result without videoUrl will pass undefined to downloadVideoBuffer and post literal "undefined" to Slack. Filter videoUrls the same way imageUrls is filtered, and use an index counter in the video-posting loop (as you already do for images).

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

<comment>Non-null assertion `v.videoUrl!` applied to every video, but `videoUrl` is optional. Now that `imageUrl` exists as an alternative, a result without `videoUrl` will pass `undefined` to `downloadVideoBuffer` and post literal `"undefined"` to Slack. Filter `videoUrls` the same way `imageUrls` is filtered, and use an index counter in the video-posting loop (as you already do for images).</comment>

<file context>
@@ -17,25 +18,54 @@ interface Thread {
-  const buffers = await Promise.all(videos.map(v => downloadVideoBuffer(v.videoUrl!)));
+  // Collect all URLs to download in parallel
+  const imageUrls = videos.map(v => v.imageUrl).filter(Boolean) as string[];
+  const videoUrls = videos.map(v => v.videoUrl!);
+
+  const [imageBuffers, videoBuffers] = await Promise.all([
</file context>
Fix with Cubic


const [imageBuffers, videoBuffers] = await Promise.all([
Promise.all(imageUrls.map(url => downloadVideoBuffer(url))),
Promise.all(videoUrls.map(url => downloadVideoBuffer(url))),
]);

// Post static images first (one per result that has imageUrl)
let imgIdx = 0;
for (const v of videos) {
if (!v.imageUrl) continue;
const imageBuffer = imageBuffers[imgIdx++];

if (imageBuffer) {
const filename = getFilenameFromUrl(v.imageUrl);
await thread.post({
markdown: "**Editorial Image**",
files: [
{
data: imageBuffer,
filename,
mimeType: "image/png",
},
],
});
} else {
await thread.post(`**Editorial Image:** ${v.imageUrl}`);
}
}

// Post each video sequentially (Slack ordering matters)
for (let i = 0; i < videos.length; i++) {
const v = videos[i];
const videoBuffer = buffers[i];
const videoBuffer = videoBuffers[i];

if (videoBuffer) {
const filename = getFilenameFromUrl(v.videoUrl!);
Expand Down
1 change: 1 addition & 0 deletions lib/agents/content/validateContentAgentCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const contentRunResultSchema = z.object({
runId: z.string(),
status: z.enum(["completed", "failed", "timeout"]),
videoUrl: z.string().optional(),
imageUrl: z.string().optional(),
captionText: z.string().optional(),
error: z.string().optional(),
});
Expand Down
Loading