@maaxyz/maa-node 4.5.6 → 5.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.d.ts +3 -0
- package/dist/controller.d.ts +8 -8
- package/dist/index-client.d.ts +0 -1
- package/dist/index-client.js +119 -172
- package/dist/index-server.d.ts +0 -1
- package/dist/index-server.js +106 -169
- package/dist/maa.d.ts +37 -33
- package/dist/resource.d.ts +6 -5
- package/dist/tasker.d.ts +13 -6
- package/dist/types.d.ts +0 -1
- package/package.json +7 -7
- package/src/client.ts +15 -0
- package/src/controller.ts +35 -49
- package/src/index-client.ts +0 -1
- package/src/index-server.ts +0 -1
- package/src/maa.d.ts +37 -33
- package/src/resource.ts +27 -18
- package/src/tasker.ts +66 -26
- package/src/types.ts +0 -2
- package/dist/pi.d.ts +0 -9
- package/dist/utils.d.ts +0 -5
- package/src/pi.ts +0 -66
- package/src/utils.ts +0 -40
package/src/client.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { ControllerBase } from './controller'
|
|
1
2
|
import * as maa from './maa'
|
|
2
3
|
import { Resource } from './resource'
|
|
4
|
+
import { Tasker } from './tasker'
|
|
3
5
|
|
|
4
6
|
export class AgentClient {
|
|
5
7
|
handle: maa.AgentClientHandle
|
|
@@ -26,6 +28,19 @@ export class AgentClient {
|
|
|
26
28
|
}
|
|
27
29
|
}
|
|
28
30
|
|
|
31
|
+
register_sink(tasker?: Tasker, resource?: Resource, controller?: ControllerBase) {
|
|
32
|
+
if (
|
|
33
|
+
!maa.agent_client_register_sink(
|
|
34
|
+
this.handle,
|
|
35
|
+
tasker?.handle ?? null,
|
|
36
|
+
resource?.handle ?? null,
|
|
37
|
+
controller?.handle ?? null
|
|
38
|
+
)
|
|
39
|
+
) {
|
|
40
|
+
throw 'AgentClient register sink failed'
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
29
44
|
async connect() {
|
|
30
45
|
if (!(await maa.agent_client_connect(this.handle))) {
|
|
31
46
|
throw 'AgentClient connect failed'
|
package/src/controller.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import path from 'path'
|
|
2
2
|
|
|
3
3
|
import { Job, JobSource } from './job'
|
|
4
|
-
import maa
|
|
5
|
-
import { ChainNotifyType } from './types'
|
|
6
|
-
import { chain_notify_impl } from './utils'
|
|
4
|
+
import maa from './maa'
|
|
7
5
|
|
|
8
6
|
class ImageJob extends Job<maa.CtrlId, JobSource<maa.CtrlId>> {
|
|
9
7
|
#ctrl: ControllerBase
|
|
@@ -122,20 +120,13 @@ export class ControllerBase {
|
|
|
122
120
|
handle: maa.ControllerHandle
|
|
123
121
|
#source: JobSource<maa.CtrlId>
|
|
124
122
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
chain_parsed_notify(
|
|
130
|
-
cb: (msg: ControllerNotify) => maa.MaybePromise<void>,
|
|
131
|
-
order: ChainNotifyType = 'after'
|
|
132
|
-
) {
|
|
133
|
-
this.chain_notify((msg: string, details: string) => {
|
|
134
|
-
return cb({
|
|
123
|
+
static wrapSink(cb: (ctrl: ControllerBase, msg: ControllerNotify) => maa.MaybePromise<void>) {
|
|
124
|
+
return ((ctrl: ControllerBase, msg: string, details: string) => {
|
|
125
|
+
return cb(ctrl, {
|
|
135
126
|
msg: msg.replace(/^Controller\./, '') as any,
|
|
136
127
|
...JSON.parse(details)
|
|
137
128
|
})
|
|
138
|
-
}
|
|
129
|
+
}) satisfies maa.EventCallbackWithHandle<ControllerBase>
|
|
139
130
|
}
|
|
140
131
|
|
|
141
132
|
constructor(handle: maa.ControllerHandle) {
|
|
@@ -150,6 +141,28 @@ export class ControllerBase {
|
|
|
150
141
|
maa.controller_destroy(this.handle)
|
|
151
142
|
}
|
|
152
143
|
|
|
144
|
+
add_sink(cb: maa.EventCallbackWithHandle<ControllerBase>) {
|
|
145
|
+
const ws = new WeakRef(this)
|
|
146
|
+
return maa.controller_add_sink(this.handle, (msg: string, details: string) => {
|
|
147
|
+
const ctrl = ws.deref()
|
|
148
|
+
if (ctrl) {
|
|
149
|
+
cb(ctrl, msg, details)
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
add_wrapped_sink(cb: (ctrl: ControllerBase, msg: ControllerNotify) => maa.MaybePromise<void>) {
|
|
155
|
+
return this.add_sink(ControllerBase.wrapSink(cb))
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
remove_sink(sink_id: maa.SinkId) {
|
|
159
|
+
maa.controller_remove_sink(this.handle, sink_id)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
clear_sinks() {
|
|
163
|
+
maa.controller_clear_sinks(this.handle)
|
|
164
|
+
}
|
|
165
|
+
|
|
153
166
|
set screenshot_target_long_side(value: number) {
|
|
154
167
|
if (!maa.controller_set_option_screenshot_target_long_side(this.handle, value)) {
|
|
155
168
|
throw 'Controller set screenshot_target_long_side failed'
|
|
@@ -251,23 +264,18 @@ export class AdbController extends ControllerBase {
|
|
|
251
264
|
config: string,
|
|
252
265
|
agent?: string
|
|
253
266
|
) {
|
|
254
|
-
let ws: WeakRef<this>
|
|
255
267
|
const h = maa.adb_controller_create(
|
|
256
268
|
adb_path,
|
|
257
269
|
address,
|
|
258
270
|
screencap_methods,
|
|
259
271
|
input_methods,
|
|
260
272
|
config,
|
|
261
|
-
agent ?? AdbController.agent_path()
|
|
262
|
-
(message, details_json) => {
|
|
263
|
-
return ws.deref()?.notify(message, details_json)
|
|
264
|
-
}
|
|
273
|
+
agent ?? AdbController.agent_path()
|
|
265
274
|
)
|
|
266
275
|
if (!h) {
|
|
267
276
|
throw 'AdbController create failed'
|
|
268
277
|
}
|
|
269
278
|
super(h)
|
|
270
|
-
ws = new WeakRef(this)
|
|
271
279
|
}
|
|
272
280
|
|
|
273
281
|
static agent_path() {
|
|
@@ -285,20 +293,15 @@ export class Win32Controller extends ControllerBase {
|
|
|
285
293
|
screencap_methods: maa.ScreencapOrInputMethods,
|
|
286
294
|
input_methods: maa.ScreencapOrInputMethods
|
|
287
295
|
) {
|
|
288
|
-
let ws: WeakRef<this>
|
|
289
296
|
const h = maa.win32_controller_create(
|
|
290
297
|
hwnd ?? ('0' as maa.DesktopHandle),
|
|
291
298
|
screencap_methods,
|
|
292
|
-
input_methods
|
|
293
|
-
(message, details_json) => {
|
|
294
|
-
return ws.deref()?.notify(message, details_json)
|
|
295
|
-
}
|
|
299
|
+
input_methods
|
|
296
300
|
)
|
|
297
301
|
if (!h) {
|
|
298
302
|
throw 'Win32Controller create failed'
|
|
299
303
|
}
|
|
300
304
|
super(h)
|
|
301
|
-
ws = new WeakRef(this)
|
|
302
305
|
}
|
|
303
306
|
|
|
304
307
|
static find() {
|
|
@@ -308,21 +311,11 @@ export class Win32Controller extends ControllerBase {
|
|
|
308
311
|
|
|
309
312
|
export class DbgController extends ControllerBase {
|
|
310
313
|
constructor(read_path: string, write_path: string, type: maa.Uint64, config: string) {
|
|
311
|
-
|
|
312
|
-
const h = maa.dbg_controller_create(
|
|
313
|
-
read_path,
|
|
314
|
-
write_path,
|
|
315
|
-
type,
|
|
316
|
-
config,
|
|
317
|
-
(message, details_json) => {
|
|
318
|
-
return ws.deref()?.notify(message, details_json)
|
|
319
|
-
}
|
|
320
|
-
)
|
|
314
|
+
const h = maa.dbg_controller_create(read_path, write_path, type, config)
|
|
321
315
|
if (!h) {
|
|
322
316
|
throw 'DbgController create failed'
|
|
323
317
|
}
|
|
324
318
|
super(h)
|
|
325
|
-
ws = new WeakRef(this)
|
|
326
319
|
}
|
|
327
320
|
}
|
|
328
321
|
|
|
@@ -331,7 +324,7 @@ export abstract class CustomControllerActor {
|
|
|
331
324
|
abstract request_uuid(): maa.MaybePromise<string | null>
|
|
332
325
|
abstract start_app(intent: string): maa.MaybePromise<boolean>
|
|
333
326
|
abstract stop_app(intent: string): maa.MaybePromise<boolean>
|
|
334
|
-
abstract screencap(): maa.MaybePromise<ImageData | null>
|
|
327
|
+
abstract screencap(): maa.MaybePromise<maa.ImageData | null>
|
|
335
328
|
abstract click(x: number, y: number): maa.MaybePromise<boolean>
|
|
336
329
|
abstract swipe(
|
|
337
330
|
x1: number,
|
|
@@ -372,7 +365,7 @@ export class CustomControllerActorDefaultImpl extends CustomControllerActor {
|
|
|
372
365
|
stop_app(intent: string): maa.MaybePromise<boolean> {
|
|
373
366
|
return false
|
|
374
367
|
}
|
|
375
|
-
screencap(): maa.MaybePromise<ImageData | null> {
|
|
368
|
+
screencap(): maa.MaybePromise<maa.ImageData | null> {
|
|
376
369
|
return null
|
|
377
370
|
}
|
|
378
371
|
click(x: number, y: number): maa.MaybePromise<boolean> {
|
|
@@ -412,19 +405,12 @@ export class CustomControllerActorDefaultImpl extends CustomControllerActor {
|
|
|
412
405
|
|
|
413
406
|
export class CustomController extends ControllerBase {
|
|
414
407
|
constructor(actor: CustomControllerActor) {
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
return (actor[action] as any)(...param)
|
|
419
|
-
},
|
|
420
|
-
(message, details_json) => {
|
|
421
|
-
return ws.deref()?.notify(message, details_json)
|
|
422
|
-
}
|
|
423
|
-
)
|
|
408
|
+
const h = maa.custom_controller_create((action, ...param) => {
|
|
409
|
+
return (actor[action] as any)(...param)
|
|
410
|
+
})
|
|
424
411
|
if (!h) {
|
|
425
412
|
throw 'CustomController create failed'
|
|
426
413
|
}
|
|
427
414
|
super(h)
|
|
428
|
-
ws = new WeakRef(this)
|
|
429
415
|
}
|
|
430
416
|
}
|
package/src/index-client.ts
CHANGED
package/src/index-server.ts
CHANGED
package/src/maa.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ export type CtrlId = Id & { __brand: 'CtrlId' }
|
|
|
14
14
|
export type TaskId = Id & { __brand: 'TaskId' }
|
|
15
15
|
export type RecoId = Id & { __brand: 'RecoId' }
|
|
16
16
|
export type NodeId = Id & { __brand: 'NodeId' }
|
|
17
|
+
export type SinkId = Id & { __brand: 'SinkId' }
|
|
17
18
|
|
|
18
19
|
export type Status = number & { __brand: 'Status' }
|
|
19
20
|
export type LoggingLevel = number & { __brand: 'LoggingLevel' }
|
|
@@ -33,7 +34,12 @@ export type FlatRect = [x: number, y: number, width: number, height: number]
|
|
|
33
34
|
|
|
34
35
|
type MaybePromise<T> = T | Promise<T>
|
|
35
36
|
|
|
36
|
-
export type
|
|
37
|
+
export type EventCallback = (message: string, details_json: string) => MaybePromise<void>
|
|
38
|
+
export type EventCallbackWithHandle<T> = (
|
|
39
|
+
handle: T,
|
|
40
|
+
message: string,
|
|
41
|
+
details_json: string
|
|
42
|
+
) => MaybePromise<void>
|
|
37
43
|
export type CustomRecognitionCallback = (
|
|
38
44
|
context: ContextHandle,
|
|
39
45
|
task_id: TaskId,
|
|
@@ -119,27 +125,29 @@ export declare function adb_controller_create(
|
|
|
119
125
|
screencap_methods: ScreencapOrInputMethods,
|
|
120
126
|
input_methods: ScreencapOrInputMethods,
|
|
121
127
|
config: string,
|
|
122
|
-
agent_path: string
|
|
123
|
-
callback: NotificationCallback | null
|
|
128
|
+
agent_path: string
|
|
124
129
|
): ControllerHandle | null
|
|
125
130
|
export declare function win32_controller_create(
|
|
126
131
|
handle: DesktopHandle,
|
|
127
132
|
screencap_methods: ScreencapOrInputMethods,
|
|
128
|
-
input_methods: ScreencapOrInputMethods
|
|
129
|
-
callback: NotificationCallback | null
|
|
133
|
+
input_methods: ScreencapOrInputMethods
|
|
130
134
|
): ControllerHandle | null
|
|
131
135
|
export declare function custom_controller_create(
|
|
132
|
-
custom_callback: CustomControllerCallback
|
|
133
|
-
callback: NotificationCallback | null
|
|
136
|
+
custom_callback: CustomControllerCallback
|
|
134
137
|
): ControllerHandle | null
|
|
135
138
|
export declare function dbg_controller_create(
|
|
136
139
|
read_path: string,
|
|
137
140
|
write_path: string,
|
|
138
141
|
type: Uint64,
|
|
139
|
-
config: string
|
|
140
|
-
callback: NotificationCallback | null
|
|
142
|
+
config: string
|
|
141
143
|
): ControllerHandle | null
|
|
142
144
|
export declare function controller_destroy(handle: ControllerHandle): void
|
|
145
|
+
export declare function controller_add_sink(
|
|
146
|
+
handle: ControllerHandle,
|
|
147
|
+
callback: EventCallback
|
|
148
|
+
): SinkId
|
|
149
|
+
export declare function controller_remove_sink(handle: ControllerHandle, sink_id: SinkId): void
|
|
150
|
+
export declare function controller_clear_sinks(handle: ControllerHandle): void
|
|
143
151
|
export declare function controller_set_option_screenshot_target_long_side(
|
|
144
152
|
handle: ControllerHandle,
|
|
145
153
|
value: number
|
|
@@ -196,10 +204,11 @@ export declare function controller_get_uuid(handle: ControllerHandle): string |
|
|
|
196
204
|
|
|
197
205
|
// resource.cpp
|
|
198
206
|
|
|
199
|
-
export declare function resource_create(
|
|
200
|
-
callback: NotificationCallback | null
|
|
201
|
-
): ResourceHandle | null
|
|
207
|
+
export declare function resource_create(): ResourceHandle | null
|
|
202
208
|
export declare function resource_destroy(handle: ResourceHandle): void
|
|
209
|
+
export declare function resource_add_sink(handle: ResourceHandle, callback: EventCallback): SinkId
|
|
210
|
+
export declare function resource_remove_sink(handle: ResourceHandle, sink_id: SinkId): void
|
|
211
|
+
export declare function resource_clear_sinks(handle: ResourceHandle): void
|
|
203
212
|
export declare function resource_set_option_inference_device(
|
|
204
213
|
handle: ResourceHandle,
|
|
205
214
|
id: InferenceDevice | number
|
|
@@ -251,7 +260,16 @@ export declare function resource_get_node_list(handle: ResourceHandle): string[]
|
|
|
251
260
|
|
|
252
261
|
// tasker.cpp
|
|
253
262
|
|
|
254
|
-
export declare function tasker_create(
|
|
263
|
+
export declare function tasker_create(): TaskerHandle | null
|
|
264
|
+
export declare function tasker_add_sink(handle: TaskerHandle, callback: EventCallback): SinkId
|
|
265
|
+
export declare function tasker_remove_sink(handle: TaskerHandle, sink_id: SinkId): void
|
|
266
|
+
export declare function tasker_clear_sinks(handle: TaskerHandle): void
|
|
267
|
+
export declare function tasker_add_context_sink(
|
|
268
|
+
handle: TaskerHandle,
|
|
269
|
+
callback: EventCallbackWithHandle<ContextHandle>
|
|
270
|
+
): SinkId
|
|
271
|
+
export declare function tasker_remove_context_sink(handle: TaskerHandle, sink_id: SinkId): void
|
|
272
|
+
export declare function tasker_clear_context_sinks(handle: TaskerHandle): void
|
|
255
273
|
export declare function tasker_destroy(handle: TaskerHandle): void
|
|
256
274
|
export declare function tasker_bind_resource(
|
|
257
275
|
handle: TaskerHandle,
|
|
@@ -333,26 +351,6 @@ export declare function set_global_option_save_draw(value: boolean): boolean
|
|
|
333
351
|
export declare function set_global_option_stdout_level(value: LoggingLevel): boolean
|
|
334
352
|
export declare function set_global_option_debug_mode(value: boolean): boolean
|
|
335
353
|
|
|
336
|
-
// pi.cpp
|
|
337
|
-
|
|
338
|
-
export declare function pi_register_custom_recognizer(
|
|
339
|
-
id: Id,
|
|
340
|
-
name: string,
|
|
341
|
-
recognizer: CustomRecognitionCallback
|
|
342
|
-
): void
|
|
343
|
-
export declare function pi_register_custom_action(
|
|
344
|
-
id: Id,
|
|
345
|
-
name: string,
|
|
346
|
-
action: CustomActionCallback
|
|
347
|
-
): void
|
|
348
|
-
export declare function pi_run_cli(
|
|
349
|
-
id: Id,
|
|
350
|
-
resource_path: string,
|
|
351
|
-
user_path: string,
|
|
352
|
-
directly: boolean,
|
|
353
|
-
callback: NotificationCallback | null
|
|
354
|
-
): Promise<boolean>
|
|
355
|
-
|
|
356
354
|
export declare const Status: Record<
|
|
357
355
|
'Invalid' | 'Pending' | 'Running' | 'Succeeded' | 'Failed',
|
|
358
356
|
Status
|
|
@@ -400,6 +398,12 @@ export declare function agent_client_bind_resource(
|
|
|
400
398
|
handle: AgentClientHandle,
|
|
401
399
|
resource: ResourceHandle
|
|
402
400
|
): boolean
|
|
401
|
+
export declare function agent_client_register_sink(
|
|
402
|
+
handle: AgentClientHandle,
|
|
403
|
+
tasker: TaskerHandle | null,
|
|
404
|
+
resource: ResourceHandle | null,
|
|
405
|
+
controller: ControllerHandle | null
|
|
406
|
+
): boolean
|
|
403
407
|
export declare function agent_client_connect(handle: AgentClientHandle): Promise<boolean>
|
|
404
408
|
export declare function agent_client_disconnect(handle: AgentClientHandle): boolean
|
|
405
409
|
export declare function agent_client_connected(handle: AgentClientHandle): boolean
|
package/src/resource.ts
CHANGED
|
@@ -3,13 +3,11 @@ import { Job, JobSource } from './job'
|
|
|
3
3
|
import maa from './maa'
|
|
4
4
|
import { DumpTask } from './pipeline'
|
|
5
5
|
import {
|
|
6
|
-
ChainNotifyType,
|
|
7
6
|
CustomActionCallback,
|
|
8
7
|
CustomActionSelf,
|
|
9
8
|
CustomRecognizerCallback,
|
|
10
9
|
CustomRecognizerSelf
|
|
11
10
|
} from './types'
|
|
12
|
-
import { chain_notify_impl } from './utils'
|
|
13
11
|
|
|
14
12
|
export type ResourceNotify = {
|
|
15
13
|
msg: 'Loading.Starting' | 'Loading.Succeeded' | 'Loading.Failed'
|
|
@@ -22,20 +20,13 @@ export class ResourceBase {
|
|
|
22
20
|
handle: maa.ResourceHandle
|
|
23
21
|
#source: JobSource<maa.ResId>
|
|
24
22
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
chain_parsed_notify(
|
|
30
|
-
cb: (msg: ResourceNotify) => maa.MaybePromise<void>,
|
|
31
|
-
order: ChainNotifyType = 'after'
|
|
32
|
-
) {
|
|
33
|
-
this.chain_notify((msg: string, details: string) => {
|
|
34
|
-
return cb({
|
|
23
|
+
static wrapSink(cb: (res: ResourceBase, msg: ResourceNotify) => maa.MaybePromise<void>) {
|
|
24
|
+
return ((res: ResourceBase, msg: string, details: string) => {
|
|
25
|
+
return cb(res, {
|
|
35
26
|
msg: msg.replace(/^Resource\./, '') as any,
|
|
36
27
|
...JSON.parse(details)
|
|
37
28
|
})
|
|
38
|
-
}
|
|
29
|
+
}) satisfies maa.EventCallbackWithHandle<ResourceBase>
|
|
39
30
|
}
|
|
40
31
|
|
|
41
32
|
constructor(handle: maa.ResourceHandle) {
|
|
@@ -50,6 +41,28 @@ export class ResourceBase {
|
|
|
50
41
|
maa.resource_destroy(this.handle)
|
|
51
42
|
}
|
|
52
43
|
|
|
44
|
+
add_sink(cb: maa.EventCallbackWithHandle<ResourceBase>) {
|
|
45
|
+
const ws = new WeakRef(this)
|
|
46
|
+
return maa.resource_add_sink(this.handle, (msg: string, details: string) => {
|
|
47
|
+
const res = ws.deref()
|
|
48
|
+
if (res) {
|
|
49
|
+
cb(res, msg, details)
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
add_wrapped_sink(cb: (ctrl: ResourceBase, msg: ResourceNotify) => maa.MaybePromise<void>) {
|
|
55
|
+
return this.add_sink(ResourceBase.wrapSink(cb))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
remove_sink(sink_id: maa.SinkId) {
|
|
59
|
+
maa.resource_remove_sink(this.handle, sink_id)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
clear_sinks() {
|
|
63
|
+
maa.resource_clear_sinks(this.handle)
|
|
64
|
+
}
|
|
65
|
+
|
|
53
66
|
set inference_device(id: keyof typeof maa.InferenceDevice | number) {
|
|
54
67
|
if (typeof id === 'string') {
|
|
55
68
|
id = maa.InferenceDevice[id]
|
|
@@ -190,14 +203,10 @@ export class ResourceBase {
|
|
|
190
203
|
|
|
191
204
|
export class Resource extends ResourceBase {
|
|
192
205
|
constructor() {
|
|
193
|
-
|
|
194
|
-
const h = maa.resource_create((message, details_json) => {
|
|
195
|
-
return ws.deref()?.notify(message, details_json)
|
|
196
|
-
})
|
|
206
|
+
const h = maa.resource_create()
|
|
197
207
|
if (!h) {
|
|
198
208
|
throw 'Resource create failed'
|
|
199
209
|
}
|
|
200
210
|
super(h)
|
|
201
|
-
ws = new WeakRef(this)
|
|
202
211
|
}
|
|
203
212
|
}
|
package/src/tasker.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
+
import { Context } from './context'
|
|
1
2
|
import { ControllerBase } from './controller'
|
|
2
3
|
import { Job, JobSource } from './job'
|
|
3
4
|
import maa from './maa'
|
|
4
5
|
import { ResourceBase } from './resource'
|
|
5
|
-
import { ChainNotifyType } from './types'
|
|
6
|
-
import { chain_notify_impl } from './utils'
|
|
7
6
|
|
|
8
7
|
type TaskDetail = ReturnType<TaskerBase['task_detail']>
|
|
9
8
|
|
|
@@ -56,14 +55,15 @@ export class TaskJob extends Job<maa.TaskId, JobSource<maa.TaskId>> {
|
|
|
56
55
|
}
|
|
57
56
|
}
|
|
58
57
|
|
|
59
|
-
export type TaskerNotify =
|
|
60
|
-
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
58
|
+
export type TaskerNotify = {
|
|
59
|
+
msg: 'Task.Started' | 'Task.Completed' | 'Task.Failed'
|
|
60
|
+
task_id: maa.TaskId
|
|
61
|
+
entry: string
|
|
62
|
+
uuid: string
|
|
63
|
+
hash: string
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export type TaskerContextNotify =
|
|
67
67
|
| {
|
|
68
68
|
msg: 'NextList.Starting' | 'NextList.Succeeded' | 'NextList.Failed'
|
|
69
69
|
task_id: maa.TaskId
|
|
@@ -90,20 +90,22 @@ export class TaskerBase {
|
|
|
90
90
|
handle: maa.TaskerHandle
|
|
91
91
|
#source: JobSource<maa.TaskId>
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
93
|
+
static wrapSink(cb: (tasker: TaskerBase, msg: TaskerNotify) => maa.MaybePromise<void>) {
|
|
94
|
+
return ((tasker: TaskerBase, msg: string, details: string) => {
|
|
95
|
+
return cb(tasker, {
|
|
96
|
+
msg: msg.replace(/^Tasker\./, '') as any,
|
|
97
|
+
...JSON.parse(details)
|
|
98
|
+
})
|
|
99
|
+
}) satisfies maa.EventCallbackWithHandle<TaskerBase>
|
|
100
|
+
}
|
|
96
101
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
this.chain_notify((msg: string, details: string) => {
|
|
102
|
-
return cb({
|
|
103
|
-
msg: msg.replace(/^(?:Tasker|Node)?\./, '') as any,
|
|
102
|
+
static wrapContextSink(cb: (ctx: Context, msg: TaskerContextNotify) => maa.MaybePromise<void>) {
|
|
103
|
+
return ((ctx: Context, msg: string, details: string) => {
|
|
104
|
+
return cb(ctx, {
|
|
105
|
+
msg: msg.replace(/^Node\./, '') as any,
|
|
104
106
|
...JSON.parse(details)
|
|
105
107
|
})
|
|
106
|
-
}
|
|
108
|
+
}) satisfies maa.EventCallbackWithHandle<Context>
|
|
107
109
|
}
|
|
108
110
|
|
|
109
111
|
constructor(handle: maa.TaskerHandle) {
|
|
@@ -118,6 +120,48 @@ export class TaskerBase {
|
|
|
118
120
|
maa.tasker_destroy(this.handle)
|
|
119
121
|
}
|
|
120
122
|
|
|
123
|
+
add_sink(cb: maa.EventCallbackWithHandle<TaskerBase>) {
|
|
124
|
+
const ws = new WeakRef(this)
|
|
125
|
+
return maa.tasker_add_sink(this.handle, (msg: string, details: string) => {
|
|
126
|
+
const tasker = ws.deref()
|
|
127
|
+
if (tasker) {
|
|
128
|
+
cb(tasker, msg, details)
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
add_wrapped_sink(cb: (tasker: TaskerBase, msg: TaskerNotify) => maa.MaybePromise<void>) {
|
|
134
|
+
return this.add_sink(TaskerBase.wrapSink(cb))
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
remove_sink(sink_id: maa.SinkId) {
|
|
138
|
+
maa.tasker_remove_sink(this.handle, sink_id)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
clear_sinks() {
|
|
142
|
+
maa.tasker_clear_sinks(this.handle)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
add_context_sink(cb: maa.EventCallbackWithHandle<Context>) {
|
|
146
|
+
return maa.tasker_add_context_sink(this.handle, (ctx, msg, details) => {
|
|
147
|
+
cb(new Context(ctx), msg, details)
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
add_wrapped_context_sink(
|
|
152
|
+
cb: (ctx: Context, msg: TaskerContextNotify) => maa.MaybePromise<void>
|
|
153
|
+
) {
|
|
154
|
+
return this.add_context_sink(TaskerBase.wrapContextSink(cb))
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
remove_context_sink(sink_id: maa.SinkId) {
|
|
158
|
+
maa.tasker_remove_context_sink(this.handle, sink_id)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
clear_context_sinks() {
|
|
162
|
+
maa.tasker_clear_context_sinks(this.handle)
|
|
163
|
+
}
|
|
164
|
+
|
|
121
165
|
bind(slave: ControllerBase | ResourceBase) {
|
|
122
166
|
let ret: boolean
|
|
123
167
|
if (slave instanceof ControllerBase) {
|
|
@@ -231,14 +275,10 @@ export class TaskerBase {
|
|
|
231
275
|
|
|
232
276
|
export class Tasker extends TaskerBase {
|
|
233
277
|
constructor() {
|
|
234
|
-
|
|
235
|
-
const h = maa.tasker_create((message, details_json) => {
|
|
236
|
-
return ws.deref()?.notify(message, details_json)
|
|
237
|
-
})
|
|
278
|
+
const h = maa.tasker_create()
|
|
238
279
|
if (!h) {
|
|
239
280
|
throw 'Tasker create failed'
|
|
240
281
|
}
|
|
241
282
|
super(h)
|
|
242
|
-
ws = new WeakRef(this)
|
|
243
283
|
}
|
|
244
284
|
}
|
package/src/types.ts
CHANGED
package/dist/pi.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import * as maa from './maa';
|
|
2
|
-
import { CustomActionCallback, CustomRecognizerCallback } from './types';
|
|
3
|
-
export declare const Pi: {
|
|
4
|
-
__running: boolean;
|
|
5
|
-
notify(message: string, details_json: string): maa.MaybePromise<void>;
|
|
6
|
-
register_custom_recognizer(id: maa.Id, name: string, func: CustomRecognizerCallback): void;
|
|
7
|
-
register_custom_action(id: maa.Id, name: string, func: CustomActionCallback): void;
|
|
8
|
-
run_cli(id: maa.Id, resource_path: string, user_path: string, directly: boolean): Promise<boolean>;
|
|
9
|
-
};
|
package/dist/utils.d.ts
DELETED
package/src/pi.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { Context } from './context'
|
|
2
|
-
import * as maa from './maa'
|
|
3
|
-
import {
|
|
4
|
-
CustomActionCallback,
|
|
5
|
-
CustomActionSelf,
|
|
6
|
-
CustomRecognizerCallback,
|
|
7
|
-
CustomRecognizerSelf
|
|
8
|
-
} from './types'
|
|
9
|
-
|
|
10
|
-
export const Pi = {
|
|
11
|
-
__running: false,
|
|
12
|
-
|
|
13
|
-
notify(message: string, details_json: string): maa.MaybePromise<void> {},
|
|
14
|
-
|
|
15
|
-
register_custom_recognizer(id: maa.Id, name: string, func: CustomRecognizerCallback) {
|
|
16
|
-
maa.pi_register_custom_recognizer(
|
|
17
|
-
id,
|
|
18
|
-
name,
|
|
19
|
-
(context, id, task, name, param, image, roi) => {
|
|
20
|
-
const self: CustomRecognizerSelf = {
|
|
21
|
-
context: new Context(context),
|
|
22
|
-
id,
|
|
23
|
-
task,
|
|
24
|
-
name,
|
|
25
|
-
param: JSON.parse(param),
|
|
26
|
-
image,
|
|
27
|
-
roi
|
|
28
|
-
}
|
|
29
|
-
return func.apply(self, [self])
|
|
30
|
-
}
|
|
31
|
-
)
|
|
32
|
-
},
|
|
33
|
-
|
|
34
|
-
register_custom_action(id: maa.Id, name: string, func: CustomActionCallback) {
|
|
35
|
-
maa.pi_register_custom_action(id, name, (context, id, task, name, param, recoId, box) => {
|
|
36
|
-
const self: CustomActionSelf = {
|
|
37
|
-
context: new Context(context),
|
|
38
|
-
id,
|
|
39
|
-
task,
|
|
40
|
-
name,
|
|
41
|
-
param: JSON.parse(param),
|
|
42
|
-
recoId,
|
|
43
|
-
box
|
|
44
|
-
}
|
|
45
|
-
return func.apply(self, [self])
|
|
46
|
-
})
|
|
47
|
-
},
|
|
48
|
-
|
|
49
|
-
async run_cli(id: maa.Id, resource_path: string, user_path: string, directly: boolean) {
|
|
50
|
-
if (Pi.__running) {
|
|
51
|
-
return false
|
|
52
|
-
}
|
|
53
|
-
Pi.__running = true
|
|
54
|
-
const res = await maa.pi_run_cli(
|
|
55
|
-
id,
|
|
56
|
-
resource_path,
|
|
57
|
-
user_path,
|
|
58
|
-
directly,
|
|
59
|
-
(message, details) => {
|
|
60
|
-
return Pi.notify(message, details)
|
|
61
|
-
}
|
|
62
|
-
)
|
|
63
|
-
Pi.__running = false
|
|
64
|
-
return res
|
|
65
|
-
}
|
|
66
|
-
}
|