file history plugin

This commit is contained in:
Wlad Paiva
2023-10-22 10:55:15 -03:00
parent 7a15bf42c3
commit d09d857f51
5 changed files with 49 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'aibitat': patch
---
`fileHistory` plugin
+2
View File
@@ -1,3 +1,5 @@
history
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
# Logs
+3 -2
View File
@@ -1,5 +1,5 @@
import {AIbitat} from '../src'
import {cli, experimental_webBrowsing} from '../src/plugins'
import {cli, experimental_webBrowsing, fileHistory} from '../src/plugins'
const aibitat = new AIbitat({
nodes: {},
@@ -20,9 +20,10 @@ const aibitat = new AIbitat({
})
.use(cli())
.use(experimental_webBrowsing())
.use(fileHistory())
await aibitat.start({
from: 'client',
to: '#content-creators',
to: 'the-researcher',
content: `Write a blog post about in Brazilian Portuguese to be posted on Medium.`,
})
+38
View File
@@ -0,0 +1,38 @@
import fs from 'fs'
import path from 'path'
import {AIbitatPlugin} from '..'
export function fileHistory({
filename = `history/chat-history-${new Date().toISOString()}.json`,
}: {
/**
* The location of the file to save the chat history
* @default `history/chat-history-${new Date().toISOString()}.json`
*/
filename?: string
} = {}) {
return {
name: 'file-history-plugin',
setup(aibitat) {
const folderPath = path.dirname(filename)
// get path from filename
if (folderPath) {
fs.mkdirSync(folderPath, {recursive: true})
}
aibitat.onMessage(() => {
const content = JSON.stringify(aibitat.chats, null, 2)
if (typeof Bun !== 'undefined') {
return Bun.write(filename, content)
}
fs.writeFile(filename, content, err => {
if (err) {
console.error(err)
}
})
})
},
} as AIbitatPlugin
}
+1
View File
@@ -1,2 +1,3 @@
export * from './cli.ts'
export * from './web-browsing.ts'
export * from './file-history.ts'