@live-game/sdk 0.5.0 → 2.0.0-dev.8
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/CHANGELOG.md +24 -0
- package/README.md +96 -19
- package/UPGRADING.md +36 -0
- package/dist/app/client.d.ts +112 -0
- package/dist/app/client.js +324 -0
- package/dist/app/contracts.d.ts +180 -0
- package/dist/app/contracts.js +41 -0
- package/dist/app/define-app.d.ts +99 -0
- package/dist/app/define-app.js +19 -0
- package/dist/app/images.d.ts +7 -0
- package/dist/app/images.js +61 -0
- package/dist/app/index.d.ts +7 -0
- package/dist/app/index.js +6 -0
- package/dist/app/live-events.d.ts +161 -0
- package/dist/app/live-events.js +15 -0
- package/dist/app/react.d.ts +101 -0
- package/dist/app/react.js +39 -0
- package/dist/app/types.d.ts +55 -0
- package/dist/app/types.js +1 -0
- package/dist/index.d.ts +1 -6
- package/dist/index.js +1 -5
- package/dist/room-snapshot.d.ts +33 -0
- package/dist/room-snapshot.js +1 -0
- package/package.json +45 -43
- package/dist/createSDK.d.ts +0 -9
- package/dist/createSDK.js +0 -17
- package/dist/mock/index.d.ts +0 -19
- package/dist/mock/index.js +0 -95
- package/dist/mock/platform.d.ts +0 -6
- package/dist/mock/platform.js +0 -45
- package/dist/react/index.d.ts +0 -6
- package/dist/react/index.js +0 -30
- package/dist/types.d.ts +0 -155
- package/dist/types.js +0 -11
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/** Versioned, browser-safe contract shared by every official and third-party app. */
|
|
2
|
+
export declare const APP_PROTOCOL: "live-game-app/2";
|
|
3
|
+
export type AppRole = "runtime" | "control" | "output";
|
|
4
|
+
export type AppKind = "game" | "tool";
|
|
5
|
+
export type AppRunStatus = "stopped" | "starting" | "running" | "stopping" | "failed";
|
|
6
|
+
export declare const APP_CAPABILITY_ROLES: {
|
|
7
|
+
readonly "events.comment": readonly ["runtime"];
|
|
8
|
+
readonly "events.gift": readonly ["runtime"];
|
|
9
|
+
readonly "events.like": readonly ["runtime"];
|
|
10
|
+
readonly "events.enter": readonly ["runtime"];
|
|
11
|
+
readonly "events.follow": readonly ["runtime"];
|
|
12
|
+
readonly "events.share": readonly ["runtime"];
|
|
13
|
+
readonly "events.fansclub": readonly ["runtime"];
|
|
14
|
+
readonly "events.room_stats": readonly ["runtime"];
|
|
15
|
+
readonly "events.room_message": readonly ["runtime"];
|
|
16
|
+
readonly "events.banner": readonly ["runtime"];
|
|
17
|
+
readonly "events.rank": readonly ["runtime"];
|
|
18
|
+
readonly "events.live_end": readonly ["runtime"];
|
|
19
|
+
readonly "events.raw": readonly ["runtime"];
|
|
20
|
+
readonly "room.read": readonly ["runtime", "control", "output"];
|
|
21
|
+
readonly "catalog.gifts": readonly ["runtime", "control", "output"];
|
|
22
|
+
readonly "config.read": readonly ["runtime", "control", "output"];
|
|
23
|
+
readonly "config.write": readonly ["control"];
|
|
24
|
+
readonly "storage.read": readonly ["runtime"];
|
|
25
|
+
readonly "storage.write": readonly ["runtime"];
|
|
26
|
+
readonly "state.read": readonly ["control", "output"];
|
|
27
|
+
readonly "state.publish": readonly ["runtime"];
|
|
28
|
+
readonly "commands.send": readonly ["control"];
|
|
29
|
+
readonly "commands.interact": readonly ["output"];
|
|
30
|
+
readonly "commands.receive": readonly ["runtime"];
|
|
31
|
+
readonly "views.notify": readonly ["runtime"];
|
|
32
|
+
readonly "guide.publish": readonly ["runtime"];
|
|
33
|
+
readonly "audio.speak": readonly ["runtime"];
|
|
34
|
+
readonly "audio.play": readonly ["runtime"];
|
|
35
|
+
readonly "audio.control": readonly ["runtime"];
|
|
36
|
+
readonly "audio.voices": readonly ["runtime", "control"];
|
|
37
|
+
readonly "timers.schedule": readonly ["runtime"];
|
|
38
|
+
readonly "text.similarity": readonly ["runtime"];
|
|
39
|
+
readonly "assets.image": readonly ["runtime", "control", "output"];
|
|
40
|
+
readonly "log.write": readonly ["runtime", "control", "output"];
|
|
41
|
+
};
|
|
42
|
+
export type AppCapability = keyof typeof APP_CAPABILITY_ROLES;
|
|
43
|
+
export type AppConfigField = {
|
|
44
|
+
type: "string" | "number" | "boolean" | "array" | "object";
|
|
45
|
+
default?: unknown;
|
|
46
|
+
[key: string]: unknown;
|
|
47
|
+
};
|
|
48
|
+
export type AppManifestV2 = {
|
|
49
|
+
manifestVersion: 2;
|
|
50
|
+
apiVersion: "2.0";
|
|
51
|
+
id: string;
|
|
52
|
+
name: string;
|
|
53
|
+
version: string;
|
|
54
|
+
kind: AppKind;
|
|
55
|
+
description?: string;
|
|
56
|
+
entries: {
|
|
57
|
+
runtime: string;
|
|
58
|
+
control?: string;
|
|
59
|
+
output?: string;
|
|
60
|
+
};
|
|
61
|
+
/** Additional lightweight views share the same runtime and output permissions. */
|
|
62
|
+
outputViews?: Record<string, {
|
|
63
|
+
name: string;
|
|
64
|
+
entry: string;
|
|
65
|
+
stateKey?: string;
|
|
66
|
+
}>;
|
|
67
|
+
requires?: {
|
|
68
|
+
live?: boolean;
|
|
69
|
+
};
|
|
70
|
+
permissions: AppCapability[];
|
|
71
|
+
configSchema?: Record<string, AppConfigField>;
|
|
72
|
+
};
|
|
73
|
+
export type AppInstanceSnapshot = {
|
|
74
|
+
id: string;
|
|
75
|
+
appId: string;
|
|
76
|
+
version: string;
|
|
77
|
+
kind: AppKind;
|
|
78
|
+
name: string;
|
|
79
|
+
runId: string | null;
|
|
80
|
+
status: AppRunStatus;
|
|
81
|
+
config: Record<string, unknown>;
|
|
82
|
+
state: unknown;
|
|
83
|
+
error?: string;
|
|
84
|
+
};
|
|
85
|
+
export type AppGift = {
|
|
86
|
+
platform: "douyin";
|
|
87
|
+
giftId: string;
|
|
88
|
+
giftName: string;
|
|
89
|
+
iconUrl?: string;
|
|
90
|
+
diamondValue?: number;
|
|
91
|
+
source: "builtin" | "catalog" | "observed";
|
|
92
|
+
};
|
|
93
|
+
export type AppGiftQuery = {
|
|
94
|
+
platform: "douyin";
|
|
95
|
+
query?: string;
|
|
96
|
+
ids?: string[];
|
|
97
|
+
};
|
|
98
|
+
export type AppGiftCatalog = {
|
|
99
|
+
version: string;
|
|
100
|
+
items: AppGift[];
|
|
101
|
+
};
|
|
102
|
+
/** Content only. Presentation and placement are owned by the host and streamer. */
|
|
103
|
+
export type AppGuideItem = {
|
|
104
|
+
id: string;
|
|
105
|
+
label: string;
|
|
106
|
+
effect: string;
|
|
107
|
+
detail?: string;
|
|
108
|
+
gift?: {
|
|
109
|
+
platform: "douyin";
|
|
110
|
+
giftId: string;
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
export type AppGuide = {
|
|
114
|
+
items: AppGuideItem[];
|
|
115
|
+
footer?: string;
|
|
116
|
+
};
|
|
117
|
+
/** Built-in output; does not load an application entry or start another runtime. */
|
|
118
|
+
export declare const APP_GUIDE_VIEW: "platform:guide";
|
|
119
|
+
export type AppBootstrap = {
|
|
120
|
+
protocol: typeof APP_PROTOCOL;
|
|
121
|
+
instanceId: string;
|
|
122
|
+
runId: string | null;
|
|
123
|
+
role: AppRole;
|
|
124
|
+
capabilities: AppCapability[];
|
|
125
|
+
config: Record<string, unknown>;
|
|
126
|
+
state: unknown;
|
|
127
|
+
status?: AppRunStatus;
|
|
128
|
+
error?: string;
|
|
129
|
+
};
|
|
130
|
+
export type AppRpcRequest = {
|
|
131
|
+
protocol: typeof APP_PROTOCOL;
|
|
132
|
+
type: "request";
|
|
133
|
+
id: string;
|
|
134
|
+
runId: string | null;
|
|
135
|
+
method: AppCapability;
|
|
136
|
+
args: unknown[];
|
|
137
|
+
};
|
|
138
|
+
export type AppRpcResponse = {
|
|
139
|
+
protocol: typeof APP_PROTOCOL;
|
|
140
|
+
type: "response";
|
|
141
|
+
id: string;
|
|
142
|
+
result?: unknown;
|
|
143
|
+
error?: {
|
|
144
|
+
code: string;
|
|
145
|
+
message: string;
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
export type AppRpcEvent = {
|
|
149
|
+
protocol: typeof APP_PROTOCOL;
|
|
150
|
+
type: "event";
|
|
151
|
+
event: string;
|
|
152
|
+
data: unknown;
|
|
153
|
+
runId: string | null;
|
|
154
|
+
deliveryId?: string;
|
|
155
|
+
};
|
|
156
|
+
export type AppRpcAck = {
|
|
157
|
+
protocol: typeof APP_PROTOCOL;
|
|
158
|
+
type: "ack";
|
|
159
|
+
id: string;
|
|
160
|
+
error?: {
|
|
161
|
+
code: string;
|
|
162
|
+
message: string;
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
export type AppRuntimeSignal = {
|
|
166
|
+
protocol: typeof APP_PROTOCOL;
|
|
167
|
+
type: "ready" | "fault";
|
|
168
|
+
message?: string;
|
|
169
|
+
};
|
|
170
|
+
export type AppLiveEnvelope<T = unknown> = {
|
|
171
|
+
schemaVersion: 1;
|
|
172
|
+
eventId: string;
|
|
173
|
+
sessionId: string;
|
|
174
|
+
sequence: number;
|
|
175
|
+
type: string;
|
|
176
|
+
sourceTs: number;
|
|
177
|
+
receivedAt: number;
|
|
178
|
+
sourceEventId?: string;
|
|
179
|
+
payload: T;
|
|
180
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Generated from packages/live-core/src/app-protocol.ts. Do not edit this copy.
|
|
2
|
+
/** Versioned, browser-safe contract shared by every official and third-party app. */
|
|
3
|
+
export const APP_PROTOCOL = "live-game-app/2";
|
|
4
|
+
export const APP_CAPABILITY_ROLES = {
|
|
5
|
+
"events.comment": ["runtime"],
|
|
6
|
+
"events.gift": ["runtime"],
|
|
7
|
+
"events.like": ["runtime"],
|
|
8
|
+
"events.enter": ["runtime"],
|
|
9
|
+
"events.follow": ["runtime"],
|
|
10
|
+
"events.share": ["runtime"],
|
|
11
|
+
"events.fansclub": ["runtime"],
|
|
12
|
+
"events.room_stats": ["runtime"],
|
|
13
|
+
"events.room_message": ["runtime"],
|
|
14
|
+
"events.banner": ["runtime"],
|
|
15
|
+
"events.rank": ["runtime"],
|
|
16
|
+
"events.live_end": ["runtime"],
|
|
17
|
+
"events.raw": ["runtime"],
|
|
18
|
+
"room.read": ["runtime", "control", "output"],
|
|
19
|
+
"catalog.gifts": ["runtime", "control", "output"],
|
|
20
|
+
"config.read": ["runtime", "control", "output"],
|
|
21
|
+
"config.write": ["control"],
|
|
22
|
+
"storage.read": ["runtime"],
|
|
23
|
+
"storage.write": ["runtime"],
|
|
24
|
+
"state.read": ["control", "output"],
|
|
25
|
+
"state.publish": ["runtime"],
|
|
26
|
+
"commands.send": ["control"],
|
|
27
|
+
"commands.interact": ["output"],
|
|
28
|
+
"commands.receive": ["runtime"],
|
|
29
|
+
"views.notify": ["runtime"],
|
|
30
|
+
"guide.publish": ["runtime"],
|
|
31
|
+
"audio.speak": ["runtime"],
|
|
32
|
+
"audio.play": ["runtime"],
|
|
33
|
+
"audio.control": ["runtime"],
|
|
34
|
+
"audio.voices": ["runtime", "control"],
|
|
35
|
+
"timers.schedule": ["runtime"],
|
|
36
|
+
"text.similarity": ["runtime"],
|
|
37
|
+
"assets.image": ["runtime", "control", "output"],
|
|
38
|
+
"log.write": ["runtime", "control", "output"],
|
|
39
|
+
};
|
|
40
|
+
/** Built-in output; does not load an application entry or start another runtime. */
|
|
41
|
+
export const APP_GUIDE_VIEW = "platform:guide";
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { type AppSDK, type AppSDKConnection } from "./client.js";
|
|
2
|
+
import type { AppCommand } from "./types.js";
|
|
3
|
+
/** Register handlers and finish initialization before the host delivers live input. */
|
|
4
|
+
export declare function defineApp<State = unknown, Config extends object = Record<string, unknown>, Command extends AppCommand = AppCommand>(setup: (sdk: AppSDK<State, Config, Command>) => unknown | Promise<unknown>, connection?: AppSDKConnection): Promise<{
|
|
5
|
+
ready: Promise<import("./types.js").AppContext<State, Config>>;
|
|
6
|
+
dispose: (error?: import("./client.js").AppSDKError) => void;
|
|
7
|
+
call: <T = unknown>(method: import("./contracts.js").AppCapability, ...args: unknown[]) => Promise<T>;
|
|
8
|
+
on: {
|
|
9
|
+
<K extends keyof {
|
|
10
|
+
"app.context": import("./types.js").AppContext<State_1, Config_1>;
|
|
11
|
+
"app.stop": null;
|
|
12
|
+
state: State_1 | null;
|
|
13
|
+
config: Config_1;
|
|
14
|
+
timer: import("./types.js").AppTimerEvent;
|
|
15
|
+
"audio.job": import("./types.js").AudioJobEvent;
|
|
16
|
+
command: Command_1;
|
|
17
|
+
"app.message": AppCommand;
|
|
18
|
+
"live.status": import("./types.js").AppSourceStatus;
|
|
19
|
+
}>(event: K, handler: (value: {
|
|
20
|
+
"app.context": import("./types.js").AppContext<State, Config>;
|
|
21
|
+
"app.stop": null;
|
|
22
|
+
state: State | null;
|
|
23
|
+
config: Config;
|
|
24
|
+
timer: import("./types.js").AppTimerEvent;
|
|
25
|
+
"audio.job": import("./types.js").AudioJobEvent;
|
|
26
|
+
command: Command;
|
|
27
|
+
"app.message": AppCommand;
|
|
28
|
+
"live.status": import("./types.js").AppSourceStatus;
|
|
29
|
+
}[K]) => unknown): () => void;
|
|
30
|
+
<T = unknown>(event: string, handler: (value: NoInfer<T>) => unknown): () => void;
|
|
31
|
+
};
|
|
32
|
+
readonly disposed: boolean;
|
|
33
|
+
lifecycle: {
|
|
34
|
+
ready: () => Promise<void>;
|
|
35
|
+
fail: (message: string) => Promise<void>;
|
|
36
|
+
onStop: (handler: () => unknown) => () => void;
|
|
37
|
+
};
|
|
38
|
+
readonly context: import("./types.js").AppContext<State, Config> | undefined;
|
|
39
|
+
supports: (capability: import("./contracts.js").AppCapability) => boolean;
|
|
40
|
+
config: {
|
|
41
|
+
get: () => Promise<Config>;
|
|
42
|
+
update: (values: Partial<Config>) => Promise<Config>;
|
|
43
|
+
onChange: (handler: (value: Config) => unknown) => () => void;
|
|
44
|
+
};
|
|
45
|
+
storage: {
|
|
46
|
+
get: <T = unknown>(key: string) => Promise<T | null>;
|
|
47
|
+
set: (key: string, value: unknown) => Promise<null>;
|
|
48
|
+
delete: (key: string) => Promise<null>;
|
|
49
|
+
};
|
|
50
|
+
state: {
|
|
51
|
+
get: <T = State>() => Promise<T | null>;
|
|
52
|
+
publish: (value: State) => Promise<null>;
|
|
53
|
+
onChange: (handler: (value: State | null) => unknown) => () => void;
|
|
54
|
+
};
|
|
55
|
+
commands: {
|
|
56
|
+
send: (...[type, payload]: import("./types.js").AppCommandArgs<Command>) => Promise<null>;
|
|
57
|
+
on: <T = Command>(handler: (value: NoInfer<T>) => unknown) => () => void;
|
|
58
|
+
};
|
|
59
|
+
views: {
|
|
60
|
+
notify: (type: string, payload?: unknown) => Promise<null>;
|
|
61
|
+
on: <T = AppCommand>(handler: (value: T) => unknown) => () => void;
|
|
62
|
+
};
|
|
63
|
+
live: {
|
|
64
|
+
room: () => Promise<import("./index.js").LiveRoomSnapshot | null>;
|
|
65
|
+
on: {
|
|
66
|
+
<K extends import("./live-events.js").LiveEventType>(type: K, handler: (event: import("./types.js").AppLiveEventMap[K], envelope: import("./contracts.js").AppLiveEnvelope<import("./types.js").AppLiveEventMap[K]>) => unknown): () => void;
|
|
67
|
+
<T>(type: import("./live-events.js").LiveEventType, handler: (event: NoInfer<T>, envelope: import("./contracts.js").AppLiveEnvelope<NoInfer<T>>) => unknown): () => void;
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
catalog: {
|
|
71
|
+
gifts: (query: import("./contracts.js").AppGiftQuery) => Promise<import("./contracts.js").AppGiftCatalog>;
|
|
72
|
+
};
|
|
73
|
+
guide: {
|
|
74
|
+
publish: (content: import("./contracts.js").AppGuide) => Promise<void>;
|
|
75
|
+
};
|
|
76
|
+
audio: {
|
|
77
|
+
speak: (text: string, options?: import("./types.js").SpeechOptions) => Promise<{
|
|
78
|
+
jobId: string;
|
|
79
|
+
}>;
|
|
80
|
+
play: (asset: string, options?: import("./types.js").AudioPlayOptions) => Promise<{
|
|
81
|
+
jobId: string;
|
|
82
|
+
}>;
|
|
83
|
+
cancel: (jobId: string) => Promise<null>;
|
|
84
|
+
voices: () => Promise<import("./types.js").AudioVoice[]>;
|
|
85
|
+
onJob: (handler: (event: import("./types.js").AudioJobEvent) => unknown) => () => void;
|
|
86
|
+
};
|
|
87
|
+
timers: {
|
|
88
|
+
set: (key: string, durationMs: number) => Promise<null>;
|
|
89
|
+
cancel: (key: string) => Promise<null>;
|
|
90
|
+
on: (handler: (event: import("./types.js").AppTimerEvent) => unknown) => () => void;
|
|
91
|
+
};
|
|
92
|
+
text: {
|
|
93
|
+
similarity: (first: string, second: string) => Promise<number>;
|
|
94
|
+
};
|
|
95
|
+
assets: {
|
|
96
|
+
image: (url: string) => Promise<string>;
|
|
97
|
+
};
|
|
98
|
+
log: (level: "info" | "warn" | "error", message: string) => Promise<null>;
|
|
99
|
+
} | null>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createAppSDK } from "./client.js";
|
|
2
|
+
/** Register handlers and finish initialization before the host delivers live input. */
|
|
3
|
+
export async function defineApp(setup, connection) {
|
|
4
|
+
const sdk = createAppSDK(connection);
|
|
5
|
+
try {
|
|
6
|
+
await sdk.ready;
|
|
7
|
+
const cleanup = await setup(sdk);
|
|
8
|
+
if (typeof cleanup === "function")
|
|
9
|
+
sdk.lifecycle.onStop(() => cleanup());
|
|
10
|
+
await sdk.lifecycle.ready();
|
|
11
|
+
return sdk;
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
const message = error instanceof Error ? error.message : "应用初始化失败";
|
|
15
|
+
await sdk.lifecycle.fail(message).catch(() => { });
|
|
16
|
+
sdk.dispose();
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type AppSDK } from "./client.js";
|
|
2
|
+
/** Bounded, deduplicated image requests for one runtime or view. URLs are renewed per client lifetime. */
|
|
3
|
+
export declare function createImageResolver(sdk: Pick<AppSDK, "assets" | "supports">): {
|
|
4
|
+
cached: (input: string | undefined) => string | undefined;
|
|
5
|
+
resolve(input: string): Promise<string>;
|
|
6
|
+
dispose(): void;
|
|
7
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { AppSDKError } from "./client.js";
|
|
2
|
+
/** Bounded, deduplicated image requests for one runtime or view. URLs are renewed per client lifetime. */
|
|
3
|
+
export function createImageResolver(sdk) {
|
|
4
|
+
const resolved = new Map();
|
|
5
|
+
const pending = new Map();
|
|
6
|
+
const queue = [];
|
|
7
|
+
let active = 0, disposed = false;
|
|
8
|
+
function pump() {
|
|
9
|
+
while (!disposed && active < 4 && queue.length) {
|
|
10
|
+
const job = queue.shift();
|
|
11
|
+
active++;
|
|
12
|
+
void sdk.assets
|
|
13
|
+
.image(job.input)
|
|
14
|
+
.then((value) => {
|
|
15
|
+
if (disposed)
|
|
16
|
+
throw new AppSDKError("DISPOSED", "图片解析器已关闭");
|
|
17
|
+
resolved.set(job.input, value);
|
|
18
|
+
while (resolved.size > 1024)
|
|
19
|
+
resolved.delete(resolved.keys().next().value);
|
|
20
|
+
job.resolve(value);
|
|
21
|
+
})
|
|
22
|
+
.catch(job.reject)
|
|
23
|
+
.finally(() => {
|
|
24
|
+
active--;
|
|
25
|
+
pending.delete(job.input);
|
|
26
|
+
pump();
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
cached: (input) => (input ? resolved.get(input) : undefined),
|
|
32
|
+
resolve(input) {
|
|
33
|
+
if (disposed)
|
|
34
|
+
return Promise.reject(new AppSDKError("DISPOSED", "图片解析器已关闭"));
|
|
35
|
+
if (!sdk.supports("assets.image"))
|
|
36
|
+
return Promise.reject(new AppSDKError("CAPABILITY_DISABLED", "当前宿主未提供图片能力"));
|
|
37
|
+
const cached = resolved.get(input);
|
|
38
|
+
if (cached)
|
|
39
|
+
return Promise.resolve(cached);
|
|
40
|
+
const current = pending.get(input);
|
|
41
|
+
if (current)
|
|
42
|
+
return current;
|
|
43
|
+
if (queue.length >= 128)
|
|
44
|
+
return Promise.reject(new AppSDKError("QUEUE_FULL", "图片请求过多,请分批读取"));
|
|
45
|
+
const task = new Promise((resolve, reject) => {
|
|
46
|
+
queue.push({ input, resolve, reject });
|
|
47
|
+
});
|
|
48
|
+
pending.set(input, task);
|
|
49
|
+
pump();
|
|
50
|
+
return task;
|
|
51
|
+
},
|
|
52
|
+
dispose() {
|
|
53
|
+
disposed = true;
|
|
54
|
+
resolved.clear();
|
|
55
|
+
for (const job of queue.splice(0)) {
|
|
56
|
+
pending.delete(job.input);
|
|
57
|
+
job.reject(new AppSDKError("DISPOSED", "图片解析器已关闭"));
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./contracts.js";
|
|
2
|
+
export * from "./types.js";
|
|
3
|
+
export * from "./live-events.js";
|
|
4
|
+
export type * from "../room-snapshot.js";
|
|
5
|
+
export { createAppSDK, AppSDKError, type AppSDK, type AppSDKConnection } from "./client.js";
|
|
6
|
+
export { defineApp } from "./define-app.js";
|
|
7
|
+
export { createImageResolver } from "./images.js";
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
export type LiveProviderKind = "official" | "collector" | "mock";
|
|
2
|
+
export type LivePlatformId = "douyin" | "kuaishou" | "mock";
|
|
3
|
+
export type LiveCaptureMode = LiveProviderKind;
|
|
4
|
+
export declare const LIVE_EVENT_TYPES: readonly ["comment", "like", "gift", "enter", "follow", "share", "fansclub", "room_stats", "room_message", "banner", "rank", "live_end", "raw"];
|
|
5
|
+
export type LiveEventType = (typeof LIVE_EVENT_TYPES)[number];
|
|
6
|
+
export type BaseLiveEvent = {
|
|
7
|
+
roomId: string;
|
|
8
|
+
liveId?: string;
|
|
9
|
+
userId: string;
|
|
10
|
+
liveUserIdSource?: string;
|
|
11
|
+
nickname: string;
|
|
12
|
+
avatar?: string;
|
|
13
|
+
ts: number;
|
|
14
|
+
/** Platform namespace, independent from how the event was captured. */
|
|
15
|
+
platform?: LivePlatformId;
|
|
16
|
+
/** How this platform event entered this application. */
|
|
17
|
+
capture?: LiveCaptureMode;
|
|
18
|
+
/** Stable message identifier when the platform exposes one. */
|
|
19
|
+
sourceEventId?: string;
|
|
20
|
+
batchId?: string;
|
|
21
|
+
source?: LiveProviderKind;
|
|
22
|
+
};
|
|
23
|
+
export type EventSource = {
|
|
24
|
+
platform: LivePlatformId;
|
|
25
|
+
capture: LiveCaptureMode;
|
|
26
|
+
roomId: string;
|
|
27
|
+
liveId?: string;
|
|
28
|
+
};
|
|
29
|
+
export type LiveEventEnvelope<T extends LiveEvent = LiveEvent> = {
|
|
30
|
+
schemaVersion: 1;
|
|
31
|
+
eventId: string;
|
|
32
|
+
sourceEventId?: string;
|
|
33
|
+
sessionId: string;
|
|
34
|
+
sequence: number;
|
|
35
|
+
type: T["type"];
|
|
36
|
+
sourceTs?: number;
|
|
37
|
+
receivedAt: number;
|
|
38
|
+
source: EventSource;
|
|
39
|
+
payload: T;
|
|
40
|
+
};
|
|
41
|
+
export type CommentEvent = BaseLiveEvent & {
|
|
42
|
+
type: "comment";
|
|
43
|
+
text: string;
|
|
44
|
+
similarity?: number;
|
|
45
|
+
};
|
|
46
|
+
export type GiftEvent = BaseLiveEvent & {
|
|
47
|
+
type: "gift";
|
|
48
|
+
giftId: string;
|
|
49
|
+
giftName?: string;
|
|
50
|
+
/** Newly observed gift units. A phase-only combo completion has zero units. */
|
|
51
|
+
count: number;
|
|
52
|
+
value?: number;
|
|
53
|
+
comboId?: string;
|
|
54
|
+
/** Cumulative units, only when the source exposes a real combo identifier. */
|
|
55
|
+
comboTotal?: number;
|
|
56
|
+
comboPhase?: "started" | "updated" | "ended";
|
|
57
|
+
};
|
|
58
|
+
export type LikeEvent = BaseLiveEvent & {
|
|
59
|
+
type: "like";
|
|
60
|
+
count: number;
|
|
61
|
+
/** Platform-reported total, never reconstructed from locally received increments. */
|
|
62
|
+
total?: number;
|
|
63
|
+
};
|
|
64
|
+
export type EnterEvent = BaseLiveEvent & {
|
|
65
|
+
type: "enter";
|
|
66
|
+
};
|
|
67
|
+
export type FollowEvent = BaseLiveEvent & {
|
|
68
|
+
type: "follow";
|
|
69
|
+
};
|
|
70
|
+
export type ShareEvent = BaseLiveEvent & {
|
|
71
|
+
type: "share";
|
|
72
|
+
target?: string;
|
|
73
|
+
count?: number;
|
|
74
|
+
};
|
|
75
|
+
export type FansclubEvent = BaseLiveEvent & {
|
|
76
|
+
type: "fansclub";
|
|
77
|
+
action?: number;
|
|
78
|
+
content?: string;
|
|
79
|
+
leftDiamond?: number;
|
|
80
|
+
};
|
|
81
|
+
export type RoomStatsEvent = {
|
|
82
|
+
type: "room_stats";
|
|
83
|
+
roomId: string;
|
|
84
|
+
liveId?: string;
|
|
85
|
+
/** Missing means unknown; heat/popularity and cumulative viewers are separate metrics. */
|
|
86
|
+
online?: number;
|
|
87
|
+
platformLikes?: number;
|
|
88
|
+
totalViewers?: number;
|
|
89
|
+
ts: number;
|
|
90
|
+
platform?: LivePlatformId;
|
|
91
|
+
capture?: LiveCaptureMode;
|
|
92
|
+
sourceEventId?: string;
|
|
93
|
+
batchId?: string;
|
|
94
|
+
source?: LiveProviderKind;
|
|
95
|
+
};
|
|
96
|
+
export type RoomMessageEvent = {
|
|
97
|
+
type: "room_message";
|
|
98
|
+
roomId: string;
|
|
99
|
+
liveId?: string;
|
|
100
|
+
content?: string;
|
|
101
|
+
roomMessageType?: number;
|
|
102
|
+
ts: number;
|
|
103
|
+
platform?: LivePlatformId;
|
|
104
|
+
capture?: LiveCaptureMode;
|
|
105
|
+
sourceEventId?: string;
|
|
106
|
+
batchId?: string;
|
|
107
|
+
source?: LiveProviderKind;
|
|
108
|
+
};
|
|
109
|
+
export type BannerEvent = {
|
|
110
|
+
type: "banner";
|
|
111
|
+
roomId: string;
|
|
112
|
+
liveId?: string;
|
|
113
|
+
position?: number;
|
|
114
|
+
actionType?: number;
|
|
115
|
+
url?: string;
|
|
116
|
+
extra?: string;
|
|
117
|
+
ts: number;
|
|
118
|
+
platform?: LivePlatformId;
|
|
119
|
+
capture?: LiveCaptureMode;
|
|
120
|
+
sourceEventId?: string;
|
|
121
|
+
batchId?: string;
|
|
122
|
+
source?: LiveProviderKind;
|
|
123
|
+
};
|
|
124
|
+
export type RankEvent = {
|
|
125
|
+
type: "rank";
|
|
126
|
+
roomId: string;
|
|
127
|
+
liveId?: string;
|
|
128
|
+
count?: number;
|
|
129
|
+
ts: number;
|
|
130
|
+
platform?: LivePlatformId;
|
|
131
|
+
capture?: LiveCaptureMode;
|
|
132
|
+
sourceEventId?: string;
|
|
133
|
+
batchId?: string;
|
|
134
|
+
source?: LiveProviderKind;
|
|
135
|
+
};
|
|
136
|
+
export type LiveEndEvent = {
|
|
137
|
+
type: "live_end";
|
|
138
|
+
roomId: string;
|
|
139
|
+
liveId?: string;
|
|
140
|
+
action?: number;
|
|
141
|
+
tips?: string;
|
|
142
|
+
ts: number;
|
|
143
|
+
platform?: LivePlatformId;
|
|
144
|
+
capture?: LiveCaptureMode;
|
|
145
|
+
sourceEventId?: string;
|
|
146
|
+
batchId?: string;
|
|
147
|
+
source?: LiveProviderKind;
|
|
148
|
+
};
|
|
149
|
+
export type RawEvent = {
|
|
150
|
+
type: "raw";
|
|
151
|
+
roomId: string;
|
|
152
|
+
liveId?: string;
|
|
153
|
+
method: string;
|
|
154
|
+
ts: number;
|
|
155
|
+
platform?: LivePlatformId;
|
|
156
|
+
capture?: LiveCaptureMode;
|
|
157
|
+
sourceEventId?: string;
|
|
158
|
+
batchId?: string;
|
|
159
|
+
source?: LiveProviderKind;
|
|
160
|
+
};
|
|
161
|
+
export type LiveEvent = CommentEvent | GiftEvent | LikeEvent | EnterEvent | FollowEvent | ShareEvent | FansclubEvent | RoomStatsEvent | RoomMessageEvent | BannerEvent | RankEvent | LiveEndEvent | RawEvent;
|