jsvelox 1.6.0 → 1.7.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 +45 -38
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsvelox",
3
- "version": "1.6.0",
3
+ "version": "1.7.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,71 @@ 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
+ }
30
+
31
+ this.memory.push({ role: 'user', content: message })
25
32
 
26
- const timeoutPromise = new Promise((_, reject) =>
27
- setTimeout(() => reject(new Error('Zaman aşımı! AI cevap vermedi.')), this.timeout)
28
- )
33
+ const timeoutPromise = new Promise((_, reject) =>
34
+ setTimeout(() => reject(new Error('Zaman aşımı! AI cevap vermedi.')), this.timeout)
35
+ )
29
36
 
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.')
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
+
65
73
  // TR: Mesajı Anthropic Claude API'sine gönderir ve metin cevabı döndürür.
66
74
  // Dahili metottur, doğrudan çağrılmamalıdır.
67
75
  // EN: Sends the message to Anthropic Claude API and returns the text response.
68
76
  // Internal method, should not be called directly.
69
-
70
77
  async _sendClaude(memory) {
71
78
  const response = await fetch('https://api.anthropic.com/v1/messages', {
72
79
  method: 'POST',
@@ -87,11 +94,11 @@ export class VeloxAI {
87
94
  }
88
95
  return data.content[0].text
89
96
  }
97
+
90
98
  // TR: Mesajı OpenAI API'sine gönderir ve metin cevabı döndürür.
91
99
  // Dahili metottur, doğrudan çağrılmamalıdır.
92
100
  // EN: Sends the message to OpenAI API and returns the text response.
93
101
  // Internal method, should not be called directly.
94
-
95
102
  async _sendOpenAI(memory) {
96
103
  const response = await fetch('https://api.openai.com/v1/chat/completions', {
97
104
  method: 'POST',
@@ -110,13 +117,13 @@ export class VeloxAI {
110
117
  }
111
118
  return data.choices[0].message.content
112
119
  }
120
+
113
121
  // TR: Mesajı Google Gemini API'sine gönderir ve metin cevabı döndürür.
114
122
  // Konuşma geçmişini Gemini'nin beklediği formata dönüştürür.
115
123
  // EN: Sends the message to Google Gemini API and returns the text response.
116
124
  // Converts conversation history into the format expected by Gemini.
117
-
118
125
  async _sendGemini(memory) {
119
- const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${this.apiKey}`
126
+ const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${this.apiKey}`
120
127
  const contents = memory.map(m => ({
121
128
  role: m.role === 'assistant' ? 'model' : 'user',
122
129
  parts: [{ text: m.content }]