Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DPIB-496: AI Feature integrations. #1

Merged
merged 30 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3cfeeb4
Init composer.json.
dmiseev Sep 4, 2024
9aec9d0
Implemented SprykerEco/ImageSearchAi.
bohdanyevtukhov Sep 6, 2024
50a0650
Normalize ImageSearchAiWidget
supproduction Sep 7, 2024
2d58b12
Fixed code review comments.
bohdanyevtukhov Sep 9, 2024
9823d08
Fixed code review comments.
bohdanyevtukhov Sep 9, 2024
61f7582
Merge branch 'feature/demo/dev-ai-integrations' into feautre/ai-integ…
supproduction Sep 9, 2024
30ec25c
Fixed CSRF token.
bohdanyevtukhov Sep 9, 2024
e943a39
Fixed code style.
bohdanyevtukhov Sep 9, 2024
2f47709
Merge branch 'feature/demo/dev-ai-integrations' into feautre/ai-integ…
supproduction Sep 9, 2024
1d1f7e5
Merge pull request #2 from spryker-eco/feautre/ai-integrations-search…
supproduction Sep 9, 2024
5a59a0c
Fixed transfer quotes.
bohdanyevtukhov Sep 9, 2024
0fb58ed
Adjusted image search ai module.
dmiseev Sep 10, 2024
c75787f
Added CI.
dmiseev Sep 10, 2024
08dc66a
Adjusted CI.
dmiseev Sep 10, 2024
34f1d08
Adjusted CI.
dmiseev Sep 10, 2024
b66a878
add handling empty result
supproduction Sep 10, 2024
b69a1a5
Merge branch 'feature/demo/dev-ai-integrations' of github.com:spryker…
supproduction Sep 10, 2024
1137ae4
Added ImageSearchAiValidator.php.
bohdanyevtukhov Sep 11, 2024
cf8952b
add error state
supproduction Sep 11, 2024
d52cec1
Fixed CI.
bohdanyevtukhov Sep 11, 2024
ac54cf2
Fixed CI.
bohdanyevtukhov Sep 11, 2024
f167e61
Fixed CI issues.
dmiseev Sep 11, 2024
2d1c323
Fixed CI issues.
dmiseev Sep 11, 2024
bad7dee
fix image
supproduction Sep 11, 2024
b87c440
DPIB-496: Adjusted composer files [skip ci]
dmiseev Sep 11, 2024
9dcc9c5
DPIB-496: Adjusted composer files.
dmiseev Sep 11, 2024
bb2a833
added open ai success check
mantasmuliar Sep 11, 2024
7f6c6f1
Updated composer.json.
bohdanyevtukhov Sep 12, 2024
6acd785
Updated composer.json.
bohdanyevtukhov Sep 12, 2024
3307aa3
Fixed CI validation.
bohdanyevtukhov Sep 12, 2024
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
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at https://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.bat]
end_of_line = crlf
31 changes: 31 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Set the default behavior, in case people don't have core.autocrlf set.
* eol=lf
* text=auto

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
*.gif binary
*.jpeg binary
*.zip binary
*.phar binary
*.ttf binary
*.woff binary
*.woff2 binary
*.eot binary
*.ico binary
*.mo binary
*.pdf binary
*.xsd binary
*.ts binary
*.exe binary

# Remove files for archives generated using `git archive`
dependency.json export-ignore
.coveralls.yml export-ignore
.travis.yml export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
architecture-baseline.json export-ignore
psalm-report.json export-ignore linguist-generated=true
61 changes: 61 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: CI

on:
push:
branches:
- 'master'
pull_request:
workflow_dispatch:

jobs:
validation:
name: Validation
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: mbstring, intl, bcmath
coverage: none

- name: Composer Install
run: composer install --prefer-dist --no-interaction --profile

- name: Run validation
run: composer validate

- name: Syntax check
run: find ./src -path src -prune -o -type f -name '*.php' -print0 | xargs -0 -n1 -P4 php -l -n | (! grep -v "No syntax errors detected" )

- name: Run CodeStyle checks
run: composer cs-check

- name: PHPStan setup
run: composer stan-setup

- name: Run PHPStan
run: composer stan

lowest:
name: Prefer Lowest
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
extensions: mbstring, intl, bcmath
coverage: none

- name: Composer Install
run: composer install --prefer-dist --no-interaction --profile

- name: Composer Update
run: composer update --prefer-lowest --prefer-dist --no-interaction --profile -vvv
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# IDEs
/.idea
/.project
/nbproject
/.buildpath
/.settings
*.sublime-*
*.AppleDouble
*.AppleDB
*.AppleDesktop

# built client resources
/src/*/Zed/*/Static/Public
/src/*/Zed/*/Static/Assets/sprite

# propel classes
/src/*/Zed/*/Persistence/Propel/Base/*
/src/*/Zed/*/Persistence/Propel/Map/*

# OS
.DS_Store

# grunt stuff
.grunt
.sass-cache
/node_modules/
/tests/_output/*
!/tests/_output/.gitkeep
vendor
composer.lock
4 changes: 4 additions & 0 deletions .license
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ImageSearchAi Changelog

[Release Changelog](https://github.com/spryker-eco/image-search-ai/releases)
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016-present Spryker Systems GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# image-search-ai
# ImageSearchAi Module

ImageSearchAi module provides the functionality to search by image using AI.

## Installation

```
composer require spryker-eco/image-search-ai
```
1 change: 1 addition & 0 deletions architecture-baseline.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
54 changes: 54 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "spryker-eco/image-search-ai",
"type": "library",
"description": "ImageSearchAi module",
"license": "MIT",
"require": {
"php": ">=8.1",
"spryker-eco/open-ai": "dev-feature/demo/dev-ai-integrations",
"spryker/catalog": "^5.0.0",
"spryker/kernel": "^3.73.0",
"spryker/symfony": "^3.0.0",
"spryker/transfer": "^3.27.0",
"spryker/util-encoding": "^2.0.0",
"spryker/util-text": "^1.1.0"
},
"require-dev": {
"phpstan/phpstan": "*",
"spryker/code-sniffer": "*",
"spryker/router": "*"
},
"suggest": {
"spryker/router": "Use this module when you want to use the Router."
},
"autoload": {
"psr-4": {
"SprykerEco\\": "src/SprykerEco/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"repositories": [
{
"type": "vcs",
"url": "[email protected]:spryker-eco/open-ai.git"
}
],
"scripts": {
"cs-check": "phpcs -p -s --standard=vendor/spryker/code-sniffer/SprykerStrict/ruleset.xml src/",
"cs-fix": "phpcbf -p --standard=vendor/spryker/code-sniffer/SprykerStrict/ruleset.xml src/",
"stan": "phpstan analyse -c phpstan.neon -l 8 src/",
"stan-setup": "cp composer.json composer.backup && COMPOSER_MEMORY_LIMIT=-1 composer require --dev phpstan/phpstan:^0.12 && mv composer.backup composer.json"
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"config": {
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
5 changes: 5 additions & 0 deletions dependency.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"include": {
"spryker/transfer": "Provides transfer objects definition with strict types."
}
}
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters:
level: 8
paths:
- src/
ignoreErrors:
- '#Call to method .+ on an unknown class .+Transfer#'
- '#Parameter .+ of method .+ has invalid typehint type .+Transfer#'
- '#Return typehint of method .+ has invalid type .+Transfer#'
- '#Instantiated class .+Transfer not found#'
5 changes: 5 additions & 0 deletions psalm-report.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<transfers xmlns="spryker:transfer-01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="spryker:transfer-01 http://static.spryker.com/transfer-01.xsd">

<transfer name="ImageSearchFindTermsResponse" strict="true">
<property name="searchUrl" type="string"/>
<property name="firstMatchProductUrl" type="string"/>
</transfer>

<transfer name="ImageSearchFindTermsErrorResponse" strict="true">
<property name="error" type="string"/>
<property name="messages" type="ImageSearchFindTermsErrorMessage[]" singular="ImageSearchFindTermsErrorMessage"/>
</transfer>

<transfer name="ImageSearchFindTermsErrorMessage" strict="true">
<property name="message" type="string"/>
</transfer>

</transfers>
106 changes: 106 additions & 0 deletions src/SprykerEco/Yves/ImageSearchAi/Controller/ImageSearchController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace SprykerEco\Yves\ImageSearchAi\Controller;

use Generated\Shared\Transfer\ImageSearchFindTermsErrorMessageTransfer;
use Generated\Shared\Transfer\ImageSearchFindTermsErrorResponseTransfer;
use Generated\Shared\Transfer\ImageSearchFindTermsResponseTransfer;
use Spryker\Service\UtilText\Model\Url\Url;
use Spryker\Yves\Kernel\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Csrf\CsrfToken;

/**
* @method \SprykerEco\Yves\ImageSearchAi\ImageSearchAiFactory getFactory()
*/
class ImageSearchController extends AbstractController
{
/**
* @var string
*/
protected const REQUEST_BODY_CONTENT_KEY_IMAGE = 'image';

/**
* @var string
*/
protected const REQUEST_BODY_CONTENT_KEY_TOKEN = '_token';

/**
* @var string
*/
protected const CSRF_TOKEN_ID = 'image_search_ai_csrf';

/**
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function findTermsAction(Request $request): JsonResponse

Check failure on line 45 in src/SprykerEco/Yves/ImageSearchAi/Controller/ImageSearchController.php

View workflow job for this annotation

GitHub Actions / Validation

Parameter $request of method SprykerEco\Yves\ImageSearchAi\Controller\ImageSearchController::findTermsAction() has invalid typehint type Symfony\Component\HttpFoundation\Request.

Check failure on line 45 in src/SprykerEco/Yves/ImageSearchAi/Controller/ImageSearchController.php

View workflow job for this annotation

GitHub Actions / Validation

Parameter $request of method SprykerEco\Yves\ImageSearchAi\Controller\ImageSearchController::findTermsAction() has invalid typehint type Symfony\Component\HttpFoundation\Request.
{
$requestBodyContent = $this->getFactory()
->getUtilEncodingService()
->decodeJson((string)$request->getContent(), true);

Check failure on line 49 in src/SprykerEco/Yves/ImageSearchAi/Controller/ImageSearchController.php

View workflow job for this annotation

GitHub Actions / Validation

Call to method getContent() on an unknown class Symfony\Component\HttpFoundation\Request.

if (
!isset($requestBodyContent[static::REQUEST_BODY_CONTENT_KEY_TOKEN]) ||
!$this->getFactory()->getCsrfTokenManager()->isTokenValid(
new CsrfToken(static::CSRF_TOKEN_ID, $requestBodyContent[static::REQUEST_BODY_CONTENT_KEY_TOKEN]),
)
) {
return $this->createAjaxErrorResponse([
'error' => 'form.csrf.error.text',
], Response::HTTP_BAD_REQUEST);
}

unset($requestBodyContent[static::REQUEST_BODY_CONTENT_KEY_TOKEN]);

$errors = $this->getFactory()->createBase64ImageValidator()->validate($requestBodyContent);
if (count($errors)) {
$errorResponse = new ImageSearchFindTermsErrorResponseTransfer();
$errorResponse->setError('Bad request');
foreach ($errors as $error) {
$errorMessage = new ImageSearchFindTermsErrorMessageTransfer();
$errorMessage->setMessage($error->getMessage());
$errorResponse->addImageSearchFindTermsErrorMessage($errorMessage);
}

return new JsonResponse($errorResponse->toArray(), Response::HTTP_BAD_REQUEST);
}

$searchString = $this->getFactory()->createImageToSearchTermsTransformer()->transform(
$requestBodyContent[static::REQUEST_BODY_CONTENT_KEY_IMAGE],
)->getMessage();

if (!$searchString) {
return new JsonResponse((new ImageSearchFindTermsResponseTransfer())->toArray());
}

$searchResults = $this
->getFactory()
->getCatalogClient()
->catalogSuggestSearch($searchString, []);

return new JsonResponse([
'searchUrl' => Url::generate('/search', ['q' => $searchString])->build(),
'firstMatchProductUrl' => Url::generate($searchResults['suggestionByType']['product_abstract'][0]['url'])->build(),
]);
}

/**
* @param array<string, mixed> $errorData
* @param int $statusCode
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
protected function createAjaxErrorResponse(array $errorData, int $statusCode): JsonResponse
{
return $this->jsonResponse($errorData, $statusCode);
}
}
Loading
Loading