From 8e499496cc52b2a5b1914c10c312559dc73f390d Mon Sep 17 00:00:00 2001 From: codecrafters-bot Date: Thu, 19 Feb 2026 14:44:46 +0000 Subject: init [skip ci] --- .codecrafters/compile.sh | 11 +++++++ .codecrafters/run.sh | 11 +++++++ .gitattributes | 1 + .gitignore | 18 +++++++++++ README.md | 34 +++++++++++++++++++++ build.zig | 34 +++++++++++++++++++++ build.zig.zon | 68 +++++++++++++++++++++++++++++++++++++++++ codecrafters.yml | 11 +++++++ src/main.zig | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ your_program.sh | 24 +++++++++++++++ 10 files changed, 291 insertions(+) create mode 100755 .codecrafters/compile.sh create mode 100755 .codecrafters/run.sh create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 README.md create mode 100644 build.zig create mode 100644 build.zig.zon create mode 100644 codecrafters.yml create mode 100644 src/main.zig create mode 100755 your_program.sh diff --git a/.codecrafters/compile.sh b/.codecrafters/compile.sh new file mode 100755 index 0000000..45fdad2 --- /dev/null +++ b/.codecrafters/compile.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to compile your program on CodeCrafters +# +# This runs before .codecrafters/run.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +zig build --prefix /tmp/codecrafters-build-claude-code-zig diff --git a/.codecrafters/run.sh b/.codecrafters/run.sh new file mode 100755 index 0000000..fa7c869 --- /dev/null +++ b/.codecrafters/run.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to run your program on CodeCrafters +# +# This runs after .codecrafters/compile.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +exec /tmp/codecrafters-build-claude-code-zig/bin/main "$@" diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..2125666 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b7bb740 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +# Zig build artifacts +main +zig-cache/ +.zig-cache/ +zig-out/ + +# Compiled binary output +bin/ +!bin/.gitkeep + +# Ignore any .o or .h files generated during build +*.o +*.h + +# Ignore OS and editor specific files +**/.DS_Store +*.swp +*~ diff --git a/README.md b/README.md new file mode 100644 index 0000000..9245763 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +[![progress-banner](https://backend.codecrafters.io/progress/claude-code/f722fdbd-6b3b-465b-aa09-ade680ac88d7)](https://app.codecrafters.io/users/codecrafters-bot?r=2qF) + +This is a starting point for Zig solutions to the +["Build Your own Claude Code" Challenge](https://codecrafters.io/challenges/claude-code). + +Claude Code is an AI coding assistant that uses Large Language Models (LLMs) to +understand code and perform actions through tool calls. In this challenge, +you'll build your own Claude Code from scratch by implementing an LLM-powered +coding assistant. + +Along the way you'll learn about HTTP RESTful APIs, OpenAI-compatible tool +calling, agent loop, and how to integrate multiple tools into an AI assistant. + +**Note**: If you're viewing this repo on GitHub, head over to +[codecrafters.io](https://codecrafters.io) to try the challenge. + +# Passing the first stage + +The entry point for your `claude-code` implementation is in `src/main.zig`. +Study and uncomment the relevant code, and submit to pass the first stage: + +```sh +codecrafters submit +``` + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +1. Ensure you have `zig (0.15)` installed locally. +2. Run `./your_program.sh` to run your program, which is implemented in + `src/main.zig`. +3. Run `codecrafters submit` to submit your solution to CodeCrafters. Test + output will be streamed to your terminal. diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..0eccf2a --- /dev/null +++ b/build.zig @@ -0,0 +1,34 @@ +const std = @import("std"); + +// Learn more about this file here: https://ziglang.org/learn/build-system +pub fn build(b: *std.Build) void { + const exe = b.addExecutable(.{ + .name = "main", + .root_module = b.createModule(.{ + .root_source_file = b.path("src/main.zig"), + .target = b.graph.host, + }), + }); + + // This declares intent for the executable to be installed into the + // standard location when the user invokes the "install" step (the default + // step when running `zig build`). + b.installArtifact(exe); + + // This *creates* a Run step in the build graph, to be executed when another + // step is evaluated that depends on it. The next line below will establish + // such a dependency. + const run_cmd = b.addRunArtifact(exe); + + // This creates a build step. It will be visible in the `zig build --help` menu, + // and can be selected like this: `zig build run` + // This will evaluate the `run` step rather than the default, which is "install". + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); + + // This allows the user to pass arguments to the application in the build + // command itself, like this: `zig build run -- arg1 arg2 etc` + if (b.args) |args| { + run_cmd.addArgs(args); + } +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..a692081 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,68 @@ +.{ + .name = .codecrafters_claude_code, + .fingerprint = 0xacb6193408117c41, + + // This is a [Semantic Version](https://semver.org/). + // In a future version of Zig it will be used for package deduplication. + .version = "0.0.0", + + // Tracks the earliest Zig version that the package considers to be a + // supported use case. + .minimum_zig_version = "0.15.1", + + // This field is optional. + // Each dependency must either provide a `url` and `hash`, or a `path`. + // `zig build --fetch` can be used to fetch all dependencies of a package, recursively. + // Once all dependencies are fetched, `zig build` no longer requires + // internet connectivity. + .dependencies = .{ + // See `zig fetch --save ` for a command-line interface for adding dependencies. + //.example = .{ + // // When updating this field to a new URL, be sure to delete the corresponding + // // `hash`, otherwise you are communicating that you expect to find the old hash at + // // the new URL. + // .url = "https://example.com/foo.tar.gz", + // + // // This is computed from the file contents of the directory of files that is + // // obtained after fetching `url` and applying the inclusion rules given by + // // `paths`. + // // + // // This field is the source of truth; packages do not come from a `url`; they + // // come from a `hash`. `url` is just one of many possible mirrors for how to + // // obtain a package matching this `hash`. + // // + // // Uses the [multihash](https://multiformats.io/multihash/) format. + // .hash = "...", + // + // // When this is provided, the package is found in a directory relative to the + // // build root. In this case the package's hash is irrelevant and therefore not + // // computed. This field and `url` are mutually exclusive. + // .path = "foo", + + // // When this is set to `true`, a package is declared to be lazily + // // fetched. This makes the dependency only get fetched if it is + // // actually used. + // .lazy = false, + //}, + }, + + // Specifies the set of files and directories that are included in this package. + // Only files and directories listed here are included in the `hash` that + // is computed for this package. + // Paths are relative to the build root. Use the empty string (`""`) to refer to + // the build root itself. + // A directory listed here means that all files within, recursively, are included. + .paths = .{ + // This makes *all* files, recursively, included in this package. It is generally + // better to explicitly list the files and directories instead, to insure that + // fetching from tarballs, file system paths, and version control all result + // in the same contents hash. + "", + // For example... + //"build.zig", + //"build.zig.zon", + //"src", + //"LICENSE", + //"README.md", + }, +} diff --git a/codecrafters.yml b/codecrafters.yml new file mode 100644 index 0000000..c42bca0 --- /dev/null +++ b/codecrafters.yml @@ -0,0 +1,11 @@ +# Set this to true if you want debug logs. +# +# These can be VERY verbose, so we suggest turning them off +# unless you really need them. +debug: false + +# Use this to change the Zig version used to run your code +# on Codecrafters. +# +# Available versions: zig-0.15.2 +buildpack: zig-0.15.2 \ No newline at end of file diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..8ebb45c --- /dev/null +++ b/src/main.zig @@ -0,0 +1,79 @@ +const std = @import("std"); + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const allocator = gpa.allocator(); + + // Parse -p flag + const args = try std.process.argsAlloc(allocator); + defer std.process.argsFree(allocator, args); + + var prompt: ?[]const u8 = null; + var i: usize = 1; + while (i < args.len) : (i += 1) { + if (std.mem.eql(u8, args[i], "-p") and i + 1 < args.len) { + i += 1; + prompt = args[i]; + } + } + + const prompt_str = prompt orelse @panic("Prompt must not be empty"); + + const api_key = std.posix.getenv("OPENROUTER_API_KEY") orelse @panic("OPENROUTER_API_KEY is not set"); + const base_url = std.posix.getenv("OPENROUTER_BASE_URL") orelse "https://openrouter.ai/api/v1"; + + // Build request body + var body_out: std.io.Writer.Allocating = .init(allocator); + defer body_out.deinit(); + var jw: std.json.Stringify = .{ .writer = &body_out.writer }; + try jw.write(.{ + .model = "anthropic/claude-haiku-4.5", + .messages = &[_]struct { role: []const u8, content: []const u8 }{ + .{ .role = "user", .content = prompt_str }, + }, + }); + const body = body_out.written(); + + // Build URL and auth header + const url_str = try std.fmt.allocPrint(allocator, "{s}/chat/completions", .{base_url}); + defer allocator.free(url_str); + + const auth_value = try std.fmt.allocPrint(allocator, "Bearer {s}", .{api_key}); + defer allocator.free(auth_value); + + // Make HTTP request + var client: std.http.Client = .{ .allocator = allocator }; + defer client.deinit(); + + var response_out: std.io.Writer.Allocating = .init(allocator); + defer response_out.deinit(); + + _ = try client.fetch(.{ + .location = .{ .url = url_str }, + .method = .POST, + .payload = body, + .extra_headers = &.{ + .{ .name = "content-type", .value = "application/json" }, + .{ .name = "authorization", .value = auth_value }, + }, + .response_writer = &response_out.writer, + }); + const response_body = response_out.written(); + + // Parse response + const parsed = try std.json.parseFromSlice(std.json.Value, allocator, response_body, .{}); + defer parsed.deinit(); + + const choices = parsed.value.object.get("choices") orelse @panic("No choices in response"); + if (choices.array.items.len == 0) { + @panic("No choices in response"); + } + + // You can use print statements as follows for debugging, they'll be visible when running tests. + std.debug.print("Logs from your program will appear here!\n", .{}); + + // TODO: Uncomment the lines below to pass the first stage + // const content = choices.array.items[0].object.get("message").?.object.get("content").?.string; + // try std.fs.File.stdout().writeAll(content); +} diff --git a/your_program.sh b/your_program.sh new file mode 100755 index 0000000..9a6bb91 --- /dev/null +++ b/your_program.sh @@ -0,0 +1,24 @@ +#!/bin/sh +# +# Use this script to run your program LOCALLY. +# +# Note: Changing this script WILL NOT affect how CodeCrafters runs your program. +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit early if any commands fail + +# Copied from .codecrafters/compile.sh +# +# - Edit this to change how your program compiles locally +# - Edit .codecrafters/compile.sh to change how your program compiles remotely +( + cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory + zig build --prefix /tmp/codecrafters-build-claude-code-zig +) + +# Copied from .codecrafters/run.sh +# +# - Edit this to change how your program runs locally +# - Edit .codecrafters/run.sh to change how your program runs remotely +exec /tmp/codecrafters-build-claude-code-zig/bin/main "$@" -- cgit v1.2.3