2024-04-16 19:50:10 +02:00
const { Document } = require ( "../../../../models/documents" ) ;
const { safeJsonParse } = require ( "../../../http" ) ;
const { summarizeContent } = require ( "../utils/summarize" ) ;
2024-04-17 23:04:51 +02:00
const Provider = require ( "../providers/ai-provider" ) ;
2024-04-16 19:50:10 +02:00
const docSummarizer = {
name : "document-summarizer" ,
startupConfig : {
params : { } ,
} ,
plugin : function ( ) {
return {
name : this . name ,
setup ( aibitat ) {
aibitat . function ( {
super : aibitat ,
name : this . name ,
controller : new AbortController ( ) ,
description :
"Can get the list of files available to search with descriptions and can select a single file to open and summarize." ,
2024-05-08 01:35:47 +02:00
examples : [
{
prompt : "Summarize example.txt" ,
call : JSON . stringify ( {
action : "summarize" ,
document _filename : "example.txt" ,
} ) ,
} ,
{
prompt : "What files can you see?" ,
call : JSON . stringify ( { action : "list" , document _filename : null } ) ,
} ,
{
prompt : "Tell me about readme.md" ,
call : JSON . stringify ( {
action : "summarize" ,
document _filename : "readme.md" ,
} ) ,
} ,
] ,
2024-04-16 19:50:10 +02:00
parameters : {
$schema : "http://json-schema.org/draft-07/schema#" ,
type : "object" ,
properties : {
action : {
type : "string" ,
enum : [ "list" , "summarize" ] ,
description :
2024-04-17 23:34:58 +02:00
"The action to take. 'list' will return all files available with their filename and descriptions. 'summarize' will open and summarize the file by the a document name." ,
2024-04-16 19:50:10 +02:00
} ,
2024-04-17 23:34:58 +02:00
document _filename : {
2024-04-16 19:50:10 +02:00
type : "string" ,
"x-nullable" : true ,
description :
2024-04-17 23:34:58 +02:00
"The file name of the document you want to get the full content of." ,
2024-04-16 19:50:10 +02:00
} ,
} ,
additionalProperties : false ,
} ,
2024-04-17 23:34:58 +02:00
handler : async function ( { action , document _filename } ) {
2024-04-16 19:50:10 +02:00
if ( action === "list" ) return await this . listDocuments ( ) ;
if ( action === "summarize" )
2024-04-17 23:34:58 +02:00
return await this . summarizeDoc ( document _filename ) ;
2024-04-16 19:50:10 +02:00
return "There is nothing we can do. This function call returns no information." ;
} ,
/ * *
* List all documents available in a workspace
* @ returns List of files and their descriptions if available .
* /
listDocuments : async function ( ) {
try {
this . super . introspect (
` ${ this . caller } : Looking at the available documents. `
) ;
const documents = await Document . where ( {
workspaceId : this . super . handlerProps . invocation . workspace _id ,
} ) ;
if ( documents . length === 0 )
return "No documents found - nothing can be done. Stop." ;
this . super . introspect (
` ${ this . caller } : Found ${ documents . length } documents `
) ;
const foundDocuments = documents . map ( ( doc ) => {
const metadata = safeJsonParse ( doc . metadata , { } ) ;
return {
document _id : doc . docId ,
filename : metadata ? . title ? ? "unknown.txt" ,
description : metadata ? . description ? ? "no description" ,
} ;
} ) ;
return JSON . stringify ( foundDocuments ) ;
} catch ( error ) {
this . super . handlerProps . log (
` document-summarizer.list raised an error. ${ error . message } `
) ;
return ` Let the user know this action was not successful. An error was raised while listing available files. ${ error . message } ` ;
}
} ,
2024-04-17 23:34:58 +02:00
summarizeDoc : async function ( filename ) {
2024-04-16 19:50:10 +02:00
try {
2024-04-17 23:34:58 +02:00
const availableDocs = safeJsonParse (
await this . listDocuments ( ) ,
[ ]
) ;
if ( ! availableDocs . length ) {
this . super . handlerProps . log (
` ${ this . caller } : No available documents to summarize. `
) ;
return "No documents were found." ;
}
const docInfo = availableDocs . find (
( info ) => info . filename === filename
) ;
if ( ! docInfo ) {
2024-04-16 19:50:10 +02:00
this . super . handlerProps . log (
2024-04-17 23:34:58 +02:00
` ${ this . caller } : No available document by the name " ${ filename } ". `
2024-04-16 19:50:10 +02:00
) ;
2024-04-17 23:34:58 +02:00
return ` No available document by the name " ${ filename } ". ` ;
2024-04-16 19:50:10 +02:00
}
2024-04-17 23:34:58 +02:00
const document = await Document . content ( docInfo . document _id ) ;
2024-04-16 19:50:10 +02:00
this . super . introspect (
` ${ this . caller } : Grabbing all content for ${
2024-04-17 23:34:58 +02:00
filename ? ? "a discovered file."
2024-04-16 19:50:10 +02:00
} `
) ;
2024-04-17 23:04:51 +02:00
if ( ! document . content || document . content . length === 0 ) {
throw new Error (
"This document has no readable content that could be found."
) ;
}
if (
document . content ? . length <
Provider . contextLimit ( this . super . provider )
) {
return document . content ;
}
2024-04-16 19:50:10 +02:00
this . super . introspect (
2024-04-17 23:34:58 +02:00
` ${ this . caller } : Summarizing ${ filename ? ? "" } ... `
2024-04-16 19:50:10 +02:00
) ;
this . super . onAbort ( ( ) => {
this . super . handlerProps . log (
"Abort was triggered, exiting summarization early."
) ;
this . controller . abort ( ) ;
} ) ;
2024-06-10 23:31:39 +02:00
return await summarizeContent ( {
provider : this . super . provider ,
model : this . super . model ,
controllerSignal : this . controller . signal ,
content : document . content ,
} ) ;
2024-04-16 19:50:10 +02:00
} catch ( error ) {
this . super . handlerProps . log (
` document-summarizer.summarizeDoc raised an error. ${ error . message } `
) ;
return ` Let the user know this action was not successful. An error was raised while summarizing the file. ${ error . message } ` ;
}
} ,
} ) ;
} ,
} ;
} ,
} ;
module . exports = {
docSummarizer ,
} ;