-
-
Notifications
You must be signed in to change notification settings - Fork 263
feat(chats): export conversation as a plain-text transcript #531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ferreus
wants to merge
1
commit into
off-grid-ai:main
Choose a base branch
from
ferreus:feat/export-conversation-text
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| import { Share } from 'react-native'; | ||
| import RNShare from 'react-native-share'; | ||
| import RNFS from 'react-native-fs'; | ||
| import { formatConversationAsText, shareConversationAsText } from '../../../src/utils/exportConversation'; | ||
| import { Conversation, Message } from '../../../src/types'; | ||
|
|
||
| function makeMessage(overrides: Partial<Message> = {}): Message { | ||
| return { | ||
| id: 'msg-1', | ||
| role: 'user', | ||
| content: 'Hello there', | ||
| timestamp: 1750000000000, | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| function makeConversation(overrides: Partial<Conversation> = {}): Conversation { | ||
| return { | ||
| id: 'conv-1', | ||
| title: 'Trip planning', | ||
| modelId: 'llama-3-8b', | ||
| messages: [makeMessage()], | ||
| createdAt: '2026-01-01T10:00:00.000Z', | ||
| updatedAt: '2026-01-02T12:30:00.000Z', | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe('formatConversationAsText', () => { | ||
| it('includes title, model, created and updated in the header', () => { | ||
| const text = formatConversationAsText(makeConversation()); | ||
| expect(text).toContain('Trip planning'); | ||
| expect(text).toContain('Model: llama-3-8b'); | ||
| expect(text).toContain(`Created: ${new Date('2026-01-01T10:00:00.000Z').toLocaleString()}`); | ||
| expect(text).toContain(`Updated: ${new Date('2026-01-02T12:30:00.000Z').toLocaleString()}`); | ||
| }); | ||
|
|
||
| it('renders only the header (with trailing newline) when there are no messages', () => { | ||
| const text = formatConversationAsText(makeConversation({ messages: [] })); | ||
| expect(text.endsWith('\n')).toBe(true); | ||
| expect(text).not.toContain('You:'); | ||
| }); | ||
|
|
||
| it('labels each role correctly', () => { | ||
| const conversation = makeConversation({ | ||
| messages: [ | ||
| makeMessage({ id: '1', role: 'user', content: 'hi' }), | ||
| makeMessage({ id: '2', role: 'assistant', content: 'hello' }), | ||
| makeMessage({ id: '3', role: 'system', content: 'sys note' }), | ||
| makeMessage({ id: '4', role: 'tool', content: 'tool output' }), | ||
| ], | ||
| }); | ||
| const text = formatConversationAsText(conversation); | ||
| expect(text).toContain('You:\nhi'); | ||
| expect(text).toContain('Assistant:\nhello'); | ||
| expect(text).toContain('System:\nsys note'); | ||
| expect(text).toContain('Tool:\ntool output'); | ||
| }); | ||
|
|
||
| it('omits the attachment note when a message has no attachments field', () => { | ||
| const text = formatConversationAsText(makeConversation({ | ||
| messages: [makeMessage({ attachments: undefined })], | ||
| })); | ||
| expect(text).not.toContain('attachment'); | ||
| }); | ||
|
|
||
| it('omits the attachment note when attachments is an empty array', () => { | ||
| const text = formatConversationAsText(makeConversation({ | ||
| messages: [makeMessage({ attachments: [] })], | ||
| })); | ||
| expect(text).not.toContain('attachment'); | ||
| }); | ||
|
|
||
| it('uses singular "attachment" for exactly one attachment', () => { | ||
| const text = formatConversationAsText(makeConversation({ | ||
| messages: [makeMessage({ attachments: [{ type: 'image', uri: 'file://a.png' } as any] })], | ||
| })); | ||
| expect(text).toContain('[1 attachment]'); | ||
| }); | ||
|
|
||
| it('uses plural "attachments" for more than one attachment', () => { | ||
| const text = formatConversationAsText(makeConversation({ | ||
| messages: [makeMessage({ | ||
| attachments: [ | ||
| { type: 'image', uri: 'file://a.png' } as any, | ||
| { type: 'image', uri: 'file://b.png' } as any, | ||
| ], | ||
| })], | ||
| })); | ||
| expect(text).toContain('[2 attachments]'); | ||
| }); | ||
|
|
||
| it('joins multiple messages with a blank line between them', () => { | ||
| const text = formatConversationAsText(makeConversation({ | ||
| messages: [ | ||
| makeMessage({ id: '1', content: 'first' }), | ||
| makeMessage({ id: '2', content: 'second' }), | ||
| ], | ||
| })); | ||
| expect(text).toContain('first\n\n['); | ||
| expect(text).toContain('second'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('shareConversationAsText', () => { | ||
| let shareSpy: jest.SpiedFunction<typeof Share.share>; | ||
| const openSpy = RNShare.open as jest.Mock; | ||
| const writeFileSpy = RNFS.writeFile as jest.Mock; | ||
|
|
||
| beforeEach(() => { | ||
| shareSpy = jest.spyOn(Share, 'share').mockResolvedValue({ action: 'sharedAction' } as any); | ||
| openSpy.mockReset().mockResolvedValue({ success: true, message: '' }); | ||
| writeFileSpy.mockReset().mockResolvedValue(undefined); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| shareSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('uses the inline Share sheet for a short conversation', async () => { | ||
| await shareConversationAsText(makeConversation()); | ||
| expect(shareSpy).toHaveBeenCalledWith({ | ||
| message: expect.stringContaining('Trip planning'), | ||
| title: 'Trip planning', | ||
| }); | ||
| expect(writeFileSpy).not.toHaveBeenCalled(); | ||
| expect(openSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('writes a .txt file and opens the native share sheet for a long conversation', async () => { | ||
| const longMessages = Array.from({ length: 20 }, (_, i) => | ||
| makeMessage({ id: `m${i}`, content: `line ${i}` })); | ||
| await shareConversationAsText(makeConversation({ title: 'Long Chat!!', messages: longMessages })); | ||
|
|
||
| expect(writeFileSpy).toHaveBeenCalledWith( | ||
| '/mock/caches/Long_Chat.txt', | ||
| expect.stringContaining('line 0'), | ||
| 'utf8', | ||
| ); | ||
| expect(openSpy).toHaveBeenCalledWith({ | ||
| url: 'file:///mock/caches/Long_Chat.txt', | ||
| type: 'text/plain', | ||
| filename: 'Long_Chat', | ||
| title: 'Long Chat!!', | ||
| failOnCancel: false, | ||
| }); | ||
| expect(shareSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('falls back to "conversation" as the file name when the title sanitizes to empty', async () => { | ||
| const longMessages = Array.from({ length: 20 }, (_, i) => | ||
| makeMessage({ id: `m${i}`, content: `line ${i}` })); | ||
| await shareConversationAsText(makeConversation({ title: '!!! ***', messages: longMessages })); | ||
|
|
||
| expect(writeFileSpy).toHaveBeenCalledWith( | ||
| '/mock/caches/conversation.txt', | ||
| expect.any(String), | ||
| 'utf8', | ||
| ); | ||
| expect(openSpy).toHaveBeenCalledWith(expect.objectContaining({ filename: 'conversation' })); | ||
| }); | ||
|
|
||
| it('swallows errors from the inline share sheet (e.g. user cancelled)', async () => { | ||
| shareSpy.mockRejectedValue(new Error('User did not share')); | ||
| await expect(shareConversationAsText(makeConversation())).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it('swallows errors from the file-based share path', async () => { | ||
| const longMessages = Array.from({ length: 20 }, (_, i) => | ||
| makeMessage({ id: `m${i}`, content: `line ${i}` })); | ||
| writeFileSpy.mockRejectedValue(new Error('disk full')); | ||
| await expect( | ||
| shareConversationAsText(makeConversation({ messages: longMessages })), | ||
| ).resolves.toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Add
accessibilityLabelto the export button.The export
TouchableOpacityis icon-only with noaccessibilityLabel. Screen reader users can't identify the action. The existing delete button has the same gap, but new code should set the label.♿ Proposed accessibility fix
<TouchableOpacity style={styles.exportAction} onPress={() => shareConversationAsText(conversation)} testID="export-conversation-button" + accessibilityLabel="Export conversation" + accessibilityRole="button" >📝 Committable suggestion
🤖 Prompt for AI Agents