2023-07-26 22:31:52 -07:00
|
|
|
/*
|
2024-02-13 07:59:27 -08:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-26 22:31:52 -07:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2024-03-13 06:38:26 -07:00
|
|
|
import { ref } from 'vue';
|
2023-01-02 22:51:49 -08:00
|
|
|
import { Interpreter, Parser, utils, values } from '@syuilo/aiscript';
|
2024-01-03 19:26:57 -08:00
|
|
|
import { aiScriptReadline, createAiScriptEnv } from '@/scripts/aiscript/api.js';
|
2023-09-19 00:37:43 -07:00
|
|
|
import { inputText } from '@/os.js';
|
|
|
|
import { Plugin, noteActions, notePostInterruptors, noteViewInterruptors, postFormActions, userActions, pageViewInterruptors } from '@/store.js';
|
2020-10-17 04:12:00 -07:00
|
|
|
|
2023-01-02 22:51:49 -08:00
|
|
|
const parser = new Parser();
|
|
|
|
const pluginContexts = new Map<string, Interpreter>();
|
2024-03-13 06:38:26 -07:00
|
|
|
export const pluginLogs = ref(new Map<string, string[]>());
|
2020-10-17 04:12:00 -07:00
|
|
|
|
2023-11-05 18:21:43 -08:00
|
|
|
export async function install(plugin: Plugin): Promise<void> {
|
2023-01-02 22:51:49 -08:00
|
|
|
// 後方互換性のため
|
|
|
|
if (plugin.src == null) return;
|
2020-10-17 04:12:00 -07:00
|
|
|
|
2023-01-02 22:51:49 -08:00
|
|
|
const aiscript = new Interpreter(createPluginEnv({
|
2020-10-17 04:12:00 -07:00
|
|
|
plugin: plugin,
|
2022-12-21 23:01:59 -08:00
|
|
|
storageKey: 'plugins:' + plugin.id,
|
2020-10-17 04:12:00 -07:00
|
|
|
}), {
|
2024-01-03 19:26:57 -08:00
|
|
|
in: aiScriptReadline,
|
2023-02-28 22:19:20 -08:00
|
|
|
out: (value): void => {
|
2020-10-17 04:12:00 -07:00
|
|
|
console.log(value);
|
2024-03-13 06:38:26 -07:00
|
|
|
pluginLogs.value.get(plugin.id).push(utils.reprValue(value));
|
2020-10-17 04:12:00 -07:00
|
|
|
},
|
2023-02-28 22:19:20 -08:00
|
|
|
log: (): void => {
|
2020-10-17 04:12:00 -07:00
|
|
|
},
|
2024-06-20 02:20:47 -07:00
|
|
|
/* dakkar 2024-06-20
|
|
|
|
|
|
|
|
passing an `err` triggers an unwanted side-effect inside the
|
|
|
|
AiScript Interpreter:
|
|
|
|
|
|
|
|
- the plugin code throws an exception of any kind (in the
|
|
|
|
specific case that made us look, it was `note.text.split(...)`
|
|
|
|
on a note with no text)
|
|
|
|
|
|
|
|
- the Interpreter's `handleError` calls `this.abort()` before
|
|
|
|
calling our `err`
|
|
|
|
|
|
|
|
- from that point on, every evaluation of that Interpreter object
|
|
|
|
returns null
|
|
|
|
|
|
|
|
- which, at least inside a noteViewInterruptor, causes all notes
|
|
|
|
to be replaced with a null
|
|
|
|
|
|
|
|
I'm reporting this problem upstream, in the meantime we'll have
|
|
|
|
to do without error logs
|
|
|
|
*/
|
|
|
|
// err: (err): void => {
|
|
|
|
// pluginLogs.value.get(plugin.id).push(`${err}`);
|
|
|
|
// throw err; // install時のtry-catchに反応させる
|
|
|
|
// },
|
2020-10-17 04:12:00 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
initPlugin({ plugin, aiscript });
|
|
|
|
|
2024-03-13 06:38:26 -07:00
|
|
|
aiscript.exec(parser.parse(plugin.src)).then(
|
|
|
|
() => {
|
|
|
|
console.info('Plugin installed:', plugin.name, 'v' + plugin.version);
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
console.error('Plugin install failed:', plugin.name, 'v' + plugin.version);
|
|
|
|
throw err;
|
|
|
|
},
|
|
|
|
);
|
2020-10-17 04:12:00 -07:00
|
|
|
}
|
|
|
|
|
2023-02-28 22:19:20 -08:00
|
|
|
function createPluginEnv(opts: { plugin: Plugin; storageKey: string }): Record<string, values.Value> {
|
|
|
|
const config = new Map<string, values.Value>();
|
|
|
|
for (const [k, v] of Object.entries(opts.plugin.config ?? {})) {
|
2023-01-02 22:51:49 -08:00
|
|
|
config.set(k, utils.jsToVal(typeof opts.plugin.configData[k] !== 'undefined' ? opts.plugin.configData[k] : v.default));
|
2020-10-17 04:12:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...createAiScriptEnv({ ...opts, token: opts.plugin.token }),
|
|
|
|
//#region Deprecated
|
|
|
|
'Mk:register_post_form_action': values.FN_NATIVE(([title, handler]) => {
|
2023-02-28 22:19:20 -08:00
|
|
|
utils.assertString(title);
|
2020-10-17 04:12:00 -07:00
|
|
|
registerPostFormAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Mk:register_user_action': values.FN_NATIVE(([title, handler]) => {
|
2023-02-28 22:19:20 -08:00
|
|
|
utils.assertString(title);
|
2020-10-17 04:12:00 -07:00
|
|
|
registerUserAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Mk:register_note_action': values.FN_NATIVE(([title, handler]) => {
|
2023-02-28 22:19:20 -08:00
|
|
|
utils.assertString(title);
|
2020-10-17 04:12:00 -07:00
|
|
|
registerNoteAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
//#endregion
|
|
|
|
'Plugin:register_post_form_action': values.FN_NATIVE(([title, handler]) => {
|
2023-02-28 22:19:20 -08:00
|
|
|
utils.assertString(title);
|
2020-10-17 04:12:00 -07:00
|
|
|
registerPostFormAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Plugin:register_user_action': values.FN_NATIVE(([title, handler]) => {
|
2023-02-28 22:19:20 -08:00
|
|
|
utils.assertString(title);
|
2020-10-17 04:12:00 -07:00
|
|
|
registerUserAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Plugin:register_note_action': values.FN_NATIVE(([title, handler]) => {
|
2023-02-28 22:19:20 -08:00
|
|
|
utils.assertString(title);
|
2020-10-17 04:12:00 -07:00
|
|
|
registerNoteAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Plugin:register_note_view_interruptor': values.FN_NATIVE(([handler]) => {
|
|
|
|
registerNoteViewInterruptor({ pluginId: opts.plugin.id, handler });
|
|
|
|
}),
|
|
|
|
'Plugin:register_note_post_interruptor': values.FN_NATIVE(([handler]) => {
|
|
|
|
registerNotePostInterruptor({ pluginId: opts.plugin.id, handler });
|
|
|
|
}),
|
2023-03-14 18:44:24 -07:00
|
|
|
'Plugin:register_page_view_interruptor': values.FN_NATIVE(([handler]) => {
|
|
|
|
registerPageViewInterruptor({ pluginId: opts.plugin.id, handler });
|
|
|
|
}),
|
2020-10-17 04:12:00 -07:00
|
|
|
'Plugin:open_url': values.FN_NATIVE(([url]) => {
|
2023-02-28 22:19:20 -08:00
|
|
|
utils.assertString(url);
|
2023-12-08 00:48:18 -08:00
|
|
|
window.open(url.value, '_blank', 'noopener');
|
2020-10-17 04:12:00 -07:00
|
|
|
}),
|
|
|
|
'Plugin:config': values.OBJ(config),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-28 22:19:20 -08:00
|
|
|
function initPlugin({ plugin, aiscript }): void {
|
2020-10-17 04:12:00 -07:00
|
|
|
pluginContexts.set(plugin.id, aiscript);
|
2024-03-13 06:38:26 -07:00
|
|
|
pluginLogs.value.set(plugin.id, []);
|
2020-10-17 04:12:00 -07:00
|
|
|
}
|
|
|
|
|
2023-02-28 22:19:20 -08:00
|
|
|
function registerPostFormAction({ pluginId, title, handler }): void {
|
2020-10-17 04:12:00 -07:00
|
|
|
postFormActions.push({
|
|
|
|
title, handler: (form, update) => {
|
2023-02-28 22:19:20 -08:00
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pluginContext.execFn(handler, [utils.jsToVal(form), values.FN_NATIVE(([key, value]) => {
|
|
|
|
if (!key || !value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
update(utils.valToJs(key), utils.valToJs(value));
|
2020-10-17 04:12:00 -07:00
|
|
|
})]);
|
2022-12-21 23:01:59 -08:00
|
|
|
},
|
2020-10-17 04:12:00 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-28 22:19:20 -08:00
|
|
|
function registerUserAction({ pluginId, title, handler }): void {
|
2020-10-17 04:12:00 -07:00
|
|
|
userActions.push({
|
|
|
|
title, handler: (user) => {
|
2023-02-28 22:19:20 -08:00
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pluginContext.execFn(handler, [utils.jsToVal(user)]);
|
2022-12-21 23:01:59 -08:00
|
|
|
},
|
2020-10-17 04:12:00 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-28 22:19:20 -08:00
|
|
|
function registerNoteAction({ pluginId, title, handler }): void {
|
2020-10-17 04:12:00 -07:00
|
|
|
noteActions.push({
|
|
|
|
title, handler: (note) => {
|
2023-02-28 22:19:20 -08:00
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pluginContext.execFn(handler, [utils.jsToVal(note)]);
|
2022-12-21 23:01:59 -08:00
|
|
|
},
|
2020-10-17 04:12:00 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-28 22:19:20 -08:00
|
|
|
function registerNoteViewInterruptor({ pluginId, handler }): void {
|
2020-10-17 04:12:00 -07:00
|
|
|
noteViewInterruptors.push({
|
|
|
|
handler: async (note) => {
|
2023-02-28 22:19:20 -08:00
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return utils.valToJs(await pluginContext.execFn(handler, [utils.jsToVal(note)]));
|
2022-12-21 23:01:59 -08:00
|
|
|
},
|
2020-10-17 04:12:00 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-28 22:19:20 -08:00
|
|
|
function registerNotePostInterruptor({ pluginId, handler }): void {
|
2020-10-17 04:12:00 -07:00
|
|
|
notePostInterruptors.push({
|
|
|
|
handler: async (note) => {
|
2023-02-28 22:19:20 -08:00
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return utils.valToJs(await pluginContext.execFn(handler, [utils.jsToVal(note)]));
|
2022-12-21 23:01:59 -08:00
|
|
|
},
|
2020-10-17 04:12:00 -07:00
|
|
|
});
|
|
|
|
}
|
2023-03-14 18:44:24 -07:00
|
|
|
|
|
|
|
function registerPageViewInterruptor({ pluginId, handler }): void {
|
|
|
|
pageViewInterruptors.push({
|
|
|
|
handler: async (page) => {
|
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return utils.valToJs(await pluginContext.execFn(handler, [utils.jsToVal(page)]));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|