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

AI translations #4531

Merged
merged 18 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
43 changes: 31 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"sass-loader": "6.0.6",
"shortid": "2.2.8",
"style-loader": "0.20.2",
"superdesk-ui-framework": "^3.1.3",
"superdesk-ui-framework": "^3.1.9",
"ts-loader": "3.5.0",
"typescript": "4.9.5",
"uuid": "8.3.1",
Expand Down
2 changes: 2 additions & 0 deletions scripts/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {time} from './time';
import {user} from './user';
import {vocabularies} from './vocabularies';
import {contentProfiles} from './content-profiles';
import {translations} from './translations';

/**
* This is core API, not extensions API.
Expand All @@ -31,4 +32,5 @@ export const sdApi = {
vocabularies,
highlights,
contentProfiles,
translations,
};
29 changes: 29 additions & 0 deletions scripts/api/translations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {IArticle} from 'superdesk-api';
import ng from 'core/services/ng';
import {extensions} from 'appConfig';
import notify from 'core/notify/notify';
import {gettext} from 'core/utils';

function aiTranslationActions(item: IArticle): void {
tomaskikutis marked this conversation as resolved.
Show resolved Hide resolved
if (extensions['ai-widget'].extension.exposes?.overrideTranslations === true) {
localStorage.setItem(
'sideWidget',
tomaskikutis marked this conversation as resolved.
Show resolved Hide resolved
JSON.stringify({
id: 'ai-assistant',
pinned: true,
activeSection: 'translations',
}),
);
}

ng.get('authoringWorkspace').open(item);
notify.success(gettext('Item Translated'));
}

interface ITranslationsApi {
aiTranslationActions(item: IArticle): void;
}

export const translations: ITranslationsApi = {
aiTranslationActions,
};
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,11 @@ export class AuthoringIntegrationWrapper extends React.PureComponent<IPropsWrapp
constructor(props: IPropsWrapper) {
super(props);

const lsSideWidget = localStorage.getItem('sideWidget');

this.state = {
sidebarMode: this.props.sidebarMode === 'hidden' ? 'hidden' : (this.props.sidebarMode ?? false),
sideWidget: null,
sideWidget: lsSideWidget != null ? JSON.parse(lsSideWidget) : null,
};

this.prepareForUnmounting = this.prepareForUnmounting.bind(this);
Expand Down
7 changes: 5 additions & 2 deletions scripts/apps/authoring-react/toolbar/translate-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,18 @@ export class TranslateModal extends React.PureComponent<IProps, IState> {

translate() {
if (this.state.initialized) {
return httpRequestJsonLocal({
return httpRequestJsonLocal<IArticle>({
method: 'POST',
path: '/archive/translate',
payload: {
desk: sdApi.desks.getCurrentDeskId(),
language: this.state.selectedLanguage,
guid: this.props.article._id,
},
}).then(() => this.props.closeModal());
}).then((item) => {
tomaskikutis marked this conversation as resolved.
Show resolved Hide resolved
sdApi.translations.aiTranslationActions(item);
this.props.closeModal();
});
}
}

Expand Down
14 changes: 12 additions & 2 deletions scripts/apps/authoring/widgets/widgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,13 @@ function WidgetsManagerCtrl(
preferencesService,
$rootScope,
) {
$scope.active = null;
const lsSideWidget = localStorage.getItem('sideWidget');
tomaskikutis marked this conversation as resolved.
Show resolved Hide resolved
const localStorageWidgetState = lsSideWidget != null ? JSON.parse(lsSideWidget) : null;
const widgetValue = lsSideWidget == null
? null
: authoringWidgets.find((widget) => widget._id === localStorageWidgetState?.id);

$scope.active = widgetValue != null ? widgetValue : null;
tomaskikutis marked this conversation as resolved.
Show resolved Hide resolved

preferencesService.get(USER_PREFERENCE_SETTINGS).then((preferences) =>
this.widgetFromPreferences = preferences,
Expand Down Expand Up @@ -338,7 +344,7 @@ function WidgetsManagerCtrl(
widgetReactIntegration.pinWidget = $scope.pinWidget;
widgetReactIntegration.getActiveWidget = () => $scope.active ?? $scope.pinnedWidget;
widgetReactIntegration.getPinnedWidget =
() => $scope.widgets.find(({pinned}) => pinned === true)?.name ?? null;
() => $scope.widgets?.find(({pinned}) => pinned === true)?.name ?? null;
tomaskikutis marked this conversation as resolved.
Show resolved Hide resolved

widgetReactIntegration.WidgetHeaderComponent = WidgetHeaderComponent;
widgetReactIntegration.WidgetLayoutComponent = WidgetLayoutComponent;
Expand Down Expand Up @@ -392,6 +398,10 @@ function WidgetsManagerCtrl(
});
};

if (widgetValue?.component != null && localStorageWidgetState?.pinned === true) {
$scope.pinWidget(widgetValue);
}

$scope.$on('$destroy', () => {
unbindAllShortcuts();
});
Expand Down
5 changes: 2 additions & 3 deletions scripts/apps/translations/services/TranslationService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';
import {gettext} from 'core/utils';
import {AuthoringWorkspaceService} from 'apps/authoring/authoring/services/AuthoringWorkspaceService';
import {sdApi} from 'api';

/**
* @ngdoc service
Expand Down Expand Up @@ -64,9 +64,8 @@ export function TranslationService(
};

api.save('translate', params).then((_item) => {
authoringWorkspace.open(_item);
sdApi.translations.aiTranslationActions(_item);
$rootScope.$broadcast('item:translate');
notify.success(gettext('Item Translated'));
});
};

Expand Down
1 change: 0 additions & 1 deletion scripts/core/superdesk-api.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* tslint:disable */

tomaskikutis marked this conversation as resolved.
Show resolved Hide resolved
declare module 'superdesk-api' {
// TYPESCRIPT TYPES

Expand Down
63 changes: 58 additions & 5 deletions scripts/extensions/ai-widget/src/ai-assistant.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React from 'react';
import {IArticleSideWidgetComponentType} from 'superdesk-api';
import {IArticleSideWidgetComponentType, ITranslation} from 'superdesk-api';
import {Spacer} from 'superdesk-ui-framework/react';
import {superdesk} from './superdesk';
import {configuration} from './configuration';
import getHeadlinesWidget from './headlines/headlines-widget';
import getSummaryWidget from './summary/summary-widget';
import DefaultAiAssistantPanel from './main-panel';
import getTranslationsWidget from './translations/translations-widget';

export type IAiAssistantSection = 'headlines' | 'summary' | null;
export type IAiAssistantSection = 'headlines' | 'summary' | 'translations' | null;
export type ITranslationLanguage = ITranslation['_id'];

interface IState {
activeSection: IAiAssistantSection;
Expand All @@ -17,28 +19,44 @@ interface IState {
*/
loadingHeadlines: boolean;
loadingSummary: boolean;
loadingTranslations: boolean;

headlines: Array<string>;
error: boolean;
summary: string;
translations: string;
activeLanguageId: ITranslationLanguage;
programmaticallyOpened: boolean;
tomaskikutis marked this conversation as resolved.
Show resolved Hide resolved
}

export class AiAssistantWidget extends React.PureComponent<IArticleSideWidgetComponentType, IState> {
constructor(props: IArticleSideWidgetComponentType) {
super(props);

const lsSideWidget = localStorage.getItem('sideWidget');
const programmaticallyOpened = lsSideWidget != null
? JSON.parse(lsSideWidget).activeSection === 'translations'
: false;

this.state = {
activeSection: null,
activeSection: lsSideWidget != null ? JSON.parse(lsSideWidget).activeSection : null,
headlines: [],
error: false,
summary: '',
translations: '',
tomaskikutis marked this conversation as resolved.
Show resolved Hide resolved
loadingSummary: true,
loadingHeadlines: true,
summary: '',
loadingTranslations: programmaticallyOpened,
error: false,
activeLanguageId: this.props.article.language,
programmaticallyOpened: programmaticallyOpened,
};

localStorage.removeItem('sideWidget');
tomaskikutis marked this conversation as resolved.
Show resolved Hide resolved

this.setError = this.setError.bind(this);
this.generateHeadlines = this.generateHeadlines.bind(this);
this.generateSummary = this.generateSummary.bind(this);
this.generateTranslations = this.generateTranslations.bind(this);
}

setError() {
Expand All @@ -59,6 +77,18 @@ export class AiAssistantWidget extends React.PureComponent<IArticleSideWidgetCom
});
}

generateTranslations() {
configuration.generateTranslations?.(this.props.article, this.state.activeLanguageId, superdesk)
.then((res) => {
this.setState({
loadingTranslations: false,
translations: res,
});
}).catch(() => {
this.setError();
});
}

generateSummary() {
configuration.generateSummary?.(this.props.article, superdesk)
.then((res) => {
Expand Down Expand Up @@ -92,6 +122,27 @@ export class AiAssistantWidget extends React.PureComponent<IArticleSideWidgetCom
fieldsData: this.props.fieldsData,
onFieldsDataChange: this.props.onFieldsDataChange,
});
const translationsWidget = getTranslationsWidget({
closeActiveSection,
article: this.props.article,
error: this.state.error,
setActiveLanguage: (language) => {
this.setState({
activeLanguageId: language,
});
},
activeLanguageId: this.state.activeLanguageId,
programmaticallyOpened: this.state.programmaticallyOpened,
generateTranslations: () => {
this.setState({
loadingTranslations: true,
}, () => this.generateTranslations());
},
translations: this.state.translations,
loading: this.state.loadingTranslations,
fieldsData: this.props.fieldsData,
onFieldsDataChange: this.props.onFieldsDataChange,
});
const summaryWidget = getSummaryWidget({
closeActiveSection,
article: this.props.article,
Expand All @@ -114,6 +165,8 @@ export class AiAssistantWidget extends React.PureComponent<IArticleSideWidgetCom
return headlinesWidget;
} else if (this.state.activeSection === 'summary') {
return summaryWidget;
} else if (this.state.activeSection === 'translations') {
return translationsWidget;
} else {
return {
header: undefined,
Expand Down
Loading
Loading