Skip to content

Commit 2def7c7

Browse files
committed
refactor: remove summary generation from document processing
- Eliminated the `generateSummary` function and its usage in document processing methods. - Updated `MessageManager` to directly set titles and descriptions based on document metadata. - Simplified formatted descriptions for PDF and text documents, removing summary references.
1 parent 8d32336 commit 2def7c7

1 file changed

Lines changed: 7 additions & 73 deletions

File tree

src/messageManager.ts

Lines changed: 7 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import {
1111
type UUID,
1212
createUniqueUuid,
1313
logger,
14-
trimTokens,
15-
parseJSONObjectFromText,
1614
} from "@elizaos/core";
1715
import type { Chat, Message, ReactionType, Update, Document } from "@telegraf/types";
1816
import type { Context, NarrowedContext, Telegraf } from "telegraf";
@@ -32,7 +30,6 @@ import fs from "fs";
3230
*/
3331
interface DocumentProcessingResult {
3432
title: string;
35-
summary: string;
3633
fullText: string;
3734
formattedDescription: string;
3835
fileName: string;
@@ -41,53 +38,6 @@ interface DocumentProcessingResult {
4138
error?: string;
4239
}
4340

44-
/**
45-
* Generates a summary for the provided text using a specified model.
46-
*
47-
* @param {IAgentRuntime} runtime - The runtime environment for the agent.
48-
* @param {string} text - The text to generate a summary for.
49-
* @returns {Promise<{ title: string; summary: string }>} An object containing the generated title and description.
50-
*/
51-
async function generateSummary(
52-
runtime: IAgentRuntime,
53-
text: string
54-
): Promise<{ title: string; summary: string }> {
55-
// make sure text is under 128k characters
56-
text = await trimTokens(text, 100000, runtime);
57-
58-
const prompt = `Please generate a concise summary for the following text:
59-
60-
Text: """
61-
${text}
62-
"""
63-
64-
Respond with a JSON object in the following format:
65-
\`\`\`json
66-
{
67-
"title": "Generated Title",
68-
"summary": "Generated summary and/or description of the text"
69-
}
70-
\`\`\``;
71-
72-
const response = await runtime.useModel(ModelType.TEXT_SMALL, {
73-
prompt,
74-
});
75-
76-
const parsedResponse = parseJSONObjectFromText(response);
77-
78-
if (parsedResponse?.title && parsedResponse?.summary) {
79-
return {
80-
title: parsedResponse.title,
81-
summary: parsedResponse.summary,
82-
};
83-
}
84-
85-
return {
86-
title: "",
87-
summary: "",
88-
};
89-
}
90-
9141
/**
9242
* Enum representing different types of media.
9343
* @enum { string }
@@ -214,7 +164,6 @@ export class MessageManager {
214164
// Generic fallback for unsupported types
215165
return {
216166
title: `Document: ${document.file_name || 'Unknown Document'}`,
217-
summary: `Type: ${document.mime_type || 'unknown'}\nSize: ${document.file_size || 0} bytes`,
218167
fullText: "",
219168
formattedDescription: `[Document: ${document.file_name || 'Unknown Document'}\nType: ${document.mime_type || 'unknown'}\nSize: ${document.file_size || 0} bytes]`,
220169
fileName: document.file_name || 'Unknown Document',
@@ -260,7 +209,6 @@ export class MessageManager {
260209
logger.warn("PDF service not available, using fallback");
261210
return {
262211
title: `PDF Document: ${document.file_name || 'Unknown Document'}`,
263-
summary: `Size: ${document.file_size || 0} bytes\nUnable to extract text content`,
264212
fullText: "",
265213
formattedDescription: `[PDF Document: ${document.file_name || 'Unknown Document'}\nSize: ${document.file_size || 0} bytes\nUnable to extract text content]`,
266214
fileName: document.file_name || 'Unknown Document',
@@ -277,17 +225,12 @@ export class MessageManager {
277225
const pdfBuffer = await response.arrayBuffer();
278226
const text = await pdfService.convertPdfToText(Buffer.from(pdfBuffer));
279227

280-
// Use generateSummary for context extraction
281-
const { title, summary } = await generateSummary(this.runtime, text);
282228

283229
logger.info(`PDF processed successfully: ${text.length} characters extracted`);
284230
return {
285-
title: title,
286-
summary: summary,
231+
title: document.file_name || 'Unknown Document',
287232
fullText: text,
288-
formattedDescription: title
289-
? `[PDF Document: ${document.file_name || 'Unknown Document'}\nTitle: ${title}\nSummary: ${summary}\n\nFull Content:\n${text}\n--- END DOCUMENT]`
290-
: `[PDF Document: ${document.file_name || 'Unknown Document'}\nContent: ${text.substring(0, 500)}... [Document truncated]`,
233+
formattedDescription: `[PDF Document: ${document.file_name || 'Unknown Document'}\nSize: ${document.file_size || 0} bytes\nUnable to extract text content]`,
291234
fileName: document.file_name || 'Unknown Document',
292235
mimeType: document.mime_type,
293236
fileSize: document.file_size,
@@ -297,7 +240,6 @@ export class MessageManager {
297240
logger.error("Error processing PDF document:", error);
298241
return {
299242
title: `PDF Document: ${document.file_name || 'Unknown Document'}`,
300-
summary: `Size: ${document.file_size || 0} bytes\nError: Unable to extract text content`,
301243
fullText: "",
302244
formattedDescription: `[PDF Document: ${document.file_name || 'Unknown Document'}\nSize: ${document.file_size || 0} bytes\nError: Unable to extract text content]`,
303245
fileName: document.file_name || 'Unknown Document',
@@ -319,17 +261,11 @@ export class MessageManager {
319261

320262
const text = await response.text();
321263

322-
// Use generateSummary for context extraction
323-
const { title, summary } = await generateSummary(this.runtime, text);
324-
325264
logger.info(`Text document processed successfully: ${text.length} characters extracted`);
326265
return {
327-
title: title,
328-
summary: summary,
266+
title: document.file_name || 'Unknown Document',
329267
fullText: text,
330-
formattedDescription: title
331-
? `[Text Document: ${document.file_name || 'Unknown Document'}\nTitle: ${title}\nSummary: ${summary}\n\nFull Content:\n${text}\n--- END DOCUMENT]`
332-
: `[Text Document: ${document.file_name || 'Unknown Document'}\nContent: ${text.substring(0, 500)}... [Document truncated]`,
268+
formattedDescription: `[Text Document: ${document.file_name || 'Unknown Document'}\nSize: ${document.file_size || 0} bytes\nError: Unable to read content]`,
333269
fileName: document.file_name || 'Unknown Document',
334270
mimeType: document.mime_type,
335271
fileSize: document.file_size,
@@ -339,7 +275,6 @@ export class MessageManager {
339275
logger.error("Error processing text document:", error);
340276
return {
341277
title: `Text Document: ${document.file_name || 'Unknown Document'}`,
342-
summary: `Size: ${document.file_size || 0} bytes\nError: Unable to read content`,
343278
fullText: "",
344279
formattedDescription: `[Text Document: ${document.file_name || 'Unknown Document'}\nSize: ${document.file_size || 0} bytes\nError: Unable to read content]`,
345280
fileName: document.file_name || 'Unknown Document',
@@ -382,12 +317,11 @@ export class MessageManager {
382317

383318
// Use structured data directly instead of regex parsing
384319
const title = documentInfo.title;
385-
const summary = documentInfo.summary;
386320
const fullText = documentInfo.fullText;
387321

388322
// Add document content to processedContent so agent can access it
389323
if (fullText) {
390-
const documentContent = `\n\n--- DOCUMENT CONTENT ---\nTitle: ${title}\nSummary: ${summary}\n\nFull Content:\n${fullText}\n--- END DOCUMENT ---\n\n`;
324+
const documentContent = `\n\n--- DOCUMENT CONTENT ---\nTitle: ${title}\n\nFull Content:\n${fullText}\n--- END DOCUMENT ---\n\n`;
391325
processedContent += documentContent;
392326
}
393327

@@ -396,8 +330,8 @@ export class MessageManager {
396330
url: fileLink.toString(),
397331
title: title,
398332
source: document.mime_type?.startsWith("application/pdf") ? "PDF" : "Document",
399-
description: summary,
400-
text: fullText || summary, // Use full text if available, fallback to summary
333+
description: documentInfo.formattedDescription,
334+
text: fullText,
401335
});
402336
logger.info(`Document processed successfully: ${documentInfo.fileName}`);
403337
} catch (error) {

0 commit comments

Comments
 (0)