@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
201 lines • 9.93 kB
TypeScript
import type { ToolCallOutcome } from '../dangling-tool-calls.js';
import type { GenuiCallRow, RecordGenuiCallInput } from '../genui-telemetry.js';
import type { AgentRunStatus, AgentStore, AppendMessageInput, CostSource, CreateThreadInput, MessageFeedback, RecordRunEndInput, RecordRunStartInput, RecordToolCallInput, RecordUsageInput, StoredMessage, ThreadDetail, ThreadSummary, ToolCallStatus, ToolResult, UpdateThreadInput, UpdateToolCallInput } from '../index.js';
import type { ActionProposalOutcomeLease, ActionProposalOutcomeStore } from '../spi/action-proposal-outcome-store.js';
import { type UsageTotals } from '../spi/agent-store.js';
import { type ToolCallApprovalState } from '../spi/approval-policy.js';
import type { ChatQueueStore, EnqueueMessageInput, QueuedMessage, QueuedMessagePatch, QueuePause } from '../spi/chat-queue.js';
import type { AgentUiComponent } from '../stream-events.js';
import { InMemoryActionProposalStore } from './in-memory-action-proposal-store.js';
/** A recorded usage row exposed to the governance read-model (input/output split + thread/day). */
export interface GovernanceUsageRow {
actorRef: string;
threadId: string;
runId?: string;
modelId: string;
purpose?: string;
inputTokens: number;
outputTokens: number;
/** Subset of `inputTokens` written to the prompt cache this turn; undefined when not reported. */
cacheWriteTokens?: number;
/** Subset of `inputTokens` served from the prompt cache this turn; undefined when not reported. */
cacheReadTokens?: number;
/** The turn's cost as recorded (reported, or estimated at write time); undefined → estimate from pricing. */
costUsd?: number;
/** Where `costUsd` came from; undefined with a `costUsd` → a row from before estimates were stored (reported). */
costSource?: CostSource;
day: string;
createdAt: string;
}
/** A recorded tool call exposed to the governance read-model (thread resolved, with timestamp). */
export interface GovernanceToolCallRow {
toolCallId: string;
toolName: string;
toolType: 'read' | 'action';
status: ToolCallStatus;
threadId: string;
runId?: string;
input: unknown;
output?: unknown;
error?: string;
executionMs?: number;
createdAt: string;
}
/** A recorded run exposed to the governance read-model (full lifecycle + rollups). */
export interface GovernanceRunRow {
runId: string;
threadId: string;
actorRef: string;
tenantRef?: string;
agentName?: string;
/** The run that delegated this one; unset for a turn a person started. */
parentRunId?: string;
status: AgentRunStatus;
startedAt: string;
finishedAt?: string;
stepCount: number;
inputTokens: number;
outputTokens: number;
costUsd?: number;
error?: string;
ttftMs?: number;
errorCode?: string;
modelId?: string;
durable: boolean;
}
/** A recorded message exposed to the governance read-model (for run-detail assembly). */
export interface GovernanceMessageRow {
id: string;
threadId: string;
runId?: string;
role: string;
content: string;
createdAt: string;
}
/** Thread metadata exposed to the governance read-model (title/actor/count/last activity). */
export interface GovernanceThreadRow {
threadId: string;
title: string;
actorRef: string;
messageCount: number;
updatedAt: string;
}
/** A fully in-memory `AgentStore` for tests and the offline demo. */
export declare class InMemoryAgentStore extends InMemoryActionProposalStore implements AgentStore, ChatQueueStore {
private readonly threads;
/** Each thread's waiting messages, in run order. */
private readonly queues;
private readonly pauses;
/** Keyed by message and model-supplied id: two responses can be handed the same id (see `toolCallKey`). */
private readonly toolCalls;
private readonly usage;
private readonly genuiCalls;
private readonly runs;
private now;
createThread(input: CreateThreadInput): Promise<ThreadSummary>;
getThread(threadId: string): Promise<ThreadDetail | null>;
getThreadActionProposalScope(threadId: string): Promise<{
threadId: string;
actorRef: string;
tenantRef: string | null;
} | null>;
getThreadActorRef(threadId: string): Promise<string | null>;
listThreads(actorRef: string, limit?: number): Promise<ThreadSummary[]>;
promoteThread(threadId: string): Promise<void>;
softDeleteThread(threadId: string): Promise<void>;
forkThread(threadId: string, fromMessageId: string): Promise<ThreadSummary>;
setTitle(threadId: string, title: string): Promise<void>;
updateThread(threadId: string, patch: UpdateThreadInput): Promise<void>;
defaultAgentForThread(threadId: string): Promise<string | null>;
personaForThread(threadId: string): Promise<string | null>;
clearActiveStream(threadId: string, runId: string): Promise<void>;
activeRunForThread(threadId: string): Promise<string | null>;
threadHeldByRun(runId: string): Promise<string | null>;
claimActiveStream(threadId: string, runId: string, options?: {
replacing?: string;
}): Promise<boolean>;
releaseActiveStream(threadId: string, runId: string): Promise<boolean>;
enqueueMessage(input: EnqueueMessageInput): Promise<QueuedMessage>;
listQueue(threadId: string): Promise<QueuedMessage[]>;
getQueuedMessage(id: string): Promise<QueuedMessage | null>;
updateQueuedMessage(id: string, patch: QueuedMessagePatch): Promise<QueuedMessage | null>;
moveQueuedMessage(id: string, index: number): Promise<boolean>;
removeQueuedMessage(id: string): Promise<boolean>;
clearQueue(threadId: string): Promise<number>;
queuePause(threadId: string): Promise<QueuePause | null>;
setQueuePause(threadId: string, pause: QueuePause | null): Promise<void>;
private findQueued;
setActiveStream(threadId: string, runId: string | null): Promise<void>;
admitActionProposalOutcome(command: ActionProposalOutcomeLease): ReturnType<ActionProposalOutcomeStore['admitActionProposalOutcome']>;
appendMessage(input: AppendMessageInput): Promise<StoredMessage>;
setMessageUi(messageId: string, ui: AgentUiComponent[]): Promise<void>;
setMessageToolResults(messageId: string, results: ToolResult[]): Promise<void>;
truncateFrom(threadId: string, messageId: string): Promise<void>;
/**
* Of `mediaIds`, the ones a surviving (or queued) message in one of this actor's threads still
* carries. Re-derived from the messages each call, so a media whose message was truncated away reads as
* unreferenced again.
*/
referencedMediaIds(actorRef: string, mediaIds: readonly string[]): Promise<string[]>;
threadOfMessage(messageId: string): Promise<string | null>;
setMessageFeedback(messageId: string, feedback: MessageFeedback | null): Promise<void>;
/**
* A provider only promises a call id is unique within one response (one assistant message), so a
* call is keyed by its message too — as `LucidAgentStore` keys its rows.
*/
private toolCallKey;
/**
* The newest call `runId` recorded under `toolCallId`, or without a run the newest overall — the
* same rule as `LucidAgentStore`. Insertion order is recording order.
*/
private findToolCall;
toolCallInput(toolCallId: string, runId?: string): Promise<unknown>;
getToolCallRunId(toolCallId: string): Promise<string | null>;
toolCallOutcomes(toolCallIds: string[]): Promise<ToolCallOutcome[]>;
failUnsettledToolCalls(runId: string, error: string): Promise<number>;
recordToolCall(input: RecordToolCallInput): Promise<void>;
updateToolCall(input: UpdateToolCallInput): Promise<void>;
rememberedApprovals(threadId: string): Promise<string[]>;
toolCallApproval(toolCallId: string, runId?: string): Promise<ToolCallApprovalState | null>;
/** A message with the approval record of every call on it that was put to a person. */
private withApprovals;
recordGenuiCalls(rows: readonly RecordGenuiCallInput[]): Promise<void>;
/** Generative-UI telemetry rows, oldest first (test helper; the governance feed). */
genuiCallRows(): GenuiCallRow[];
recordUsage(input: RecordUsageInput): Promise<void>;
getRunActorRef(runId: string): Promise<string | null>;
recordRunStart(input: RecordRunStartInput): Promise<void>;
recordRunEnd(input: RecordRunEndInput): Promise<void>;
usageBetween(actorRef: string, fromDay: string, toDay: string): Promise<UsageTotals>;
quotaToday(actorRef: string, day: string): Promise<{
usedTokens: number;
}>;
/** Test helper: read the recorded usage rows (modelId + purpose + token totals). */
usageRows(): {
actorRef: string;
tokens: number;
modelId: string;
purpose: string;
}[];
/** Governance read-model feed: recorded usage rows with the input/output split + thread/day. */
governanceUsage(): GovernanceUsageRow[];
/** Governance read-model feed: recorded tool calls with the resolved thread + timestamp. */
governanceToolCalls(): GovernanceToolCallRow[];
/** Governance read-model feed: recorded runs (full lifecycle + rollups). */
governanceRuns(): GovernanceRunRow[];
/** Governance read-model feed: recorded messages with their thread + run correlation. */
governanceMessages(): GovernanceMessageRow[];
/** Governance read-model feed: thread metadata (title/actor/message count/last activity). */
governanceThreads(): GovernanceThreadRow[];
private threadIdForMessage;
/** Test helper: read the recorded tool-call rows. */
toolCallRows(): {
toolCallId: string;
runId?: string;
toolName: string;
status: ToolCallStatus;
output?: unknown;
}[];
private toSummary;
}
//# sourceMappingURL=in-memory-store.d.ts.map