loopzero-gateway-feishu-lark 0.0.0-stage → 0.2.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.
@@ -0,0 +1,262 @@
1
+ import { BasePlatformAdapter, OutboundMessage, SendResult, MessageEvent } from 'loopzero-gateway';
2
+
3
+ /**
4
+ * Options and seams for `FeishuLarkAdapter`.
5
+ *
6
+ * @public
7
+ */
8
+ /**
9
+ * What one open-platform call answered: `code` 0 is success, anything else is Lark's own error
10
+ * code with its `msg`.
11
+ *
12
+ * @public
13
+ */
14
+ interface LarkApiResult {
15
+ readonly code: number;
16
+ readonly msg?: string;
17
+ /** The created or replied message's id, on a successful send. */
18
+ readonly messageId?: string;
19
+ }
20
+ /**
21
+ * Callbacks the long connection reports to.
22
+ *
23
+ * @public
24
+ */
25
+ interface LarkConnectionHandlers {
26
+ /** One `im.message.receive_v1` event, as the SDK's dispatcher hands it over. Must not throw. */
27
+ readonly onMessage: (data: unknown) => void;
28
+ /** The SDK gave up: initial connect refused for good, or its reconnects exhausted (D7). */
29
+ readonly onError: (err: Error) => void;
30
+ }
31
+ /**
32
+ * The part of the Lark open platform the adapter uses. The default is built from
33
+ * `@larksuiteoapi/node-sdk`; tests pass a scripted one.
34
+ *
35
+ * @public
36
+ */
37
+ interface LarkTransport {
38
+ /** Fetch a tenant access token, which proves the app id and secret. */
39
+ tenantToken(): Promise<LarkApiResult>;
40
+ /** Open the long connection; resolves once it is up, rejects when the first connect fails. */
41
+ start(handlers: LarkConnectionHandlers): Promise<void>;
42
+ /** Send `text` as a new message to the chat `chatId`. */
43
+ createMessage(chatId: string, text: string): Promise<LarkApiResult>;
44
+ /** Send `text` as a reply to the message `messageId`. */
45
+ replyMessage(messageId: string, text: string): Promise<LarkApiResult>;
46
+ /**
47
+ * Send `GET /open-apis/im/v1/messages/{messageId}/resources/{fileKey}?type=file` with a tenant
48
+ * access token, under `signal`, or answer why it did not (row H-2). Optional: without it, an
49
+ * inbound audio message is skipped. The answer is never parsed here; `deferInboundAudio` bounds it.
50
+ */
51
+ downloadResource?(messageId: string, fileKey: string, signal: AbortSignal): Promise<Response | {
52
+ readonly reason: string;
53
+ }>;
54
+ /** Close the long connection. Idempotent. */
55
+ close(): void;
56
+ }
57
+ /**
58
+ * Construction options for `FeishuLarkAdapter`.
59
+ *
60
+ * @public
61
+ */
62
+ interface FeishuLarkAdapterOptions {
63
+ /** The app's App ID, `cli_` followed by 16 hex characters. */
64
+ readonly appId: string;
65
+ /**
66
+ * The feishu-lark app's App Secret. With the App ID it fetches the tenant access token every call
67
+ * uses and authenticates the long connection.
68
+ *
69
+ * @platform-term App Secret
70
+ * @issued-at the Feishu or Lark developer console, Credentials & Basic Info page of the custom app
71
+ */
72
+ readonly appSecret: string;
73
+ /** Which open platform the app lives on: `"feishu"` (open.feishu.cn, the default) or `"lark"` (open.larksuite.com). */
74
+ readonly domain?: "feishu" | "lark";
75
+ /** Test seam: the open-platform transport. Default one built from `@larksuiteoapi/node-sdk`. */
76
+ readonly transport?: LarkTransport;
77
+ }
78
+
79
+ /**
80
+ * `FeishuLarkAdapter` — `BasePlatformAdapter` over the Feishu/Lark open platform: events from the
81
+ * SDK's long connection, replies through `im.message.create` and `im.message.reply`.
82
+ *
83
+ * @public
84
+ */
85
+
86
+ /**
87
+ * Feishu/Lark adapter for one custom app.
88
+ *
89
+ * `connect()` fetches a tenant access token, which proves the App ID and Secret, then opens the
90
+ * long connection; it answers `false`, never a throw, when either fails or 10000 ms pass. Each
91
+ * `im.message.receive_v1` text or audio message from a user becomes a `FeishuLarkMessageEvent` once (the
92
+ * last 1000 event ids are remembered, so Lark's redelivery is not answered twice), delivered after
93
+ * the dispatcher has returned, so the SDK acknowledges it inside Lark's window. The SDK's own
94
+ * reconnect stays on; when it gives up, the adapter logs one line and reads as disconnected (D7).
95
+ *
96
+ * @public
97
+ */
98
+ declare class FeishuLarkAdapter extends BasePlatformAdapter {
99
+ readonly platform: "feishu_lark";
100
+ private readonly transport;
101
+ private readonly seen;
102
+ private handler;
103
+ private connected;
104
+ private connecting;
105
+ private warnedAboutHtml;
106
+ /** Bumped by every `disconnect()` and every connect that gave up, so a late answer is dropped. */
107
+ private generation;
108
+ /** @throws Error when `appId` is not `cli_` followed by 16 hex characters */
109
+ constructor(opts: FeishuLarkAdapterOptions);
110
+ connect(): Promise<boolean>;
111
+ disconnect(): Promise<void>;
112
+ sendMessage(out: OutboundMessage): Promise<SendResult>;
113
+ private sendText;
114
+ onInbound(handler: (event: MessageEvent) => Promise<void>): () => void;
115
+ /** Deliver an event that arrived out of band, through `runHandler`. */
116
+ deliver(event: MessageEvent): Promise<"ok" | "no_handler" | "handler_threw">;
117
+ /** The text to send: markdown loses its markers, html goes as is with one warning. */
118
+ protected outboundText(out: OutboundMessage): string;
119
+ /**
120
+ * Prove the credentials, then open the long connection. An attempt overtaken by `disconnect()`,
121
+ * or by its own deadline, stops at the next step and leaves nothing open.
122
+ */
123
+ private openSession;
124
+ /** The transport's resource download, bound to it, or `undefined` when it has none (row H-2). */
125
+ private resourceDownload;
126
+ private onConnectionLost;
127
+ /** Runs inside the SDK's dispatcher: it must return at once and never throw. */
128
+ private onMessage;
129
+ }
130
+
131
+ /**
132
+ * `feishuLarkChannel` — the Feishu/Lark channel described as data, with environment bindings.
133
+ *
134
+ * Typed locally on purpose: the object is structurally a `ChannelDescriptor`, but a published
135
+ * `.d.ts` must not name core types an older peer lacks.
136
+ */
137
+
138
+ type Config = Readonly<Record<string, unknown>>;
139
+ /** One configuration field, shaped like the core's `ChannelConfigField` (typed locally). */
140
+ interface ConfigField {
141
+ readonly key: string;
142
+ readonly required: boolean;
143
+ readonly secret: boolean;
144
+ readonly description: string;
145
+ readonly env: string;
146
+ }
147
+ /**
148
+ * The Feishu/Lark channel: it opens its own long connection in `connect()`, so it needs no
149
+ * webhook route.
150
+ *
151
+ * `create` refuses a missing or wrongly typed value, a malformed `appId` and an unknown `domain`
152
+ * with `GatewayConfigurationError` (prefix `gateway-feishu-lark`) (code `invalid_config`, the key in the message); it
153
+ * performs no network call.
154
+ *
155
+ * @public
156
+ */
157
+ declare const feishuLarkChannel: {
158
+ platform: "feishu_lark";
159
+ transport: "socket";
160
+ senderIdentity: "authenticated";
161
+ config: readonly ConfigField[];
162
+ create(config: Config): FeishuLarkAdapter;
163
+ };
164
+
165
+ /**
166
+ * The default `LarkTransport`, over `@larksuiteoapi/node-sdk`'s `Client` and long-connection
167
+ * `WSClient`. The SDK is an optional peer, loaded on first use.
168
+ */
169
+
170
+ interface SdkApiAnswer {
171
+ readonly code?: number;
172
+ readonly msg?: string;
173
+ readonly data?: {
174
+ readonly message_id?: string;
175
+ };
176
+ }
177
+ /** What `auth/v3/tenant_access_token/internal` answers: the token sits beside `code`. */
178
+ interface SdkTokenAnswer extends SdkApiAnswer {
179
+ readonly tenant_access_token?: string;
180
+ /** Seconds until the token expires. */
181
+ readonly expire?: number;
182
+ }
183
+ /** The part of `@larksuiteoapi/node-sdk` this transport uses. */
184
+ interface LarkSdk {
185
+ readonly Domain: {
186
+ readonly Feishu: number;
187
+ readonly Lark: number;
188
+ };
189
+ readonly Client: new (params: {
190
+ appId: string;
191
+ appSecret: string;
192
+ domain: number;
193
+ }) => {
194
+ readonly auth: {
195
+ readonly tenantAccessToken: {
196
+ internal(payload: {
197
+ data: {
198
+ app_id: string;
199
+ app_secret: string;
200
+ };
201
+ }): Promise<SdkTokenAnswer>;
202
+ };
203
+ };
204
+ readonly im: {
205
+ readonly message: {
206
+ create(payload: {
207
+ params: {
208
+ receive_id_type: "chat_id";
209
+ };
210
+ data: {
211
+ receive_id: string;
212
+ msg_type: "text";
213
+ content: string;
214
+ };
215
+ }): Promise<SdkApiAnswer>;
216
+ reply(payload: {
217
+ path: {
218
+ message_id: string;
219
+ };
220
+ data: {
221
+ msg_type: "text";
222
+ content: string;
223
+ };
224
+ }): Promise<SdkApiAnswer>;
225
+ };
226
+ };
227
+ };
228
+ readonly WSClient: new (params: {
229
+ appId: string;
230
+ appSecret: string;
231
+ domain: number;
232
+ onReady?: () => void;
233
+ onError?: (err: Error) => void;
234
+ }) => {
235
+ start(params: {
236
+ eventDispatcher: unknown;
237
+ }): Promise<void>;
238
+ close(): void;
239
+ };
240
+ readonly EventDispatcher: new (params: Record<string, never>) => {
241
+ register(handles: Record<string, (data: unknown) => Promise<void>>): unknown;
242
+ };
243
+ }
244
+ /** Options the default transport needs. */
245
+ interface LarkTransportOptions {
246
+ readonly appId: string;
247
+ readonly appSecret: string;
248
+ readonly domain?: "feishu" | "lark";
249
+ }
250
+ /**
251
+ * Build the transport over the SDK. `load` is the test seam: it returns the SDK module.
252
+ *
253
+ * The long connection keeps the SDK's own reconnect (`autoReconnect` left at its default): the SDK
254
+ * retries on the interval Feishu's servers send, and the adapter runs no loop of its own (plan D7).
255
+ *
256
+ * `downloadResource` reads a message resource with plain `fetch` (the second seam), so the size and
257
+ * deadline bounds of `deferInboundAudio` apply while the body streams. Its tenant token is fetched
258
+ * through the SDK and kept until five minutes before the expiry Lark states.
259
+ */
260
+ declare function createLarkTransport(options: LarkTransportOptions, load?: () => Promise<LarkSdk>, fetchImpl?: typeof fetch): LarkTransport;
261
+
262
+ export { FeishuLarkAdapter, type FeishuLarkAdapterOptions, type LarkApiResult, type LarkConnectionHandlers, type LarkSdk, type LarkTransport, type LarkTransportOptions, createLarkTransport, feishuLarkChannel };
@@ -0,0 +1,262 @@
1
+ import { BasePlatformAdapter, OutboundMessage, SendResult, MessageEvent } from 'loopzero-gateway';
2
+
3
+ /**
4
+ * Options and seams for `FeishuLarkAdapter`.
5
+ *
6
+ * @public
7
+ */
8
+ /**
9
+ * What one open-platform call answered: `code` 0 is success, anything else is Lark's own error
10
+ * code with its `msg`.
11
+ *
12
+ * @public
13
+ */
14
+ interface LarkApiResult {
15
+ readonly code: number;
16
+ readonly msg?: string;
17
+ /** The created or replied message's id, on a successful send. */
18
+ readonly messageId?: string;
19
+ }
20
+ /**
21
+ * Callbacks the long connection reports to.
22
+ *
23
+ * @public
24
+ */
25
+ interface LarkConnectionHandlers {
26
+ /** One `im.message.receive_v1` event, as the SDK's dispatcher hands it over. Must not throw. */
27
+ readonly onMessage: (data: unknown) => void;
28
+ /** The SDK gave up: initial connect refused for good, or its reconnects exhausted (D7). */
29
+ readonly onError: (err: Error) => void;
30
+ }
31
+ /**
32
+ * The part of the Lark open platform the adapter uses. The default is built from
33
+ * `@larksuiteoapi/node-sdk`; tests pass a scripted one.
34
+ *
35
+ * @public
36
+ */
37
+ interface LarkTransport {
38
+ /** Fetch a tenant access token, which proves the app id and secret. */
39
+ tenantToken(): Promise<LarkApiResult>;
40
+ /** Open the long connection; resolves once it is up, rejects when the first connect fails. */
41
+ start(handlers: LarkConnectionHandlers): Promise<void>;
42
+ /** Send `text` as a new message to the chat `chatId`. */
43
+ createMessage(chatId: string, text: string): Promise<LarkApiResult>;
44
+ /** Send `text` as a reply to the message `messageId`. */
45
+ replyMessage(messageId: string, text: string): Promise<LarkApiResult>;
46
+ /**
47
+ * Send `GET /open-apis/im/v1/messages/{messageId}/resources/{fileKey}?type=file` with a tenant
48
+ * access token, under `signal`, or answer why it did not (row H-2). Optional: without it, an
49
+ * inbound audio message is skipped. The answer is never parsed here; `deferInboundAudio` bounds it.
50
+ */
51
+ downloadResource?(messageId: string, fileKey: string, signal: AbortSignal): Promise<Response | {
52
+ readonly reason: string;
53
+ }>;
54
+ /** Close the long connection. Idempotent. */
55
+ close(): void;
56
+ }
57
+ /**
58
+ * Construction options for `FeishuLarkAdapter`.
59
+ *
60
+ * @public
61
+ */
62
+ interface FeishuLarkAdapterOptions {
63
+ /** The app's App ID, `cli_` followed by 16 hex characters. */
64
+ readonly appId: string;
65
+ /**
66
+ * The feishu-lark app's App Secret. With the App ID it fetches the tenant access token every call
67
+ * uses and authenticates the long connection.
68
+ *
69
+ * @platform-term App Secret
70
+ * @issued-at the Feishu or Lark developer console, Credentials & Basic Info page of the custom app
71
+ */
72
+ readonly appSecret: string;
73
+ /** Which open platform the app lives on: `"feishu"` (open.feishu.cn, the default) or `"lark"` (open.larksuite.com). */
74
+ readonly domain?: "feishu" | "lark";
75
+ /** Test seam: the open-platform transport. Default one built from `@larksuiteoapi/node-sdk`. */
76
+ readonly transport?: LarkTransport;
77
+ }
78
+
79
+ /**
80
+ * `FeishuLarkAdapter` — `BasePlatformAdapter` over the Feishu/Lark open platform: events from the
81
+ * SDK's long connection, replies through `im.message.create` and `im.message.reply`.
82
+ *
83
+ * @public
84
+ */
85
+
86
+ /**
87
+ * Feishu/Lark adapter for one custom app.
88
+ *
89
+ * `connect()` fetches a tenant access token, which proves the App ID and Secret, then opens the
90
+ * long connection; it answers `false`, never a throw, when either fails or 10000 ms pass. Each
91
+ * `im.message.receive_v1` text or audio message from a user becomes a `FeishuLarkMessageEvent` once (the
92
+ * last 1000 event ids are remembered, so Lark's redelivery is not answered twice), delivered after
93
+ * the dispatcher has returned, so the SDK acknowledges it inside Lark's window. The SDK's own
94
+ * reconnect stays on; when it gives up, the adapter logs one line and reads as disconnected (D7).
95
+ *
96
+ * @public
97
+ */
98
+ declare class FeishuLarkAdapter extends BasePlatformAdapter {
99
+ readonly platform: "feishu_lark";
100
+ private readonly transport;
101
+ private readonly seen;
102
+ private handler;
103
+ private connected;
104
+ private connecting;
105
+ private warnedAboutHtml;
106
+ /** Bumped by every `disconnect()` and every connect that gave up, so a late answer is dropped. */
107
+ private generation;
108
+ /** @throws Error when `appId` is not `cli_` followed by 16 hex characters */
109
+ constructor(opts: FeishuLarkAdapterOptions);
110
+ connect(): Promise<boolean>;
111
+ disconnect(): Promise<void>;
112
+ sendMessage(out: OutboundMessage): Promise<SendResult>;
113
+ private sendText;
114
+ onInbound(handler: (event: MessageEvent) => Promise<void>): () => void;
115
+ /** Deliver an event that arrived out of band, through `runHandler`. */
116
+ deliver(event: MessageEvent): Promise<"ok" | "no_handler" | "handler_threw">;
117
+ /** The text to send: markdown loses its markers, html goes as is with one warning. */
118
+ protected outboundText(out: OutboundMessage): string;
119
+ /**
120
+ * Prove the credentials, then open the long connection. An attempt overtaken by `disconnect()`,
121
+ * or by its own deadline, stops at the next step and leaves nothing open.
122
+ */
123
+ private openSession;
124
+ /** The transport's resource download, bound to it, or `undefined` when it has none (row H-2). */
125
+ private resourceDownload;
126
+ private onConnectionLost;
127
+ /** Runs inside the SDK's dispatcher: it must return at once and never throw. */
128
+ private onMessage;
129
+ }
130
+
131
+ /**
132
+ * `feishuLarkChannel` — the Feishu/Lark channel described as data, with environment bindings.
133
+ *
134
+ * Typed locally on purpose: the object is structurally a `ChannelDescriptor`, but a published
135
+ * `.d.ts` must not name core types an older peer lacks.
136
+ */
137
+
138
+ type Config = Readonly<Record<string, unknown>>;
139
+ /** One configuration field, shaped like the core's `ChannelConfigField` (typed locally). */
140
+ interface ConfigField {
141
+ readonly key: string;
142
+ readonly required: boolean;
143
+ readonly secret: boolean;
144
+ readonly description: string;
145
+ readonly env: string;
146
+ }
147
+ /**
148
+ * The Feishu/Lark channel: it opens its own long connection in `connect()`, so it needs no
149
+ * webhook route.
150
+ *
151
+ * `create` refuses a missing or wrongly typed value, a malformed `appId` and an unknown `domain`
152
+ * with `GatewayConfigurationError` (prefix `gateway-feishu-lark`) (code `invalid_config`, the key in the message); it
153
+ * performs no network call.
154
+ *
155
+ * @public
156
+ */
157
+ declare const feishuLarkChannel: {
158
+ platform: "feishu_lark";
159
+ transport: "socket";
160
+ senderIdentity: "authenticated";
161
+ config: readonly ConfigField[];
162
+ create(config: Config): FeishuLarkAdapter;
163
+ };
164
+
165
+ /**
166
+ * The default `LarkTransport`, over `@larksuiteoapi/node-sdk`'s `Client` and long-connection
167
+ * `WSClient`. The SDK is an optional peer, loaded on first use.
168
+ */
169
+
170
+ interface SdkApiAnswer {
171
+ readonly code?: number;
172
+ readonly msg?: string;
173
+ readonly data?: {
174
+ readonly message_id?: string;
175
+ };
176
+ }
177
+ /** What `auth/v3/tenant_access_token/internal` answers: the token sits beside `code`. */
178
+ interface SdkTokenAnswer extends SdkApiAnswer {
179
+ readonly tenant_access_token?: string;
180
+ /** Seconds until the token expires. */
181
+ readonly expire?: number;
182
+ }
183
+ /** The part of `@larksuiteoapi/node-sdk` this transport uses. */
184
+ interface LarkSdk {
185
+ readonly Domain: {
186
+ readonly Feishu: number;
187
+ readonly Lark: number;
188
+ };
189
+ readonly Client: new (params: {
190
+ appId: string;
191
+ appSecret: string;
192
+ domain: number;
193
+ }) => {
194
+ readonly auth: {
195
+ readonly tenantAccessToken: {
196
+ internal(payload: {
197
+ data: {
198
+ app_id: string;
199
+ app_secret: string;
200
+ };
201
+ }): Promise<SdkTokenAnswer>;
202
+ };
203
+ };
204
+ readonly im: {
205
+ readonly message: {
206
+ create(payload: {
207
+ params: {
208
+ receive_id_type: "chat_id";
209
+ };
210
+ data: {
211
+ receive_id: string;
212
+ msg_type: "text";
213
+ content: string;
214
+ };
215
+ }): Promise<SdkApiAnswer>;
216
+ reply(payload: {
217
+ path: {
218
+ message_id: string;
219
+ };
220
+ data: {
221
+ msg_type: "text";
222
+ content: string;
223
+ };
224
+ }): Promise<SdkApiAnswer>;
225
+ };
226
+ };
227
+ };
228
+ readonly WSClient: new (params: {
229
+ appId: string;
230
+ appSecret: string;
231
+ domain: number;
232
+ onReady?: () => void;
233
+ onError?: (err: Error) => void;
234
+ }) => {
235
+ start(params: {
236
+ eventDispatcher: unknown;
237
+ }): Promise<void>;
238
+ close(): void;
239
+ };
240
+ readonly EventDispatcher: new (params: Record<string, never>) => {
241
+ register(handles: Record<string, (data: unknown) => Promise<void>>): unknown;
242
+ };
243
+ }
244
+ /** Options the default transport needs. */
245
+ interface LarkTransportOptions {
246
+ readonly appId: string;
247
+ readonly appSecret: string;
248
+ readonly domain?: "feishu" | "lark";
249
+ }
250
+ /**
251
+ * Build the transport over the SDK. `load` is the test seam: it returns the SDK module.
252
+ *
253
+ * The long connection keeps the SDK's own reconnect (`autoReconnect` left at its default): the SDK
254
+ * retries on the interval Feishu's servers send, and the adapter runs no loop of its own (plan D7).
255
+ *
256
+ * `downloadResource` reads a message resource with plain `fetch` (the second seam), so the size and
257
+ * deadline bounds of `deferInboundAudio` apply while the body streams. Its tenant token is fetched
258
+ * through the SDK and kept until five minutes before the expiry Lark states.
259
+ */
260
+ declare function createLarkTransport(options: LarkTransportOptions, load?: () => Promise<LarkSdk>, fetchImpl?: typeof fetch): LarkTransport;
261
+
262
+ export { FeishuLarkAdapter, type FeishuLarkAdapterOptions, type LarkApiResult, type LarkConnectionHandlers, type LarkSdk, type LarkTransport, type LarkTransportOptions, createLarkTransport, feishuLarkChannel };