@fraqjs/fraq 0.10.3 → 0.12.0

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/README.md CHANGED
@@ -50,4 +50,4 @@ npx tsx index.ts
50
50
 
51
51
  上面的例子只是 Fraq 的最小使用方式,你还可以继续了解如何构造更丰富的消息、定义更复杂的指令参数、编写可复用的插件等。
52
52
 
53
- 完整文档请见 [fraq.ntqqrev.org](https://fraq.ntqqrev.org/)。
53
+ 完整文档请见 [fraq.dev](https://fraq.dev/)。
package/dist/index.d.mts CHANGED
@@ -405,7 +405,7 @@ declare namespace types_d_exports {
405
405
  };
406
406
  }
407
407
  declare const milkyVersion = '1.3';
408
- declare const milkyPackageVersion = '1.3.0-rc.1';
408
+ declare const milkyPackageVersion = '1.3.0-rc.2';
409
409
  /** 机器人离线事件 */
410
410
  interface BotOfflineEvent {
411
411
  /** 数据类型区分字段,表示自身为机器人离线事件 */
@@ -2846,7 +2846,7 @@ interface GetImplInfoOutput {
2846
2846
  | 'iphone'
2847
2847
  | 'harmony'
2848
2848
  | 'watch';
2849
- /** 协议端实现的 Milky 协议版本,目前为 "1.2" */
2849
+ /** 协议端实现的 Milky 协议版本,目前为 "1.3" */
2850
2850
  milky_version: string;
2851
2851
  }
2852
2852
  /** 获取协议端信息 API 响应数据 */
@@ -2868,7 +2868,7 @@ interface GetImplInfoOutput_ZodInput {
2868
2868
  | 'iphone'
2869
2869
  | 'harmony'
2870
2870
  | 'watch';
2871
- /** 协议端实现的 Milky 协议版本,目前为 "1.2" */
2871
+ /** 协议端实现的 Milky 协议版本,目前为 "1.3" */
2872
2872
  milky_version: string;
2873
2873
  }
2874
2874
  /** 获取用户个人信息 API 请求参数 */
@@ -4151,6 +4151,8 @@ interface GetPrivateFileDownloadUrlInput {
4151
4151
  file_id: string;
4152
4152
  /** 文件的 TriSHA1 哈希值 */
4153
4153
  file_hash: string;
4154
+ /** 是否为自己发送的文件 */
4155
+ is_self_send: boolean;
4154
4156
  }
4155
4157
  /** 获取私聊文件下载链接 API 请求参数 */
4156
4158
  interface GetPrivateFileDownloadUrlInput_ZodInput {
@@ -4160,6 +4162,8 @@ interface GetPrivateFileDownloadUrlInput_ZodInput {
4160
4162
  file_id: string;
4161
4163
  /** 文件的 TriSHA1 哈希值 */
4162
4164
  file_hash: string;
4165
+ /** 是否为自己发送的文件 */
4166
+ is_self_send?: boolean | null | undefined;
4163
4167
  }
4164
4168
  /** 获取私聊文件下载链接 API 响应数据 */
4165
4169
  interface GetPrivateFileDownloadUrlOutput {
@@ -5373,52 +5377,6 @@ interface ApiEndpoints$1 {
5373
5377
  };
5374
5378
  }
5375
5379
  //#endregion
5376
- //#region src/protocol/endpoint.d.ts
5377
- type RequiredKeys<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? never : K }[keyof T];
5378
- type AllOptional<T> = RequiredKeys<T> extends never ? true : false;
5379
- type RawApiEndpoint<E extends keyof ApiEndpoints$1> = {
5380
- request: ApiEndpoints$1[E]['request_ZodInput'] extends null ? null : ApiEndpoints$1[E]['request_ZodInput'];
5381
- response: ApiEndpoints$1[E]['response'] extends null ? null : ApiEndpoints$1[E]['response'];
5382
- };
5383
- type ApiEndpointFunction<E extends keyof ApiEndpoints$1> = RawApiEndpoint<E>['request'] extends null
5384
- ? () => Promise<RawApiEndpoint<E>['response']>
5385
- : AllOptional<RawApiEndpoint<E>['request']> extends true
5386
- ? (params?: RawApiEndpoint<E>['request']) => Promise<RawApiEndpoint<E>['response']>
5387
- : (params: RawApiEndpoint<E>['request']) => Promise<RawApiEndpoint<E>['response']>;
5388
- type ApiEndpoints = { [E in keyof ApiEndpoints$1]: ApiEndpointFunction<E> };
5389
- type EventMap = {
5390
- [K in Event['event_type']]: Extract<
5391
- Event,
5392
- {
5393
- event_type: K;
5394
- }
5395
- >;
5396
- };
5397
- //#endregion
5398
- //#region src/protocol/client.d.ts
5399
- interface MilkyEventSubscription {
5400
- closed: Promise<void>;
5401
- stop(): void | Promise<void>;
5402
- }
5403
- interface MilkyClient extends ApiEndpoints {
5404
- startEvents(onEvent: (event: Event) => void | Promise<void>): Promise<MilkyEventSubscription>;
5405
- }
5406
- declare class MilkyClientBase {
5407
- readonly baseUrl: string;
5408
- readonly wsBaseUrl: string;
5409
- private readonly accessToken?;
5410
- private readonly baseHeaders;
5411
- constructor(
5412
- baseUrl: string | URL,
5413
- options?: {
5414
- accessToken?: string;
5415
- },
5416
- );
5417
- callApi(endpoint: string, params?: unknown): Promise<unknown>;
5418
- startEvents(onEvent: (event: Event) => void | Promise<void>): Promise<MilkyEventSubscription>;
5419
- }
5420
- declare function createMilkyClient(...params: ConstructorParameters<typeof MilkyClientBase>): MilkyClient;
5421
- //#endregion
5422
5380
  //#region src/routing/tokenizer.d.ts
5423
5381
  type Token =
5424
5382
  | string
@@ -5446,6 +5404,8 @@ declare class Tokenizer {
5446
5404
  greedy(): string;
5447
5405
  isCatchAllAvailable(): boolean;
5448
5406
  catchAll(): IncomingSegment[];
5407
+ consumeMention(userId: number): boolean;
5408
+ consumeTextPrefix(prefix: string): boolean;
5449
5409
  private findNextPosition;
5450
5410
  private findTextTokenStart;
5451
5411
  private readTextToken;
@@ -5512,6 +5472,9 @@ declare namespace param {
5512
5472
  type Pattern = Record<string, Parameter<any>>;
5513
5473
  type ParamsOf<P extends Pattern> = { [K in keyof P]: P[K] extends Parameter<infer T> ? T : never };
5514
5474
  type Executor<P extends Pattern> = (session: Session, params: ParamsOf<P>) => void | Promise<void>;
5475
+ type RouteMeta = Record<string, unknown> & {
5476
+ tags?: readonly string[];
5477
+ };
5515
5478
  interface Command<P extends Pattern> {
5516
5479
  name: string;
5517
5480
  pattern: P;
@@ -5519,16 +5482,19 @@ interface Command<P extends Pattern> {
5519
5482
  description?: string;
5520
5483
  aliases?: string[];
5521
5484
  hidden?: boolean;
5485
+ meta?: RouteMeta;
5522
5486
  }
5523
5487
  interface RawPattern<P extends Pattern> {
5524
5488
  pattern: P;
5525
5489
  execute: Executor<P>;
5490
+ meta?: RouteMeta;
5526
5491
  }
5527
5492
  interface SessionReplyOptions {
5528
5493
  withQuote?: boolean;
5529
5494
  withMention?: boolean;
5530
5495
  }
5531
5496
  interface Session {
5497
+ selfId: number;
5532
5498
  raw: IncomingMessage;
5533
5499
  reply(
5534
5500
  textOrSegments: string | OutgoingSegment_ZodInput[],
@@ -5546,6 +5512,7 @@ declare class CommandBuilder<P extends Pattern = {}, S = Command<P>> {
5546
5512
  private description?;
5547
5513
  private aliases?;
5548
5514
  private hidden?;
5515
+ private routeMeta?;
5549
5516
  constructor(name: string, sink?: (command: Command<P>) => S);
5550
5517
  arg<K extends string, T>(
5551
5518
  key: K,
@@ -5561,11 +5528,40 @@ declare class CommandBuilder<P extends Pattern = {}, S = Command<P>> {
5561
5528
  describe(description: string): CommandBuilder<P, S>;
5562
5529
  alias(...aliases: string[]): CommandBuilder<P, S>;
5563
5530
  hide(): CommandBuilder<P, S>;
5531
+ meta(meta: RouteMeta): CommandBuilder<P, S>;
5532
+ tag(...tags: string[]): CommandBuilder<P, S>;
5564
5533
  execute(executor: Executor<P>): S;
5565
5534
  }
5535
+ declare function mergeRouteMeta(left?: RouteMeta, right?: RouteMeta): RouteMeta | undefined;
5566
5536
  //#endregion
5567
5537
  //#region src/routing/router.d.ts
5568
5538
  type SessionPredicate = (session: Session) => boolean;
5539
+ type RouteActivation =
5540
+ | {
5541
+ type: 'direct';
5542
+ }
5543
+ | {
5544
+ type: 'mention';
5545
+ }
5546
+ | {
5547
+ type: 'prefix';
5548
+ prefix: string;
5549
+ };
5550
+ type RouteDescriptor =
5551
+ | {
5552
+ type: 'command';
5553
+ path: readonly string[];
5554
+ name: string;
5555
+ aliases?: readonly string[];
5556
+ meta?: RouteMeta;
5557
+ }
5558
+ | {
5559
+ type: 'rawPattern';
5560
+ path: readonly string[];
5561
+ meta?: RouteMeta;
5562
+ };
5563
+ type RouteActivationResolver = (route: RouteDescriptor, session: Session) => readonly RouteActivation[] | undefined;
5564
+ declare const defaultRouteActivationResolver: RouteActivationResolver;
5569
5565
  type RouteEntry =
5570
5566
  | {
5571
5567
  type: 'command';
@@ -5590,11 +5586,13 @@ type RouteBranch =
5590
5586
  type: 'command';
5591
5587
  path: string[];
5592
5588
  command: Command<Pattern>;
5589
+ meta?: RouteMeta;
5593
5590
  }
5594
5591
  | {
5595
5592
  type: 'rawPattern';
5596
5593
  path: string[];
5597
5594
  rawPattern: RawPattern<Pattern>;
5595
+ meta?: RouteMeta;
5598
5596
  };
5599
5597
  type RouteMatchResult =
5600
5598
  | {
@@ -5602,16 +5600,22 @@ type RouteMatchResult =
5602
5600
  path: string[];
5603
5601
  command: Command<Pattern>;
5604
5602
  params: any;
5603
+ activation?: RouteActivation;
5605
5604
  }
5606
5605
  | {
5607
5606
  type: 'rawPattern';
5608
5607
  path: string[];
5609
5608
  rawPattern: RawPattern<Pattern>;
5610
5609
  params: any;
5610
+ activation?: RouteActivation;
5611
5611
  };
5612
5612
  declare class Router {
5613
- private readonly entries;
5614
- private readonly groups;
5613
+ private entries;
5614
+ private groups;
5615
+ private activationResolver;
5616
+ private scopeMeta?;
5617
+ setActivationResolver(resolver: RouteActivationResolver): this;
5618
+ withMeta(meta: RouteMeta): Router;
5615
5619
  command(name: string): CommandBuilder;
5616
5620
  command<P extends Pattern>(command: Command<P>): this;
5617
5621
  rawPattern(): CommandBuilder<{}, RawPattern<{}>>;
@@ -5623,17 +5627,107 @@ declare class Router {
5623
5627
  branches(session: Session): RouteBranch[];
5624
5628
  match(session: Session, message: IncomingMessage): RouteMatchResult | undefined;
5625
5629
  dispatch(session: Session, message: IncomingMessage): Promise<boolean>;
5626
- private matchFrom;
5627
- private matchEntry;
5630
+ private describeBranch;
5631
+ private consumeActivation;
5632
+ private matchBranch;
5633
+ private matchPath;
5628
5634
  private matchCommand;
5629
- private matchGroup;
5630
5635
  private matchRawPattern;
5631
5636
  private branchesFrom;
5637
+ private applyScopeMeta;
5632
5638
  private resolveAliasConflicts;
5633
5639
  private validatePattern;
5634
5640
  private capturePattern;
5635
5641
  }
5636
5642
  //#endregion
5643
+ //#region src/core/activation.d.ts
5644
+ type ContextRouteActivationByScene = Partial<
5645
+ Record<IncomingMessage['message_scene'] | 'default', readonly RouteActivation[]>
5646
+ >;
5647
+ type ContextRouteActivation = readonly RouteActivation[] | ContextRouteActivationByScene;
5648
+ interface ContextRouteActivationMatcher {
5649
+ type?: RouteDescriptor['type'] | readonly RouteDescriptor['type'][];
5650
+ plugin?: string | readonly string[];
5651
+ context?: string | readonly string[];
5652
+ tag?: string | readonly string[];
5653
+ command?: string | readonly string[];
5654
+ path?: readonly string[];
5655
+ route?: readonly string[];
5656
+ predicate?: (route: RouteDescriptor, session: Session) => boolean;
5657
+ }
5658
+ interface ContextRouteActivationRule {
5659
+ match?: ContextRouteActivationMatcher;
5660
+ activation: ContextRouteActivation;
5661
+ }
5662
+ interface ContextRouteActivationConfig {
5663
+ default?: ContextRouteActivation;
5664
+ rules?: ContextRouteActivationRule | readonly ContextRouteActivationRule[];
5665
+ }
5666
+ interface ContextRoutingOptions {
5667
+ activation?: ContextRouteActivationConfig;
5668
+ activationResolver?: RouteActivationResolver;
5669
+ }
5670
+ declare function createContextRouteActivationResolver(
5671
+ routing: ContextRoutingOptions | undefined,
5672
+ fallback?: RouteActivationResolver,
5673
+ ): RouteActivationResolver;
5674
+ //#endregion
5675
+ //#region src/protocol/endpoint.d.ts
5676
+ type RequiredKeys<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? never : K }[keyof T];
5677
+ type AllOptional<T> = RequiredKeys<T> extends never ? true : false;
5678
+ type RawApiEndpoint<E extends keyof ApiEndpoints$1> = {
5679
+ request: ApiEndpoints$1[E]['request_ZodInput'] extends null ? null : ApiEndpoints$1[E]['request_ZodInput'];
5680
+ response: ApiEndpoints$1[E]['response'] extends null ? null : ApiEndpoints$1[E]['response'];
5681
+ };
5682
+ type ApiEndpointFunction<E extends keyof ApiEndpoints$1> = RawApiEndpoint<E>['request'] extends null
5683
+ ? () => Promise<RawApiEndpoint<E>['response']>
5684
+ : AllOptional<RawApiEndpoint<E>['request']> extends true
5685
+ ? (params?: RawApiEndpoint<E>['request']) => Promise<RawApiEndpoint<E>['response']>
5686
+ : (params: RawApiEndpoint<E>['request']) => Promise<RawApiEndpoint<E>['response']>;
5687
+ type ApiEndpoints = { [E in keyof ApiEndpoints$1]: ApiEndpointFunction<E> };
5688
+ type EventMap = {
5689
+ [K in Event['event_type']]: Extract<
5690
+ Event,
5691
+ {
5692
+ event_type: K;
5693
+ }
5694
+ >;
5695
+ };
5696
+ //#endregion
5697
+ //#region src/protocol/client.d.ts
5698
+ interface MilkyClient extends ApiEndpoints {}
5699
+ declare class MilkyClientBase {
5700
+ readonly baseUrl: string;
5701
+ readonly wsBaseUrl: string;
5702
+ private readonly accessToken?;
5703
+ private readonly baseHeaders;
5704
+ constructor(
5705
+ baseUrl: string | URL,
5706
+ options?: {
5707
+ accessToken?: string;
5708
+ },
5709
+ );
5710
+ callApi(endpoint: string, params?: unknown): Promise<unknown>;
5711
+ }
5712
+ declare function createMilkyClient(...params: ConstructorParameters<typeof MilkyClientBase>): MilkyClient;
5713
+ //#endregion
5714
+ //#region src/protocol/event.d.ts
5715
+ interface MilkyEventSubscription {
5716
+ closed: Promise<void>;
5717
+ stop(): void | Promise<void>;
5718
+ }
5719
+ interface MilkyEventSource {
5720
+ readonly name?: string;
5721
+ start(onEvent: (event: Event) => void | Promise<void>): Promise<MilkyEventSubscription>;
5722
+ }
5723
+ interface MilkyWebSocketEventSourceOptions {
5724
+ accessToken?: string;
5725
+ }
5726
+ declare function createMilkyWebSocketEventSource(
5727
+ baseUrl: string | URL,
5728
+ options?: MilkyWebSocketEventSourceOptions,
5729
+ ): MilkyEventSource;
5730
+ //#endregion
5637
5731
  //#region src/core/filter.d.ts
5638
5732
  type Filter = { [K in keyof EventMap]?: (event: EventMap[K]) => boolean };
5639
5733
  declare namespace filter {
@@ -5716,21 +5810,25 @@ interface ContextOptions {
5716
5810
  maxDelayMs?: number;
5717
5811
  };
5718
5812
  logHandler?: LogHandler;
5813
+ routing?: ContextRoutingOptions;
5719
5814
  }
5720
5815
  interface ContextUrlOptions {
5721
5816
  accessToken?: string;
5817
+ installEventSource?: boolean;
5722
5818
  }
5723
5819
  declare class Context {
5724
5820
  readonly client: MilkyClient;
5725
5821
  readonly router: Router;
5726
5822
  readonly logger: Logger;
5727
5823
  readonly name: string;
5824
+ readonly routeActivationResolver: RouteActivationResolver;
5728
5825
  private readonly parent?;
5729
5826
  private readonly filter?;
5730
5827
  private readonly eventBus;
5731
5828
  private readonly plugins;
5732
5829
  private readonly services;
5733
5830
  private readonly subContexts;
5831
+ private readonly eventSourceRuntimes;
5734
5832
  private readonly parentEventForwarder?;
5735
5833
  private readonly timers;
5736
5834
  private readonly initialReconnectDelayMs;
@@ -5739,16 +5837,13 @@ declare class Context {
5739
5837
  private state;
5740
5838
  private startPromise?;
5741
5839
  private stopPromise?;
5742
- private eventSubscription?;
5743
- private eventStreamTask?;
5744
- private reconnectTimer?;
5745
- private resolveReconnectTimer?;
5746
5840
  private constructor();
5747
5841
  on<K extends keyof EventMap>(type: K, handler: (event: EventMap[K]) => void | Promise<void>): () => void;
5748
5842
  install<T extends ParameterList, I extends Injection | undefined, OI extends Injection | undefined>(
5749
5843
  plugin: Plugin<T, I, OI>,
5750
5844
  ...args: T
5751
5845
  ): void;
5846
+ installEventSource(eventSource: MilkyEventSource): void;
5752
5847
  provide<T extends object>(service: ServiceClass<T>, instance: T): void;
5753
5848
  resolve<T extends object>(service: ServiceClass<T>): T;
5754
5849
  tryResolve<T extends object>(service: ServiceClass<T>): T | undefined;
@@ -5756,7 +5851,7 @@ declare class Context {
5756
5851
  fork(name: string, filter?: Filter): Context;
5757
5852
  timeout(delayMs: number, callback: () => void | Promise<void>): NodeJS.Timeout;
5758
5853
  interval(intervalMs: number, callback: () => void | Promise<void>): NodeJS.Timeout;
5759
- createSession(message: IncomingMessage): Session;
5854
+ createSession(selfId: number, message: IncomingMessage): Session;
5760
5855
  start(): Promise<void>;
5761
5856
  stop(): Promise<void>;
5762
5857
  private startInternal;
@@ -5765,7 +5860,7 @@ declare class Context {
5765
5860
  private assertCanScheduleTimer;
5766
5861
  private runTimerCallback;
5767
5862
  private clearTimers;
5768
- private stopEventStream;
5863
+ private stopEventSources;
5769
5864
  private acceptsParentEvent;
5770
5865
  private applyPlugins;
5771
5866
  private recursiveApplyPlugins;
@@ -5779,7 +5874,8 @@ declare class Context {
5779
5874
  private createUnresolvablePluginError;
5780
5875
  private getPluginContext;
5781
5876
  private createProxyContextForPlugin;
5782
- private runEventStream;
5877
+ private startEventSource;
5878
+ private runEventSource;
5783
5879
  private waitForReconnectDelay;
5784
5880
  private resolveReconnectDelay;
5785
5881
  static fromUrl(baseUrl: string | URL, options?: ContextOptions & ContextUrlOptions): Context;
@@ -5833,6 +5929,12 @@ export {
5833
5929
  CommandBuilder,
5834
5930
  Context,
5835
5931
  ContextOptions,
5932
+ ContextRouteActivation,
5933
+ ContextRouteActivationByScene,
5934
+ ContextRouteActivationConfig,
5935
+ ContextRouteActivationMatcher,
5936
+ ContextRouteActivationRule,
5937
+ ContextRoutingOptions,
5836
5938
  ContextUrlOptions,
5837
5939
  Disposable,
5838
5940
  EventMap,
@@ -5844,16 +5946,22 @@ export {
5844
5946
  LogMessage,
5845
5947
  Logger,
5846
5948
  MilkyClient,
5949
+ MilkyEventSource,
5847
5950
  MilkyEventSubscription,
5951
+ MilkyWebSocketEventSourceOptions,
5848
5952
  Parameter,
5849
5953
  ParameterList,
5850
5954
  ParamsOf,
5851
5955
  Pattern,
5852
5956
  Plugin,
5853
5957
  RawPattern,
5958
+ RouteActivation,
5959
+ RouteActivationResolver,
5854
5960
  RouteBranch,
5961
+ RouteDescriptor,
5855
5962
  RouteEntry,
5856
5963
  RouteMatchResult,
5964
+ RouteMeta,
5857
5965
  Router,
5858
5966
  ServiceClass,
5859
5967
  Session,
@@ -5861,11 +5969,15 @@ export {
5861
5969
  SessionReplyOptions,
5862
5970
  TypeInstruction,
5863
5971
  combineLogHandlers,
5972
+ createContextRouteActivationResolver,
5864
5973
  createMilkyClient,
5974
+ createMilkyWebSocketEventSource,
5975
+ defaultRouteActivationResolver,
5865
5976
  definePlugin,
5866
5977
  filter,
5867
5978
  implementsESNextDisposable,
5868
5979
  isDisposable,
5980
+ mergeRouteMeta,
5869
5981
  type types_d_exports as milky,
5870
5982
  milkyPackageVersion,
5871
5983
  milkyVersion,