Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
import type {
RunningAgentsResult,
SessionRuntimeState,
WorkbenchMessage,
WorkbenchSession,
} from '@/context/ApiContext';
import { isBackendFailureNotice, isDetachedCompletionMessage, isTerminalAgentMessage, isTranscriptMessage } from '@/lib/chatMessageTypes';
import { specFor } from '@/lib/messageTypes';
import type { PetPose } from './PetAvatar';
/**
* What the desktop pet shows for its bound session. One pure derivation from
* the same reads the Workbench already makes; see
* docs/plans/2026-10-01-desktop-pet.md ("Pet state").
*/
export type PetState = 'needs_input' | 'blocked' | 'ready' | 'running' | 'idle';
export type PetStateInputs = {
session: Pick<WorkbenchSession, 'agent_status'> | null;
turn: Pick<SessionRuntimeState, 'foreground' | 'in_flight'> | null;
pendingVaultRequests: number;
unreadCount: number;
};
/** The options of a message's quick-reply group, or `[]` when it has none. */
export const quickReplyOptions = (message: WorkbenchMessage): string[] => {
const content = message.content as { quick_replies?: unknown } | null | undefined;
const options = content?.quick_replies;
return Array.isArray(options)
? options.filter((option): option is string => typeof option === 'string' && option.length > 0)
: [];
};
export const quickReplyChosen = (message: WorkbenchMessage): string | null => {
const chosen = (message.content as { quick_reply_chosen?: unknown } | null | undefined)?.quick_reply_chosen;
return typeof chosen === 'string' && chosen ? chosen : null;
};
const isAgentResult = (message: WorkbenchMessage): boolean =>
message.author === 'agent' && message.type === 'result';
/** A row the user sent the agent as input (the catalog's `inputAuthors`), not
* a display-only row such as a Show Page annotation. */
const isUserInput = (message: WorkbenchMessage): boolean =>
message.author === 'user' && specFor(message.type).inputAuthors.includes('user');
/**
* The backend failure notice that ended the latest exchange, or null: a notice
* after the last user input with no result since. A turn the agent could not run
* (Vibey with no model source, say) ends with one of these instead of a result.
*/
export const latestFailureNotice = (messages: WorkbenchMessage[]): WorkbenchMessage | null => {
for (let index = messages.length - 1; index >= 0; index -= 1) {
const message = messages[index];
if (isUserInput(message)) return null;
// A reply after the notice answered the exchange (a retry that went on to reply).
if (isAgentResult(message) && !isDetachedCompletionMessage(message)) return null;
// The foreground exchange ends at its terminal row, by the transcript's own rule:
// detached completions and replayed failures report work outside it and are skipped.
if (isTerminalAgentMessage(message)) return isBackendFailureNotice(message) ? message : null;
}
return null;
};
/** The latest agent result in the tail, or null. */
export const latestAgentResult = (messages: WorkbenchMessage[]): WorkbenchMessage | null => {
for (let index = messages.length - 1; index >= 0; index -= 1) {
if (isAgentResult(messages[index])) return messages[index];
}
return null;
};
/**
* The quick-reply group the panel offers now: the latest agent result's
* group, while unanswered. Quick replies are suggestions that end most agent
* replies, so they are answerable in the panel but never change the pet's
* state.
*
* The group is the latest agent result's, while unanswered. A group is answered by a chosen option or by any
* user input after it (a free-text reply, which then runs as a turn). An
* older unanswered group is not an open question either — once a newer result
* exists the agent has moved on — so it never counts (it stays clickable in
* the Workbench).
*/
export const openQuickReplies = (messages: WorkbenchMessage[]): { message: WorkbenchMessage; options: string[] } | null => {
for (let index = messages.length - 1; index >= 0; index -= 1) {
const message = messages[index];
if (isUserInput(message)) return null;
if (!isAgentResult(message)) continue;
if (quickReplyChosen(message)) return null;
const options = quickReplyOptions(message);
return options.length > 0 ? { message, options } : null;
}
return null;
};
const isRunning = (turn: PetStateInputs['turn']): boolean =>
turn?.foreground === 'running' || turn?.in_flight === true;
/**
* Priority: Needs input > Blocked > Ready > Running > Idle. Needs input means
* the agent cannot continue without the user: a pending vault request.
*/
export const derivePetState = (inputs: PetStateInputs): PetState => {
if (inputs.pendingVaultRequests > 0) return 'needs_input';
if (inputs.session?.agent_status === 'failed') return 'blocked';
if (!isRunning(inputs.turn) && inputs.unreadCount > 0) return 'ready';
if (isRunning(inputs.turn)) return 'running';
return 'idle';
};
/**
* Whether an agent is mid-turn in any conversation the user can see: an
* active instance of a foreground session, on any surface. Background runs,
* Runtime-owned sessions and processes without a session do not count, and
* an unreachable snapshot says nothing is known to be working.
*/
export const conversationAgentWorking = (result: RunningAgentsResult): boolean =>
result.ok === true
&& result.agents.some((agent) => agent.state === 'active' && agent.visibility === 'foreground');
/**
* What the avatar draws. The bound session's state wins; when it has nothing
* to say, an agent working in another conversation keeps the pet busy (and
* awake). Only the pose follows other sessions: the panel, badge and
* `data-state` describe the bound session alone.
*/
export const derivePetPose = (
state: PetState,
{ othersWorking, asleep }: { othersWorking: boolean; asleep: boolean },
): PetPose => {
if (state !== 'idle') return state;
if (othersWorking) return 'team';
return asleep ? 'sleeping' : 'idle';
};
/**
* The rows the panel shows as the latest exchange: the last user message, then
* every unread agent result (oldest first), or the last agent result when none
* is unread. `hasOlder` says the server has rows before the loaded tail.
* `unreadCount` is the inbox's session count: when it is ahead of the tail,
* unread rows may sit past what is loaded even if every loaded result is read.
*/
export const latestExchange = (
messages: WorkbenchMessage[],
hasOlder: boolean,
unreadCount = 0,
): {
user: WorkbenchMessage | null;
results: WorkbenchMessage[];
/** False when unread results may reach past the loaded tail, so the pet
* must not mark read and sends the user to the Workbench instead. */
unreadComplete: boolean;
/** True when the results answer an input before `user`: the latest input was
* answered by silence, so its Turn's earlier text became the result where it
* stood (`metadata.promoted_from`). The panel draws them before `user`,
* right after the input they answer. */
answersEarlierInput: boolean;
} => {
const transcript = messages.filter((message) => isTranscriptMessage(message));
let user: WorkbenchMessage | null = null;
for (let index = transcript.length - 1; index >= 0; index -= 1) {
if (transcript[index].author === 'user') {
user = transcript[index];
break;
}
}
const results = transcript.filter(isAgentResult);
const unread = results.filter((message) => message.read_at === null);
// Only a promoted result: any other result before the latest input keeps
// being shown after it, as before.
const answersEarlierInput = (shown: WorkbenchMessage[]) => {
const last = shown[shown.length - 1];
return Boolean(
user && last && last.metadata?.promoted_from && transcript.indexOf(last) < transcript.indexOf(user),
);
};
if (unread.length === 0) {
const last = results[results.length - 1];
const shown = last ? [last] : [];
// Inbox says something is unread, but the tail has no unread result: those
// replies sit past the loaded window. A fully-loaded session treats a
// leftover count as stale.
return {
user,
results: shown,
unreadComplete: unreadCount <= 0 || !hasOlder,
answersEarlierInput: answersEarlierInput(shown),
};
}
// Unread rows reach past the tail when the oldest loaded result is itself
// unread, or when the inbox count is ahead of the unread results we have.
const unreadComplete = !hasOlder || (results[0].read_at !== null && unread.length >= unreadCount);
return { user, results: unread, unreadComplete, answersEarlierInput: answersEarlierInput(unread) };
};