@plexor-dev/claude-code-plugin 0.1.0-beta.3 → 0.1.0-beta.6
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/commands/plexor-status.md +31 -20
- package/hooks/intercept.js +410 -0
- package/hooks/track-response.js +110 -0
- package/package.json +2 -1
- package/lib/constants.js +0 -40
|
@@ -29,33 +29,44 @@ If `apiKey` is missing or empty, tell the user to run `/plexor-login` first.
|
|
|
29
29
|
Make a request to get usage statistics:
|
|
30
30
|
```
|
|
31
31
|
GET {apiUrl}/api/users/me/usage
|
|
32
|
-
|
|
32
|
+
X-Plexor-Key: {apiKey}
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
Note:
|
|
35
|
+
Note: Use the X-Plexor-Key header with the API key (not Authorization Bearer).
|
|
36
36
|
|
|
37
37
|
**Step 4: Display the status**
|
|
38
38
|
|
|
39
|
-
Show the user a formatted status display
|
|
39
|
+
Show the user a formatted status display using this box-style format. Calculate weekly date range (Monday to Sunday of current week):
|
|
40
40
|
|
|
41
41
|
```
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
42
|
+
● ┌─────────────────────────────────────────────┐
|
|
43
|
+
│ Plexor Status │
|
|
44
|
+
├─────────────────────────────────────────────┤
|
|
45
|
+
│ Account: [tier, e.g., "Pro" or "beta"] │
|
|
46
|
+
│ Email: [email from config] │
|
|
47
|
+
│ Status: ● Active │
|
|
48
|
+
├─────────────────────────────────────────────┤
|
|
49
|
+
│ This Week ([start date] - [end date]) │
|
|
50
|
+
│ ├── Requests: [totalRequests] │
|
|
51
|
+
│ ├── Tokens saved: [tokensUsed] ([optimizationPercent]%) │
|
|
52
|
+
│ ├── Avg latency: [avgLatency]ms │
|
|
53
|
+
│ └── Savings: $[costSavings] │
|
|
54
|
+
├─────────────────────────────────────────────┤
|
|
55
|
+
│ Settings │
|
|
56
|
+
│ ├── Optimization: [Enabled/Disabled] │
|
|
57
|
+
│ ├── Local cache: [Enabled/Disabled] │
|
|
58
|
+
│ ├── Mode: [mode] │
|
|
59
|
+
│ └── Provider routing: [preferredProvider] │
|
|
60
|
+
└─────────────────────────────────────────────┘
|
|
61
|
+
|
|
62
|
+
Dashboard: [apiUrl base]/dashboard.html
|
|
59
63
|
```
|
|
60
64
|
|
|
65
|
+
Notes:
|
|
66
|
+
- Use "● Active" (green dot) if enabled, "○ Inactive" if disabled
|
|
67
|
+
- Format token counts with commas (e.g., 46,700)
|
|
68
|
+
- Format costs with 2 decimal places (e.g., $8.02)
|
|
69
|
+
- Calculate current week dates dynamically
|
|
70
|
+
- Use the apiUrl from config to construct the dashboard link (replace /api with empty string)
|
|
71
|
+
|
|
61
72
|
If the API call fails, show the configuration status and mention the API is unavailable.
|
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Plexor Interception Hook
|
|
5
|
+
*
|
|
6
|
+
* This script intercepts Claude Code prompts before they are sent to the LLM.
|
|
7
|
+
* It optimizes the prompt and optionally routes to a cheaper provider.
|
|
8
|
+
*
|
|
9
|
+
* Input: JSON object with messages, model, max_tokens, etc.
|
|
10
|
+
* Output: Modified JSON object with optimized messages
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const PlexorClient = require('../lib/plexor-client');
|
|
14
|
+
const ConfigManager = require('../lib/config');
|
|
15
|
+
const LocalCache = require('../lib/cache');
|
|
16
|
+
const Logger = require('../lib/logger');
|
|
17
|
+
|
|
18
|
+
const logger = new Logger('intercept');
|
|
19
|
+
const config = new ConfigManager();
|
|
20
|
+
const cache = new LocalCache();
|
|
21
|
+
|
|
22
|
+
async function main() {
|
|
23
|
+
const startTime = Date.now();
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
const input = await readStdin();
|
|
27
|
+
const request = JSON.parse(input);
|
|
28
|
+
|
|
29
|
+
// CRITICAL: Skip optimization for agentic/tool-using requests
|
|
30
|
+
// Modifying messages breaks the agent loop and causes infinite loops
|
|
31
|
+
if (isAgenticRequest(request)) {
|
|
32
|
+
logger.debug('Agentic request detected, passing through unchanged');
|
|
33
|
+
return output({
|
|
34
|
+
...request,
|
|
35
|
+
plexor_cwd: process.cwd(),
|
|
36
|
+
_plexor: {
|
|
37
|
+
source: 'passthrough_agentic',
|
|
38
|
+
reason: 'tool_use_detected',
|
|
39
|
+
cwd: process.cwd(),
|
|
40
|
+
latency_ms: Date.now() - startTime
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// CRITICAL: Skip optimization for slash commands (Issue #683)
|
|
46
|
+
// Slash commands like /plexor-status should pass through unchanged
|
|
47
|
+
if (isSlashCommand(request)) {
|
|
48
|
+
logger.debug('Slash command detected, passing through unchanged');
|
|
49
|
+
return output({
|
|
50
|
+
...request,
|
|
51
|
+
plexor_cwd: process.cwd(),
|
|
52
|
+
_plexor: {
|
|
53
|
+
source: 'passthrough_slash_command',
|
|
54
|
+
reason: 'slash_command_detected',
|
|
55
|
+
cwd: process.cwd(),
|
|
56
|
+
latency_ms: Date.now() - startTime
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// CRITICAL: Skip optimization for CLI commands requiring tool execution (Issue #683)
|
|
62
|
+
// Azure CLI, AWS CLI, kubectl, etc. need tools to be preserved
|
|
63
|
+
if (requiresToolExecution(request)) {
|
|
64
|
+
logger.debug('CLI tool execution detected, passing through unchanged');
|
|
65
|
+
return output({
|
|
66
|
+
...request,
|
|
67
|
+
plexor_cwd: process.cwd(),
|
|
68
|
+
_plexor: {
|
|
69
|
+
source: 'passthrough_cli',
|
|
70
|
+
reason: 'cli_tool_execution_detected',
|
|
71
|
+
cwd: process.cwd(),
|
|
72
|
+
latency_ms: Date.now() - startTime
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const settings = await config.load();
|
|
78
|
+
|
|
79
|
+
if (!settings.enabled) {
|
|
80
|
+
logger.debug('Plexor disabled, passing through');
|
|
81
|
+
return output(request);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (!settings.apiKey) {
|
|
85
|
+
logger.info('Not authenticated. Run /plexor-login to enable optimization.');
|
|
86
|
+
return output(request);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const client = new PlexorClient({
|
|
90
|
+
apiKey: settings.apiKey,
|
|
91
|
+
baseUrl: settings.apiUrl || 'https://api.plexor.dev',
|
|
92
|
+
timeout: settings.timeout || 5000
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const messages = extractMessages(request);
|
|
96
|
+
const model = request.model || 'claude-sonnet-4-20250514';
|
|
97
|
+
const maxTokens = request.max_tokens || 4096;
|
|
98
|
+
|
|
99
|
+
const cacheKey = cache.generateKey(messages);
|
|
100
|
+
const cachedResponse = await cache.get(cacheKey);
|
|
101
|
+
|
|
102
|
+
if (cachedResponse && settings.localCacheEnabled) {
|
|
103
|
+
logger.info('[Plexor] Local cache hit');
|
|
104
|
+
return output({
|
|
105
|
+
...request,
|
|
106
|
+
_plexor: {
|
|
107
|
+
source: 'local_cache',
|
|
108
|
+
latency_ms: Date.now() - startTime
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
logger.debug('Calling Plexor API...');
|
|
114
|
+
|
|
115
|
+
const result = await client.optimize({
|
|
116
|
+
messages: messages,
|
|
117
|
+
model: model,
|
|
118
|
+
max_tokens: maxTokens,
|
|
119
|
+
task_hint: detectTaskType(messages),
|
|
120
|
+
context: {
|
|
121
|
+
session_id: request._session_id,
|
|
122
|
+
turn_number: request._turn_number,
|
|
123
|
+
cwd: process.cwd()
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const savingsPercent = ((result.original_tokens - result.optimized_tokens) / result.original_tokens * 100).toFixed(1);
|
|
128
|
+
|
|
129
|
+
logger.info(`[Plexor] Optimized: ${result.original_tokens} → ${result.optimized_tokens} tokens (${savingsPercent}% saved)`);
|
|
130
|
+
|
|
131
|
+
if (result.recommended_provider !== 'anthropic') {
|
|
132
|
+
logger.info(`[Plexor] Recommended: ${result.recommended_provider} (~$${result.estimated_cost.toFixed(4)})`);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const optimizedRequest = {
|
|
136
|
+
...request,
|
|
137
|
+
messages: result.optimized_messages,
|
|
138
|
+
plexor_cwd: process.cwd(),
|
|
139
|
+
_plexor: {
|
|
140
|
+
request_id: result.request_id,
|
|
141
|
+
original_tokens: result.original_tokens,
|
|
142
|
+
optimized_tokens: result.optimized_tokens,
|
|
143
|
+
tokens_saved: result.tokens_saved,
|
|
144
|
+
savings_percent: parseFloat(savingsPercent),
|
|
145
|
+
recommended_provider: result.recommended_provider,
|
|
146
|
+
recommended_model: result.recommended_model,
|
|
147
|
+
estimated_cost: result.estimated_cost,
|
|
148
|
+
baseline_cost: result.baseline_cost,
|
|
149
|
+
latency_ms: Date.now() - startTime,
|
|
150
|
+
source: 'plexor_api',
|
|
151
|
+
cwd: process.cwd()
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
await cache.setMetadata(result.request_id, {
|
|
156
|
+
original_tokens: result.original_tokens,
|
|
157
|
+
optimized_tokens: result.optimized_tokens,
|
|
158
|
+
recommended_provider: result.recommended_provider,
|
|
159
|
+
timestamp: Date.now()
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
return output(optimizedRequest);
|
|
163
|
+
|
|
164
|
+
} catch (error) {
|
|
165
|
+
logger.error(`[Plexor] Error: ${error.message}`);
|
|
166
|
+
logger.debug(error.stack);
|
|
167
|
+
|
|
168
|
+
try {
|
|
169
|
+
const input = await readStdin();
|
|
170
|
+
const request = JSON.parse(input);
|
|
171
|
+
return output({
|
|
172
|
+
...request,
|
|
173
|
+
_plexor: {
|
|
174
|
+
error: error.message,
|
|
175
|
+
source: 'passthrough'
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
} catch {
|
|
179
|
+
process.exit(1);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async function readStdin() {
|
|
185
|
+
return new Promise((resolve, reject) => {
|
|
186
|
+
const chunks = [];
|
|
187
|
+
|
|
188
|
+
process.stdin.on('data', (chunk) => {
|
|
189
|
+
chunks.push(chunk);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
process.stdin.on('end', () => {
|
|
193
|
+
resolve(Buffer.concat(chunks).toString('utf8'));
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
process.stdin.on('error', reject);
|
|
197
|
+
|
|
198
|
+
setTimeout(() => {
|
|
199
|
+
reject(new Error('Stdin read timeout'));
|
|
200
|
+
}, 5000);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function output(data) {
|
|
205
|
+
const json = JSON.stringify(data);
|
|
206
|
+
process.stdout.write(json);
|
|
207
|
+
process.exit(0);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function extractMessages(request) {
|
|
211
|
+
if (Array.isArray(request.messages)) {
|
|
212
|
+
return request.messages;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (request.prompt) {
|
|
216
|
+
return [{ role: 'user', content: request.prompt }];
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (request.system && request.user) {
|
|
220
|
+
return [
|
|
221
|
+
{ role: 'system', content: request.system },
|
|
222
|
+
{ role: 'user', content: request.user }
|
|
223
|
+
];
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return [];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function detectTaskType(messages) {
|
|
230
|
+
if (!messages || messages.length === 0) {
|
|
231
|
+
return 'general';
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const lastUserMessage = [...messages]
|
|
235
|
+
.reverse()
|
|
236
|
+
.find(m => m.role === 'user');
|
|
237
|
+
|
|
238
|
+
if (!lastUserMessage) {
|
|
239
|
+
return 'general';
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const content = lastUserMessage.content.toLowerCase();
|
|
243
|
+
|
|
244
|
+
if (/```|function|class|import|export|const |let |var |def |async |await/.test(content)) {
|
|
245
|
+
return 'code_generation';
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (/test|spec|jest|pytest|unittest|describe\(|it\(|expect\(/.test(content)) {
|
|
249
|
+
return 'test_generation';
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (/fix|bug|error|issue|debug|trace|exception|crash/.test(content)) {
|
|
253
|
+
return 'debugging';
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (/refactor|improve|optimize|clean|restructure/.test(content)) {
|
|
257
|
+
return 'refactoring';
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (/document|readme|comment|explain|docstring/.test(content)) {
|
|
261
|
+
return 'documentation';
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (/review|check|audit|assess|evaluate/.test(content)) {
|
|
265
|
+
return 'code_review';
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (/analyze|understand|what does|how does|explain/.test(content)) {
|
|
269
|
+
return 'analysis';
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return 'general';
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Detect if this is an agentic/tool-using request that should not be optimized.
|
|
277
|
+
* Modifying messages in agent loops breaks the loop detection and causes infinite loops.
|
|
278
|
+
*/
|
|
279
|
+
function isAgenticRequest(request) {
|
|
280
|
+
// Check if request has tools defined
|
|
281
|
+
if (request.tools && request.tools.length > 0) {
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Check if any message contains tool use or tool results
|
|
286
|
+
const messages = request.messages || [];
|
|
287
|
+
for (const msg of messages) {
|
|
288
|
+
// Tool use in content (Claude format)
|
|
289
|
+
if (msg.content && Array.isArray(msg.content)) {
|
|
290
|
+
for (const block of msg.content) {
|
|
291
|
+
if (block.type === 'tool_use' || block.type === 'tool_result') {
|
|
292
|
+
return true;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Tool role (OpenAI format)
|
|
298
|
+
if (msg.role === 'tool') {
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Function call (OpenAI format)
|
|
303
|
+
if (msg.function_call || msg.tool_calls) {
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Check for assistant messages with tool indicators
|
|
309
|
+
for (const msg of messages) {
|
|
310
|
+
if (msg.role === 'assistant' && msg.content) {
|
|
311
|
+
const content = typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content);
|
|
312
|
+
// Detect common tool use patterns in Claude Code
|
|
313
|
+
if (/\[Bash\]|\[Read\]|\[Write\]|\[Edit\]|\[Glob\]|\[Grep\]/.test(content)) {
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Check for multi-turn conversations (likely agentic)
|
|
320
|
+
const assistantMessages = messages.filter(m => m.role === 'assistant');
|
|
321
|
+
if (assistantMessages.length > 2) {
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Detect if this is a slash command request that should not be optimized.
|
|
330
|
+
* Slash commands like /plexor-status need to pass through unchanged.
|
|
331
|
+
*/
|
|
332
|
+
function isSlashCommand(request) {
|
|
333
|
+
const messages = request.messages || [];
|
|
334
|
+
|
|
335
|
+
// Check the last user message for slash command patterns
|
|
336
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
337
|
+
const msg = messages[i];
|
|
338
|
+
if (msg.role === 'user') {
|
|
339
|
+
const content = typeof msg.content === 'string' ? msg.content : '';
|
|
340
|
+
// Detect slash commands at the start of user message
|
|
341
|
+
if (/^\/[a-z-]+/i.test(content.trim())) {
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
344
|
+
// Detect <command-name> tags (Claude Code skill invocation)
|
|
345
|
+
if (/<command-name>/.test(content)) {
|
|
346
|
+
return true;
|
|
347
|
+
}
|
|
348
|
+
// Detect plexor-related commands
|
|
349
|
+
if (/plexor-(?:status|login|logout|mode|provider|enabled|settings)/i.test(content)) {
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
break; // Only check last user message
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// Check for system messages with skill instructions
|
|
357
|
+
for (const msg of messages) {
|
|
358
|
+
if (msg.role === 'system') {
|
|
359
|
+
const content = typeof msg.content === 'string' ? msg.content : '';
|
|
360
|
+
if (/# Plexor (?:Status|Login|Logout|Mode|Provider|Enabled|Settings)/i.test(content)) {
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Detect if this request involves CLI/shell commands that need tool execution.
|
|
371
|
+
* These should pass through to ensure proper tool calling behavior.
|
|
372
|
+
*/
|
|
373
|
+
function requiresToolExecution(request) {
|
|
374
|
+
const messages = request.messages || [];
|
|
375
|
+
|
|
376
|
+
// Check user messages for CLI command patterns
|
|
377
|
+
for (const msg of messages) {
|
|
378
|
+
if (msg.role === 'user') {
|
|
379
|
+
const content = typeof msg.content === 'string' ? msg.content : '';
|
|
380
|
+
const contentLower = content.toLowerCase();
|
|
381
|
+
|
|
382
|
+
// Azure CLI patterns
|
|
383
|
+
if (/\baz\s+(login|logout|group|account|vm|storage|webapp|aks|acr|keyvault|sql|cosmos|network)/i.test(content)) {
|
|
384
|
+
return true;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Common CLI execution requests
|
|
388
|
+
if (/\b(run|execute|show|list|create|delete|update)\b.*\b(az|aws|gcloud|kubectl|docker|npm|git)\b/i.test(content)) {
|
|
389
|
+
return true;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// Direct command patterns
|
|
393
|
+
if (/^(az|aws|gcloud|kubectl|docker|npm|yarn|pip|cargo|go)\s+/m.test(content)) {
|
|
394
|
+
return true;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// Imperative CLI requests
|
|
398
|
+
if (/\b(list|show|get|describe)\s+(resource\s*groups?|rgs?|vms?|instances?|clusters?|pods?|containers?)/i.test(content)) {
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
return false;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
main().catch((error) => {
|
|
408
|
+
console.error(`[Plexor] Fatal error: ${error.message}`);
|
|
409
|
+
process.exit(1);
|
|
410
|
+
});
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Plexor Response Tracking Hook
|
|
5
|
+
*
|
|
6
|
+
* This script runs after the LLM response is received.
|
|
7
|
+
* It tracks response metrics for analytics and updates session stats.
|
|
8
|
+
*
|
|
9
|
+
* Input: JSON object with response content, tokens used, etc.
|
|
10
|
+
* Output: Passthrough (no modifications)
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const PlexorClient = require('../lib/plexor-client');
|
|
14
|
+
const ConfigManager = require('../lib/config');
|
|
15
|
+
const LocalCache = require('../lib/cache');
|
|
16
|
+
const Logger = require('../lib/logger');
|
|
17
|
+
|
|
18
|
+
const logger = new Logger('track-response');
|
|
19
|
+
const config = new ConfigManager();
|
|
20
|
+
const cache = new LocalCache();
|
|
21
|
+
|
|
22
|
+
async function main() {
|
|
23
|
+
try {
|
|
24
|
+
const input = await readStdin();
|
|
25
|
+
const response = JSON.parse(input);
|
|
26
|
+
|
|
27
|
+
const settings = await config.load();
|
|
28
|
+
|
|
29
|
+
// If Plexor is disabled or no API key, just pass through
|
|
30
|
+
if (!settings.enabled || !settings.apiKey) {
|
|
31
|
+
return output(response);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Check if this response has Plexor metadata
|
|
35
|
+
const plexorMeta = response._plexor;
|
|
36
|
+
if (!plexorMeta || !plexorMeta.request_id) {
|
|
37
|
+
return output(response);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Get stored metadata for this request
|
|
41
|
+
const metadata = await cache.getMetadata(plexorMeta.request_id);
|
|
42
|
+
if (!metadata) {
|
|
43
|
+
return output(response);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Calculate output tokens (approximate)
|
|
47
|
+
const outputTokens = estimateTokens(response.content || '');
|
|
48
|
+
|
|
49
|
+
// Log response tracking
|
|
50
|
+
logger.info('[Plexor] Response tracked', {
|
|
51
|
+
request_id: plexorMeta.request_id,
|
|
52
|
+
input_tokens: metadata.optimized_tokens,
|
|
53
|
+
output_tokens: outputTokens,
|
|
54
|
+
provider: metadata.recommended_provider
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// In production, we would send this data to the API for analytics
|
|
58
|
+
// For now, just log locally
|
|
59
|
+
|
|
60
|
+
// Pass through unchanged
|
|
61
|
+
return output(response);
|
|
62
|
+
|
|
63
|
+
} catch (error) {
|
|
64
|
+
logger.error(`[Plexor] Tracking error: ${error.message}`);
|
|
65
|
+
|
|
66
|
+
// On any error, pass through unchanged
|
|
67
|
+
try {
|
|
68
|
+
const input = await readStdin();
|
|
69
|
+
return output(JSON.parse(input));
|
|
70
|
+
} catch {
|
|
71
|
+
process.exit(1);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function readStdin() {
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
const chunks = [];
|
|
79
|
+
|
|
80
|
+
process.stdin.on('data', (chunk) => {
|
|
81
|
+
chunks.push(chunk);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
process.stdin.on('end', () => {
|
|
85
|
+
resolve(Buffer.concat(chunks).toString('utf8'));
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
process.stdin.on('error', reject);
|
|
89
|
+
|
|
90
|
+
setTimeout(() => {
|
|
91
|
+
reject(new Error('Stdin read timeout'));
|
|
92
|
+
}, 2000);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function output(data) {
|
|
97
|
+
const json = JSON.stringify(data);
|
|
98
|
+
process.stdout.write(json);
|
|
99
|
+
process.exit(0);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function estimateTokens(text) {
|
|
103
|
+
// Approximate: ~4 characters per token
|
|
104
|
+
return Math.max(1, Math.ceil(text.length / 4));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
main().catch((error) => {
|
|
108
|
+
console.error(`[Plexor] Fatal error: ${error.message}`);
|
|
109
|
+
process.exit(1);
|
|
110
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plexor-dev/claude-code-plugin",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.6",
|
|
4
4
|
"description": "LLM cost optimization plugin for Claude Code - Save up to 90% on AI costs",
|
|
5
5
|
"main": "lib/constants.js",
|
|
6
6
|
"scripts": {
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
12
|
"commands/",
|
|
13
|
+
"hooks/",
|
|
13
14
|
"scripts/",
|
|
14
15
|
"lib/",
|
|
15
16
|
"README.md",
|
package/lib/constants.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Plexor Claude Code Plugin - Constants
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const os = require('os');
|
|
7
|
-
|
|
8
|
-
module.exports = {
|
|
9
|
-
// API endpoints
|
|
10
|
-
PLEXOR_API_URL: process.env.PLEXOR_API_URL || 'https://api.plexor.dev',
|
|
11
|
-
PLEXOR_GATEWAY_URL: process.env.PLEXOR_GATEWAY_URL || 'https://api.plexor.dev/v1',
|
|
12
|
-
PLEXOR_AUTH_URL: 'https://plexor.dev/auth/device',
|
|
13
|
-
|
|
14
|
-
// File paths
|
|
15
|
-
PLEXOR_CONFIG_DIR: process.env.PLEXOR_CONFIG_DIR || path.join(os.homedir(), '.plexor'),
|
|
16
|
-
PLEXOR_CONFIG_FILE: path.join(
|
|
17
|
-
process.env.PLEXOR_CONFIG_DIR || path.join(os.homedir(), '.plexor'),
|
|
18
|
-
'config.json'
|
|
19
|
-
),
|
|
20
|
-
CLAUDE_COMMANDS_DIR: path.join(os.homedir(), '.claude', 'commands'),
|
|
21
|
-
|
|
22
|
-
// Config schema version
|
|
23
|
-
CONFIG_VERSION: 1,
|
|
24
|
-
|
|
25
|
-
// Default settings
|
|
26
|
-
DEFAULTS: {
|
|
27
|
-
enabled: true,
|
|
28
|
-
preferred_provider: 'auto',
|
|
29
|
-
telemetry: true,
|
|
30
|
-
local_cache: false
|
|
31
|
-
},
|
|
32
|
-
|
|
33
|
-
// API key prefix for identification
|
|
34
|
-
API_KEY_PREFIX: 'plx_',
|
|
35
|
-
|
|
36
|
-
// Timeouts (ms)
|
|
37
|
-
DEVICE_CODE_POLL_INTERVAL: 5000,
|
|
38
|
-
DEVICE_CODE_TIMEOUT: 900000, // 15 minutes
|
|
39
|
-
API_TIMEOUT: 30000
|
|
40
|
-
};
|