-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TASK: Add very basic module to create and edit assistants
- Loading branch information
Showing
7 changed files
with
206 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Sitegeist\Chatterbox\Controller; | ||
|
||
use Neos\Flow\Annotations as Flow; | ||
use Neos\Fusion\View\FusionView; | ||
use Neos\Neos\Controller\Module\AbstractModuleController; | ||
use OpenAI\Client; | ||
use OpenAI\Responses\Assistants\AssistantResponse; | ||
use Sitegeist\Chatterbox\Domain\AssistantRecord; | ||
|
||
class AssistantModuleController extends AbstractModuleController | ||
{ | ||
protected $defaultViewObjectName = FusionView::class; | ||
|
||
#[Flow\Inject] | ||
protected Client $client; | ||
|
||
public function indexAction():void | ||
{ | ||
$assistants = array_map( | ||
fn(AssistantResponse $assistantResponse) => AssistantRecord::fromAssistantResponse($assistantResponse), | ||
$this->client->assistants()->list()->data | ||
); | ||
|
||
$this->view->assign('assistants', $assistants); | ||
} | ||
|
||
public function editAction(string $assistantId):void | ||
{ | ||
$assistantResponse = $this->client->assistants()->retrieve($assistantId); | ||
$this->view->assign('assistant', AssistantRecord::fromAssistantResponse($assistantResponse)); | ||
} | ||
|
||
public function updateAction(AssistantRecord $assistant):void | ||
{ | ||
$this->client->assistants()->modify($assistant->id, ['name' => $assistant->name, 'description' => $assistant->description, 'instructions' => $assistant->instructions]); | ||
$this->addFlashMessage('Assistant ' . $assistant->name . ' was updated'); | ||
$this->redirect('index'); | ||
} | ||
|
||
public function newAction():void | ||
{ | ||
} | ||
|
||
public function createAction(string $name):void | ||
{ | ||
$assistantResponse = $this->client->assistants()->create(['name' => $name, 'model' => 'gpt-4-1106-preview']); | ||
$this->redirect('edit', null, null, ['assistantId' => $assistantResponse->id]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Sitegeist\Chatterbox\Domain; | ||
|
||
use OpenAI\Responses\Assistants\AssistantResponse; | ||
|
||
class AssistantRecord | ||
{ | ||
public function __construct( | ||
public readonly string $id, | ||
public readonly string $model, | ||
public readonly string $name, | ||
public readonly ?string $description, | ||
public readonly ?string $instructions, | ||
) { | ||
} | ||
|
||
public static function fromAssistantResponse(AssistantResponse $response): static | ||
{ | ||
return new static( | ||
$response->id, | ||
$response->model, | ||
$response->name, | ||
$response->description, | ||
$response->instructions | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
include: resource://Neos.Fusion/Private/Fusion/Root.fusion | ||
include: resource://Neos.Fusion.Form/Private/Fusion/Root.fusion | ||
include: **/*.fusion | ||
|
||
Sitegeist.Chatterbox.AssistantModuleController.index = Sitegeist.Chatterbox:Assistant.List | ||
Sitegeist.Chatterbox.AssistantModuleController.edit = Sitegeist.Chatterbox:Assistant.Edit | ||
Sitegeist.Chatterbox.AssistantModuleController.new = Sitegeist.Chatterbox:Assistant.New | ||
|
||
prototype(Sitegeist.Chatterbox:Assistant.List) < prototype(Neos.Fusion:Component) { | ||
renderer = afx` | ||
<legend>Available assistants</legend> | ||
<table class="neos-table"> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Description</th> | ||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<Neos.Fusion:Loop items={assistants} itemName="assistant"> | ||
<tr> | ||
<td>{assistant.name}</td> | ||
<td>{assistant.description}</td> | ||
<td class="neos-priority1 neos-aCenter"> | ||
<Neos.Fusion:Link.Action href.action="edit" href.arguments.assistantId={assistant.id}> | ||
<i class="fas fa-pencil-alt icon-white"></i> | ||
</Neos.Fusion:Link.Action> | ||
</td> | ||
</tr> | ||
</Neos.Fusion:Loop> | ||
</tbody> | ||
</table> | ||
|
||
<div class="neos-footer"> | ||
<Neos.Fusion:Link.Action href.action="new" class="neos-button" >Create new assistant</Neos.Fusion:Link.Action> | ||
</div> | ||
|
||
` | ||
} | ||
|
||
prototype(Sitegeist.Chatterbox:Assistant.New) < prototype(Neos.Fusion:Component) { | ||
renderer = afx` | ||
<legend>Create new assistant</legend> | ||
<Neos.Fusion.Form:Form | ||
form.target.action="create" | ||
> | ||
<div class="neos-control-group"> | ||
<label class="neos-control-label" >Name</label> | ||
<div class="neos-controls neos-controls-row"> | ||
<Neos.Fusion.Form:Input field.name="name" /> | ||
</div> | ||
</div> | ||
<div class="neos-footer"> | ||
<Neos.Fusion:Link.Action href.action="index" class="neos-button" >Back</Neos.Fusion:Link.Action> | ||
<Neos.Fusion.Form:Button attributes.class="neos-button neos-button-primary">Create</Neos.Fusion.Form:Button> | ||
</div> | ||
</Neos.Fusion.Form:Form> | ||
` | ||
} | ||
|
||
|
||
prototype(Sitegeist.Chatterbox:Assistant.Edit) < prototype(Neos.Fusion:Component) { | ||
renderer = afx` | ||
<legend>Edit assistant {assistant.name}</legend> | ||
<Neos.Fusion.Form:Form | ||
form.target.action="update" | ||
form.data.assistant={assistant} | ||
> | ||
<Neos.Fusion.Form:Hidden field.name="assistant[id]" /> | ||
<Neos.Fusion.Form:Hidden field.name="assistant[model]" /> | ||
|
||
<div class="neos-control-group"> | ||
<label class="neos-control-label" >Name</label> | ||
<div class="neos-controls neos-controls-row"> | ||
<Neos.Fusion.Form:Input attributes.class="neos-span12" field.name="assistant[name]" /> | ||
</div> | ||
</div> | ||
|
||
<div class="neos-control-group"> | ||
<label class="neos-control-label" >Description</label> | ||
<div class="neos-controls neos-controls-row"> | ||
<Neos.Fusion.Form:Textarea attributes.rows="5" attributes.class="neos-span12" field.name="assistant[description]" /> | ||
</div> | ||
</div> | ||
|
||
<div class="neos-control-group"> | ||
<label class="neos-control-label" >Instructions</label> | ||
<div class="neos-controls neos-controls-row"> | ||
<Neos.Fusion.Form:Textarea attributes.rows="20" attributes.class="neos-span12" field.name="assistant[instructions]" /> | ||
</div> | ||
</div> | ||
|
||
<div class="neos-footer"> | ||
<Neos.Fusion:Link.Action href.action="index" class="neos-button" >Back</Neos.Fusion:Link.Action> | ||
<Neos.Fusion.Form:Button attributes.class="neos-button neos-button-primary">Save</Neos.Fusion.Form:Button> | ||
</div> | ||
|
||
</Neos.Fusion.Form:Form> | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters