Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/google-docs/contentful-app-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
"path": "functions/createEntriesFromDocument.js",
"entryFile": "functions/createEntriesFromDocument.ts",
"allowNetworks": [
"https://api.openai.com"
"https://api.openai.com",
"https://docs.googleapis.com",
"https://docs.google.com",
"https://jsonplaceholder.typicode.com"
],
"accepts": [
"appaction.call"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
import { ContentTypeProps } from 'contentful-management';
import { fetchGoogleDoc } from '../../service/googleDriveService';

/**
* Configuration for the content type parser
*/
interface DocumentParserConfig {
openaiApiKey: string;
modelVersion: string;
jsonData: any;
document: any;
// contentTypes: ContentTypeProps[];
googleDocUrl: string;
}

/**
Expand All @@ -26,9 +26,11 @@ interface DocumentParserConfig {
* @returns Promise resolving to LLM response
*/
export async function createDocument(config: DocumentParserConfig) {
const { openaiApiKey, modelVersion, jsonData, document } = config;
const prompt = buildPrompt(jsonData);
return await callOpenAI(prompt, modelVersion, openaiApiKey);
const { googleDocUrl } = config;
const googleDocContent = await fetchGoogleDoc(googleDocUrl);
// const prompt = buildPrompt(jsonData);
// return await callOpenAI(prompt, modelVersion, openaiApiKey);
return googleDocContent as string;
}

function buildPrompt(jsonData: any): string {
Expand Down
21 changes: 8 additions & 13 deletions apps/google-docs/functions/createEntriesFromDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import { analyzeContentTypes } from './agents/contentTypeParserAgent/contentType
import { createDocument } from './agents/documentParserAgent/documentParser.agent';
import { fetchContentTypes } from './service/contentTypeService';
import { initContentfulManagementClient } from './service/initCMAClient';
import { fetchGoogleDoc } from './service/googleDriveService';

export type AppActionParameters = {
contentTypeIds: string[];
prompt: string;
googleDocUrl: string;
};

interface AppInstallationParameters {
openAiApiKey: string;
}
Expand All @@ -28,28 +30,21 @@ export const handler: FunctionEventHandler<
event: AppActionRequest<'Custom', AppActionParameters>,
context: FunctionEventContext
) => {
const { contentTypeIds } = event.body;
const { contentTypeIds, googleDocUrl } = event.body;
const { openAiApiKey } = context.appInstallationParameters as AppInstallationParameters;

// INTEG-3262 and INTEG-3263: Take in Content Type, Prompt, and Upload File from user

const cma = initContentfulManagementClient(context);

const contentTypes = await fetchContentTypes(cma, new Set<string>(contentTypeIds));
const contentTypeParserAgentResult = await analyzeContentTypes({ contentTypes, openAiApiKey });

console.log('contentTypeParserAgentResult', contentTypeParserAgentResult);
const contentTypeParserAgentResult = await analyzeContentTypes({ contentTypes, openAiApiKey });
// console.log('contentTypeParserAgentResult', contentTypeParserAgentResult);

// INTEG-3261: Pass the ai content type response to the observer for analysis
// createContentTypeObservationsFromLLMResponse()

// INTEG-3263: Implement the document parser agent
// const aiDocumentResponse = createDocument({
// openaiApiKey: openAiApiKey,
// modelVersion: 'gpt-4o',
// jsonData: aiContentTypeResponse,
// document: document,
// });
const aiDocumentResponse = await createDocument({ googleDocUrl });

// INTEG-3261: Pass the ai document response to the observer for analysis
// createDocumentObservationsFromLLMResponse()
Expand All @@ -60,5 +55,5 @@ export const handler: FunctionEventHandler<
// INTEG-3265: Create the assets in Contentful using the asset service
// await createAssets()

return { success: true, response: contentTypeParserAgentResult };
return { success: true, response: { contentTypeParserAgentResult, aiDocumentResponse } };
};
12 changes: 12 additions & 0 deletions apps/google-docs/functions/service/googleDriveService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export async function fetchGoogleDoc(googleDocUrl: string): Promise<string> {
// Extract /d/{id}
const m = googleDocUrl.match(/\/d\/([a-zA-Z0-9-_]+)/);
if (!m) throw new Error('Invalid Google Doc URL format');
const documentId = m[1];

const exportUrl = `https://docs.google.com/document/d/${documentId}/export?format=html`;

const res = await fetch(exportUrl);
const html = await res.text();
return html;
}
Loading
Loading