@cogitator-ai/channels 0.15.12 → 0.16.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 (71) hide show
  1. package/README.md +88 -23
  2. package/dist/assistant-config.d.ts +38 -0
  3. package/dist/assistant-config.d.ts.map +1 -1
  4. package/dist/assistant-config.js +61 -0
  5. package/dist/assistant-config.js.map +1 -1
  6. package/dist/channels/bluesky/channel.d.ts.map +1 -1
  7. package/dist/channels/bluesky/channel.js +1 -0
  8. package/dist/channels/bluesky/channel.js.map +1 -1
  9. package/dist/channels/github/channel.d.ts +222 -0
  10. package/dist/channels/github/channel.d.ts.map +1 -0
  11. package/dist/channels/github/channel.js +736 -0
  12. package/dist/channels/github/channel.js.map +1 -0
  13. package/dist/channels/github/client.d.ts +45 -0
  14. package/dist/channels/github/client.d.ts.map +1 -0
  15. package/dist/channels/github/client.js +119 -0
  16. package/dist/channels/github/client.js.map +1 -0
  17. package/dist/channels/github/events.d.ts +49 -0
  18. package/dist/channels/github/events.d.ts.map +1 -0
  19. package/dist/channels/github/events.js +147 -0
  20. package/dist/channels/github/events.js.map +1 -0
  21. package/dist/channels/github/index.d.ts +4 -0
  22. package/dist/channels/github/index.d.ts.map +1 -0
  23. package/dist/channels/github/index.js +4 -0
  24. package/dist/channels/github/index.js.map +1 -0
  25. package/dist/channels/slack.d.ts.map +1 -1
  26. package/dist/channels/slack.js +2 -1
  27. package/dist/channels/slack.js.map +1 -1
  28. package/dist/channels/telegram.d.ts.map +1 -1
  29. package/dist/channels/telegram.js +14 -6
  30. package/dist/channels/telegram.js.map +1 -1
  31. package/dist/channels/threads/channel.d.ts.map +1 -1
  32. package/dist/channels/threads/channel.js +1 -0
  33. package/dist/channels/threads/channel.js.map +1 -1
  34. package/dist/channels/webchat.d.ts +8 -0
  35. package/dist/channels/webchat.d.ts.map +1 -1
  36. package/dist/channels/webchat.js +54 -15
  37. package/dist/channels/webchat.js.map +1 -1
  38. package/dist/channels/whatsapp.d.ts.map +1 -1
  39. package/dist/channels/whatsapp.js +13 -0
  40. package/dist/channels/whatsapp.js.map +1 -1
  41. package/dist/formatters/markdown.d.ts.map +1 -1
  42. package/dist/formatters/markdown.js +1 -0
  43. package/dist/formatters/markdown.js.map +1 -1
  44. package/dist/gateway.d.ts +69 -1
  45. package/dist/gateway.d.ts.map +1 -1
  46. package/dist/gateway.js +249 -40
  47. package/dist/gateway.js.map +1 -1
  48. package/dist/heartbeat.d.ts +5 -1
  49. package/dist/heartbeat.d.ts.map +1 -1
  50. package/dist/heartbeat.js +13 -3
  51. package/dist/heartbeat.js.map +1 -1
  52. package/dist/index.d.ts +5 -1
  53. package/dist/index.d.ts.map +1 -1
  54. package/dist/index.js +3 -1
  55. package/dist/index.js.map +1 -1
  56. package/dist/merge-messages.d.ts +3 -2
  57. package/dist/merge-messages.d.ts.map +1 -1
  58. package/dist/merge-messages.js +4 -2
  59. package/dist/merge-messages.js.map +1 -1
  60. package/dist/message-queue.d.ts +14 -4
  61. package/dist/message-queue.d.ts.map +1 -1
  62. package/dist/message-queue.js +36 -35
  63. package/dist/message-queue.js.map +1 -1
  64. package/dist/runtime-builder.d.ts.map +1 -1
  65. package/dist/runtime-builder.js +39 -5
  66. package/dist/runtime-builder.js.map +1 -1
  67. package/dist/thread-key.d.ts +50 -0
  68. package/dist/thread-key.d.ts.map +1 -0
  69. package/dist/thread-key.js +82 -0
  70. package/dist/thread-key.js.map +1 -0
  71. package/package.json +8 -7
@@ -0,0 +1,222 @@
1
+ import { type RequestListener } from 'node:http';
2
+ import type { Attachment, Channel, ChannelMessage, SendOptions } from '@cogitator-ai/types';
3
+ import { type GitHubToken } from './client.js';
4
+ /** The longest comment GitHub takes, in characters. */
5
+ export declare const GITHUB_COMMENT_LIMIT = 65536;
6
+ /** The shortest time between two edits of one comment, under GitHub's limit on content changes. */
7
+ export declare const GITHUB_MIN_EDIT_INTERVAL = 3000;
8
+ /** How an author is related to a repository, as GitHub's `author_association` says it. */
9
+ export type GitHubAssociation = 'OWNER' | 'MEMBER' | 'COLLABORATOR' | 'CONTRIBUTOR' | 'FIRST_TIME_CONTRIBUTOR' | 'FIRST_TIMER' | 'MANNEQUIN' | 'NONE';
10
+ /** The authors the bot answers by default: the repository's owner, the organization's members and collaborators. */
11
+ export declare const GITHUB_TRUSTED_ASSOCIATIONS: readonly GitHubAssociation[];
12
+ export interface GitHubWebhookConfig {
13
+ /** The secret set on the webhook, which signs every delivery. Required. */
14
+ secret: string;
15
+ /**
16
+ * Run an HTTP server of the channel's own on this port. Without it, mount
17
+ * `handleWebhook` or `webhookListener()` in a server of yours.
18
+ */
19
+ port?: number;
20
+ /** The path of the channel's own server (default `/github/webhook`). */
21
+ path?: string;
22
+ host?: string;
23
+ }
24
+ export interface GitHubChannelConfig {
25
+ /**
26
+ * A personal access token, a GitHub App installation token, or a function
27
+ * that returns a fresh one before every request. It needs to read and write
28
+ * issues and pull requests of the repositories the bot answers in.
29
+ */
30
+ token: GitHubToken;
31
+ /** Where deliveries come from: GitHub only pushes events, so the webhook is required. */
32
+ webhook: GitHubWebhookConfig;
33
+ /**
34
+ * The bot's login, for skipping its own comments and finding mentions of it.
35
+ * Loaded with `GET /user` when not set, which a GitHub App installation
36
+ * token cannot do: an app sets it to `<app-slug>[bot]`.
37
+ */
38
+ login?: string;
39
+ /**
40
+ * Which events the bot answers: `mention` (default) answers only those
41
+ * that mention `@login`, `all` every new issue, pull request, comment and
42
+ * review in the allowed repositories. Once the bot answered someone in a
43
+ * conversation, their next comment there reaches it without a mention (an
44
+ * answer to an approval prompt, a follow-up), and so does every comment in
45
+ * a review thread it replied in.
46
+ */
47
+ trigger?: 'mention' | 'all';
48
+ /**
49
+ * The repositories to answer in, as `owner/repo` or `owner/*` for every
50
+ * repository of an owner. Default: every repository that sends events,
51
+ * which `start()` warns about.
52
+ */
53
+ repositories?: string[];
54
+ /**
55
+ * Whose events the bot answers, by their relation to the repository
56
+ * (`author_association`). Default `OWNER`, `MEMBER` and `COLLABORATOR`,
57
+ * since what people write steers an agent that holds the token. `'any'`
58
+ * answers everyone, for a public bot whose agent has nothing to leak.
59
+ */
60
+ associations?: readonly GitHubAssociation[] | 'any';
61
+ /** Logins the bot answers whatever their association, such as private members of an organization. */
62
+ authors?: string[];
63
+ /** Answer other bots and apps too (default false, so two bots cannot answer each other forever). */
64
+ allowBots?: boolean;
65
+ /**
66
+ * Stream answers into a comment that is edited as the text grows (default
67
+ * false). GitHub notifies subscribers of a comment once, with its first
68
+ * text, and limits how often content may be changed, so answers are sent
69
+ * once finished unless this is on. Edits of one comment are at least
70
+ * `GITHUB_MIN_EDIT_INTERVAL` (3 s) apart, the latest text winning.
71
+ */
72
+ stream?: boolean;
73
+ /** The REST API root, `https://<host>/api/v3` on GitHub Enterprise Server. */
74
+ apiUrl?: string;
75
+ fetch?: typeof fetch;
76
+ }
77
+ /** A webhook delivery, as any HTTP server hands it over. */
78
+ export interface GitHubWebhookRequest {
79
+ method: string;
80
+ headers: Record<string, string | string[] | undefined>;
81
+ /** The body exactly as received, which the signature covers. */
82
+ rawBody?: string | Uint8Array;
83
+ }
84
+ export interface GitHubWebhookResponse {
85
+ status: number;
86
+ body: string;
87
+ }
88
+ /** Whether `signature` (`sha256=<hex>`) is the HMAC SHA-256 of `body` with `secret`. */
89
+ export declare function verifyGitHubSignature(body: string | Uint8Array, signature: string | undefined, secret: string): boolean;
90
+ /**
91
+ * A GitHub channel: an agent answers issues, pull requests, their comments
92
+ * and reviews, in the repositories a webhook sends events from. Each issue or
93
+ * pull request is one conversation, `channelId` `owner/repo#number`, and
94
+ * answers are comments on it. A comment on a line of a pull request is
95
+ * answered in its review thread, which is the message's `topicId` (the id of
96
+ * the thread's first comment), so every review thread is a conversation of
97
+ * its own. Deliveries are checked against the webhook secret, answered at
98
+ * once and handled after.
99
+ */
100
+ export declare class GitHubChannel implements Channel {
101
+ readonly type = "github";
102
+ readonly editable: boolean;
103
+ readonly nativeMarkdown = true;
104
+ readonly maxMessageChars = 65536;
105
+ private readonly config;
106
+ private readonly client;
107
+ private handler;
108
+ private readonly deliveries;
109
+ private readonly handled;
110
+ private readonly reactions;
111
+ private readonly reacting;
112
+ private readonly edits;
113
+ /** People who wrote to the bot in a conversation and were not answered yet, by conversation. */
114
+ private readonly waiting;
115
+ /** The conversation and author of each message handed to the agent, by `channelId|messageId`. */
116
+ private readonly askers;
117
+ private readonly engaged;
118
+ private server?;
119
+ private identity?;
120
+ private botLogin?;
121
+ private running;
122
+ private stopped;
123
+ constructor(config: GitHubChannelConfig);
124
+ /** The bot's login, once known: as configured, or loaded by `start()` or the first delivery. */
125
+ get login(): string | undefined;
126
+ onMessage(handler: (msg: ChannelMessage) => Promise<void>): void;
127
+ start(): Promise<void>;
128
+ stop(): Promise<void>;
129
+ /**
130
+ * Answers a webhook delivery: checks its signature, answers at once and
131
+ * hands the event to the agent after. Mount it in any HTTP server, with the
132
+ * raw body unparsed. It works before `start()` too, and answers `503` once
133
+ * the channel is stopped. A delivery GitHub sends again is handled once:
134
+ * by its `X-GitHub-Delivery` and by the comment, issue or review it
135
+ * carries, which the signature covers. Both are remembered in memory only,
136
+ * for the latest 1000, so a delivery sent again after a restart is
137
+ * handled again.
138
+ */
139
+ handleWebhook(request: GitHubWebhookRequest): Promise<GitHubWebhookResponse>;
140
+ /**
141
+ * A Node request listener that answers webhook deliveries on any path it
142
+ * is mounted at, such as a route of a server that serves other things too.
143
+ * Bodies over 1 MB are refused with `413`.
144
+ */
145
+ webhookListener(): RequestListener;
146
+ /**
147
+ * Comments on the issue or pull request of `channelId`, as several
148
+ * comments when the text is longer than GitHub takes. With a `topicId`, or
149
+ * a `replyTo` of a review comment, the comments are replies in that review
150
+ * thread. Resolves with the first comment's id.
151
+ */
152
+ sendText(channelId: string, text: string, options?: SendOptions): Promise<string>;
153
+ /**
154
+ * Replaces the text of a comment the bot made (`comment:<id>` or
155
+ * `review_comment:<id>`). Edits of one comment go out at least
156
+ * `GITHUB_MIN_EDIT_INTERVAL` apart: an edit made sooner waits, and edits
157
+ * made while it waits are folded into it, the latest text winning.
158
+ */
159
+ editText(channelId: string, messageId: string, text: string): Promise<void>;
160
+ /** Sends the latest text of an edit once the comment's last edit is far enough back. */
161
+ private flushEdit;
162
+ /**
163
+ * GitHub has no API to upload files to comments, so a file with a public
164
+ * `url` is sent as a comment that links it (an image shows inline), and a
165
+ * file that only has a `buffer` is refused.
166
+ */
167
+ sendFile(channelId: string, file: Attachment, options?: SendOptions): Promise<string>;
168
+ sendTyping(): Promise<void>;
169
+ /** Deletes a comment the bot made. */
170
+ deleteMessage(channelId: string, messageId: string): Promise<void>;
171
+ /**
172
+ * Reacts to an issue, pull request or comment, replacing the bot's earlier
173
+ * reaction on it. GitHub has eight reactions (+1, -1, laugh, confused,
174
+ * heart, hooray, rocket, eyes), named or as their emoji, and the status
175
+ * emojis of thinking, a tool and an error become eyes, rocket and
176
+ * confused. Other emojis are skipped, and so are reviews, which take no
177
+ * reactions. Reactions to one message are set one after another, so the
178
+ * last one called is the one that stays.
179
+ */
180
+ setReaction(channelId: string, messageId: string, emoji: string): Promise<void>;
181
+ private react;
182
+ private comment;
183
+ private commentPath;
184
+ private reactionsPath;
185
+ /**
186
+ * Notes that the bot answered in a conversation. The person the answer is
187
+ * for (its `replyTo`, or everyone waiting there when it names no message
188
+ * the bot was asked in) may write on without a mention, and in a review
189
+ * thread everyone may.
190
+ */
191
+ private answered;
192
+ private remember;
193
+ /** Whether a message reaches the bot without a mention: in a conversation it answered. */
194
+ private takesWithoutMention;
195
+ /**
196
+ * Hands a delivery's event to the agent when it is one to answer. Resolves
197
+ * false when the event was dropped because the bot's login could not be
198
+ * loaded or the channel stopped, so the delivery may be sent again.
199
+ */
200
+ private receive;
201
+ /**
202
+ * The bot's login for a delivery: loaded when it is not known yet, and
203
+ * tried again a few times, since GitHub does not send a delivery again by
204
+ * itself. The delivery is dropped, with an error logged, when it cannot be
205
+ * loaded.
206
+ */
207
+ private loginForDelivery;
208
+ /** Loads the bot's login once, unless it is configured. */
209
+ private identify;
210
+ private allowsRepository;
211
+ /** Whether the author may talk to the bot: by their association, or named in `authors`. */
212
+ private trusts;
213
+ private isOwnOrBot;
214
+ /** The name people mention the bot by: its login, without `[bot]` for an app. */
215
+ private mentionName;
216
+ private mentionsBot;
217
+ private stripMention;
218
+ private listen;
219
+ }
220
+ /** A GitHub channel that answers issues, pull requests, comments and reviews. */
221
+ export declare function githubChannel(config: GitHubChannelConfig): GitHubChannel;
222
+ //# sourceMappingURL=channel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../../../src/channels/github/channel.ts"],"names":[],"mappings":"AACA,OAAO,EAAsC,KAAK,eAAe,EAAe,MAAM,WAAW,CAAC;AAElG,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAG5F,OAAO,EAAgB,KAAK,WAAW,EAAE,MAAM,UAAU,CAAC;AAQ1D,uDAAuD;AACvD,eAAO,MAAM,oBAAoB,QAAS,CAAC;AAI3C,mGAAmG;AACnG,eAAO,MAAM,wBAAwB,OAAO,CAAC;AAI7C,0FAA0F;AAC1F,MAAM,MAAM,iBAAiB,GACzB,OAAO,GACP,QAAQ,GACR,cAAc,GACd,aAAa,GACb,wBAAwB,GACxB,aAAa,GACb,WAAW,GACX,MAAM,CAAC;AAEX,oHAAoH;AACpH,eAAO,MAAM,2BAA2B,EAAE,SAAS,iBAAiB,EAInE,CAAC;AA4BF,MAAM,WAAW,mBAAmB;IAClC,2EAA2E;IAC3E,MAAM,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,wEAAwE;IACxE,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,mBAAmB;IAClC;;;;OAIG;IACH,KAAK,EAAE,WAAW,CAAC;IACnB,yFAAyF;I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