mirror of
https://github.com/harivansh-afk/clanker-agent.git
synced 2026-04-15 05:02:07 +00:00
fix loading
This commit is contained in:
parent
a46e9ede61
commit
cc7f68d4d2
2 changed files with 35 additions and 13 deletions
|
|
@ -65,7 +65,7 @@ export default function (pi: ExtensionAPI) {
|
||||||
|
|
||||||
pi.on("session_start", async (_event, ctx) => {
|
pi.on("session_start", async (_event, ctx) => {
|
||||||
const config = loadConfig(ctx.cwd);
|
const config = loadConfig(ctx.cwd);
|
||||||
registry.loadConfig(config, ctx.cwd);
|
await registry.loadConfig(config, ctx.cwd);
|
||||||
|
|
||||||
const errors = registry.getErrors();
|
const errors = registry.getErrors();
|
||||||
for (const err of errors) {
|
for (const err of errors) {
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,6 @@
|
||||||
* pi-channels — Adapter registry + route resolution.
|
* pi-channels — Adapter registry + route resolution.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { createSlackAdapter } from "./adapters/slack.js";
|
|
||||||
import { createTelegramAdapter } from "./adapters/telegram.js";
|
|
||||||
import { createWebhookAdapter } from "./adapters/webhook.js";
|
|
||||||
import type {
|
import type {
|
||||||
AdapterConfig,
|
AdapterConfig,
|
||||||
AdapterDirection,
|
AdapterDirection,
|
||||||
|
|
@ -27,14 +24,30 @@ type AdapterFactory = (
|
||||||
config: AdapterConfig,
|
config: AdapterConfig,
|
||||||
cwd?: string,
|
cwd?: string,
|
||||||
log?: AdapterLogger,
|
log?: AdapterLogger,
|
||||||
) => ChannelAdapter;
|
) => ChannelAdapter | Promise<ChannelAdapter>;
|
||||||
|
|
||||||
const builtinFactories: Record<string, AdapterFactory> = {
|
const builtinFactories: Record<string, () => Promise<{ default?: unknown } | Record<string, unknown>>> = {
|
||||||
telegram: createTelegramAdapter,
|
telegram: () => import("./adapters/telegram.js"),
|
||||||
webhook: createWebhookAdapter,
|
webhook: () => import("./adapters/webhook.js"),
|
||||||
slack: createSlackAdapter,
|
slack: () => import("./adapters/slack.js"),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function getFactoryExport(
|
||||||
|
type: string,
|
||||||
|
mod: Record<string, unknown>,
|
||||||
|
): AdapterFactory | null {
|
||||||
|
if (type === "telegram" && typeof mod.createTelegramAdapter === "function") {
|
||||||
|
return mod.createTelegramAdapter as AdapterFactory;
|
||||||
|
}
|
||||||
|
if (type === "webhook" && typeof mod.createWebhookAdapter === "function") {
|
||||||
|
return mod.createWebhookAdapter as AdapterFactory;
|
||||||
|
}
|
||||||
|
if (type === "slack" && typeof mod.createSlackAdapter === "function") {
|
||||||
|
return mod.createSlackAdapter as AdapterFactory;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// ── Registry ────────────────────────────────────────────────────
|
// ── Registry ────────────────────────────────────────────────────
|
||||||
|
|
||||||
export class ChannelRegistry {
|
export class ChannelRegistry {
|
||||||
|
|
@ -62,7 +75,7 @@ export class ChannelRegistry {
|
||||||
* Load adapters + routes from config. Custom adapters (registered via events) are preserved.
|
* Load adapters + routes from config. Custom adapters (registered via events) are preserved.
|
||||||
* @param cwd — working directory, passed to adapter factories for settings resolution.
|
* @param cwd — working directory, passed to adapter factories for settings resolution.
|
||||||
*/
|
*/
|
||||||
loadConfig(config: ChannelConfig, cwd?: string): void {
|
async loadConfig(config: ChannelConfig, cwd?: string): Promise<void> {
|
||||||
this.errors = [];
|
this.errors = [];
|
||||||
|
|
||||||
// Stop existing adapters
|
// Stop existing adapters
|
||||||
|
|
@ -87,8 +100,8 @@ export class ChannelRegistry {
|
||||||
|
|
||||||
// Create adapters from config
|
// Create adapters from config
|
||||||
for (const [name, adapterConfig] of Object.entries(config.adapters)) {
|
for (const [name, adapterConfig] of Object.entries(config.adapters)) {
|
||||||
const factory = builtinFactories[adapterConfig.type];
|
const loader = builtinFactories[adapterConfig.type];
|
||||||
if (!factory) {
|
if (!loader) {
|
||||||
this.errors.push({
|
this.errors.push({
|
||||||
adapter: name,
|
adapter: name,
|
||||||
error: `Unknown adapter type: ${adapterConfig.type}`,
|
error: `Unknown adapter type: ${adapterConfig.type}`,
|
||||||
|
|
@ -96,7 +109,16 @@ export class ChannelRegistry {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
this.adapters.set(name, factory(adapterConfig, cwd, this.log));
|
const mod = await loader();
|
||||||
|
const factory = getFactoryExport(adapterConfig.type, mod);
|
||||||
|
if (!factory) {
|
||||||
|
this.errors.push({
|
||||||
|
adapter: name,
|
||||||
|
error: `Adapter module for type ${adapterConfig.type} did not export a valid factory`,
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
this.adapters.set(name, await factory(adapterConfig, cwd, this.log));
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
this.errors.push({ adapter: name, error: err.message });
|
this.errors.push({ adapter: name, error: err.message });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue