|
| 1 | +/** |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { googleAI } from '@genkit-ai/google-genai'; |
| 18 | +import * as fs from 'fs'; |
| 19 | +import { |
| 20 | + genkit, |
| 21 | + restartTool, |
| 22 | + type GenerateResponse, |
| 23 | + type MessageData, |
| 24 | + type ToolRequestPart, |
| 25 | +} from 'genkit'; |
| 26 | +import * as path from 'path'; |
| 27 | +import * as readline from 'readline'; |
| 28 | +import { filesystem, retry, skills, toolApproval } from '../src/index.js'; |
| 29 | + |
| 30 | +const rl = readline.createInterface({ |
| 31 | + input: process.stdin, |
| 32 | + output: process.stdout, |
| 33 | +}); |
| 34 | + |
| 35 | +function askQuestion(query: string): Promise<string> { |
| 36 | + return new Promise((resolve) => rl.question(query, resolve)); |
| 37 | +} |
| 38 | + |
| 39 | +async function main() { |
| 40 | + const ai = genkit({ |
| 41 | + plugins: [ |
| 42 | + googleAI(), |
| 43 | + filesystem.plugin(), |
| 44 | + skills.plugin(), |
| 45 | + toolApproval.plugin(), |
| 46 | + retry.plugin(), |
| 47 | + ], |
| 48 | + }); |
| 49 | + |
| 50 | + const currentDir = process.cwd(); |
| 51 | + const fsRoot = path.join(currentDir, 'workspace'); |
| 52 | + const skillsRoot = path.join(currentDir, 'skills'); |
| 53 | + |
| 54 | + // Ensure workspace exists |
| 55 | + if (!fs.existsSync(fsRoot)) { |
| 56 | + fs.mkdirSync(fsRoot, { recursive: true }); |
| 57 | + } |
| 58 | + |
| 59 | + console.log('--- Coding Agent ---'); |
| 60 | + console.log('Type your request. To exit, type "exit".'); |
| 61 | + |
| 62 | + let messages: MessageData[] = [ |
| 63 | + { |
| 64 | + role: 'system', |
| 65 | + content: [ |
| 66 | + { |
| 67 | + text: |
| 68 | + `You are a helpful coding agent. Very terse but thoughtful and careful.\n` + |
| 69 | + `Your working directory is in ${fsRoot}, you are not allowed to access anything outside it.\n` + |
| 70 | + `Use skills. ALWAYS start by analyzing the current state of the workspace, ` + |
| 71 | + `there might be something already there.`, |
| 72 | + }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + ]; |
| 76 | + |
| 77 | + while (true) { |
| 78 | + const input = await askQuestion('\n> '); |
| 79 | + if (input.trim().toLowerCase() === 'exit') { |
| 80 | + break; |
| 81 | + } |
| 82 | + |
| 83 | + try { |
| 84 | + let interruptRestart: ToolRequestPart[] | undefined; |
| 85 | + let response: GenerateResponse; |
| 86 | + |
| 87 | + while (true) { |
| 88 | + response = await ai.generate({ |
| 89 | + model: 'googleai/gemini-flash-latest', |
| 90 | + prompt: interruptRestart ? undefined : input, |
| 91 | + messages: messages, |
| 92 | + resume: interruptRestart ? { restart: interruptRestart } : undefined, |
| 93 | + use: [ |
| 94 | + toolApproval({ |
| 95 | + approved: ['read_file', 'list_files', 'use_skill'], |
| 96 | + }), |
| 97 | + skills({ skillPaths: [skillsRoot] }), |
| 98 | + filesystem({ rootDirectory: fsRoot }), |
| 99 | + ], |
| 100 | + maxTurns: 20, |
| 101 | + }); |
| 102 | + |
| 103 | + if (response.finishReason !== 'interrupted') { |
| 104 | + break; |
| 105 | + } |
| 106 | + |
| 107 | + const interrupts = response.interrupts; |
| 108 | + if (!interrupts || interrupts.length === 0) { |
| 109 | + console.log('Interrupted but no interrupt record found.'); |
| 110 | + break; |
| 111 | + } |
| 112 | + |
| 113 | + const approvedInterrupts: ToolRequestPart[] = []; |
| 114 | + for (const interrupt of interrupts) { |
| 115 | + console.log('\n*** Tool Approval Required ***'); |
| 116 | + console.log(`Tool: ${interrupt.toolRequest.name}`); |
| 117 | + console.log(`Input: ${JSON.stringify(interrupt.toolRequest.input)}`); |
| 118 | + |
| 119 | + const approval = await askQuestion('Approve? (y/N): '); |
| 120 | + if (approval.trim().toLowerCase() === 'y') { |
| 121 | + approvedInterrupts.push( |
| 122 | + restartTool(interrupt, { toolApproved: true }) |
| 123 | + ); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if (approvedInterrupts.length > 0) { |
| 128 | + console.log('Resuming...'); |
| 129 | + interruptRestart = approvedInterrupts; |
| 130 | + messages = response.messages; |
| 131 | + } else { |
| 132 | + console.log('Tool denied.'); |
| 133 | + break; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + console.log(`\nAI Response:\n${response.text}`); |
| 138 | + messages = response.messages; |
| 139 | + } catch (e) { |
| 140 | + console.error('Error during generation:', e); |
| 141 | + } |
| 142 | + } |
| 143 | + rl.close(); |
| 144 | +} |
| 145 | + |
| 146 | +main(); |
0 commit comments