update AiScript to 0.12.0

This commit is contained in:
syuilo 2023-01-03 15:51:49 +09:00
parent db1ca28de3
commit e44f83fb9c
15 changed files with 75 additions and 157 deletions

View file

@ -20,6 +20,9 @@ You should also include the user name that made the change.
- You may have to `yarn run clean-all`, `sudo corepack enable` and `yarn set version berry` before running `yarn install` if you're still on yarn classic
- 新たに動的なPagesを作ることはできなくなりました
- 代わりに今後AiScriptを用いてより柔軟に動的なコンテンツを作成できるMisskey Play機能の実装を予定しています。
- AiScriptが0.12.0にアップデートされました
- 0.12.0の変更点についてはこちら https://github.com/syuilo/aiscript/blob/master/CHANGELOG.md#0120
- 0.12.0未満のプラグインは読み込むことはできません
- iOS15以下のデバイスはサポートされなくなりました
- API: カスタム絵文字エンティティに`url`プロパティが含まれなくなりました
- 絵文字画像を表示するには、`<instance host>/emoji/<emoji name>.webp`にリクエストすると画像が返ります。

View file

@ -37,7 +37,6 @@
"@nestjs/testing": "9.2.1",
"@peertube/http-signature": "1.7.0",
"@sinonjs/fake-timers": "10.0.2",
"@syuilo/aiscript": "0.11.1",
"accepts": "^1.3.8",
"ajv": "8.11.2",
"archiver": "5.3.1",

View file

@ -11,7 +11,7 @@
"@rollup/plugin-alias": "4.0.2",
"@rollup/plugin-json": "6.0.0",
"@rollup/pluginutils": "5.0.2",
"@syuilo/aiscript": "0.11.1",
"@syuilo/aiscript": "0.12.0",
"@tabler/icons": "^1.118.0",
"@vitejs/plugin-vue": "4.0.0",
"@vue/compiler-sfc": "3.2.45",

View file

@ -6,7 +6,6 @@
<script lang="ts">
import { defineComponent, onMounted, nextTick, onUnmounted, PropType } from 'vue';
import { parse } from '@syuilo/aiscript';
import XBlock from './page.block.vue';
import { Hpml } from '@/scripts/hpml/evaluator';
import { url } from '@/config';
@ -28,38 +27,11 @@ export default defineComponent({
randomSeed: Math.random(),
visitor: $i,
url: url,
enableAiScript: !defaultStore.state.disablePagesScript,
});
onMounted(() => {
nextTick(() => {
if (props.page.script && hpml.aiscript) {
let ast;
try {
ast = parse(props.page.script);
} catch (err) {
console.error(err);
/*os.alert({
type: 'error',
text: 'Syntax error :('
});*/
return;
}
hpml.aiscript.exec(ast).then(() => {
hpml.eval();
}).catch(err => {
console.error(err);
/*os.alert({
type: 'error',
text: err
});*/
});
} else {
hpml.eval();
}
});
onUnmounted(() => {
if (hpml.aiscript) hpml.aiscript.abort();
hpml.eval();
});
});

View file

@ -27,7 +27,7 @@ import 'prismjs/components/prism-javascript';
import 'prismjs/themes/prism-okaidia.css';
import { PrismEditor } from 'vue-prism-editor';
import 'vue-prism-editor/dist/prismeditor.min.css';
import { AiScript, parse, utils } from '@syuilo/aiscript';
import { Interpreter, Parser, utils } from '@syuilo/aiscript';
import MkContainer from '@/components/MkContainer.vue';
import MkButton from '@/components/MkButton.vue';
import { createAiScriptEnv } from '@/scripts/aiscript/api';
@ -36,6 +36,8 @@ import { $i } from '@/account';
import { i18n } from '@/i18n';
import { definePageMetadata } from '@/scripts/page-metadata';
const parser = new Parser();
const code = ref('');
const logs = ref<any[]>([]);
@ -50,7 +52,7 @@ watch(code, () => {
async function run() {
logs.value = [];
const aiscript = new AiScript(createAiScriptEnv({
const aiscript = new Interpreter(createAiScriptEnv({
storageKey: 'scratchpad',
token: $i?.token,
}), {
@ -84,7 +86,7 @@ async function run() {
let ast;
try {
ast = parse(code.value);
ast = parser.parse(code.value);
} catch (error) {
os.alert({
type: 'error',

View file

@ -27,7 +27,6 @@
<FormSwitch v-model="imageNewTab" class="_formBlock">{{ i18n.ts.openImageInNewTab }}</FormSwitch>
<FormSwitch v-model="enableInfiniteScroll" class="_formBlock">{{ i18n.ts.enableInfiniteScroll }}</FormSwitch>
<FormSwitch v-model="useReactionPickerForContextMenu" class="_formBlock">{{ i18n.ts.useReactionPickerForContextMenu }}</FormSwitch>
<FormSwitch v-model="disablePagesScript" class="_formBlock">{{ i18n.ts.disablePagesScript }}</FormSwitch>
<FormSelect v-model="serverDisconnectedBehavior" class="_formBlock">
<template #label>{{ i18n.ts.whenServerDisconnected }}</template>
@ -141,7 +140,6 @@ const disableShowingAnimatedImages = computed(defaultStore.makeGetterSetter('dis
const loadRawImages = computed(defaultStore.makeGetterSetter('loadRawImages'));
const imageNewTab = computed(defaultStore.makeGetterSetter('imageNewTab'));
const nsfw = computed(defaultStore.makeGetterSetter('nsfw'));
const disablePagesScript = computed(defaultStore.makeGetterSetter('disablePagesScript'));
const showFixedPostForm = computed(defaultStore.makeGetterSetter('showFixedPostForm'));
const numberOfPageCache = computed(defaultStore.makeGetterSetter('numberOfPageCache'));
const instanceTicker = computed(defaultStore.makeGetterSetter('instanceTicker'));

View file

@ -14,8 +14,7 @@
<script lang="ts" setup>
import { defineAsyncComponent, nextTick, ref } from 'vue';
import { AiScript, parse } from '@syuilo/aiscript';
import { serialize } from '@syuilo/aiscript/built/serializer';
import { Interpreter, Parser, utils } from '@syuilo/aiscript';
import { v4 as uuid } from 'uuid';
import FormTextarea from '@/components/form/textarea.vue';
import FormButton from '@/components/MkButton.vue';
@ -26,23 +25,41 @@ import { unisonReload } from '@/scripts/unison-reload';
import { i18n } from '@/i18n';
import { definePageMetadata } from '@/scripts/page-metadata';
const parser = new Parser();
const code = ref(null);
function installPlugin({ id, meta, ast, token }) {
function installPlugin({ id, meta, src, token }) {
ColdDeviceStorage.set('plugins', ColdDeviceStorage.get('plugins').concat({
...meta,
id,
active: true,
configData: {},
token: token,
ast: ast,
src: src,
}));
}
async function install() {
if (code.value == null) return;
const lv = utils.getLangVersion(code.value);
if (lv == null) {
os.alert({
type: 'error',
text: 'No language version annotation found :(',
});
return;
} else if (lv !== '0.12.0') {
os.alert({
type: 'error',
text: `aiscript version '${lv}' is not supported :(`,
});
return;
}
let ast;
try {
ast = parse(code.value);
ast = parser.parse(code.value);
} catch (err) {
os.alert({
type: 'error',
@ -51,7 +68,7 @@ async function install() {
return;
}
const meta = AiScript.collectMetadata(ast);
const meta = Interpreter.collectMetadata(ast);
if (meta == null) {
os.alert({
type: 'error',
@ -103,7 +120,7 @@ async function install() {
name, version, author, description, permissions, config,
},
token,
ast: serialize(ast),
src: code.value,
});
os.success();

View file

@ -62,7 +62,6 @@ const defaultStoreSaveKeys: (keyof typeof defaultStore['state'])[] = [
'loadRawImages',
'imageNewTab',
'disableShowingAnimatedImages',
'disablePagesScript',
'emojiStyle',
'disableDrawer',
'useBlurEffectForModal',

View file

@ -1,16 +1,17 @@
import { AiScript, utils, values } from '@syuilo/aiscript';
import { deserialize } from '@syuilo/aiscript/built/serializer';
import { jsToVal } from '@syuilo/aiscript/built/interpreter/util';
import { Interpreter, Parser, utils, values } from '@syuilo/aiscript';
import { createAiScriptEnv } from '@/scripts/aiscript/api';
import { inputText } from '@/os';
import { noteActions, notePostInterruptors, noteViewInterruptors, postFormActions, userActions } from '@/store';
const pluginContexts = new Map<string, AiScript>();
const parser = new Parser();
const pluginContexts = new Map<string, Interpreter>();
export function install(plugin) {
// 後方互換性のため
if (plugin.src == null) return;
console.info('Plugin installed:', plugin.name, 'v' + plugin.version);
const aiscript = new AiScript(createPluginEnv({
const aiscript = new Interpreter(createPluginEnv({
plugin: plugin,
storageKey: 'plugins:' + plugin.id,
}), {
@ -32,13 +33,13 @@ export function install(plugin) {
initPlugin({ plugin, aiscript });
aiscript.exec(deserialize(plugin.ast));
aiscript.exec(parser.parse(plugin.src));
}
function createPluginEnv(opts) {
const config = new Map();
for (const [k, v] of Object.entries(opts.plugin.config || {})) {
config.set(k, jsToVal(typeof opts.plugin.configData[k] !== 'undefined' ? opts.plugin.configData[k] : v.default));
config.set(k, utils.jsToVal(typeof opts.plugin.configData[k] !== 'undefined' ? opts.plugin.configData[k] : v.default));
}
return {

View file

@ -1,13 +1,11 @@
import autobind from 'autobind-decorator';
import { PageVar, envVarsDef, Fn, HpmlScope, HpmlError } from '.';
import { version } from '@/config';
import { AiScript, utils, values } from '@syuilo/aiscript';
import { createAiScriptEnv } from '../aiscript/api';
import { markRaw, ref, Ref, unref } from 'vue';
import { collectPageVars } from '../collect-page-vars';
import { initHpmlLib, initAiLib } from './lib';
import * as os from '@/os';
import { markRaw, ref, Ref, unref } from 'vue';
import { Expr, isLiteralValue, Variable } from './expr';
import { PageVar, envVarsDef, Fn, HpmlScope, HpmlError } from '.';
import { version } from '@/config';
import * as os from '@/os';
/**
* Hpml evaluator
@ -16,7 +14,6 @@ export class Hpml {
private variables: Variable[];
private pageVars: PageVar[];
private envVars: Record<keyof typeof envVarsDef, any>;
public aiscript?: AiScript;
public pageVarUpdatedCallback?: values.VFn;
public canvases: Record<string, HTMLCanvasElement> = {};
public vars: Ref<Record<string, any>> = ref({});
@ -24,7 +21,6 @@ export class Hpml {
private opts: {
randomSeed: string; visitor?: any; url?: string;
enableAiScript: boolean;
};
constructor(page: Hpml['page'], opts: Hpml['opts']) {
@ -33,31 +29,6 @@ export class Hpml {
this.pageVars = collectPageVars(this.page.content);
this.opts = opts;
if (this.opts.enableAiScript) {
this.aiscript = markRaw(new AiScript({ ...createAiScriptEnv({
storageKey: 'pages:' + this.page.id,
}), ...initAiLib(this) }, {
in: (q) => {
return new Promise(ok => {
os.inputText({
title: q,
}).then(({ canceled, result: a }) => {
ok(a);
});
});
},
out: (value) => {
console.log(value);
},
log: (type, params) => {
},
}));
this.aiscript.scope.opts.onUpdated = (name, value) => {
this.eval();
};
}
const date = new Date();
this.envVars = {
@ -74,7 +45,7 @@ export class Hpml {
IS_CAT: opts.visitor ? opts.visitor.isCat : false,
SEED: opts.randomSeed ? opts.randomSeed : '',
YMD: `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`,
AISCRIPT_DISABLED: !this.opts.enableAiScript,
AISCRIPT_DISABLED: true,
NULL: null,
};
@ -99,13 +70,6 @@ export class Hpml {
});
}
@autobind
public callAiScript(fn: string) {
try {
if (this.aiscript) this.aiscript.execFn(this.aiscript.scope.get(fn), []);
} catch (err) {}
}
@autobind
public registerCanvas(id: string, canvas: any) {
this.canvases[id] = canvas;
@ -116,9 +80,6 @@ export class Hpml {
const pageVar = this.pageVars.find(v => v.name === name);
if (pageVar !== undefined) {
pageVar.value = value;
if (this.pageVarUpdatedCallback) {
if (this.aiscript) this.aiscript.execFn(this.pageVarUpdatedCallback, [values.STR(name), utils.jsToVal(value)]);
}
} else {
throw new HpmlError(`No such page var '${name}'`);
}
@ -180,18 +141,6 @@ export class Hpml {
return scope.getState(expr.value);
}
if (expr.type === 'aiScriptVar') {
if (this.aiscript) {
try {
return utils.valToJs(this.aiscript.scope.get(expr.value));
} catch (err) {
return null;
}
} else {
return null;
}
}
// Define user function
if (expr.type === 'fn') {
return {

View file

@ -1,9 +1,8 @@
import tinycolor from 'tinycolor2';
import { Hpml } from './evaluator';
import { values, utils } from '@syuilo/aiscript';
import { Fn, HpmlScope } from '.';
import { Expr } from './expr';
import seedrandom from 'seedrandom';
import { Hpml } from './evaluator';
import { Expr } from './expr';
import { Fn, HpmlScope } from '.';
/* TODO: https://www.chartjs.org/docs/latest/configuration/canvas-background.html#color
// https://stackoverflow.com/questions/38493564/chart-area-background-color-chartjs

View file

@ -170,10 +170,6 @@ export const defaultStore = markRaw(new Storage('base', {
where: 'device',
default: false,
},
disablePagesScript: {
where: 'device',
default: false,
},
emojiStyle: {
where: 'device',
default: 'twemoji', // twemoji / fluentEmoji / native

View file

@ -14,7 +14,7 @@
<script lang="ts" setup>
import { onMounted, onUnmounted, ref, watch } from 'vue';
import { AiScript, parse, utils } from '@syuilo/aiscript';
import { Interpreter, Parser, utils } from '@syuilo/aiscript';
import { useWidgetPropsManager, Widget, WidgetComponentEmits, WidgetComponentExpose, WidgetComponentProps } from './widget';
import { GetFormResultType } from '@/scripts/form';
import * as os from '@/os';
@ -52,6 +52,8 @@ const { widgetProps, configure } = useWidgetPropsManager(name,
emit,
);
const parser = new Parser();
const logs = ref<{
id: string;
text: string;
@ -60,7 +62,7 @@ const logs = ref<{
const run = async () => {
logs.value = [];
const aiscript = new AiScript(createAiScriptEnv({
const aiscript = new Interpreter(createAiScriptEnv({
storageKey: 'widget',
token: $i?.token,
}), {
@ -94,7 +96,7 @@ const run = async () => {
let ast;
try {
ast = parse(widgetProps.script);
ast = parser.parse(widgetProps.script);
} catch (err) {
os.alert({
type: 'error',

View file

@ -8,7 +8,7 @@
<script lang="ts" setup>
import { onMounted, onUnmounted, ref, watch } from 'vue';
import { AiScript, parse, utils } from '@syuilo/aiscript';
import { Interpreter, Parser, utils } from '@syuilo/aiscript';
import { useWidgetPropsManager, Widget, WidgetComponentEmits, WidgetComponentExpose, WidgetComponentProps } from './widget';
import { GetFormResultType } from '@/scripts/form';
import * as os from '@/os';
@ -48,8 +48,10 @@ const { widgetProps, configure } = useWidgetPropsManager(name,
emit,
);
const parser = new Parser();
const run = async () => {
const aiscript = new AiScript(createAiScriptEnv({
const aiscript = new Interpreter(createAiScriptEnv({
storageKey: 'widget',
token: $i?.token,
}), {
@ -72,7 +74,7 @@ const run = async () => {
let ast;
try {
ast = parse(widgetProps.script);
ast = parser.parse(widgetProps.script);
} catch (err) {
os.alert({
type: 'error',

View file

@ -1973,16 +1973,15 @@ __metadata:
languageName: node
linkType: hard
"@syuilo/aiscript@npm:0.11.1":
version: 0.11.1
resolution: "@syuilo/aiscript@npm:0.11.1"
"@syuilo/aiscript@npm:0.12.0":
version: 0.12.0
resolution: "@syuilo/aiscript@npm:0.12.0"
dependencies:
autobind-decorator: 2.4.0
chalk: 4.0.0
seedrandom: 3.0.5
stringz: 2.1.0
uuid: 7.0.3
checksum: c5e15045abdf1393f1da9c58f25dd301bd2b6c5808f2ca58334e5d488c227b189ec5200ec86326ed381380a9ff76dbc05fd30882f562383be76b83dc4fabb11b
uuid: 8.3.2
checksum: 82b52a6c602a8c3090b9457a0e9de99898b03cd8f054855b2f57439534257ef2780013a53eaeeef68c9893d96d3ec02fc6d0ede56396c2bcf054cf43b2297b67
languageName: node
linkType: hard
@ -4136,7 +4135,6 @@ __metadata:
"@sinonjs/fake-timers": 10.0.2
"@swc/core": 1.3.24
"@swc/jest": 0.2.24
"@syuilo/aiscript": 0.11.1
"@tensorflow/tfjs": ^4.1.0
"@tensorflow/tfjs-node": 4.1.0
"@types/accepts": 1.3.5
@ -4869,16 +4867,6 @@ __metadata:
languageName: node
linkType: hard
"chalk@npm:4.0.0":
version: 4.0.0
resolution: "chalk@npm:4.0.0"
dependencies:
ansi-styles: ^4.1.0
supports-color: ^7.1.0
checksum: a9580afd4af8ffa8add8edb565d1b3f77efb880c5d887d3bb72a948d1bfb2bc764db2ceb6d62a60103aa384f3da71eb1969c7f68e886055e0a3438550e809cde
languageName: node
linkType: hard
"chalk@npm:5.2.0":
version: 5.2.0
resolution: "chalk@npm:5.2.0"
@ -8038,7 +8026,7 @@ __metadata:
"@rollup/plugin-alias": 4.0.2
"@rollup/plugin-json": 6.0.0
"@rollup/pluginutils": 5.0.2
"@syuilo/aiscript": 0.11.1
"@syuilo/aiscript": 0.12.0
"@tabler/icons": ^1.118.0
"@types/escape-regexp": 0.0.1
"@types/glob": 8.0.0
@ -16915,15 +16903,6 @@ __metadata:
languageName: node
linkType: hard
"uuid@npm:7.0.3":
version: 7.0.3
resolution: "uuid@npm:7.0.3"
bin:
uuid: dist/bin/uuid
checksum: f5b7b5cc28accac68d5c083fd51cca64896639ebd4cca88c6cfb363801aaa83aa439c86dfc8446ea250a7a98d17afd2ad9e88d9d4958c79a412eccb93bae29de
languageName: node
linkType: hard
"uuid@npm:8.0.0":
version: 8.0.0
resolution: "uuid@npm:8.0.0"
@ -16933,6 +16912,15 @@ __metadata:
languageName: node
linkType: hard
"uuid@npm:8.3.2, uuid@npm:^8.3.0, uuid@npm:^8.3.2":
version: 8.3.2
resolution: "uuid@npm:8.3.2"
bin:
uuid: dist/bin/uuid
checksum: 5575a8a75c13120e2f10e6ddc801b2c7ed7d8f3c8ac22c7ed0c7b2ba6383ec0abda88c905085d630e251719e0777045ae3236f04c812184b7c765f63a70e58df
languageName: node
linkType: hard
"uuid@npm:9.0.0":
version: 9.0.0
resolution: "uuid@npm:9.0.0"
@ -16951,15 +16939,6 @@ __metadata:
languageName: node
linkType: hard
"uuid@npm:^8.3.0, uuid@npm:^8.3.2":
version: 8.3.2
resolution: "uuid@npm:8.3.2"
bin:
uuid: dist/bin/uuid
checksum: 5575a8a75c13120e2f10e6ddc801b2c7ed7d8f3c8ac22c7ed0c7b2ba6383ec0abda88c905085d630e251719e0777045ae3236f04c812184b7c765f63a70e58df
languageName: node
linkType: hard
"v8-to-istanbul@npm:^9.0.1":
version: 9.0.1
resolution: "v8-to-istanbul@npm:9.0.1"