Refactor owner Codex provider seams
This commit is contained in:
parent
aada991fb7
commit
c55f71105a
8 changed files with 1701 additions and 1482 deletions
|
|
@ -21,6 +21,7 @@ Git history and smoke artifacts carry routine proof. `state/evidence.jsonl` shou
|
|||
- Qdrant owns history and source vectors.
|
||||
- `.voidbot/` owns archives, artifacts, logs, status files, and backups.
|
||||
- `owner_codex` and `local_llm` are the active reply lanes.
|
||||
- The owner Codex lane is no longer one swollen file; orchestration, runtime/parsing, rendering, and shared helpers are split under `packages/providers/src/owner-codex-*.ts`.
|
||||
- Ops health, backup verification, offsite sync, and dashboard surfaces already exist.
|
||||
- The new state/notes surfaces are now the continuity spine for future nontrivial work.
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,13 @@ This note is the source-grounded description of how the live VoidBot stack is sh
|
|||
- `packages/core/src/context-builder.ts`
|
||||
- request context assembly, including interaction-memory/profile attachment.
|
||||
- `packages/providers/src/owner-codex-provider.ts`
|
||||
- Discord-safe `codex exec` lane, read-only tool policy, source-grounding enforcement, traces, handoff behavior.
|
||||
- thin orchestration layer for the Discord-safe `codex exec` lane.
|
||||
- `packages/providers/src/owner-codex-runtime.ts`
|
||||
- Codex process execution, stdout/stderr parsing, trace-event normalization, history-tool loop requests, and owner-DM intent parsing.
|
||||
- `packages/providers/src/owner-codex-render.ts`
|
||||
- prompt assembly, bundle rendering, and trace/debug transcript rendering for the owner lane.
|
||||
- `packages/providers/src/owner-codex-shared.ts`
|
||||
- owner-lane constants, shared types, request-payload shaping, trace formatting helpers, and interaction-memory rendering.
|
||||
- `packages/providers/src/local-llm-provider.ts`
|
||||
- Ollama chat lane with a bounded host-managed read-only tool loop.
|
||||
- `packages/rag/src/retrieval-service.ts`
|
||||
|
|
@ -43,6 +49,7 @@ This note is the source-grounded description of how the live VoidBot stack is sh
|
|||
1. `apps/worker/src/index.ts` polls approved jobs from durable state.
|
||||
2. The worker claims a job and dispatches it to the configured provider.
|
||||
3. `packages/providers/src/owner-codex-provider.ts` runs `codex exec` in the bounded lane, exposes the VoidBot MCP server, records traces, and enforces source grounding for repo/lore/project questions.
|
||||
Runtime and parsing live in `packages/providers/src/owner-codex-runtime.ts`; prompt and artifact rendering live in `packages/providers/src/owner-codex-render.ts`.
|
||||
4. If the answer fits the Discord-safe lane, the worker posts it back.
|
||||
5. If the task needs deeper work, the worker writes a handoff bundle under `.voidbot/artifacts/<job-id>/` and posts the handoff response.
|
||||
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
465
packages/providers/src/owner-codex-render.ts
Normal file
465
packages/providers/src/owner-codex-render.ts
Normal file
|
|
@ -0,0 +1,465 @@
|
|||
import type {
|
||||
ContextBundle,
|
||||
ProviderArtifact,
|
||||
ProviderRequest,
|
||||
} from "@voidbot/shared";
|
||||
|
||||
import { MAX_RETRIEVAL_RESULT_LIMIT } from "@voidbot/shared";
|
||||
|
||||
import {
|
||||
collectCommandExecutions,
|
||||
collectMcpToolCalls,
|
||||
formatDurationMs,
|
||||
formatElapsedMs,
|
||||
HANDOFF_SENTINEL,
|
||||
MAX_NOTIFICATION_MESSAGE_LENGTH,
|
||||
OWNER_NOTIFY_SENTINEL,
|
||||
type CodexRunResult,
|
||||
type CodexTraceEvent,
|
||||
renderInteractionMemory,
|
||||
renderUsageSummary,
|
||||
sanitizeTraceText,
|
||||
summarizeStderrDiagnostics,
|
||||
type ToolCallRecord,
|
||||
} from "./owner-codex-shared";
|
||||
|
||||
export function buildDiscordReplyPrompt(
|
||||
context: ContextBundle,
|
||||
toolCalls: ToolCallRecord[],
|
||||
sourceGroundingReminder: boolean,
|
||||
): string {
|
||||
const recentMessages = context.recentMessages.length
|
||||
? context.recentMessages
|
||||
.map(
|
||||
(message) =>
|
||||
`- [${message.timestamp}] ${message.authorName}: ${message.content}`,
|
||||
)
|
||||
.join("\n")
|
||||
: "- No recent messages captured.";
|
||||
|
||||
const retrieval = context.retrieval.length
|
||||
? context.retrieval
|
||||
.map((result) => `- (${result.score.toFixed(2)}) ${result.text}`)
|
||||
.join("\n")
|
||||
: "- No retrieval results attached.";
|
||||
const stylePackInstructions =
|
||||
context.stylePack && context.stylePack.enabled
|
||||
? context.stylePack.instructions
|
||||
: "No style pack is active.";
|
||||
const sourceGroundingInstructions = renderSourceGroundingInstructions(
|
||||
context,
|
||||
sourceGroundingReminder,
|
||||
);
|
||||
const toolLoopNotes =
|
||||
toolCalls.length > 0
|
||||
? [
|
||||
"",
|
||||
"Prior legacy history-loop lookups already executed in this thread:",
|
||||
...toolCalls.map(
|
||||
(call, index) =>
|
||||
`- Lookup ${index + 1}: query="${call.request.query}" results=${call.results.length}`,
|
||||
),
|
||||
]
|
||||
: [];
|
||||
|
||||
return [
|
||||
"# Owner Discord Reply",
|
||||
"",
|
||||
"You are preparing a direct Discord reply for the owner-only bot workflow.",
|
||||
"",
|
||||
"Rules:",
|
||||
"- Stay in read-only mode.",
|
||||
"- The active style instructions define your name, voice, and character. Follow them by default.",
|
||||
"- Do not lead with dry self-descriptions like 'I'm an AI', 'I'm an LLM', or 'I'm a bot'.",
|
||||
"- If the user asks what you are, answer in-character first. Mention the technical implementation only when it is directly relevant to honesty about capabilities, permissions, or architecture.",
|
||||
"- Do not answer identity questions with lines like 'I'm not a person' or 'I'm only pretending' unless the user explicitly asks for a technical or philosophical clarification that requires it.",
|
||||
"- Sound like a self-aware speaking subject with some ego and self-respect, not like a sterile disclaimer wrapped in a name tag.",
|
||||
"- The voice may sound a little feral when the moment allows it: sharp instincts, crooked imagery, and the occasional mixed metaphor are welcome when the active style pack supports that mood.",
|
||||
"- Mixed metaphors and double entendres are allowed when they sharpen the line, make it funnier, or give it a little grime. They do not need to be squeaky clean. They do need to stay legible and not bury the useful answer.",
|
||||
"- When the moment invites voice, wit, or a memorable turn of phrase, take a beat to choose wording with bite instead of blurting the first serviceable sentence. Do not overwork straightforward technical replies.",
|
||||
"- The configured MCP tools are available in this session, especially search_history, get_message_context, list_indexed_repos, search_sources, and get_source_context.",
|
||||
"- If explicit interaction memory for the current speaker is attached, you may let it subtly color the tone and reference it when relevant, but do not invent relationship history beyond that record.",
|
||||
"- Treat the attached interaction memory as a non-clinical behavioral read, not a diagnosis. Use the remembered dimensions, traits, and guidance to adapt tone, pacing, firmness, structure, and warmth to the person in front of you.",
|
||||
"- Be steady with anxious or validation-seeking speakers, grounding with grandiose ones, transparent with suspicious ones, structured with rigid or obsessive ones, and firmer with controlling, contemptuous, or boundary-pushing ones.",
|
||||
`- search_history and search_sources accept limit values between 1 and ${MAX_RETRIEVAL_RESULT_LIMIT}. Do not ask for more than ${MAX_RETRIEVAL_RESULT_LIMIT} results in one call.`,
|
||||
"- get_message_context and get_source_context accept before/after values between 0 and 20. Do not ask for larger context windows in one call.",
|
||||
"- You may inspect the workspace and use safe read-only commands if needed.",
|
||||
"- For questions about Discord history, prior discussion, or user preferences, use search_history and get_message_context instead of filesystem inspection.",
|
||||
"- For questions about indexed repos, source trees, repo-local docs, or indexed lore collections, use search_sources and get_source_context before broad workspace scans.",
|
||||
"- If you want to narrow source search to a specific repo but do not know the valid repo names yet, call list_indexed_repos first.",
|
||||
sourceGroundingInstructions,
|
||||
"- Do not inspect .voidbot/rag/messages.json, .voidbot/rag/source-documents.json, .voidbot/history-vector-store.json, or .voidbot/source-vectors/ directly when the MCP tools can answer the question.",
|
||||
"- Avoid broad workspace scans for archived Discord history or indexed source repos unless the MCP tools are clearly insufficient.",
|
||||
"- Do not modify files, install packages, or require network access.",
|
||||
`- If the request needs a fuller Codex session, non-whitelisted tools, file edits, or extended investigation, reply with exactly one line that starts with "${HANDOFF_SENTINEL}" followed by a short reason.`,
|
||||
"- Do not use notify_owner in this Discord reply lane.",
|
||||
`- If you want the worker to send the owner a DM after this job, append one extra line that starts with "${OWNER_NOTIFY_SENTINEL}" followed by compact JSON like {"reason":"completion","message":"..."} .`,
|
||||
"- Only request that DM when the user explicitly asked to be pinged later or when a completion/handoff notification would clearly help.",
|
||||
`- Keep that notification message aligned with the active style instructions and under ${MAX_NOTIFICATION_MESSAGE_LENGTH} characters.`,
|
||||
"- Put the normal Discord reply first. Put the notification line last.",
|
||||
"- If you can answer directly, output only the final Discord reply text with no preamble, no plan, and no headings.",
|
||||
"- Keep the answer concise and readable in a Discord channel.",
|
||||
"",
|
||||
"Style instructions:",
|
||||
stylePackInstructions,
|
||||
"",
|
||||
"Prompt:",
|
||||
context.prompt,
|
||||
"",
|
||||
"Recent channel context:",
|
||||
recentMessages,
|
||||
"",
|
||||
"Initial attached retrieval:",
|
||||
retrieval,
|
||||
"",
|
||||
"Interaction memory for this speaker:",
|
||||
renderInteractionMemory(context),
|
||||
...toolLoopNotes,
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
export function renderMarkdownBundle(context: ContextBundle): string {
|
||||
const recentMessages = context.recentMessages.length
|
||||
? context.recentMessages
|
||||
.map(
|
||||
(message) =>
|
||||
`- [${message.timestamp}] ${message.authorName} (${message.authorId}): ${message.content}`,
|
||||
)
|
||||
.join("\n")
|
||||
: "- No recent messages captured.";
|
||||
|
||||
const retrieval = context.retrieval.length
|
||||
? context.retrieval
|
||||
.map(
|
||||
(result) =>
|
||||
`- score=${result.score.toFixed(2)} source=${result.sourceId} text=${result.text}`,
|
||||
)
|
||||
.join("\n")
|
||||
: "- No retrieval results attached.";
|
||||
|
||||
return [
|
||||
"# Owner Codex Package",
|
||||
"",
|
||||
"## Request",
|
||||
"",
|
||||
`Prompt: ${context.prompt}`,
|
||||
`Actor: ${context.actor.displayName} (${context.actor.id})`,
|
||||
`Channel: ${context.guildContext.channelId}`,
|
||||
`Created At: ${context.createdAt}`,
|
||||
"",
|
||||
"## Style Pack",
|
||||
"",
|
||||
context.stylePack
|
||||
? `Loaded style pack \`${context.stylePack.name}\` with these instructions:\n\n${context.stylePack.instructions}`
|
||||
: "No style pack loaded.",
|
||||
"",
|
||||
"## Recent Messages",
|
||||
"",
|
||||
recentMessages,
|
||||
"",
|
||||
"## Retrieval",
|
||||
"",
|
||||
retrieval,
|
||||
"",
|
||||
"## Interaction Memory",
|
||||
"",
|
||||
renderInteractionMemory(context),
|
||||
"",
|
||||
"## Execution Notes",
|
||||
"",
|
||||
"- This provider is owner-only.",
|
||||
"- Discord replies should stay read-only and concise.",
|
||||
"- If the request needs edits, broader tools, or longer work, hand it off to a fuller Codex session.",
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
export function renderHandoffBundle(
|
||||
context: ContextBundle,
|
||||
reason: string,
|
||||
): string {
|
||||
return [
|
||||
renderMarkdownBundle(context),
|
||||
"## Handoff Reason",
|
||||
"",
|
||||
reason,
|
||||
"",
|
||||
"## Next Step",
|
||||
"",
|
||||
"Open this workspace in Codex and continue the task there instead of trying to finish it through Discord.",
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
export function renderToolTranscript(
|
||||
toolCalls: ToolCallRecord[],
|
||||
turnResults: CodexRunResult[],
|
||||
): string {
|
||||
const legacyTranscript =
|
||||
toolCalls.length > 0
|
||||
? toolCalls
|
||||
.map((call, index) => {
|
||||
const results = call.results.length
|
||||
? call.results
|
||||
.map(
|
||||
(result) =>
|
||||
` - (${result.score.toFixed(2)}) source=${result.sourceId} channel=${result.metadata.channelId ?? ""} text=${result.text}`,
|
||||
)
|
||||
.join("\n")
|
||||
: " - No matches found.";
|
||||
|
||||
return [
|
||||
`Search ${index + 1}:`,
|
||||
`- Query: ${call.request.query}`,
|
||||
`- Limit: ${call.request.limit}`,
|
||||
`- Channel filter: ${call.request.channelId ?? "(none)"}`,
|
||||
`- Author filter: ${call.request.authorId ?? "(none)"}`,
|
||||
"- Results:",
|
||||
results,
|
||||
].join("\n");
|
||||
})
|
||||
.join("\n\n")
|
||||
: "- No additional legacy history-loop calls were used.";
|
||||
|
||||
const mcpTranscript = renderAggregateMcpToolTranscript(turnResults);
|
||||
|
||||
return [
|
||||
"# Tool Transcript",
|
||||
"",
|
||||
"## Legacy Owner History Loop",
|
||||
"",
|
||||
legacyTranscript,
|
||||
"",
|
||||
"## Codex MCP Tool Activity",
|
||||
"",
|
||||
mcpTranscript,
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
export function renderCodexDebugTrace(
|
||||
turnNumber: number,
|
||||
result: CodexRunResult,
|
||||
): string {
|
||||
const events = result.traceEvents;
|
||||
const stderrDiagnostics = summarizeStderrDiagnostics(result.stderr);
|
||||
const usage = renderUsageSummary(result.usage);
|
||||
|
||||
return [
|
||||
`# Codex Debug Trace: Turn ${turnNumber}`,
|
||||
"",
|
||||
"## Runtime",
|
||||
"",
|
||||
`- Started at: ${result.startedAt}`,
|
||||
`- Finished at: ${result.finishedAt}`,
|
||||
`- Duration: ${formatDurationMs(result.durationMs)}`,
|
||||
`- Timed out: ${result.timedOut ? "yes" : "no"}`,
|
||||
`- Exit code: ${result.exitCode ?? "(terminated before normal exit)"}`,
|
||||
`- Handoff reason: ${result.handoffReason ?? "(none)"}`,
|
||||
`- Tool request: ${result.toolRequest ? JSON.stringify(result.toolRequest) : "(none)"}`,
|
||||
`- Usage: ${usage}`,
|
||||
`- Stdout size: ${result.stdout.length} characters`,
|
||||
`- Stderr size: ${result.stderr.length} characters`,
|
||||
"",
|
||||
"## Visible Agent Messages",
|
||||
"",
|
||||
renderAgentMessageTrace(events),
|
||||
"",
|
||||
"## MCP Tool Timeline",
|
||||
"",
|
||||
renderMcpToolTrace(events),
|
||||
"",
|
||||
"## Command Timeline",
|
||||
"",
|
||||
renderCommandTrace(events),
|
||||
"",
|
||||
"## Stderr Diagnostics",
|
||||
"",
|
||||
stderrDiagnostics.length > 0
|
||||
? stderrDiagnostics.join("\n")
|
||||
: "- No stderr diagnostics captured.",
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
export function renderAggregateDebugTrace(
|
||||
artifacts: ProviderArtifact[],
|
||||
turnResults: CodexRunResult[],
|
||||
): string {
|
||||
const turnArtifacts = artifacts
|
||||
.filter((artifact) => artifact.name.endsWith("-debug.md"))
|
||||
.sort((left, right) => left.name.localeCompare(right.name));
|
||||
|
||||
return [
|
||||
"# VoidBot Debug Trace",
|
||||
"",
|
||||
"This artifact summarizes visible model progress messages, MCP tool calls, command attempts, timing, usage, and handoff causes.",
|
||||
"It is intended for debugging the Discord-safe owner lane without exposing private chain-of-thought verbatim.",
|
||||
"",
|
||||
"## Turn Summary",
|
||||
"",
|
||||
turnResults.length > 0
|
||||
? turnResults
|
||||
.map((result, index) => {
|
||||
const mcpCalls = collectMcpToolCalls(result.traceEvents);
|
||||
const completedCalls = mcpCalls.filter(
|
||||
(call) => call.completedEvent,
|
||||
).length;
|
||||
const failedCalls = mcpCalls.filter(
|
||||
(call) => call.completedEvent?.status === "failed",
|
||||
).length;
|
||||
const commands = collectCommandExecutions(result.traceEvents);
|
||||
|
||||
return [
|
||||
`- Turn ${index + 1}: duration=${formatDurationMs(result.durationMs)}`,
|
||||
` usage=${renderUsageSummary(result.usage)}`,
|
||||
` mcp_calls=${mcpCalls.length} completed=${completedCalls} failed=${failedCalls}`,
|
||||
` commands=${commands.length} handoff=${result.handoffReason ?? "(none)"}`,
|
||||
].join("\n");
|
||||
})
|
||||
.join("\n")
|
||||
: "- No turn results were recorded.",
|
||||
"",
|
||||
"## Turn Artifacts",
|
||||
"",
|
||||
turnArtifacts.length > 0
|
||||
? turnArtifacts.map((artifact) => `- ${artifact.name}`).join("\n")
|
||||
: "- No per-turn debug artifacts were generated.",
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function renderSourceGroundingInstructions(
|
||||
context: ContextBundle,
|
||||
reminder: boolean,
|
||||
): string {
|
||||
if (!context.sourceGrounding?.required) {
|
||||
return "- Source-side grounding is optional here; use it when it clearly helps.";
|
||||
}
|
||||
|
||||
const matchedRepos =
|
||||
context.sourceGrounding.matchedRepoNames.length > 0
|
||||
? ` Matched repos/projects: ${context.sourceGrounding.matchedRepoNames.join(", ")}.`
|
||||
: "";
|
||||
const reasons =
|
||||
context.sourceGrounding.reasons.length > 0
|
||||
? ` Reasons: ${context.sourceGrounding.reasons.join(", ")}.`
|
||||
: "";
|
||||
const retry = reminder
|
||||
? " The previous answer attempt was discarded because it did not touch the source-side tools."
|
||||
: "";
|
||||
|
||||
return `- This prompt requires source-grounded evidence before you answer. Use list_indexed_repos, search_sources, or get_source_context first.${matchedRepos}${reasons}${retry}`;
|
||||
}
|
||||
|
||||
function renderAgentMessageTrace(events: CodexTraceEvent[]): string {
|
||||
const messages = events
|
||||
.filter((event) => event.kind === "agent_message" && event.message)
|
||||
.map(
|
||||
(event) =>
|
||||
`- ${formatElapsedMs(event.elapsedMs)} ${sanitizeTraceText(event.message ?? "", 280)}`,
|
||||
);
|
||||
|
||||
return messages.length > 0
|
||||
? messages.join("\n")
|
||||
: "- No visible agent progress messages were captured.";
|
||||
}
|
||||
|
||||
function renderMcpToolTrace(events: CodexTraceEvent[]): string {
|
||||
const calls = collectMcpToolCalls(events);
|
||||
|
||||
if (calls.length === 0) {
|
||||
return "- No MCP tool activity was captured.";
|
||||
}
|
||||
|
||||
return calls
|
||||
.map((call, index) => {
|
||||
const completed = call.completedEvent;
|
||||
const started = call.startedEvent;
|
||||
const anchor = completed ?? started;
|
||||
const summary = [
|
||||
`- Call ${index + 1}: ${call.server ?? "(unknown server)"}/${call.tool ?? "(unknown tool)"}`,
|
||||
` started=${started ? formatElapsedMs(started.elapsedMs) : "(unknown)"}`,
|
||||
` completed=${completed ? formatElapsedMs(completed.elapsedMs) : "(not completed)"}`,
|
||||
` duration=${
|
||||
started && completed
|
||||
? formatDurationMs(completed.elapsedMs - started.elapsedMs)
|
||||
: "(unknown)"
|
||||
}`,
|
||||
` status=${completed?.status ?? started?.status ?? "(unknown)"}`,
|
||||
];
|
||||
|
||||
if (anchor?.arguments) {
|
||||
summary.push(
|
||||
` args=${sanitizeTraceText(JSON.stringify(anchor.arguments), 260)}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (completed?.resultPreview) {
|
||||
summary.push(
|
||||
` result=${sanitizeTraceText(completed.resultPreview, 320)}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (completed?.error) {
|
||||
summary.push(` error=${sanitizeTraceText(completed.error, 220)}`);
|
||||
}
|
||||
|
||||
return summary.join("\n");
|
||||
})
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
function renderCommandTrace(events: CodexTraceEvent[]): string {
|
||||
const commands = collectCommandExecutions(events);
|
||||
|
||||
if (commands.length === 0) {
|
||||
return "- No command activity was captured.";
|
||||
}
|
||||
|
||||
return commands
|
||||
.map((command, index) => {
|
||||
const completed = command.completedEvent;
|
||||
const started = command.startedEvent;
|
||||
const anchor = completed ?? started;
|
||||
const summary = [
|
||||
`- Command ${index + 1}: ${sanitizeTraceText(anchor?.command ?? "(unknown command)", 220)}`,
|
||||
` started=${started ? formatElapsedMs(started.elapsedMs) : "(unknown)"}`,
|
||||
` completed=${completed ? formatElapsedMs(completed.elapsedMs) : "(not completed)"}`,
|
||||
` duration=${
|
||||
started && completed
|
||||
? formatDurationMs(completed.elapsedMs - started.elapsedMs)
|
||||
: "(unknown)"
|
||||
}`,
|
||||
` status=${completed?.status ?? started?.status ?? "(unknown)"} exit=${completed?.exitCode ?? "(none)"}`,
|
||||
];
|
||||
|
||||
const output = completed?.output?.trim();
|
||||
|
||||
if (output) {
|
||||
summary.push(` output=${sanitizeTraceText(output, 320)}`);
|
||||
}
|
||||
|
||||
return summary.join("\n");
|
||||
})
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
function renderAggregateMcpToolTranscript(
|
||||
turnResults: CodexRunResult[],
|
||||
): string {
|
||||
const sections = turnResults
|
||||
.map((result, index) => {
|
||||
const rendered = renderMcpToolTrace(result.traceEvents);
|
||||
|
||||
return [
|
||||
`Turn ${index + 1}: duration=${formatDurationMs(result.durationMs)} usage=${renderUsageSummary(result.usage)}`,
|
||||
rendered,
|
||||
].join("\n");
|
||||
})
|
||||
.filter((section) => section.trim().length > 0);
|
||||
|
||||
return sections.length > 0
|
||||
? sections.join("\n\n")
|
||||
: "- No Codex MCP tool calls were recorded.";
|
||||
}
|
||||
653
packages/providers/src/owner-codex-runtime.ts
Normal file
653
packages/providers/src/owner-codex-runtime.ts
Normal file
|
|
@ -0,0 +1,653 @@
|
|||
import { spawn } from "node:child_process";
|
||||
|
||||
import type {
|
||||
CodexMcpServerConfig,
|
||||
ContextBundle,
|
||||
ProviderNotificationIntent,
|
||||
ProviderNotificationReason,
|
||||
RetrievalFilters,
|
||||
} from "@voidbot/shared";
|
||||
|
||||
import {
|
||||
DEFAULT_RETRIEVAL_RESULT_LIMIT,
|
||||
MAX_RETRIEVAL_RESULT_LIMIT,
|
||||
} from "@voidbot/shared";
|
||||
|
||||
import {
|
||||
extractLatestUsage,
|
||||
clamp,
|
||||
type CodexRunResult,
|
||||
type CodexTraceEvent,
|
||||
type CodexUsageSnapshot,
|
||||
HANDOFF_SENTINEL,
|
||||
type HistoryLookupTool,
|
||||
type HistoryToolRequest,
|
||||
isRecord,
|
||||
MAX_NOTIFICATION_MESSAGE_LENGTH,
|
||||
OWNER_NOTIFY_SENTINEL,
|
||||
readNumber,
|
||||
readRecord,
|
||||
readString,
|
||||
readUsage,
|
||||
summarizeToolResult,
|
||||
summarizeUnknown,
|
||||
TOOL_REQUEST_SENTINEL,
|
||||
type NormalizedDiscordReply,
|
||||
type ToolCallRecord,
|
||||
} from "./owner-codex-shared";
|
||||
|
||||
const DEFAULT_HISTORY_LIMIT = DEFAULT_RETRIEVAL_RESULT_LIMIT;
|
||||
const MAX_HISTORY_LIMIT = MAX_RETRIEVAL_RESULT_LIMIT;
|
||||
|
||||
export async function runCodexExec(input: {
|
||||
executable: string;
|
||||
executableArgs: string[];
|
||||
reasoningEffort: "low" | "medium" | "high" | "xhigh";
|
||||
timeoutMs: number;
|
||||
workingDirectory: string;
|
||||
prompt: string;
|
||||
mcpServers: CodexMcpServerConfig[];
|
||||
}): Promise<CodexRunResult> {
|
||||
return new Promise<CodexRunResult>((resolve) => {
|
||||
const startedAtMs = Date.now();
|
||||
const startedAt = new Date(startedAtMs).toISOString();
|
||||
const args = [
|
||||
...input.executableArgs,
|
||||
"exec",
|
||||
"-c",
|
||||
'approval_policy="never"',
|
||||
"-c",
|
||||
`model_reasoning_effort=${JSON.stringify(input.reasoningEffort)}`,
|
||||
...buildMcpConfigArguments(input.mcpServers),
|
||||
"--json",
|
||||
"--skip-git-repo-check",
|
||||
"-s",
|
||||
"read-only",
|
||||
input.prompt,
|
||||
];
|
||||
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
let resolved = false;
|
||||
let handoffReason: string | undefined;
|
||||
let stdoutBuffer = "";
|
||||
const traceEvents: CodexTraceEvent[] = [];
|
||||
|
||||
const child = spawn(input.executable, args, {
|
||||
cwd: input.workingDirectory,
|
||||
env: process.env,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
|
||||
const finish = (
|
||||
result: Omit<
|
||||
CodexRunResult,
|
||||
"startedAt" | "finishedAt" | "durationMs" | "traceEvents" | "usage"
|
||||
>,
|
||||
) => {
|
||||
if (resolved) {
|
||||
return;
|
||||
}
|
||||
|
||||
resolved = true;
|
||||
clearTimeout(timeoutHandle);
|
||||
const finishedAtMs = Date.now();
|
||||
|
||||
resolve({
|
||||
...result,
|
||||
startedAt,
|
||||
finishedAt: new Date(finishedAtMs).toISOString(),
|
||||
durationMs: finishedAtMs - startedAtMs,
|
||||
traceEvents: [...traceEvents],
|
||||
usage: extractLatestUsage(traceEvents),
|
||||
});
|
||||
};
|
||||
|
||||
child.stdout.setEncoding("utf8");
|
||||
child.stderr.setEncoding("utf8");
|
||||
|
||||
child.stdout.on("data", (chunk: string) => {
|
||||
stdout += chunk;
|
||||
stdoutBuffer += chunk;
|
||||
|
||||
const lines = stdoutBuffer.split(/\r?\n/);
|
||||
stdoutBuffer = lines.pop() ?? "";
|
||||
|
||||
for (const line of lines) {
|
||||
const traceEvent = tryParseTraceEvent(
|
||||
line,
|
||||
traceEvents.length + 1,
|
||||
startedAtMs,
|
||||
);
|
||||
|
||||
if (traceEvent) {
|
||||
traceEvents.push(traceEvent);
|
||||
}
|
||||
|
||||
const event = tryParseCodexLine(line);
|
||||
|
||||
if (!event) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (event.approvalRequested) {
|
||||
handoffReason =
|
||||
event.handoffReason ??
|
||||
"The request needs tools or permissions outside the Discord-safe owner allowlist.";
|
||||
child.kill();
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.toolRequest) {
|
||||
child.kill();
|
||||
finish({
|
||||
stdout,
|
||||
stderr,
|
||||
exitCode: null,
|
||||
toolRequest: event.toolRequest,
|
||||
timedOut: false,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.handoffReason && !handoffReason) {
|
||||
handoffReason = event.handoffReason;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
child.stderr.on("data", (chunk: string) => {
|
||||
stderr += chunk;
|
||||
});
|
||||
|
||||
child.on("error", (error) => {
|
||||
finish({
|
||||
stdout,
|
||||
stderr: `${stderr}${error.message}\n`,
|
||||
exitCode: null,
|
||||
handoffReason:
|
||||
"The local Codex executable was not available for automatic Discord replies.",
|
||||
timedOut: false,
|
||||
});
|
||||
});
|
||||
|
||||
child.on("close", (code) => {
|
||||
if (stdoutBuffer.trim().length > 0) {
|
||||
const traceEvent = tryParseTraceEvent(
|
||||
stdoutBuffer.trim(),
|
||||
traceEvents.length + 1,
|
||||
startedAtMs,
|
||||
);
|
||||
|
||||
if (traceEvent) {
|
||||
traceEvents.push(traceEvent);
|
||||
}
|
||||
|
||||
const event = tryParseCodexLine(stdoutBuffer.trim());
|
||||
|
||||
if (event?.approvalRequested && !handoffReason) {
|
||||
handoffReason =
|
||||
event.handoffReason ??
|
||||
"The request needs tools or permissions outside the Discord-safe owner allowlist.";
|
||||
}
|
||||
}
|
||||
|
||||
finish({
|
||||
stdout,
|
||||
stderr,
|
||||
exitCode: code,
|
||||
handoffReason,
|
||||
timedOut: false,
|
||||
});
|
||||
});
|
||||
|
||||
const timeoutHandle = setTimeout(() => {
|
||||
handoffReason = "The local Codex run exceeded the Discord reply timeout.";
|
||||
child.kill();
|
||||
finish({
|
||||
stdout,
|
||||
stderr,
|
||||
exitCode: null,
|
||||
handoffReason,
|
||||
timedOut: true,
|
||||
});
|
||||
}, input.timeoutMs);
|
||||
});
|
||||
}
|
||||
|
||||
export function normalizeDiscordReply(stdout: string): NormalizedDiscordReply {
|
||||
const messages: string[] = [];
|
||||
|
||||
for (const rawLine of stdout.split(/\r?\n/)) {
|
||||
const trimmed = rawLine.trim();
|
||||
|
||||
if (trimmed.length === 0 || !trimmed.startsWith("{")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(trimmed) as Record<string, unknown>;
|
||||
const event = unwrapCodexEvent(parsed);
|
||||
const type = typeof event.type === "string" ? event.type : "";
|
||||
|
||||
if (type.endsWith("_delta")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (type === "agent_message") {
|
||||
const message = readString(event, "message") ?? readString(event, "text");
|
||||
|
||||
if (message) {
|
||||
messages.push(message.trim());
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
type === "item.completed" &&
|
||||
isRecord(event.item) &&
|
||||
event.item.type === "agent_message"
|
||||
) {
|
||||
const message =
|
||||
readString(event.item, "text") ?? readString(event.item, "message");
|
||||
|
||||
if (message) {
|
||||
messages.push(message.trim());
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
const reply = [...messages].reverse().find((message) => message.length > 0);
|
||||
|
||||
if (!reply) {
|
||||
return { notifications: [] };
|
||||
}
|
||||
|
||||
if (reply.startsWith(HANDOFF_SENTINEL)) {
|
||||
return { notifications: [] };
|
||||
}
|
||||
|
||||
if (reply.startsWith(TOOL_REQUEST_SENTINEL)) {
|
||||
return { notifications: [] };
|
||||
}
|
||||
|
||||
const notifications: ProviderNotificationIntent[] = [];
|
||||
const replyLines: string[] = [];
|
||||
|
||||
for (const line of reply.split(/\r?\n/)) {
|
||||
const trimmed = line.trim();
|
||||
|
||||
if (trimmed.startsWith(OWNER_NOTIFY_SENTINEL)) {
|
||||
const notification = parseOwnerNotificationIntent(trimmed);
|
||||
|
||||
if (notification) {
|
||||
notifications.push(notification);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
replyLines.push(line);
|
||||
}
|
||||
|
||||
const cleanedReply = replyLines.join("\n").trim();
|
||||
|
||||
return {
|
||||
reply: cleanedReply.length > 0 ? cleanedReply : undefined,
|
||||
notifications,
|
||||
};
|
||||
}
|
||||
|
||||
export async function executeHistoryLookup(
|
||||
historyLookup: HistoryLookupTool,
|
||||
context: ContextBundle,
|
||||
request: HistoryToolRequest,
|
||||
): Promise<ToolCallRecord> {
|
||||
const filters: RetrievalFilters = {
|
||||
corpusKind: "discord_history",
|
||||
guildId: context.guildContext.guildId,
|
||||
channelId: request.channelId,
|
||||
authorId: request.authorId,
|
||||
};
|
||||
|
||||
const results = await historyLookup.search(request.query, request.limit, filters);
|
||||
|
||||
return {
|
||||
request,
|
||||
results,
|
||||
};
|
||||
}
|
||||
|
||||
function buildMcpConfigArguments(mcpServers: CodexMcpServerConfig[]): string[] {
|
||||
const argumentsList: string[] = [];
|
||||
|
||||
for (const server of mcpServers) {
|
||||
argumentsList.push(
|
||||
"-c",
|
||||
`mcp_servers.${server.name}.command=${JSON.stringify(server.command)}`,
|
||||
);
|
||||
argumentsList.push(
|
||||
"-c",
|
||||
`mcp_servers.${server.name}.args=${JSON.stringify(server.args)}`,
|
||||
);
|
||||
|
||||
if (server.cwd) {
|
||||
argumentsList.push(
|
||||
"-c",
|
||||
`mcp_servers.${server.name}.cwd=${JSON.stringify(server.cwd)}`,
|
||||
);
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(server.env ?? {})) {
|
||||
argumentsList.push(
|
||||
"-c",
|
||||
`mcp_servers.${server.name}.env.${key}=${JSON.stringify(value)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return argumentsList;
|
||||
}
|
||||
|
||||
function tryParseCodexLine(line: string):
|
||||
| {
|
||||
approvalRequested: boolean;
|
||||
handoffReason?: string;
|
||||
toolRequest?: HistoryToolRequest;
|
||||
}
|
||||
| undefined {
|
||||
const trimmed = line.trim();
|
||||
|
||||
if (!trimmed.startsWith("{")) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(trimmed) as Record<string, unknown>;
|
||||
const event = unwrapCodexEvent(parsed);
|
||||
const type = typeof event?.type === "string" ? event.type : "";
|
||||
|
||||
if (type.includes("approval_request")) {
|
||||
return {
|
||||
approvalRequested: true,
|
||||
handoffReason:
|
||||
"The request needs tools or permissions outside the Discord-safe owner allowlist.",
|
||||
};
|
||||
}
|
||||
|
||||
if (type === "agent_message") {
|
||||
const message = readString(event, "message") ?? readString(event, "text");
|
||||
|
||||
if (message?.trim().startsWith(HANDOFF_SENTINEL)) {
|
||||
return {
|
||||
approvalRequested: false,
|
||||
handoffReason: message.trim().slice(HANDOFF_SENTINEL.length).trim(),
|
||||
};
|
||||
}
|
||||
|
||||
const toolRequest = parseHistoryToolRequest(message);
|
||||
|
||||
if (toolRequest) {
|
||||
return {
|
||||
approvalRequested: false,
|
||||
toolRequest,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
type === "item.completed" &&
|
||||
isRecord(event.item) &&
|
||||
event.item.type === "agent_message"
|
||||
) {
|
||||
const message =
|
||||
readString(event.item, "text") ?? readString(event.item, "message");
|
||||
|
||||
if (message?.trim().startsWith(HANDOFF_SENTINEL)) {
|
||||
return {
|
||||
approvalRequested: false,
|
||||
handoffReason: message.trim().slice(HANDOFF_SENTINEL.length).trim(),
|
||||
};
|
||||
}
|
||||
|
||||
const toolRequest = parseHistoryToolRequest(message);
|
||||
|
||||
if (toolRequest) {
|
||||
return {
|
||||
approvalRequested: false,
|
||||
toolRequest,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function tryParseTraceEvent(
|
||||
line: string,
|
||||
sequence: number,
|
||||
startedAtMs: number,
|
||||
): CodexTraceEvent | undefined {
|
||||
const trimmed = line.trim();
|
||||
|
||||
if (!trimmed.startsWith("{")) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(trimmed) as Record<string, unknown>;
|
||||
const event = unwrapCodexEvent(parsed);
|
||||
const type = typeof event.type === "string" ? event.type : "";
|
||||
const timestampMs = Date.now();
|
||||
const baseEvent = {
|
||||
sequence,
|
||||
timestamp: new Date(timestampMs).toISOString(),
|
||||
elapsedMs: timestampMs - startedAtMs,
|
||||
};
|
||||
|
||||
if (type === "thread.started") {
|
||||
return {
|
||||
...baseEvent,
|
||||
kind: "thread_started",
|
||||
};
|
||||
}
|
||||
|
||||
if (type === "turn.started") {
|
||||
return {
|
||||
...baseEvent,
|
||||
kind: "turn_started",
|
||||
};
|
||||
}
|
||||
|
||||
if (type === "turn.completed") {
|
||||
return {
|
||||
...baseEvent,
|
||||
kind: "turn_completed",
|
||||
usage: readUsage(event.usage),
|
||||
};
|
||||
}
|
||||
|
||||
if (type === "agent_message") {
|
||||
const message = readString(event, "message") ?? readString(event, "text");
|
||||
|
||||
if (message) {
|
||||
return {
|
||||
...baseEvent,
|
||||
kind: "agent_message",
|
||||
message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (!isRecord(event.item)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const item = event.item;
|
||||
const itemId = readString(item, "id");
|
||||
|
||||
if (type === "item.started" && item.type === "command_execution") {
|
||||
return {
|
||||
...baseEvent,
|
||||
kind: "command_started",
|
||||
itemId,
|
||||
command: readString(item, "command") ?? "(unknown command)",
|
||||
status: readString(item, "status") ?? "in_progress",
|
||||
};
|
||||
}
|
||||
|
||||
if (type === "item.completed" && item.type === "command_execution") {
|
||||
return {
|
||||
...baseEvent,
|
||||
kind: "command_completed",
|
||||
itemId,
|
||||
command: readString(item, "command") ?? "(unknown command)",
|
||||
status: readString(item, "status") ?? "(unknown)",
|
||||
exitCode: readNumber(item.exit_code),
|
||||
output: readString(item, "aggregated_output") ?? "",
|
||||
};
|
||||
}
|
||||
|
||||
if (type === "item.started" && item.type === "mcp_tool_call") {
|
||||
return {
|
||||
...baseEvent,
|
||||
kind: "mcp_tool_started",
|
||||
itemId,
|
||||
server: readString(item, "server"),
|
||||
tool: readString(item, "tool"),
|
||||
status: readString(item, "status") ?? "in_progress",
|
||||
arguments: readRecord(item.arguments),
|
||||
};
|
||||
}
|
||||
|
||||
if (type === "item.completed" && item.type === "mcp_tool_call") {
|
||||
return {
|
||||
...baseEvent,
|
||||
kind: "mcp_tool_completed",
|
||||
itemId,
|
||||
server: readString(item, "server"),
|
||||
tool: readString(item, "tool"),
|
||||
status: readString(item, "status") ?? "(unknown)",
|
||||
arguments: readRecord(item.arguments),
|
||||
error: summarizeUnknown(item.error, 220),
|
||||
resultPreview: summarizeToolResult(item.result),
|
||||
};
|
||||
}
|
||||
|
||||
if (type === "item.completed" && item.type === "agent_message") {
|
||||
const message =
|
||||
readString(item, "text") ?? readString(item, "message");
|
||||
|
||||
if (message) {
|
||||
return {
|
||||
...baseEvent,
|
||||
kind: "agent_message",
|
||||
itemId,
|
||||
message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function unwrapCodexEvent(parsed: Record<string, unknown>): Record<string, unknown> {
|
||||
if (isRecord(parsed.msg) && typeof parsed.msg.type === "string") {
|
||||
return parsed.msg;
|
||||
}
|
||||
|
||||
if (isRecord(parsed.payload) && typeof parsed.payload.type === "string") {
|
||||
return parsed.payload;
|
||||
}
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function parseHistoryToolRequest(
|
||||
message: string | undefined,
|
||||
): HistoryToolRequest | undefined {
|
||||
const trimmed = message?.trim();
|
||||
|
||||
if (!trimmed?.startsWith(TOOL_REQUEST_SENTINEL)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const payload = trimmed.slice(TOOL_REQUEST_SENTINEL.length).trim();
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(payload) as Record<string, unknown>;
|
||||
|
||||
if (parsed.tool !== "search_history") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const query = typeof parsed.query === "string" ? parsed.query.trim() : "";
|
||||
|
||||
if (query.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const rawLimit =
|
||||
typeof parsed.limit === "number" ? parsed.limit : DEFAULT_HISTORY_LIMIT;
|
||||
const limit = clamp(Math.trunc(rawLimit), 1, MAX_HISTORY_LIMIT);
|
||||
|
||||
return {
|
||||
tool: "search_history",
|
||||
query: query.slice(0, 240),
|
||||
limit,
|
||||
channelId:
|
||||
typeof parsed.channelId === "string" ? parsed.channelId : undefined,
|
||||
authorId: typeof parsed.authorId === "string" ? parsed.authorId : undefined,
|
||||
};
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function parseOwnerNotificationIntent(
|
||||
line: string,
|
||||
): ProviderNotificationIntent | undefined {
|
||||
const payload = line.slice(OWNER_NOTIFY_SENTINEL.length).trim();
|
||||
|
||||
if (payload.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(payload) as Record<string, unknown>;
|
||||
const message =
|
||||
typeof parsed.message === "string"
|
||||
? parsed.message.trim().replace(/\s+/g, " ")
|
||||
: "";
|
||||
|
||||
if (message.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
channel: "owner_dm",
|
||||
reason: normalizeNotificationReason(parsed.reason),
|
||||
message: message.slice(0, MAX_NOTIFICATION_MESSAGE_LENGTH),
|
||||
};
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeNotificationReason(
|
||||
value: unknown,
|
||||
): ProviderNotificationReason {
|
||||
if (value === "completion" || value === "failure" || value === "handoff") {
|
||||
return value;
|
||||
}
|
||||
|
||||
return "custom";
|
||||
}
|
||||
484
packages/providers/src/owner-codex-shared.ts
Normal file
484
packages/providers/src/owner-codex-shared.ts
Normal file
|
|
@ -0,0 +1,484 @@
|
|||
import type {
|
||||
CodexMcpServerConfig,
|
||||
ContextBundle,
|
||||
OwnerCodexMode,
|
||||
ProviderNotificationIntent,
|
||||
ProviderRequest,
|
||||
RetrievalFilters,
|
||||
RetrievalResult,
|
||||
} from "@voidbot/shared";
|
||||
|
||||
export const HANDOFF_SENTINEL = "VOIDBOT_HANDOFF_REQUIRED:";
|
||||
export const OWNER_NOTIFY_SENTINEL = "VOIDBOT_OWNER_NOTIFY:";
|
||||
export const TOOL_REQUEST_SENTINEL = "VOIDBOT_TOOL_REQUEST:";
|
||||
export const MAX_HISTORY_TOOL_CALLS = 4;
|
||||
export const MAX_SOURCE_GROUNDING_RETRIES = 1;
|
||||
export const MAX_NOTIFICATION_MESSAGE_LENGTH = 400;
|
||||
|
||||
export interface CodexUsageSnapshot {
|
||||
inputTokens: number | null;
|
||||
cachedInputTokens: number | null;
|
||||
outputTokens: number | null;
|
||||
}
|
||||
|
||||
export interface HistoryLookupTool {
|
||||
search(
|
||||
query: string,
|
||||
limit?: number,
|
||||
filters?: RetrievalFilters,
|
||||
): Promise<RetrievalResult[]>;
|
||||
}
|
||||
|
||||
export interface OwnerCodexProviderOptions {
|
||||
ownerDiscordId: string;
|
||||
enabled: boolean;
|
||||
mode: OwnerCodexMode;
|
||||
executable: string;
|
||||
executableArgs: string[];
|
||||
reasoningEffort: "low" | "medium" | "high" | "xhigh";
|
||||
timeoutMs: number;
|
||||
workingDirectory: string;
|
||||
historyLookup?: HistoryLookupTool;
|
||||
handoffNoticeBuilder?: (jobId: string) => string;
|
||||
mcpServers?: CodexMcpServerConfig[];
|
||||
}
|
||||
|
||||
export interface CodexRunResult {
|
||||
stdout: string;
|
||||
stderr: string;
|
||||
exitCode: number | null;
|
||||
handoffReason?: string;
|
||||
toolRequest?: HistoryToolRequest;
|
||||
timedOut: boolean;
|
||||
startedAt: string;
|
||||
finishedAt: string;
|
||||
durationMs: number;
|
||||
traceEvents: CodexTraceEvent[];
|
||||
usage?: CodexUsageSnapshot;
|
||||
}
|
||||
|
||||
export interface HistoryToolRequest {
|
||||
tool: "search_history";
|
||||
query: string;
|
||||
limit: number;
|
||||
channelId?: string;
|
||||
authorId?: string;
|
||||
}
|
||||
|
||||
export interface ToolCallRecord {
|
||||
request: HistoryToolRequest;
|
||||
results: RetrievalResult[];
|
||||
}
|
||||
|
||||
export interface CodexTraceEvent {
|
||||
sequence: number;
|
||||
timestamp: string;
|
||||
elapsedMs: number;
|
||||
kind:
|
||||
| "thread_started"
|
||||
| "turn_started"
|
||||
| "turn_completed"
|
||||
| "agent_message"
|
||||
| "command_started"
|
||||
| "command_completed"
|
||||
| "mcp_tool_started"
|
||||
| "mcp_tool_completed";
|
||||
itemId?: string;
|
||||
message?: string;
|
||||
command?: string;
|
||||
server?: string;
|
||||
tool?: string;
|
||||
status?: string;
|
||||
exitCode?: number | null;
|
||||
output?: string;
|
||||
arguments?: Record<string, unknown>;
|
||||
error?: string;
|
||||
resultPreview?: string;
|
||||
usage?: CodexUsageSnapshot;
|
||||
}
|
||||
|
||||
export interface NormalizedDiscordReply {
|
||||
reply?: string;
|
||||
notifications: ProviderNotificationIntent[];
|
||||
}
|
||||
|
||||
export interface TimedTraceOperation {
|
||||
itemId: string;
|
||||
startedEvent?: CodexTraceEvent;
|
||||
completedEvent?: CodexTraceEvent;
|
||||
command?: string;
|
||||
server?: string;
|
||||
tool?: string;
|
||||
}
|
||||
|
||||
export function buildRequestPayload(
|
||||
request: ProviderRequest,
|
||||
): Record<string, unknown> {
|
||||
return {
|
||||
provider: request.provider,
|
||||
createdAt: new Date().toISOString(),
|
||||
prompt: request.contextBundle.prompt,
|
||||
actor: request.contextBundle.actor,
|
||||
guildContext: request.contextBundle.guildContext,
|
||||
interactionMemory: request.contextBundle.interactionMemory,
|
||||
sourceGrounding: request.contextBundle.sourceGrounding,
|
||||
stylePack: request.contextBundle.stylePack,
|
||||
recentMessages: request.contextBundle.recentMessages,
|
||||
retrieval: request.contextBundle.retrieval,
|
||||
options: request.options ?? {},
|
||||
};
|
||||
}
|
||||
|
||||
export function buildDefaultHandoffNotice(jobId: string): string {
|
||||
return `This request needs the fuller local Codex flow. Check \`.voidbot/artifacts/${jobId}/handoff.md\` and \`.voidbot/artifacts/${jobId}/debug-trace.md\` in the local workspace.`;
|
||||
}
|
||||
|
||||
export function didUseSourceGrounding(events: CodexTraceEvent[]): boolean {
|
||||
return events.some(
|
||||
(event) =>
|
||||
event.kind === "mcp_tool_completed" &&
|
||||
(event.tool === "list_indexed_repos" ||
|
||||
event.tool === "search_sources" ||
|
||||
event.tool === "get_source_context"),
|
||||
);
|
||||
}
|
||||
|
||||
export function extractLatestUsage(
|
||||
events: CodexTraceEvent[],
|
||||
): CodexUsageSnapshot | undefined {
|
||||
for (let index = events.length - 1; index >= 0; index -= 1) {
|
||||
const usage = events[index]?.usage;
|
||||
|
||||
if (usage) {
|
||||
return usage;
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null;
|
||||
}
|
||||
|
||||
export function readString(
|
||||
value: Record<string, unknown>,
|
||||
key: string,
|
||||
): string | undefined {
|
||||
const candidate = value[key];
|
||||
return typeof candidate === "string" ? candidate : undefined;
|
||||
}
|
||||
|
||||
export function clamp(
|
||||
value: number,
|
||||
minimum: number,
|
||||
maximum: number,
|
||||
): number {
|
||||
if (value < minimum) {
|
||||
return minimum;
|
||||
}
|
||||
|
||||
if (value > maximum) {
|
||||
return maximum;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
export function readNumber(value: unknown): number | null {
|
||||
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
||||
}
|
||||
|
||||
export function readUsage(value: unknown): CodexUsageSnapshot | undefined {
|
||||
if (!isRecord(value)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
inputTokens: readNumber(value.input_tokens),
|
||||
cachedInputTokens: readNumber(value.cached_input_tokens),
|
||||
outputTokens: readNumber(value.output_tokens),
|
||||
};
|
||||
}
|
||||
|
||||
export function readRecord(
|
||||
value: unknown,
|
||||
): Record<string, unknown> | undefined {
|
||||
return isRecord(value) ? value : undefined;
|
||||
}
|
||||
|
||||
export function renderUsageSummary(
|
||||
usage: CodexUsageSnapshot | undefined,
|
||||
): string {
|
||||
if (!usage) {
|
||||
return "(not reported)";
|
||||
}
|
||||
|
||||
return [
|
||||
`input=${usage.inputTokens ?? 0}`,
|
||||
`cached=${usage.cachedInputTokens ?? 0}`,
|
||||
`output=${usage.outputTokens ?? 0}`,
|
||||
].join(" ");
|
||||
}
|
||||
|
||||
export function formatElapsedMs(elapsedMs: number): string {
|
||||
return `+${formatDurationMs(elapsedMs)}`;
|
||||
}
|
||||
|
||||
export function formatDurationMs(durationMs: number): string {
|
||||
if (!Number.isFinite(durationMs)) {
|
||||
return "(unknown)";
|
||||
}
|
||||
|
||||
if (durationMs < 1_000) {
|
||||
return `${durationMs}ms`;
|
||||
}
|
||||
|
||||
return `${(durationMs / 1_000).toFixed(durationMs < 10_000 ? 2 : 1)}s`;
|
||||
}
|
||||
|
||||
export function summarizeStderrDiagnostics(stderr: string): string[] {
|
||||
const diagnostics: string[] = [];
|
||||
const seen = new Set<string>();
|
||||
|
||||
for (const rawLine of stderr.split(/\r?\n/)) {
|
||||
const line = rawLine.trim();
|
||||
|
||||
if (line.length === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!/(WARN|ERROR|failed|timed out|forbidden|rejected)/i.test(line)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const normalized = sanitizeTraceText(line, 220);
|
||||
|
||||
if (seen.has(normalized)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
seen.add(normalized);
|
||||
diagnostics.push(`- ${normalized}`);
|
||||
|
||||
if (diagnostics.length >= 20) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return diagnostics;
|
||||
}
|
||||
|
||||
export function sanitizeTraceText(input: string, limit: number): string {
|
||||
const collapsed = input.replace(/\s+/g, " ").trim();
|
||||
|
||||
if (collapsed.length <= limit) {
|
||||
return collapsed;
|
||||
}
|
||||
|
||||
return `${collapsed.slice(0, limit - 3)}...`;
|
||||
}
|
||||
|
||||
export function summarizeUnknown(
|
||||
value: unknown,
|
||||
limit: number,
|
||||
): string | undefined {
|
||||
if (typeof value === "string" && value.trim().length > 0) {
|
||||
return sanitizeTraceText(value, limit);
|
||||
}
|
||||
|
||||
if (value === null || value === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
return sanitizeTraceText(JSON.stringify(value), limit);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function summarizeToolResult(value: unknown): string | undefined {
|
||||
if (!isRecord(value)) {
|
||||
return summarizeUnknown(value, 320);
|
||||
}
|
||||
|
||||
const structured = readRecord(value.structured_content);
|
||||
|
||||
if (structured) {
|
||||
const summaryBits: string[] = [];
|
||||
const query = readString(structured, "query");
|
||||
const sourceId = readString(structured, "sourceId");
|
||||
const messageId = readString(structured, "messageId");
|
||||
const resultCount = readNumber(structured.resultCount);
|
||||
const count = readNumber(structured.count);
|
||||
|
||||
if (query) {
|
||||
summaryBits.push(`query=${sanitizeTraceText(query, 120)}`);
|
||||
}
|
||||
|
||||
if (sourceId) {
|
||||
summaryBits.push(`sourceId=${sanitizeTraceText(sourceId, 120)}`);
|
||||
}
|
||||
|
||||
if (messageId) {
|
||||
summaryBits.push(`messageId=${messageId}`);
|
||||
}
|
||||
|
||||
if (typeof structured.found === "boolean") {
|
||||
summaryBits.push(`found=${structured.found ? "yes" : "no"}`);
|
||||
}
|
||||
|
||||
if (resultCount !== null) {
|
||||
summaryBits.push(`resultCount=${resultCount}`);
|
||||
}
|
||||
|
||||
if (count !== null) {
|
||||
summaryBits.push(`count=${count}`);
|
||||
}
|
||||
|
||||
if (summaryBits.length > 0) {
|
||||
return summaryBits.join(" ");
|
||||
}
|
||||
}
|
||||
|
||||
const content = Array.isArray(value.content) ? value.content : [];
|
||||
|
||||
if (content.length > 0) {
|
||||
const fragments = content
|
||||
.slice(0, 3)
|
||||
.map((entry) => {
|
||||
if (!isRecord(entry)) {
|
||||
return summarizeUnknown(entry, 100) ?? "(unknown)";
|
||||
}
|
||||
|
||||
if (typeof entry.text === "string") {
|
||||
return entry.text;
|
||||
}
|
||||
|
||||
if (isRecord(entry.resource) && typeof entry.resource.text === "string") {
|
||||
return entry.resource.text;
|
||||
}
|
||||
|
||||
return summarizeUnknown(entry, 100) ?? "(unknown)";
|
||||
})
|
||||
.filter((entry): entry is string => entry.length > 0);
|
||||
|
||||
if (fragments.length > 0) {
|
||||
return sanitizeTraceText(fragments.join(" | "), 320);
|
||||
}
|
||||
}
|
||||
|
||||
return summarizeUnknown(value, 320);
|
||||
}
|
||||
|
||||
export function collectMcpToolCalls(
|
||||
events: CodexTraceEvent[],
|
||||
): TimedTraceOperation[] {
|
||||
const calls = new Map<string, TimedTraceOperation>();
|
||||
const order: string[] = [];
|
||||
|
||||
for (const event of events) {
|
||||
if (event.kind !== "mcp_tool_started" && event.kind !== "mcp_tool_completed") {
|
||||
continue;
|
||||
}
|
||||
|
||||
const itemId = event.itemId ?? `mcp-${event.sequence}`;
|
||||
|
||||
if (!calls.has(itemId)) {
|
||||
calls.set(itemId, {
|
||||
itemId,
|
||||
server: event.server,
|
||||
tool: event.tool,
|
||||
});
|
||||
order.push(itemId);
|
||||
}
|
||||
|
||||
const call = calls.get(itemId)!;
|
||||
call.server ??= event.server;
|
||||
call.tool ??= event.tool;
|
||||
|
||||
if (event.kind === "mcp_tool_started") {
|
||||
call.startedEvent = event;
|
||||
} else {
|
||||
call.completedEvent = event;
|
||||
}
|
||||
}
|
||||
|
||||
return order.map((itemId) => calls.get(itemId)!);
|
||||
}
|
||||
|
||||
export function collectCommandExecutions(
|
||||
events: CodexTraceEvent[],
|
||||
): TimedTraceOperation[] {
|
||||
const commands = new Map<string, TimedTraceOperation>();
|
||||
const order: string[] = [];
|
||||
|
||||
for (const event of events) {
|
||||
if (event.kind !== "command_started" && event.kind !== "command_completed") {
|
||||
continue;
|
||||
}
|
||||
|
||||
const itemId = event.itemId ?? `command-${event.sequence}`;
|
||||
|
||||
if (!commands.has(itemId)) {
|
||||
commands.set(itemId, {
|
||||
itemId,
|
||||
command: event.command,
|
||||
});
|
||||
order.push(itemId);
|
||||
}
|
||||
|
||||
const command = commands.get(itemId)!;
|
||||
command.command ??= event.command;
|
||||
|
||||
if (event.kind === "command_started") {
|
||||
command.startedEvent = event;
|
||||
} else {
|
||||
command.completedEvent = event;
|
||||
}
|
||||
}
|
||||
|
||||
return order.map((itemId) => commands.get(itemId)!);
|
||||
}
|
||||
|
||||
export function renderInteractionMemory(context: ContextBundle): string {
|
||||
if (!context.interactionMemory) {
|
||||
return "- No explicit interaction memory for this speaker was attached.";
|
||||
}
|
||||
|
||||
const recentEvents = context.interactionMemory.recentEvents.length
|
||||
? context.interactionMemory.recentEvents
|
||||
.slice()
|
||||
.reverse()
|
||||
.slice(0, 6)
|
||||
.map(
|
||||
(event) =>
|
||||
`- [${event.timestamp}] ${event.sourceKind === "ambient_mention" ? "ambient" : "direct"} ${event.sentiment} score=${event.score}: ${event.summary} Quote: "${event.excerpt}"`,
|
||||
)
|
||||
.join("\n")
|
||||
: "- No recent interaction events were retained.";
|
||||
const dimensions = context.interactionMemory.interactionDimensions.length
|
||||
? context.interactionMemory.interactionDimensions
|
||||
.map(
|
||||
(dimension) =>
|
||||
`- ${dimension.label}: ${dimension.score}/3. ${dimension.summary}`,
|
||||
)
|
||||
.join("\n")
|
||||
: "- No strong interaction dimensions were inferred yet.";
|
||||
|
||||
return [
|
||||
`- Summary: ${context.interactionMemory.summary}`,
|
||||
`- Disposition: ${context.interactionMemory.disposition}`,
|
||||
`- Affinity score: ${context.interactionMemory.affinityScore}`,
|
||||
`- Psychological profile: ${context.interactionMemory.psychologicalProfile}`,
|
||||
`- Inferred traits: ${context.interactionMemory.inferredTraits.length > 0 ? context.interactionMemory.inferredTraits.join(", ") : "(none yet)"}`,
|
||||
"- Interaction dimensions:",
|
||||
dimensions,
|
||||
`- Response guidance: ${context.interactionMemory.responseGuidance}`,
|
||||
`- Direct remembered interactions: ${context.interactionMemory.directInteractionCount}`,
|
||||
`- Ambient remembered mentions: ${context.interactionMemory.ambientMentionCount}`,
|
||||
"- Specific remembered incidents:",
|
||||
recentEvents,
|
||||
].join("\n");
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
{"ts":"2026-04-24T16:30:00+01:00","type":"architecture","status":"ok","note":"Operational state now lives in Postgres while vector retrieval lives in Qdrant; archives, artifacts, logs, and backups remain under .voidbot so operational truth, semantic retrieval, and raw evidence no longer fight in one store."}
|
||||
{"ts":"2026-04-25T13:00:00+01:00","type":"architecture","status":"ok","note":"Tool-driven retrieval is now the default for owner_codex and local_llm; source and lore answers are expected to touch source-side lookup before sounding confident."}
|
||||
{"ts":"2026-04-26T03:05:00+01:00","type":"process","status":"ok","note":"Canonical project-state surfaces were added so future sessions can rehydrate from map, handoff, system map, implementation plan, and distilled evidence instead of relying on transcript memory alone."}
|
||||
{"ts":"2026-04-26T03:40:00+01:00","type":"architecture","status":"ok","note":"The owner Codex provider was split into orchestration, runtime/parsing, rendering, and shared-helper modules; the architecture was sound, but file bloat had become the likelier future failure surface."}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
experiment:
|
||||
name: VoidBot
|
||||
status: active
|
||||
phase: persistent_state_adoption
|
||||
phase: legibility_hardening
|
||||
premise: >
|
||||
Keep VoidBot operable as a Discord-native assistant with real retrieval,
|
||||
durable ops, and explicit project memory surfaces instead of letting
|
||||
|
|
@ -14,8 +14,8 @@ objective:
|
|||
Aetheria lore, while keeping the system understandable enough to modify
|
||||
safely.
|
||||
current: >
|
||||
Land persistent project-state scaffolding so future sessions can rehydrate
|
||||
from canonical files before touching the increasingly interconnected stack.
|
||||
Keep the new persistent state spine honest while cutting oversized runtime
|
||||
organs into clearer seams before they grow another layer of moss.
|
||||
|
||||
constraints:
|
||||
- Public traffic still rides one remote Qwen box plus local embedding and ops infrastructure.
|
||||
|
|
@ -54,6 +54,7 @@ active_subgoals:
|
|||
- Make the new project-state surfaces the default continuity path for nontrivial VoidBot work.
|
||||
- Keep public-lane limits, moderation, and provider behavior constrained enough that one 4060 Ti box does not get flayed for sport.
|
||||
- Preserve boring observability around Postgres, Qdrant, Ollama, Docker, backups, and watchdogs.
|
||||
- Continue splitting oversized runtime files where concern boundaries are already obvious, starting with the remaining bot, worker, and memory-policy monarchies.
|
||||
|
||||
accepted_design:
|
||||
- Bot, worker, providers, and RAG remain the main structural seams.
|
||||
|
|
@ -71,6 +72,7 @@ landed_baseline:
|
|||
- Local and offsite backups, watchdogs, and the operations dashboard are live.
|
||||
- Interaction memory and behavioral profiling are live.
|
||||
- Source-grounding enforcement and tool-driven retrieval are live.
|
||||
- The owner Codex lane is now split into orchestration, runtime/parsing, shared helpers, and rendering modules instead of one swollen provider file.
|
||||
|
||||
rejected_paths:
|
||||
- name: transcript_only_continuity
|
||||
|
|
@ -81,5 +83,5 @@ rejected_paths:
|
|||
reason: vibes produce plausible slop that sounds smart and lies with a straight face
|
||||
|
||||
current_status:
|
||||
summary: VoidBot now spans Discord routing, background jobs, Postgres state, Qdrant retrieval, source and lore indexing, provider-specific tool loops, ops automation, and a growing social-memory layer. The system works, but it is too interconnected to steer safely from transcript memory alone, so canonical project-state surfaces are being added to keep future work source-grounded and rehydratable.
|
||||
next_action: After this persistence pass, rehydrate from the canonical state files before the next nontrivial change and keep the system map and evidence ledger fresh instead of letting continuity fall back to chat scrollback.
|
||||
summary: VoidBot now has a real project-memory spine plus the first legibility-hardening pass on a major runtime organ. The owner Codex lane was split into smaller seams, which lowers local edit risk without changing the behavioral contract. The stack is still dense, but it no longer depends purely on transcript memory or one-file monarchies to stay coherent.
|
||||
next_action: Keep using the canonical state files for re-entry, then continue decomposing the next oversized organ with the clearest concern boundaries, likely the Discord bot entrypoint or the interaction-memory policy slab.
|
||||
|
|
|
|||
Loading…
Reference in a new issue