Skip to content

Commit

Permalink
feat: added support for create_time and update_time metadata paramete…
Browse files Browse the repository at this point in the history
…rs per pionxzh#94
  • Loading branch information
alex-rosenberg35 committed Apr 10, 2023
1 parent a8a44f2 commit 304d335
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/userscript/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ const modelMapping: { [key in ModelSlug]: string } = {
export function processConversation(conversation: ApiConversationWithId, conversationChoices: Array<number | null> = []): ConversationResult {
const title = conversation.title || 'ChatGPT Conversation'
const createTime = conversation.create_time
const updateTime = conversation.update_time
const modelSlug = Object.values(conversation.mapping).find(node => node.message?.metadata?.model_slug)?.message?.metadata?.model_slug || ''
const model = modelSlug ? (modelMapping[modelSlug] || '') : ''

Expand Down Expand Up @@ -236,6 +237,7 @@ export function processConversation(conversation: ApiConversationWithId, convers
modelSlug,
model,
createTime,
updateTime,
conversationNodes: result,
}
}
8 changes: 6 additions & 2 deletions packages/userscript/src/exporter/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function exportToHtml(fileNameFormat: string, metaList: ExportMeta[
const conversation = processConversation(rawConversation, conversationChoices)
const html = conversationToHtml(conversation, userAvatar, metaList)

const fileName = getFileNameWithFormat(fileNameFormat, 'html', { title: conversation.title, chatId })
const fileName = getFileNameWithFormat(fileNameFormat, 'html', { title: conversation.title, chatId, createTime: conversation.createTime, updateTime: conversation.updateTime })
downloadFile(fileName, 'text/html', standardizeLineBreaks(html))

return true
Expand All @@ -40,6 +40,8 @@ export async function exportAllToHtml(fileNameFormat: string, apiConversations:
const fileName = getFileNameWithFormat(fileNameFormat, 'html', {
title: conversation.title,
chatId: conversation.id,
createTime: conversation.createTime,
updateTime: conversation.updateTime,
})
const content = conversationToHtml(conversation, userAvatar, metaList)
zip.file(fileName, content)
Expand All @@ -52,7 +54,7 @@ export async function exportAllToHtml(fileNameFormat: string, apiConversations:
}

function conversationToHtml(conversation: ConversationResult, avatar: string, metaList?: ExportMeta[]) {
const { id, title, model, modelSlug, conversationNodes } = conversation
const { id, title, model, modelSlug, createTime, updateTime, conversationNodes } = conversation

const conversationHtml = conversationNodes.map((item) => {
const author = item.message?.author.role === 'assistant' ? 'ChatGPT' : 'You'
Expand Down Expand Up @@ -120,6 +122,8 @@ function conversationToHtml(conversation: ConversationResult, avatar: string, me
.replace('{source}', source)
.replace('{model}', model)
.replace('{mode_name}', modelSlug)
.replace("{create_time}", unixTimestampToISOString(createTime))
.replace("{update_time}", unixTimestampToISOString(updateTime))

return [name, val] as const
})
Expand Down
2 changes: 2 additions & 0 deletions packages/userscript/src/exporter/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export async function exportAllToJson(fileNameFormat: string, apiConversations:
const fileName = getFileNameWithFormat(fileNameFormat, 'json', {
title: conversation.title,
chatId: conversation.id,
createTime: conversation.createTime,
updateTime: conversation.updateTime,
})
const content = conversationToJson(rawConversation)
zip.file(fileName, content)
Expand Down
8 changes: 6 additions & 2 deletions packages/userscript/src/exporter/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function exportToMarkdown(fileNameFormat: string, metaList: ExportM
const conversation = processConversation(rawConversation, conversationChoices)
const markdown = conversationToMarkdown(conversation, metaList)

const fileName = getFileNameWithFormat(fileNameFormat, 'md', { title: conversation.title, chatId })
const fileName = getFileNameWithFormat(fileNameFormat, 'md', { title: conversation.title, chatId, createTime: conversation.createTime, updateTime: conversation.updateTime })
downloadFile(fileName, 'text/markdown', standardizeLineBreaks(markdown))

return true
Expand All @@ -34,6 +34,8 @@ export async function exportAllToMarkdown(fileNameFormat: string, apiConversatio
const fileName = getFileNameWithFormat(fileNameFormat, 'md', {
title: conversation.title,
chatId: conversation.id,
createTime: conversation.createTime,
updateTime: conversation.updateTime,
})
const content = conversationToMarkdown(conversation, metaList)
zip.file(fileName, content)
Expand All @@ -58,7 +60,9 @@ function conversationToMarkdown(conversation: ConversationResult, metaList?: Exp
.replace('{timestamp}', timestamp())
.replace('{source}', source)
.replace('{model}', model)
.replace('{modelSlug}', modelSlug)
.replace('{model_name}', modelSlug)
.replace("{create_time}", unixTimestampToISOString(createTime))
.replace("{update_time}", unixTimestampToISOString(updateTime))

return `${name}: ${val}`
})
Expand Down
12 changes: 11 additions & 1 deletion packages/userscript/src/ui/SettingDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export const SettingDialog: FC<SettingDialogProps> = ({
const timestamp = _timestamp()
const title = sanitize(_title).replace(/\s+/g, '_')
const chatId = getChatIdFromUrl() || 'this-is-a-mock-chat-id'
const preview = getFileNameWithFormat(format, '{ext}', { title, chatId })
const createTime = Math.floor((new Date()).getTime() / 1000)
const updateTime = createTime
const preview = getFileNameWithFormat(format, '{ext}', { title, chatId, createTime, updateTime })

const source = `${baseUrl}/${chatId}`

Expand Down Expand Up @@ -69,6 +71,10 @@ export const SettingDialog: FC<SettingDialogProps> = ({
<Variable name="{timestamp}" title={timestamp} />
,{' '}
<Variable name="{chat_id}" title={chatId} />
,{' '}
<Variable name="{create_time}" title={createTime} />
,{' '}
<Variable name="{update_time}" title={updateTime} />
</p>
<input className="Input mt-4" id="filename" value={format} onChange={e => setFormat(e.currentTarget.value)} />
<p className="mt-1 text-sm text-gray-700 dark:text-gray-300">
Expand Down Expand Up @@ -123,6 +129,10 @@ export const SettingDialog: FC<SettingDialogProps> = ({
<Variable name="{model}" title={'ChatGPT-3.5'} />
,{' '}
<Variable name="{model_name}" title={'text-davinci-002-render-sha'} />
,{' '}
<Variable name="{create_time}" title={'2023-04-10T21:45:35.027Z'} />
,{' '}
<Variable name="{update_time}" title={'2023-04-10T21:45:35.027Z'} />
</p>
{exportMetaList.map((meta, i) => (
<div className="flex items-center mt-2" key={i}>
Expand Down
6 changes: 6 additions & 0 deletions packages/userscript/src/utils/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ export function getFileNameWithFormat(format: string, ext: string, {
title = document.title,
// chatId will be empty when exporting all conversations
chatId = '',
createTime,
updateTime
} = {}) {
const _title = sanitize(title).replace(/\s+/g, '_')
const _createTime = unixTimestampToFormattedDashString(createTime)
const _updateTime = unixTimestampToFormattedDashString(updateTime)

return format
.replace('{title}', _title)
.replace('{date}', dateStr())
.replace('{timestamp}', timestamp())
.replace('{chat_id}', chatId)
.replace("{create_time}", _createTime)
.replace("{update_time}", _updateTime)
.concat(`.${ext}`)
}
20 changes: 20 additions & 0 deletions packages/userscript/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,23 @@ export function timestamp() {
export function getColorScheme(): 'light' | 'dark' {
return document.documentElement.style.getPropertyValue('color-scheme') as 'light' | 'dark'
}

export function unixTimestampToISOString(timestamp) {
if (!timestamp) return;
return (new Date(timestamp * 1000)).toISOString();
}

export function unixTimestampToFormattedDashString(timestamp) {
if (!timestamp) return;
var date = new Date(timestamp * 1000);
var pad = function(n) { return (n<10) ? '0'+n : n; };
var m = date.getMonth()+1;
var d = date.getDate();
var y = date.getFullYear();
var h = date.getHours();
var ampm = (h >= 12) ? 'PM' : 'AM';
h = h % 12;
h = (h === 0) ? 12 : h;
var mm = pad(date.getMinutes());
return `${m}-${d}-${y}-${h}-${mm}-${ampm}`;
}

0 comments on commit 304d335

Please sign in to comment.