geminiService.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // This is a mock service. In a real application, you would import and use the @google/genai library.
  2. // import { GoogleGenAI, Chat } from "@google/genai";
  3. import { ChatMessage } from '../types';
  4. // Mock response structure to emulate the real API
  5. interface MockGenerateContentResponse {
  6. text: string;
  7. }
  8. class MockChat {
  9. private systemInstruction: string;
  10. constructor(config?: { systemInstruction: string }) {
  11. this.systemInstruction = config?.systemInstruction || "You are a helpful assistant.";
  12. console.log("MockChat initialized with instruction:", this.systemInstruction);
  13. }
  14. sendMessage(message: { message: string }): Promise<MockGenerateContentResponse> {
  15. console.log("MockChat received message:", message.message);
  16. return new Promise(resolve => {
  17. setTimeout(() => {
  18. const responses = [
  19. "That's a very interesting question! Let me think...",
  20. "Based on your persona settings, I would say the best approach is to be direct and informative.",
  21. "I'm sorry, I cannot answer that question based on my current knowledge base.",
  22. "Of course! I can help with that. What are the specific details you need?",
  23. "Analyzing your request... It seems you're asking about product features. Our product excels in A, B, and C."
  24. ];
  25. const randomResponse = responses[Math.floor(Math.random() * responses.length)];
  26. resolve({ text: randomResponse });
  27. }, 800);
  28. });
  29. }
  30. }
  31. class MockGoogleGenAI {
  32. public chats = {
  33. create: (config?: { systemInstruction: string }) => {
  34. return new MockChat(config);
  35. }
  36. };
  37. }
  38. const mockAi = new MockGoogleGenAI();
  39. let mockChat: MockChat | null = null;
  40. export const createAIChatSession = (persona: string) => {
  41. mockChat = mockAi.chats.create({ systemInstruction: persona });
  42. };
  43. export const sendChatMessage = async (message: string): Promise<ChatMessage> => {
  44. if (!mockChat) {
  45. createAIChatSession("You are a helpful assistant."); // Default persona
  46. }
  47. const response = await mockChat!.sendMessage({ message });
  48. const aiResponse: ChatMessage = { id: `msg-ai-${Date.now()}`, sender: 'ai', text: response.text };
  49. return aiResponse;
  50. };
  51. export const polishScript = async (originalScript: string): Promise<string[]> => {
  52. console.log("Mock polishScript called with:", originalScript);
  53. // In a real app, you would call Gemini here.
  54. // const response = await ai.models.generateContent({ model: 'gemini-2.5-flash', contents: `Rewrite the following script in 3 different styles (professional, witty, and casual):\n\n"${originalScript}"` });
  55. // For now, return mock data.
  56. return new Promise(resolve => {
  57. setTimeout(() => {
  58. resolve([
  59. `[Professional] We are pleased to welcome you to our presentation. We have some exciting updates to share.`,
  60. `[Witty] Settle in, folks! You're about to witness a presentation of epic proportions.`,
  61. `[Casual] Hey everyone, thanks for joining. Let's get started with our presentation.`
  62. ]);
  63. }, 1200);
  64. });
  65. };
  66. export const generateArticle = async (topic: string): Promise<{ title: string; summary: string; content: string; }> => {
  67. console.log("Mock generateArticle called with:", topic);
  68. // In a real app, you would call Gemini here.
  69. return new Promise(resolve => {
  70. setTimeout(() => {
  71. resolve({
  72. title: `Exploring the Wonders of ${topic}`,
  73. summary: `A brief overview of the key aspects and fascinating details surrounding ${topic}. Discover why it's a subject of great interest.`,
  74. content: `The topic of ${topic} is a multifaceted and deeply engaging subject. Historically, its roots can be traced back to ancient civilizations where it played a pivotal role in culture and daily life. \n\nScientifically, ${topic} presents a complex set of principles that continue to be studied by experts worldwide. The implications of these studies are far-reaching, potentially impacting technology, medicine, and our understanding of the universe. \n\nFrom a cultural standpoint, ${topic} has inspired countless works of art, literature, and music, proving its enduring relevance in the human experience. As we look to the future, the evolution of ${topic} promises even more exciting developments.`
  75. });
  76. }, 1500);
  77. });
  78. };
  79. export const mockProductDatabase = [
  80. { title: "Smart Noise-Cancelling Headphones", price: "$149.99" },
  81. { title: "Ergonomic Mechanical Keyboard", price: "$89.99" },
  82. { title: "4K Ultra-HD Webcam", price: "$69.99" },
  83. { title: "Organic French Press Coffee Beans", price: "$22.50" },
  84. { title: "Minimalist Desk Lamp with Wireless Charging", price: "$45.00" },
  85. { title: "Hand-Poured Soy Wax Candle", price: "$18.00" },
  86. { title: "The Alchemist by Paulo Coelho", price: "$12.99" },
  87. { title: "Atomic Habits by James Clear", price: "$15.99" },
  88. { title: "Lightweight Waterproof Hiking Backpack", price: "$79.95" },
  89. { title: "Insulated Stainless Steel Water Bottle", price: "$25.00" },
  90. { title: "Professional Chef's Knife - 8 Inch", price: "$120.00" },
  91. { title: "Non-Stick Ceramic Frying Pan Set", price: "$59.99" },
  92. { title: "Portable Blender for Shakes and Smoothies", price: "$34.99" },
  93. { title: "Yoga Mat with Carrying Strap", price: "$29.99" },
  94. { title: "Adjustable Dumbbell Set", price: "$199.99" },
  95. ];
  96. // Simple hash function to get a consistent index from a string
  97. const simpleHash = (str: string) => {
  98. let hash = 0;
  99. for (let i = 0; i < str.length; i++) {
  100. const char = str.charCodeAt(i);
  101. hash = (hash << 5) - hash + char;
  102. hash |= 0; // Convert to 32bit integer
  103. }
  104. return Math.abs(hash);
  105. };
  106. export const parseProductUrl = async (url: string): Promise<{ title: string; imageUrl: string; price: string; }> => {
  107. console.log("Mock parseProductUrl called with:", url);
  108. // In a real app, this might be a backend call that scrapes the page or calls an API.
  109. // Here, we simulate it by using the URL to predictably pick from a larger mock database.
  110. return new Promise(resolve => {
  111. setTimeout(() => {
  112. const hash = simpleHash(url);
  113. const product = mockProductDatabase[hash % mockProductDatabase.length];
  114. const imageUrl = `https://picsum.photos/seed/${encodeURIComponent(url)}/400/400`;
  115. resolve({ ...product, imageUrl });
  116. }, 800 + Math.random() * 500); // Simulate network delay
  117. });
  118. };