diff --git a/.changeset/wise-walls-pay.md b/.changeset/wise-walls-pay.md new file mode 100644 index 0000000..38d7c69 --- /dev/null +++ b/.changeset/wise-walls-pay.md @@ -0,0 +1,5 @@ +--- +'aibitat': patch +--- + +`fileHistory` plugin diff --git a/.gitignore b/.gitignore index e93321d..c69a32c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +history + # Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore # Logs diff --git a/examples/5-web-browsing.ts b/examples/5-web-browsing.ts index 3b5bfb2..a0bd1a0 100644 --- a/examples/5-web-browsing.ts +++ b/examples/5-web-browsing.ts @@ -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.`, }) diff --git a/src/plugins/file-history.ts b/src/plugins/file-history.ts new file mode 100644 index 0000000..964753a --- /dev/null +++ b/src/plugins/file-history.ts @@ -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 +} diff --git a/src/plugins/index.ts b/src/plugins/index.ts index e2c3bea..61945bb 100644 --- a/src/plugins/index.ts +++ b/src/plugins/index.ts @@ -1,2 +1,3 @@ export * from './cli.ts' export * from './web-browsing.ts' +export * from './file-history.ts'