hedgequantx 2.9.15 → 2.9.16

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hedgequantx",
3
- "version": "2.9.15",
3
+ "version": "2.9.16",
4
4
  "description": "HedgeQuantX - Prop Futures Trading CLI",
5
5
  "main": "src/app.js",
6
6
  "bin": {
@@ -56,83 +56,59 @@ const API_CHAT_ENDPOINTS = {
56
56
  * @param {Object} agent - Agent config
57
57
  * @returns {Promise<Object>} { success, latency, formatValid, error }
58
58
  */
59
- const testApiKeyConnection = (agent) => {
60
- return new Promise((resolve) => {
61
- const startTime = Date.now();
62
- const endpoint = API_CHAT_ENDPOINTS[agent.provider];
63
-
64
- if (!endpoint || !agent.apiKey) {
65
- resolve({ success: false, latency: 0, formatValid: false, error: 'Missing endpoint or API key' });
66
- return;
67
- }
68
-
69
- const url = new URL(endpoint);
70
- const body = JSON.stringify({
71
- model: agent.modelId,
72
- messages: [{ role: 'user', content: TEST_PROMPT }],
73
- max_tokens: 100,
74
- stream: false
75
- });
59
+ const testApiKeyConnection = async (agent) => {
60
+ const startTime = Date.now();
61
+ const endpoint = API_CHAT_ENDPOINTS[agent.provider];
62
+
63
+ if (!endpoint || !agent.apiKey) {
64
+ return { success: false, latency: 0, formatValid: false, error: 'Missing endpoint or API key' };
65
+ }
66
+
67
+ try {
68
+ const controller = new AbortController();
69
+ const timeoutId = setTimeout(() => controller.abort(), AGENT_TIMEOUT);
76
70
 
77
- const options = {
78
- hostname: url.hostname,
79
- path: url.pathname,
71
+ const response = await fetch(endpoint, {
80
72
  method: 'POST',
81
73
  headers: {
82
74
  'Content-Type': 'application/json',
83
- 'Authorization': `Bearer ${agent.apiKey}`,
84
- 'Content-Length': Buffer.byteLength(body)
75
+ 'Authorization': `Bearer ${agent.apiKey}`
85
76
  },
86
- timeout: AGENT_TIMEOUT
87
- };
88
-
89
- const req = https.request(options, (res) => {
90
- let data = '';
91
- res.on('data', chunk => data += chunk);
92
- res.on('end', () => {
93
- const latency = Date.now() - startTime;
94
- try {
95
- // Handle potential SSE format (data: {...}\n\ndata: {...})
96
- let jsonData = data.trim();
97
- if (jsonData.startsWith('data:')) {
98
- // SSE format - extract last complete JSON
99
- const lines = jsonData.split('\n').filter(l => l.startsWith('data:'));
100
- const lastData = lines.filter(l => l !== 'data: [DONE]').pop();
101
- if (lastData) jsonData = lastData.replace('data: ', '');
102
- }
103
-
104
- const parsed = JSON.parse(jsonData);
105
- if (res.statusCode >= 200 && res.statusCode < 300) {
106
- const content = parsed.choices?.[0]?.message?.content ||
107
- parsed.choices?.[0]?.delta?.content || '';
108
- const formatResult = validateResponseFormat(content);
109
- resolve({
110
- success: formatResult.valid,
111
- latency,
112
- formatValid: formatResult.valid,
113
- error: formatResult.valid ? null : formatResult.error,
114
- response: content
115
- });
116
- } else {
117
- resolve({ success: false, latency, formatValid: false,
118
- error: parsed.error?.message || `HTTP ${res.statusCode}` });
119
- }
120
- } catch (e) {
121
- resolve({ success: false, latency, formatValid: false, error: `Parse error: ${e.message}` });
122
- }
123
- });
77
+ body: JSON.stringify({
78
+ model: agent.modelId,
79
+ messages: [{ role: 'user', content: TEST_PROMPT }],
80
+ max_tokens: 100,
81
+ stream: false
82
+ }),
83
+ signal: controller.signal
124
84
  });
125
85
 
126
- req.on('error', (e) => resolve({
127
- success: false, latency: Date.now() - startTime, formatValid: false, error: e.message
128
- }));
129
- req.on('timeout', () => {
130
- req.destroy();
131
- resolve({ success: false, latency: AGENT_TIMEOUT, formatValid: false, error: 'Timeout' });
132
- });
133
- req.write(body);
134
- req.end();
135
- });
86
+ clearTimeout(timeoutId);
87
+ const latency = Date.now() - startTime;
88
+
89
+ const data = await response.json();
90
+
91
+ if (response.ok) {
92
+ const content = data.choices?.[0]?.message?.content || '';
93
+ const formatResult = validateResponseFormat(content);
94
+ return {
95
+ success: formatResult.valid,
96
+ latency,
97
+ formatValid: formatResult.valid,
98
+ error: formatResult.valid ? null : formatResult.error,
99
+ response: content
100
+ };
101
+ } else {
102
+ return { success: false, latency, formatValid: false,
103
+ error: data.error?.message || `HTTP ${response.status}` };
104
+ }
105
+ } catch (e) {
106
+ const latency = Date.now() - startTime;
107
+ if (e.name === 'AbortError') {
108
+ return { success: false, latency: AGENT_TIMEOUT, formatValid: false, error: 'Timeout' };
109
+ }
110
+ return { success: false, latency, formatValid: false, error: e.message };
111
+ }
136
112
  };
137
113
 
138
114
  /**