Optimize academy
This commit is contained in:
@@ -344,7 +344,18 @@ final class AcademyAccessService
|
||||
$previewImage = $this->promptPreviewImagePayload((string) ($prompt->preview_image ?? ''));
|
||||
$documentation = $this->promptDocumentationPayload($prompt->documentation);
|
||||
$placeholders = $this->promptPlaceholdersPayload((array) ($prompt->placeholders ?? []));
|
||||
$allFilledExamples = $this->promptFilledExamplesPayload((array) ($prompt->filled_examples ?? []));
|
||||
$filledExamplesTotal = count($allFilledExamples);
|
||||
$hasFullFilledExamplesAccess = (bool) (($viewer?->hasAcademyProAccess() ?? false) || ($viewer?->hasStaffAccess() ?? false));
|
||||
$hasPartialFilledExamplesAccess = (bool) ($viewer?->hasAcademyCreatorAccess() ?? false);
|
||||
$visibleFilledExamples = match (true) {
|
||||
! $includeFull => [],
|
||||
$hasFullFilledExamplesAccess => $allFilledExamples,
|
||||
$hasPartialFilledExamplesAccess => array_slice($allFilledExamples, 0, 2),
|
||||
default => [],
|
||||
};
|
||||
$hasPlaceholderInputs = $this->promptHasPlaceholderInputs((string) $prompt->prompt, $placeholders);
|
||||
$hasFilledExamples = $allFilledExamples !== [];
|
||||
$hasHelperPrompts = $this->promptHelperPromptsPayload((array) ($prompt->helper_prompts ?? [])) !== [];
|
||||
$hasPromptVariants = $this->promptVariantsPayload((array) ($prompt->prompt_variants ?? [])) !== [];
|
||||
$helperPrompts = $authorized && $includeFull
|
||||
@@ -367,6 +378,12 @@ final class AcademyAccessService
|
||||
'documentation' => $documentation,
|
||||
'placeholders' => $placeholders,
|
||||
'has_placeholder_inputs' => $hasPlaceholderInputs,
|
||||
'filled_examples' => $visibleFilledExamples,
|
||||
'has_filled_examples' => $hasFilledExamples,
|
||||
'filled_examples_total' => $filledExamplesTotal,
|
||||
'can_access_filled_examples' => ($hasFullFilledExamplesAccess || $hasPartialFilledExamplesAccess) && $includeFull,
|
||||
'has_more_filled_examples' => $filledExamplesTotal > count($visibleFilledExamples),
|
||||
'has_full_filled_examples_access' => $hasFullFilledExamplesAccess,
|
||||
'has_helper_prompts' => $hasHelperPrompts,
|
||||
'has_prompt_variants' => $hasPromptVariants,
|
||||
'helper_prompts' => $helperPrompts,
|
||||
@@ -396,6 +413,47 @@ final class AcademyAccessService
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, mixed> $filledExamples
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
private function promptFilledExamplesPayload(array $filledExamples): array
|
||||
{
|
||||
return collect($filledExamples)
|
||||
->filter(static fn ($example): bool => is_array($example))
|
||||
->map(function (array $example): array {
|
||||
return [
|
||||
'title' => $this->nullableTrimmedString($example['title'] ?? null),
|
||||
'description' => $this->nullableTrimmedString($example['description'] ?? null),
|
||||
'placeholder_values' => collect(is_array($example['placeholder_values'] ?? null) ? $example['placeholder_values'] : [])
|
||||
->mapWithKeys(function ($value, $key): array {
|
||||
$normalizedKey = trim((string) $key);
|
||||
|
||||
if ($normalizedKey === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [$normalizedKey => $value];
|
||||
})
|
||||
->all(),
|
||||
'prompt' => trim((string) ($example['prompt'] ?? '')),
|
||||
'negative_prompt' => $this->nullableTrimmedString($example['negative_prompt'] ?? null),
|
||||
];
|
||||
})
|
||||
->filter(function (array $example): bool {
|
||||
return collect([
|
||||
$example['title'] ?? null,
|
||||
$example['description'] ?? null,
|
||||
$example['prompt'] ?? null,
|
||||
$example['negative_prompt'] ?? null,
|
||||
$example['placeholder_values'] ?? null,
|
||||
])->contains(fn ($item): bool => $item !== null && $item !== '' && $item !== []);
|
||||
})
|
||||
->take(5)
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $documentation
|
||||
* @return array<string, mixed>
|
||||
|
||||
Reference in New Issue
Block a user