@datagrok/hit-triage 1.10.5 → 1.10.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +10 -0
- package/CHANGELOG.md +10 -0
- package/LICENSE.md +16 -0
- package/README_OLD.md +3 -3
- package/dist/package-test.js +34 -1
- package/dist/package-test.js.map +1 -1
- package/dist/package.js +430 -1
- package/dist/package.js.map +1 -1
- package/package.json +16 -42
- package/src/app/accordeons/new-hit-design-campaign-accordeon.ts +8 -2
- package/src/app/accordeons/new-hit-design-template-accordeon.ts +4 -1
- package/src/app/ai/view-functions.ts +549 -0
- package/src/app/base-view.ts +14 -2
- package/src/app/dialogs/functions-dialog.ts +9 -0
- package/src/app/hit-app-base.ts +1 -0
- package/src/app/hit-design-app.ts +66 -2
- package/src/app/hit-design-views/info-view.ts +4 -3
- package/src/app/hit-design-views/submit-view.ts +13 -4
- package/src/app/hit-triage-app.ts +8 -1
- package/src/app/hit-triage-views/info-view.ts +4 -3
- package/src/app/hit-triage-views/submit-view.ts +6 -4
- package/src/package-api.ts +29 -14
- package/src/package.g.ts +5 -0
- package/src/package.ts +8 -0
- package/src/packageSettingsEditor.ts +5 -11
- package/tsconfig.json +4 -69
- package/webpack.config.js +3 -46
- package/.eslintignore +0 -1
- package/.eslintrc.json +0 -45
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
/* eslint-disable max-len */
|
|
2
|
+
/**
|
|
3
|
+
* AI view functions for the Hit apps (Hit Triage, Hit Design, PeptiHit, PepTriage).
|
|
4
|
+
*
|
|
5
|
+
* The vocabulary is registered ONCE per session through `grok.functions.register`
|
|
6
|
+
* (real platform Funcs, no package.ts entries) and reported through `getFunctions()`
|
|
7
|
+
* overrides: the info (landing) views return the info set, and the campaign table
|
|
8
|
+
* views get an instance `getFunctions` override returning only the campaign set plus
|
|
9
|
+
* a bridge (searchHitTableFunctions / callHitTableFunction) to the table view's own
|
|
10
|
+
* standard functions. Every function takes the generic `view` argument the AI
|
|
11
|
+
* assistant injects and resolves the owning app from it at call time.
|
|
12
|
+
*/
|
|
13
|
+
import * as grok from 'datagrok-api/grok';
|
|
14
|
+
import * as DG from 'datagrok-api/dg';
|
|
15
|
+
import {HitAppBase} from '../hit-app-base';
|
|
16
|
+
import {HitDesignApp} from '../hit-design-app';
|
|
17
|
+
import {_package} from '../../package';
|
|
18
|
+
import {AppName, HitDesignCampaign, HitDesignTemplate, HitTriageCampaign, HitTriageTemplate} from '../types';
|
|
19
|
+
import {TileCategoriesColName, ViDColName} from '../consts';
|
|
20
|
+
import {checkEditPermissions, loadTemplate} from '../utils';
|
|
21
|
+
import {registerMol} from '../utils/molreg';
|
|
22
|
+
import {createNewHitDesignDataFrame} from '../accordeons/new-hit-design-campaign-accordeon';
|
|
23
|
+
|
|
24
|
+
type AnyCampaign = HitTriageCampaign | HitDesignCampaign;
|
|
25
|
+
type AnyTemplate = HitTriageTemplate | HitDesignTemplate;
|
|
26
|
+
|
|
27
|
+
/** The members of {@link HitDesignApp} / HitTriageApp the functions below rely on
|
|
28
|
+
* (they live on the concrete apps, not on {@link HitAppBase}). */
|
|
29
|
+
interface IHitApp {
|
|
30
|
+
campaign?: AnyCampaign;
|
|
31
|
+
campaignId?: string;
|
|
32
|
+
molColName?: string;
|
|
33
|
+
infoView: IHitInfoView;
|
|
34
|
+
saveCampaign(...args: unknown[]): Promise<AnyCampaign>;
|
|
35
|
+
submitParams?: {fName: string, package: string};
|
|
36
|
+
submitView?: {submit(): Promise<boolean>, submitWithStatus?(status?: string): Promise<boolean>};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface IHitInfoView {
|
|
40
|
+
setCampaign(name: string): Promise<unknown>;
|
|
41
|
+
deleteCampaignAndRefresh(appName: AppName, campaignId: string): Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type HitApp = HitAppBase<AnyTemplate> & IHitApp;
|
|
45
|
+
|
|
46
|
+
const AI_NAMESPACE = 'HitTriageAI';
|
|
47
|
+
const DEFAULT_SEARCH_LIMIT = 10;
|
|
48
|
+
|
|
49
|
+
const _funcs = new Map<string, DG.Func>();
|
|
50
|
+
|
|
51
|
+
/** Registers one vocabulary Func on first use; later calls return the cached one. */
|
|
52
|
+
function aiFunc(name: string, signature: string, description: string, run: (...args: any[]) => any): DG.Func {
|
|
53
|
+
let f = _funcs.get(name);
|
|
54
|
+
if (!f) {
|
|
55
|
+
f = grok.functions.register({signature, run, isAsync: true, namespace: AI_NAMESPACE,
|
|
56
|
+
tags: 'hit-ai-function', options: {description}});
|
|
57
|
+
_funcs.set(name, f);
|
|
58
|
+
}
|
|
59
|
+
return f;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Campaign table views carry a back reference to their app. */
|
|
63
|
+
const appByView = new WeakMap<object, HitAppBase<AnyTemplate>>();
|
|
64
|
+
|
|
65
|
+
/** Resolves the owning app from the AI-injected view argument: the campaign table
|
|
66
|
+
* view (back reference), an info/submit view (its `.app`), or the app's MultiView
|
|
67
|
+
* (its current view's `.app`). */
|
|
68
|
+
function appOf(view: any): HitApp {
|
|
69
|
+
const v = view?.jsView ?? view;
|
|
70
|
+
for (const c of [v, view, v?.currentView]) {
|
|
71
|
+
if (c == null)
|
|
72
|
+
continue;
|
|
73
|
+
const byRef = appByView.get(c);
|
|
74
|
+
if (byRef instanceof HitAppBase)
|
|
75
|
+
return byRef as HitApp;
|
|
76
|
+
if (c.app instanceof HitAppBase)
|
|
77
|
+
return c.app as HitApp;
|
|
78
|
+
if (c.currentView?.app instanceof HitAppBase)
|
|
79
|
+
return c.currentView.app as HitApp;
|
|
80
|
+
}
|
|
81
|
+
throw new Error('The current view does not belong to a Hit Triage / Hit Design app');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function designAppOf(view: any): HitDesignApp & IHitApp {
|
|
85
|
+
const app = appOf(view);
|
|
86
|
+
if (!(app instanceof HitDesignApp))
|
|
87
|
+
throw new Error('This action applies only to Hit Design / PeptiHit campaigns');
|
|
88
|
+
return app as HitDesignApp & IHitApp;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The open campaign table view: the injected view itself, or the app's current one. */
|
|
92
|
+
function campaignTableView(view: any): DG.TableView {
|
|
93
|
+
const v = view?.jsView ?? view;
|
|
94
|
+
if (v instanceof DG.TableView)
|
|
95
|
+
return v;
|
|
96
|
+
const app = appOf(view) as any;
|
|
97
|
+
const tv = app._designView ?? app._pickView;
|
|
98
|
+
if (tv instanceof DG.TableView)
|
|
99
|
+
return tv;
|
|
100
|
+
throw new Error('No campaign table view is open');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function campaignBrief(c: AnyCampaign) {
|
|
104
|
+
return {
|
|
105
|
+
code: c.name,
|
|
106
|
+
...(c.friendlyName && c.friendlyName !== c.name ? {name: c.friendlyName} : {}),
|
|
107
|
+
template: c.templateName,
|
|
108
|
+
status: c.status ?? null,
|
|
109
|
+
...(c.authorUserFriendlyName ? {author: c.authorUserFriendlyName} : {}),
|
|
110
|
+
...(c.createDate ? {created: c.createDate} : {}),
|
|
111
|
+
...(c.rowCount != null ? {rows: c.rowCount} : {}),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function computeBrief(t?: AnyTemplate) {
|
|
116
|
+
const compute = t?.compute;
|
|
117
|
+
if (!compute)
|
|
118
|
+
return null;
|
|
119
|
+
return {
|
|
120
|
+
descriptors: compute.descriptors?.enabled ? compute.descriptors.args : [],
|
|
121
|
+
functions: (compute.functions ?? []).map((f) => `${f.package}:${f.name}`),
|
|
122
|
+
scripts: (compute.scripts ?? []).map((s) => s.name),
|
|
123
|
+
queries: (compute.queries ?? []).map((q) => q.name),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function canEditCampaign(c: AnyCampaign): Promise<boolean> {
|
|
128
|
+
return !c.authorUserId || !c.permissions || await checkEditPermissions(c.authorUserId, c.permissions);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function findCampaign(app: HitApp, campaignCode: string): Promise<AnyCampaign | null> {
|
|
132
|
+
const campaigns = await _package.loadCampaigns(app.appName, []);
|
|
133
|
+
return campaigns[campaignCode] ??
|
|
134
|
+
Object.values(campaigns).find((c) => (c.friendlyName ?? '').toLowerCase() === campaignCode.toLowerCase()) ?? null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function listTemplateNames(appName: AppName): Promise<string[]> {
|
|
138
|
+
return (await _package.files.list(`${appName}/templates`))
|
|
139
|
+
.filter((f) => f.name.endsWith('.json'))
|
|
140
|
+
.map((f) => f.name.slice(0, -5));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Reduce a table-function invocation result to something JSON-serializable. */
|
|
144
|
+
function serializeResult(value: any): any {
|
|
145
|
+
if (value == null || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean')
|
|
146
|
+
return value;
|
|
147
|
+
if (value instanceof DG.DataFrame)
|
|
148
|
+
return {type: 'dataframe', name: value.name, rowCount: value.rowCount, columns: value.columns.names()};
|
|
149
|
+
if (value instanceof DG.Column)
|
|
150
|
+
return {type: 'column', name: value.name, length: value.length};
|
|
151
|
+
if (value instanceof DG.ViewBase)
|
|
152
|
+
return {type: 'view', name: value.name};
|
|
153
|
+
try {
|
|
154
|
+
return JSON.parse(JSON.stringify(value));
|
|
155
|
+
} catch (_) {
|
|
156
|
+
return String(value);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// ─────────────────────── the info (landing) view set ───────────────────────
|
|
161
|
+
|
|
162
|
+
export function hitInfoViewFunctions(): DG.Func[] {
|
|
163
|
+
return [
|
|
164
|
+
aiFunc('getHitAppInfo', 'dynamic getHitAppInfo(view view)',
|
|
165
|
+
'Overview of this Hit app - its name, saved campaign and template counts, the campaign currently open. Call this first',
|
|
166
|
+
async (view: any) => {
|
|
167
|
+
const app = appOf(view);
|
|
168
|
+
const campaigns = await _package.loadCampaigns(app.appName, []);
|
|
169
|
+
return {
|
|
170
|
+
app: app.appName,
|
|
171
|
+
campaigns: Object.keys(campaigns).length,
|
|
172
|
+
templates: await listTemplateNames(app.appName),
|
|
173
|
+
currentCampaign: app.campaignId ?? app.campaign?.name ?? null,
|
|
174
|
+
canCreateCampaignsHere: app instanceof HitDesignApp,
|
|
175
|
+
};
|
|
176
|
+
}),
|
|
177
|
+
|
|
178
|
+
aiFunc('listHitCampaigns', 'dynamic listHitCampaigns(view view)',
|
|
179
|
+
'List the saved campaigns of this Hit app with code, name, template, status, author and row count. Campaigns are addressed by code',
|
|
180
|
+
async (view: any) => {
|
|
181
|
+
const app = appOf(view);
|
|
182
|
+
const campaigns = Object.values(await _package.loadCampaigns(app.appName, []));
|
|
183
|
+
return {total: campaigns.length, campaigns: campaigns.map(campaignBrief)};
|
|
184
|
+
}),
|
|
185
|
+
|
|
186
|
+
aiFunc('getHitCampaignDetails', 'dynamic getHitCampaignDetails(view view, string campaignCode)',
|
|
187
|
+
'Full detail of one saved campaign - fields, row counts, compute methods, stages, whether the current user can edit it',
|
|
188
|
+
async (view: any, campaignCode: string) => {
|
|
189
|
+
const app = appOf(view);
|
|
190
|
+
const c = await findCampaign(app, campaignCode);
|
|
191
|
+
if (!c)
|
|
192
|
+
return {success: false, error: `No campaign '${campaignCode}' — call listHitCampaigns for codes`};
|
|
193
|
+
return {
|
|
194
|
+
...campaignBrief(c),
|
|
195
|
+
...(c.campaignFields && Object.keys(c.campaignFields).length ? {campaignFields: c.campaignFields} : {}),
|
|
196
|
+
...(c.filteredRowCount != null ? {filteredRows: c.filteredRowCount} : {}),
|
|
197
|
+
...(c.lastModifiedUserName ? {lastModifiedBy: c.lastModifiedUserName} : {}),
|
|
198
|
+
...((c.template as AnyTemplate & {stages?: string[]})?.stages?.length ?
|
|
199
|
+
{stages: (c.template as AnyTemplate & {stages?: string[]}).stages} : {}),
|
|
200
|
+
compute: computeBrief(c.template),
|
|
201
|
+
canEdit: await canEditCampaign(c),
|
|
202
|
+
};
|
|
203
|
+
}),
|
|
204
|
+
|
|
205
|
+
aiFunc('openHitCampaign', 'dynamic openHitCampaign(view view, string campaignCode)',
|
|
206
|
+
'Open a saved campaign so the user can work on it - loads its table and shows the campaign view',
|
|
207
|
+
async (view: any, campaignCode: string) => {
|
|
208
|
+
const app = appOf(view);
|
|
209
|
+
const c = await findCampaign(app, campaignCode);
|
|
210
|
+
if (!c)
|
|
211
|
+
return {success: false, error: `No campaign '${campaignCode}' — call listHitCampaigns for codes`};
|
|
212
|
+
await app.infoView.setCampaign(c.name);
|
|
213
|
+
if (app.campaignId !== c.name)
|
|
214
|
+
return {success: false, error: 'Failed to open the campaign — you may not have permission to view it'};
|
|
215
|
+
return {success: true, campaign: c.name, note: 'The campaign view is now open'};
|
|
216
|
+
}),
|
|
217
|
+
|
|
218
|
+
aiFunc('listHitTemplates', 'dynamic listHitTemplates(view view)',
|
|
219
|
+
'List the campaign template names of this Hit app',
|
|
220
|
+
async (view: any) => ({templates: await listTemplateNames(appOf(view).appName)})),
|
|
221
|
+
|
|
222
|
+
aiFunc('getHitTemplateDetails', 'dynamic getHitTemplateDetails(view view, string templateName)',
|
|
223
|
+
'Full detail of one campaign template - its campaign fields, stages, compute methods, data source and submit function',
|
|
224
|
+
async (view: any, templateName: string) => {
|
|
225
|
+
const app = appOf(view);
|
|
226
|
+
const path = `${app.appName}/templates/${templateName}.json`;
|
|
227
|
+
if (!await _package.files.exists(path))
|
|
228
|
+
return {success: false, error: `No template '${templateName}' — call listHitTemplates for names`};
|
|
229
|
+
const t = await loadTemplate<AnyTemplate>(path);
|
|
230
|
+
const asDesign = t as HitDesignTemplate;
|
|
231
|
+
const asTriage = t as HitTriageTemplate;
|
|
232
|
+
return {
|
|
233
|
+
name: t.name,
|
|
234
|
+
key: (t as {key?: string}).key,
|
|
235
|
+
campaignFields: (t.campaignFields ?? []).map((f) => ({name: f.name, type: f.type, required: !!f.required})),
|
|
236
|
+
...(asDesign.stages?.length ? {stages: asDesign.stages} : {}),
|
|
237
|
+
...(asTriage.dataSourceType ? {dataSourceType: asTriage.dataSourceType} : {}),
|
|
238
|
+
...(asTriage.queryFunctionName ? {dataSourceFunction: asTriage.queryFunctionName} : {}),
|
|
239
|
+
compute: computeBrief(t),
|
|
240
|
+
...(t.submit ? {submitFunction: `${t.submit.package}:${t.submit.fName}`} : {}),
|
|
241
|
+
};
|
|
242
|
+
}),
|
|
243
|
+
|
|
244
|
+
aiFunc('createHitCampaign', 'dynamic createHitCampaign(view view, string templateName, string campaignName, map campaignFields)',
|
|
245
|
+
'Create a new campaign from a template and open it. Hit Design and PeptiHit only. campaignName and campaignFields are optional, but fields the template marks required must be provided',
|
|
246
|
+
async (view: any, templateName: string, campaignName?: string, campaignFields?: {[key: string]: unknown}) => {
|
|
247
|
+
const app = designAppOf(view);
|
|
248
|
+
const path = `${app.appName}/templates/${templateName}.json`;
|
|
249
|
+
if (!await _package.files.exists(path))
|
|
250
|
+
return {success: false, error: `No template '${templateName}' — call listHitTemplates for names`};
|
|
251
|
+
const template = await loadTemplate<HitDesignTemplate>(path);
|
|
252
|
+
const missing = (template.campaignFields ?? [])
|
|
253
|
+
.filter((f) => f.required && (campaignFields?.[f.name] == null || campaignFields[f.name] === ''))
|
|
254
|
+
.map((f) => f.name);
|
|
255
|
+
if (missing.length)
|
|
256
|
+
return {success: false, error: `Missing required campaign fields: ${missing.join(', ')} — pass them in campaignFields`};
|
|
257
|
+
app.clearCampaign();
|
|
258
|
+
app.dataFrame = createNewHitDesignDataFrame(template, app.appName === 'PeptiHit');
|
|
259
|
+
await app.setTemplate(template as any);
|
|
260
|
+
app.campaignProps = {...(campaignFields ?? {})};
|
|
261
|
+
const friendly = campaignName?.trim() ? campaignName.trim() : undefined;
|
|
262
|
+
await app.saveCampaign(false, true, {friendlyName: friendly});
|
|
263
|
+
if (template.layoutViewState && app.campaign)
|
|
264
|
+
app.campaign.layout = template.layoutViewState;
|
|
265
|
+
return {success: true, campaign: app.campaignId, note: 'The campaign is created and its design view is open'};
|
|
266
|
+
}),
|
|
267
|
+
|
|
268
|
+
aiFunc('deleteHitCampaign', 'dynamic deleteHitCampaign(view view, string campaignCode)',
|
|
269
|
+
'Permanently delete a saved campaign. ALWAYS confirm with the user first',
|
|
270
|
+
async (view: any, campaignCode: string) => {
|
|
271
|
+
const app = appOf(view);
|
|
272
|
+
const c = await findCampaign(app, campaignCode);
|
|
273
|
+
if (!c)
|
|
274
|
+
return {success: false, error: `No campaign '${campaignCode}' — call listHitCampaigns for codes`};
|
|
275
|
+
if (!await canEditCampaign(c))
|
|
276
|
+
return {success: false, error: 'You do not have permission to delete this campaign'};
|
|
277
|
+
await app.infoView.deleteCampaignAndRefresh(app.appName, c.name);
|
|
278
|
+
return {success: true, deleted: c.name};
|
|
279
|
+
}),
|
|
280
|
+
];
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// ─────────────────────── the campaign (table view) set ───────────────────────
|
|
284
|
+
|
|
285
|
+
function campaignSharedFunctions(): DG.Func[] {
|
|
286
|
+
return [
|
|
287
|
+
aiFunc('getHitCampaignState', 'dynamic getHitCampaignState(view view)',
|
|
288
|
+
'State of the open campaign - name, template, status, molecule column, stages, columns, row counts, edit permission, submit function. Call this first. The molecule data itself lives in the attached table',
|
|
289
|
+
async (view: any) => {
|
|
290
|
+
const app = appOf(view);
|
|
291
|
+
const df = app.dataFrame;
|
|
292
|
+
const stages = app instanceof HitDesignApp ? app.stages : [];
|
|
293
|
+
return {
|
|
294
|
+
app: app.appName,
|
|
295
|
+
campaign: app.campaign ? campaignBrief(app.campaign) : null,
|
|
296
|
+
...(app.campaignId ? {code: app.campaignId} : {}),
|
|
297
|
+
moleculeColumn: app.molColName ?? null,
|
|
298
|
+
...((app as {helmColName?: string}).helmColName ? {sequenceColumn: (app as {helmColName?: string}).helmColName} : {}),
|
|
299
|
+
...(stages.length ? {stages} : {}),
|
|
300
|
+
rows: df?.rowCount ?? 0,
|
|
301
|
+
filteredRows: df?.filter.trueCount ?? 0,
|
|
302
|
+
columns: df ? df.columns.toList().filter((c) => !c.name.startsWith('~'))
|
|
303
|
+
.map((c) => ({name: c.name, type: c.type, ...(c.semType ? {semType: c.semType} : {})})) : [],
|
|
304
|
+
canEdit: app.canEdit,
|
|
305
|
+
submitFunction: app.submitParams ? `${app.submitParams.package}:${app.submitParams.fName}` : null,
|
|
306
|
+
compute: computeBrief(app.campaign?.template ?? app.template),
|
|
307
|
+
};
|
|
308
|
+
}),
|
|
309
|
+
|
|
310
|
+
aiFunc('saveHitCampaign', 'dynamic saveHitCampaign(view view)',
|
|
311
|
+
'Save the open campaign - its table, layout and metadata',
|
|
312
|
+
async (view: any) => {
|
|
313
|
+
const app = appOf(view);
|
|
314
|
+
if (!app.canEdit)
|
|
315
|
+
return {success: false, error: 'You do not have permission to modify this campaign'};
|
|
316
|
+
await app.saveCampaign();
|
|
317
|
+
return {success: true, campaign: app.campaignId ?? app.campaign?.name};
|
|
318
|
+
}),
|
|
319
|
+
|
|
320
|
+
aiFunc('setHitCampaignStatus', 'dynamic setHitCampaignStatus(view view, string status)',
|
|
321
|
+
'Set the status of the open campaign (free text, e.g. In Progress, Approved) and save it',
|
|
322
|
+
async (view: any, status: string) => {
|
|
323
|
+
const app = appOf(view);
|
|
324
|
+
if (!status?.trim())
|
|
325
|
+
return {success: false, error: 'status is required'};
|
|
326
|
+
if (!app.campaign)
|
|
327
|
+
return {success: false, error: 'No campaign is open'};
|
|
328
|
+
if (!app.canEdit)
|
|
329
|
+
return {success: false, error: 'You do not have permission to modify this campaign'};
|
|
330
|
+
app.campaign.status = status.trim();
|
|
331
|
+
await app.saveCampaign();
|
|
332
|
+
return {success: true, status: app.campaign.status};
|
|
333
|
+
}),
|
|
334
|
+
|
|
335
|
+
aiFunc('submitHitCampaign', 'dynamic submitHitCampaign(view view, string status)',
|
|
336
|
+
'Submit the campaign molecules to the configured submit function and save. status is optional and applies to Hit Design campaigns (Hit Triage submits always set the status to Submitted). ALWAYS confirm with the user first',
|
|
337
|
+
async (view: any, status?: string) => {
|
|
338
|
+
const app = appOf(view);
|
|
339
|
+
if (!app.canEdit)
|
|
340
|
+
return {success: false, error: 'You do not have permission to modify this campaign'};
|
|
341
|
+
if (!app.submitParams)
|
|
342
|
+
return {success: false, error: 'No submit function is configured for this campaign template'};
|
|
343
|
+
const submitView = app.submitView;
|
|
344
|
+
if (!submitView)
|
|
345
|
+
return {success: false, error: 'The campaign is not fully open yet'};
|
|
346
|
+
const ok = submitView.submitWithStatus ?
|
|
347
|
+
await submitView.submitWithStatus(status ?? undefined) :
|
|
348
|
+
await submitView.submit();
|
|
349
|
+
if (!ok)
|
|
350
|
+
return {success: false, error: 'The configured submit function could not be run'};
|
|
351
|
+
return {success: true, submittedTo: `${app.submitParams.package}:${app.submitParams.fName}`};
|
|
352
|
+
}),
|
|
353
|
+
|
|
354
|
+
aiFunc('searchHitTableFunctions', 'dynamic searchHitTableFunctions(view view, string query, int limit)',
|
|
355
|
+
'Search the standard table view functions (hundreds of data commands - add columns, aggregate, select rows, viewers). ALWAYS pass a query. Invoke matches with callHitTableFunction',
|
|
356
|
+
async (view: any, query?: string, limit?: number) => {
|
|
357
|
+
const tv = campaignTableView(view);
|
|
358
|
+
const funcs = DG.View.prototype.getFunctions.call(tv) as DG.Func[];
|
|
359
|
+
const terms = (query ?? '').toLowerCase().split(/\s+/).filter((t) => t.length > 0);
|
|
360
|
+
const score = (f: DG.Func) => {
|
|
361
|
+
if (!terms.length)
|
|
362
|
+
return 1;
|
|
363
|
+
const text = [f.name, f.friendlyName, f.description].filter(Boolean).join(' ').toLowerCase();
|
|
364
|
+
return terms.filter((t) => text.includes(t)).length;
|
|
365
|
+
};
|
|
366
|
+
const matched = funcs.map((f) => ({f, s: score(f)})).filter((x) => x.s > 0)
|
|
367
|
+
.sort((a, b) => b.s - a.s).map((x) => x.f);
|
|
368
|
+
const max = limit && limit > 0 ? limit : DEFAULT_SEARCH_LIMIT;
|
|
369
|
+
return {
|
|
370
|
+
total: matched.length,
|
|
371
|
+
functions: matched.slice(0, max).map((f) => ({
|
|
372
|
+
name: f.name,
|
|
373
|
+
description: f.description || f.friendlyName || f.name,
|
|
374
|
+
inputs: f.inputs.filter((p) => p.name !== 'view' && (p.propertyType as string) !== 'view')
|
|
375
|
+
.map((p) => ({name: p.name, type: p.propertyType})),
|
|
376
|
+
})),
|
|
377
|
+
...(matched.length > max ? {note: `Showing ${max} of ${matched.length} — refine the query`} : {}),
|
|
378
|
+
};
|
|
379
|
+
}),
|
|
380
|
+
|
|
381
|
+
aiFunc('callHitTableFunction', 'dynamic callHitTableFunction(view view, string name, map parameters)',
|
|
382
|
+
'Invoke a standard table view function by name (from searchHitTableFunctions) with parameters keyed by input name',
|
|
383
|
+
async (view: any, name: string, parameters?: {[key: string]: unknown}) => {
|
|
384
|
+
const tv = campaignTableView(view);
|
|
385
|
+
const funcs = DG.View.prototype.getFunctions.call(tv) as DG.Func[];
|
|
386
|
+
const f = funcs.find((x) => x.name === name) ??
|
|
387
|
+
funcs.find((x) => x.name.toLowerCase() === (name ?? '').toLowerCase());
|
|
388
|
+
if (!f)
|
|
389
|
+
return {success: false, error: `No table function '${name}' — call searchHitTableFunctions to find it`};
|
|
390
|
+
const params: {[key: string]: unknown} = {...(parameters ?? {})};
|
|
391
|
+
for (const inp of f.inputs) {
|
|
392
|
+
if (!(inp.name in params) && (inp.name === 'view' || (inp.propertyType as string) === 'view'))
|
|
393
|
+
params[inp.name] = tv;
|
|
394
|
+
}
|
|
395
|
+
const result = await f.apply(params);
|
|
396
|
+
return {success: true, ...(result != null ? {result: serializeResult(result)} : {})};
|
|
397
|
+
}),
|
|
398
|
+
];
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function designCampaignFunctions(): DG.Func[] {
|
|
402
|
+
return [
|
|
403
|
+
...campaignSharedFunctions(),
|
|
404
|
+
|
|
405
|
+
aiFunc('setHitMolecule', 'dynamic setHitMolecule(view view, string molecule, int row)',
|
|
406
|
+
'Set the molecule of a row (SMILES or molblock, HELM for PeptiHit). Omit row to add a new row. Registers the V-iD and runs the configured property calculations',
|
|
407
|
+
async (view: any, molecule: string, row?: number) => {
|
|
408
|
+
const app = designAppOf(view);
|
|
409
|
+
if (!app.canEdit)
|
|
410
|
+
return {success: false, error: 'You do not have permission to modify this campaign'};
|
|
411
|
+
if (!molecule?.trim())
|
|
412
|
+
return {success: false, error: 'molecule is required'};
|
|
413
|
+
const df = app.dataFrame;
|
|
414
|
+
if (!df)
|
|
415
|
+
return {success: false, error: 'No campaign table is open'};
|
|
416
|
+
const helmColName = (app as {helmColName?: string}).helmColName;
|
|
417
|
+
const entryCol = helmColName ? df.col(helmColName) : df.col(app.molColName!);
|
|
418
|
+
if (!entryCol)
|
|
419
|
+
return {success: false, error: 'The molecule column was not found'};
|
|
420
|
+
let idx = row ?? null;
|
|
421
|
+
if (idx == null) {
|
|
422
|
+
df.rows.addNew(null, true);
|
|
423
|
+
idx = df.rowCount - 1;
|
|
424
|
+
} else if (idx < 0 || idx >= df.rowCount)
|
|
425
|
+
return {success: false, error: `row must be between 0 and ${df.rowCount - 1}`};
|
|
426
|
+
entryCol.set(idx, molecule, true);
|
|
427
|
+
if (helmColName) {
|
|
428
|
+
// PeptiHit: the calculation converts the sequence and fills the molecule column
|
|
429
|
+
await app.performSingleCellCalculations(idx, molecule);
|
|
430
|
+
const mol = df.col(app.molColName!)?.get(idx);
|
|
431
|
+
if (mol) {
|
|
432
|
+
try {
|
|
433
|
+
const vid = await registerMol(mol, app.campaignId!, app.appName);
|
|
434
|
+
if (vid)
|
|
435
|
+
df.col(ViDColName)?.set(idx, vid, true);
|
|
436
|
+
} catch (e) {
|
|
437
|
+
console.error('Failed to register molecule', e);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
} else {
|
|
441
|
+
try {
|
|
442
|
+
const vid = await registerMol(molecule, app.campaignId!, app.appName);
|
|
443
|
+
if (vid)
|
|
444
|
+
df.col(ViDColName)?.set(idx, vid, true);
|
|
445
|
+
} catch (e) {
|
|
446
|
+
console.error('Failed to register molecule', e);
|
|
447
|
+
}
|
|
448
|
+
await app.performSingleCellCalculations(idx, molecule);
|
|
449
|
+
}
|
|
450
|
+
return {success: true, row: idx, ...(df.col(ViDColName)?.get(idx) ? {vId: df.col(ViDColName)!.get(idx)} : {})};
|
|
451
|
+
}),
|
|
452
|
+
|
|
453
|
+
aiFunc('setHitStage', 'dynamic setHitStage(view view, int row, string stage)',
|
|
454
|
+
'Move a row to another stage of the campaign progress tracker and save',
|
|
455
|
+
async (view: any, row: number, stage: string) => {
|
|
456
|
+
const app = designAppOf(view);
|
|
457
|
+
if (!app.canEdit)
|
|
458
|
+
return {success: false, error: 'You do not have permission to modify this campaign'};
|
|
459
|
+
const df = app.dataFrame;
|
|
460
|
+
const stageCol = df?.col(TileCategoriesColName);
|
|
461
|
+
if (!df || !stageCol)
|
|
462
|
+
return {success: false, error: 'This campaign has no stage column'};
|
|
463
|
+
if (row == null || row < 0 || row >= df.rowCount)
|
|
464
|
+
return {success: false, error: `row must be between 0 and ${df.rowCount - 1}`};
|
|
465
|
+
if (!app.stages.includes(stage))
|
|
466
|
+
return {success: false, error: `Unknown stage '${stage}' — the stages are: ${app.stages.join(', ')}`};
|
|
467
|
+
stageCol.set(row, stage, true);
|
|
468
|
+
await app.saveCampaign(false);
|
|
469
|
+
return {success: true, row, stage};
|
|
470
|
+
}),
|
|
471
|
+
|
|
472
|
+
aiFunc('setHitStages', 'dynamic setHitStages(view view, list stages)',
|
|
473
|
+
'Replace the stage list of the campaign progress tracker. Rows in a removed stage move to the first stage. Saves the campaign',
|
|
474
|
+
async (view: any, stages: string[]) => {
|
|
475
|
+
const app = designAppOf(view);
|
|
476
|
+
if (!app.canEdit)
|
|
477
|
+
return {success: false, error: 'You do not have permission to modify this campaign'};
|
|
478
|
+
if (!Array.isArray(stages) || !stages.length || stages.some((s) => typeof s !== 'string' || !s.trim()))
|
|
479
|
+
return {success: false, error: 'Pass stages as a non-empty list of names'};
|
|
480
|
+
if (!app.campaign?.template || !app.template)
|
|
481
|
+
return {success: false, error: 'No campaign is open'};
|
|
482
|
+
if (!app.dataFrame?.col(TileCategoriesColName))
|
|
483
|
+
return {success: false, error: 'This campaign has no stage column'};
|
|
484
|
+
await app.setStages(stages.map((s) => s.trim()));
|
|
485
|
+
return {success: true, stages: app.stages};
|
|
486
|
+
}),
|
|
487
|
+
|
|
488
|
+
aiFunc('recalculateHitRow', 'dynamic recalculateHitRow(view view, int row)',
|
|
489
|
+
'Re-run the configured property calculations for one row of the campaign',
|
|
490
|
+
async (view: any, row: number) => {
|
|
491
|
+
const app = designAppOf(view);
|
|
492
|
+
const df = app.dataFrame;
|
|
493
|
+
if (!df)
|
|
494
|
+
return {success: false, error: 'No campaign table is open'};
|
|
495
|
+
if (row == null || row < 0 || row >= df.rowCount)
|
|
496
|
+
return {success: false, error: `row must be between 0 and ${df.rowCount - 1}`};
|
|
497
|
+
const helmColName = (app as {helmColName?: string}).helmColName;
|
|
498
|
+
const value = (helmColName ? df.col(helmColName) : df.col(app.molColName!))?.get(row);
|
|
499
|
+
if (!value)
|
|
500
|
+
return {success: false, error: 'The row has no molecule to calculate from'};
|
|
501
|
+
await app.performSingleCellCalculations(row, value);
|
|
502
|
+
return {success: true, row};
|
|
503
|
+
}),
|
|
504
|
+
];
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// ─────────────────────── attachment ───────────────────────
|
|
508
|
+
|
|
509
|
+
export function hitInfoAiDescription(appName: AppName): string {
|
|
510
|
+
const purpose = appName === 'Hit Design' || appName === 'PeptiHit' ?
|
|
511
|
+
'design molecules in a spreadsheet, calculate properties, and track progress through stages' :
|
|
512
|
+
'ingest a molecule dataset, calculate properties, and filter it down to the hits';
|
|
513
|
+
return `${appName} — the landing page of a molecule campaign app where teams ${purpose}. ` +
|
|
514
|
+
'Act on it through the view functions (search list_view_functions with "hit"): getHitAppInfo (call first), ' +
|
|
515
|
+
'listHitCampaigns / getHitCampaignDetails / openHitCampaign to browse and continue campaigns, ' +
|
|
516
|
+
'listHitTemplates / getHitTemplateDetails for the templates, createHitCampaign / deleteHitCampaign to manage them ' +
|
|
517
|
+
'(confirm deletions with the user first).';
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function campaignAiDescription(appName: AppName, kind: 'design' | 'pick'): string {
|
|
521
|
+
const work = kind === 'design' ?
|
|
522
|
+
'sketching and editing molecules in the grid. Hit-specific functions: getHitCampaignState (call first), ' +
|
|
523
|
+
'setHitMolecule / setHitStage / setHitStages / recalculateHitRow to edit, ' +
|
|
524
|
+
'saveHitCampaign / setHitCampaignStatus / submitHitCampaign to persist' :
|
|
525
|
+
'filtering the enriched molecule table down to the hits. Hit-specific functions: getHitCampaignState (call first), ' +
|
|
526
|
+
'saveHitCampaign / setHitCampaignStatus / submitHitCampaign';
|
|
527
|
+
return `An open ${appName} campaign — a molecule spreadsheet for ${work}. ` +
|
|
528
|
+
'The underlying table view also has hundreds of standard data commands — find them with ' +
|
|
529
|
+
'searchHitTableFunctions (always pass a query) and run them with callHitTableFunction. ' +
|
|
530
|
+
'The data itself lives in the attached table.';
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/** Called from the app constructors: the MultiView (the shell view hosting the info
|
|
534
|
+
* view) reports the info vocabulary. */
|
|
535
|
+
export function attachHitInfoAi(multiView: DG.MultiView, app: HitAppBase<AnyTemplate>): void {
|
|
536
|
+
appByView.set(multiView, app);
|
|
537
|
+
// TODO: Remove before api release
|
|
538
|
+
(multiView as any).aiDescription = hitInfoAiDescription(app.appName);
|
|
539
|
+
multiView.getFunctions = () => hitInfoViewFunctions();
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/** Called when a campaign table view is created: only the hit-specific vocabulary is
|
|
543
|
+
* reported (the standard table functions stay reachable through the search/call bridge). */
|
|
544
|
+
export function attachHitCampaignAi(view: DG.TableView, app: HitAppBase<AnyTemplate>, kind: 'design' | 'pick'): void {
|
|
545
|
+
appByView.set(view, app);
|
|
546
|
+
// TODO: Remove before api release
|
|
547
|
+
(view as any).aiDescription = campaignAiDescription(app.appName, kind);
|
|
548
|
+
view.getFunctions = () => kind === 'design' ? designCampaignFunctions() : campaignSharedFunctions();
|
|
549
|
+
}
|
package/src/app/base-view.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as DG from 'datagrok-api/dg';
|
|
2
2
|
import {AppName, HitDesignTemplate, HitTriageTemplate, PeptiHitTemplate} from './types';
|
|
3
|
-
import {HitDesignApp} from './hit-design-app';
|
|
4
|
-
import {HitTriageApp} from './hit-triage-app';
|
|
3
|
+
import type {HitDesignApp} from './hit-design-app';
|
|
4
|
+
import type {HitTriageApp} from './hit-triage-app';
|
|
5
5
|
import {_package} from '../package';
|
|
6
6
|
|
|
7
7
|
export class HitBaseView<Ttemplate extends HitDesignTemplate | HitTriageTemplate | PeptiHitTemplate,
|
|
@@ -16,6 +16,9 @@ export class HitBaseView<Ttemplate extends HitDesignTemplate | HitTriageTemplate
|
|
|
16
16
|
|
|
17
17
|
get template(): Ttemplate {return this.app.template! as Ttemplate;}
|
|
18
18
|
|
|
19
|
+
/** Info views implement this to rebuild their content. */
|
|
20
|
+
init?(presetTemplate?: any): Promise<void>;
|
|
21
|
+
|
|
19
22
|
/** Override to initialize the view based on the session. */
|
|
20
23
|
onActivated(): void {
|
|
21
24
|
}
|
|
@@ -29,5 +32,14 @@ export class HitBaseView<Ttemplate extends HitDesignTemplate | HitTriageTemplate
|
|
|
29
32
|
|
|
30
33
|
async deleteCampaign(appName: AppName, campaignId: string): Promise<void> {
|
|
31
34
|
await _package.files.delete(`${appName}/campaigns/${campaignId}`);
|
|
35
|
+
delete (_package.campaignsCache[appName] as {[name: string]: unknown} | undefined)?.[campaignId];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The full campaign-deletion flow the UI and the AI share: delete the files,
|
|
39
|
+
* hide the campaign from the cached list, and rebuild the view. */
|
|
40
|
+
async deleteCampaignAndRefresh(appName: AppName, campaignId: string): Promise<void> {
|
|
41
|
+
await this.deleteCampaign(appName, campaignId);
|
|
42
|
+
this.deletedCampaigns.push(campaignId);
|
|
43
|
+
await this.init?.();
|
|
32
44
|
}
|
|
33
45
|
}
|
|
@@ -11,6 +11,9 @@ import {HTFunctionOrderingLSKey} from '../consts';
|
|
|
11
11
|
export async function chemFunctionsDialog(app: HitAppBase<any>,
|
|
12
12
|
onOk: (result: IComputeDialogResult) => void, onCancel: () => void,
|
|
13
13
|
template: Omit<HitTriageTemplate, 'dataSourceType'>, dialog?: boolean,
|
|
14
|
+
// When true, each function/script/query gets a "re-run when campaign opens" checkbox whose state
|
|
15
|
+
// is persisted in the campaign's compute config (see HitDesignApp.reComputeOnCampaignOpen).
|
|
16
|
+
rerunOnOpen?: boolean,
|
|
14
17
|
): Promise<IChemFunctionsDialogResult> {
|
|
15
18
|
return _chemFunctionsDialog(
|
|
16
19
|
app.computeFunctions,
|
|
@@ -19,5 +22,11 @@ export async function chemFunctionsDialog(app: HitAppBase<any>,
|
|
|
19
22
|
dialog,
|
|
20
23
|
() => grok.chem.descriptorsTree() as Promise<IDescriptorTree>,
|
|
21
24
|
HTFunctionOrderingLSKey,
|
|
25
|
+
undefined,
|
|
26
|
+
rerunOnOpen ? {
|
|
27
|
+
label: 'Re-run when campaign opens',
|
|
28
|
+
tooltip: 'Recompute this function for the whole molecule column every time this campaign is ' +
|
|
29
|
+
'opened. Useful for database lookups that may resolve after the molecule was first drawn.',
|
|
30
|
+
} : undefined,
|
|
22
31
|
);
|
|
23
32
|
}
|
package/src/app/hit-app-base.ts
CHANGED
|
@@ -16,6 +16,7 @@ export abstract class HitAppBase<T> {
|
|
|
16
16
|
public computeFunctions: ComputeFunctions;
|
|
17
17
|
public isJoining = false;
|
|
18
18
|
protected hasEditPermission = false;
|
|
19
|
+
public get canEdit(): boolean {return this.hasEditPermission;}
|
|
19
20
|
private _appName: AppName;
|
|
20
21
|
public get appName(): AppName {return this._appName;}
|
|
21
22
|
public static molFileExtReaders: { ext: string; handlerFunc: DG.Func }[] = ['sdf', 'mol', 'smi', 'mol2']
|