127 lines
3.9 KiB
TypeScript
127 lines
3.9 KiB
TypeScript
import { HermesAdapter, HermesError, Json } from './hermes.adapter';
|
|
/** Context MUST be constructed from verified backend identity, never request bodies. */
|
|
export interface RuntimePrincipal {
|
|
subject: string;
|
|
profiles: readonly string[];
|
|
}
|
|
export type RuntimePermission =
|
|
| 'runtime.read'
|
|
| 'cron.pause'
|
|
| 'cron.resume'
|
|
| 'chat'
|
|
| 'control'
|
|
| 'event.dispatch';
|
|
export interface RuntimeAuthorizer {
|
|
authorize(
|
|
principal: RuntimePrincipal,
|
|
permission: RuntimePermission,
|
|
profile?: string,
|
|
): Promise<boolean>;
|
|
}
|
|
/** No controller or socket is registered: only the backend gatekeeper can call this seam. */
|
|
export class HermesIntegrationService {
|
|
constructor(
|
|
private readonly adapter: HermesAdapter,
|
|
private readonly policy: RuntimeAuthorizer,
|
|
private readonly resumeEnabled = false,
|
|
) {}
|
|
capabilities() {
|
|
return {
|
|
inventory: true,
|
|
cronPause: true,
|
|
cronResume: this.resumeEnabled,
|
|
chat: false,
|
|
control: false,
|
|
eventDispatch: false,
|
|
} as const;
|
|
}
|
|
private async check(
|
|
principal: RuntimePrincipal,
|
|
permission: RuntimePermission,
|
|
profile?: string,
|
|
): Promise<void> {
|
|
if (
|
|
!principal?.subject ||
|
|
!Array.isArray(principal.profiles) ||
|
|
(profile && !principal.profiles.includes(profile))
|
|
)
|
|
throw new HermesError('FORBIDDEN');
|
|
try {
|
|
if (
|
|
(await this.policy.authorize(principal, permission, profile)) !== true
|
|
)
|
|
throw new HermesError('FORBIDDEN');
|
|
} catch {
|
|
throw new HermesError('FORBIDDEN');
|
|
}
|
|
}
|
|
async health(p: RuntimePrincipal) {
|
|
await this.check(p, 'runtime.read');
|
|
return this.adapter.health();
|
|
}
|
|
async status(p: RuntimePrincipal, profile: string) {
|
|
await this.check(p, 'runtime.read', profile);
|
|
return this.adapter.status(profile);
|
|
}
|
|
async profiles(p: RuntimePrincipal) {
|
|
await this.check(p, 'runtime.read');
|
|
return (await this.adapter.profiles()).filter(
|
|
(r) => typeof r.name === 'string' && p.profiles.includes(r.name),
|
|
);
|
|
}
|
|
async modelInfo(p: RuntimePrincipal, profile: string) {
|
|
await this.check(p, 'runtime.read', profile);
|
|
return this.adapter.modelInfo(profile);
|
|
}
|
|
async modelOptions(p: RuntimePrincipal, profile: string) {
|
|
await this.check(p, 'runtime.read', profile);
|
|
return this.adapter.modelOptions(profile);
|
|
}
|
|
async usage(p: RuntimePrincipal, profile: string, days = 30) {
|
|
await this.check(p, 'runtime.read', profile);
|
|
return this.adapter.usage(profile, days);
|
|
}
|
|
async sessions(p: RuntimePrincipal, profile: string, limit = 20, offset = 0) {
|
|
await this.check(p, 'runtime.read', profile);
|
|
return this.adapter.sessions(profile, limit, offset);
|
|
}
|
|
async crons(p: RuntimePrincipal, profile: string) {
|
|
await this.check(p, 'runtime.read', profile);
|
|
return this.adapter.crons(profile);
|
|
}
|
|
async pauseCron(p: RuntimePrincipal, profile: string, id: string) {
|
|
await this.check(p, 'cron.pause', profile);
|
|
return this.adapter.pauseCron(profile, id);
|
|
}
|
|
async resumeCron(p: RuntimePrincipal, profile: string, id: string) {
|
|
await this.check(p, 'cron.resume', profile);
|
|
if (!this.resumeEnabled) throw new HermesError('FORBIDDEN');
|
|
return this.adapter.resumeCron(profile, id);
|
|
}
|
|
async chat(
|
|
p: RuntimePrincipal,
|
|
profile: string,
|
|
_text: string,
|
|
): Promise<never> {
|
|
await this.check(p, 'chat', profile);
|
|
throw new HermesError('UNSUPPORTED');
|
|
}
|
|
async control(
|
|
p: RuntimePrincipal,
|
|
profile: string,
|
|
_command: 'interrupt',
|
|
): Promise<never> {
|
|
await this.check(p, 'control', profile);
|
|
throw new HermesError('UNSUPPORTED');
|
|
}
|
|
/** Disabled independently of policy: no unreviewed event may become an agent prompt. */
|
|
async dispatchEvent(
|
|
p: RuntimePrincipal,
|
|
profile: string,
|
|
_event: Json,
|
|
): Promise<never> {
|
|
await this.check(p, 'event.dispatch', profile);
|
|
throw new HermesError('FORBIDDEN');
|
|
}
|
|
}
|