summaryrefslogtreecommitdiff
path: root/packages/frontend/src/aiscript/api.ts
blob: 3a476787fef8ba8ca7905d341baf6571d041765a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { errors, utils, values } from '@syuilo/aiscript';
import * as Misskey from 'misskey-js';
import { url, lang } from '@@/js/config.js';
import { assertStringAndIsIn } from './common.js';
import * as os from '@/os.js';
import { misskeyApi } from '@/utility/misskey-api.js';
import { $i } from '@/i.js';
import { miLocalStorage } from '@/local-storage.js';
import { customEmojis } from '@/custom-emojis.js';

const DIALOG_TYPES = [
	'error',
	'info',
	'success',
	'warning',
	'waiting',
	'question',
] as const;

export function aiScriptReadline(q: string): Promise<string> {
	return new Promise(ok => {
		os.inputText({
			title: q,
		}).then(({ result: a }) => {
			ok(a ?? '');
		});
	});
}

export function createAiScriptEnv(opts: { storageKey: string, token?: string }) {
	return {
		USER_ID: $i ? values.STR($i.id) : values.NULL,
		USER_NAME: $i?.name ? 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),
		SERVER_URL: values.STR(url),
		'Mk:dialog': values.FN_NATIVE(async ([_title, _text, _type]) => {
			let title: string | undefined = undefined;
			let text: string | undefined = undefined;
			let type: typeof DIALOG_TYPES[number] = 'info';

			if (_title != null) {
				if (utils.isString(_title)) {
					title = _title.value;
				} else {
					utils.assertNull(_title);
				}
			}

			if (_text != null) {
				if (utils.isString(_text)) {
					text = _text.value;
				} else {
					utils.assertNull(_text);
				}
			}

			if (_type != null) {
				if (utils.isString(_type)) {
					assertStringAndIsIn(_type, DIALOG_TYPES);
					type = _type.value;
				} else {
					utils.assertNull(_type);
				}
			}

			await os.alert({
				type,
				title,
				text,
			});
			return values.NULL;
		}),
		'Mk:confirm': values.FN_NATIVE(async ([_title, _text, _type]) => {
			let title: string | undefined = undefined;
			let text: string | undefined = undefined;
			let type: typeof DIALOG_TYPES[number] = 'question';

			if (_title != null) {
				if (utils.isString(_title)) {
					title = _title.value;
				} else {
					utils.assertNull(_title);
				}
			}

			if (_text != null) {
				if (utils.isString(_text)) {
					text = _text.value;
				} else {
					utils.assertNull(_text);
				}
			}

			if (_type != null) {
				if (utils.isString(_type)) {
					assertStringAndIsIn(_type, DIALOG_TYPES);
					type = _type.value;
				} else {
					utils.assertNull(_type);
				}
			}

			const confirm = await os.confirm({
				type,
				title,
				text,
			});
			return confirm.canceled ? values.FALSE : values.TRUE;
		}),
		'Mk:toast': values.FN_NATIVE(([text]) => {
			utils.assertString(text);
			os.toast(text.value);
			return values.NULL;
		}),
		'Mk:api': values.FN_NATIVE(async ([ep, param, token]) => {
			utils.assertString(ep);
			if (ep.value.includes('://') || ep.value.includes('..')) {
				throw new errors.AiScriptRuntimeError('invalid endpoint');
			}

			let actualToken: string | null = null;
			if (token != null && !utils.isNull(token)) {
				utils.assertString(token);
				// バグがあればundefinedもあり得るため念のため
				if (typeof token.value !== 'string') throw new errors.AiScriptRuntimeError('invalid token');
				actualToken = token.value;
			}

			if (actualToken == null) {
				actualToken = opts.token ?? null;
			}

			if (param == null) {
				throw new errors.AiScriptRuntimeError('expected param');
			}

			utils.assertObject(param);
			return misskeyApi(ep.value as keyof Misskey.Endpoints, utils.valToJs(param) as object, actualToken).then(res => {
				return utils.jsToVal(res);
			}, err => {
				return values.ERROR('request_failed', utils.jsToVal(err));
			});
		}),
		/* セキュリティ上の問題があるため無効化
		'Mk:apiExternal': values.FN_NATIVE(async ([host, ep, param, token]) => {
			utils.assertString(host);
			utils.assertString(ep);
			if (token) utils.assertString(token);
			return os.apiExternal(host.value, ep.value, utils.valToJs(param), token?.value).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);
			utils.expectAny(value);
			miLocalStorage.setItem(`aiscript:${opts.storageKey}:${key.value}`, JSON.stringify(utils.valToJs(value)));
			return values.NULL;
		}),
		'Mk:load': values.FN_NATIVE(([key]) => {
			utils.assertString(key);
			return utils.jsToVal(miLocalStorage.getItemAsJson(`aiscript:${opts.storageKey}:${key.value}`) ?? null);
		}),
		'Mk:remove': values.FN_NATIVE(([key]) => {
			utils.assertString(key);
			miLocalStorage.removeItem(`aiscript:${opts.storageKey}:${key.value}`);
			return values.NULL;
		}),
		'Mk:url': values.FN_NATIVE(() => {
			return values.STR(window.location.href);
		}),
		'Mk:nyaize': values.FN_NATIVE(([text]) => {
			utils.assertString(text);
			return values.STR(Misskey.nyaize(text.value));
		}),
	};
}