2025-09-21 11:02:31 -07:00
import { tool , type UIMessageStreamWriter } from "ai" ;
import { z } from "zod" ;
2025-02-13 08:25:57 -08:00
import {
artifactKinds ,
documentHandlersByArtifactKind ,
2025-09-21 11:02:31 -07:00
} from "@/lib/artifacts/server" ;
2026-03-13 23:18:01 +00:00
import type { AuthSession } from "@/lib/auth" ;
2025-09-21 11:02:31 -07:00
import type { ChatMessage } from "@/lib/types" ;
import { generateUUID } from "@/lib/utils" ;
2025-01-23 01:19:48 +05:30
2025-09-21 11:02:31 -07:00
type CreateDocumentProps = {
2026-03-13 23:18:01 +00:00
session : NonNullable < AuthSession > ;
2025-07-03 02:26:34 -07:00
dataStream : UIMessageStreamWriter < ChatMessage > ;
2025-09-21 11:02:31 -07:00
} ;
2025-01-23 01:19:48 +05:30
2025-02-03 20:33:15 +05:30
export const createDocument = ( { session , dataStream } : CreateDocumentProps ) = >
2025-01-23 01:19:48 +05:30
tool ( {
description :
2025-09-21 11:02:31 -07:00
"Create a document for a writing or content creation activities. This tool will call other functions that will generate the contents of the document based on the title and kind." ,
2025-07-03 02:26:34 -07:00
inputSchema : z.object ( {
2025-01-23 01:19:48 +05:30
title : z.string ( ) ,
2025-02-13 08:25:57 -08:00
kind : z.enum ( artifactKinds ) ,
2025-01-23 01:19:48 +05:30
} ) ,
execute : async ( { title , kind } ) = > {
const id = generateUUID ( ) ;
2025-02-04 17:15:39 +03:00
2025-07-03 02:26:34 -07:00
dataStream . write ( {
2025-09-21 11:02:31 -07:00
type : "data-kind" ,
2025-07-03 02:26:34 -07:00
data : kind ,
transient : true ,
2025-02-04 17:15:39 +03:00
} ) ;
2025-01-23 01:19:48 +05:30
2025-07-03 02:26:34 -07:00
dataStream . write ( {
2025-09-21 11:02:31 -07:00
type : "data-id" ,
2025-07-03 02:26:34 -07:00
data : id ,
transient : true ,
2025-01-23 01:19:48 +05:30
} ) ;
2025-07-03 02:26:34 -07:00
dataStream . write ( {
2025-09-21 11:02:31 -07:00
type : "data-title" ,
2025-07-03 02:26:34 -07:00
data : title ,
transient : true ,
2025-01-23 01:19:48 +05:30
} ) ;
2025-07-03 02:26:34 -07:00
dataStream . write ( {
2025-09-21 11:02:31 -07:00
type : "data-clear" ,
2025-07-03 02:26:34 -07:00
data : null ,
transient : true ,
2025-01-23 01:19:48 +05:30
} ) ;
2025-02-13 08:25:57 -08:00
const documentHandler = documentHandlersByArtifactKind . find (
( documentHandlerByArtifactKind ) = >
2025-09-21 11:02:31 -07:00
documentHandlerByArtifactKind . kind === kind
2025-02-04 17:15:39 +03:00
) ;
2025-01-23 01:19:48 +05:30
2025-02-04 17:15:39 +03:00
if ( ! documentHandler ) {
throw new Error ( ` No document handler found for kind: ${ kind } ` ) ;
2025-01-23 01:19:48 +05:30
}
2025-02-04 17:15:39 +03:00
await documentHandler . onCreateDocument ( {
id ,
title ,
dataStream ,
session ,
} ) ;
2025-09-21 11:02:31 -07:00
dataStream . write ( { type : "data-finish" , data : null , transient : true } ) ;
2025-01-23 01:19:48 +05:30
return {
id ,
title ,
kind ,
2025-09-21 11:02:31 -07:00
content : "A document was created and is now visible to the user." ,
2025-01-23 01:19:48 +05:30
} ;
} ,
} ) ;