forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter-selection.ts
More file actions
120 lines (110 loc) · 4.09 KB
/
Copy pathadapter-selection.ts
File metadata and controls
120 lines (110 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { AcpRuntimeAdapter } from "../adapters/acp.js";
import { HermesRuntimeAdapter } from "../adapters/hermes.js";
import { OpenClawRuntimeAdapter } from "../adapters/openclaw.js";
import { adapterCapabilitiesFor, type AdapterCapabilities, type ProductionAdapterId, type RuntimeAdapter } from "../adapters/interface.js";
import type { AdapterRegistry } from "./adapter-registry.js";
export const ADAPTER_ACTIVATION_ENV = {
acp: undefined,
"pi-mono": "OMI_AUTH_TOKEN",
hermes: "OMI_HERMES_ADAPTER_COMMAND",
openclaw: "OMI_OPENCLAW_ADAPTER_COMMAND",
} as const;
export type SelectableAdapterId = keyof typeof ADAPTER_ACTIVATION_ENV;
export interface AdapterProfile {
adapterId: ProductionAdapterId;
activationEnv?: string;
maxWorkers: number;
capabilities: AdapterCapabilities;
createAdapter: (options: { log: (message: string) => void }) => RuntimeAdapter;
}
export const ADAPTER_PROFILES: Record<ProductionAdapterId, AdapterProfile> = {
acp: {
adapterId: "acp",
activationEnv: ADAPTER_ACTIVATION_ENV.acp,
maxWorkers: 1,
capabilities: adapterCapabilitiesFor("acp"),
createAdapter: () => new AcpRuntimeAdapter(),
},
"pi-mono": {
adapterId: "pi-mono",
activationEnv: ADAPTER_ACTIVATION_ENV["pi-mono"],
maxWorkers: 1,
capabilities: adapterCapabilitiesFor("pi-mono"),
createAdapter: () => {
throw new Error("pi-mono adapter requires authenticated PiMonoAdapter construction");
},
},
hermes: {
adapterId: "hermes",
activationEnv: ADAPTER_ACTIVATION_ENV.hermes,
maxWorkers: 1,
capabilities: adapterCapabilitiesFor("hermes"),
createAdapter: ({ log }) => new HermesRuntimeAdapter({ log }),
},
openclaw: {
adapterId: "openclaw",
activationEnv: ADAPTER_ACTIVATION_ENV.openclaw,
maxWorkers: 1,
capabilities: adapterCapabilitiesFor("openclaw"),
createAdapter: ({ log }) => new OpenClawRuntimeAdapter({ log }),
},
};
export function adapterIdForHarnessMode(harnessMode: string | undefined): SelectableAdapterId {
if (harnessMode === undefined) return "acp";
switch (harnessMode) {
case "piMono":
case "pi-mono":
return "pi-mono";
case "hermes":
return "hermes";
case "openclaw":
case "openClaw":
return "openclaw";
case "acp":
return "acp";
default:
throw new Error(`Unknown harness mode: ${harnessMode}`);
}
}
export function adapterActivationEnv(adapterId: SelectableAdapterId): string | undefined {
return ADAPTER_PROFILES[adapterId].activationEnv;
}
export function adapterIsActivated(
adapterId: SelectableAdapterId,
env: NodeJS.ProcessEnv = process.env
): boolean {
const activationEnv = adapterActivationEnv(adapterId);
return activationEnv === undefined || Boolean(env[activationEnv]?.trim());
}
export function adapterProfile(adapterId: ProductionAdapterId): AdapterProfile {
return ADAPTER_PROFILES[adapterId];
}
export function adapterActivationError(adapterId: ProductionAdapterId): string | undefined {
const envName = adapterActivationEnv(adapterId);
if (!envName) return undefined;
const label = adapterId === "pi-mono" ? "pi-mono" : adapterId === "openclaw" ? "OpenClaw" : "Hermes";
if (adapterId === "hermes" || adapterId === "openclaw") {
return `${label} is not available. Make sure ${label} is installed first, then try again.`;
}
return "Omi AI is not available. Sign in and try again.";
}
export function ensureRegisteredAdapter(
registry: AdapterRegistry,
adapterId: ProductionAdapterId,
options: {
log: (message: string) => void;
maxWorkers?: number;
onCreate?: (adapter: RuntimeAdapter) => void;
}
): boolean {
if (!adapterIsActivated(adapterId)) return false;
if (registry.has(adapterId)) return true;
const profile = adapterProfile(adapterId);
registry.register(adapterId, () => {
const adapter = profile.createAdapter({ log: options.log });
options.onCreate?.(adapter);
return adapter;
}, options.maxWorkers ?? profile.maxWorkers);
options.log(`Adapter registered id=${adapterId} tools=${profile.capabilities.supportsTools} maxWorkers=${options.maxWorkers ?? profile.maxWorkers}`);
return true;
}