|
| 1 | +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +import { readEnv } from './util'; |
| 4 | + |
| 5 | +const INSTRUCTIONS_CACHE_TTL_MS = 15 * 60 * 1000; // 15 minutes |
| 6 | + |
| 7 | +interface InstructionsCacheEntry { |
| 8 | + fetchedInstructions: string; |
| 9 | + fetchedAt: number; |
| 10 | +} |
| 11 | + |
| 12 | +const instructionsCache = new Map<string, InstructionsCacheEntry>(); |
| 13 | + |
| 14 | +// Periodically evict stale entries so the cache doesn't grow unboundedly. |
| 15 | +const _cacheCleanupInterval = setInterval(() => { |
| 16 | + const now = Date.now(); |
| 17 | + for (const [key, entry] of instructionsCache) { |
| 18 | + if (now - entry.fetchedAt > INSTRUCTIONS_CACHE_TTL_MS) { |
| 19 | + instructionsCache.delete(key); |
| 20 | + } |
| 21 | + } |
| 22 | +}, INSTRUCTIONS_CACHE_TTL_MS); |
| 23 | + |
| 24 | +// Don't keep the process alive just for cleanup. |
| 25 | +_cacheCleanupInterval.unref(); |
| 26 | + |
| 27 | +export async function getInstructions(stainlessApiKey: string | undefined): Promise<string> { |
| 28 | + const cacheKey = stainlessApiKey ?? ''; |
| 29 | + const cached = instructionsCache.get(cacheKey); |
| 30 | + |
| 31 | + if (cached && Date.now() - cached.fetchedAt <= INSTRUCTIONS_CACHE_TTL_MS) { |
| 32 | + return cached.fetchedInstructions; |
| 33 | + } |
| 34 | + |
| 35 | + const fetchedInstructions = await fetchLatestInstructions(stainlessApiKey); |
| 36 | + instructionsCache.set(cacheKey, { fetchedInstructions, fetchedAt: Date.now() }); |
| 37 | + return fetchedInstructions; |
| 38 | +} |
| 39 | + |
| 40 | +async function fetchLatestInstructions(stainlessApiKey: string | undefined): Promise<string> { |
| 41 | + // Setting the stainless API key is optional, but may be required |
| 42 | + // to authenticate requests to the Stainless API. |
| 43 | + const response = await fetch( |
| 44 | + readEnv('CODE_MODE_INSTRUCTIONS_URL') ?? 'https://api.stainless.com/api/ai/instructions/finch', |
| 45 | + { |
| 46 | + method: 'GET', |
| 47 | + headers: { ...(stainlessApiKey && { Authorization: stainlessApiKey }) }, |
| 48 | + }, |
| 49 | + ); |
| 50 | + |
| 51 | + let instructions: string | undefined; |
| 52 | + if (!response.ok) { |
| 53 | + console.warn( |
| 54 | + 'Warning: failed to retrieve MCP server instructions. Proceeding with default instructions...', |
| 55 | + ); |
| 56 | + |
| 57 | + instructions = ` |
| 58 | + This is the finch MCP server. You will use Code Mode to help the user perform |
| 59 | + actions. You can use search_docs tool to learn about how to take action with this server. Then, |
| 60 | + you will write TypeScript code using the execute tool take action. It is CRITICAL that you be |
| 61 | + thoughtful and deliberate when executing code. Always try to entirely solve the problem in code |
| 62 | + block: it can be as long as you need to get the job done! |
| 63 | + `; |
| 64 | + } |
| 65 | + |
| 66 | + instructions ??= ((await response.json()) as { instructions: string }).instructions; |
| 67 | + instructions = ` |
| 68 | + If needed, you can get the current time by executing Date.now(). |
| 69 | +
|
| 70 | + ${instructions} |
| 71 | + `; |
| 72 | + |
| 73 | + return instructions; |
| 74 | +} |
0 commit comments