2021-02-06 06:36:47 -06:00
|
|
|
<template>
|
2022-05-28 00:28:12 -05:00
|
|
|
<MkContainer :show-header="widgetProps.showHeader" class="mkw-aiscript">
|
2022-07-20 08:24:26 -05:00
|
|
|
<template #header><i class="fas fa-terminal"></i>{{ i18n.ts._widgets.aiscript }}</template>
|
2021-02-06 06:36:47 -06:00
|
|
|
|
|
|
|
<div class="uylguesu _monospace">
|
2022-01-08 05:30:01 -06:00
|
|
|
<textarea v-model="widgetProps.script" placeholder="(1 + 1)"></textarea>
|
2021-11-19 04:36:12 -06:00
|
|
|
<button class="_buttonPrimary" @click="run">RUN</button>
|
2021-02-06 06:36:47 -06:00
|
|
|
<div class="logs">
|
2021-11-19 04:36:12 -06:00
|
|
|
<div v-for="log in logs" :key="log.id" class="log" :class="{ print: log.print }">{{ log.text }}</div>
|
2021-02-06 06:36:47 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</MkContainer>
|
|
|
|
</template>
|
|
|
|
|
2022-01-08 05:30:01 -06:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { onMounted, onUnmounted, ref, watch } from 'vue';
|
2022-07-20 08:24:26 -05:00
|
|
|
import { AiScript, parse, utils } from '@syuilo/aiscript';
|
2022-01-08 05:30:01 -06:00
|
|
|
import { useWidgetPropsManager, Widget, WidgetComponentEmits, WidgetComponentExpose, WidgetComponentProps } from './widget';
|
2022-07-20 08:24:26 -05:00
|
|
|
import { GetFormResultType } from '@/scripts/form';
|
2021-11-11 11:02:25 -06:00
|
|
|
import * as os from '@/os';
|
2022-09-06 04:21:49 -05:00
|
|
|
import MkContainer from '@/components/MkContainer.vue';
|
2021-11-11 11:02:25 -06:00
|
|
|
import { createAiScriptEnv } from '@/scripts/aiscript/api';
|
2022-01-08 05:30:01 -06:00
|
|
|
import { $i } from '@/account';
|
2022-07-20 08:24:26 -05:00
|
|
|
import { i18n } from '@/i18n';
|
2021-02-06 06:36:47 -06:00
|
|
|
|
2022-01-08 05:30:01 -06:00
|
|
|
const name = 'aiscript';
|
2021-02-06 06:36:47 -06:00
|
|
|
|
2022-01-08 05:30:01 -06:00
|
|
|
const widgetPropsDef = {
|
|
|
|
showHeader: {
|
|
|
|
type: 'boolean' as const,
|
|
|
|
default: true,
|
2021-02-06 06:36:47 -06:00
|
|
|
},
|
2022-01-08 05:30:01 -06:00
|
|
|
script: {
|
|
|
|
type: 'string' as const,
|
|
|
|
multiline: true,
|
|
|
|
default: '(1 + 1)',
|
|
|
|
hidden: true,
|
2021-02-06 06:36:47 -06:00
|
|
|
},
|
2022-01-08 05:30:01 -06:00
|
|
|
};
|
2021-02-06 06:36:47 -06:00
|
|
|
|
2022-01-08 05:30:01 -06:00
|
|
|
type WidgetProps = GetFormResultType<typeof widgetPropsDef>;
|
2021-02-06 06:36:47 -06:00
|
|
|
|
2022-01-08 05:30:01 -06:00
|
|
|
// 現時点ではvueの制限によりimportしたtypeをジェネリックに渡せない
|
|
|
|
//const props = defineProps<WidgetComponentProps<WidgetProps>>();
|
|
|
|
//const emit = defineEmits<WidgetComponentEmits<WidgetProps>>();
|
|
|
|
const props = defineProps<{ widget?: Widget<WidgetProps>; }>();
|
2022-05-25 02:43:12 -05:00
|
|
|
const emit = defineEmits<{ (ev: 'updateProps', props: WidgetProps); }>();
|
2022-01-08 05:30:01 -06:00
|
|
|
|
|
|
|
const { widgetProps, configure } = useWidgetPropsManager(name,
|
|
|
|
widgetPropsDef,
|
|
|
|
props,
|
|
|
|
emit,
|
|
|
|
);
|
|
|
|
|
|
|
|
const logs = ref<{
|
|
|
|
id: string;
|
|
|
|
text: string;
|
|
|
|
print: boolean;
|
|
|
|
}[]>([]);
|
|
|
|
|
|
|
|
const run = async () => {
|
|
|
|
logs.value = [];
|
|
|
|
const aiscript = new AiScript(createAiScriptEnv({
|
|
|
|
storageKey: 'widget',
|
|
|
|
token: $i?.token,
|
|
|
|
}), {
|
|
|
|
in: (q) => {
|
|
|
|
return new Promise(ok => {
|
|
|
|
os.inputText({
|
|
|
|
title: q,
|
|
|
|
}).then(({ canceled, result: a }) => {
|
|
|
|
ok(a);
|
2021-02-06 06:36:47 -06:00
|
|
|
});
|
2022-01-08 05:30:01 -06:00
|
|
|
});
|
|
|
|
},
|
|
|
|
out: (value) => {
|
|
|
|
logs.value.push({
|
|
|
|
id: Math.random().toString(),
|
|
|
|
text: value.type === 'str' ? value.value : utils.valToString(value),
|
|
|
|
print: true,
|
|
|
|
});
|
2021-02-06 06:36:47 -06:00
|
|
|
},
|
2022-01-08 05:30:01 -06: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 08:24:26 -05:00
|
|
|
},
|
2022-01-08 05:30:01 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
let ast;
|
|
|
|
try {
|
|
|
|
ast = parse(widgetProps.script);
|
2022-05-25 02:43:12 -05:00
|
|
|
} catch (err) {
|
2022-01-08 05:30:01 -06:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
text: 'Syntax error :(',
|
|
|
|
});
|
|
|
|
return;
|
2021-02-06 06:36:47 -06:00
|
|
|
}
|
2022-01-08 05:30:01 -06:00
|
|
|
try {
|
|
|
|
await aiscript.exec(ast);
|
2022-05-25 02:43:12 -05:00
|
|
|
} catch (err) {
|
2022-01-08 05:30:01 -06:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-05-25 02:43:12 -05:00
|
|
|
text: err,
|
2022-01-08 05:30:01 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
defineExpose<WidgetComponentExpose>({
|
|
|
|
name,
|
|
|
|
configure,
|
|
|
|
id: props.widget ? props.widget.id : null,
|
2021-02-06 06:36:47 -06:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.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-09 22:40:50 -05:00
|
|
|
border-bottom: solid 0.5px var(--divider);
|
2021-02-06 06:36:47 -06:00
|
|
|
border-radius: 0;
|
|
|
|
box-sizing: border-box;
|
|
|
|
font: inherit;
|
|
|
|
|
2021-10-02 12:46:58 -05:00
|
|
|
&:focus-visible {
|
2021-02-06 06:36:47 -06: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-09 22:40:50 -05:00
|
|
|
border-top: solid 0.5px var(--divider);
|
2021-02-06 06:36:47 -06:00
|
|
|
text-align: left;
|
|
|
|
padding: 16px;
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
> .log {
|
|
|
|
&:not(.print) {
|
|
|
|
opacity: 0.7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|