-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: insert ai-generated text and images
Signed-off-by: Elizabeth Danzberger <lizzy7128@tutanota.de>
- Loading branch information
Showing
8 changed files
with
193 additions
and
6 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
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
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,34 @@ | ||
<?php | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Richdocuments; | ||
|
||
use OCP\TaskProcessing\IManager; | ||
|
||
class TaskProcessingManager { | ||
public const array SUPPORTED_TASK_TYPES = [ | ||
'core:text2text', | ||
'core:text2image', | ||
]; | ||
|
||
public function __construct( | ||
private IManager $taskProcessing, | ||
) { | ||
} | ||
|
||
public function isTaskProcessingEnabled(): bool { | ||
// Check if task processing should be considered enabled | ||
// if any of our supported task types are available | ||
$availableTaskTypes = array_intersect_key( | ||
$this->taskProcessing->getAvailableTaskTypes(), | ||
array_flip(self::SUPPORTED_TASK_TYPES) | ||
); | ||
|
||
return !empty($availableTaskTypes); | ||
} | ||
} |
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,76 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import { translate as t } from '@nextcloud/l10n' | ||
import { generateUrl } from '@nextcloud/router' | ||
import axios from '@nextcloud/axios' | ||
|
||
const SupportedTaskTypes = { | ||
Text: 'core:text2text', | ||
Image: 'core:text2image', | ||
} | ||
|
||
export default { | ||
data() { | ||
return { | ||
task: null, | ||
} | ||
}, | ||
methods: { | ||
async openAssistant() { | ||
this.task = await window.OCA.Assistant.openAssistantForm({ | ||
appId: 'richdocuments', | ||
customId: 'richdocuments:' + this.fileid, | ||
isInsideViewer: true, | ||
actionButtons: [ | ||
{ | ||
label: t('richdocuments', 'Insert into document'), | ||
title: t('richdocuments', 'Insert into document'), | ||
onClick: () => this.handleTask(this.task), | ||
}, | ||
], | ||
}) | ||
}, | ||
handleTask(task) { | ||
switch (task.type) { | ||
|
||
case SupportedTaskTypes.Text: | ||
this.insertAIText(task.output.output) | ||
break | ||
|
||
case SupportedTaskTypes.Image: | ||
this.insertAIImages(task.output.images) | ||
break | ||
|
||
default: | ||
break | ||
} | ||
}, | ||
insertAIText(text) { | ||
this.sendPostMessage('Action_Paste', { | ||
Mimetype: 'text/plain;charset=utf-8', | ||
Data: text, | ||
}) | ||
}, | ||
async insertAIImages(images) { | ||
const assets = await axios({ | ||
method: 'post', | ||
url: generateUrl('apps/richdocuments/assets/tasks'), | ||
data: { | ||
taskId: this.task.id, | ||
fileIds: [images[0]], | ||
}, | ||
}) | ||
|
||
// For now, we only insert the first generated image | ||
const firstImage = assets.data[0] | ||
|
||
this.sendPostMessage('Action_InsertGraphic', { | ||
filename: firstImage.filename, | ||
url: firstImage.url, | ||
}) | ||
}, | ||
}, | ||
} |
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