Skip to content

Commit

Permalink
feat: handle assistant sync task
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Trovic <[email protected]>
  • Loading branch information
luka-nextcloud committed Jan 24, 2024
1 parent 7b8b3de commit cd4d726
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ jobs:
ref: ${{ matrix.server-versions }}
path: apps/viewer

- name: Checkout assistant
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
with:
repository: nextcloud/assistant
ref: 'main'
path: apps/assistant

- name: Checkout app
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
with:
Expand Down
73 changes: 73 additions & 0 deletions cypress/e2e/Assistant.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { initUserAndFiles, randUser } from '../utils/index.js'

const currentUser = randUser()

const fileName = 'empty.md'

describe('Assistant', () => {
before(() => {
initUserAndFiles(currentUser, fileName)
})

beforeEach(() => {
cy.login(currentUser)
cy.visit('/apps/files')
})

it('See assistant button', () => {
cy.isolateTest({
sourceFile: fileName,
})
cy.openFile(fileName, { force: true })

cy.getContent()
.click({ force: true })

cy.get('.floating-menu')
.should('be.visible')

cy.get('.floating-menu')
.click()

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('Translate').to.be.oneOf(entries)
expect('Show assistant results').to.be.oneOf(entries)
})
})

it('Send free prompt request', () => {
cy.isolateTest({
sourceFile: fileName,
})
cy.openFile(fileName, { force: true })

cy.getContent()
.click({ force: true })
cy.get('.floating-menu')
.click()
cy.get('.action-item__popper ul li').first()
.click()

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

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

cy.get('.assistant-modal--content .close-button')
.click()
cy.get('.floating-menu')
.click()
cy.get('.action-item__popper ul li').last()
.click()

cy.get('.modal-container__content .task-list')
.should('be.visible')
.should('contain', 'Hello World')
})
})
4 changes: 3 additions & 1 deletion cypress/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ if [ $? -eq 0 ]; then
php /var/www/html/occ user:add --password-from-env user2
php /var/www/html/occ app:enable viewer
php /var/www/html/occ app:enable text
php /var/www/html/occ app:enable assistant
php /var/www/html/occ app:enable testing
php /var/www/html/occ app:list
"
fi

exec $@
exec $@
2 changes: 1 addition & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
use OCA\Text\Middleware\SessionMiddleware;
use OCA\Text\Notification\Notifier;
use OCA\Text\Service\ConfigService;
use OCA\TPAssistant\Event\BeforeAssistantNotificationEvent;
use OCA\TpAssistant\Event\BeforeAssistantNotificationEvent;
use OCA\Viewer\Event\LoadViewer;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
Expand Down
2 changes: 1 addition & 1 deletion lib/Listeners/BeforeAssistantNotificationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace OCA\Text\Listeners;

use OCA\Text\AppInfo\Application;
use OCA\TPAssistant\Event\BeforeAssistantNotificationEvent;
use OCA\TpAssistant\Event\BeforeAssistantNotificationEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IURLGenerator;
Expand Down
44 changes: 37 additions & 7 deletions src/components/Assistant.vue
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ import {
} from './Editor.provider.js'
import { FloatingMenu } from '@tiptap/vue-2'
import Translate from './Modal/Translate.vue'
import { subscribe, unsubscribe } from '@nextcloud/event-bus'
const limitInRange = (num, min, max) => {
return Math.min(Math.max(parseInt(num), parseInt(min)), parseInt(max))
Expand Down Expand Up @@ -213,7 +214,7 @@ export default {
},
computed: {
showAssistant() {
return !this.$isRichWorkspace && !this.$isPublic && window?.OCA?.TPAssistant?.openAssistantForm
return !this.$isRichWorkspace && !this.$isPublic && window?.OCA?.TpAssistant?.openAssistantForm
},
identifier() {
return 'text-file:' + this.$file.fileId
Expand Down Expand Up @@ -241,15 +242,15 @@ export default {
this.$editor.on('selectionUpdate', this.onSelection)
this.fetchTasks()
this.$interval = setInterval(() => this.fetchTasks(), 60 * 1000)
subscribe('notifications:notification:received', this.checkNotification)
},
beforeDestroy() {
if (!this.showAssistant) {
return
}
this.$editor.off('selectionUpdate', this.onSelection)
clearInterval(this.$interval)
unsubscribe('notifications:notification:received', this.checkNotification)
},
methods: {
async fetchTasks() {
Expand All @@ -268,19 +269,36 @@ export default {
}
}).sort((a, b) => b.id - a.id)
},
async checkNotification(event) {
if (event.notification.app !== 'assistant' || event.notification.actions[0].type !== 'WEB') {
return
}
await this.fetchTasks()
},
onSelection() {
const { state } = this.$editor
const { from, to } = state.selection
this.selection = state.doc.textBetween(from, to, ' ')
},
async openAssistantForm(taskType = null) {
await window.OCA.TPAssistant.openAssistantForm(
await window.OCA.TpAssistant.openAssistantForm(
{
appId: 'text',
identifier: this.identifier,
taskType,
input: this.selection,
isInsideViewer: true,
closeOnResult: false,
actionButtons: [
{
type: 'primary',
title: t('text', 'Insert result'),
label: t('text', 'Insert result'),
onClick: (lastTask) => {
this.insertResult(lastTask)
},
},
],
})
await this.fetchTasks()
},
Expand Down Expand Up @@ -308,7 +326,7 @@ export default {
this.displayTranslate = false
},
async openResult(task) {
window.OCA?.TPAssistant.openAssistantResult(task)
window.OCA?.TpAssistant.openAssistantResult(task)
},
async insertResult(task) {
this.$editor.commands.insertContent(task.output)
Expand Down Expand Up @@ -344,11 +362,23 @@ export default {
getReferenceClientRect: () => {
const editorRect = this.$parent.$el.querySelector('.ProseMirror').getBoundingClientRect()
const pos = posToDOMRect(this.$editor.view, this.$editor.state.selection.from, this.$editor.state.selection.to)
let rightSpacing = 0
let addTopSpacing = 0
if (editorRect.width < 670) {
rightSpacing = 20
}
if (editorRect.width < 425) {
rightSpacing = 66
addTopSpacing = 30
}
return {
...pos,
width: editorRect.width,
width: editorRect.width - rightSpacing,
height: limitInRange(pos.height, buttonSize, window.innerHeight),
top: limitInRange(pos.top, topSpacing, window.innerHeight - bottomSpacing),
top: limitInRange(pos.top, topSpacing, window.innerHeight - bottomSpacing) + addTopSpacing,
left: editorRect.left,
right: editorRect.right,
bottom: limitInRange(pos.top + buttonSize, bottomSpacing, window.innerHeight - topSpacing) + 22,
Expand Down
2 changes: 1 addition & 1 deletion tests/stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract public function getShare(): \OCP\Share\IShare;
}
}

namespace OCA\TPAssistant\Event {
namespace OCA\TpAssistant\Event {

use OCP\TextProcessing\Task;

Expand Down

0 comments on commit cd4d726

Please sign in to comment.