2023-07-27 14:31:52 +09:00
|
|
|
<!--
|
2024-02-14 00:59:27 +09:00
|
|
|
SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2021-02-06 21:36:47 +09:00
|
|
|
<template>
|
2023-05-19 16:20:53 +09:00
|
|
|
<MkContainer :showHeader="widgetProps.showHeader" data-cy-mkw-aiscript class="mkw-aiscript">
|
2023-01-15 08:30:29 +09:00
|
|
|
<template #icon><i class="ti ti-terminal-2"></i></template>
|
|
|
|
<template #header>{{ i18n.ts._widgets.aiscript }}</template>
|
2021-02-06 21:36:47 +09:00
|
|
|
|
|
|
|
<div class="uylguesu _monospace">
|
2022-01-08 20:30:01 +09:00
|
|
|
<textarea v-model="widgetProps.script" placeholder="(1 + 1)"></textarea>
|
2021-11-19 19:36:12 +09:00
|
|
|
<button class="_buttonPrimary" @click="run">RUN</button>
|
2021-02-06 21:36:47 +09:00
|
|
|
<div class="logs">
|
2021-11-19 19:36:12 +09:00
|
|
|
<div v-for="log in logs" :key="log.id" class="log" :class="{ print: log.print }">{{ log.text }}</div>
|
2021-02-06 21:36:47 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</MkContainer>
|
|
|
|
</template>
|
|
|
|
|
2022-01-08 20:30:01 +09:00
|
|
|
<script lang="ts" setup>
|
2023-02-16 23:09:41 +09:00
|
|
|
import { ref } from 'vue';
|
2023-01-03 15:51:49 +09:00
|
|
|
import { Interpreter, Parser, utils } from '@syuilo/aiscript';
|
2023-12-21 11:36:45 +09:00
|
|
|
import { useWidgetPropsManager, WidgetComponentEmits, WidgetComponentExpose, WidgetComponentProps } from './widget.js';
|
2023-09-19 16:37:43 +09:00
|
|
|
import { GetFormResultType } from '@/scripts/form.js';
|
|
|
|
import * as os from '@/os.js';
|
2022-09-06 18:21:49 +09:00
|
|
|
import MkContainer from '@/components/MkContainer.vue';
|
2024-01-04 12:26:57 +09:00
|
|
|
import { aiScriptReadline, createAiScriptEnv } from '@/scripts/aiscript/api.js';
|
2023-09-19 16:37:43 +09:00
|
|
|
import { $i } from '@/account.js';
|
|
|
|
import { i18n } from '@/i18n.js';
|
2021-02-06 21:36:47 +09:00
|
|
|
|
2022-01-08 20:30:01 +09:00
|
|
|
const name = 'aiscript';
|
2021-02-06 21:36:47 +09:00
|
|
|
|
2022-01-08 20:30:01 +09:00
|
|
|
const widgetPropsDef = {
|
|
|
|
showHeader: {
|
|
|
|
type: 'boolean' as const,
|
|
|
|
default: true,
|
2021-02-06 21:36:47 +09:00
|
|
|
},
|
2022-01-08 20:30:01 +09:00
|
|
|
script: {
|
|
|
|
type: 'string' as const,
|
|
|
|
multiline: true,
|
|
|
|
default: '(1 + 1)',
|
|
|
|
hidden: true,
|
2021-02-06 21:36:47 +09:00
|
|
|
},
|
2022-01-08 20:30:01 +09:00
|
|
|
};
|
2021-02-06 21:36:47 +09:00
|
|
|
|
2022-01-08 20:30:01 +09:00
|
|
|
type WidgetProps = GetFormResultType<typeof widgetPropsDef>;
|
2021-02-06 21:36:47 +09:00
|
|
|
|
2023-05-19 16:20:53 +09:00
|
|
|
const props = defineProps<WidgetComponentProps<WidgetProps>>();
|
|
|
|
const emit = defineEmits<WidgetComponentEmits<WidgetProps>>();
|
2022-01-08 20:30:01 +09:00
|
|
|
|
|
|
|
const { widgetProps, configure } = useWidgetPropsManager(name,
|
|
|
|
widgetPropsDef,
|
|
|
|
props,
|
|
|
|
emit,
|
|
|
|
);
|
|
|
|
|
2023-01-03 15:51:49 +09:00
|
|
|
const parser = new Parser();
|
|
|
|
|
2022-01-08 20:30:01 +09:00
|
|
|
const logs = ref<{
|
|
|
|
id: string;
|
|
|
|
text: string;
|
|
|
|
print: boolean;
|
|
|
|
}[]>([]);
|
|
|
|
|
|
|
|
const run = async () => {
|
|
|
|
logs.value = [];
|
2023-01-03 15:51:49 +09:00
|
|
|
const aiscript = new Interpreter(createAiScriptEnv({
|
2022-01-08 20:30:01 +09:00
|
|
|
storageKey: 'widget',
|
|
|
|
token: $i?.token,
|
|
|
|
}), {
|
2024-01-04 12:26:57 +09:00
|
|
|
in: aiScriptReadline,
|
2022-01-08 20:30:01 +09:00
|
|
|
out: (value) => {
|
|
|
|
logs.value.push({
|
|
|
|
id: Math.random().toString(),
|
|
|
|
text: value.type === 'str' ? value.value : utils.valToString(value),
|
|
|
|
print: true,
|
|
|
|
});
|
2021-02-06 21:36:47 +09:00
|
|
|
},
|
2022-01-08 20:30:01 +09:00
|
|
|
log: (type, params) => {
|
|
|
|
switch (type) {
|
|
|
|
case 'end': logs.value.push({
|
|
|
|
id: Math.random().toString(),
|
|
|
|
text: utils.valToString(params.val, true),
|
|
|
|
print: false,
|
|
|
|
}); break;
|
|
|
|
default: break;
|
|
|
|
}
|
2022-07-20 22:24:26 +09:00
|
|
|
},
|
2022-01-08 20:30:01 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
let ast;
|
|
|
|
try {
|
2023-01-03 15:51:49 +09:00
|
|
|
ast = parser.parse(widgetProps.script);
|
2022-05-25 16:43:12 +09:00
|
|
|
} catch (err) {
|
2022-01-08 20:30:01 +09:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
text: 'Syntax error :(',
|
|
|
|
});
|
|
|
|
return;
|
2021-02-06 21:36:47 +09:00
|
|
|
}
|
2022-01-08 20:30:01 +09:00
|
|
|
try {
|
|
|
|
await aiscript.exec(ast);
|
2022-05-25 16:43:12 +09:00
|
|
|
} catch (err) {
|
2022-01-08 20:30:01 +09:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-05-25 16:43:12 +09:00
|
|
|
text: err,
|
2022-01-08 20:30:01 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
defineExpose<WidgetComponentExpose>({
|
|
|
|
name,
|
|
|
|
configure,
|
|
|
|
id: props.widget ? props.widget.id : null,
|
2021-02-06 21:36:47 +09:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2022-12-27 18:29:39 +09:00
|
|
|
<style lang="scss" scoped>
|
2021-02-06 21:36:47 +09:00
|
|
|
.uylguesu {
|
|
|
|
text-align: right;
|
|
|
|
|
|
|
|
> textarea {
|
|
|
|
display: block;
|
|
|
|
width: 100%;
|
|
|
|
max-width: 100%;
|
|
|
|
min-width: 100%;
|
|
|
|
padding: 16px;
|
|
|
|
color: var(--fg);
|
|
|
|
background: transparent;
|
|
|
|
border: none;
|
2021-04-10 12:40:50 +09:00
|
|
|
border-bottom: solid 0.5px var(--divider);
|
2021-02-06 21:36:47 +09:00
|
|
|
border-radius: 0;
|
|
|
|
box-sizing: border-box;
|
|
|
|
font: inherit;
|
|
|
|
|
2021-10-03 02:46:58 +09:00
|
|
|
&:focus-visible {
|
2021-02-06 21:36:47 +09:00
|
|
|
outline: none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> button {
|
|
|
|
display: inline-block;
|
|
|
|
margin: 8px;
|
|
|
|
padding: 0 10px;
|
|
|
|
height: 28px;
|
|
|
|
outline: none;
|
|
|
|
border-radius: 4px;
|
|
|
|
|
|
|
|
&:disabled {
|
|
|
|
opacity: 0.7;
|
|
|
|
cursor: default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .logs {
|
2021-04-10 12:40:50 +09:00
|
|
|
border-top: solid 0.5px var(--divider);
|
2021-02-06 21:36:47 +09:00
|
|
|
text-align: left;
|
|
|
|
padding: 16px;
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
> .log {
|
|
|
|
&:not(.print) {
|
|
|
|
opacity: 0.7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|