caiosilva1221 commited on
Commit
261a335
verified
1 Parent(s): 0e737c6

Update src/components/ask-ai/ask-ai.tsx

Browse files
Files changed (1) hide show
  1. src/components/ask-ai/ask-ai.tsx +19 -36
src/components/ask-ai/ask-ai.tsx CHANGED
@@ -42,28 +42,16 @@ function AskAI({
42
  try {
43
  onNewPrompt(prompt);
44
 
45
- const request = await fetch("https://openrouter.ai/api/v1/chat/completions", {
46
  method: "POST",
47
  headers: {
48
- "Authorization": "Bearer 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",
49
  "Content-Type": "application/json",
50
- "HTTP-Referer": "https://raypages.com", // ou o seu dom铆nio
51
- "X-Title": "RayPages AI"
52
  },
53
  body: JSON.stringify({
54
- model: "deepseek-chat-v3.0",
55
- stream: true,
56
- messages: [
57
- {
58
- role: "system",
59
- content: "Voc锚 茅 um especialista em landing pages. Gere um HTML bonito e persuasivo para a copy enviada. Responda apenas com c贸digo HTML v谩lido e completo."
60
- },
61
- {
62
- role: "user",
63
- content: prompt
64
- }
65
- ]
66
- })
67
  });
68
 
69
  if (!request.ok || !request.body) {
@@ -92,26 +80,20 @@ function AskAI({
92
  }
93
 
94
  const chunk = decoder.decode(value, { stream: true });
95
- const matches = chunk.match(/"content":"([^"]*)"/g);
96
- if (matches) {
97
- for (const match of matches) {
98
- const text = match.replace(/"content":"|"/g, "").replace(/\\n/g, "\n").replace(/\\t/g, "\t");
99
- contentResponse += text;
100
-
101
- const newHtml = contentResponse.match(/<!DOCTYPE html>[\s\S]*/)?.[0];
102
- if (newHtml) {
103
- let partialDoc = newHtml;
104
- if (!partialDoc.includes("</html>")) partialDoc += "\n</html>";
105
-
106
- const now = Date.now();
107
- if (now - lastRenderTime > 300) {
108
- setHtml(partialDoc);
109
- lastRenderTime = now;
110
- }
111
-
112
- if (partialDoc.length > 200) onScrollToBottom();
113
- }
114
  }
 
 
115
  }
116
 
117
  read();
@@ -161,3 +143,4 @@ function AskAI({
161
  }
162
 
163
  export default AskAI;
 
 
42
  try {
43
  onNewPrompt(prompt);
44
 
45
+ const request = await fetch("/api/ask-ai", {
46
  method: "POST",
47
  headers: {
 
48
  "Content-Type": "application/json",
 
 
49
  },
50
  body: JSON.stringify({
51
+ prompt,
52
+ previousPrompt,
53
+ html,
54
+ }),
 
 
 
 
 
 
 
 
 
55
  });
56
 
57
  if (!request.ok || !request.body) {
 
80
  }
81
 
82
  const chunk = decoder.decode(value, { stream: true });
83
+ contentResponse += chunk;
84
+
85
+ const newHtml = contentResponse.match(/<!DOCTYPE html>[\s\S]*/)?.[0];
86
+ if (newHtml) {
87
+ let partialDoc = newHtml;
88
+ if (!partialDoc.includes("</html>")) partialDoc += "\n</html>";
89
+
90
+ const now = Date.now();
91
+ if (now - lastRenderTime > 300) {
92
+ setHtml(partialDoc);
93
+ lastRenderTime = now;
 
 
 
 
 
 
 
 
94
  }
95
+
96
+ if (partialDoc.length > 200) onScrollToBottom();
97
  }
98
 
99
  read();
 
143
  }
144
 
145
  export default AskAI;
146
+