@@ -4,11 +4,9 @@ import type {
44 FunctionTypeEnum ,
55 AppActionRequest ,
66} from '@contentful/node-apps-toolkit' ;
7- import { ContentTypeProps } from 'contentful-management' ;
87import { createPreviewWithAgent } from '../../agents/documentParserAgent/documentParser.agent' ;
98import { fetchContentTypes } from '../../service/contentTypeService' ;
109import { initContentfulManagementClient } from '../../service/initCMAClient' ;
11- import { EntryToCreate } from '../../agents/documentParserAgent/schema' ;
1210
1311export type AppActionParameters = {
1412 contentTypeIds : string [ ] ;
@@ -19,74 +17,6 @@ interface AppInstallationParameters {
1917 openAiApiKey : string ;
2018}
2119
22- interface AssetInfo {
23- url : string ;
24- altText : string ;
25- fileName : string ;
26- }
27-
28- /**
29- * Extracts asset information from entries by scanning RichText fields for image tokens
30- */
31- function extractAssetsFromEntries (
32- entries : EntryToCreate [ ] ,
33- contentTypes : ContentTypeProps [ ]
34- ) : AssetInfo [ ] {
35- const IMAGE_TOKEN_REGEX = / ! \[ ( [ ^ \] ] * ?) \] \( ( [ \s \S ] * ?) \) / g;
36- const assetsMap = new Map < string , AssetInfo > ( ) ;
37-
38- for ( const entry of entries ) {
39- const contentType = contentTypes . find ( ( ct ) => ct . sys . id === entry . contentTypeId ) ;
40- if ( ! contentType ) continue ;
41-
42- // Check all RichText fields
43- for ( const field of contentType . fields ) {
44- if ( field . type !== 'RichText' ) continue ;
45-
46- const localizedValue = entry . fields [ field . id ] ;
47- if ( ! localizedValue || typeof localizedValue !== 'object' ) continue ;
48-
49- // Check all locales
50- for ( const value of Object . values ( localizedValue ) ) {
51- if ( typeof value !== 'string' ) continue ;
52-
53- // Reset regex state
54- IMAGE_TOKEN_REGEX . lastIndex = 0 ;
55- for ( const match of value . matchAll ( IMAGE_TOKEN_REGEX ) ) {
56- const altText = ( match [ 1 ] || '' ) . trim ( ) ;
57- const url = String ( match [ 2 ] ) . replace ( / \s + / g, '' ) . trim ( ) ;
58-
59- if ( ! url ) continue ;
60-
61- // Extract filename from URL
62- let fileName = 'image' ;
63- try {
64- const urlObj = new URL ( url ) ;
65- const pathname = urlObj . pathname . toLowerCase ( ) ;
66- const pathParts = pathname . split ( '/' ) . filter ( Boolean ) ;
67- fileName = pathParts [ pathParts . length - 1 ] || 'image' ;
68- // Remove query params if present
69- fileName = fileName . split ( '?' ) [ 0 ] ;
70- } catch {
71- // If URL parsing fails, use default
72- }
73-
74- // Use URL as key to avoid duplicates
75- if ( ! assetsMap . has ( url ) ) {
76- assetsMap . set ( url , {
77- url,
78- altText : altText || fileName ,
79- fileName : fileName || 'image' ,
80- } ) ;
81- }
82- }
83- }
84- }
85- }
86-
87- return Array . from ( assetsMap . values ( ) ) ;
88- }
89-
9020/**
9121 * Create Preview
9222 *
@@ -102,9 +32,7 @@ export const handler: FunctionEventHandler<
10232 context : FunctionEventContext
10333) => {
10434 const { contentTypeIds, documentJson } = event . body ;
105- console . log ( 'Content types:' , contentTypeIds ) ;
10635 const { openAiApiKey } = context . appInstallationParameters as AppInstallationParameters ;
107- console . log ( 'Open AI API Key:' , openAiApiKey ) ;
10836 if ( ! documentJson ) {
10937 throw new Error ( 'Document JSON is required' ) ;
11038 }
@@ -115,26 +43,20 @@ export const handler: FunctionEventHandler<
11543
11644 const cma = initContentfulManagementClient ( context ) ;
11745 const contentTypes = await fetchContentTypes ( cma , new Set < string > ( contentTypeIds ) ) ;
118- console . log ( 'Content types:' , contentTypes ) ;
11946
12047 // Use the same AI agent to analyze the document and generate proposed entries
12148 const aiDocumentResponse = await createPreviewWithAgent ( {
12249 documentJson,
12350 openAiApiKey,
12451 contentTypes,
12552 } ) ;
126- console . log ( 'AI document response:' , aiDocumentResponse ) ;
127- // Extract asset information from entries
128- const assets = extractAssetsFromEntries ( aiDocumentResponse . entries , contentTypes ) ;
12953
130- console . log ( 'Assets:' , assets ) ;
131- // Return plan data without creating entries
13254 return {
13355 success : true ,
13456 response : {
135- ... aiDocumentResponse ,
136- assets ,
137- totalAssets : assets . length ,
57+ entries : aiDocumentResponse . entries ,
58+ summary : aiDocumentResponse . summary ,
59+ totalEntries : aiDocumentResponse . totalEntries ,
13860 } ,
13961 } ;
14062} ;
0 commit comments