fix(api): require api server callbacks
This commit is contained in:
1 parent
529bb109db
commit
5155136937
9 files changed
+103
-59
No files matched your search
@@ -21,12 +21,10 @@ export { AgentApiServer };
|
||||
*/
|
||||
export function createApiServer(
|
||||
db: AppDatabase,
|
||||
onDomainChanged: () => void,
|
||||
onWakeAgent: (agentId: string, companyId: string, trigger: string) => string | null,
|
||||
callbacks?: Partial<ApiServerEventCallbacks>,
|
||||
callbacks: ApiServerEventCallbacks,
|
||||
allowedOrigins?: Iterable<string>,
|
||||
): AgentApiServer {
|
||||
const server = new AgentApiServer(db, onDomainChanged, onWakeAgent, callbacks, allowedOrigins);
|
||||
const server = new AgentApiServer(db, callbacks, allowedOrigins);
|
||||
registerAgentRoutes(server);
|
||||
registerAgentControlRoutes(server);
|
||||
registerAgentStandupRoute(server);
|
||||
|
||||
@@ -83,6 +83,35 @@ export interface ApiServerEventCallbacks {
|
||||
notify: (options: { title: string; body: string; urgency: "critical" | "informational"; navigation?: { section: SectionId; entityId?: string }; batchKey?: string }) => void;
|
||||
}
|
||||
|
||||
const REQUIRED_API_SERVER_CALLBACKS: readonly (keyof ApiServerEventCallbacks)[] = [
|
||||
"onDomainChanged",
|
||||
"onWakeAgent",
|
||||
"onTaskCreated",
|
||||
"onTaskStatusChanged",
|
||||
"onCommentPosted",
|
||||
"onApprovalCreated",
|
||||
"onApprovalResolved",
|
||||
"createDocument",
|
||||
"onAutoAssignTask",
|
||||
"onHumanMessage",
|
||||
"injectRuntimeMessage",
|
||||
"emitEvent",
|
||||
"notify",
|
||||
];
|
||||
|
||||
function assertCompleteApiServerCallbacks(
|
||||
callbacks: Partial<ApiServerEventCallbacks> | null | undefined,
|
||||
): asserts callbacks is ApiServerEventCallbacks {
|
||||
if (!callbacks) {
|
||||
throw new Error("Missing AgentApiServer callbacks.");
|
||||
}
|
||||
for (const name of REQUIRED_API_SERVER_CALLBACKS) {
|
||||
if (typeof callbacks[name] !== "function") {
|
||||
throw new Error(`Missing AgentApiServer callback: ${name}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Token-bucket rate limiter scoped per (companyId, agentId).
|
||||
*
|
||||
@@ -139,32 +168,16 @@ export class AgentApiServer {
|
||||
|
||||
constructor(
|
||||
db: AppDatabase,
|
||||
onDomainChanged: () => void,
|
||||
onWakeAgent: (agentId: string, companyId: string, trigger: string) => string | null,
|
||||
callbacks?: Partial<ApiServerEventCallbacks>,
|
||||
callbacks: ApiServerEventCallbacks,
|
||||
allowedOrigins: Iterable<string> = ["app://-"],
|
||||
) {
|
||||
assertCompleteApiServerCallbacks(callbacks);
|
||||
this._db = db;
|
||||
this._onDomainChangedCb = onDomainChanged;
|
||||
this._onWakeAgentCb = onWakeAgent;
|
||||
this._emitEventCb = callbacks?.emitEvent ?? (() => {});
|
||||
this._onDomainChangedCb = callbacks.onDomainChanged;
|
||||
this._onWakeAgentCb = callbacks.onWakeAgent;
|
||||
this._emitEventCb = callbacks.emitEvent;
|
||||
this._allowedOrigins = new Set(Array.from(allowedOrigins).filter(Boolean));
|
||||
this._callbacks = {
|
||||
onDomainChanged,
|
||||
onWakeAgent,
|
||||
onTaskCreated: callbacks?.onTaskCreated ?? (() => {}),
|
||||
onTaskStatusChanged: callbacks?.onTaskStatusChanged ?? (() => {}),
|
||||
onCommentPosted: callbacks?.onCommentPosted ?? (() => {}),
|
||||
onApprovalCreated: callbacks?.onApprovalCreated ?? (() => {}),
|
||||
onApprovalResolved: callbacks?.onApprovalResolved ?? (() => {}),
|
||||
createDocument: callbacks?.createDocument
|
||||
?? (() => { throw new Error("createDocument callback is not wired."); }),
|
||||
onAutoAssignTask: callbacks?.onAutoAssignTask ?? (() => null),
|
||||
onHumanMessage: callbacks?.onHumanMessage ?? (() => {}),
|
||||
injectRuntimeMessage: callbacks?.injectRuntimeMessage ?? (() => false),
|
||||
emitEvent: callbacks?.emitEvent ?? (() => {}),
|
||||
notify: callbacks?.notify ?? (() => {}),
|
||||
};
|
||||
this._callbacks = { ...callbacks };
|
||||
}
|
||||
|
||||
setBrowserActionHandler(handler: (actionId: string, companyId: string) => void) {
|
||||
|
||||
@@ -1,17 +1,13 @@
|
||||
import log from "electron-log/main.js";
|
||||
import type { AgentApiServer } from "../api-server";
|
||||
import type { AgentApiServer, ApiServerEventCallbacks } from "../api-server";
|
||||
import type { AppDatabase } from "../database";
|
||||
import type { AgentMessageRecord, DesktopEvent, SectionId } from "@shared/types";
|
||||
import type { DesktopEvent } from "@shared/types";
|
||||
import type { NotificationOptions } from "../notification-service";
|
||||
import type { createServiceGraph } from "../service-graph";
|
||||
import type { createThreadIntegration } from "../thread-integration";
|
||||
import type { createCheckpointEngine } from "../checkpoint-engine";
|
||||
import type { AutomationEventContext } from "../automation/types";
|
||||
import {
|
||||
createDocumentWithLifecycle,
|
||||
type DocumentLifecycleInput,
|
||||
type DocumentLifecycleOutcome,
|
||||
} from "../document-lifecycle";
|
||||
import { createDocumentWithLifecycle } from "../document-lifecycle";
|
||||
import type { DocumentLifecycleProcessor } from "../document-lifecycle-processor";
|
||||
|
||||
type ServiceGraph = ReturnType<typeof createServiceGraph>;
|
||||
@@ -19,21 +15,7 @@ type ThreadIntegration = ReturnType<typeof createThreadIntegration>;
|
||||
type CheckpointEngine = ReturnType<typeof createCheckpointEngine>;
|
||||
|
||||
export interface CreateApiServerFn {
|
||||
(db: AppDatabase, callbacks: {
|
||||
onDomainChanged: () => void;
|
||||
onWakeAgent: (agentId: string, companyId: string, trigger: string) => string | null;
|
||||
onTaskCreated: (taskId: string, companyId: string) => void;
|
||||
onTaskStatusChanged: (taskId: string, companyId: string, prev: string, next: string) => void;
|
||||
onCommentPosted: (taskId: string, companyId: string, authorAgentId: string | null) => void;
|
||||
onApprovalCreated: (approvalId: string, companyId: string, taskId: string | null, approvalType: string, summary: string) => void;
|
||||
onApprovalResolved: (approvalId: string, companyId: string, decision: string) => void;
|
||||
createDocument: (input: DocumentLifecycleInput) => DocumentLifecycleOutcome;
|
||||
onAutoAssignTask: (taskId: string, companyId: string) => string | null;
|
||||
onHumanMessage: (companyId: string, toAgentId?: string | null, threadId?: string | null, waitStateId?: string | null) => void;
|
||||
injectRuntimeMessage: (recipientAgentId: string, companyId: string, message: AgentMessageRecord) => boolean;
|
||||
emitEvent: (event: DesktopEvent) => void;
|
||||
notify: (options: { title: string; body: string; urgency: "critical" | "informational"; navigation?: { section: SectionId; entityId?: string }; batchKey?: string }) => void;
|
||||
}, allowedOrigins: string[]): AgentApiServer;
|
||||
(db: AppDatabase, callbacks: ApiServerEventCallbacks, allowedOrigins: string[]): AgentApiServer;
|
||||
}
|
||||
|
||||
export interface WireApiServerDeps {
|
||||
|
||||
@@ -69,13 +69,7 @@ export function createMainRuntime({
|
||||
devServerUrl,
|
||||
workerPath,
|
||||
createApiServer: (db, callbacks, allowedOrigins) =>
|
||||
createApiServer(
|
||||
db,
|
||||
support.publishDomainChanged,
|
||||
(agentId, companyId, trigger) => callbacks.onWakeAgent(agentId, companyId, trigger),
|
||||
callbacks,
|
||||
allowedOrigins,
|
||||
),
|
||||
createApiServer(db, callbacks, allowedOrigins),
|
||||
createRunToken: createAgentToken,
|
||||
seedDefaultAutomationRules: support.seedDefaultAutomationRules,
|
||||
isQuittingRef: {
|
||||
|
||||
Reference in new issue
Block a user