@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 CHANGED
@@ -1,11 +1,14 @@
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
  export declare class AgentClient {
4
6
  handle: maa.AgentClientHandle;
5
7
  constructor(identifier?: string);
6
8
  destroy(): void;
7
9
  get identifier(): string | null;
8
10
  bind_resource(resource: Resource): void;
11
+ register_sink(tasker?: Tasker, resource?: Resource, controller?: ControllerBase): void;
9
12
  connect(): Promise<void>;
10
13
  disconnect(): void;
11
14
  get connected(): boolean;
@@ -1,7 +1,5 @@
1
1
  import { Job, JobSource } from './job';
2
- import maa, { ImageData } from './maa';
3
- import { ChainNotifyType } from './types';
4
- import { chain_notify_impl } from './utils';
2
+ import maa from './maa';
5
3
  declare class ImageJob extends Job<maa.CtrlId, JobSource<maa.CtrlId>> {
6
4
  #private;
7
5
  constructor(ctrl: ControllerBase, source: JobSource<maa.CtrlId>, id: maa.CtrlId);
@@ -83,11 +81,13 @@ export type ControllerNotify = {
83
81
  export declare class ControllerBase {
84
82
  #private;
85
83
  handle: maa.ControllerHandle;
86
- notify(message: string, details_json: string): maa.MaybePromise<void>;
87
- chain_notify: typeof chain_notify_impl;
88
- chain_parsed_notify(cb: (msg: ControllerNotify) => maa.MaybePromise<void>, order?: ChainNotifyType): void;
84
+ static wrapSink(cb: (ctrl: ControllerBase, msg: ControllerNotify) => maa.MaybePromise<void>): (ctrl: ControllerBase, msg: string, details: string) => maa.MaybePromise<void>;
89
85
  constructor(handle: maa.ControllerHandle);
90
86
  destroy(): void;
87
+ add_sink(cb: maa.EventCallbackWithHandle<ControllerBase>): maa.SinkId;
88
+ add_wrapped_sink(cb: (ctrl: ControllerBase, msg: ControllerNotify) => maa.MaybePromise<void>): maa.SinkId;
89
+ remove_sink(sink_id: maa.SinkId): void;
90
+ clear_sinks(): void;
91
91
  set screenshot_target_long_side(value: number);
92
92
  set screenshot_target_short_side(value: number);
93
93
  set screenshot_use_raw_size(value: boolean);
@@ -125,7 +125,7 @@ export declare abstract class CustomControllerActor {
125
125
  abstract request_uuid(): maa.MaybePromise<string | null>;
126
126
  abstract start_app(intent: string): maa.MaybePromise<boolean>;
127
127
  abstract stop_app(intent: string): maa.MaybePromise<boolean>;
128
- abstract screencap(): maa.MaybePromise<ImageData | null>;
128
+ abstract screencap(): maa.MaybePromise<maa.ImageData | null>;
129
129
  abstract click(x: number, y: number): maa.MaybePromise<boolean>;
130
130
  abstract swipe(x1: number, y1: number, x2: number, y2: number, duration: number): maa.MaybePromise<boolean>;
131
131
  abstract touch_down(contact: number, x: number, y: number, pressure: number): maa.MaybePromise<boolean>;
@@ -141,7 +141,7 @@ export declare class CustomControllerActorDefaultImpl extends CustomControllerAc
141
141
  request_uuid(): maa.MaybePromise<string | null>;
142
142
  start_app(intent: string): maa.MaybePromise<boolean>;
143
143
  stop_app(intent: string): maa.MaybePromise<boolean>;
144
- screencap(): maa.MaybePromise<ImageData | null>;
144
+ screencap(): maa.MaybePromise<maa.ImageData | null>;
145
145
  click(x: number, y: number): maa.MaybePromise<boolean>;
146
146
  swipe(x1: number, y1: number, x2: number, y2: number, duration: number): maa.MaybePromise<boolean>;
147
147
  touch_down(contact: number, x: number, y: number, pressure: number): maa.MaybePromise<boolean>;
@@ -6,7 +6,6 @@ export * from './resource';
6
6
  export * from './tasker';
7
7
  export * from './context';
8
8
  export * from './global';
9
- export * from './pi';
10
9
  export * from './types';
11
10
  export * from './pipeline';
12
11
  export * from './client';
@@ -84,7 +84,6 @@ __export(index_client_exports, {
84
84
  DbgController: () => DbgController,
85
85
  Global: () => Global,
86
86
  Job: () => Job,
87
- Pi: () => Pi,
88
87
  Resource: () => Resource,
89
88
  ResourceBase: () => ResourceBase,
90
89
  TaskJob: () => TaskJob,
@@ -150,42 +149,6 @@ var Job = class {
150
149
  // src/controller.ts
151
150
  var import_path = __toESM(require("path"));
152
151
  var import_maa = __toESM(require_maa());
153
-
154
- // src/utils.ts
155
- function chain_notify_impl(cb, order = "after") {
156
- const old = this.notify;
157
- switch (order) {
158
- case "before":
159
- this.notify = async (msg, details) => {
160
- await cb(msg, details);
161
- await old.call(this, msg, details);
162
- };
163
- break;
164
- case "after":
165
- this.notify = async (msg, details) => {
166
- await old.call(this, msg, details);
167
- await cb(msg, details);
168
- };
169
- break;
170
- case "before-no-wait":
171
- this.notify = async (msg, details) => {
172
- cb(msg, details);
173
- await old.call(this, msg, details);
174
- };
175
- break;
176
- case "after-no-wait":
177
- this.notify = async (msg, details) => {
178
- await old.call(this, msg, details);
179
- cb(msg, details);
180
- };
181
- break;
182
- case "replace":
183
- this.notify = cb;
184
- break;
185
- }
186
- }
187
-
188
- // src/controller.ts
189
152
  var ImageJob = class extends Job {
190
153
  #ctrl;
191
154
  constructor(ctrl, source, id) {
@@ -212,19 +175,16 @@ var ImageJob = class extends Job {
212
175
  return pro;
213
176
  }
214
177
  };
215
- var ControllerBase = class {
178
+ var ControllerBase = class _ControllerBase {
216
179
  handle;
217
180
  #source;
218
- notify(message, details_json) {
219
- }
220
- chain_notify = chain_notify_impl;
221
- chain_parsed_notify(cb, order = "after") {
222
- this.chain_notify((msg, details) => {
223
- return cb({
181
+ static wrapSink(cb) {
182
+ return (ctrl, msg, details) => {
183
+ return cb(ctrl, {
224
184
  msg: msg.replace(/^Controller\./, ""),
225
185
  ...JSON.parse(details)
226
186
  });
227
- }, order);
187
+ };
228
188
  }
229
189
  constructor(handle) {
230
190
  this.handle = handle;
@@ -236,6 +196,24 @@ var ControllerBase = class {
236
196
  destroy() {
237
197
  import_maa.default.controller_destroy(this.handle);
238
198
  }
199
+ add_sink(cb) {
200
+ const ws = new WeakRef(this);
201
+ return import_maa.default.controller_add_sink(this.handle, (msg, details) => {
202
+ const ctrl = ws.deref();
203
+ if (ctrl) {
204
+ cb(ctrl, msg, details);
205
+ }
206
+ });
207
+ }
208
+ add_wrapped_sink(cb) {
209
+ return this.add_sink(_ControllerBase.wrapSink(cb));
210
+ }
211
+ remove_sink(sink_id) {
212
+ import_maa.default.controller_remove_sink(this.handle, sink_id);
213
+ }
214
+ clear_sinks() {
215
+ import_maa.default.controller_clear_sinks(this.handle);
216
+ }
239
217
  set screenshot_target_long_side(value) {
240
218
  if (!import_maa.default.controller_set_option_screenshot_target_long_side(this.handle, value)) {
241
219
  throw "Controller set screenshot_target_long_side failed";
@@ -311,23 +289,18 @@ var ControllerBase = class {
311
289
  };
312
290
  var AdbController = class _AdbController extends ControllerBase {
313
291
  constructor(adb_path, address, screencap_methods, input_methods, config, agent) {
314
- let ws;
315
292
  const h = import_maa.default.adb_controller_create(
316
293
  adb_path,
317
294
  address,
318
295
  screencap_methods,
319
296
  input_methods,
320
297
  config,
321
- agent ?? _AdbController.agent_path(),
322
- (message, details_json) => {
323
- return ws.deref()?.notify(message, details_json);
324
- }
298
+ agent ?? _AdbController.agent_path()
325
299
  );
326
300
  if (!h) {
327
301
  throw "AdbController create failed";
328
302
  }
329
303
  super(h);
330
- ws = new WeakRef(this);
331
304
  }
332
305
  static agent_path() {
333
306
  return import_path.default.join(__dirname, "..", "agent");
@@ -338,20 +311,15 @@ var AdbController = class _AdbController extends ControllerBase {
338
311
  };
339
312
  var Win32Controller = class extends ControllerBase {
340
313
  constructor(hwnd, screencap_methods, input_methods) {
341
- let ws;
342
314
  const h = import_maa.default.win32_controller_create(
343
315
  hwnd ?? "0",
344
316
  screencap_methods,
345
- input_methods,
346
- (message, details_json) => {
347
- return ws.deref()?.notify(message, details_json);
348
- }
317
+ input_methods
349
318
  );
350
319
  if (!h) {
351
320
  throw "Win32Controller create failed";
352
321
  }
353
322
  super(h);
354
- ws = new WeakRef(this);
355
323
  }
356
324
  static find() {
357
325
  return import_maa.default.find_desktop();
@@ -359,21 +327,11 @@ var Win32Controller = class extends ControllerBase {
359
327
  };
360
328
  var DbgController = class extends ControllerBase {
361
329
  constructor(read_path, write_path, type, config) {
362
- let ws;
363
- const h = import_maa.default.dbg_controller_create(
364
- read_path,
365
- write_path,
366
- type,
367
- config,
368
- (message, details_json) => {
369
- return ws.deref()?.notify(message, details_json);
370
- }
371
- );
330
+ const h = import_maa.default.dbg_controller_create(read_path, write_path, type, config);
372
331
  if (!h) {
373
332
  throw "DbgController create failed";
374
333
  }
375
334
  super(h);
376
- ws = new WeakRef(this);
377
335
  }
378
336
  };
379
337
  var CustomControllerActor = class {
@@ -424,20 +382,13 @@ var CustomControllerActorDefaultImpl = class extends CustomControllerActor {
424
382
  };
425
383
  var CustomController = class extends ControllerBase {
426
384
  constructor(actor) {
427
- let ws;
428
- const h = import_maa.default.custom_controller_create(
429
- (action, ...param) => {
430
- return actor[action](...param);
431
- },
432
- (message, details_json) => {
433
- return ws.deref()?.notify(message, details_json);
434
- }
435
- );
385
+ const h = import_maa.default.custom_controller_create((action, ...param) => {
386
+ return actor[action](...param);
387
+ });
436
388
  if (!h) {
437
389
  throw "CustomController create failed";
438
390
  }
439
391
  super(h);
440
- ws = new WeakRef(this);
441
392
  }
442
393
  };
443
394
 
@@ -472,19 +423,24 @@ var TaskJob = class extends Job {
472
423
  return pro;
473
424
  }
474
425
  };
475
- var TaskerBase = class {
426
+ var TaskerBase = class _TaskerBase {
476
427
  handle;
477
428
  #source;
478
- notify(message, details_json) {
429
+ static wrapSink(cb) {
430
+ return (tasker, msg, details) => {
431
+ return cb(tasker, {
432
+ msg: msg.replace(/^Tasker\./, ""),
433
+ ...JSON.parse(details)
434
+ });
435
+ };
479
436
  }
480
- chain_notify = chain_notify_impl;
481
- chain_parsed_notify(cb, order = "after") {
482
- this.chain_notify((msg, details) => {
483
- return cb({
484
- msg: msg.replace(/^(?:Tasker|Node)?\./, ""),
437
+ static wrapContextSink(cb) {
438
+ return (ctx, msg, details) => {
439
+ return cb(ctx, {
440
+ msg: msg.replace(/^Node\./, ""),
485
441
  ...JSON.parse(details)
486
442
  });
487
- }, order);
443
+ };
488
444
  }
489
445
  constructor(handle) {
490
446
  this.handle = handle;
@@ -496,6 +452,38 @@ var TaskerBase = class {
496
452
  destroy() {
497
453
  import_maa2.default.tasker_destroy(this.handle);
498
454
  }
455
+ add_sink(cb) {
456
+ const ws = new WeakRef(this);
457
+ return import_maa2.default.tasker_add_sink(this.handle, (msg, details) => {
458
+ const tasker = ws.deref();
459
+ if (tasker) {
460
+ cb(tasker, msg, details);
461
+ }
462
+ });
463
+ }
464
+ add_wrapped_sink(cb) {
465
+ return this.add_sink(_TaskerBase.wrapSink(cb));
466
+ }
467
+ remove_sink(sink_id) {
468
+ import_maa2.default.tasker_remove_sink(this.handle, sink_id);
469
+ }
470
+ clear_sinks() {
471
+ import_maa2.default.tasker_clear_sinks(this.handle);
472
+ }
473
+ add_context_sink(cb) {
474
+ return import_maa2.default.tasker_add_context_sink(this.handle, (ctx, msg, details) => {
475
+ cb(new Context(ctx), msg, details);
476
+ });
477
+ }
478
+ add_wrapped_context_sink(cb) {
479
+ return this.add_context_sink(_TaskerBase.wrapContextSink(cb));
480
+ }
481
+ remove_context_sink(sink_id) {
482
+ import_maa2.default.tasker_remove_context_sink(this.handle, sink_id);
483
+ }
484
+ clear_context_sinks() {
485
+ import_maa2.default.tasker_clear_context_sinks(this.handle);
486
+ }
499
487
  bind(slave) {
500
488
  let ret;
501
489
  if (slave instanceof ControllerBase) {
@@ -596,15 +584,11 @@ var TaskerBase = class {
596
584
  };
597
585
  var Tasker = class extends TaskerBase {
598
586
  constructor() {
599
- let ws;
600
- const h = import_maa2.default.tasker_create((message, details_json) => {
601
- return ws.deref()?.notify(message, details_json);
602
- });
587
+ const h = import_maa2.default.tasker_create();
603
588
  if (!h) {
604
589
  throw "Tasker create failed";
605
590
  }
606
591
  super(h);
607
- ws = new WeakRef(this);
608
592
  }
609
593
  };
610
594
 
@@ -676,19 +660,16 @@ var Context = class _Context {
676
660
 
677
661
  // src/resource.ts
678
662
  var import_maa3 = __toESM(require_maa());
679
- var ResourceBase = class {
663
+ var ResourceBase = class _ResourceBase {
680
664
  handle;
681
665
  #source;
682
- notify(message, details_json) {
683
- }
684
- chain_notify = chain_notify_impl;
685
- chain_parsed_notify(cb, order = "after") {
686
- this.chain_notify((msg, details) => {
687
- return cb({
666
+ static wrapSink(cb) {
667
+ return (res, msg, details) => {
668
+ return cb(res, {
688
669
  msg: msg.replace(/^Resource\./, ""),
689
670
  ...JSON.parse(details)
690
671
  });
691
- }, order);
672
+ };
692
673
  }
693
674
  constructor(handle) {
694
675
  this.handle = handle;
@@ -700,6 +681,24 @@ var ResourceBase = class {
700
681
  destroy() {
701
682
  import_maa3.default.resource_destroy(this.handle);
702
683
  }
684
+ add_sink(cb) {
685
+ const ws = new WeakRef(this);
686
+ return import_maa3.default.resource_add_sink(this.handle, (msg, details) => {
687
+ const res = ws.deref();
688
+ if (res) {
689
+ cb(res, msg, details);
690
+ }
691
+ });
692
+ }
693
+ add_wrapped_sink(cb) {
694
+ return this.add_sink(_ResourceBase.wrapSink(cb));
695
+ }
696
+ remove_sink(sink_id) {
697
+ import_maa3.default.resource_remove_sink(this.handle, sink_id);
698
+ }
699
+ clear_sinks() {
700
+ import_maa3.default.resource_clear_sinks(this.handle);
701
+ }
703
702
  set inference_device(id) {
704
703
  if (typeof id === "string") {
705
704
  id = import_maa3.default.InferenceDevice[id];
@@ -817,15 +816,11 @@ var ResourceBase = class {
817
816
  };
818
817
  var Resource = class extends ResourceBase {
819
818
  constructor() {
820
- let ws;
821
- const h = import_maa3.default.resource_create((message, details_json) => {
822
- return ws.deref()?.notify(message, details_json);
823
- });
819
+ const h = import_maa3.default.resource_create();
824
820
  if (!h) {
825
821
  throw "Resource create failed";
826
822
  }
827
823
  super(h);
828
- ws = new WeakRef(this);
829
824
  }
830
825
  };
831
826
 
@@ -862,103 +857,56 @@ var Global = {
862
857
  }
863
858
  };
864
859
 
865
- // src/pi.ts
866
- var maa7 = __toESM(require_maa());
867
- var Pi = {
868
- __running: false,
869
- notify(message, details_json) {
870
- },
871
- register_custom_recognizer(id, name, func) {
872
- maa7.pi_register_custom_recognizer(
873
- id,
874
- name,
875
- (context, id2, task, name2, param, image, roi) => {
876
- const self = {
877
- context: new Context(context),
878
- id: id2,
879
- task,
880
- name: name2,
881
- param: JSON.parse(param),
882
- image,
883
- roi
884
- };
885
- return func.apply(self, [self]);
886
- }
887
- );
888
- },
889
- register_custom_action(id, name, func) {
890
- maa7.pi_register_custom_action(id, name, (context, id2, task, name2, param, recoId, box) => {
891
- const self = {
892
- context: new Context(context),
893
- id: id2,
894
- task,
895
- name: name2,
896
- param: JSON.parse(param),
897
- recoId,
898
- box
899
- };
900
- return func.apply(self, [self]);
901
- });
902
- },
903
- async run_cli(id, resource_path, user_path, directly) {
904
- if (Pi.__running) {
905
- return false;
906
- }
907
- Pi.__running = true;
908
- const res = await maa7.pi_run_cli(
909
- id,
910
- resource_path,
911
- user_path,
912
- directly,
913
- (message, details) => {
914
- return Pi.notify(message, details);
915
- }
916
- );
917
- Pi.__running = false;
918
- return res;
919
- }
920
- };
921
-
922
860
  // src/client.ts
923
- var maa8 = __toESM(require_maa());
861
+ var maa7 = __toESM(require_maa());
924
862
  var AgentClient = class {
925
863
  handle;
926
864
  constructor(identifier) {
927
- const h = maa8.agent_client_create(identifier ?? null);
865
+ const h = maa7.agent_client_create(identifier ?? null);
928
866
  if (!h) {
929
867
  throw "AgentClient create failed";
930
868
  }
931
869
  this.handle = h;
932
870
  }
933
871
  destroy() {
934
- maa8.agent_client_destroy(this.handle);
872
+ maa7.agent_client_destroy(this.handle);
935
873
  }
936
874
  get identifier() {
937
- return maa8.agent_client_identifier(this.handle);
875
+ return maa7.agent_client_identifier(this.handle);
938
876
  }
939
877
  bind_resource(resource) {
940
- if (!maa8.agent_client_bind_resource(this.handle, resource.handle)) {
878
+ if (!maa7.agent_client_bind_resource(this.handle, resource.handle)) {
941
879
  throw "AgentClient bind resource failed";
942
880
  }
943
881
  }
882
+ register_sink(tasker, resource, controller) {
883
+ if (!maa7.agent_client_register_sink(
884
+ this.handle,
885
+ tasker?.handle ?? null,
886
+ resource?.handle ?? null,
887
+ controller?.handle ?? null
888
+ )) {
889
+ throw "AgentClient register sink failed";
890
+ }
891
+ }
944
892
  async connect() {
945
- if (!await maa8.agent_client_connect(this.handle)) {
893
+ if (!await maa7.agent_client_connect(this.handle)) {
946
894
  throw "AgentClient connect failed";
947
895
  }
948
896
  }
949
897
  disconnect() {
950
- if (!maa8.agent_client_disconnect(this.handle)) {
898
+ if (!maa7.agent_client_disconnect(this.handle)) {
951
899
  throw "AgentClient disconnect failed";
952
900
  }
953
901
  }
954
902
  get connected() {
955
- return maa8.agent_client_connected(this.handle);
903
+ return maa7.agent_client_connected(this.handle);
956
904
  }
957
905
  get alive() {
958
- return maa8.agent_client_alive(this.handle);
906
+ return maa7.agent_client_alive(this.handle);
959
907
  }
960
908
  set timeout(ms) {
961
- if (!maa8.agent_client_set_timeout(this.handle, ms)) {
909
+ if (!maa7.agent_client_set_timeout(this.handle, ms)) {
962
910
  throw "AgentClient set timeout failed";
963
911
  }
964
912
  }
@@ -975,7 +923,6 @@ var AgentClient = class {
975
923
  DbgController,
976
924
  Global,
977
925
  Job,
978
- Pi,
979
926
  Resource,
980
927
  ResourceBase,
981
928
  TaskJob,
@@ -6,7 +6,6 @@ export * from './resource';
6
6
  export * from './tasker';
7
7
  export * from './context';
8
8
  export * from './global';
9
- export * from './pi';
10
9
  export * from './types';
11
10
  export * from './pipeline';
12
11
  export * from './server';