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