Repository navigation
Expand file tree
/
Copy pathmemory_save.zig
More file actions
79 lines (75 loc) · 3.66 KB
/
Copy pathmemory_save.zig
File metadata and controls
79 lines (75 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const std = @import("std");
const memory_mod = @import("../state/memory.zig");
const openai = @import("../model/openai.zig");
const Agent = @import("../agent/mod.zig").Agent;
const summary = @import("summary.zig");
const tool = @import("tool.zig");
pub const spec: tool.Tool = .{
.name = "memory_save",
.description = "Save one durable note to memory so it survives this session: add a note, or replace or delete an exact existing one.",
.parameters_json =
\\{"type":"object","properties":{"text":{"type":"string","description":"Note to add, new note for replace, or exact existing note for delete"},"action":{"type":"string","enum":["add","replace","delete"],"description":"Default add; replace and delete require an exact existing note"},"old_text":{"type":"string","description":"Exact existing note to replace; required only for replace"}},"required":["text"]}
,
.label = "saving memory…",
.summary = writeSummary,
.run = run,
};
/// What is being done to memory, and which note: `add` and `delete` are told
/// apart at the first word, and the note itself follows, cut short — it is
/// prose, and a memory line is not the place to read it whole.
fn writeSummary(ctx: tool.Context, w: *std.Io.Writer, arguments: []const u8) !bool {
return summary.writeFields(w, ctx.allocator, arguments, &.{ "action", "text" });
}
fn run(ctx: tool.Context, args: []const u8) !tool.ToolResult {
const agent = ctx.agent orelse return ctx.refuse("Error: this tool requires an agent context.");
return execute(agent, args);
}
pub fn execute(self: *Agent, arguments: []const u8) !tool.ToolResult {
if (!self.memory.enabled) {
return .{
.output = try self.allocator.dupe(u8, "Memory is unavailable: no storage path could be resolved."),
.is_error = true,
};
}
const note = openai.extractStringArg(self.allocator, arguments, "text") catch {
return .{
.output = try self.allocator.dupe(u8, "Error: memory_save needs a non-empty 'text' string"),
.is_error = true,
};
};
defer self.allocator.free(note);
const parsed = try std.json.parseFromSlice(struct {
text: []const u8,
action: []const u8 = "add",
old_text: ?[]const u8 = null,
}, self.allocator, arguments, .{});
defer parsed.deinit();
// Stage edits so persistence failure cannot publish an unsaved note.
var staged = try memory_mod.Memory.initWithPath(self.allocator, self.io, null);
defer staged.deinit();
staged.enabled = true;
if (self.memory.path) |path| staged.path = try self.allocator.dupe(u8, path);
for (self.memory.entries.items) |entry| _ = try staged.add(entry);
const added = staged.edit(parsed.value.action, note, parsed.value.old_text) catch |err| switch (err) {
error.EmptyMemory => return .{
.output = try self.allocator.dupe(u8, "Error: memory_save needs a non-empty 'text' string"),
.is_error = true,
},
else => return .{
.output = try std.fmt.allocPrint(self.allocator, "Memory edit failed: {s}", .{@errorName(err)}),
.is_error = true,
},
};
if (!added) return .{
.output = try std.fmt.allocPrint(self.allocator, "Already in memory: {s}", .{note}),
.is_error = false,
};
staged.save() catch |err| {
return .{ .output = try std.fmt.allocPrint(self.allocator, "Memory save failed: {s}", .{@errorName(err)}), .is_error = true };
};
std.mem.swap(memory_mod.Memory, &self.memory, &staged);
return .{
.output = try std.fmt.allocPrint(self.allocator, "Memory {s}: {s}", .{ parsed.value.action, note }),
.is_error = false,
};
}