-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.lua
More file actions
144 lines (126 loc) · 3.95 KB
/
Copy pathstore.lua
File metadata and controls
144 lines (126 loc) · 3.95 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
local json = require("vendor.json")
local Store = {}
local function _mkdir(path)
os.execute('mkdir -p "' .. path .. '" 2>/dev/null')
end
local function _read_file(path)
local f = io.open(path, "r")
if not f then return nil end
local content = f:read("*a")
f:close()
return content
end
local function _write_file(path, content)
_mkdir(path:match("^(.*)/"))
local f = io.open(path, "w")
if not f then return nil, "cannot write " .. path end
f:write(content)
f:close()
return true
end
local function _merge_config(base, override)
if not override then return base end
for k, v in pairs(override) do
if type(v) == "table" and type(base[k]) == "table" then
_merge_config(base[k], v)
else
base[k] = v
end
end
return base
end
function Store.load_config(project_dir)
local home = os.getenv("HOME") or os.getenv("USERPROFILE") or "/tmp"
local defaults = {
base_url = "https://api.deepseek.com/v1",
model = "deepseek-v4-pro",
api_key = "",
max_steps = 100, -- safety backstop only; loop detection is the real guard
max_tokens = 131072,
request_timeout = 600,
context_limit = 200000,
compact_threshold = 0.85,
-- No permissions default here: tools.lua's DEFAULT_PERMISSIONS is the single
-- source of truth. A config.json may override per-tool levels (applied in
-- Agent:init); absent that, the tool defaults stand.
}
local home_config_path = home .. "/.cluade/config.json"
local home_raw = _read_file(home_config_path)
if home_raw then
local ok, parsed = pcall(json.decode, home_raw)
if ok then _merge_config(defaults, parsed) end
end
local project_config_path = project_dir .. "/.cluade/config.json"
local proj_raw = _read_file(project_config_path)
if proj_raw then
local ok, parsed = pcall(json.decode, proj_raw)
if ok then _merge_config(defaults, parsed) end
end
local env_key = os.getenv("OPENAI_API_KEY") or os.getenv("ANTHROPIC_API_KEY")
if env_key and #env_key > 0 then
defaults.api_key = env_key
end
return defaults
end
function Store.load_session(sessions_dir, id)
local path = sessions_dir .. "/" .. id .. ".json"
local raw = _read_file(path)
if not raw then return nil, "session not found: " .. id end
local ok, parsed = pcall(json.decode, raw)
if not ok then return nil, "corrupt session: " .. parsed end
return parsed
end
function Store.save_session(sessions_dir, id, data)
_mkdir(sessions_dir)
local path = sessions_dir .. "/" .. id .. ".json"
local body = json.encode(data)
return _write_file(path, body)
end
function Store.list_sessions(sessions_dir)
local cmd = 'ls -t "' .. sessions_dir .. '"/*.json 2>/dev/null'
local f = io.popen(cmd)
if not f then return {} end
local sessions = {}
local home = os.getenv("HOME") or os.getenv("USERPROFILE") or "/tmp"
for line in f:lines() do
local id = line:match("([^/]+)%.json$")
if id and id ~= "last_session" then
local raw = _read_file(line)
if raw then
local ok, parsed = pcall(json.decode, raw)
if ok then
sessions[#sessions + 1] = {
id = id,
created = parsed.created or "",
messages = #(parsed.messages or {}),
cwd = parsed.cwd or "",
}
end
end
end
end
f:close()
return sessions
end
function Store.get_last_session_id(home_dir)
local path = home_dir .. "/.cluade/last_session"
local raw = _read_file(path)
return raw and raw:match("^%s*(.-)%s*$")
end
function Store.set_last_session_id(home_dir, id)
_mkdir(home_dir .. "/.cluade")
_write_file(home_dir .. "/.cluade/last_session", id)
end
function Store.make_session_id()
return os.date("%Y%m%d-%H%M%S") .. "-" .. tostring(math.random(1000, 9999))
end
function Store.new_session_data(cwd)
local home = os.getenv("HOME") or os.getenv("USERPROFILE") or "/tmp"
return {
id = Store.make_session_id(),
cwd = cwd,
created = os.date("%Y-%m-%d %H:%M:%S"),
messages = {},
}
end
return Store