Skip to content

Commit

Permalink
♻️ Refactor CLI handling for empty staged changes and model selection
Browse files Browse the repository at this point in the history
  • Loading branch information
segersniels committed Apr 5, 2024
1 parent 88bfd43 commit 941a56e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
5 changes: 5 additions & 0 deletions apps/cli/src/git.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ pub fn getStagedChanges(allocator: std.mem.Allocator) ![]u8 {
"--cached",
} });

if (result.stdout.len == 0) {
try std.io.getStdOut().writer().print("No changes to commit\n", .{});
std.process.exit(0);
}

return result.stdout;
}

Expand Down
12 changes: 1 addition & 11 deletions apps/cli/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const allocator = arena.allocator();

var config = struct {
model: []const u8 = "gpt-4-turbo-preview",
model: []const u8 = openai.Model.Default.toString(),
}{};

var model = cli.Option{
Expand All @@ -19,23 +19,13 @@ var model = cli.Option{

fn generate_handler() !void {
const diff = try git.getStagedChanges(allocator);
if (diff.len == 0) {
try std.io.getStdOut().writer().print("No changes to commit\n", .{});
return;
}

const response = try openai.getCompletion(allocator, diff, config.model);

try std.io.getStdOut().writer().print("{s}\n", .{response.choices[0].message.content});
}

fn commit_handler() !void {
const diff = try git.getStagedChanges(allocator);
if (diff.len == 0) {
try std.io.getStdOut().writer().print("No changes to commit\n", .{});
return;
}

const response = try openai.getCompletion(allocator, diff, config.model);
const message = response.choices[0].message.content;

Expand Down
23 changes: 23 additions & 0 deletions apps/cli/src/openai.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ const CompletionResponse = struct {
choices: []Choice,
};

pub const Model = enum(u2) {
Fast,
Default,

/// Returns the string representation of the enum value
pub fn toString(self: Model) []const u8 {
return switch (self) {
.Fast => "gpt-3.5-turbo",
.Default => "gpt-4-turbo-preview",
};
}

/// Checks whether the provided model is supported
pub fn isSupported(model: []const u8) bool {
return std.mem.eql(u8, model, Model.Fast.toString()) or std.mem.eql(u8, model, Model.Default.toString());
}
};

const SYSTEM_MESSAGE =
\\ You are a helpful coding assistant responsible for generating fitting commit messages.
\\ You will be provided a git diff or code snippet and you are expected to provide a suitable commit message.
Expand Down Expand Up @@ -48,6 +66,11 @@ const SYSTEM_MESSAGE =
;

pub fn getCompletion(allocator: std.mem.Allocator, prompt: []const u8, model: []const u8) !CompletionResponse {
if (!Model.isSupported(model)) {
std.debug.print("Unsupported model provided. If you believe the model should be supported, report an issue at https://github.com/segersniels/genmoji/issues.\n", .{});
std.process.exit(1);
}

var env = try std.process.getEnvMap(allocator);
defer env.deinit();

Expand Down

0 comments on commit 941a56e

Please sign in to comment.