SuperSeller3000/src/Infrastructure/AI/Agent/SpecsResearchAgent.php

69 lines
2.3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace App\Infrastructure\AI\Agent;
use App\Infrastructure\AI\OllamaClientInterface;
use App\Infrastructure\AI\PromptTemplateService;
use App\Infrastructure\Search\WebSearchInterface;
final class SpecsResearchAgent
{
public function __construct(
private readonly OllamaClientInterface $client,
private readonly PromptTemplateService $prompts,
private readonly WebSearchInterface $search,
private readonly string $model,
) {
}
/**
feat: Frappe ERP matching, pipeline model cache, ACL, stock field, specs by type Frappe ERP: - findExistingCustomer() on FrappeErpAdapter — two-step name+address lookup - FrappeHttpClient: add put() method; switch invoice submit to PUT docstatus=1 (Frappe v16) - buildItemDescription() uses specsText + inventory number + serial number - Integration tests: find Simon Kühn, create real 1337€ invoice, cancel+delete in tearDown - FRAPPE_GENERIC_ITEM_CODE=SKU002 added to .env.local and bin/test-integration Pipeline — model cache: - PhotoUploadHandler: after vision, check DB for existing article with same modelNumber - On match: copy ebayTitle/ebayDescription/specsText/attributes, skip specs+JSON+eBay steps - DraftArticleHandler: apply model_match data and mark job complete directly - ArticleRepository: findCompletedByModelNumber() query Pipeline — specs by article type: - SpecsResearchAgent: accept attributeFields list, format as bullet list in {{fields}} var - SpecsResearchHandler: derive attribute names from ArticleType, pass to agent - SpecsResearchMessage: add attributeFields param - Prompt migration: replace hardcoded laptop spec list with {{fields}} placeholder Article: - specsText field (nullable text column + migration) - stock field visible on index and editable in CRUD form - addAttributeValue()/removeAttributeValue() adder-remover pair for Symfony form binding - AttributeValue::getArticle() getter - AttributeValueFormType: detect required attributes from ArticleType assignments, set required=true - ManualIngestType: add stock/quantity field (default 1, min 1) Users / ACL: - PermissionVoter: define named permission constants + allPermissions() - User: getGrantedPermissions()/setGrantedPermissions() helpers - UserCrudController: permissions checkbox group on edit form UI / assets: - public/css/admin/custom.css: red asterisk for required fields - DashboardController: register custom CSS Infra: - PipelineJobFailureListener: mark job failed (with real error) when Messenger exhausts retries - doctrine.yaml: exclude app.inventory_seq from schema diff - ErpAdapterInterface: add findExistingCustomer() Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-18 16:42:15 +00:00
* @param list<string> $attributeFields Attribute names defined for this article type
*
* @return array{specsText: string, correctedModelNumber: string}
*/
feat: Frappe ERP matching, pipeline model cache, ACL, stock field, specs by type Frappe ERP: - findExistingCustomer() on FrappeErpAdapter — two-step name+address lookup - FrappeHttpClient: add put() method; switch invoice submit to PUT docstatus=1 (Frappe v16) - buildItemDescription() uses specsText + inventory number + serial number - Integration tests: find Simon Kühn, create real 1337€ invoice, cancel+delete in tearDown - FRAPPE_GENERIC_ITEM_CODE=SKU002 added to .env.local and bin/test-integration Pipeline — model cache: - PhotoUploadHandler: after vision, check DB for existing article with same modelNumber - On match: copy ebayTitle/ebayDescription/specsText/attributes, skip specs+JSON+eBay steps - DraftArticleHandler: apply model_match data and mark job complete directly - ArticleRepository: findCompletedByModelNumber() query Pipeline — specs by article type: - SpecsResearchAgent: accept attributeFields list, format as bullet list in {{fields}} var - SpecsResearchHandler: derive attribute names from ArticleType, pass to agent - SpecsResearchMessage: add attributeFields param - Prompt migration: replace hardcoded laptop spec list with {{fields}} placeholder Article: - specsText field (nullable text column + migration) - stock field visible on index and editable in CRUD form - addAttributeValue()/removeAttributeValue() adder-remover pair for Symfony form binding - AttributeValue::getArticle() getter - AttributeValueFormType: detect required attributes from ArticleType assignments, set required=true - ManualIngestType: add stock/quantity field (default 1, min 1) Users / ACL: - PermissionVoter: define named permission constants + allPermissions() - User: getGrantedPermissions()/setGrantedPermissions() helpers - UserCrudController: permissions checkbox group on edit form UI / assets: - public/css/admin/custom.css: red asterisk for required fields - DashboardController: register custom CSS Infra: - PipelineJobFailureListener: mark job failed (with real error) when Messenger exhausts retries - doctrine.yaml: exclude app.inventory_seq from schema diff - ErpAdapterInterface: add findExistingCustomer() Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-18 16:42:15 +00:00
public function research(string $modelName, string $articleTypeName, string $manufacturer = '', array $attributeFields = []): array
{
$subject = trim(($manufacturer !== '' ? $manufacturer.' ' : '').$modelName);
$searchResults = $this->search->search("{$subject} {$articleTypeName} specifications");
feat: Frappe ERP matching, pipeline model cache, ACL, stock field, specs by type Frappe ERP: - findExistingCustomer() on FrappeErpAdapter — two-step name+address lookup - FrappeHttpClient: add put() method; switch invoice submit to PUT docstatus=1 (Frappe v16) - buildItemDescription() uses specsText + inventory number + serial number - Integration tests: find Simon Kühn, create real 1337€ invoice, cancel+delete in tearDown - FRAPPE_GENERIC_ITEM_CODE=SKU002 added to .env.local and bin/test-integration Pipeline — model cache: - PhotoUploadHandler: after vision, check DB for existing article with same modelNumber - On match: copy ebayTitle/ebayDescription/specsText/attributes, skip specs+JSON+eBay steps - DraftArticleHandler: apply model_match data and mark job complete directly - ArticleRepository: findCompletedByModelNumber() query Pipeline — specs by article type: - SpecsResearchAgent: accept attributeFields list, format as bullet list in {{fields}} var - SpecsResearchHandler: derive attribute names from ArticleType, pass to agent - SpecsResearchMessage: add attributeFields param - Prompt migration: replace hardcoded laptop spec list with {{fields}} placeholder Article: - specsText field (nullable text column + migration) - stock field visible on index and editable in CRUD form - addAttributeValue()/removeAttributeValue() adder-remover pair for Symfony form binding - AttributeValue::getArticle() getter - AttributeValueFormType: detect required attributes from ArticleType assignments, set required=true - ManualIngestType: add stock/quantity field (default 1, min 1) Users / ACL: - PermissionVoter: define named permission constants + allPermissions() - User: getGrantedPermissions()/setGrantedPermissions() helpers - UserCrudController: permissions checkbox group on edit form UI / assets: - public/css/admin/custom.css: red asterisk for required fields - DashboardController: register custom CSS Infra: - PipelineJobFailureListener: mark job failed (with real error) when Messenger exhausts retries - doctrine.yaml: exclude app.inventory_seq from schema diff - ErpAdapterInterface: add findExistingCustomer() Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-18 16:42:15 +00:00
$fieldsList = [] !== $attributeFields
? implode("\n", array_map(static fn (string $f) => "- {$f}", $attributeFields))
: '- all relevant technical specifications';
$prompt = $this->prompts->render('specs_research', [
'articleType' => $articleTypeName,
'subject' => $subject,
'searchResults' => $searchResults !== '' ? $searchResults : 'No web results available.',
feat: Frappe ERP matching, pipeline model cache, ACL, stock field, specs by type Frappe ERP: - findExistingCustomer() on FrappeErpAdapter — two-step name+address lookup - FrappeHttpClient: add put() method; switch invoice submit to PUT docstatus=1 (Frappe v16) - buildItemDescription() uses specsText + inventory number + serial number - Integration tests: find Simon Kühn, create real 1337€ invoice, cancel+delete in tearDown - FRAPPE_GENERIC_ITEM_CODE=SKU002 added to .env.local and bin/test-integration Pipeline — model cache: - PhotoUploadHandler: after vision, check DB for existing article with same modelNumber - On match: copy ebayTitle/ebayDescription/specsText/attributes, skip specs+JSON+eBay steps - DraftArticleHandler: apply model_match data and mark job complete directly - ArticleRepository: findCompletedByModelNumber() query Pipeline — specs by article type: - SpecsResearchAgent: accept attributeFields list, format as bullet list in {{fields}} var - SpecsResearchHandler: derive attribute names from ArticleType, pass to agent - SpecsResearchMessage: add attributeFields param - Prompt migration: replace hardcoded laptop spec list with {{fields}} placeholder Article: - specsText field (nullable text column + migration) - stock field visible on index and editable in CRUD form - addAttributeValue()/removeAttributeValue() adder-remover pair for Symfony form binding - AttributeValue::getArticle() getter - AttributeValueFormType: detect required attributes from ArticleType assignments, set required=true - ManualIngestType: add stock/quantity field (default 1, min 1) Users / ACL: - PermissionVoter: define named permission constants + allPermissions() - User: getGrantedPermissions()/setGrantedPermissions() helpers - UserCrudController: permissions checkbox group on edit form UI / assets: - public/css/admin/custom.css: red asterisk for required fields - DashboardController: register custom CSS Infra: - PipelineJobFailureListener: mark job failed (with real error) when Messenger exhausts retries - doctrine.yaml: exclude app.inventory_seq from schema diff - ErpAdapterInterface: add findExistingCustomer() Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-18 16:42:15 +00:00
'fields' => $fieldsList,
]);
$result = $this->client->generate($this->model, $prompt);
if ('' === trim($result)) {
throw new \RuntimeException("No specifications found for model: {$modelName}");
}
return $this->parseResponse($result);
}
/** @return array{specsText: string, correctedModelNumber: string} */
private function parseResponse(string $raw): array
{
$correctedModelNumber = '';
if (preg_match('/^CORRECTED_MODEL_NUMBER:\s*(\S+)/m', $raw, $matches)) {
$correctedModelNumber = trim($matches[1]);
// Strip the line from the specs text
$raw = preg_replace('/^CORRECTED_MODEL_NUMBER:[^\n]*\n?/m', '', $raw) ?? $raw;
}
return [
'specsText' => trim($raw),
'correctedModelNumber' => $correctedModelNumber,
];
}
}