import { DynamicContributorContext } from './types.js'; /** * Dynamic Prompt Generators * * This module contains functions for generating dynamic system prompts for the AI agent. * Each function should return a string (or Promise) representing a prompt, possibly using the provided context. * * --- * Guidelines for Adding Prompt Functions: * - Place all dynamic prompt-generating functions in this file. * - Also update the `registry.ts` file to register the new function. * - Use XML tags to indicate the start and end of the dynamic prompt - they are known to improve performance * - Each function should be named clearly to reflect its purpose (e.g., getCurrentDate, getEnvironmentInfo). */ /** * Returns the current date (without time to prevent KV-cache invalidation). */ export async function getCurrentDate(_context: DynamicContributorContext): Promise { const date = new Date().toISOString().split('T')[0]; return `Current date: ${date}`; } /** * Returns environment information to help agents understand their execution context. * This is kept separate from date to optimize caching (env info rarely changes). * * Includes: * - Working directory (cwd) * - Platform (os) * - Whether the cwd is a git repository * - Default shell * * Note: This function uses dynamic imports for Node.js modules to maintain browser compatibility. * In browser environments, it returns a placeholder message. */ export async function getEnvironmentInfo(_context: DynamicContributorContext): Promise { // Check if we're in a Node.js environment if (typeof process === 'undefined' || !process.cwd) { return 'Environment info not available in browser context'; } try { // Dynamic imports for Node.js modules (browser-safe) const [{ existsSync }, { platform }, { join }] = await Promise.all([ import('fs'), import('os'), import('path'), ]); const cwd = process.cwd(); const os = platform(); const isGitRepo = existsSync(join(cwd, '.git')); const shell = process.env.SHELL || (os === 'win32' ? 'cmd.exe' : '/bin/sh'); return ` ${cwd} ${os} ${isGitRepo} ${shell} `; } catch { return 'Environment info not available'; } } // TODO: This needs to be optimized to only fetch resources when needed. Currently this runs every time the prompt is generated. export async function getResourceData(context: DynamicContributorContext): Promise { const resources = await context.mcpManager.listAllResources(); if (!resources || resources.length === 0) { return ''; } const parts = await Promise.all( resources.map(async (resource) => { try { const response = await context.mcpManager.readResource(resource.key); const first = response?.contents?.[0]; let content: string; if (first && 'text' in first && first.text && typeof first.text === 'string') { content = first.text; } else if ( first && 'blob' in first && first.blob && typeof first.blob === 'string' ) { content = first.blob; } else { content = JSON.stringify(response, null, 2); } const label = resource.summary.name || resource.summary.uri; return `${content}`; } catch (error: any) { return `Error loading resource: ${ error.message || error }`; } }) ); return `\n${parts.join('\n')}\n`; }