Skip to content

Commit 3384421

Browse files
authored
feat: include tool context in session history export (#42)
1 parent 1a1c91e commit 3384421

3 files changed

Lines changed: 241 additions & 8 deletions

File tree

‎src/renderer/App.tsx‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import {
1919
} from './components/WorkbenchViews';
2020
import { Transcript } from './components/Transcript';
2121
import { CommandPalette } from './components/CommandPalette';
22+
import {
23+
sessionExportFilename,
24+
sessionExportMarkdown,
25+
} from './export-session';
2226
import { timeAgo, useWorkbench } from './useWorkbench';
2327

2428
function App() {
@@ -108,19 +112,20 @@ function App() {
108112
const live = b.tabs;
109113
const exportSession = () => {
110114
if (!preview) return;
111-
const text = preview.entries
112-
.map((e) =>
113-
e.kind === 'tool'
114-
? `## ${e.tool}\n\n${e.output}`
115-
: `## ${e.kind}\n\n${e.text}`,
116-
)
117-
.join('\n\n');
115+
const exportedAt = new Date();
116+
const text = sessionExportMarkdown({
117+
title: preview.info.title,
118+
cwd: preview.info.cwd,
119+
model: preview.info.model,
120+
exportedAt,
121+
entries: preview.entries,
122+
});
118123
const url = URL.createObjectURL(
119124
new Blob([text], { type: 'text/markdown' }),
120125
);
121126
const a = document.createElement('a');
122127
a.href = url;
123-
a.download = 'pidex-session.md';
128+
a.download = sessionExportFilename(preview.info.title, exportedAt);
124129
a.click();
125130
setTimeout(() => URL.revokeObjectURL(url), 1000);
126131
};

‎src/renderer/export-session.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import type { Entry } from './timeline';
2+
3+
export type SessionExportInput = {
4+
title: string;
5+
cwd: string;
6+
model: string;
7+
exportedAt: Date;
8+
entries: Entry[];
9+
};
10+
11+
function fenced(text: string): string {
12+
let longest = 0;
13+
let run = 0;
14+
for (const ch of text) {
15+
run = ch === '`' ? run + 1 : 0;
16+
if (run > longest) longest = run;
17+
}
18+
const fence = '`'.repeat(Math.max(3, longest + 1));
19+
return `${fence}\n${text.replace(/\n$/, '')}\n${fence}`;
20+
}
21+
22+
function toolStatus(entry: Extract<Entry, { kind: 'tool' }>): string {
23+
if (entry.running) return 'Running';
24+
return entry.ok ? 'Done' : 'Failed';
25+
}
26+
27+
function toolHeading(entry: Extract<Entry, { kind: 'tool' }>): string {
28+
const args =
29+
entry.args.length > 120 ? `${entry.args.slice(0, 120)}...` : entry.args;
30+
const summary = args ? ` / ${args}` : '';
31+
return `## ${entry.tool}${summary} (${toolStatus(entry)})`;
32+
}
33+
34+
export function sessionExportMarkdown(input: SessionExportInput): string {
35+
const parts: string[] = [
36+
`# ${input.title.trim() || 'Untitled session'}`,
37+
`Exported from pidex at ${input.exportedAt.toISOString()}.`,
38+
];
39+
if (input.cwd.trim()) parts.push(`It ran in \`${input.cwd.trim()}\`.`);
40+
if (input.model.trim()) parts.push(`It used \`${input.model.trim()}\`.`);
41+
if (input.entries.length === 0) {
42+
parts.push('No messages in this selection.');
43+
return `${parts.join('\n\n')}\n`;
44+
}
45+
for (const entry of input.entries) {
46+
if (entry.kind === 'user') {
47+
parts.push(`## You\n\n${entry.text}`);
48+
} else if (entry.kind === 'assistant') {
49+
const body: string[] = [];
50+
if (entry.text) body.push(entry.text);
51+
if (entry.thinking) body.push(`### Thinking\n\n${entry.thinking}`);
52+
parts.push(`## Pi\n\n${body.join('\n\n') || 'No message text.'}`);
53+
} else {
54+
const inputBlock = entry.input ? fenced(entry.input) : 'No input.';
55+
const outputBlock = entry.output ? fenced(entry.output) : 'No output.';
56+
parts.push(
57+
`${toolHeading(entry)}\n\n### Input\n\n${inputBlock}\n\n### Output\n\n${outputBlock}`,
58+
);
59+
}
60+
}
61+
return `${parts.join('\n\n')}\n`;
62+
}
63+
64+
export function sessionExportFilename(title: string, exportedAt: Date): string {
65+
const slug = title
66+
.toLowerCase()
67+
.replace(/[^a-z0-9]+/g, '-')
68+
.replace(/^-+|-+$/g, '')
69+
.slice(0, 60)
70+
.replace(/-+$/, '');
71+
const day = exportedAt.toISOString().slice(0, 10);
72+
return `pidex-${slug || 'session'}-${day}.md`;
73+
}

‎tests/unit/export-session.test.ts‎

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import { describe, expect, it } from 'vitest';
2+
import {
3+
sessionExportFilename,
4+
sessionExportMarkdown,
5+
} from '../../src/renderer/export-session';
6+
import type { Entry } from '../../src/renderer/timeline';
7+
8+
const exportedAt = new Date('2026-11-08T01:02:03.000Z');
9+
10+
const bashDone: Extract<Entry, { kind: 'tool' }> = {
11+
key: 'tool-1',
12+
kind: 'tool',
13+
toolId: 'call_1',
14+
tool: 'bash',
15+
args: 'npm test',
16+
input: 'npm test',
17+
output: '3 passed',
18+
running: false,
19+
ok: true,
20+
};
21+
22+
describe('sessionExportMarkdown', () => {
23+
it('writes the full conversation with context and tool input', () => {
24+
const markdown = sessionExportMarkdown({
25+
title: 'Fix the export',
26+
cwd: '/repo/app',
27+
model: 'anthropic/claude-test',
28+
exportedAt,
29+
entries: [
30+
{ key: 'u1', kind: 'user', text: 'please fix it' },
31+
{
32+
key: 'a1',
33+
kind: 'assistant',
34+
text: 'On it.',
35+
thinking: 'check the tests first',
36+
streaming: false,
37+
},
38+
bashDone,
39+
],
40+
});
41+
expect(markdown).toBe(
42+
[
43+
'# Fix the export',
44+
'',
45+
'Exported from pidex at 2026-11-08T01:02:03.000Z.',
46+
'',
47+
'It ran in `/repo/app`.',
48+
'',
49+
'It used `anthropic/claude-test`.',
50+
'',
51+
'## You',
52+
'',
53+
'please fix it',
54+
'',
55+
'## Pi',
56+
'',
57+
'On it.',
58+
'',
59+
'### Thinking',
60+
'',
61+
'check the tests first',
62+
'',
63+
'## bash / npm test (Done)',
64+
'',
65+
'### Input',
66+
'',
67+
'```',
68+
'npm test',
69+
'```',
70+
'',
71+
'### Output',
72+
'',
73+
'```',
74+
'3 passed',
75+
'```',
76+
'',
77+
].join('\n'),
78+
);
79+
});
80+
81+
it('marks failed and running tools with the card wording', () => {
82+
const failed = sessionExportMarkdown({
83+
title: 'T',
84+
cwd: '',
85+
model: '',
86+
exportedAt,
87+
entries: [{ ...bashDone, key: 'f', ok: false, output: 'boom' }],
88+
});
89+
expect(failed).toContain('## bash / npm test (Failed)');
90+
const running = sessionExportMarkdown({
91+
title: 'T',
92+
cwd: '',
93+
model: '',
94+
exportedAt,
95+
entries: [{ ...bashDone, key: 'r', running: true, output: '' }],
96+
});
97+
expect(running).toContain('## bash / npm test (Running)');
98+
});
99+
100+
it('keeps an empty input and output readable', () => {
101+
const markdown = sessionExportMarkdown({
102+
title: 'T',
103+
cwd: '',
104+
model: '',
105+
exportedAt,
106+
entries: [{ ...bashDone, key: 'e', input: '', output: '' }],
107+
});
108+
expect(markdown).toContain('### Input\n\nNo input.');
109+
expect(markdown).toContain('### Output\n\nNo output.');
110+
});
111+
112+
it('widens the fence when the output holds backticks', () => {
113+
const markdown = sessionExportMarkdown({
114+
title: 'T',
115+
cwd: '',
116+
model: '',
117+
exportedAt,
118+
entries: [
119+
{ ...bashDone, key: 'b', output: 'before\n```\ncode\n```\nafter' },
120+
],
121+
});
122+
expect(markdown).toContain('\n````\nbefore\n```\ncode\n```\nafter\n````');
123+
});
124+
125+
it('handles an empty selection and a blank title', () => {
126+
const markdown = sessionExportMarkdown({
127+
title: ' ',
128+
cwd: '',
129+
model: '',
130+
exportedAt,
131+
entries: [],
132+
});
133+
expect(markdown).toContain('# Untitled session');
134+
expect(markdown).toContain('No messages in this selection.');
135+
expect(markdown).not.toContain('It ran in');
136+
expect(markdown).not.toContain('It used');
137+
});
138+
});
139+
140+
describe('sessionExportFilename', () => {
141+
it('builds the name from the title and export day', () => {
142+
expect(sessionExportFilename('Fix: the Bug!', exportedAt)).toBe(
143+
'pidex-fix-the-bug-2026-11-08.md',
144+
);
145+
});
146+
147+
it('falls back when the title has no usable words', () => {
148+
expect(sessionExportFilename('!!!', exportedAt)).toBe(
149+
'pidex-session-2026-11-08.md',
150+
);
151+
expect(sessionExportFilename('', exportedAt)).toBe(
152+
'pidex-session-2026-11-08.md',
153+
);
154+
});
155+
});

0 commit comments

Comments
 (0)