Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/renderer/export-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,41 @@ function handoffSection(cwd: string): string {
].join('\n');
}

function summarySection(entries: Entry[]): string {
const users = entries.filter((e) => e.kind === 'user').length;
const assistants = entries.filter((e) => e.kind === 'assistant').length;
const messages = users + assistants;
const tools = entries.filter(
(e): e is Extract<Entry, { kind: 'tool' }> => e.kind === 'tool',
);
const lines = [
'## Summary',
'',
`- ${messages} ${messages === 1 ? 'message' : 'messages'}: ${users} from you, ${assistants} from Pi.`,
];
if (tools.length > 0) {
const done = tools.filter((e) => !e.running && e.ok).length;
const failed = tools.filter((e) => !e.running && !e.ok).length;
const running = tools.filter((e) => e.running).length;
lines.push(
`- ${tools.length} ${tools.length === 1 ? 'tool call' : 'tool calls'}: ${done} done, ${failed} failed, ${running} running.`,
`- Tools used: ${[...new Set(tools.map((e) => e.tool))].join(', ')}.`,
);
}
return lines.join('\n');
}

export function sessionExportMarkdown(input: SessionExportInput): string {
const parts: string[] = [
`# ${input.title.trim() || 'Untitled session'}`,
`Exported from pidex at ${input.exportedAt.toISOString()}.`,
];
if (input.cwd.trim()) parts.push(`It ran in \`${input.cwd.trim()}\`.`);
if (input.model.trim()) parts.push(`It used \`${input.model.trim()}\`.`);
if (input.entries.length > 0) parts.push(handoffSection(input.cwd));
if (input.entries.length > 0) {
parts.push(summarySection(input.entries));
parts.push(handoffSection(input.cwd));
}
if (input.entries.length === 0) {
parts.push('No messages in this selection.');
return `${parts.join('\n\n')}\n`;
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/export-session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ describe('sessionExportMarkdown', () => {
'',
'It used `anthropic/claude-test`.',
'',
'## Summary',
'',
'- 2 messages: 1 from you, 1 from Pi.',
'- 1 tool call: 1 done, 0 failed, 0 running.',
'- Tools used: bash.',
'',
'## Continue elsewhere',
'',
'This file is portable context, not a restorable Pi session archive.',
Expand Down Expand Up @@ -158,6 +164,28 @@ describe('sessionExportMarkdown', () => {
expect(markdown).not.toContain('Same folder (`');
});

it('summarises tool outcomes and distinct tools in first-use order', () => {
const markdown = sessionExportMarkdown({
title: 'T',
cwd: '',
model: '',
exportedAt,
entries: [
{ key: 'u1', kind: 'user', text: 'hi' },
{ ...bashDone, key: 't1' },
{ ...bashDone, key: 't2', tool: 'read', ok: false, output: 'boom' },
{ ...bashDone, key: 't3', tool: 'bash', running: true, output: '' },
],
});
expect(markdown).toContain(
'- 1 message: 1 from you, 0 from Pi.',
);
expect(markdown).toContain(
'- 3 tool calls: 1 done, 1 failed, 1 running.',
);
expect(markdown).toContain('- Tools used: bash, read.');
});

it('handles an empty selection and a blank title', () => {
const markdown = sessionExportMarkdown({
title: ' ',
Expand All @@ -171,6 +199,7 @@ describe('sessionExportMarkdown', () => {
expect(markdown).not.toContain('It ran in');
expect(markdown).not.toContain('It used');
expect(markdown).not.toContain('Continue elsewhere');
expect(markdown).not.toContain('## Summary');
});
});

Expand Down
Loading