2023-07-27 07:31:52 +02:00
|
|
|
<!--
|
2024-02-13 16:50:11 +01:00
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
2023-07-27 07:31:52 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2023-01-05 05:59:48 +01:00
|
|
|
<template>
|
2023-05-19 09:20:53 +02:00
|
|
|
<MkContainer :showHeader="widgetProps.showHeader" class="mkw-aiscriptApp">
|
2023-01-05 05:59:48 +01:00
|
|
|
<template #header>App</template>
|
|
|
|
<div :class="$style.root">
|
|
|
|
<MkAsUi v-if="root" :component="root" :components="components" size="small"/>
|
|
|
|
</div>
|
|
|
|
</MkContainer>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2023-02-16 15:09:41 +01:00
|
|
|
import { onMounted, Ref, ref, watch } from 'vue';
|
|
|
|
import { Interpreter, Parser } from '@syuilo/aiscript';
|
2023-12-21 03:36:45 +01:00
|
|
|
import { useWidgetPropsManager, WidgetComponentEmits, WidgetComponentExpose, WidgetComponentProps } from './widget.js';
|
2023-09-19 09:37:43 +02:00
|
|
|
import { GetFormResultType } from '@/scripts/form.js';
|
|
|
|
import * as os from '@/os.js';
|
2024-01-04 04:26:57 +01:00
|
|
|
import { aiScriptReadline, createAiScriptEnv } from '@/scripts/aiscript/api.js';
|
2023-09-19 09:37:43 +02:00
|
|
|
import { $i } from '@/account.js';
|
2023-01-05 05:59:48 +01:00
|
|
|
import MkAsUi from '@/components/MkAsUi.vue';
|
|
|
|
import MkContainer from '@/components/MkContainer.vue';
|
2023-09-19 09:37:43 +02:00
|
|
|
import { AsUiComponent, AsUiRoot, registerAsUiLib } from '@/scripts/aiscript/ui.js';
|
2023-01-05 05:59:48 +01:00
|
|
|
|
|
|
|
const name = 'aiscriptApp';
|
|
|
|
|
|
|
|
const widgetPropsDef = {
|
|
|
|
script: {
|
|
|
|
type: 'string' as const,
|
|
|
|
multiline: true,
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
showHeader: {
|
|
|
|
type: 'boolean' as const,
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
type WidgetProps = GetFormResultType<typeof widgetPropsDef>;
|
|
|
|
|
2023-05-19 09:20:53 +02:00
|
|
|
const props = defineProps<WidgetComponentProps<WidgetProps>>();
|
|
|
|
const emit = defineEmits<WidgetComponentEmits<WidgetProps>>();
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2023-01-05 05:59:48 +01:00
|
|
|
const { widgetProps, configure } = useWidgetPropsManager(name,
|
|
|
|
widgetPropsDef,
|
|
|
|
props,
|
|
|
|
emit,
|
|
|
|
);
|
|
|
|
|
|
|
|
const parser = new Parser();
|
|
|
|
|
|
|
|
const root = ref<AsUiRoot>();
|
2023-12-07 06:42:09 +01:00
|
|
|
const components = ref<Ref<AsUiComponent>[]>([]);
|
2023-01-05 05:59:48 +01:00
|
|
|
|
|
|
|
async function run() {
|
|
|
|
const aiscript = new Interpreter({
|
|
|
|
...createAiScriptEnv({
|
|
|
|
storageKey: 'widget',
|
|
|
|
token: $i?.token,
|
|
|
|
}),
|
2023-12-07 06:42:09 +01:00
|
|
|
...registerAsUiLib(components.value, (_root) => {
|
2023-01-05 05:59:48 +01:00
|
|
|
root.value = _root.value;
|
|
|
|
}),
|
|
|
|
}, {
|
2024-01-04 04:26:57 +01:00
|
|
|
in: aiScriptReadline,
|
2023-01-05 05:59:48 +01:00
|
|
|
out: (value) => {
|
|
|
|
// nop
|
|
|
|
},
|
|
|
|
log: (type, params) => {
|
|
|
|
// nop
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
let ast;
|
|
|
|
try {
|
|
|
|
ast = parser.parse(widgetProps.script);
|
|
|
|
} catch (err) {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
text: 'Syntax error :(',
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await aiscript.exec(ast);
|
|
|
|
} catch (err) {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
title: 'AiScript Error',
|
|
|
|
text: err.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
watch(() => widgetProps.script, () => {
|
|
|
|
run();
|
|
|
|
});
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
run();
|
|
|
|
});
|
|
|
|
|
|
|
|
defineExpose<WidgetComponentExpose>({
|
|
|
|
name,
|
|
|
|
configure,
|
|
|
|
id: props.widget ? props.widget.id : null,
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" module>
|
|
|
|
.root {
|
|
|
|
padding: 16px;
|
|
|
|
}
|
|
|
|
</style>
|