@zelkovajs/adapter-kit 0.0.0-stage → 0.8.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/dist/adapter.d.ts +72 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +336 -0
- package/dist/adapter.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/testing.d.ts +24 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +97 -0
- package/dist/testing.js.map +1 -0
- package/dist/tokens.d.ts +8 -0
- package/dist/tokens.d.ts.map +1 -0
- package/dist/tokens.js +33 -0
- package/dist/tokens.js.map +1 -0
- package/dist/wire.d.ts +54 -0
- package/dist/wire.d.ts.map +1 -0
- package/dist/wire.js +5 -0
- package/dist/wire.js.map +1 -0
- package/package.json +41 -3
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/NOTICE
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export interface IncomingEvent {
|
|
2
|
+
/** The key of the adapter for the conversation, for example `<channel>:<thread ts>`. */
|
|
3
|
+
thread: string;
|
|
4
|
+
/** The user ID of the platform. */
|
|
5
|
+
platformUser: string;
|
|
6
|
+
/** A typed message. */
|
|
7
|
+
text?: string;
|
|
8
|
+
/** The action token of a pressed button, exactly as the kit issued it. */
|
|
9
|
+
action?: string;
|
|
10
|
+
/** Give it only when the platform vouches for the address. Zelkova can then link the user automatically. */
|
|
11
|
+
verifiedEmail?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface OutgoingMessage {
|
|
14
|
+
text: string;
|
|
15
|
+
buttons?: Array<{
|
|
16
|
+
label: string;
|
|
17
|
+
action: string;
|
|
18
|
+
style?: 'primary' | 'danger';
|
|
19
|
+
}>;
|
|
20
|
+
link?: {
|
|
21
|
+
label: string;
|
|
22
|
+
url: string;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export type Duration = `${number}${'ms' | 's' | 'm' | 'h' | 'd'}`;
|
|
26
|
+
export interface KitOptions {
|
|
27
|
+
/** The URL of the Zelkova deployment. The default is `ZELKOVA_URL`. */
|
|
28
|
+
url?: string;
|
|
29
|
+
/** The `zka_...` credential of the adapter registration. The default is `ZELKOVA_ADAPTER_CREDENTIAL`. */
|
|
30
|
+
credential?: string;
|
|
31
|
+
/** The long poll of the outbox. The default is `30s`. Zelkova waits at most 30 seconds. */
|
|
32
|
+
outboxWait?: Duration;
|
|
33
|
+
/** The tries to post one outbox item. The default is 5. */
|
|
34
|
+
maxDeliveryAttempts?: number;
|
|
35
|
+
}
|
|
36
|
+
/** An error response of Zelkova, or a network failure (`status` 0). */
|
|
37
|
+
export declare class ZelkovaCallError extends Error {
|
|
38
|
+
readonly status: number;
|
|
39
|
+
readonly code: string;
|
|
40
|
+
readonly retryAfterMs?: number | undefined;
|
|
41
|
+
constructor(status: number, code: string, message: string, retryAfterMs?: number | undefined);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The base class of a user adapter. The subclass connects to the platform in `start`, calls
|
|
45
|
+
* `receive` for each event and posts to the platform in `send` and `sendRich`. The kit keeps no
|
|
46
|
+
* state on disk.
|
|
47
|
+
*/
|
|
48
|
+
export declare abstract class ConversationalAdapter {
|
|
49
|
+
#private;
|
|
50
|
+
constructor(options?: KitOptions);
|
|
51
|
+
/** Connect to the platform and call this.receive for each event. */
|
|
52
|
+
abstract start(): Promise<void>;
|
|
53
|
+
/** Post plain text to a thread. */
|
|
54
|
+
abstract send(thread: string, text: string): Promise<void>;
|
|
55
|
+
/** Optional: post text with buttons and a link. Without it, the kit calls send with a text version. */
|
|
56
|
+
sendRich?(thread: string, message: OutgoingMessage): Promise<void>;
|
|
57
|
+
/** Writes a line to the standard error. Override it to use your own logger. */
|
|
58
|
+
protected log(message: string): void;
|
|
59
|
+
/** Waits before a new try. `stop()` ends the wait early with an abort error. */
|
|
60
|
+
protected delay(ms: number, signal: AbortSignal): Promise<void>;
|
|
61
|
+
/** Returns as soon as the event is queued, so platform acknowledgement deadlines are met. */
|
|
62
|
+
protected receive(event: IncomingEvent): void;
|
|
63
|
+
/**
|
|
64
|
+
* Starts the adapter and the outbox loop; resolves when stop() is called.
|
|
65
|
+
* @throws {ZelkovaCallError} with the status 401 if Zelkova refuses the credential. Do not catch
|
|
66
|
+
* it at the top level of the service: Node.js then exits with a non-zero code.
|
|
67
|
+
*/
|
|
68
|
+
run(): Promise<void>;
|
|
69
|
+
/** Stops new events, finishes the events in progress, acknowledges what the kit posted, and returns. */
|
|
70
|
+
stop(): Promise<void>;
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,aAAa;IAC5B,wFAAwF;IACxF,MAAM,EAAE,MAAM,CAAC;IACf,mCAAmC;IACnC,YAAY,EAAE,MAAM,CAAC;IACrB,uBAAuB;IACvB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,0EAA0E;IAC1E,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,4GAA4G;IAC5G,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO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AAG,OAAO,CAAC,IAAI,CAAC;IAElE,+EAA+E;IAC/E,SAAS,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAIpC,gFAAgF;cAChE,KAAK,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAIrE,6FAA6F;IAC7F,SAAS,CAAC,OAAO,CAAC,KAAK,EAAE,aAAa,GAAG,IAAI;IAU7C;;;;OAIG;IACG,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC;IAW1B,wGAAwG;IACxG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAkKtB"}
|
package/dist/adapter.js
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import { setTimeout as sleep } from 'node:timers/promises';
|
|
2
|
+
import { decodeAction, encodeAction } from './tokens.js';
|
|
3
|
+
/** An error response of Zelkova, or a network failure (`status` 0). */
|
|
4
|
+
export class ZelkovaCallError extends Error {
|
|
5
|
+
status;
|
|
6
|
+
code;
|
|
7
|
+
retryAfterMs;
|
|
8
|
+
constructor(status, code, message, retryAfterMs) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.status = status;
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.retryAfterMs = retryAfterMs;
|
|
13
|
+
this.name = 'ZelkovaCallError';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const UNIT_MS = { ms: 1, s: 1_000, m: 60_000, h: 3_600_000, d: 86_400_000 };
|
|
17
|
+
const MAX_TOKEN_CHARS = 200;
|
|
18
|
+
const CHAT_RETRY_MS = [1_000, 3_000];
|
|
19
|
+
const MAX_BACKOFF_MS = 30_000;
|
|
20
|
+
const UNAVAILABLE_TEXT = 'Zelkova is temporarily unavailable. Please send your message again in a few minutes.';
|
|
21
|
+
function parseDuration(value) {
|
|
22
|
+
const match = /^(\d+)(ms|s|m|h|d)$/.exec(value);
|
|
23
|
+
if (match === null)
|
|
24
|
+
throw new Error(`Invalid duration "${value}". Use a whole number and a unit: ms, s, m, h or d.`);
|
|
25
|
+
return Number(match[1]) * UNIT_MS[match[2]];
|
|
26
|
+
}
|
|
27
|
+
const backoffMs = (attempt) => Math.min(1_000 * 2 ** (attempt - 1), MAX_BACKOFF_MS);
|
|
28
|
+
const isFatal = (error) => error instanceof ZelkovaCallError && error.status === 401;
|
|
29
|
+
/** A failure that a later try can fix: the network, `429` or `5xx`. */
|
|
30
|
+
const isTransient = (error) => error instanceof ZelkovaCallError && (error.status === 0 || error.status === 429 || error.status >= 500);
|
|
31
|
+
/**
|
|
32
|
+
* The buttons of a question whose answer is one boolean, or one choice of up to five values.
|
|
33
|
+
* The answer is the value itself, or an object with the one required property.
|
|
34
|
+
* Returns null for each other answer schema.
|
|
35
|
+
*/
|
|
36
|
+
function choicesOf(schema) {
|
|
37
|
+
const leaf = (s) => {
|
|
38
|
+
if (typeof s !== 'object' || s === null)
|
|
39
|
+
return null;
|
|
40
|
+
const node = s;
|
|
41
|
+
if (node.type === 'boolean')
|
|
42
|
+
return [true, false];
|
|
43
|
+
if (Array.isArray(node.enum) && node.enum.length > 0 && node.enum.length <= 5)
|
|
44
|
+
return node.enum;
|
|
45
|
+
return null;
|
|
46
|
+
};
|
|
47
|
+
const label = (v) => (v === true ? 'Yes' : v === false ? 'No' : String(v));
|
|
48
|
+
const top = leaf(schema);
|
|
49
|
+
if (top !== null)
|
|
50
|
+
return top.map((value) => ({ label: label(value), value }));
|
|
51
|
+
const object = schema;
|
|
52
|
+
if (object.type !== 'object' || !Array.isArray(object.required) || object.required.length !== 1)
|
|
53
|
+
return null;
|
|
54
|
+
const key = String(object.required[0]);
|
|
55
|
+
const values = leaf(object.properties?.[key]);
|
|
56
|
+
return values?.map((value) => ({ label: label(value), value: { [key]: value } })) ?? null;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The base class of a user adapter. The subclass connects to the platform in `start`, calls
|
|
60
|
+
* `receive` for each event and posts to the platform in `send` and `sendRich`. The kit keeps no
|
|
61
|
+
* state on disk.
|
|
62
|
+
*/
|
|
63
|
+
export class ConversationalAdapter {
|
|
64
|
+
#url;
|
|
65
|
+
#credential;
|
|
66
|
+
#waitSeconds;
|
|
67
|
+
#maxDeliveryAttempts;
|
|
68
|
+
/** The last event of each thread. The next event of the thread waits for it. */
|
|
69
|
+
#queues = new Map();
|
|
70
|
+
#abort = new AbortController();
|
|
71
|
+
#running = false;
|
|
72
|
+
#stopping = null;
|
|
73
|
+
#fatal = null;
|
|
74
|
+
#loop = Promise.resolve();
|
|
75
|
+
#done = null;
|
|
76
|
+
/** The ID of the last outbox item that the kit posted, and of the last item that Zelkova acknowledged. */
|
|
77
|
+
#posted = null;
|
|
78
|
+
#acked = null;
|
|
79
|
+
constructor(options = {}) {
|
|
80
|
+
const url = options.url ?? process.env['ZELKOVA_URL'];
|
|
81
|
+
const credential = options.credential ?? process.env['ZELKOVA_ADAPTER_CREDENTIAL'];
|
|
82
|
+
if (!url)
|
|
83
|
+
throw new Error('Set ZELKOVA_URL, or give the url option.');
|
|
84
|
+
if (!credential)
|
|
85
|
+
throw new Error('Set ZELKOVA_ADAPTER_CREDENTIAL, or give the credential option.');
|
|
86
|
+
this.#url = url.replace(/\/+$/, '');
|
|
87
|
+
this.#credential = credential;
|
|
88
|
+
this.#waitSeconds = Math.ceil(parseDuration(options.outboxWait ?? '30s') / 1_000);
|
|
89
|
+
this.#maxDeliveryAttempts = Math.max(1, options.maxDeliveryAttempts ?? 5);
|
|
90
|
+
}
|
|
91
|
+
/** Writes a line to the standard error. Override it to use your own logger. */
|
|
92
|
+
log(message) {
|
|
93
|
+
console.error(`[adapter-kit] ${message}`);
|
|
94
|
+
}
|
|
95
|
+
/** Waits before a new try. `stop()` ends the wait early with an abort error. */
|
|
96
|
+
async delay(ms, signal) {
|
|
97
|
+
await sleep(ms, undefined, { signal });
|
|
98
|
+
}
|
|
99
|
+
/** Returns as soon as the event is queued, so platform acknowledgement deadlines are met. */
|
|
100
|
+
receive(event) {
|
|
101
|
+
if (this.#stopping !== null)
|
|
102
|
+
return;
|
|
103
|
+
const previous = this.#queues.get(event.thread) ?? Promise.resolve();
|
|
104
|
+
const next = previous.then(() => this.#handle(event)).catch((error) => this.log(`An event of the thread ${event.thread} failed: ${String(error)}`));
|
|
105
|
+
this.#queues.set(event.thread, next);
|
|
106
|
+
void next.then(() => {
|
|
107
|
+
if (this.#queues.get(event.thread) === next)
|
|
108
|
+
this.#queues.delete(event.thread);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Starts the adapter and the outbox loop; resolves when stop() is called.
|
|
113
|
+
* @throws {ZelkovaCallError} with the status 401 if Zelkova refuses the credential. Do not catch
|
|
114
|
+
* it at the top level of the service: Node.js then exits with a non-zero code.
|
|
115
|
+
*/
|
|
116
|
+
async run() {
|
|
117
|
+
if (this.#running)
|
|
118
|
+
throw new Error('The adapter runs already.');
|
|
119
|
+
this.#running = true;
|
|
120
|
+
const done = new Promise((resolve, reject) => {
|
|
121
|
+
this.#done = { resolve, reject };
|
|
122
|
+
});
|
|
123
|
+
await this.start();
|
|
124
|
+
this.#loop = this.#outboxLoop();
|
|
125
|
+
return done;
|
|
126
|
+
}
|
|
127
|
+
/** Stops new events, finishes the events in progress, acknowledges what the kit posted, and returns. */
|
|
128
|
+
stop() {
|
|
129
|
+
this.#stopping ??= (async () => {
|
|
130
|
+
this.#abort.abort();
|
|
131
|
+
while (this.#queues.size > 0)
|
|
132
|
+
await Promise.all(this.#queues.values());
|
|
133
|
+
await this.#loop;
|
|
134
|
+
if (this.#fatal === null) {
|
|
135
|
+
try {
|
|
136
|
+
await this.#ack();
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
this.log(`The last acknowledgement failed: ${String(error)}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (this.#fatal === null)
|
|
143
|
+
this.#done?.resolve();
|
|
144
|
+
else
|
|
145
|
+
this.#done?.reject(this.#fatal);
|
|
146
|
+
})();
|
|
147
|
+
return this.#stopping;
|
|
148
|
+
}
|
|
149
|
+
/** Stops the kit after a `401`: a retry cannot help. */
|
|
150
|
+
#fail(error) {
|
|
151
|
+
if (this.#fatal !== null)
|
|
152
|
+
return;
|
|
153
|
+
this.#fatal = error;
|
|
154
|
+
this.log(`Zelkova refused the credential of the adapter. The adapter stops. ${String(error)}`);
|
|
155
|
+
void this.stop();
|
|
156
|
+
}
|
|
157
|
+
/** Calls Zelkova. `stop()` aborts only a long poll: an event in progress and the last acknowledgement finish. */
|
|
158
|
+
async #call(method, path, body, headers, timeoutMs, longPoll = false) {
|
|
159
|
+
const timeout = AbortSignal.timeout(timeoutMs);
|
|
160
|
+
const signal = longPoll ? AbortSignal.any([this.#abort.signal, timeout]) : timeout;
|
|
161
|
+
let response;
|
|
162
|
+
try {
|
|
163
|
+
response = await fetch(`${this.#url}${path}`, {
|
|
164
|
+
method,
|
|
165
|
+
headers: { authorization: `Bearer ${this.#credential}`, ...(body === undefined ? {} : { 'content-type': 'application/json' }), ...headers },
|
|
166
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
167
|
+
signal,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
if (longPoll && this.#abort.signal.aborted)
|
|
172
|
+
throw error;
|
|
173
|
+
throw new ZelkovaCallError(0, 'network', `The call to Zelkova failed: ${String(error)}`);
|
|
174
|
+
}
|
|
175
|
+
const text = await response.text();
|
|
176
|
+
if (response.ok)
|
|
177
|
+
return text === '' ? null : JSON.parse(text);
|
|
178
|
+
let error = {};
|
|
179
|
+
try {
|
|
180
|
+
error = JSON.parse(text).error ?? {};
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
// The body is not JSON. The status alone describes the error.
|
|
184
|
+
}
|
|
185
|
+
const retryAfter = Number(response.headers.get('retry-after'));
|
|
186
|
+
throw new ZelkovaCallError(response.status, error.code ?? 'unknown', error.message ?? `Zelkova answered ${response.status}.`, Number.isFinite(retryAfter) && retryAfter >= 0 && response.headers.has('retry-after') ? retryAfter * 1_000 : undefined);
|
|
187
|
+
}
|
|
188
|
+
async #handle(event) {
|
|
189
|
+
let message;
|
|
190
|
+
if (event.action !== undefined) {
|
|
191
|
+
const decoded = decodeAction(event.action);
|
|
192
|
+
if (decoded === null) {
|
|
193
|
+
this.log(`The kit ignored an action token that it cannot decode, in the thread ${event.thread}.`);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
message = decoded;
|
|
197
|
+
}
|
|
198
|
+
else if (event.text !== undefined && event.text !== '') {
|
|
199
|
+
message = { kind: 'text', text: event.text };
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const headers = { 'zelkova-platform-user': event.platformUser };
|
|
205
|
+
if (event.verifiedEmail !== undefined)
|
|
206
|
+
headers['zelkova-verified-email'] = event.verifiedEmail;
|
|
207
|
+
const path = `/v1/threads/${encodeURIComponent(event.thread)}/messages`;
|
|
208
|
+
let reply;
|
|
209
|
+
for (let attempt = 0;; attempt++) {
|
|
210
|
+
try {
|
|
211
|
+
reply = await this.#call('POST', path, message, headers, 120_000);
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
catch (error) {
|
|
215
|
+
if (isFatal(error))
|
|
216
|
+
return this.#fail(error);
|
|
217
|
+
if (!isTransient(error)) {
|
|
218
|
+
const text = error instanceof ZelkovaCallError ? error.message : String(error);
|
|
219
|
+
return this.send(event.thread, `Zelkova refused the message: ${text}`);
|
|
220
|
+
}
|
|
221
|
+
const wait = CHAT_RETRY_MS[attempt];
|
|
222
|
+
if (wait === undefined) {
|
|
223
|
+
this.log(`Zelkova is unavailable for the thread ${event.thread}: ${String(error)}`);
|
|
224
|
+
return this.send(event.thread, UNAVAILABLE_TEXT);
|
|
225
|
+
}
|
|
226
|
+
// stop() does not end this wait, because stop() finishes each event in progress.
|
|
227
|
+
await this.delay(error.retryAfterMs ?? wait, new AbortController().signal);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (reply !== null)
|
|
231
|
+
await this.#post(event.thread, replyMessage(reply));
|
|
232
|
+
}
|
|
233
|
+
/** Posts with `sendRich`, or with `send` and a text version of the buttons and the link. */
|
|
234
|
+
async #post(thread, message) {
|
|
235
|
+
const { hint, ...rich } = message;
|
|
236
|
+
if (this.sendRich !== undefined && (rich.buttons !== undefined || rich.link !== undefined))
|
|
237
|
+
return this.sendRich(thread, rich);
|
|
238
|
+
const lines = [rich.text];
|
|
239
|
+
if (rich.buttons !== undefined && hint !== undefined)
|
|
240
|
+
lines.push(hint);
|
|
241
|
+
// An approval question of Zelkova holds the console link in its text already.
|
|
242
|
+
if (rich.link !== undefined && !rich.text.includes(rich.link.url))
|
|
243
|
+
lines.push(`${rich.link.label}: ${rich.link.url}`);
|
|
244
|
+
return this.send(thread, lines.join('\n'));
|
|
245
|
+
}
|
|
246
|
+
async #ack() {
|
|
247
|
+
const upTo = this.#posted;
|
|
248
|
+
if (upTo === null || upTo === this.#acked)
|
|
249
|
+
return;
|
|
250
|
+
await this.#call('POST', '/v1/adapter/outbox/ack', { upTo }, {}, 30_000);
|
|
251
|
+
this.#acked = upTo;
|
|
252
|
+
}
|
|
253
|
+
async #outboxLoop() {
|
|
254
|
+
let failures = 0;
|
|
255
|
+
while (!this.#abort.signal.aborted) {
|
|
256
|
+
try {
|
|
257
|
+
await this.#ack();
|
|
258
|
+
const query = new URLSearchParams({ wait: String(this.#waitSeconds) });
|
|
259
|
+
if (this.#posted !== null)
|
|
260
|
+
query.set('after', this.#posted);
|
|
261
|
+
const page = await this.#call('GET', `/v1/adapter/outbox?${query}`, undefined, {}, (this.#waitSeconds + 30) * 1_000, true);
|
|
262
|
+
for (const item of page?.items ?? []) {
|
|
263
|
+
// After a lost acknowledgement, Zelkova can send an item again that this process posted.
|
|
264
|
+
if (this.#posted !== null && BigInt(item.id) <= BigInt(this.#posted))
|
|
265
|
+
continue;
|
|
266
|
+
await this.#deliver(item);
|
|
267
|
+
this.#posted = item.id;
|
|
268
|
+
await this.#ack();
|
|
269
|
+
}
|
|
270
|
+
failures = 0;
|
|
271
|
+
}
|
|
272
|
+
catch (error) {
|
|
273
|
+
if (this.#abort.signal.aborted)
|
|
274
|
+
return;
|
|
275
|
+
if (isFatal(error))
|
|
276
|
+
return this.#fail(error);
|
|
277
|
+
failures++;
|
|
278
|
+
this.log(`The outbox call failed: ${String(error)}`);
|
|
279
|
+
try {
|
|
280
|
+
await this.delay(error.retryAfterMs ?? backoffMs(failures), this.#abort.signal);
|
|
281
|
+
}
|
|
282
|
+
catch {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
/** Posts one item. After `maxDeliveryAttempts` failures, the kit logs the failure and goes on. */
|
|
289
|
+
async #deliver(item) {
|
|
290
|
+
const message = itemMessage(item);
|
|
291
|
+
for (let attempt = 1;; attempt++) {
|
|
292
|
+
try {
|
|
293
|
+
return await this.#post(item.thread, message);
|
|
294
|
+
}
|
|
295
|
+
catch (error) {
|
|
296
|
+
if (attempt >= this.#maxDeliveryAttempts) {
|
|
297
|
+
this.log(`The kit gave up on the outbox item ${item.id} for the thread ${item.thread} after ${attempt} tries: ${String(error)}`);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
await this.delay(backoffMs(attempt), this.#abort.signal);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
function replyMessage(reply) {
|
|
306
|
+
switch (reply.kind) {
|
|
307
|
+
case 'confirm':
|
|
308
|
+
return {
|
|
309
|
+
text: reply.text,
|
|
310
|
+
buttons: [
|
|
311
|
+
{ label: 'Start', action: encodeAction({ kind: 'confirm', sessionId: reply.sessionId, start: true }), style: 'primary' },
|
|
312
|
+
{ label: 'Cancel', action: encodeAction({ kind: 'confirm', sessionId: reply.sessionId, start: false }), style: 'danger' },
|
|
313
|
+
],
|
|
314
|
+
};
|
|
315
|
+
case 'link':
|
|
316
|
+
return { text: reply.text, link: { label: 'Open the link', url: reply.url } };
|
|
317
|
+
default:
|
|
318
|
+
return { text: reply.text };
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
function itemMessage(item) {
|
|
322
|
+
const question = item.question;
|
|
323
|
+
if (item.kind !== 'question' || question === undefined)
|
|
324
|
+
return { text: item.text };
|
|
325
|
+
const link = question.humanOnly ? { label: 'Open the run in the Zelkova console', url: question.consoleUrl } : undefined;
|
|
326
|
+
const choices = choicesOf(question.answerSchema);
|
|
327
|
+
const buttons = choices?.map((choice) => ({
|
|
328
|
+
label: choice.label,
|
|
329
|
+
action: encodeAction({ kind: 'answer', runId: item.runId, waitId: question.waitId, value: choice.value }),
|
|
330
|
+
}));
|
|
331
|
+
if (buttons === undefined || buttons.some((button) => button.action.length > MAX_TOKEN_CHARS)) {
|
|
332
|
+
return { text: `${item.text}\nReply in this thread with your answer.`, ...(link && { link }) };
|
|
333
|
+
}
|
|
334
|
+
return { text: item.text, buttons, hint: `Reply with one of: ${buttons.map((b) => b.label).join(', ')}.`, ...(link && { link }) };
|
|
335
|
+
}
|
|
336
|
+
//# sourceMappingURL=adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAAI,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAE3D,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAmCzD,uEAAuE;AACvE,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAE9B;IACA;IAEA;IAJX,YACW,MAAc,EACd,IAAY,EACrB,OAAe,EACN,YAAqB;QAE9B,KAAK,CAAC,OAAO,CAAC,CAAC;QALN,WAAM,GAAN,MAAM,CAAQ;QACd,SAAI,GAAJ,IAAI,CAAQ;QAEZ,iBAA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AAC,IAAI,IAAI,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC;IACjG,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,sBAAsB,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,IAAI,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC;AACpI,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { ConversationalAdapter, ZelkovaCallError } from './adapter.js';
|
|
2
|
+
export type { Duration, IncomingEvent, KitOptions, OutgoingMessage } from './adapter.js';
|
|
3
|
+
export type { ChatSessionId, IncomingMessage, OutboxItem, Reply, RunId, WaitId } from './wire.js';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACvE,YAAY,EAAE,QAAQ,EAAE,aAAa,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACzF,YAAY,EAAE,aAAa,EAAE,eAAe,EAAE,UAAU,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { IncomingMessage, OutboxItem, Reply } from './wire.js';
|
|
2
|
+
export interface FakeZelkova {
|
|
3
|
+
/** The URL of the fake. Give it to the adapter as `url`. */
|
|
4
|
+
url: string;
|
|
5
|
+
/** The credential that the fake accepts. Each other credential gets `401`. */
|
|
6
|
+
credential: string;
|
|
7
|
+
/** Queues the reply to the next message. Without a queued reply, the fake answers with a `message`. */
|
|
8
|
+
reply(r: Reply): void;
|
|
9
|
+
/** Adds an item to the outbox. The `id` must be a decimal number, larger than the `id` of each earlier item. */
|
|
10
|
+
push(item: OutboxItem): void;
|
|
11
|
+
/** Each message that the fake received, in order. */
|
|
12
|
+
received(): Array<{
|
|
13
|
+
thread: string;
|
|
14
|
+
platformUser: string;
|
|
15
|
+
message: IncomingMessage;
|
|
16
|
+
verifiedEmail?: string;
|
|
17
|
+
}>;
|
|
18
|
+
/** The `upTo` cursor of each acknowledgement, in order. */
|
|
19
|
+
acked(): string[];
|
|
20
|
+
close(): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
/** A local stand-in for Zelkova: scripted replies and outbox items, and a record of what the adapter sent. */
|
|
23
|
+
export declare function fakeZelkova(): Promise<FakeZelkova>;
|
|
24
|
+
//# sourceMappingURL=testing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,eAAe,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAEpE,MAAM,WAAW,WAAW;IAC1B,4DAA4D;IAC5D,GAAG,EAAE,MAAM,CAAC;IACZ,8EAA8E;IAC9E,UAAU,EAAE,MAAM,CAAC;IACnB,uGAAuG;IACvG,KAAK,CAAC,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC;IACtB,gHAAgH;IAChH,IAAI,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,CAAC;IAC7B,qDAAqD;IACrD,QAAQ,IAAI,KAAK,CAAC;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,eAAe,CAAC;QAAC,aAAa,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC9G,2DAA2D;IAC3D,KAAK,IAAI,MAAM,EAAE,CAAC;IAClB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED,8GAA8G;AAC9G,wBAAsB,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC,CA2FxD"}
|
package/dist/testing.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { createServer } from 'node:http';
|
|
2
|
+
/** A local stand-in for Zelkova: scripted replies and outbox items, and a record of what the adapter sent. */
|
|
3
|
+
export async function fakeZelkova() {
|
|
4
|
+
const credential = 'zka_fake_credential';
|
|
5
|
+
const replies = [];
|
|
6
|
+
const outbox = [];
|
|
7
|
+
const received = [];
|
|
8
|
+
const acked = [];
|
|
9
|
+
let ackedUpTo = -1n;
|
|
10
|
+
const waiters = new Set();
|
|
11
|
+
const wake = () => {
|
|
12
|
+
for (const resolve of [...waiters])
|
|
13
|
+
resolve();
|
|
14
|
+
};
|
|
15
|
+
const json = (res, status, body) => {
|
|
16
|
+
res.writeHead(status, { 'content-type': 'application/json' }).end(JSON.stringify(body));
|
|
17
|
+
};
|
|
18
|
+
const error = (res, status, code, message) => json(res, status, { error: { code, message } });
|
|
19
|
+
const bodyOf = async (req) => {
|
|
20
|
+
let text = '';
|
|
21
|
+
for await (const chunk of req)
|
|
22
|
+
text += String(chunk);
|
|
23
|
+
return JSON.parse(text);
|
|
24
|
+
};
|
|
25
|
+
const pending = (after) => outbox.filter((item) => BigInt(item.id) > after && BigInt(item.id) > ackedUpTo);
|
|
26
|
+
const handle = async (req, res) => {
|
|
27
|
+
const url = new URL(req.url ?? '/', 'http://fake');
|
|
28
|
+
if (req.headers.authorization !== `Bearer ${credential}`)
|
|
29
|
+
return error(res, 401, 'unauthenticated', 'The credential is not valid.');
|
|
30
|
+
const platformUser = req.headers['zelkova-platform-user'];
|
|
31
|
+
const chat = /^\/v1\/threads\/([^/]+)\/messages$/.exec(url.pathname);
|
|
32
|
+
if (req.method === 'POST' && chat !== null) {
|
|
33
|
+
if (typeof platformUser !== 'string')
|
|
34
|
+
return error(res, 403, 'permission_denied', 'The adapter must send Zelkova-Platform-User.');
|
|
35
|
+
const email = req.headers['zelkova-verified-email'];
|
|
36
|
+
const message = (await bodyOf(req));
|
|
37
|
+
received.push({ thread: decodeURIComponent(chat[1]), platformUser, message, ...(typeof email === 'string' && { verifiedEmail: email }) });
|
|
38
|
+
return json(res, 200, replies.shift() ?? { kind: 'message', text: 'Received.' });
|
|
39
|
+
}
|
|
40
|
+
if (url.pathname.startsWith('/v1/adapter/outbox') && platformUser !== undefined) {
|
|
41
|
+
return error(res, 403, 'permission_denied', 'The outbox takes only the credential of a user adapter, without a platform user.');
|
|
42
|
+
}
|
|
43
|
+
if (req.method === 'GET' && url.pathname === '/v1/adapter/outbox') {
|
|
44
|
+
const afterParam = url.searchParams.get('after');
|
|
45
|
+
const after = afterParam === null ? -1n : BigInt(afterParam);
|
|
46
|
+
const waitMs = Math.min(Number(url.searchParams.get('wait') ?? 0), 30) * 1_000;
|
|
47
|
+
if (pending(after).length === 0 && waitMs > 0) {
|
|
48
|
+
await new Promise((resolve) => {
|
|
49
|
+
const done = () => {
|
|
50
|
+
clearTimeout(timer);
|
|
51
|
+
waiters.delete(done);
|
|
52
|
+
resolve();
|
|
53
|
+
};
|
|
54
|
+
const timer = setTimeout(done, waitMs);
|
|
55
|
+
waiters.add(done);
|
|
56
|
+
res.on('close', done);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
const items = pending(after);
|
|
60
|
+
return json(res, 200, { items, cursor: items.at(-1)?.id ?? afterParam });
|
|
61
|
+
}
|
|
62
|
+
if (req.method === 'POST' && url.pathname === '/v1/adapter/outbox/ack') {
|
|
63
|
+
const { upTo } = (await bodyOf(req));
|
|
64
|
+
acked.push(upTo);
|
|
65
|
+
if (BigInt(upTo) > ackedUpTo)
|
|
66
|
+
ackedUpTo = BigInt(upTo);
|
|
67
|
+
res.writeHead(204).end();
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
error(res, 404, 'not_found', 'The fake has no such route.');
|
|
71
|
+
};
|
|
72
|
+
const server = createServer((req, res) => {
|
|
73
|
+
handle(req, res).catch((e) => {
|
|
74
|
+
if (!res.headersSent)
|
|
75
|
+
error(res, 500, 'internal', String(e));
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
|
|
79
|
+
const { port } = server.address();
|
|
80
|
+
return {
|
|
81
|
+
url: `http://127.0.0.1:${port}`,
|
|
82
|
+
credential,
|
|
83
|
+
reply: (r) => void replies.push(r),
|
|
84
|
+
push: (item) => {
|
|
85
|
+
outbox.push(item);
|
|
86
|
+
wake();
|
|
87
|
+
},
|
|
88
|
+
received: () => [...received],
|
|
89
|
+
acked: () => [...acked],
|
|
90
|
+
close: async () => {
|
|
91
|
+
wake();
|
|
92
|
+
server.closeAllConnections();
|
|
93
|
+
await new Promise((resolve, reject) => server.close((e) => (e ? reject(e) : resolve())));
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=testing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testing.js","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAsBzC,8GAA8G;AAC9G,MAAM,CAAC,KAAK,UAAU,WAAW;IAC/B,MAAM,UAAU,GAAG,qBAAqB,CAAC;IACzC,MAAM,OAAO,GAAY,EAAE,CAAC;IAC5B,MAAM,MAAM,GAAiB,EAAE,CAAC;IAChC,MAAM,QAAQ,GAAwC,EAAE,CAAC;IACzD,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,IAAI,SAAS,GAAG,CAAC,EAAE,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I,EAAE,EAAE;YACb,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAClB,IAAI,EAAE,CAAC;QACT,CAAC;QACD,QAAQ,EAAE,GAAG,EAAE,CAAC,CAAC,GAAG,QAAQ,CAAC;QAC7B,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC,GAAG,KAAK,CAAC;QACvB,KAAK,EAAE,KAAK,IAAI,EAAE;YAChB,IAAI,EAAE,CAAC;YACP,MAAM,CAAC,mBAAmB,EAAE,CAAC;YAC7B,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC;QACjG,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/tokens.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { IncomingMessage } from './wire.js';
|
|
2
|
+
/** Encodes a `confirm` or an `answer` message into an action token. */
|
|
3
|
+
export declare function encodeAction(message: Exclude<IncomingMessage, {
|
|
4
|
+
kind: 'text';
|
|
5
|
+
}>): string;
|
|
6
|
+
/** Decodes an action token. Returns null for a token that the kit did not issue. */
|
|
7
|
+
export declare function decodeAction(token: string): IncomingMessage | null;
|
|
8
|
+
//# sourceMappingURL=tokens.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokens.d.ts","sourceRoot":"","sources":["../src/tokens.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAOjD,uEAAuE;AACvE,wBAAgB,YAAY,CAAC,OAAO,EAAE,OAAO,CAAC,eAAe,EAAE;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,GAAG,MAAM,CAGxF;AAED,oFAAoF;AACpF,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe,GAAG,IAAI,CAkBlE"}
|
package/dist/tokens.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// An action token is `zk1.` and the base64url of a short JSON array. It holds a
|
|
2
|
+
// confirmation, `["c", sessionId, start]`, or an answer, `["a", runId, waitId, value]`.
|
|
3
|
+
// A token grants nothing by itself: Zelkova checks who may give the answer.
|
|
4
|
+
const PREFIX = 'zk1.';
|
|
5
|
+
/** Encodes a `confirm` or an `answer` message into an action token. */
|
|
6
|
+
export function encodeAction(message) {
|
|
7
|
+
const tuple = message.kind === 'confirm' ? ['c', message.sessionId, message.start] : ['a', message.runId, message.waitId, message.value];
|
|
8
|
+
return PREFIX + Buffer.from(JSON.stringify(tuple)).toString('base64url');
|
|
9
|
+
}
|
|
10
|
+
/** Decodes an action token. Returns null for a token that the kit did not issue. */
|
|
11
|
+
export function decodeAction(token) {
|
|
12
|
+
if (!token.startsWith(PREFIX) || token.length > 200)
|
|
13
|
+
return null;
|
|
14
|
+
let tuple;
|
|
15
|
+
try {
|
|
16
|
+
tuple = JSON.parse(Buffer.from(token.slice(PREFIX.length), 'base64url').toString('utf8'));
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
if (!Array.isArray(tuple))
|
|
22
|
+
return null;
|
|
23
|
+
const id = (value, prefix) => typeof value === 'string' && value.startsWith(prefix) && value.length <= 128;
|
|
24
|
+
const [kind, a, b, value] = tuple;
|
|
25
|
+
if (kind === 'c' && tuple.length === 3 && id(a, 'chs_') && typeof b === 'boolean') {
|
|
26
|
+
return { kind: 'confirm', sessionId: a, start: b };
|
|
27
|
+
}
|
|
28
|
+
if (kind === 'a' && tuple.length === 4 && id(a, 'run_') && id(b, 'wait_')) {
|
|
29
|
+
return { kind: 'answer', runId: a, waitId: b, value };
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
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,EAAmB,EAAE,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,MAAM,IAAI,GAAG,CAAC;IAC7I,MAAM,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC,GAAG,KAAkB,CAAC;IAC/C,IAAI,IAAI,KAAK,GAAG,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,CAAC,IAAI,OAAO,CAAC,KAAK,SAAS,EAAE,CAAC;QAClF,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,CAAoB,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC;IACxE,CAAC;IACD,IAAI,IAAI,KAAK,GAAG,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,CAAC;QAC1E,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAoB,EAAE,MAAM,EAAE,CAAqB,EAAE,KAAK,EAAE,CAAC;IAC/F,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/dist/wire.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export type ChatSessionId = `chs_${string}`;
|
|
2
|
+
export type RunId = `run_${string}`;
|
|
3
|
+
export type WaitId = `wait_${string}`;
|
|
4
|
+
/** A message to `POST /v1/threads/<thread>/messages`. */
|
|
5
|
+
export type IncomingMessage = {
|
|
6
|
+
kind: 'text';
|
|
7
|
+
text: string;
|
|
8
|
+
} | {
|
|
9
|
+
kind: 'confirm';
|
|
10
|
+
sessionId: ChatSessionId;
|
|
11
|
+
start: boolean;
|
|
12
|
+
} | {
|
|
13
|
+
kind: 'answer';
|
|
14
|
+
runId: RunId;
|
|
15
|
+
waitId: WaitId;
|
|
16
|
+
value: unknown;
|
|
17
|
+
};
|
|
18
|
+
/** The response of `POST /v1/threads/<thread>/messages`. */
|
|
19
|
+
export type Reply = {
|
|
20
|
+
kind: 'message';
|
|
21
|
+
text: string;
|
|
22
|
+
} | {
|
|
23
|
+
kind: 'question';
|
|
24
|
+
text: string;
|
|
25
|
+
} | {
|
|
26
|
+
kind: 'confirm';
|
|
27
|
+
text: string;
|
|
28
|
+
sessionId: ChatSessionId;
|
|
29
|
+
workflow: string;
|
|
30
|
+
input: unknown;
|
|
31
|
+
} | {
|
|
32
|
+
kind: 'started';
|
|
33
|
+
text: string;
|
|
34
|
+
runId: RunId;
|
|
35
|
+
} | {
|
|
36
|
+
kind: 'link';
|
|
37
|
+
text: string;
|
|
38
|
+
url: string;
|
|
39
|
+
};
|
|
40
|
+
/** An item of `GET /v1/adapter/outbox`: something to post to a thread. */
|
|
41
|
+
export interface OutboxItem {
|
|
42
|
+
id: string;
|
|
43
|
+
thread: string;
|
|
44
|
+
runId: RunId;
|
|
45
|
+
kind: 'question' | 'result' | 'failed' | 'cancelled';
|
|
46
|
+
text: string;
|
|
47
|
+
question?: {
|
|
48
|
+
waitId: WaitId;
|
|
49
|
+
answerSchema: object;
|
|
50
|
+
humanOnly: boolean;
|
|
51
|
+
consoleUrl: string;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=wire.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wire.d.ts","sourceRoot":"","sources":["../src/wire.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,aAAa,GAAG,OAAO,MAAM,EAAE,CAAC;AAC5C,MAAM,MAAM,KAAK,GAAG,OAAO,MAAM,EAAE,CAAC;AACpC,MAAM,MAAM,MAAM,GAAG,QAAQ,MAAM,EAAE,CAAC;AAEtC,yDAAyD;AACzD,MAAM,MAAM,eAAe,GACvB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM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@@ -0,0 +1,5 @@
|
|
|
1
|
+
// The wire types of the chat endpoint and the outbox. They are copies of the types in
|
|
2
|
+
// `@zelkovajs/core/contracts`, so the kit has no run-time dependency on the core.
|
|
3
|
+
// `test/wire.test.ts` checks that each copy equals the type of the core.
|
|
4
|
+
export {};
|
|
5
|
+
//# sourceMappingURL=wire.js.map
|
package/dist/wire.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wire.js","sourceRoot":"","sources":["../src/wire.ts"],"names":[],"mappings":"AAAA,sFAAsF;AACtF,kFAAkF;AAClF,yEAAyE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zelkovajs/adapter-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.8.0",
|
|
4
|
+
"description": "The kit for a Zelkova user adapter: a service that connects a chat platform, such as Slack, to Zelkova.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/zelkovajs/zelkova.git",
|
|
9
|
+
"directory": "packages/adapter-kit"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/zelkovajs/zelkova/tree/main/packages/adapter-kit#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/zelkovajs/zelkova/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"LICENSE",
|
|
19
|
+
"NOTICE"
|
|
20
|
+
],
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"default": "./dist/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./testing": {
|
|
27
|
+
"types": "./dist/testing.d.ts",
|
|
28
|
+
"default": "./dist/testing.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@zelkovajs/core": "0.8.0"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=24"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsc -p tsconfig.build.json",
|
|
42
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
43
|
+
}
|
|
6
44
|
}
|
package/README.md
DELETED