videochat tool worker install

This commit is contained in:
Jeff Emmett 2024-12-08 18:32:39 -05:00
parent 2d562b3e4c
commit 4901a56d61
2 changed files with 25 additions and 25 deletions

View File

@ -82,12 +82,15 @@ export class VideoChatShape extends BaseBoxShapeUtil<IVideoChatShape> {
} }
try { try {
// First, request a room from our worker
const response = await fetch(`${WORKER_URL}/daily/rooms`, { const response = await fetch(`${WORKER_URL}/daily/rooms`, {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
// You might want to pass additional configuration if needed
body: JSON.stringify({ body: JSON.stringify({
name: `room-${shape.id}`, // Add a unique identifier
properties: { properties: {
enable_recording: true, enable_recording: true,
max_participants: 8, max_participants: 8,
@ -96,11 +99,13 @@ export class VideoChatShape extends BaseBoxShapeUtil<IVideoChatShape> {
}) })
if (!response.ok) { if (!response.ok) {
throw new Error("Failed to create room") const errorData = (await response.json()) as { message: string }
throw new Error(errorData.message || "Failed to create room")
} }
const data = await response.json() const data = await response.json()
// Update the shape with the room URL from the response
this.editor.updateShape<IVideoChatShape>({ this.editor.updateShape<IVideoChatShape>({
id: shape.id, id: shape.id,
type: "VideoChat", type: "VideoChat",
@ -111,6 +116,7 @@ export class VideoChatShape extends BaseBoxShapeUtil<IVideoChatShape> {
}) })
} catch (error) { } catch (error) {
console.error("Failed to create Daily room:", error) console.error("Failed to create Daily room:", error)
throw error // Re-throw to handle in the component
} }
} }

View File

@ -121,55 +121,49 @@ const router = AutoRouter<IRequest, [env: Environment, ctx: ExecutionContext]>({
.post("/daily/rooms", async (request, env) => { .post("/daily/rooms", async (request, env) => {
try { try {
// Log the request for debugging const { name, properties } = (await request.json()) as {
console.log( name: string
"Creating Daily room with API key:", properties: Record<string, unknown>
env.DAILY_API_KEY ? "present" : "missing", }
)
// Create a room using Daily.co API
const response = await fetch("https://api.daily.co/v1/rooms", { const response = await fetch("https://api.daily.co/v1/rooms", {
method: "POST", method: "POST",
headers: { headers: {
Authorization: `Bearer ${env.DAILY_API_KEY}`,
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: `Bearer ${env.DAILY_API_KEY}`,
}, },
body: await request.text(), body: JSON.stringify({
name,
properties,
}),
}) })
if (!response.ok) { if (!response.ok) {
const errorText = await response.text() const error = (await response.json()) as { message: string }
console.error("Daily API error:", errorText) return new Response(JSON.stringify({ message: error.message }), {
return new Response(`Daily API error: ${errorText}`, { status: 400,
status: response.status, headers: { "Content-Type": "application/json" },
}) })
} }
const data = await response.json() const data = await response.json()
// Log successful response
console.log("Daily room created:", data)
return new Response( return new Response(
JSON.stringify({ JSON.stringify({
...(data as Record<string, unknown>),
url: `https://${env.DAILY_DOMAIN}/${ url: `https://${env.DAILY_DOMAIN}/${
(data as Record<string, unknown>).name (data as Record<string, unknown>).name
}`, }`,
}), }),
{ {
headers: { headers: { "Content-Type": "application/json" },
"Content-Type": "application/json",
// Add CORS headers specifically for this endpoint if needed
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
},
}, },
) )
} catch (error) { } catch (error) {
console.error("Error creating Daily room:", error)
return new Response( return new Response(
JSON.stringify({ error: "Failed to create Daily room" }), JSON.stringify({
message: error instanceof Error ? error.message : "Unknown error",
}),
{ {
status: 500, status: 500,
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },