Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions lib/Controller/ChattyLLMController.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private function improveAgencyActionNames(array $actions): array {
*
* Create a new chat session, add a system message with user instructions
*
* @param int $timestamp The session creation date
* @param ?int $timestamp The session creation date
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know this was valid docblock syntax

* @param ?string $title The session title
* @return JSONResponse<Http::STATUS_OK, array{session: AssistantChatSession}, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED, array{error: string}, array{}>
* @throws AppConfigTypeConflictException
Expand All @@ -209,7 +209,7 @@ private function improveAgencyActionNames(array $actions): array {
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function newSession(int $timestamp, ?string $title = null): JSONResponse {
public function newSession(?int $timestamp = null, ?string $title = null): JSONResponse {
try {
$session = $this->chatService->createChatSession($this->userId, $timestamp, $title);
return new JSONResponse([
Expand Down Expand Up @@ -342,7 +342,7 @@ public function getSessions(): JSONResponse {
* @param int $sessionId The chat session ID
* @param string $role Role of the message (human, assistant etc...)
* @param string $content Content of the message
* @param int $timestamp Date of the message
* @param ?int $timestamp Date of the message
* @param ?list<array{type: string, file_id: int}> $attachments List of attachment objects
* @param bool $firstHumanMessage Is it the first human message of the session?
* @return JSONResponse<Http::STATUS_OK, AssistantChatMessage, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED|Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
Expand All @@ -355,7 +355,7 @@ public function getSessions(): JSONResponse {
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function newMessage(
int $sessionId, string $role, string $content, int $timestamp, ?array $attachments = null, bool $firstHumanMessage = false,
int $sessionId, string $role, string $content, ?int $timestamp = null, ?array $attachments = null, bool $firstHumanMessage = false,
): JSONResponse {
try {
$message = $this->chatService->createMessage($this->userId, $sessionId, $role, $content, $timestamp, $attachments, $firstHumanMessage);
Expand Down
5 changes: 2 additions & 3 deletions lib/Service/AssignmentsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function createAssignment(?string $userId, string $title, string $prompt,
} catch (Exception $e) {
throw new InternalException(previous: $e);
}
$session = $this->chatService->createChatSession($userId, $this->timeFactory->now()->getTimestamp(), $title);
$session = $this->chatService->createChatSession($userId, title: $title);
$session->setAssignmentId($assignment->getId());
try {
$this->sessionMapper->update($session);
Expand Down Expand Up @@ -121,7 +121,7 @@ public function scheduleAssignmentRun(?string $userId, int $assignmentId): void
$assignment = $this->assignmentMapper->find($userId, $assignmentId);
$assignment->setLastRunAt($this->timeFactory->now()->getTimestamp());
$this->assignmentMapper->update($assignment);
$this->chatService->createMessage($userId, $session->getId(), Message::ROLE_HUMAN, $assignment->getPrompt(), $this->timeFactory->now()->getTimestamp());
$this->chatService->createMessage($userId, $session->getId(), Message::ROLE_HUMAN, $assignment->getPrompt());
$this->chatService->scheduleAssignmentMessageGeneration($userId, $session->getId());
} catch (BadRequestException|InternalException|DoesNotExistException|MultipleObjectsReturnedException|Exception $e) {
$this->logger->error('Error while running assignment ' . $assignmentId . ' for user ' . $userId, ['exception' => $e]);
Expand All @@ -132,7 +132,6 @@ public function scheduleAssignmentRun(?string $userId, int $assignmentId): void
$session->getId(),
Message::ROLE_ASSISTANT,
$this->l10n->t('An error occurred while scheduling this assignment run. Reach out to your system administrator if this issue persists.'),
$this->timeFactory->now()->getTimestamp()
);
} catch (BadRequestException|InternalException|NotFoundException|UnauthorizedException $e) {
$this->logger->error('Error while creating error message for assignment ' . $assignmentId . ' for user ' . $userId, ['exception' => $e]);
Expand Down
6 changes: 5 additions & 1 deletion lib/Service/ChatService.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function getSessionsForUser(?string $userId): array {
* @throws NotFoundException
* @throws UnauthorizedException
*/
public function createMessage(?string $userId, int $sessionId, string $role, string $content, int $timestamp, ?array $attachments = null, bool $firstHumanMessage = false): Message {
public function createMessage(?string $userId, int $sessionId, string $role, string $content, ?int $timestamp = null, ?array $attachments = null, bool $firstHumanMessage = false): Message {
if ($userId === null) {
throw new UnauthorizedException($this->l10n->t('Unauthorized'));
}
Expand All @@ -183,6 +183,10 @@ public function createMessage(?string $userId, int $sessionId, string $role, str
throw new BadRequestException($this->l10n->t('The new message is too long'));
}

if ($timestamp === null) {
$timestamp = $this->timeFactory->now()->getTimestamp();
}

if ($timestamp > 10_000_000_000) {
$timestamp = intdiv($timestamp, 1000);
}
Expand Down
19 changes: 9 additions & 10 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2760,18 +2760,17 @@
}
],
"requestBody": {
"required": true,
"required": false,
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"timestamp"
],
"properties": {
"timestamp": {
"type": "integer",
"format": "int64",
"nullable": true,
"default": null,
"description": "The session creation date"
},
"title": {
Expand Down Expand Up @@ -3271,18 +3270,17 @@
}
],
"requestBody": {
"required": true,
"required": false,
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"timestamp"
],
"properties": {
"timestamp": {
"type": "integer",
"format": "int64",
"nullable": true,
"default": null,
"description": "The session creation date"
},
"title": {
Expand Down Expand Up @@ -3680,8 +3678,7 @@
"required": [
"sessionId",
"role",
"content",
"timestamp"
"content"
],
"properties": {
"sessionId": {
Expand All @@ -3700,6 +3697,8 @@
"timestamp": {
"type": "integer",
"format": "int64",
"nullable": true,
"default": null,
"description": "Date of the message"
},
"attachments": {
Expand Down
Loading