Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194/** Advisor tool registration and ledger-driven execution. @module */import type { AgentToolResult, ExtensionContext, Theme, ToolRenderResultOptions,} from '@earendil-works/pi-coding-agent';import type { ReadonlyDeep, } from 'type-fest';import type { ForeignHostCapability, } from '@monochromatic-dev/ownership-marker-foreign-borrowed/ts';import { ADVISOR_TOOL_NAME, } from './constants.ts';import { AdvisorOperationError, } from './operation-error.ts';import { formatAdvisorProgress, } from './operation-progress.ts';import type { AdvisorOperationSnapshot, } from './operation-types.ts';import { renderAdvisorCall, renderAdvisorResult,} from './rendering.ts';import { runAdvisor, } from './run-advisor.ts';import { AdvisorToolParametersSchema, prepareAdvisorArguments,} from './tool-params.ts';import type { AdvisorConfig, AdvisorDetails, AdvisorProgressDetails, AdvisorToolDefinition, AdvisorToolResult,} from './types.ts';
export { runAdvisor, } from './run-advisor.ts';
/** Runtime state and persistence boundaries supplied by the extension. */export type CreateAdvisorToolOptions = { /** Runtime config snapshot. */ readonly getConfig: () => AdvisorConfig; /** Session enablement. */ readonly getSessionEnabled: () => boolean; /** Current Pi-loaded project context. */ readonly getProjectContext: () => string; /** Preserve accounting before throwing through the host's error boundary. */ readonly onFailure?: (failure: { readonly toolCallId: string; readonly operation: AdvisorOperationSnapshot }) => void;};
/** Host-defined positional progress callback, including absence outside streaming hosts. */type AdvisorUpdateCallback = Parameters<AdvisorToolDefinition<typeof AdvisorToolParametersSchema>['execute']>[3];
/** Register a default fallback tool while preserving exact explicit-model requests. @param toolOptions - session accessors and accounting persistence @returns Pi tool definition @example ```ts pi.registerTool(createAdvisorTool({ getConfig, getSessionEnabled, getProjectContext })); ``` */export function createAdvisorTool(toolOptions: CreateAdvisorToolOptions,): AdvisorToolDefinition<typeof AdvisorToolParametersSchema> { return { name: ADVISOR_TOOL_NAME, label: 'Advisor', description: 'Consult independent scoped reviewer models using the current conversation context. Empty params start with the highest expected-cost eligible non-current model and recover through other scoped models on failure. Optional configured overlap collects completed reviews together after bounded straggler grace. Explicit model requests never switch models.', promptSnippet: 'Consult Advisor with empty params for default recovery, or specify an exact scoped model and optional focus question.', promptGuidelines: [ 'Advisor receives the current serialized conversation and loaded project context automatically.', 'Call Advisor to check flawed assumptions, missing verification, and overlooked files.', 'Advisor may return multiple separately labelled reviews; none carries a quality guarantee.', 'Explicit Advisor model requests remain exact and never fall back to another model.', ], parameters: AdvisorToolParametersSchema, executionMode: 'sequential', prepareArguments: prepareAdvisorArguments, /** Run through one operation and expose bounded metadata-only progress. @param toolCallId - host identity for persisted failure accounting @param params - model and focus question @param signal - caller cancellation @param onUpdate - host progress callback @param ctx - scope, session, and provider capabilities @returns original collected reviews and aggregate available usage @mutates ctx - resolves scope and provider authentication through the host @mutates onUpdate - publishes progress to the host */ async execute( toolCallId: string, params: { readonly model?: string; readonly question?: string }, // oxlint-disable-next-line no-restricted-syntax/no-nullish-union -- Pi dictates this optional positional signal before required host context; intersecting the entire optional type with a marker would erase undefined. signal: ForeignHostCapability<AbortSignal> | undefined, onUpdate: AdvisorUpdateCallback, ctx: ForeignHostCapability<ExtensionContext>, ): Promise<AdvisorToolResult> { if (!toolOptions.getSessionEnabled()) throw new Error('advisor: disabled for this session. Run /advisor on to re-enable.',); try { /** Final reviews and operation metadata. */ const result = await runAdvisor({ ctx, config: toolOptions.getConfig(), projectContext: toolOptions.getProjectContext(), toolCallId, ...(params.model === undefined ? {} : { requestedSlug: params.model, }), ...(params.question === undefined ? {} : { question: params.question, }), ...(signal === undefined ? {} : { signal, }), onUpdate(operation): void { onUpdate?.({ content: [{ type: 'text', text: formatAdvisorProgress({ operation, now: Date.now(), },), },], details: { kind: 'advisor-progress', }, },); }, },); return { content: [{ type: 'text', text: result.text, },], details: result.details, ...(result.details .usage === undefined ? {} : { usage: result.details .usage, }), }; } catch (error) { if (error instanceof AdvisorOperationError) toolOptions.onFailure?.({ toolCallId, operation: error.operation, },); throw error; } }, /** Render the initial requested identity through the host theme. */ renderCall( args: { readonly model?: string; readonly question?: string }, theme: ForeignHostCapability<Theme>, ) { return renderAdvisorCall({ args, theme, },); }, /** Render partial metadata independently from final successful or failed results. */ renderResult( result: ReadonlyDeep<AgentToolResult<AdvisorDetails | AdvisorProgressDetails>>, renderOptions: ReadonlyDeep<ToolRenderResultOptions>, theme: ForeignHostCapability<Theme>, ) { return renderAdvisorResult({ result, expanded: renderOptions.expanded, isPartial: renderOptions.isPartial, theme, },); }, };}