File size: 3,244 Bytes
753dbc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env node

/**
 * CC Site Maker CLI Tool
 *
 * A simple command-line utility to generate HTML using the CC Site Maker API
 * Usage: node cli-generator.js "Your prompt" output.html
 */

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import readline from 'readline';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Default configuration - modify as needed
const CONFIG = {
  apiUrl: process.env.API_URL || 'http://localhost:7860/api/external/ask-ai',
  apiKey: process.env.API_KEY || ''
};

// Create readline interface for user input
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

/**
 * Generate HTML from prompt using the API
 * @param {string} prompt - The prompt to send to the AI
 * @param {string} apiKey - Your API key
 * @returns {Promise<string>} The generated HTML
 */
async function generateHTML(prompt, apiKey) {
  console.log('Generating HTML...');

  const response = await fetch(CONFIG.apiUrl, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-API-Key': apiKey
    },
    body: JSON.stringify({
      prompt: prompt
    })
  });

  if (!response.ok) {
    let errorMessage;
    try {
      const errorData = await response.json();
      errorMessage = errorData.message;
    } catch (e) {
      errorMessage = `HTTP error! Status: ${response.status}`;
    }
    throw new Error(errorMessage || 'Failed to generate HTML');
  }

  // For streamed responses
  const reader = response.body.getReader();
  const decoder = new TextDecoder('utf-8');
  let result = '';
  let progress = 0;

  process.stdout.write('Receiving response: ');

  while (true) {
    const { done, value } = await reader.read();
    if (done) break;

    result += decoder.decode(value, { stream: true });
    progress += value.length;

    // Show some progress indicators
    process.stdout.write('.');
  }

  process.stdout.write('\n');
  return result;
}

/**
 * Save HTML to a file
 * @param {string} html - HTML content
 * @param {string} filename - Output filename
 */
function saveHTMLToFile(html, filename) {
  const outputPath = path.resolve(process.cwd(), filename);
  fs.writeFileSync(outputPath, html);
  console.log(`HTML saved to: ${outputPath}`);
}

/**
 * Main function
 */
async function main() {
  // Get command line arguments
  const args = process.argv.slice(2);
  let prompt = args[0];
  let outputFile = args[1] || 'output.html';

  // If no prompt provided, ask for it
  if (!prompt) {
    prompt = await new Promise((resolve) => {
      rl.question('Enter your prompt: ', (answer) => resolve(answer));
    });
  }

  // If no API key, ask for it
  if (!CONFIG.apiKey) {
    CONFIG.apiKey = await new Promise((resolve) => {
      rl.question('Enter your API key: ', (answer) => resolve(answer));
    });
  }

  try {
    const html = await generateHTML(prompt, CONFIG.apiKey);
    saveHTMLToFile(html, outputFile);
  } catch (error) {
    console.error('Error:', error.message);
    process.exit(1);
  } finally {
    rl.close();
  }
}

// Run the main function
main().catch((error) => {
  console.error('Unhandled error:', error);
  process.exit(1);
});