Skip to content

Commit

Permalink
chore: Fixee the thingee (#41)
Browse files Browse the repository at this point in the history
* chore: Also add the file_name property when invoking the UPDATE hook.

* chore: Refactor that twice-used code into a simple helper.

* fix: Load the file storage correctly, fix the log messages.

* fix: If we had tests this would be less painful.
  • Loading branch information
cafuego authored Mar 20, 2024
1 parent fce9231 commit 8a67861
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
33 changes: 22 additions & 11 deletions html/modules/custom/ocha_ai_summarize/ocha_ai_summarize.module
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,7 @@ function ocha_ai_summarize_node_update_summary(EntityInterface $entity) {
$item->language = $document_language;
$item->output_language = $output_language;
$item->num_paragraphs = $entity->get('field_number_of_paragraphs')->value ?? 3;

$item->file_name = NULL;
/** @var \Drupal\file\Plugin\Field\FieldType\FileItem $file_item */
$file_item = $entity->get('field_document')->first() ?? NULL;
if (!empty($file_item)) {
/** @var \Drupal\file\Entity\File $file */
$file = $this->entityTypeManager->getStorage('file')->load($file_item->getValue()['target_id']);
if (!empty($file)) {
$item->file_name = $file->getFilename();
}
}
$item->file_name = ocha_ai_summarize_get_entity_document($entity);

$queue->createItem($item);
}
Expand Down Expand Up @@ -221,6 +211,7 @@ function ocha_ai_summarize_node_update_action_points(EntityInterface $entity) {
$item->brain = $entity->get('field_ai_brain')->value;
$item->language = $document_language;
$item->output_language = $output_language;
$item->file_name = ocha_ai_summarize_get_entity_document($entity);

$queue->createItem($item);
}
Expand Down Expand Up @@ -1309,3 +1300,23 @@ function ocha_ai_summarize_get_aws_client_options() {
'region' => $region,
];
}

/**
* Helper to retrieve a document file name for the queue items.
*
* @return string
* A filename.
*/
function ocha_ai_summarize_get_entity_document(EntityInterface $entity) {
/** @var \Drupal\file\Plugin\Field\FieldType\FileItem $file_item */
$file_item = $entity->get('field_document')->first() ?? NULL;
if (empty($file_item)) {
return NULL;
}
/** @var \Drupal\file\Entity\File $file */
$file = \Drupal::entityTypeManager()->getStorage('file')->load($file_item->getValue()['target_id']);
if (empty($file)) {
return NULL;
}
return $file->getFilename();
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,18 @@ public function processItem($data) {
return;
}

\Drupal::logger('AI Summarize')->info('Generate action points for @file_name', [
'@file_name' => $data->file_name,
]);
if ($document_language !== $output_language) {
\Drupal::logger('AI Summarize')->info('Generate action points from @file_name and translate to @output_language using @brain', [
'@brain' => $data->brain,
'@output_language' => ocha_ai_summarize_get_lang_name_translated($output_language),
]);
}
else {
\Drupal::logger('AI Summarize')->info('Generate action points from @file_name using @brain', [
'@brain' => $data->brain,
'@file_name' => $data->file_name,
]);
}

$prompt = $this->t("Extract the action points from the following meeting minutes.", [], [
'langcode' => ocha_ai_summarize_get_lang_code($document_language),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,20 @@ public function processItem($data) {
}

if ($document_language !== $output_language) {
\Drupal::logger('AI Summarize')->info('Summarize the text from @file_name in @num_paragraphs paragraphs', [
'@file_name' => $data->file_name,
'@num_paragraphs' => $num_paragraphs,
]);
}
else {
\Drupal::logger('AI Summarize')->info('Summarize the text from @file_name in @num_paragraphs paragraphs and translate to @output_language', [
\Drupal::logger('AI Summarize')->info('Summarize the text from @file_name in @num_paragraphs paragraphs and translate to @output_language using @brain', [
'@brain' => $data->brain,
'@file_name' => $data->file_name,
'@num_paragraphs' => $num_paragraphs,
'@output_language' => ocha_ai_summarize_get_lang_name_translated($output_language),
]);
}
else {
\Drupal::logger('AI Summarize')->info('Summarize the text from @file_name in @num_paragraphs paragraphs using @brain', [
'@brain' => $data->brain,
'@file_name' => $data->file_name,
'@num_paragraphs' => $num_paragraphs,
]);
}

$prompt = $this->t('Summarize the following text in @num_paragraphs paragraphs', [
'@num_paragraphs' => $num_paragraphs,
Expand Down

0 comments on commit 8a67861

Please sign in to comment.