-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrenderItem.ts
More file actions
204 lines (188 loc) · 6.12 KB
/
renderItem.ts
File metadata and controls
204 lines (188 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { z } from "zod";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { getRenderableItemsDetails, renderItem } from "../sdk";
export function registerRenderItem(server: McpServer) {
const Input = {
isDesign: z
.boolean()
.describe(
"True when the parent is a Design; false when it is a Project."
),
projectDesignId: z
.string()
.describe("Parent identifier (projectId or designId)."),
templateVariantId: z
.string()
.describe(
"Template/variant identifier (the renderable leaf under the parent)."
),
parameters: z
.record(z.any())
.default({})
.describe(
"Key-value parameters required by the chosen template/variant to customize the render. Mandatory parameters must be provided. Parameter type must be respected."
),
};
const Output = {
id: z.string().optional().describe("Server-assigned render job ID."),
state: z
.enum([
"PENDING",
"THROTTLED",
"QUEUED",
"IN_PROGRESS",
"DONE",
"FAILED",
"INVALID",
"CANCELLED",
])
.optional()
.describe("Current state of the render job."),
output: z
.string()
.nullable()
.optional()
.describe("URL to the rendered video, if state is DONE."),
error: z
.record(z.any())
.nullable()
.optional()
.describe("Error details, if state is FAILED or INVALID."),
};
server.registerTool(
"render_item",
{
title: "Render Item",
description: `
Create a render for a selected Project template or Design variant with specified parameters.
How to use:
- Call this after the user selects a candidate from \`get_renderable_items_details\`.
- Call this only once the user approved all parameters for the chosen template/variant.
Guidance:
- Use parameters to customize the render.
- All mandatory parameters must be provided.
- Provide values for optional parameters if it makes sense.
- Parameter types must be respected:
- STRING: text string relevant to the parameter context.
- MEDIA: URL to a media file (image, audio, or video). Ensure the URL is publicly accessible and points directly to the media file.
- MEDIA (image): URL to an image file (jpg, png, etc.).
- MEDIA (audio): URL to an audio file (mp3, wav, etc.).
- MEDIA (video): URL to a video file (mp4, mov, etc.).
- COLOR: hex color code (e.g. #FF5733).
- If a parameter has a default value and the user does not provide a value, the default will be used.
- If the user is unsure about a parameter, ask for clarification rather than guessing.
- When referencing parameters in conversation, use their \`label\` or \`description\` for clarity.
Use when:
- The user wants to create a video from a specific template/variant with defined parameters.
`,
inputSchema: Input,
outputSchema: Output,
},
async ({ isDesign, projectDesignId, templateVariantId, parameters }) => {
// TODO: Handle object parameters "my.parameter.x"
try {
// Validate that the chosen project / design exists
const projectDesignItems = await getRenderableItemsDetails(
projectDesignId,
isDesign
);
if (projectDesignItems.length === 0) {
return {
content: [
{
type: "text",
text: `Could not find a ${
isDesign ? "design" : "project"
} with id ${projectDesignId} .`,
},
],
};
}
// Validate that the chosen template / variant exists
const renderableItem = projectDesignItems.find(
(item) => item.templateVariantId === templateVariantId
);
if (!renderableItem) {
return {
content: [
{
type: "text",
text: `Could not find a ${
isDesign ? "variant" : "template"
} with id ${templateVariantId} under the specified ${
isDesign ? "design" : "project"
} (${projectDesignId}).`,
},
],
structuredContent: {},
isError: true,
};
}
// Validate parameters
const mandatoryParams = renderableItem.parameters.filter(
(p) => p.mandatory
);
const providedParams = Object.keys(parameters);
const missingParams = mandatoryParams.filter(
(p) => !providedParams.includes(p.key)
);
if (missingParams.length > 0) {
return {
content: [
{
type: "text",
text: `Missing required parameters: ${missingParams
.map((p) => p.key)
.join(", ")}.`,
},
],
structuredContent: {},
isError: true,
};
}
// If everything looks good, submit the render
const render = await renderItem({
isDesign,
projectDesignId,
templateVariantId,
parameters,
});
return {
content: [
{
type: "text",
text: `🚀 Render submitted successfully!
**Render ID:** ${render.id}
**Parameters Used:**
${Object.entries(parameters)
.map(([key, value]) => {
const param = renderableItem.parameters.find((p) => p.key === key);
return `• ${param?.label || key}: ${value}`;
})
.join("\n")}
The render is being processed. Use the render ID to check status and retrieve the final video when complete.`,
},
],
structuredContent: {
id: render.id,
state: render.state ?? "QUEUED",
output: render.output ?? null,
error: null,
},
};
} catch (err: any) {
// Handle API errors gracefully
return {
content: [
{
type: "text",
text: `Failed to create render: ${err}`,
},
],
structuredContent: {},
isError: true,
};
}
}
);
}