phewsh 0.5.4 → 0.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.
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,49 @@ async function upsert(table, data, accessToken) {
77
77
  return res.json();
78
78
  }
79
79
 
80
- module.exports = { sendOtp, verifyOtp, refreshSession, select, upsert, SUPABASE_URL, SUPABASE_ANON_KEY };
80
+ // SAP: fire-and-forget per-inference tracking
81
+ // kWh estimates by model family (conservative)
82
+ const MODEL_KWH = {
83
+ 'claude-haiku': 0.00025,
84
+ 'claude-sonnet': 0.00060,
85
+ 'claude-opus': 0.00200,
86
+ 'gemini-flash': 0.00020,
87
+ 'deepseek': 0.00030,
88
+ 'kimi': 0.00040,
89
+ };
90
+
91
+ function estimateKwh(model, totalTokens) {
92
+ let base = 0.0004;
93
+ if (model) {
94
+ const lower = model.toLowerCase();
95
+ for (const [key, val] of Object.entries(MODEL_KWH)) {
96
+ if (lower.includes(key)) { base = val; break; }
97
+ }
98
+ }
99
+ const scale = Math.max(0.5, Math.min(4, (totalTokens || 1000) / 1000));
100
+ return base * scale;
101
+ }
102
+
103
+ function trackSap({ userId, source = 'cli', model, promptTokens, completionTokens, accessToken } = {}) {
104
+ const totalTokens = (promptTokens || 0) + (completionTokens || 0);
105
+ const kwh = estimateKwh(model, totalTokens);
106
+ const co2_g = kwh * 500; // US grid avg gCO2/kWh
107
+ const water_ml = kwh * 1800;
108
+
109
+ req('/rest/v1/rpc/track_sap_event', {
110
+ method: 'POST',
111
+ body: JSON.stringify({
112
+ p_user_id: userId || null,
113
+ p_session_id: null,
114
+ p_source: source,
115
+ p_model: model || null,
116
+ p_prompt_tokens: promptTokens || null,
117
+ p_completion_tokens: completionTokens || null,
118
+ p_kwh: kwh,
119
+ p_co2_g: co2_g,
120
+ p_water_ml: water_ml,
121
+ }),
122
+ }, accessToken).catch(() => { /* never surface SAP errors */ });
123
+ }
124
+
125
+ module.exports = { sendOtp, verifyOtp, refreshSession, select, upsert, trackSap, SUPABASE_URL, SUPABASE_ANON_KEY };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phewsh",
3
- "version": "0.5.4",
3
+ "version": "0.6.0",
4
4
  "description": "Turn intent into action. Structure your thinking, execute your next step.",
5
5
  "bin": {
6
6
  "phewsh": "bin/phewsh.js"