Refactor runtime around isolated bot instances
This commit is contained in:
+7
-15
@@ -1,6 +1,5 @@
|
||||
import type { ConversationRuntime } from "../acp/types.js";
|
||||
import type { GatewayPolicy } from "../config.js";
|
||||
import type { RoleRegistry } from "../roles/role-registry.js";
|
||||
import type { PlatformAdapter } from "./adapter.js";
|
||||
import { CommandRouter, type ParsedCommand } from "./command-router.js";
|
||||
import { chatKeyFor } from "./durable-session-store.js";
|
||||
@@ -12,12 +11,12 @@ export class Gateway {
|
||||
private readonly locks = new Map<string, Promise<void>>();
|
||||
readonly commandRouter = new CommandRouter();
|
||||
|
||||
constructor(private readonly policy: GatewayPolicy, private readonly runtime: ConversationRuntime, private readonly roles: RoleRegistry) {}
|
||||
constructor(private readonly policy: GatewayPolicy, private readonly runtime: ConversationRuntime) {}
|
||||
|
||||
async receive(message: IncomingMessage, adapter: PlatformAdapter, options: { synchronous?: boolean } = {}): Promise<GatewayResult> {
|
||||
const policyError = this.checkPolicy(message);
|
||||
if (policyError) {
|
||||
console.log(`Message ignored by policy: ${policyError} (${message.platform} ${message.chatId} ${message.userId})`);
|
||||
console.log(`Message ignored by policy: ${policyError} (platform=${message.platform})`);
|
||||
return { ok: true, ignored: true, error: policyError };
|
||||
}
|
||||
|
||||
@@ -43,24 +42,17 @@ export class Gateway {
|
||||
stats(): ReturnType<ConversationRuntime["stats"]> & { lockedChats: number } { return { ...this.runtime.stats(), lockedChats: this.locks.size }; }
|
||||
|
||||
private async executeCommand(command: ParsedCommand, message: IncomingMessage): Promise<string> {
|
||||
const prefix = command.deprecatedAlias ? "Deprecated alias; use /role or /roles.\n" : "";
|
||||
switch (command.kind) {
|
||||
case "help": return ["Commands:", "/roles", "/role <id>", "/status", "/cancel", "/new", "/help"].join("\n");
|
||||
case "roles": return `${prefix}Available roles: ${this.roles.list().join(", ")}`;
|
||||
case "role":
|
||||
if (!command.argument) return `${prefix}Usage: /role <id>`;
|
||||
if (!this.roles.has(command.argument)) return `${prefix}Unknown role: ${command.argument}`;
|
||||
await this.runtime.cancel(message.platform, message.chatId);
|
||||
await this.runtime.selectRole(message.platform, message.chatId, command.argument);
|
||||
return `${prefix}Selected role: ${command.argument}`;
|
||||
case "help": return ["Commands:", "/status", "/cancel", "/new", "/help"].join("\n");
|
||||
case "status": {
|
||||
const status = this.runtime.status(message.platform, message.chatId);
|
||||
return `OK\n${Object.entries(status).map(([key, value]) => `${key}=${value}`).join("\n")}`;
|
||||
}
|
||||
case "new":
|
||||
await this.runtime.reset(message.platform, message.chatId);
|
||||
return "Started a new native ACP session for this chat and role.";
|
||||
return "Started a new native ACP session for this chat.";
|
||||
case "cancel": return this.cancelText(message);
|
||||
case "retired-role": return "Role switching was removed in Config v3; this instance has one fixed Bot and ACP agent.";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,8 +70,8 @@ export class Gateway {
|
||||
}
|
||||
|
||||
private checkPolicy(message: IncomingMessage): string | undefined {
|
||||
if (this.policy.allowedUsers.length > 0 && !this.policy.allowedUsers.includes(message.userId)) return `User not allowed: ${message.userId}`;
|
||||
if (this.policy.allowedChats.length > 0 && !this.policy.allowedChats.includes(message.chatId)) return `Chat not allowed: ${message.chatId}`;
|
||||
if (this.policy.allowedUsers.length > 0 && !this.policy.allowedUsers.includes(message.userId)) return "User not allowed";
|
||||
if (this.policy.allowedChats.length > 0 && !this.policy.allowedChats.includes(message.chatId)) return "Chat not allowed";
|
||||
if (this.policy.requireMentionInGroup && message.isGroup && !message.mentionsBot) return "Mention required in group chat";
|
||||
return undefined;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user