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

fix(Assistant): Migrate to new task processing API #6079

Merged
merged 2 commits into from
Jul 25, 2024
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
16 changes: 12 additions & 4 deletions cypress/e2e/Assistant.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('Assistant', () => {
cy.get('.action-item__popper ul').children().should(($children) => {
const entries = $children.find('button').map((i, el) => el.innerText).get()
expect(entries.length).to.be.greaterThan(0)
expect('Free prompt').to.be.oneOf(entries)
expect('Free text to text prompt').to.be.oneOf(entries)
expect('Translate').to.be.oneOf(entries)
expect('Show assistant results').to.be.oneOf(entries)
})
Expand All @@ -53,20 +53,28 @@ describe('Assistant', () => {
.click({ force: true })
cy.get('[data-cy="assistantMenu"]')
.click()
cy.get('.action-item__popper ul li').first()
cy.get('.action-item__popper li')
.contains('Free text to text prompt')
.click()

cy.get('.assistant-modal--content #input-prompt')
cy.get('.assistant-modal--content #input-input')
.should('be.visible')

cy.get('.assistant-modal--content #input-prompt')
cy.get('.assistant-modal--content #input-input')
.type('Hello World')
cy.get('.assistant-modal--content .submit-button')
.click()

// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(2000)

cy.get('.assistant-modal--content button')
.contains('Run in the background')
.click()

cy.get('.assistant-modal--content')
.should('contain', 'Your task has been scheduled')

cy.get('.assistant-modal--content .close-button')
.click()
cy.get('[data-cy="assistantMenu"]')
Expand Down
32 changes: 20 additions & 12 deletions lib/Service/InitialStateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@
namespace OCA\Text\Service;

use OCP\AppFramework\Services\IInitialState;
use OCP\TextProcessing\IManager;
use OCP\TextProcessing\ITaskType;
use OCP\TaskProcessing\IManager;
use OCP\Translation\ITranslationManager;

class InitialStateProvider {
private const ASSISTANT_TASK_TYPES = [
'core:text2text',
'core:text2text:formalization',
'core:text2text:headline',
'core:text2text:reformulation',
'core:text2text:simplification',
'core:text2text:summary',
'core:text2text:topics',
];

public function __construct(
private IInitialState $initialState,
private ConfigService $configService,
private ITranslationManager $translationManager,
private IManager $textProcessingManager,
private IManager $taskProcessingManager,
private ?string $userId
) {
}
Expand Down Expand Up @@ -53,16 +62,15 @@ public function provideState(): void {
$this->translationManager->getLanguages()
);

$filteredTypes = array_filter($this->taskProcessingManager->getAvailableTaskTypes(), static function (string $taskType) {
return in_array($taskType, self::ASSISTANT_TASK_TYPES, true);
}, ARRAY_FILTER_USE_KEY);
$this->initialState->provideInitialState(
'textprocessing',
array_map(function (string $className) {
/** @var class-string<ITaskType> $className */
$type = \OCP\Server::get($className);
return [
'task' => $className,
'name' => $type->getName(),
];
}, $this->textProcessingManager->getAvailableTaskTypes()),
'taskprocessing',
array_map(static function (string $typeId, array $taskType) {
$taskType['id'] = $typeId;
return $taskType;
}, array_keys($filteredTypes), array_values($filteredTypes)),
);

$this->initialState->provideInitialState(
Expand Down
76 changes: 39 additions & 37 deletions src/components/Assistant.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
<template #icon>
<CreationIcon :size="20" class="icon" />
</template>
<NcActionButton v-for="provider in providers"
:key="provider.task"
<NcActionButton v-for="type in taskTypes"
:key="type.id"
close-after-click
@click="openAssistantForm(provider.task)">
@click="openAssistantForm(type.id)">
<template #icon>
<PencilIcon v-if="provider.task == 'OCP\\TextProcessing\\FreePromptTaskType'" :size="20" />
<TextShort v-else-if="provider.task == 'OCP\\TextProcessing\\SummarizeTaskType'" :size="20" />
<FormatHeader1 v-else-if="provider.task == 'OCP\\TextProcessing\\HeadlineTaskType'" :size="20" />
<Shuffle v-else-if="provider.task == 'OCA\\OpenAi\\TextProcessing\\ReformulateTaskType'" :size="20" />
<PencilIcon v-if="type.id == 'core:text2text'" :size="20" />
<FormatHeader1 v-else-if="type.id == 'core:text2text:headline'" :size="20" />
<Shuffle v-else-if="type.id == 'core:text2text:reformulation'" :size="20" />
<TextShort v-else-if="type.id == 'core:text2text:summary'" :size="20" />
<CreationIcon v-else />
</template>
{{ provider.name }}
{{ type.name }}
</NcActionButton>
<NcActionButton data-cy="open-translate" close-after-click @click="openTranslateDialog">
<template #icon>
Expand Down Expand Up @@ -63,7 +63,7 @@
:force-display-actions="true"
@click="() => openResult(task)">
<template #subname>
{{ task.input }}
{{ task.input.input }}
</template>
<template #icon>
<CheckCircleIcon v-if="task.status === STATUS_SUCCESSFUL" :size="20" class="icon-status--success" />
Expand All @@ -72,7 +72,7 @@
</template>
<template #indicator>
<NcActions :inline="2">
<NcActionButton v-if="task.status === STATUS_SUCCESSFUL" :title="task.output" @click.stop="() => copyResult(task)">
<NcActionButton v-if="task.status === STATUS_SUCCESSFUL" :title="task.output.output" @click.stop="() => copyResult(task)">
<template #icon>
<ClipboardTextOutlineIcon :size="20" />
</template>
Expand All @@ -81,13 +81,13 @@
</NcActions>
</template>
<template #actions>
<NcActionButton v-if="task.status === STATUS_SUCCESSFUL" :title="task.output" @click.stop="() => openResult(task)">
<NcActionButton v-if="task.status === STATUS_SUCCESSFUL" :title="task.output.output" @click.stop="() => openResult(task)">
<template #icon>
<CreationIcon :size="20" />
</template>
{{ t('text', 'Show result') }}
</NcActionButton>
<NcActionButton v-if="task.status === STATUS_SUCCESSFUL" :title="task.output" @click="() => insertResult(task)">
<NcActionButton v-if="task.status === STATUS_SUCCESSFUL" :title="task.output.output" @click="() => insertResult(task)">
<template #icon>
<TextBoxPlusOutlineIcon :size="20" />
</template>
Expand Down Expand Up @@ -138,11 +138,12 @@ const limitInRange = (num, min, max) => {
return Math.min(Math.max(parseInt(num), parseInt(min)), parseInt(max))
}

const STATUS_FAILED = 4
const STATUS_SUCCESSFUL = 3
const STATUS_RUNNING = 2
const STATUS_SCHEDULED = 1
const STATUS_UNKNOWN = 0
// https://github.com/nextcloud/server/blob/master/lib/public/TaskProcessing/Task.php#L366-L371
const STATUS_FAILED = 'STATUS_FAILED'
const STATUS_SUCCESSFUL = 'STATUS_SUCCESSFUL'
const STATUS_RUNNING = 'STATUS_RUNNING'
const STATUS_SCHEDULED = 'STATUS_SCHEDULED'
const STATUS_UNKNOWN = 'STATUS_UNKNOWN'

export default {
name: 'Assistant',
Expand Down Expand Up @@ -174,7 +175,7 @@ export default {
],
data() {
return {
providers: OCP.InitialState.loadState('text', 'textprocessing'),
taskTypes: OCP.InitialState.loadState('text', 'taskprocessing'),
selection: '',
tasks: [],

Expand All @@ -190,7 +191,7 @@ export default {
},
computed: {
showAssistant() {
return !this.$isRichWorkspace && !this.$isPublic && window?.OCA?.TPAssistant?.openAssistantForm
return !this.$isRichWorkspace && !this.$isPublic && window.OCA.Assistant?.openAssistantForm
},
identifier() {
return 'text-file:' + this.$file.fileId
Expand All @@ -210,6 +211,9 @@ export default {

return null
},
taskTypeIds() {
return this.taskTypes.map((type) => type.id)
},
},
beforeMount() {
if (!this.showAssistant) {
Expand All @@ -230,18 +234,13 @@ export default {
},
methods: {
async fetchTasks() {
const result = await axios.get(generateOcsUrl('/textprocessing/tasks/app/text') + '?identifier=' + this.identifier)

const taskMap = {}
for (const index in this.providers) {
const provider = this.providers[index]
taskMap[provider.task] = provider
}
const result = await axios.get(generateOcsUrl('/taskprocessing/tasks/app/text') + '?customId=' + this.identifier)

this.tasks = result.data.ocs.data.tasks.map((task) => {
const filteredTasks = result.data.ocs.data.tasks.filter(t => this.taskTypeIds.includes(t.type))
this.tasks = filteredTasks.map((task) => {
return {
...task,
title: taskMap[task.type].name,
title: this.taskTypes.find(t => t.id === task.type).name,
}
}).sort((a, b) => b.id - a.id)
},
Expand All @@ -257,12 +256,14 @@ export default {
this.selection = state.doc.textBetween(from, to, ' ')
},
async openAssistantForm(taskType = null) {
await window.OCA.TPAssistant.openAssistantForm(
await window.OCA.Assistant.openAssistantForm(
{
appId: 'text',
identifier: this.identifier,
customId: this.identifier,
taskType,
input: this.selection,
inputs: {
input: this.selection,
},
isInsideViewer: true,
closeOnResult: false,
actionButtons: [
Expand All @@ -275,8 +276,9 @@ export default {
},
},
],
})
await this.fetchTasks()
}).finally(() => {
this.fetchTasks()
})
},
openTranslateDialog() {
if (!this.selection.trim().length) {
Expand All @@ -285,15 +287,15 @@ export default {
emit('text:translate-modal:show', { content: this.selection || '' })
},
async openResult(task) {
window.OCA?.TPAssistant.openAssistantResult(task)
window.OCA.Assistant.openAssistantTask(task)
},
async insertResult(task) {
this.$editor.commands.insertContent(task.output)
this.$editor.commands.insertContent(task.output.output)
this.showTaskList = false
},
async copyResult(task) {
try {
await navigator.clipboard.writeText(task.output)
await navigator.clipboard.writeText(task.output.output)
showSuccess(t('text', 'Nextcloud Assistant result copied'))
this.showTaskList = false
} catch (error) {
Expand All @@ -303,7 +305,7 @@ export default {
},
async deleteTask(task) {
try {
await axios.delete(generateOcsUrl(`/textprocessing/task/${task.id}`))
await axios.delete(generateOcsUrl(`/taskprocessing/task/${task.id}`))
} catch (e) {
console.error('Failed to delete task', e)
}
Expand Down
Loading