Last active
December 17, 2025 13:26
-
-
Save james-world/857409d64bccb6c4f992a406250361d1 to your computer and use it in GitHub Desktop.
This is a single-file application of a functional coding-agent (like e.g. Claude Code) using Microsoft Agent Framework in just 99 lines of code. See comments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/local/share/dotnet/dotnet run | |
| #:package Microsoft.Agents.AI.OpenAI@1.0.0-preview.251113.1 | |
| #:package OpenAI@2.7.0 | |
| using OpenAI; | |
| using Microsoft.Extensions.AI; | |
| using System.ComponentModel; | |
| using static System.Console; | |
| var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY"); | |
| var model = "gpt-4o-mini"; | |
| var openAIClient = new OpenAIClient(openAIKey); | |
| var agent = openAIClient | |
| .GetChatClient(model) | |
| .CreateAIAgent( | |
| instructions: "You are a helpful coding assistant.", | |
| tools: | |
| [ | |
| AIFunctionFactory.Create(ReadFile), | |
| AIFunctionFactory.Create(ListFiles), | |
| AIFunctionFactory.Create(EditFile) | |
| ] | |
| ); | |
| WriteLine("Chat with OpenAI Agent (Ctrl-C to quit)"); | |
| var thread = agent.GetNewThread(); | |
| while (true) | |
| { | |
| ForegroundColor = ConsoleColor.Blue; | |
| Write("You: "); | |
| ResetColor(); | |
| var user = ReadLine(); | |
| if (string.IsNullOrWhiteSpace(user)) break; | |
| try | |
| { | |
| var result = await agent.RunAsync(user, thread); | |
| ForegroundColor = ConsoleColor.Green; | |
| WriteLine(result?.ToString() ?? string.Empty); | |
| } | |
| catch (Exception ex) | |
| { | |
| ForegroundColor = ConsoleColor.Red; | |
| WriteLine($"Error: {ex.Message}"); | |
| } | |
| finally | |
| { | |
| ResetColor(); | |
| } | |
| } | |
| [Description("Read the contents of a given relative file path. Use this when you want to see what's inside a file. Do not use this with directory names.")] | |
| static string ReadFile( | |
| [Description("The relative path of a file in the working directory.")] string path) | |
| { | |
| if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("path required"); | |
| return File.ReadAllText(path); | |
| } | |
| [Description("List the files and directories at a given path. If no path is provided, list the files in the current directory.")] | |
| static string ListFiles( | |
| [Description("Optional relative path to list files from. Defaults to current directory if not provided.")] string? path = null) | |
| { | |
| var dir = string.IsNullOrWhiteSpace(path) ? "." : path; | |
| if (!Directory.Exists(dir)) throw new DirectoryNotFoundException(dir); | |
| var entries = Directory.EnumerateFileSystemEntries(dir) | |
| .Select(e => Path.GetFileName(e) + (Directory.Exists(e) ? "/" : "")); | |
| return "[" + string.Join(", ", entries) + "]"; | |
| } | |
| [Description("Make edits to a text file. Replaces old_str with new_str in the given file. If file doesn't exist and old_str is empty, creates file with new_str.")] | |
| static string EditFile( | |
| [Description("The path to the file.")] string path, | |
| [Description("Text to search for - must match exactly.")] string old_str, | |
| [Description("Text to replace old_str with.")] string new_str) | |
| { | |
| if (string.IsNullOrWhiteSpace(path) || old_str == new_str) | |
| throw new ArgumentException("invalid input parameters"); | |
| if (!File.Exists(path)) | |
| { | |
| if (string.IsNullOrEmpty(old_str)) | |
| { | |
| var dir = Path.GetDirectoryName(path); | |
| if (!string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir); | |
| File.WriteAllText(path, new_str); | |
| return $"Successfully created file {path}"; | |
| } | |
| throw new FileNotFoundException(path); | |
| } | |
| var content = File.ReadAllText(path); | |
| var replaced = content.Replace(old_str, new_str); | |
| if (replaced == content && old_str != string.Empty) | |
| throw new InvalidOperationException("old_str not found in file"); | |
| File.WriteAllText(path, replaced); | |
| return "OK"; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OPENAI_API_KEYchmod +x codingagent.cson Mac or Linux and execute with the filename alone, or on any platform use,dotnet codingagent.cs