jsvelox 1.5.0 → 1.6.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 +47 -17
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsvelox",
3
- "version": "1.5.0",
3
+ "version": "1.6.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
@@ -1,4 +1,8 @@
1
1
  export class VeloxAI {
2
+ // TR: Kütüphaneyi başlatır. API key, provider (claude/openai/gemini), model ve timeout ayarlarını alır.
3
+ // Eksik bilgi girilirse hata fırlatır.
4
+ // EN: Initializes the library. Takes API key, provider (claude/openai/gemini), model and timeout settings.
5
+ // Throws an error if required fields are missing.
2
6
  constructor(config) {
3
7
  if (!config.apiKey) {
4
8
  throw new Error('API key zorunlu!')
@@ -12,15 +16,19 @@ export class VeloxAI {
12
16
  this.memory = []
13
17
  this.timeout = config.timeout || 30000
14
18
  }
15
-
19
+ // TR: Kullanıcının mesajını seçili yapay zekaya gönderir ve cevabı döndürür.
20
+ // Konuşma geçmişini (memory) otomatik yönetir. Hata olursa memory'i temizler.
21
+ // EN: Sends the user's message to the selected AI and returns the response.
22
+ // Automatically manages conversation history (memory). Clears memory on error.
16
23
  async send(message) {
17
- this.memory.push({ role: 'user', content: message })
24
+ this.memory.push({ role: 'user', content: message })
18
25
 
19
- const timeoutPromise = new Promise((_, reject) =>
20
- setTimeout(() => reject(new Error('Zaman aşımı! AI cevap vermedi.')), this.timeout)
21
- )
26
+ const timeoutPromise = new Promise((_, reject) =>
27
+ setTimeout(() => reject(new Error('Zaman aşımı! AI cevap vermedi.')), this.timeout)
28
+ )
22
29
 
23
- let cevap
30
+ let cevap
31
+ try {
24
32
  if (this.provider === 'claude') {
25
33
  cevap = await Promise.race([this._sendClaude(this.memory), timeoutPromise])
26
34
  } else if (this.provider === 'openai') {
@@ -30,21 +38,34 @@ export class VeloxAI {
30
38
  } else {
31
39
  throw new Error('Geçersiz provider. claude, openai veya gemini kullan.')
32
40
  }
33
-
34
- this.memory.push({ role: 'assistant', content: cevap })
35
- return cevap
41
+ } catch (err) {
42
+ this.memory.pop() // hata olunca memory'den çıkar
43
+ throw err
36
44
  }
37
45
 
46
+ this.memory.push({ role: 'assistant', content: cevap })
47
+ return cevap
48
+ }
49
+ // TR: Mesajı gönderir, hata olursa belirtilen sayıda tekrar dener (varsayılan 3 kez).
50
+ // Her deneme arasında 1 saniye bekler. Tüm denemeler başarısız olursa hatayı fırlatır.
51
+ // EN: Sends the message and retries on failure up to the specified number of times (default 3).
52
+ // Waits 1 second between retries. Throws the error if all attempts fail.
53
+
38
54
  async sendWithRetry(message, retries = 3) {
39
- for (let i = 0; i < retries; i++) {
40
- try {
41
- return await this.send(message)
42
- } catch (err) {
43
- if (i === retries - 1) throw err
44
- await new Promise(r => setTimeout(r, 1000))
45
- }
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))
46
62
  }
47
- }
63
+ }
64
+ }
65
+ // TR: Mesajı Anthropic Claude API'sine gönderir ve metin cevabı döndürür.
66
+ // Dahili metottur, doğrudan çağrılmamalıdır.
67
+ // EN: Sends the message to Anthropic Claude API and returns the text response.
68
+ // Internal method, should not be called directly.
48
69
 
49
70
  async _sendClaude(memory) {
50
71
  const response = await fetch('https://api.anthropic.com/v1/messages', {
@@ -66,6 +87,10 @@ export class VeloxAI {
66
87
  }
67
88
  return data.content[0].text
68
89
  }
90
+ // TR: Mesajı OpenAI API'sine gönderir ve metin cevabı döndürür.
91
+ // Dahili metottur, doğrudan çağrılmamalıdır.
92
+ // EN: Sends the message to OpenAI API and returns the text response.
93
+ // Internal method, should not be called directly.
69
94
 
70
95
  async _sendOpenAI(memory) {
71
96
  const response = await fetch('https://api.openai.com/v1/chat/completions', {
@@ -85,6 +110,11 @@ export class VeloxAI {
85
110
  }
86
111
  return data.choices[0].message.content
87
112
  }
113
+ // TR: Mesajı Google Gemini API'sine gönderir ve metin cevabı döndürür.
114
+ // Konuşma geçmişini Gemini'nin beklediği formata dönüştürür.
115
+ // EN: Sends the message to Google Gemini API and returns the text response.
116
+ // Converts conversation history into the format expected by Gemini.
117
+
88
118
  async _sendGemini(memory) {
89
119
  const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${this.apiKey}`
90
120
  const contents = memory.map(m => ({