phewsh 0.5.4 → 0.6.1
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/ai.js +18 -8
- package/lib/supabase.js +50 -1
- package/package.json +1 -1
package/commands/ai.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
const path = require('path');
|
|
3
3
|
const os = require('os');
|
|
4
|
+
const { trackSap } = require('../lib/supabase');
|
|
4
5
|
|
|
5
6
|
const CONFIG_PATH = path.join(os.homedir(), '.phewsh', 'config.json');
|
|
6
7
|
const INTENT_DIR = path.join(process.cwd(), '.intent');
|
|
@@ -35,14 +36,10 @@ function buildSystemPrompt(intentFiles) {
|
|
|
35
36
|
return `You are a focused execution assistant. The user has structured intent artifacts that define what they are building. Use these as your primary context for every response — stay aligned with their vision, plan, and next actions.\n\n${sections}`;
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
async function streamResponse(apiKey, systemPrompt, userPrompt) {
|
|
39
|
+
async function streamResponse(apiKey, systemPrompt, userPrompt, userId, accessToken) {
|
|
40
|
+
const model = 'claude-sonnet-4-6';
|
|
39
41
|
const messages = [{ role: 'user', content: userPrompt }];
|
|
40
|
-
const body = {
|
|
41
|
-
model: 'claude-sonnet-4-6',
|
|
42
|
-
max_tokens: 1024,
|
|
43
|
-
messages,
|
|
44
|
-
stream: true,
|
|
45
|
-
};
|
|
42
|
+
const body = { model, max_tokens: 1024, messages, stream: true };
|
|
46
43
|
if (systemPrompt) body.system = systemPrompt;
|
|
47
44
|
|
|
48
45
|
const response = await fetch('https://api.anthropic.com/v1/messages', {
|
|
@@ -62,6 +59,9 @@ async function streamResponse(apiKey, systemPrompt, userPrompt) {
|
|
|
62
59
|
|
|
63
60
|
process.stdout.write('\n');
|
|
64
61
|
|
|
62
|
+
let promptTokens = null;
|
|
63
|
+
let completionTokens = null;
|
|
64
|
+
|
|
65
65
|
for await (const chunk of response.body) {
|
|
66
66
|
const text = Buffer.from(chunk).toString('utf-8');
|
|
67
67
|
const lines = text.split('\n').filter(l => l.startsWith('data: '));
|
|
@@ -73,11 +73,21 @@ async function streamResponse(apiKey, systemPrompt, userPrompt) {
|
|
|
73
73
|
if (parsed.type === 'content_block_delta' && parsed.delta?.text) {
|
|
74
74
|
process.stdout.write(parsed.delta.text);
|
|
75
75
|
}
|
|
76
|
+
// Capture token counts from Anthropic stream
|
|
77
|
+
if (parsed.type === 'message_start' && parsed.message?.usage) {
|
|
78
|
+
promptTokens = parsed.message.usage.input_tokens;
|
|
79
|
+
}
|
|
80
|
+
if (parsed.type === 'message_delta' && parsed.usage) {
|
|
81
|
+
completionTokens = parsed.usage.output_tokens;
|
|
82
|
+
}
|
|
76
83
|
} catch { /* skip malformed chunks */ }
|
|
77
84
|
}
|
|
78
85
|
}
|
|
79
86
|
|
|
80
87
|
process.stdout.write('\n\n');
|
|
88
|
+
|
|
89
|
+
// SAP: fire-and-forget after stream completes
|
|
90
|
+
trackSap({ userId, source: 'cli', model, promptTokens, completionTokens, accessToken });
|
|
81
91
|
}
|
|
82
92
|
|
|
83
93
|
async function main() {
|
|
@@ -135,7 +145,7 @@ async function main() {
|
|
|
135
145
|
console.log('\n No .intent/ found — running without project context');
|
|
136
146
|
}
|
|
137
147
|
|
|
138
|
-
await streamResponse(config.apiKey, systemPrompt, prompt);
|
|
148
|
+
await streamResponse(config.apiKey, systemPrompt, prompt, config.supabaseUserId, config.supabaseAccessToken);
|
|
139
149
|
return;
|
|
140
150
|
}
|
|
141
151
|
|
package/lib/supabase.js
CHANGED
|
@@ -77,4 +77,53 @@ async function upsert(table, data, accessToken) {
|
|
|
77
77
|
return res.json();
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
|
|
80
|
+
// SAP: fire-and-forget per-inference tracking
|
|
81
|
+
// Session ID — unique per CLI invocation, enables multi-prompt correlation
|
|
82
|
+
const { randomUUID } = require('crypto');
|
|
83
|
+
const CLI_SESSION_ID = randomUUID();
|
|
84
|
+
|
|
85
|
+
// kWh estimates by model family (conservative)
|
|
86
|
+
const MODEL_KWH = {
|
|
87
|
+
'claude-haiku': 0.00025,
|
|
88
|
+
'claude-sonnet': 0.00060,
|
|
89
|
+
'claude-opus': 0.00200,
|
|
90
|
+
'gemini-flash': 0.00020,
|
|
91
|
+
'deepseek': 0.00030,
|
|
92
|
+
'kimi': 0.00040,
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
function estimateKwh(model, totalTokens) {
|
|
96
|
+
let base = 0.0004;
|
|
97
|
+
if (model) {
|
|
98
|
+
const lower = model.toLowerCase();
|
|
99
|
+
for (const [key, val] of Object.entries(MODEL_KWH)) {
|
|
100
|
+
if (lower.includes(key)) { base = val; break; }
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const scale = Math.max(0.5, Math.min(4, (totalTokens || 1000) / 1000));
|
|
104
|
+
return base * scale;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function trackSap({ userId, source = 'cli', model, promptTokens, completionTokens, accessToken } = {}) {
|
|
108
|
+
const totalTokens = (promptTokens || 0) + (completionTokens || 0);
|
|
109
|
+
const kwh = estimateKwh(model, totalTokens);
|
|
110
|
+
const co2_g = kwh * 500; // US grid avg gCO2/kWh
|
|
111
|
+
const water_ml = kwh * 1800;
|
|
112
|
+
|
|
113
|
+
req('/rest/v1/rpc/track_sap_event', {
|
|
114
|
+
method: 'POST',
|
|
115
|
+
body: JSON.stringify({
|
|
116
|
+
p_user_id: userId || null,
|
|
117
|
+
p_session_id: CLI_SESSION_ID,
|
|
118
|
+
p_source: source,
|
|
119
|
+
p_model: model || null,
|
|
120
|
+
p_prompt_tokens: promptTokens || null,
|
|
121
|
+
p_completion_tokens: completionTokens || null,
|
|
122
|
+
p_kwh: kwh,
|
|
123
|
+
p_co2_g: co2_g,
|
|
124
|
+
p_water_ml: water_ml,
|
|
125
|
+
}),
|
|
126
|
+
}, accessToken).catch(() => { /* never surface SAP errors */ });
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
module.exports = { sendOtp, verifyOtp, refreshSession, select, upsert, trackSap, SUPABASE_URL, SUPABASE_ANON_KEY };
|