mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-17 03:10:38 +09:00
feat(adapters): external adapter plugin system with dynamic UI parser
- Plugin loader: install/reload/remove/reinstall external adapters from npm packages or local directories - Plugin store persisted at ~/.paperclip/adapter-plugins.json - Self-healing UI parser resolution with version caching - UI: Adapter Manager page, dynamic loader, display registry with humanized names for unknown adapter types - Dev watch: exclude adapter-plugins dir from tsx watcher to prevent mid-request server restarts during reinstall - All consumer fallbacks use getAdapterLabel() for consistent display - AdapterTypeDropdown uses controlled open state for proper close behavior - Remove hermes-local from built-in UI (externalized to plugin) - Add docs for external adapters and UI parser contract
This commit is contained in:
parent
f8452a4520
commit
14d59da316
72 changed files with 4102 additions and 585 deletions
|
|
@ -67,18 +67,6 @@ import {
|
|||
import {
|
||||
agentConfigurationDoc as piAgentConfigurationDoc,
|
||||
} from "@paperclipai/adapter-pi-local";
|
||||
import {
|
||||
execute as hermesExecute,
|
||||
testEnvironment as hermesTestEnvironment,
|
||||
sessionCodec as hermesSessionCodec,
|
||||
listSkills as hermesListSkills,
|
||||
syncSkills as hermesSyncSkills,
|
||||
detectModel as detectModelFromHermes,
|
||||
} from "hermes-paperclip-adapter/server";
|
||||
import {
|
||||
agentConfigurationDoc as hermesAgentConfigurationDoc,
|
||||
models as hermesModels,
|
||||
} from "hermes-paperclip-adapter";
|
||||
import { processAdapter } from "./process/index.js";
|
||||
import { httpAdapter } from "./http/index.js";
|
||||
|
||||
|
|
@ -175,21 +163,10 @@ const piLocalAdapter: ServerAdapterModule = {
|
|||
agentConfigurationDoc: piAgentConfigurationDoc,
|
||||
};
|
||||
|
||||
const hermesLocalAdapter: ServerAdapterModule = {
|
||||
type: "hermes_local",
|
||||
execute: hermesExecute,
|
||||
testEnvironment: hermesTestEnvironment,
|
||||
sessionCodec: hermesSessionCodec,
|
||||
listSkills: hermesListSkills,
|
||||
syncSkills: hermesSyncSkills,
|
||||
models: hermesModels,
|
||||
supportsLocalAgentJwt: true,
|
||||
agentConfigurationDoc: hermesAgentConfigurationDoc,
|
||||
detectModel: () => detectModelFromHermes(),
|
||||
};
|
||||
const adaptersByType = new Map<string, ServerAdapterModule>();
|
||||
|
||||
const adaptersByType = new Map<string, ServerAdapterModule>(
|
||||
[
|
||||
function registerBuiltInAdapters() {
|
||||
for (const adapter of [
|
||||
claudeLocalAdapter,
|
||||
codexLocalAdapter,
|
||||
openCodeLocalAdapter,
|
||||
|
|
@ -197,21 +174,84 @@ const adaptersByType = new Map<string, ServerAdapterModule>(
|
|||
cursorLocalAdapter,
|
||||
geminiLocalAdapter,
|
||||
openclawGatewayAdapter,
|
||||
hermesLocalAdapter,
|
||||
processAdapter,
|
||||
httpAdapter,
|
||||
].map((a) => [a.type, a]),
|
||||
);
|
||||
]) {
|
||||
adaptersByType.set(adapter.type, adapter);
|
||||
}
|
||||
}
|
||||
|
||||
export function getServerAdapter(type: string): ServerAdapterModule {
|
||||
registerBuiltInAdapters();
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Load external adapter plugins (droid, hermes, etc.)
|
||||
//
|
||||
// External adapter packages export createServerAdapter() which returns a
|
||||
// ServerAdapterModule. The host fills in sessionManagement.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
import { buildExternalAdapters } from "./plugin-loader.js";
|
||||
import { getDisabledAdapterTypes } from "../services/adapter-plugin-store.js";
|
||||
|
||||
/** Cached sync wrapper — the store is a simple JSON file read, safe to call frequently. */
|
||||
function getDisabledAdapterTypesFromStore(): string[] {
|
||||
return getDisabledAdapterTypes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load external adapters from the plugin store and hardcoded sources.
|
||||
* Called once at module initialization. The promise is exported so that
|
||||
* callers (e.g. assertKnownAdapterType, app startup) can await completion
|
||||
* and avoid racing against the loading window.
|
||||
*/
|
||||
const externalAdaptersReady: Promise<void> = (async () => {
|
||||
try {
|
||||
const externalAdapters = await buildExternalAdapters();
|
||||
for (const externalAdapter of externalAdapters) {
|
||||
adaptersByType.set(
|
||||
externalAdapter.type,
|
||||
{
|
||||
...externalAdapter,
|
||||
sessionManagement: getAdapterSessionManagement(externalAdapter.type) ?? undefined,
|
||||
},
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[paperclip] Failed to load external adapters:", err);
|
||||
}
|
||||
})();
|
||||
|
||||
/**
|
||||
* Await this before validating adapter types to avoid race conditions
|
||||
* during server startup. External adapters are loaded asynchronously;
|
||||
* calling assertKnownAdapterType before this resolves will reject
|
||||
* valid external adapter types.
|
||||
*/
|
||||
export function waitForExternalAdapters(): Promise<void> {
|
||||
return externalAdaptersReady;
|
||||
}
|
||||
|
||||
export function registerServerAdapter(adapter: ServerAdapterModule): void {
|
||||
adaptersByType.set(adapter.type, adapter);
|
||||
}
|
||||
|
||||
export function unregisterServerAdapter(type: string): void {
|
||||
if (type === processAdapter.type || type === httpAdapter.type) return;
|
||||
adaptersByType.delete(type);
|
||||
}
|
||||
|
||||
export function requireServerAdapter(type: string): ServerAdapterModule {
|
||||
const adapter = adaptersByType.get(type);
|
||||
if (!adapter) {
|
||||
// Fall back to process adapter for unknown types
|
||||
return processAdapter;
|
||||
throw new Error(`Unknown adapter type: ${type}`);
|
||||
}
|
||||
return adapter;
|
||||
}
|
||||
|
||||
export function getServerAdapter(type: string): ServerAdapterModule {
|
||||
return adaptersByType.get(type) ?? processAdapter;
|
||||
}
|
||||
|
||||
export async function listAdapterModels(type: string): Promise<{ id: string; label: string }[]> {
|
||||
const adapter = adaptersByType.get(type);
|
||||
if (!adapter) return [];
|
||||
|
|
@ -226,13 +266,32 @@ export function listServerAdapters(): ServerAdapterModule[] {
|
|||
return Array.from(adaptersByType.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* List adapters excluding those that are disabled in settings.
|
||||
* Used for menus and agent creation flows — disabled adapters remain
|
||||
* functional for existing agents but hidden from selection.
|
||||
*/
|
||||
export function listEnabledServerAdapters(): ServerAdapterModule[] {
|
||||
const disabled = getDisabledAdapterTypesFromStore();
|
||||
const disabledSet = disabled.length > 0 ? new Set(disabled) : null;
|
||||
return disabledSet
|
||||
? Array.from(adaptersByType.values()).filter((a) => !disabledSet.has(a.type))
|
||||
: Array.from(adaptersByType.values());
|
||||
}
|
||||
|
||||
export async function detectAdapterModel(
|
||||
type: string,
|
||||
): Promise<{ model: string; provider: string; source: string } | null> {
|
||||
): Promise<{ model: string; provider: string; source: string; candidates?: string[] } | null> {
|
||||
const adapter = adaptersByType.get(type);
|
||||
if (!adapter?.detectModel) return null;
|
||||
const detected = await adapter.detectModel();
|
||||
return detected ? { model: detected.model, provider: detected.provider, source: detected.source } : null;
|
||||
if (!detected) return null;
|
||||
return {
|
||||
model: detected.model,
|
||||
provider: detected.provider,
|
||||
source: detected.source,
|
||||
...(detected.candidates?.length ? { candidates: detected.candidates } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
export function findServerAdapter(type: string): ServerAdapterModule | null {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue