File size: 2,112 Bytes
6fffa8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { GenerateNodeResponse } from "./canvas-model";

export type ParentContext = {
  id: string;
  title: string;
  query: string;
  imagePrompt: string;
  summary?: string;
  visualFacts?: Array<{
    label: string;
    detail: string;
  }>;
};

export type GenerateNodeInput = {
  query: string;
  branchPrompt?: string;
  imageBase64?: string;
  parent?: ParentContext;
};

export type GenerateNodePayload = {
  query: string;
  branchPrompt?: string;
  imageBase64?: string;
  parentNodeId?: string;
  parentContext?: Omit<ParentContext, "id">;
};

export function buildGenerateNodePayload(input: GenerateNodeInput): GenerateNodePayload {
  const payload: GenerateNodePayload = {
    query: input.query,
  };

  if (input.branchPrompt) {
    payload.branchPrompt = input.branchPrompt;
  }
  if (input.imageBase64) {
    payload.imageBase64 = input.imageBase64;
  }
  if (input.parent) {
    payload.parentNodeId = input.parent.id;
    const parentContext: Omit<ParentContext, "id"> = {
      title: input.parent.title,
      query: input.parent.query,
      imagePrompt: input.parent.imagePrompt,
    };
    if (input.parent.summary) {
      parentContext.summary = input.parent.summary;
    }
    if (input.parent.visualFacts) {
      parentContext.visualFacts = input.parent.visualFacts;
    }
    payload.parentContext = parentContext;
  }

  return payload;
}

export async function generateVeniceNode(input: GenerateNodeInput): Promise<GenerateNodeResponse> {
  const baseUrl = process.env.NEXT_PUBLIC_VENICE_API_URL || "";
  const response = await fetch(`${baseUrl}/api/generate-node`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(buildGenerateNodePayload(input)),
  });

  if (!response.ok) {
    const detail = await response.text();
    throw new Error(detail || `Generation failed with status ${response.status}`);
  }

  const payload = (await response.json()) as Omit<GenerateNodeResponse, "query">;
  return {
    ...payload,
    query: input.branchPrompt ? `${input.query} - ${input.branchPrompt}` : input.query,
  };
}