Skip to content

Commit

Permalink
file history plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
wladpaiva committed Oct 22, 2023
1 parent 7a15bf4 commit d09d857
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-walls-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'aibitat': patch
---

`fileHistory` plugin
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
history

# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs
Expand Down
5 changes: 3 additions & 2 deletions examples/5-web-browsing.ts
Original file line number Diff line number Diff line change
@@ -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: {},
Expand All @@ -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 changes: 38 additions & 0 deletions src/plugins/file-history.ts
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './cli.ts'
export * from './web-browsing.ts'
export * from './file-history.ts'

0 comments on commit d09d857

Please sign in to comment.