const endpoint = 'http://10.249.196.59:11434/api/generate';

const options = {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    model: 'deepseek-r1:latest',
    prompt: 'Tell me a very long joke!',
    stream: true,
    think: false,
  }),
};

fetch(endpoint, options)
  .then((res) => {
    const reader = res.body.getReader();
    const decoder = new TextDecoder();

    let buffer = '';

    reader.read().then(async ({ done, value }) => {
      while (!done) {
        buffer += decoder.decode(value, { stream: true });

        const lines = buffer.split(/\r?\n/);
        buffer = lines.pop() ?? ''; // keep partial line for next chunk

        for (const rawLine of lines) {
          const line = rawLine.trim();
          if (!line) continue;

          let payload = line;
          if (payload.startsWith('data:')) {
            payload = payload.slice(5).trim();
          }
          if (!payload || payload === '[DONE]') continue;

          try {
            const parsed = JSON.parse(payload);
            process.stdout.write(parsed.response ?? parsed.text ?? '');
          } catch (err) {
            // If this chunk is not valid JSON, print it as raw text as a fallback.
            process.stdout.write(payload);
          }
        }

        ({ done, value } = await reader.read());
      }

      if (buffer) {
        try {
          const parsed = JSON.parse(buffer);
          process.stdout.write(parsed.response ?? parsed.text ?? '');
        } catch (err) {
          process.stdout.write(buffer);
        }
      }
    });
  })
  .catch(console.error);
