Skip to content

Commit d42a8ae

Browse files
morealclaude
andcommitted
Update BotImpl to support instance mode
Add options to BotImpl for integration with Instance: - federation: Accept an existing Federation instead of creating new one - skipInitialize: Skip dispatcher/listener registration when managed by Instance Update InstanceImpl to use these options when creating bots, ensuring all bots share the same Federation instance. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 00d4e2f commit d42a8ae

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

packages/botkit/src/bot-impl.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ import type { Text } from "./text.ts";
102102
export interface BotImplOptions<TContextData>
103103
extends CreateBotOptions<TContextData> {
104104
collectionWindow?: number;
105+
/**
106+
* An existing federation to use instead of creating a new one.
107+
* When provided, `skipInitialize` should also be set to `true`.
108+
*/
109+
federation?: Federation<TContextData>;
110+
/**
111+
* Whether to skip the initialization of the federation.
112+
* Set this to `true` when this bot is managed by an Instance.
113+
*/
114+
skipInitialize?: boolean;
105115
}
106116

107117
export class BotImpl<TContextData> implements Bot<TContextData> {
@@ -159,7 +169,7 @@ export class BotImpl<TContextData> implements Bot<TContextData> {
159169
css: "",
160170
...(options.pages ?? {}),
161171
};
162-
this.federation = createFederation<TContextData>({
172+
this.federation = options.federation ?? createFederation<TContextData>({
163173
kv: options.kv,
164174
queue: options.queue,
165175
userAgent: {
@@ -168,7 +178,9 @@ export class BotImpl<TContextData> implements Bot<TContextData> {
168178
});
169179
this.behindProxy = options.behindProxy ?? false;
170180
this.collectionWindow = options.collectionWindow ?? 50;
171-
this.initialize();
181+
if (!options.skipInitialize) {
182+
this.initialize();
183+
}
172184
}
173185

174186
initialize(): void {

packages/botkit/src/instance-impl.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ export class InstanceImpl<TContextData> implements Instance<TContextData> {
232232
behindProxy: this.behindProxy,
233233
pages: this.pages,
234234
collectionWindow: this.collectionWindow,
235+
federation: this.federation,
236+
skipInitialize: true,
235237
});
236238
// Copy event handlers from template
237239
bot.onFollow = template.onFollow;
@@ -619,6 +621,8 @@ export class InstanceImpl<TContextData> implements Instance<TContextData> {
619621
behindProxy: this.behindProxy,
620622
pages: this.pages,
621623
collectionWindow: this.collectionWindow,
624+
federation: this.federation,
625+
skipInitialize: true,
622626
});
623627

624628
this.#staticBots.set(identifier, bot);
@@ -639,6 +643,8 @@ export class InstanceImpl<TContextData> implements Instance<TContextData> {
639643
behindProxy: this.behindProxy,
640644
pages: this.pages,
641645
collectionWindow: this.collectionWindow,
646+
federation: this.federation,
647+
skipInitialize: true,
642648
});
643649

644650
this.#dynamicDispatchers.push({ dispatcher, template });

0 commit comments

Comments
 (0)