Skip to content

Commit f875b96

Browse files
authored
Merge pull request #11049 from appwrite/console-module
2 parents 6b881d4 + 2e57e5a commit f875b96

17 files changed

Lines changed: 471 additions & 194 deletions

File tree

app/config/services.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'name' => 'Console',
2121
'subtitle' => '',
2222
'description' => '',
23-
'controller' => 'web/console.php',
23+
'controller' => '', // Uses modules
2424
'sdk' => false,
2525
'docs' => false,
2626
'docsUrl' => '',
@@ -270,9 +270,9 @@
270270
'console' => [
271271
'key' => 'console',
272272
'name' => 'Console',
273-
'subtitle' => 'The Console service allows you to interact with console relevant informations.',
273+
'subtitle' => 'The Console service allows you to interact with console relevant information.',
274274
'description' => '',
275-
'controller' => 'api/console.php',
275+
'controller' => '', // Uses modules
276276
'sdk' => true,
277277
'docs' => true,
278278
'docsUrl' => '',

app/controllers/api/console.php

Lines changed: 0 additions & 147 deletions
This file was deleted.

app/controllers/web/console.php

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Appwrite\Platform\Modules\Console\Http\Assistant;
4+
5+
use Appwrite\SDK\AuthType;
6+
use Appwrite\SDK\ContentType;
7+
use Appwrite\SDK\Method;
8+
use Appwrite\SDK\Response as SDKResponse;
9+
use Appwrite\Utopia\Response;
10+
use Utopia\Platform\Action;
11+
use Utopia\Platform\Scope\HTTP;
12+
use Utopia\Validator\Text;
13+
14+
class Create extends Action
15+
{
16+
use HTTP;
17+
18+
public static function getName(): string
19+
{
20+
return 'createAssistantQuery';
21+
}
22+
23+
public function __construct()
24+
{
25+
$this
26+
->setHttpMethod(Action::HTTP_REQUEST_METHOD_POST)
27+
->setHttpPath('/v1/console/assistant')
28+
->desc('Create assistant query')
29+
->groups(['api', 'assistant'])
30+
->label('scope', 'assistant.read')
31+
->label('sdk', new Method(
32+
namespace: 'assistant',
33+
group: 'console',
34+
name: 'chat',
35+
description: '/docs/references/assistant/chat.md',
36+
auth: [AuthType::ADMIN],
37+
responses: [
38+
new SDKResponse(
39+
code: Response::STATUS_CODE_OK,
40+
model: Response::MODEL_NONE,
41+
)
42+
],
43+
contentType: ContentType::TEXT
44+
))
45+
->label('abuse-limit', 15)
46+
->label('abuse-key', 'userId:{userId}')
47+
->param('prompt', '', new Text(2000), 'Prompt. A string containing questions asked to the AI assistant.')
48+
->inject('response')
49+
->callback($this->action(...));
50+
}
51+
52+
public function action(string $prompt, Response $response)
53+
{
54+
$ch = curl_init('http://appwrite-assistant:3003/v1/models/assistant/prompt');
55+
$responseHeaders = [];
56+
$query = json_encode(['prompt' => $prompt]);
57+
$headers = ['accept: text/event-stream'];
58+
$handleEvent = function ($ch, $data) use ($response) {
59+
$response->chunk($data);
60+
61+
return \strlen($data);
62+
};
63+
64+
curl_setopt($ch, CURLOPT_WRITEFUNCTION, $handleEvent);
65+
66+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
67+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
68+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
69+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
70+
curl_setopt($ch, CURLOPT_TIMEOUT, 9000);
71+
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$responseHeaders) {
72+
$len = strlen($header);
73+
$header = explode(':', $header, 2);
74+
75+
if (count($header) < 2) { // ignore invalid headers
76+
return $len;
77+
}
78+
79+
$responseHeaders[strtolower(trim($header[0]))] = trim($header[1]);
80+
81+
return $len;
82+
});
83+
84+
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
85+
86+
curl_exec($ch);
87+
88+
curl_close($ch);
89+
90+
$response->chunk('', true);
91+
}
92+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Appwrite\Platform\Modules\Console\Http\Init;
4+
5+
use Appwrite\Extend\Exception;
6+
use Utopia\Database\Document;
7+
use Utopia\Platform\Action;
8+
9+
class API extends Action
10+
{
11+
public static function getName(): string
12+
{
13+
return 'consoleAPI';
14+
}
15+
16+
public function __construct()
17+
{
18+
$this
19+
->setType(Action::TYPE_INIT)
20+
->groups(['console'])
21+
->inject('project')
22+
->callback(function (Document $project) {
23+
if ($project->getId() !== 'console') {
24+
throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN);
25+
}
26+
});
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Appwrite\Platform\Modules\Console\Http\Init;
4+
5+
use Appwrite\Utopia\Request;
6+
use Appwrite\Utopia\Response;
7+
use Utopia\Platform\Action;
8+
9+
class Web extends Action
10+
{
11+
public static function getName(): string
12+
{
13+
return 'consoleWeb';
14+
}
15+
16+
public function __construct()
17+
{
18+
$this
19+
->setType(Action::TYPE_INIT)
20+
->groups(['web'])
21+
->inject('request')
22+
->inject('response')
23+
->callback(function (Request $request, Response $response) {
24+
$response
25+
->addHeader('X-Frame-Options', 'SAMEORIGIN') // Avoid console and homepage from showing in iframes
26+
->addHeader('X-XSS-Protection', '1; mode=block; report=/v1/xss?url=' . \urlencode($request->getURI()))
27+
->addHeader('X-UA-Compatible', 'IE=Edge') // Deny IE browsers from going into quirks mode
28+
;
29+
});
30+
}
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Appwrite\Platform\Modules\Console\Http\Redirects\Auth;
4+
5+
use Appwrite\Platform\Modules\Console\Http\Redirects\Base;
6+
7+
class Get extends Base
8+
{
9+
public static function getName(): string
10+
{
11+
return 'consoleRedirectAuth';
12+
}
13+
14+
protected function getPath(): string
15+
{
16+
return '/auth/*';
17+
}
18+
}

0 commit comments

Comments
 (0)