({ session, dataStream }: CreateDocumentProps)
| 14 | } |
| 15 | |
| 16 | export const createDocument = ({ session, dataStream }: CreateDocumentProps) => |
| 17 | tool({ |
| 18 | description: |
| 19 | '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.', |
| 20 | inputSchema: z.object({ |
| 21 | title: z.string(), |
| 22 | kind: z.enum(artifactKinds), |
| 23 | }), |
| 24 | execute: async ({ title, kind }) => { |
| 25 | const id = generateUUID(); |
| 26 | |
| 27 | dataStream.write({ |
| 28 | type: 'data-kind', |
| 29 | data: kind, |
| 30 | transient: true, |
| 31 | }); |
| 32 | |
| 33 | dataStream.write({ |
| 34 | type: 'data-id', |
| 35 | data: id, |
| 36 | transient: true, |
| 37 | }); |
| 38 | |
| 39 | dataStream.write({ |
| 40 | type: 'data-title', |
| 41 | data: title, |
| 42 | transient: true, |
| 43 | }); |
| 44 | |
| 45 | dataStream.write({ |
| 46 | type: 'data-clear', |
| 47 | data: null, |
| 48 | transient: true, |
| 49 | }); |
| 50 | |
| 51 | const documentHandler = documentHandlersByArtifactKind.find( |
| 52 | (documentHandlerByArtifactKind) => |
| 53 | documentHandlerByArtifactKind.kind === kind, |
| 54 | ); |
| 55 | |
| 56 | if (!documentHandler) { |
| 57 | throw new Error(`No document handler found for kind: ${kind}`); |
| 58 | } |
| 59 | |
| 60 | await documentHandler.onCreateDocument({ |
| 61 | id, |
| 62 | title, |
| 63 | dataStream, |
| 64 | session, |
| 65 | }); |
| 66 | |
| 67 | dataStream.write({ type: 'data-finish', data: null, transient: true }); |
| 68 | |
| 69 | return { |
| 70 | id, |
| 71 | title, |
| 72 | kind, |
| 73 | content: 'A document was created and is now visible to the user.', |
no test coverage detected
searching dependent graphs…