-
Notifications
You must be signed in to change notification settings - Fork 170
[INTEG-3262] feat: create ai content type parser agent #10255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Harika Kondur (harikakondur)
merged 20 commits into
master
from
INTEG-3262-create-ai-agent-content-type-parser
Nov 18, 2025
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
913aa3c
feat: helper function to fetch content types
harikakondur 12129ce
wip: content parser agent
harikakondur dc133c6
chore:cleanup
harikakondur 510d858
feat: content type service
harikakondur 97f3d5a
Merge https://github.com/contentful/apps into INTEG-3262-create-ai-ag…
harikakondur a71c0e6
ct picker modal for testing
harikakondur 22d528f
fix: allow for multi select in content type picker modal
harikakondur e852115
calls app action
harikakondur 3e9359a
app action fetches a single content type
harikakondur de47cf5
app action fetches multiple content types
harikakondur 1c5c92f
app action calls agent!!!
harikakondur 0264f2a
fix: openai api key parameter bug
harikakondur 4a0f390
chore: cleanup config page
harikakondur f4066b6
chore: cleanup variable names
harikakondur 8ec9ae7
cleanup:logs n comments
harikakondur fa7e5f6
fic: agent loader
harikakondur dc90ce9
Merge branch 'master' into INTEG-3262-create-ai-agent-content-type-pa…
harikakondur 7862d38
chore: cleanup styles
harikakondur 762c80c
fix: creating a `getAppActionId()` util to clean up code
harikakondur aa5fdca
Merge branch 'master' into INTEG-3262-create-ai-agent-content-type-pa…
harikakondur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 0 additions & 51 deletions
51
apps/google-docs/functions/agents/contentTypeParser.agent.ts
This file was deleted.
Oops, something went wrong.
92 changes: 92 additions & 0 deletions
92
apps/google-docs/functions/agents/contentTypeParserAgent/contentTypeParser.agent.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { createOpenAI } from '@ai-sdk/openai'; | ||
| import { FinalContentTypesResultSummary, FinalContentTypesAnalysisSchema } from './schema'; | ||
| import { generateObject } from 'ai'; | ||
| import { ContentTypeProps } from 'contentful-management'; | ||
|
|
||
| export interface ContentTypeParserConfig { | ||
| contentTypes: ContentTypeProps[]; | ||
| openAiApiKey: string; | ||
| } | ||
|
|
||
| /** | ||
| * AI Agent that parses an array of Contentful content types and generates structured summaries | ||
| * | ||
| * @param contentTypes - Array of Contentful content type objects | ||
| * @param config - Optional configuration for the AI model | ||
| * @returns Promise resolving to structured parse result with summaries | ||
| * | ||
| */ | ||
| export async function analyzeContentTypes({ | ||
| contentTypes, | ||
| openAiApiKey, | ||
| }: ContentTypeParserConfig): Promise<FinalContentTypesResultSummary> { | ||
| // TODO: Double check these values and make sure they are compatible because not every user will have a key | ||
| // to access all models | ||
| const modelVersion = 'gpt-4o'; | ||
| const temperature = 0.3; | ||
|
|
||
| const openaiClient = createOpenAI({ | ||
| apiKey: openAiApiKey, | ||
| }); | ||
|
|
||
| const prompt = buildAnalysisPrompt(contentTypes); | ||
|
|
||
| const result = await generateObject({ | ||
| model: openaiClient(modelVersion), | ||
| schema: FinalContentTypesAnalysisSchema, | ||
| temperature, | ||
| system: buildSystemPrompt(), | ||
| prompt, | ||
| }); | ||
|
|
||
| const finalAnalysis = result.object as FinalContentTypesResultSummary; | ||
| return finalAnalysis; | ||
| } | ||
|
|
||
| /** | ||
| * Builds the system prompt for the AI | ||
| */ | ||
| function buildSystemPrompt(): string { | ||
| return `You are an expert Contentful content modeling analyst. Your role is to analyze Contentful content type definitions and provide clear, actionable summaries. | ||
|
|
||
| Your analysis should: | ||
| 1. Identify the purpose and intended use of each content type | ||
| 2. Explain what each field represents and how it should be used | ||
| 3. Identify relationships between content types | ||
| 4. Assess the overall model complexity | ||
| 5. Provide practical recommendations for content editors and developers | ||
|
|
||
| Focus on clarity and actionability. Your summaries will be used by: | ||
| - Content editors who need to understand how to use content types | ||
| - Content strategists planning content architecture`; | ||
| } | ||
|
|
||
| /** | ||
| * Builds the analysis prompt from content type data | ||
| */ | ||
| function buildAnalysisPrompt(contentTypes: ContentTypeProps[]): string { | ||
| const contentTypeList = contentTypes.map((ct) => ct.name).join(', '); | ||
| const totalFields = contentTypes.reduce((sum, ct) => sum + (ct.fields?.length || 0), 0); | ||
|
|
||
| return `Analyze the following Contentful content type definitions and provide concise summaries. | ||
|
|
||
| CONTENT TYPES TO ANALYZE: ${contentTypeList} | ||
| TOTAL CONTENT TYPES: ${contentTypes.length} | ||
| TOTAL FIELDS ACROSS ALL TYPES: ${totalFields} | ||
|
|
||
| CONTENT TYPE DEFINITIONS: | ||
| ${JSON.stringify(contentTypes, null, 2)} | ||
|
|
||
| For each content type, provide: | ||
| 1. Clear description of what this content type represents | ||
| 2. The intended purpose and use cases | ||
| 3. Total field count | ||
| 4. Names of 3-5 most important/key fields | ||
| 5. 2-3 practical usage recommendations | ||
|
|
||
| Also provide: | ||
| - Overall summary of the content model (2-3 sentences) | ||
| - Complexity assessment (simple/moderate/complex as a string) | ||
|
|
||
| Keep responses concise and actionable.`; | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
apps/google-docs/functions/agents/contentTypeParserAgent/schema.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { z } from 'zod'; | ||
|
|
||
| // Schema Definitions for the Content Type Parser Agent | ||
|
|
||
| // Each invidual CT analysis by the AI Agent output schema | ||
| export const ContentTypeAnalysisSchema = z.object({ | ||
| id: z.string(), | ||
| name: z.string(), | ||
| description: z.string(), | ||
| purpose: z.string(), | ||
| fieldCount: z.number(), | ||
| keyFields: z.array(z.string()), | ||
| recommendations: z.array(z.string()), | ||
| }); | ||
|
|
||
| // The entire set of CT analyses by the AI Agent output schema | ||
| export const FinalContentTypesAnalysisSchema = z.object({ | ||
| contentTypes: z.array(ContentTypeAnalysisSchema), | ||
| summary: z.string(), | ||
| complexity: z.string(), | ||
| }); | ||
|
|
||
| export type ContentTypeSummary = z.infer<typeof ContentTypeAnalysisSchema>; | ||
| export type FinalContentTypesResultSummary = z.infer<typeof FinalContentTypesAnalysisSchema>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { PlainClientAPI } from 'contentful-management'; | ||
|
|
||
| /* | ||
| * Fetches the user selected content types that the user wants the AI to create entries for | ||
| * @param cma Content Management API client | ||
| * @param contentTypeIds Array of content type IDs | ||
| * @returns array of Content type json objects | ||
| */ | ||
| export const fetchContentTypes = async ( | ||
| cma: PlainClientAPI, | ||
| contentTypeIds: Set<string> | ||
| ): Promise<any> => { | ||
| try { | ||
| const response = await cma.contentType.getMany({}); | ||
| const selectedContentTypes = response.items.filter((item) => contentTypeIds.has(item.sys.id)); | ||
| return selectedContentTypes; | ||
| } catch (error) { | ||
| throw new Error(`Failed to fetch content types ${contentTypeIds}: ${error}`); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { FunctionEventContext } from '@contentful/node-apps-toolkit'; | ||
| import { PlainClientAPI, createClient } from 'contentful-management'; | ||
|
|
||
| export function initContentfulManagementClient(context: FunctionEventContext): PlainClientAPI { | ||
| if (!context.cmaClientOptions) { | ||
| throw new Error( | ||
| 'Contentful Management API client options are only provided for certain function types. To learn more about using the CMA within functions, see https://www.contentful.com/developers/docs/extensibility/app-framework/functions/#using-the-cma.' | ||
| ); | ||
| } | ||
| return createClient(context.cmaClientOptions, { | ||
| type: 'plain', | ||
| defaults: { | ||
| spaceId: context.spaceId, | ||
| environmentId: context.environmentId, | ||
| }, | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ES2020", | ||
| "module": "CommonJS", | ||
| "lib": ["ES2020"], | ||
| "strict": true, | ||
|
|
||
| }, | ||
| "include": ["./**/*.ts"], | ||
| "exclude": ["node_modules", "build"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { defineConfig } from 'vitest/config'; | ||
| import { loadEnv } from 'vite'; | ||
| import path from 'path'; | ||
|
|
||
| export default defineConfig(({ mode }) => ({ | ||
| test: { | ||
| globals: true, | ||
| environment: 'node', | ||
| // Load .env from parent directory (google-docs root) | ||
| env: loadEnv(mode, path.resolve(__dirname, '..'), ''), | ||
| // Don't use the React setup file for functions tests | ||
| setupFiles: undefined, | ||
| // Only include tests in the functions directory | ||
| include: ['**/*.test.ts', '**/*.spec.ts'], | ||
| }, | ||
| })); | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we go with using OpenAI for the GA release, we could allow the user to select their model from a dropdown list in the app configuration. We could get the list of available models from an API, similar to what we do in the other OpenAI apps: https://github.com/contentful/apps/blob/master/apps/ai-content-generator/src/components/config/model/Model.tsx#L23
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add this to a fast follow ticket