logancyang_obsidian-copilot/scripts/printPromptDebugEntry.ts
Zero Liu 6ca2dc01ea
chore(eslint): enable no-explicit-any; fix ~395 violations (#2452)
* chore(eslint): enable no-explicit-any; fix ~395 violations

Switches @typescript-eslint/no-explicit-any from "off" to "error" and
replaces explicit `any` with proper types or `unknown` + narrowing
across ~100 source files and 15 test files. Eleven eslint-disable
comments remain: one for a BaseLanguageModel prototype patch and ten
for Orama<any> API surfaces where typed alternatives poison Orama's
internal inference to `never`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(scripts): cast chainContext to ChainManager in printPromptDebugEntry

The earlier `as any` → `as unknown` rewrite left buildAgentPromptDebugReport's
chainManager argument typed as `unknown`, which CI's `tsc -noEmit` (without
`--skipLibCheck`) flagged. Restore the cast through the real ChainManager type.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* chore(eslint): fix remaining no-explicit-any and unnecessary-type-assertion errors

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* chore(eslint): drop redundant no-explicit-any rule

Already set to "error" by typescript-eslint/recommendedTypeChecked via
obsidianmd's recommended config.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 02:08:45 -07:00

134 lines
3.9 KiB
TypeScript

import type ChainManager from "@/LLMProviders/chainManager";
import MemoryManager from "@/LLMProviders/memoryManager";
import { ModelAdapterFactory } from "@/LLMProviders/chainRunner/utils/modelAdapter";
import { buildAgentPromptDebugReport } from "@/LLMProviders/chainRunner/utils/promptDebugService";
import { ToolRegistry } from "@/tools/ToolRegistry";
import { ChatMessage } from "@/types/message";
import { initializeBuiltinTools } from "@/tools/builtinTools";
import { getSettings } from "@/settings/model";
import { UserMemoryManager } from "@/memory/UserMemoryManager";
import type { App } from "obsidian";
import type { BaseChatModel } from "@langchain/core/language_models/chat_models";
interface HeadlessApp {
vault: {
getRoot: () => { name: string };
getAbstractFileByPath: (path: string) => null;
read: (file: unknown) => Promise<string>;
getMarkdownFiles: () => unknown[];
getAllLoadedFiles: () => unknown[];
adapter: {
mkdir: (path: string) => Promise<void>;
};
};
metadataCache: {
getFirstLinkpathDest: () => null;
getFileCache: () => null;
};
workspace: {
getActiveFile: () => null;
getLeaf: () => { openFile: () => Promise<void> };
};
}
/**
* Create a minimal Obsidian app stub suitable for CLI usage.
*
* The autonomous agent only needs vault lookups and metadata cache reads, so this
* provides no-op implementations that satisfy those expectations.
*/
function createHeadlessApp(): HeadlessApp {
return {
vault: {
getRoot: () => ({ name: "root" }),
getAbstractFileByPath: () => null,
read: async () => "",
getMarkdownFiles: () => [],
getAllLoadedFiles: () => [],
adapter: {
mkdir: async () => {
/* no-op */
},
},
},
metadataCache: {
getFirstLinkpathDest: () => null,
getFileCache: () => null,
},
workspace: {
getActiveFile: () => null,
getLeaf: () => ({
openFile: async () => {
/* no-op */
},
}),
},
};
}
/**
* Format a plain user message into the ChatMessage shape used by the agent.
*
* @param message - Raw user text to analyse.
* @returns Minimal chat message.
*/
function buildChatMessage(message: string): ChatMessage {
return {
message,
originalMessage: message,
sender: "user",
timestamp: null,
isVisible: true,
};
}
/**
* Generate the annotated prompt debug report for a given user input.
*
* @param args - CLI arguments (expects the user prompt as the concatenated string).
*/
export async function run(args: string[]): Promise<void> {
const userInput = args.join(" ").trim();
if (!userInput) {
console.error('Usage: npm run prompt:debug -- "your message here"');
process.exitCode = 1;
return;
}
const app = createHeadlessApp();
// eslint-disable-next-line obsidianmd/no-global-this -- node-only debug script, no window available
(global as unknown as { app: unknown }).app = app;
initializeBuiltinTools();
const registry = ToolRegistry.getInstance();
const settings = getSettings();
const enabledToolIds = new Set(settings.autonomousAgentEnabledToolIds || []);
const availableTools = registry.getEnabledTools(enabledToolIds, false);
// Generate simple tool descriptions (native tool calling handles schema via bindTools)
const toolDescriptions = availableTools
.map((tool) => `${tool.name}: ${tool.description}`)
.join("\n");
const memoryManager = MemoryManager.getInstance();
const userMemoryManager = new UserMemoryManager(app as unknown as App);
const chainContext = {
memoryManager,
userMemoryManager,
} as unknown as ChainManager;
const adapter = ModelAdapterFactory.createAdapter({
modelName: "gpt-4",
} as unknown as BaseChatModel);
const report = await buildAgentPromptDebugReport({
chainManager: chainContext,
adapter,
availableTools,
toolDescriptions,
userMessage: buildChatMessage(userInput),
});
console.log(report.annotatedPrompt);
}