-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfetch-session.mjs
More file actions
executable file
·322 lines (277 loc) · 9.79 KB
/
fetch-session.mjs
File metadata and controls
executable file
·322 lines (277 loc) · 9.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env node
/**
* Fetch and parse pi-share (shittycodingagent.ai/buildwithpi.ai/buildwithpi.com) session exports.
*
* Usage:
* node fetch-session.mjs <url-or-gist-id> [--header] [--entries] [--system] [--tools] [--human-summary] [--no-cache]
*
* Options:
* (no flag) Output full session data JSON
* --header Output just the session header
* --entries Output entries as JSON lines (one per line)
* --system Output the system prompt
* --tools Output tool definitions
* --human-summary Summarize what the human did in this session (uses haiku-4-5)
* --no-cache Bypass cache and fetch fresh
*/
import { mkdirSync, readFileSync, writeFileSync, existsSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { spawnSync } from 'child_process';
const CACHE_DIR = join(tmpdir(), 'pi-share-cache');
const args = process.argv.slice(2);
const input = args.find(a => !a.startsWith('--'));
const flags = new Set(args.filter(a => a.startsWith('--')));
if (!input) {
console.error('Usage: node fetch-session.mjs <url-or-gist-id> [--header|--entries|--system|--tools]');
process.exit(1);
}
// Cache functions
function getCachePath(gistId) {
return join(CACHE_DIR, `${gistId}.json`);
}
function readCache(gistId) {
const path = getCachePath(gistId);
if (existsSync(path)) {
return JSON.parse(readFileSync(path, 'utf-8'));
}
return null;
}
function writeCache(gistId, data) {
mkdirSync(CACHE_DIR, { recursive: true });
writeFileSync(getCachePath(gistId), JSON.stringify(data));
}
// Extract gist ID from URL or use directly
function extractGistId(input) {
// Handle full URLs like https://shittycodingagent.ai/session/?<id>
const queryMatch = input.match(/[?&]([a-f0-9]{32})/i);
if (queryMatch) return queryMatch[1];
// Handle path-based URLs like https://buildwithpi.ai/session/<id>
const pathMatch = input.match(/\/session\/?([a-f0-9]{32})/i);
if (pathMatch) return pathMatch[1];
// Handle direct gist ID
if (/^[a-f0-9]{32}$/i.test(input)) return input;
// Handle gist.github.com URLs
const gistMatch = input.match(/gist\.github\.com\/[^/]+\/([a-f0-9]+)/i);
if (gistMatch) return gistMatch[1];
throw new Error(`Cannot extract gist ID from: ${input}`);
}
// Fetch session HTML from gist
async function fetchSessionHtml(gistId) {
const gistRes = await fetch(`https://api.github.com/gists/${gistId}`);
if (!gistRes.ok) {
if (gistRes.status === 404) throw new Error('Session not found (gist deleted or invalid ID)');
throw new Error(`GitHub API error: ${gistRes.status}`);
}
const gist = await gistRes.json();
const file = gist.files?.['session.html'];
if (!file) {
const available = Object.keys(gist.files || {}).join(', ') || 'none';
throw new Error(`No session.html in gist. Available: ${available}`);
}
// Fetch raw content if truncated
if (file.truncated && file.raw_url) {
const rawRes = await fetch(file.raw_url);
if (!rawRes.ok) throw new Error('Failed to fetch raw content');
return rawRes.text();
}
return file.content;
}
// Extract base64 session data from HTML
function extractSessionData(html) {
// New format: <script id="session-data" type="application/json">BASE64</script>
const match = html.match(/<script[^>]*id="session-data"[^>]*>([^<]+)<\/script>/);
if (match) {
const base64 = match[1].trim();
const json = Buffer.from(base64, 'base64').toString('utf-8');
return JSON.parse(json);
}
throw new Error('No session data found in HTML. This may be an older export format without embedded data.');
}
// Truncate text to maxLen, adding ellipsis if truncated
function truncate(text, maxLen = 150) {
if (!text || text.length <= maxLen) return text;
return text.slice(0, maxLen) + '...';
}
// Extract condensed session data for human summary
function extractForSummary(data) {
const turns = [];
let turnNumber = 0;
for (const entry of data.entries) {
if (entry.type !== 'message') continue;
const msg = entry.message;
if (!msg || !msg.role) continue;
if (msg.role === 'user') {
turnNumber++;
// Extract user text
const textParts = (msg.content || [])
.filter(c => c.type === 'text')
.map(c => c.text)
.join('\n');
if (textParts.trim()) {
turns.push({
turn: turnNumber,
role: 'human',
text: textParts
});
}
} else if (msg.role === 'assistant') {
// Extract condensed assistant info: brief text + tool summary
const textParts = [];
const toolCalls = [];
for (const block of (msg.content || [])) {
if (block.type === 'text' && block.text) {
// Just first 200 chars of assistant text for context
textParts.push(truncate(block.text, 200));
} else if (block.type === 'toolCall') {
// Condense tool call: name + truncated key info
let summary = block.toolName;
if (block.args) {
if (block.args.path) {
summary += `: ${truncate(block.args.path, 100)}`;
} else if (block.args.command) {
summary += `: ${truncate(block.args.command, 100)}`;
} else {
// Generic args truncation
const argsStr = JSON.stringify(block.args);
summary += `: ${truncate(argsStr, 100)}`;
}
}
toolCalls.push(summary);
}
}
if (textParts.length || toolCalls.length) {
turns.push({
turn: turnNumber,
role: 'assistant',
text: textParts.length ? textParts[0] : null,
tools: toolCalls.length ? toolCalls : null
});
}
} else if (msg.role === 'toolResult') {
// Just note if there was an error
const hasError = (msg.content || []).some(c => c.isError);
if (hasError) {
turns.push({
turn: turnNumber,
role: 'tool_error',
text: 'Tool returned an error'
});
}
}
}
return {
sessionId: data.header?.id,
timestamp: data.header?.timestamp,
cwd: data.header?.cwd,
turns
};
}
// Format condensed data as text for the summarizer
function formatForSummary(condensed) {
const lines = [];
lines.push(`Session: ${condensed.sessionId || 'unknown'}`);
lines.push(`Time: ${condensed.timestamp || 'unknown'}`);
lines.push(`Directory: ${condensed.cwd || 'unknown'}`);
lines.push('');
lines.push('=== Conversation ===');
lines.push('');
for (const turn of condensed.turns) {
if (turn.role === 'human') {
lines.push(`[Turn ${turn.turn}] HUMAN:`);
lines.push(turn.text);
lines.push('');
} else if (turn.role === 'assistant') {
lines.push(`[Turn ${turn.turn}] ASSISTANT (condensed):`);
if (turn.text) {
lines.push(` Response: ${turn.text}`);
}
if (turn.tools && turn.tools.length) {
lines.push(` Tools used: ${turn.tools.join(', ')}`);
}
lines.push('');
} else if (turn.role === 'tool_error') {
lines.push(`[Turn ${turn.turn}] ⚠️ Tool error occurred`);
lines.push('');
}
}
return lines.join('\n');
}
// Generate human summary using haiku via pi
async function generateHumanSummary(data) {
const condensed = extractForSummary(data);
const formatted = formatForSummary(condensed);
const prompt = `You are analyzing a coding agent session transcript. Your task is to summarize what the HUMAN did, not what the AI agent did.
Focus on:
1. What was the human's initial goal/request?
2. How many times did they have to re-prompt or steer the agent?
3. What kind of steering did they do? (corrections, clarifications, changes of direction, expressing frustration, etc.)
4. Did the human have to intervene when things went wrong?
5. How specific vs vague were their instructions?
Write a ~300 word summary in third person ("The user asked...", "They then had to clarify...").
Include a brief note about what domain/tools were involved for context, but keep focus on the human's actions and experience.
Here is the condensed session transcript:
${formatted}`;
try {
const result = spawnSync('pi', [
'--provider', 'anthropic',
'--model', 'claude-haiku-4-5',
'--no-tools',
'--no-session',
'-p',
prompt
], {
encoding: 'utf-8',
maxBuffer: 10 * 1024 * 1024,
timeout: 60000
});
if (result.error) {
throw result.error;
}
if (result.status !== 0) {
throw new Error(result.stderr || 'pi command failed');
}
return result.stdout.trim();
} catch (err) {
throw new Error(`Failed to generate summary: ${err.message}`);
}
}
// Main
async function main() {
try {
const gistId = extractGistId(input);
// Check cache first (unless --no-cache)
let data = null;
if (!flags.has('--no-cache')) {
data = readCache(gistId);
}
if (!data) {
const html = await fetchSessionHtml(gistId);
data = extractSessionData(html);
writeCache(gistId, data);
}
if (flags.has('--header')) {
console.log(JSON.stringify(data.header));
} else if (flags.has('--entries')) {
// Output as JSON lines - one entry per line
for (const entry of data.entries) {
console.log(JSON.stringify(entry));
}
} else if (flags.has('--system')) {
console.log(data.systemPrompt || '');
} else if (flags.has('--tools')) {
console.log(JSON.stringify(data.tools || []));
} else if (flags.has('--human-summary')) {
// Generate human-centric summary using haiku
const summary = await generateHumanSummary(data);
console.log(summary);
} else {
// Default: full session data
console.log(JSON.stringify(data));
}
} catch (err) {
console.error(err.message);
process.exit(1);
}
}
main();