jsvelox 1.6.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +57 -38
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsvelox",
3
- "version": "1.6.0",
3
+ "version": "1.8.0",
4
4
  "description": "Lightweight JS library for AI integration and developer utilities",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/index.js CHANGED
@@ -9,64 +9,83 @@ export class VeloxAI {
9
9
  }
10
10
  if (!config.provider) {
11
11
  throw new Error('Provider zorunlu! claude, openai veya gemini kullan.')
12
- }
13
- this.provider = config.provider
14
- this.apiKey = config.apiKey
15
- this.model = config.model || null
12
+ }
13
+ this.provider = config.provider.toLowerCase().trim()
14
+ this.apiKey = config.apiKey.trim()
15
+ this.model = config.model ? config.model.trim() : null
16
16
  this.memory = []
17
- this.timeout = config.timeout || 30000
17
+ this.timeout = typeof config.timeout === 'number' && config.timeout > 0
18
+ ? config.timeout
19
+ : 30000
18
20
  }
21
+
19
22
  // TR: Kullanıcının mesajını seçili yapay zekaya gönderir ve cevabı döndürür.
20
23
  // Konuşma geçmişini (memory) otomatik yönetir. Hata olursa memory'i temizler.
21
24
  // EN: Sends the user's message to the selected AI and returns the response.
22
25
  // Automatically manages conversation history (memory). Clears memory on error.
23
26
  async send(message) {
24
- this.memory.push({ role: 'user', content: message })
27
+ if (!message || typeof message !== 'string' || message.trim() === '') {
28
+ throw new Error('Mesaj boş olamaz!')
29
+ }
25
30
 
26
- const timeoutPromise = new Promise((_, reject) =>
27
- setTimeout(() => reject(new Error('Zaman aşımı! AI cevap vermedi.')), this.timeout)
28
- )
31
+ this.memory.push({ role: 'user', content: message })
29
32
 
30
- let cevap
31
- try {
32
- if (this.provider === 'claude') {
33
- cevap = await Promise.race([this._sendClaude(this.memory), timeoutPromise])
34
- } else if (this.provider === 'openai') {
35
- cevap = await Promise.race([this._sendOpenAI(this.memory), timeoutPromise])
36
- } else if (this.provider === 'gemini') {
37
- cevap = await Promise.race([this._sendGemini(this.memory), timeoutPromise])
38
- } else {
39
- throw new Error('Geçersiz provider. claude, openai veya gemini kullan.')
33
+ const timeoutPromise = new Promise((_, reject) =>
34
+ setTimeout(() => reject(new Error('Zaman aşımı! AI cevap vermedi.')), this.timeout)
35
+ )
36
+
37
+ let cevap
38
+ try {
39
+ if (this.provider === 'claude') {
40
+ cevap = await Promise.race([this._sendClaude(this.memory), timeoutPromise])
41
+ } else if (this.provider === 'openai') {
42
+ cevap = await Promise.race([this._sendOpenAI(this.memory), timeoutPromise])
43
+ } else if (this.provider === 'gemini') {
44
+ cevap = await Promise.race([this._sendGemini(this.memory), timeoutPromise])
45
+ } else {
46
+ throw new Error('Geçersiz provider. claude, openai veya gemini kullan.')
47
+ }
48
+ } catch (err) {
49
+ this.memory.pop()
50
+ throw err
40
51
  }
41
- } catch (err) {
42
- this.memory.pop() // hata olunca memory'den çıkar
43
- throw err
52
+
53
+ this.memory.push({ role: 'assistant', content: cevap })
54
+ return cevap
44
55
  }
45
56
 
46
- this.memory.push({ role: 'assistant', content: cevap })
47
- return cevap
48
- }
49
57
  // TR: Mesajı gönderir, hata olursa belirtilen sayıda tekrar dener (varsayılan 3 kez).
50
58
  // Her deneme arasında 1 saniye bekler. Tüm denemeler başarısız olursa hatayı fırlatır.
51
59
  // EN: Sends the message and retries on failure up to the specified number of times (default 3).
52
60
  // Waits 1 second between retries. Throws the error if all attempts fail.
53
-
54
61
  async sendWithRetry(message, retries = 3) {
55
- for (let i = 0; i < retries; i++) {
56
- try {
57
- return await this.send(message)
58
- } catch (err) {
59
- this.memory.pop() // hata olunca memory'den çıkar
60
- if (i === retries - 1) throw err
61
- await new Promise(r => setTimeout(r, 1000))
62
+ for (let i = 0; i < retries; i++) {
63
+ try {
64
+ return await this.send(message)
65
+ } catch (err) {
66
+ this.memory.pop()
67
+ if (i === retries - 1) throw err
68
+ await new Promise(r => setTimeout(r, 1000))
69
+ }
62
70
  }
63
71
  }
64
- }
72
+
73
+ // TR: Konuşma geçmişini temizler. Yeni bir konuşma başlatmak için kullanılır.
74
+ // EN: Clears the conversation history. Used to start a new conversation.
75
+ clearMemory() {
76
+ this.memory = []
77
+ }
78
+
79
+ // TR: Mevcut konuşma geçmişini döndürür.
80
+ // EN: Returns the current conversation history.
81
+ getMemory() {
82
+ return this.memory
83
+ }
84
+
65
85
  // TR: Mesajı Anthropic Claude API'sine gönderir ve metin cevabı döndürür.
66
86
  // Dahili metottur, doğrudan çağrılmamalıdır.
67
87
  // EN: Sends the message to Anthropic Claude API and returns the text response.
68
88
  // Internal method, should not be called directly.
69
-
70
89
  async _sendClaude(memory) {
71
90
  const response = await fetch('https://api.anthropic.com/v1/messages', {
72
91
  method: 'POST',
@@ -87,11 +106,11 @@ export class VeloxAI {
87
106
  }
88
107
  return data.content[0].text
89
108
  }
109
+
90
110
  // TR: Mesajı OpenAI API'sine gönderir ve metin cevabı döndürür.
91
111
  // Dahili metottur, doğrudan çağrılmamalıdır.
92
112
  // EN: Sends the message to OpenAI API and returns the text response.
93
113
  // Internal method, should not be called directly.
94
-
95
114
  async _sendOpenAI(memory) {
96
115
  const response = await fetch('https://api.openai.com/v1/chat/completions', {
97
116
  method: 'POST',
@@ -110,13 +129,13 @@ export class VeloxAI {
110
129
  }
111
130
  return data.choices[0].message.content
112
131
  }
132
+
113
133
  // TR: Mesajı Google Gemini API'sine gönderir ve metin cevabı döndürür.
114
134
  // Konuşma geçmişini Gemini'nin beklediği formata dönüştürür.
115
135
  // EN: Sends the message to Google Gemini API and returns the text response.
116
136
  // Converts conversation history into the format expected by Gemini.
117
-
118
137
  async _sendGemini(memory) {
119
- const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${this.apiKey}`
138
+ const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${this.apiKey}`
120
139
  const contents = memory.map(m => ({
121
140
  role: m.role === 'assistant' ? 'model' : 'user',
122
141
  parts: [{ text: m.content }]