@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
83 lines • 4.09 kB
JavaScript
import { snapshotActionProposal } from './action-proposal-transitions.js';
import { turnChannel } from './genui/channels.js';
import { createNegotiatedUiCollector } from './negotiated-tool-ui.js';
export class ActionProposalExecutor {
options;
constructor(options) {
this.options = options;
}
async execute(proposal) {
let ui = createNegotiatedUiCollector(proposal.originToolCallId, {
actor: { id: proposal.actorRef },
threadId: proposal.threadId,
});
try {
const actor = await this.options.resolver.resolve({
actorRef: proposal.actorRef,
tenantRef: proposal.tenantRef,
});
if (!actor ||
actor.id !== proposal.actorRef ||
(actor.tenantRef ?? null) !== proposal.tenantRef)
throw new Error('Action proposal requester no longer exists or scope changed');
const deps = await this.options.resolveExecution(proposal, actor);
if (deps.registry.spec(proposal.toolName)?.kind !== 'action')
throw new Error('Action proposal tool no longer exists as an action');
const context = proposal.executionContext;
ui = createNegotiatedUiCollector(proposal.originToolCallId, {
actor,
threadId: proposal.threadId,
...(context?.agentName !== undefined ? { agentName: context.agentName } : {}),
...(context?.uiCapabilities !== undefined
? { uiCapabilities: context.uiCapabilities }
: {}),
channel: turnChannel(context?.pageContext),
}, deps.resolveUiCatalog);
const output = await deps.registry.invoke(proposal.toolName, snapshotActionProposal(Object.hasOwn(proposal, 'preparationInput') ? proposal.preparationInput : proposal.input), {
actor,
threadId: proposal.threadId,
runId: proposal.originRunId,
requestId: context?.requestId ?? proposal.originRunId,
toolCallId: proposal.originToolCallId,
idempotencyKey: proposal.idempotencyKey,
...(context?.agentName !== undefined ? { agentName: context.agentName } : {}),
...(deps.persona !== undefined ? { persona: deps.persona } : {}),
...(context?.pageContext !== undefined ? { pageContext: context.pageContext } : {}),
...(context?.uiCapabilities !== undefined
? { uiCapabilities: context.uiCapabilities }
: {}),
channel: turnChannel(context?.pageContext),
...(deps.host !== undefined ? { host: deps.host } : {}),
...(deps.onPresentationError !== undefined
? {
onPresentationError: (error, details) => deps.onPresentationError(error, {
...details,
toolCallId: proposal.originToolCallId,
runId: proposal.originRunId,
threadId: proposal.threadId,
}),
}
: {}),
emitUi: ui.emit,
}, deps.rolesPolicy, {
approvedInput: proposal.input,
...(deps.allowedTools !== undefined ? { allowedTools: deps.allowedTools } : {}),
});
return snapshotActionProposal({
status: 'succeeded',
...(output !== undefined ? { result: output } : {}),
ui: ui.components(),
...(ui.text() ? { text: ui.text() } : {}),
});
}
catch (error) {
return {
status: 'failed',
error: error instanceof Error ? error.message : String(error),
ui: ui.components(),
...(ui.text() ? { text: ui.text() } : {}),
};
}
}
}
//# sourceMappingURL=action-proposal-executor.js.map