-
Notifications
You must be signed in to change notification settings - Fork 34
feature: command to export conversation in markdown #40
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
spinualexandru
wants to merge
1
commit into
Mote-Software:main
Choose a base branch
from
spinualexandru:feature/export-markdown-command
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
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
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
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
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,78 @@ | ||||||
import {Command, Message} from '../types/index.js'; | ||||||
import React from 'react'; | ||||||
import SuccessMessage from '../components/success-message.js'; | ||||||
import fs from 'fs/promises'; | ||||||
import path from 'path'; | ||||||
|
||||||
const formatMessageContent = (message: Message) => { | ||||||
let content = ''; | ||||||
switch (message.role) { | ||||||
case 'user': | ||||||
content += `## User\n${message.content}`; | ||||||
break; | ||||||
case 'assistant': | ||||||
content += `## Assistant\n${message.content || ''}`; | ||||||
if (message.tool_calls) { | ||||||
content += `\n\n[tool_use: ${message.tool_calls | ||||||
.map(tc => tc.function.name) | ||||||
.join(', ')}]`; | ||||||
} | ||||||
break; | ||||||
case 'tool': | ||||||
content += | ||||||
`## Tool Output: ${message.name}\n` + | ||||||
'```\n' + | ||||||
`${message.content}\n` + | ||||||
'```\n'; | ||||||
break; | ||||||
case 'system': | ||||||
// For now, we don't include system messages in the export | ||||||
return ''; | ||||||
default: | ||||||
return ''; | ||||||
} | ||||||
return content + '\n\n'; | ||||||
}; | ||||||
|
||||||
function Export({filename}: {filename: string}) { | ||||||
return ( | ||||||
<SuccessMessage | ||||||
message={`✔️ Chat exported to ${filename}`} | ||||||
></SuccessMessage> | ||||||
); | ||||||
} | ||||||
|
||||||
export const exportCommand: Command = { | ||||||
name: 'export', | ||||||
description: 'Export the chat history to a markdown file', | ||||||
handler: async ( | ||||||
args: string[], | ||||||
messages: Message[], | ||||||
{provider, model, tokens}, | ||||||
) => { | ||||||
const filename = | ||||||
args[0] || | ||||||
`nanocoder-chat-${new Date().toISOString().replace(/:/g, '-')}.md`; | ||||||
const filepath = path.resolve(process.cwd(), filename); | ||||||
|
||||||
const frontmatter = `--- | ||||||
session_date: ${new Date().toISOString()} | ||||||
provider: ${provider} | ||||||
model: ${model} | ||||||
total_tokens: ${tokens} | ||||||
--- | ||||||
|
||||||
# Nanocoder Chat Export | ||||||
|
||||||
`; | ||||||
|
||||||
const markdownContent = messages.map(formatMessageContent).join(''); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
|
||||||
await fs.writeFile(filepath, frontmatter + markdownContent); | ||||||
|
||||||
return React.createElement(Export, { | ||||||
key: `export-${Date.now()}`, | ||||||
filename, | ||||||
}); | ||||||
}, | ||||||
}; |
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
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
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import React from 'react'; | ||
import {Command} from '../types/index.js'; | ||
|
||
export const modelCommand: Command = { | ||
name: 'model', | ||
description: 'Select a model for the current provider', | ||
handler: async (_args: string[]) => { | ||
handler: async (_args: string[], _messages, _metadata) => { | ||
// This command is handled specially in app.tsx | ||
// This handler exists only for registration purposes | ||
return undefined; | ||
return React.createElement(React.Fragment); | ||
}, | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import React from 'react'; | ||
import {Command} from '../types/index.js'; | ||
|
||
export const providerCommand: Command = { | ||
name: 'provider', | ||
description: 'Switch between AI providers', | ||
handler: async (_args: string[]) => { | ||
handler: async (_args: string[], _messages, _metadata) => { | ||
// This command is handled specially in app.tsx | ||
// This handler exists only for registration purposes | ||
return undefined; | ||
return React.createElement(React.Fragment); | ||
}, | ||
}; |
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import React from 'react'; | ||
import type {Command} from '../types/index.js'; | ||
|
||
export const themeCommand: Command = { | ||
name: 'theme', | ||
description: 'Select a theme for the Nanocoder CLI', | ||
handler: async (_args: string[]) => { | ||
handler: async (_args: string[], _messages, _metadata) => { | ||
// This command is handled specially in app.tsx | ||
// This handler exists only for registration purposes | ||
return undefined; | ||
return React.createElement(React.Fragment); | ||
}, | ||
}; |
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
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.
[nitpick] The filename generation uses a magic string replacement pattern. Consider extracting this to a helper function or using a more robust date formatting approach for better maintainability.
Copilot uses AI. Check for mistakes.