@alekstar79/ykassa-messenger 0.0.0-stage → 1.1.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.
Files changed (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +159 -2
  3. package/dist/adapters/max.d.ts +43 -0
  4. package/dist/adapters/max.d.ts.map +1 -0
  5. package/dist/adapters/max.js +513 -0
  6. package/dist/adapters/max.js.map +1 -0
  7. package/dist/adapters/telegram.d.ts +50 -0
  8. package/dist/adapters/telegram.d.ts.map +1 -0
  9. package/dist/adapters/telegram.js +283 -0
  10. package/dist/adapters/telegram.js.map +1 -0
  11. package/dist/apiClient.d.ts +54 -0
  12. package/dist/apiClient.d.ts.map +1 -0
  13. package/dist/apiClient.js +145 -0
  14. package/dist/apiClient.js.map +1 -0
  15. package/dist/approvals.d.ts +36 -0
  16. package/dist/approvals.d.ts.map +1 -0
  17. package/dist/approvals.js +66 -0
  18. package/dist/approvals.js.map +1 -0
  19. package/dist/audit.d.ts +16 -0
  20. package/dist/audit.d.ts.map +1 -0
  21. package/dist/audit.js +30 -0
  22. package/dist/audit.js.map +1 -0
  23. package/dist/config.d.ts +18 -0
  24. package/dist/config.d.ts.map +1 -0
  25. package/dist/config.js +162 -0
  26. package/dist/config.js.map +1 -0
  27. package/dist/gateway.d.ts +47 -0
  28. package/dist/gateway.d.ts.map +1 -0
  29. package/dist/gateway.js +436 -0
  30. package/dist/gateway.js.map +1 -0
  31. package/dist/index.d.ts +13 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +13 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/render.d.ts +85 -0
  36. package/dist/render.d.ts.map +1 -0
  37. package/dist/render.js +223 -0
  38. package/dist/render.js.map +1 -0
  39. package/dist/runtime.d.ts +24 -0
  40. package/dist/runtime.d.ts.map +1 -0
  41. package/dist/runtime.js +87 -0
  42. package/dist/runtime.js.map +1 -0
  43. package/dist/server.d.ts +2 -0
  44. package/dist/server.d.ts.map +1 -0
  45. package/dist/server.js +36 -0
  46. package/dist/server.js.map +1 -0
  47. package/dist/session.d.ts +17 -0
  48. package/dist/session.d.ts.map +1 -0
  49. package/dist/session.js +62 -0
  50. package/dist/session.js.map +1 -0
  51. package/dist/throttle.d.ts +18 -0
  52. package/dist/throttle.d.ts.map +1 -0
  53. package/dist/throttle.js +71 -0
  54. package/dist/throttle.js.map +1 -0
  55. package/dist/types.d.ts +52 -0
  56. package/dist/types.d.ts.map +1 -0
  57. package/dist/types.js +2 -0
  58. package/dist/types.js.map +1 -0
  59. package/package.json +76 -4
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aleksey Tarasenko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,160 @@
1
- # Temporary Holding Version
1
+ # YooKassa Messenger Gateway
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The gateway is an independent HTTP client of the headless API. It contains no
4
+ LLM or MCP logic; API policy and payment execution remain in `apps/api`.
5
+
6
+ Run tests with `npm test --workspace=@alekstar79/ykassa-messenger`. From the
7
+ client project directory, start the Messenger gateway with
8
+ `npx @alekstar79/ykassa-mcp messenger`. In this repository, first build it with
9
+ `npm run build --workspace=@alekstar79/ykassa-messenger`, then use
10
+ `npm run start --workspace=@alekstar79/ykassa-messenger` for local development.
11
+ It starts each enabled Telegram or MAX transport whose client token is
12
+ configured.
13
+
14
+ ## Chat commands
15
+
16
+ Commands work in an authorized private chat. Send `/help` to the bot for this
17
+ list:
18
+
19
+ - `/start` sends the welcome message. A sender who is not allowlisted is still
20
+ denied.
21
+ - `/help` explains the available commands and how to send a request.
22
+ - `/status` shows the configured client name and its allowed MCP scenarios.
23
+ - `/reset` clears a waiting file request. It does not cancel a pending
24
+ approval; use the approval card's buttons to approve or reject it.
25
+
26
+ Each request is sent to the API independently; earlier user messages are not
27
+ replayed into later asks. If MAX sends request text and its file as separate
28
+ messages, mention the file or list in the text. The gateway waits for the next
29
+ valid attachment before calling the API, and discards that waiting request if a
30
+ different text request arrives first. MAX can deliver the text and attachment
31
+ as separate events even when they were sent together. When a request is waiting
32
+ for a file, the gateway delays its "send the file" prompt for 3 seconds; a file
33
+ that arrives during that window is processed with the request without the
34
+ premature prompt.
35
+
36
+ Risky operations are decided with the buttons on the approval card. Typing
37
+ “yes”, “approve”, or a similar chat message does not submit an approval.
38
+ If the model replies to an explicit payment/invoice creation request by asking
39
+ for text confirmation, the API retries once with only that creation tool
40
+ required. A prepared proposal appears as an approval card, without a separate
41
+ confirmation-text reply; the user does not need to send an extra message.
42
+ Recovered proposals always require explicit approval, even below the configured
43
+ threshold. This recovery requires an OpenAI-compatible provider that supports
44
+ required tool choice. If recovery fails, the bot reports that the operation
45
+ was not executed.
46
+
47
+ ## Telegram manual E2E
48
+
49
+ Live E2E uses a test Telegram bot and the operator's YooKassa test environment.
50
+ Do not commit bot or YooKassa tokens.
51
+
52
+ 1. Create a test bot with `@BotFather` and keep its token outside source control.
53
+ 2. In `clients/<name>/config.yaml`, enable Telegram with an empty allowlist.
54
+ This denies all senders during the initial identity check:
55
+
56
+ ```yaml
57
+ messengers:
58
+ telegram:
59
+ enabled: true
60
+ allowed_user_ids: []
61
+ locale: ru
62
+ ```
63
+
64
+ 3. In `clients/<name>/.env`, set `TELEGRAM_BOT_TOKEN`. Keep the existing
65
+ `YOOKASSA_SHOP_ID` and `YOOKASSA_MCP_TOKEN` values there as well. Never put
66
+ the bot token in YAML or commit it.
67
+ 4. In a separate terminal, start the local Copilot proxy and complete OAuth
68
+ if prompted:
69
+
70
+ ```sh
71
+ HOST=127.0.0.1 npx copilot-api@latest start
72
+ ```
73
+
74
+ Keep it running and bound to loopback. Verify the model list with
75
+ `curl http://127.0.0.1:4141/v1/models`.
76
+ 5. Merge these LLM settings into the root `.env`, preserving `PORT` and other
77
+ unrelated values. `gpt-4o` is available in the model list shown in this
78
+ runbook's setup; choose another exact ID from `/v1/models` if needed:
79
+
80
+ ```dotenv
81
+ LLM_PROVIDER=openai
82
+ LLM_MODEL=gpt-4o
83
+ LLM_BASE_URL=http://127.0.0.1:4141/v1
84
+ LLM_API_KEY=dummy-token-for-proxy
85
+ ```
86
+
87
+ The API key here is a local placeholder, not a GitHub credential. Keep
88
+ YooKassa credentials in the client `.env`, then start the API with
89
+ `npm run start:api`. See [copilot-proxy.md](../../docs/copilot-proxy.md)
90
+ for details and the proxy's security limitations.
91
+ 6. Start the gateway with
92
+ `npx @alekstar79/ykassa-mcp messenger` (or the workspace command during local
93
+ development). The gateway scans
94
+ `CLIENTS_DIR` (default `./clients`) and starts one bot per enabled client
95
+ with a valid token. Before allowing any sender, send `/start` to the bot
96
+ once. The request is denied and recorded without its message body; read your
97
+ `user_id` from the corresponding `message_denied` record in
98
+ `logs/messenger-actions.jsonl`. Add that ID as a quoted string to
99
+ `allowed_user_ids`, then restart the gateway.
100
+ 7. In the bot's private chat, verify:
101
+ - A request for a 100 RUB payment produces a `Pay` URL button when permitted
102
+ by the client's API policy.
103
+ - A 5000 RUB request that crosses the configured approval threshold displays
104
+ approval buttons immediately, without a separate “Подтверждаю” message;
105
+ approve it and verify the resulting payment link.
106
+ - A CSV attachment within `limits.max_attachment_kb` is handled by the API.
107
+ - For a split MAX upload, send a prompt mentioning the file or list, confirm
108
+ the bot waits without calling the API, then send the CSV/TXT file.
109
+ - A sender ID not in `allowed_user_ids` is denied.
110
+ 8. Check `logs/messenger-actions.jsonl` for gateway events and
111
+ `logs/mcp-calls.jsonl` for API-side MCP audit records. Neither log should
112
+ contain bot tokens or message bodies.
113
+
114
+ The gateway keeps waiting file requests and approval correlation in memory.
115
+ Restarting it clears that state; this is an MVP limitation, not a durability
116
+ guarantee.
117
+ Telegram's Bot API file-download limit also applies. The gateway rejects
118
+ downloads above that transport limit; accepted files are subsequently checked
119
+ by the core against the configured file size and CSV/TXT policy.
120
+
121
+ ## MAX setup and manual E2E
122
+
123
+ The MAX adapter uses the official Bot API with long polling. MAX documents
124
+ long polling for development and testing only and recommends webhooks for
125
+ production; webhook delivery is not implemented by this gateway.
126
+
127
+ 1. Create a MAX bot and keep its access token outside source control.
128
+ 2. In `clients/<name>/config.yaml`, enable MAX with an empty allowlist:
129
+
130
+ ```yaml
131
+ messengers:
132
+ max:
133
+ enabled: true
134
+ allowed_user_ids: []
135
+ locale: ru
136
+ ```
137
+
138
+ 3. Add `MAX_BOT_TOKEN` to `clients/<name>/.env`. The same client can keep
139
+ Telegram enabled; each transport starts independently when its own token
140
+ is present.
141
+ 4. Start the API and local LLM proxy as described in the Telegram E2E steps,
142
+ then start the Messenger gateway. Send `/start` to the MAX bot once. The
143
+ request is denied until the sender is allowlisted. Read the sender's
144
+ `user_id` from the `message_denied` entry in
145
+ `logs/messenger-actions.jsonl`, add that ID as a quoted string to
146
+ `allowed_user_ids`, and restart the gateway.
147
+ 5. In the bot's private chat, verify `/help`, `/status`, a permitted payment
148
+ request with a payment-link button, and a request above the approval
149
+ threshold with working approve/reject buttons.
150
+ 6. Ask the bot to process a CSV/TXT file and attach one file within the
151
+ client's configured size limit in the same composer. Verify that no
152
+ premature "send the file" prompt appears if MAX delivers the text and file
153
+ as separate events. Also test a split upload with the file sent after the
154
+ 3-second grace period: the bot should ask for the file and process it when
155
+ it arrives. MAX image, audio, video, multiple-file, and non-CSV/TXT
156
+ attachments are not passed to the API.
157
+
158
+ The MAX adapter and gateway wiring are covered by the Messenger workspace test
159
+ suite. Live MAX E2E requires an operator-provided bot token and an approved MAX
160
+ bot; no live MAX requests are made by automated tests.
@@ -0,0 +1,43 @@
1
+ import type { Button, CallbackEvent, CallbackHandler, IncomingMessage, MessageHandler, MessageRef } from '../types.js';
2
+ export interface MaxAdapterOptions {
3
+ fetcher?: typeof fetch;
4
+ pollTimeoutSeconds?: number;
5
+ maxAttachmentKb?: number;
6
+ }
7
+ interface MaxFileDownload {
8
+ url: string;
9
+ filename: string;
10
+ size: number;
11
+ }
12
+ type MappedMaxMessage = IncomingMessage & {
13
+ fileDownload?: MaxFileDownload;
14
+ };
15
+ export declare class MaxAdapter {
16
+ private readonly token;
17
+ readonly platform: 'max';
18
+ private readonly fetcher;
19
+ private readonly pollTimeoutSeconds;
20
+ private readonly maxAttachmentKb;
21
+ private started;
22
+ private running;
23
+ private pollingTask;
24
+ private pollAbortController;
25
+ private onMessage;
26
+ private onCallback;
27
+ private marker;
28
+ constructor(token: string, options?: MaxAdapterOptions);
29
+ start(onMessage: MessageHandler, onCallback: CallbackHandler): Promise<void>;
30
+ sendText(chatId: string, text: string, buttons?: Button[]): Promise<MessageRef>;
31
+ editText(ref: MessageRef, text: string, buttons?: Button[]): Promise<void>;
32
+ answerCallback(callbackId: string, text?: string): Promise<void>;
33
+ setTyping(_chatId: string): Promise<void>;
34
+ stop(): Promise<void>;
35
+ private pollUpdates;
36
+ private dispatchUpdate;
37
+ private requestJson;
38
+ private downloadFile;
39
+ }
40
+ export declare function mapMaxMessage(update: unknown, maxAttachmentKb?: number): MappedMaxMessage | undefined;
41
+ export declare function mapMaxCallback(update: unknown): CallbackEvent | undefined;
42
+ export {};
43
+ //# sourceMappingURL=max.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"max.d.ts","sourceRoot":"","sources":["../../src/adapters/max.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,MAAM,EACN,aAAa,EACb,eAAe,EAEf,eAAe,EACf,cAAc,EACd,UAAU,EACX,MAAM,aAAa,CAAC;AASrB,MAAM,WAAW,iBAAiB;IAChC,OAAO,CAAC,EAAE,OAAO,KAAK,CAAC;IACvB,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,UAAU,eAAe;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,KAAK,gBAAgB,GAAG,eAAe,GAAG;IACxC,YAAY,CAAC,EAAE,eAAe,CAAC;CAChC,CAAC;AAcF,qBAAa,UAAU;IAcnB,OAAO,CAAC,QAAQ,CAAC,KAAK;IAbxB,QAAQ,CAAC,QAAQ,EAAG,KAAK,CAAU;IACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAe;IACvC,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAS;IAC5C,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAS;IACzC,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,WAAW,CAA4B;IAC/C,OAAO,CAAC,mBAAmB,CAA8B;IACzD,OAAO,CAAC,SAAS,CAA6B;IAC9C,OAAO,CAAC,UAAU,CAA8B;IAChD,OAAO,CAAC,MAAM,CAAqB;IAEnC,YACmB,KAAK,EAAE,MAAM,EAC9B,OAAO,GAAE,iBAAsB,EAwBhC;IAEK,KAAK,CACT,SAAS,EAAE,cAAc,EACzB,UAAU,EAAE,eAAe,GAC1B,OAAO,CAAC,IAAI,CAAC,CAOf;IAEK,QAAQ,CACZ,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,MAAM,EAAE,GACjB,OAAO,CAAC,UAAU,CAAC,CAcrB;IAEK,QAAQ,CACZ,GAAG,EAAE,UAAU,EACf,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,MAAM,EAAE,GACjB,OAAO,CAAC,IAAI,CAAC,CAcf;IAEK,cAAc,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAUrE;IAEK,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE9C;IAEK,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAU1B;YAEa,WAAW;YA6CX,cAAc;YAqCd,WAAW;YAsCX,YAAY;CAqB3B;AAED,wBAAgB,aAAa,CAC3B,MAAM,EAAE,OAAO,EACf,eAAe,SAA4B,GAC1C,gBAAgB,GAAG,SAAS,CAyD9B;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,OAAO,GAAG,aAAa,GAAG,SAAS,CAuCzE"}