summaryrefslogtreecommitdiff
path: root/packages/frontend/src/scripts/aiscript
diff options
context:
space:
mode:
Diffstat (limited to 'packages/frontend/src/scripts/aiscript')
-rw-r--r--packages/frontend/src/scripts/aiscript/api.ts28
-rw-r--r--packages/frontend/src/scripts/aiscript/ui.ts63
2 files changed, 69 insertions, 22 deletions
diff --git a/packages/frontend/src/scripts/aiscript/api.ts b/packages/frontend/src/scripts/aiscript/api.ts
index c8b90b4fd7..9f60e52cea 100644
--- a/packages/frontend/src/scripts/aiscript/api.ts
+++ b/packages/frontend/src/scripts/aiscript/api.ts
@@ -1,16 +1,22 @@
+/*
+ * SPDX-FileCopyrightText: syuilo and other misskey contributors
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
import { utils, values } from '@syuilo/aiscript';
-import * as os from '@/os';
-import { $i } from '@/account';
-import { miLocalStorage } from '@/local-storage';
-import { customEmojis } from '@/custom-emojis';
+import * as os from '@/os.js';
+import { $i } from '@/account.js';
+import { miLocalStorage } from '@/local-storage.js';
+import { customEmojis } from '@/custom-emojis.js';
+import { lang } from '@/config.js';
export function createAiScriptEnv(opts) {
- let apiRequests = 0;
return {
USER_ID: $i ? values.STR($i.id) : values.NULL,
USER_NAME: $i ? values.STR($i.name) : values.NULL,
USER_USERNAME: $i ? values.STR($i.username) : values.NULL,
CUSTOM_EMOJIS: utils.jsToVal(customEmojis.value),
+ LOCALE: values.STR(lang),
'Mk:dialog': values.FN_NATIVE(async ([title, text, type]) => {
await os.alert({
type: type ? type.value : 'info',
@@ -28,15 +34,19 @@ export function createAiScriptEnv(opts) {
return confirm.canceled ? values.FALSE : values.TRUE;
}),
'Mk:api': values.FN_NATIVE(async ([ep, param, token]) => {
+ utils.assertString(ep);
+ if (ep.value.includes('://')) throw new Error('invalid endpoint');
if (token) {
utils.assertString(token);
// バグがあればundefinedもあり得るため念のため
if (typeof token.value !== 'string') throw new Error('invalid token');
}
- apiRequests++;
- if (apiRequests > 16) return values.NULL;
- const res = await os.api(ep.value, utils.valToJs(param), token ? token.value : (opts.token ?? null));
- return utils.jsToVal(res);
+ const actualToken: string|null = token?.value ?? opts.token ?? null;
+ return os.api(ep.value, utils.valToJs(param), actualToken).then(res => {
+ return utils.jsToVal(res);
+ }, err => {
+ return values.ERROR('request_failed', utils.jsToVal(err));
+ });
}),
'Mk:save': values.FN_NATIVE(([key, value]) => {
utils.assertString(key);
diff --git a/packages/frontend/src/scripts/aiscript/ui.ts b/packages/frontend/src/scripts/aiscript/ui.ts
index c26ae5a4df..d326b956e8 100644
--- a/packages/frontend/src/scripts/aiscript/ui.ts
+++ b/packages/frontend/src/scripts/aiscript/ui.ts
@@ -1,3 +1,8 @@
+/*
+ * SPDX-FileCopyrightText: syuilo and other misskey contributors
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
import { utils, values } from '@syuilo/aiscript';
import { v4 as uuid } from 'uuid';
import { ref, Ref } from 'vue';
@@ -119,7 +124,14 @@ export type AsUiPostFormButton = AsUiComponentBase & {
};
};
-export type AsUiComponent = AsUiRoot | AsUiContainer | AsUiText | AsUiMfm | AsUiButton | AsUiButtons | AsUiSwitch | AsUiTextarea | AsUiTextInput | AsUiNumberInput | AsUiSelect | AsUiFolder | AsUiPostFormButton;
+export type AsUiPostForm = AsUiComponentBase & {
+ type: 'postForm';
+ form?: {
+ text: string;
+ };
+};
+
+export type AsUiComponent = AsUiRoot | AsUiContainer | AsUiText | AsUiMfm | AsUiButton | AsUiButtons | AsUiSwitch | AsUiTextarea | AsUiTextInput | AsUiNumberInput | AsUiSelect | AsUiFolder | AsUiPostFormButton | AsUiPostForm;
export function patch(id: string, def: values.Value, call: (fn: values.VFn, args: values.Value[]) => Promise<values.Value>) {
// TODO
@@ -457,6 +469,27 @@ function getPostFormButtonOptions(def: values.Value | undefined, call: (fn: valu
};
}
+function getPostFormOptions(def: values.Value | undefined, call: (fn: values.VFn, args: values.Value[]) => Promise<values.Value>): Omit<AsUiPostForm, 'id' | 'type'> {
+ utils.assertObject(def);
+
+ const form = def.value.get('form');
+ if (form) utils.assertObject(form);
+
+ const getForm = () => {
+ const text = form!.value.get('text');
+ utils.assertString(text);
+ return {
+ text: text.value,
+ };
+ };
+
+ return {
+ form: form ? getForm() : {
+ text: '',
+ },
+ };
+}
+
export function registerAsUiLib(components: Ref<AsUiComponent>[], done: (root: Ref<AsUiRoot>) => void) {
const instances = {};
@@ -518,51 +551,55 @@ export function registerAsUiLib(components: Ref<AsUiComponent>[], done: (root: R
}),
'Ui:C:container': values.FN_NATIVE(([def, id], opts) => {
- return createComponentInstance('container', def, id, getContainerOptions, opts.call);
+ return createComponentInstance('container', def, id, getContainerOptions, opts.topCall);
}),
'Ui:C:text': values.FN_NATIVE(([def, id], opts) => {
- return createComponentInstance('text', def, id, getTextOptions, opts.call);
+ return createComponentInstance('text', def, id, getTextOptions, opts.topCall);
}),
'Ui:C:mfm': values.FN_NATIVE(([def, id], opts) => {
- return createComponentInstance('mfm', def, id, getMfmOptions, opts.call);
+ return createComponentInstance('mfm', def, id, getMfmOptions, opts.topCall);
}),
'Ui:C:textarea': values.FN_NATIVE(([def, id], opts) => {
- return createComponentInstance('textarea', def, id, getTextareaOptions, opts.call);
+ return createComponentInstance('textarea', def, id, getTextareaOptions, opts.topCall);
}),
'Ui:C:textInput': values.FN_NATIVE(([def, id], opts) => {
- return createComponentInstance('textInput', def, id, getTextInputOptions, opts.call);
+ return createComponentInstance('textInput', def, id, getTextInputOptions, opts.topCall);
}),
'Ui:C:numberInput': values.FN_NATIVE(([def, id], opts) => {
- return createComponentInstance('numberInput', def, id, getNumberInputOptions, opts.call);
+ return createComponentInstance('numberInput', def, id, getNumberInputOptions, opts.topCall);
}),
'Ui:C:button': values.FN_NATIVE(([def, id], opts) => {
- return createComponentInstance('button', def, id, getButtonOptions, opts.call);
+ return createComponentInstance('button', def, id, getButtonOptions, opts.topCall);
}),
'Ui:C:buttons': values.FN_NATIVE(([def, id], opts) => {
- return createComponentInstance('buttons', def, id, getButtonsOptions, opts.call);
+ return createComponentInstance('buttons', def, id, getButtonsOptions, opts.topCall);
}),
'Ui:C:switch': values.FN_NATIVE(([def, id], opts) => {
- return createComponentInstance('switch', def, id, getSwitchOptions, opts.call);
+ return createComponentInstance('switch', def, id, getSwitchOptions, opts.topCall);
}),
'Ui:C:select': values.FN_NATIVE(([def, id], opts) => {
- return createComponentInstance('select', def, id, getSelectOptions, opts.call);
+ return createComponentInstance('select', def, id, getSelectOptions, opts.topCall);
}),
'Ui:C:folder': values.FN_NATIVE(([def, id], opts) => {
- return createComponentInstance('folder', def, id, getFolderOptions, opts.call);
+ return createComponentInstance('folder', def, id, getFolderOptions, opts.topCall);
}),
'Ui:C:postFormButton': values.FN_NATIVE(([def, id], opts) => {
- return createComponentInstance('postFormButton', def, id, getPostFormButtonOptions, opts.call);
+ return createComponentInstance('postFormButton', def, id, getPostFormButtonOptions, opts.topCall);
+ }),
+
+ 'Ui:C:postForm': values.FN_NATIVE(([def, id], opts) => {
+ return createComponentInstance('postForm', def, id, getPostFormOptions, opts.topCall);
}),
};
}