jsvelox 1.0.0 → 1.2.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 (3) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +51 -8
  3. package/test.html +0 -27
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsvelox",
3
- "version": "1.0.0",
3
+ "version": "1.2.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
@@ -4,24 +4,49 @@ export class VeloxAI {
4
4
  throw new Error('API key zorunlu!')
5
5
  }
6
6
  if (!config.provider) {
7
- throw new Error('Provider zorunlu! claude veya openai kullan.')
7
+ throw new Error('Provider zorunlu! claude, openai veya gemini kullan.')
8
8
  }
9
9
  this.provider = config.provider
10
10
  this.apiKey = config.apiKey
11
11
  this.model = config.model || null
12
+ this.memory = []
13
+ this.timeout = config.timeout || 30000
12
14
  }
13
15
 
14
16
  async send(message) {
17
+ this.memory.push({ role: 'user', content: message })
18
+
19
+ const timeoutPromise = new Promise((_, reject) =>
20
+ setTimeout(() => reject(new Error('Zaman aşımı! AI cevap vermedi.')), this.timeout)
21
+ )
22
+
23
+ let cevap
15
24
  if (this.provider === 'claude') {
16
- return await this._sendClaude(message)
25
+ cevap = await Promise.race([this._sendClaude(this.memory), timeoutPromise])
17
26
  } else if (this.provider === 'openai') {
18
- return await this._sendOpenAI(message)
27
+ cevap = await Promise.race([this._sendOpenAI(this.memory), timeoutPromise])
28
+ } else if (this.provider === 'gemini') {
29
+ cevap = await Promise.race([this._sendGemini(this.memory), timeoutPromise])
19
30
  } else {
20
- throw new Error('Geçersiz provider. claude veya openai kullan.')
31
+ throw new Error('Geçersiz provider. claude, openai veya gemini kullan.')
21
32
  }
33
+
34
+ this.memory.push({ role: 'assistant', content: cevap })
35
+ return cevap
22
36
  }
23
37
 
24
- async _sendClaude(message) {
38
+ 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
+ }
46
+ }
47
+ }
48
+
49
+ async _sendClaude(memory) {
25
50
  const response = await fetch('https://api.anthropic.com/v1/messages', {
26
51
  method: 'POST',
27
52
  headers: {
@@ -32,7 +57,7 @@ export class VeloxAI {
32
57
  body: JSON.stringify({
33
58
  model: this.model || 'claude-3-5-sonnet-20241022',
34
59
  max_tokens: 1024,
35
- messages: [{ role: 'user', content: message }]
60
+ messages: memory
36
61
  })
37
62
  })
38
63
  const data = await response.json()
@@ -42,7 +67,7 @@ export class VeloxAI {
42
67
  return data.content[0].text
43
68
  }
44
69
 
45
- async _sendOpenAI(message) {
70
+ async _sendOpenAI(memory) {
46
71
  const response = await fetch('https://api.openai.com/v1/chat/completions', {
47
72
  method: 'POST',
48
73
  headers: {
@@ -51,7 +76,7 @@ export class VeloxAI {
51
76
  },
52
77
  body: JSON.stringify({
53
78
  model: this.model || 'gpt-4o-mini',
54
- messages: [{ role: 'user', content: message }]
79
+ messages: memory
55
80
  })
56
81
  })
57
82
  const data = await response.json()
@@ -60,4 +85,22 @@ export class VeloxAI {
60
85
  }
61
86
  return data.choices[0].message.content
62
87
  }
88
+ async _sendGemini(memory) {
89
+ const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${this.apiKey}`
90
+ const contents = memory.map(m => ({
91
+ role: m.role === 'assistant' ? 'model' : 'user',
92
+ parts: [{ text: m.content }]
93
+ }))
94
+
95
+ const response = await fetch(url, {
96
+ method: 'POST',
97
+ headers: { 'Content-Type': 'application/json' },
98
+ body: JSON.stringify({ contents })
99
+ })
100
+ const data = await response.json()
101
+ if (!response.ok) {
102
+ throw new Error(`Gemini hata: ${data.error?.message || 'Bilinmeyen hata'}`)
103
+ }
104
+ return data.candidates[0].content.parts[0].text
105
+ }
63
106
  }
package/test.html DELETED
@@ -1,27 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="tr">
3
- <head>
4
- <meta charset="UTF-8">
5
- <title>Velox Test</title>
6
- </head>
7
- <body>
8
- <input type="text" id="userInput" placeholder="Mesajını yaz...">
9
- <button onclick="gonder()">Gönder</button>
10
- <p id="cevap"></p>
11
-
12
- <script type="module">
13
- import { VeloxAI } from './src/index.js'
14
-
15
- const ai = new VeloxAI({
16
- provider: 'claude',
17
- apiKey: 'BURAYA-API-KEY-GELECEK'
18
- })
19
-
20
- window.gonder = async function() {
21
- const mesaj = document.getElementById('userInput').value
22
- const cevap = await ai.send(mesaj)
23
- document.getElementById('cevap').innerText = cevap
24
- }
25
- </script>
26
- </body>
27
- </html>