Repository navigation
fix(cli): add --data-dir flag + AGENTMEMORY_DATA_DIR so engine state lives outside repos #314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f06ea0e
fix(cli): add --data-dir flag + AGENTMEMORY_DATA_DIR so engine state …
mvanhorn ef668eb
feat(cli): adopt legacy ./data stores before platform-default data dir
mvanhorn 6c76a62
docs(skills): regenerate REFERENCE.md to include AGENTMEMORY_DATA_DIR
mvanhorn 4a6067d
docs: fix the local-models anchor in the provider table
mvanhorn 9f5f449
fix: narrow legacy data adoption, XDG relocation, and env export
mvanhorn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import { existsSync } from "node:fs"; | ||
| import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path"; | ||
| import { homedir, platform } from "node:os"; | ||
|
|
||
| export type DataDirSource = "flag" | "env" | "default"; | ||
|
|
||
| export type ResolvedDataDir = { | ||
| dataDir: string; | ||
| source: DataDirSource; | ||
| relocatedFrom?: string; | ||
| }; | ||
|
|
||
| type ResolveDataDirOptions = { | ||
| args?: string[]; | ||
| env?: NodeJS.ProcessEnv; | ||
| cwd?: string; | ||
| home?: string; | ||
| platform?: NodeJS.Platform; | ||
| }; | ||
|
|
||
| function argValue(args: string[], name: string): string | undefined { | ||
| const equals = `${name}=`; | ||
| const inline = args.find((arg) => arg.startsWith(equals)); | ||
| if (inline) return inline.slice(equals.length); | ||
|
|
||
| const idx = args.indexOf(name); | ||
| if (idx !== -1) return args[idx + 1]; | ||
| return undefined; | ||
| } | ||
|
|
||
| function expandHome(pathValue: string, home: string): string { | ||
| if (pathValue === "~") return home; | ||
| if (pathValue.startsWith("~/") || pathValue.startsWith("~\\")) { | ||
| return join(home, pathValue.slice(2)); | ||
| } | ||
| return pathValue; | ||
| } | ||
|
|
||
| function absoluteDataDir(pathValue: string, cwd: string, home: string): string { | ||
| const expanded = expandHome(pathValue, home); | ||
| return isAbsolute(expanded) ? expanded : resolve(cwd, expanded); | ||
| } | ||
|
|
||
| function platformDefaultDataDir( | ||
| env: NodeJS.ProcessEnv, | ||
| home: string, | ||
| nodePlatform: NodeJS.Platform, | ||
| useXdg = true, | ||
| ): string { | ||
| if (nodePlatform === "darwin") { | ||
| return join(home, "Library", "Application Support", "agentmemory"); | ||
| } | ||
| if (nodePlatform === "win32") { | ||
| const appData = env["APPDATA"]; | ||
| return appData | ||
| ? join(appData, "agentmemory") | ||
| : join(home, ".agentmemory"); | ||
| } | ||
| const xdgDataHome = env["XDG_DATA_HOME"]; | ||
| if (useXdg && xdgDataHome && isAbsolute(xdgDataHome)) { | ||
| return join(xdgDataHome, "agentmemory"); | ||
| } | ||
| return join(home, ".local", "share", "agentmemory"); | ||
| } | ||
|
|
||
| function nearestGitParent(pathValue: string): string | undefined { | ||
| let current = pathValue; | ||
| while (true) { | ||
| if (existsSync(join(current, ".git"))) return current; | ||
| const parent = dirname(current); | ||
| if (parent === current) return undefined; | ||
| current = parent; | ||
| } | ||
| } | ||
|
|
||
| function isWithin(parent: string, child: string): boolean { | ||
| const pathFromParent = relative(parent, child); | ||
| return ( | ||
| pathFromParent === "" || | ||
| (pathFromParent !== ".." && | ||
| !pathFromParent.startsWith(`..${sep}`) && | ||
| !isAbsolute(pathFromParent)) | ||
| ); | ||
| } | ||
|
|
||
| export function resolveDataDir(options: ResolveDataDirOptions = {}): ResolvedDataDir { | ||
| const args = options.args ?? process.argv.slice(2); | ||
| const env = options.env ?? process.env; | ||
| const cwd = options.cwd ?? process.cwd(); | ||
| const home = options.home ?? homedir(); | ||
| const nodePlatform = options.platform ?? platform(); | ||
| const parsedInstance = parseInt(argValue(args, "--instance") ?? "0", 10); | ||
| const instance = | ||
| parsedInstance > 0 && parsedInstance <= 50 ? parsedInstance : 0; | ||
| const forInstance = (dataDir: string) => | ||
| instance ? join(dataDir, `instance-${instance}`) : dataDir; | ||
|
|
||
| const flagValue = argValue(args, "--data-dir"); | ||
| if (flagValue) { | ||
| return { | ||
| dataDir: forInstance(absoluteDataDir(flagValue, cwd, home)), | ||
| source: "flag", | ||
| }; | ||
| } | ||
|
|
||
| const envValue = env["AGENTMEMORY_DATA_DIR"]; | ||
| if (envValue) { | ||
| return { | ||
| dataDir: forInstance(absoluteDataDir(envValue, cwd, home)), | ||
| source: "env", | ||
| }; | ||
| } | ||
|
|
||
| const legacyDir = resolve(cwd, "data"); | ||
| const hasLegacyStore = | ||
| existsSync(join(legacyDir, "state_store.db")) || | ||
| existsSync(join(legacyDir, "iii-config.yaml")); | ||
| if (instance === 0 && hasLegacyStore) { | ||
| return { dataDir: legacyDir, source: "default" }; | ||
| } | ||
|
|
||
| const defaultDir = platformDefaultDataDir(env, home, nodePlatform); | ||
| const gitParent = nearestGitParent(cwd); | ||
| if (gitParent && isWithin(gitParent, defaultDir)) { | ||
| const relocated = platformDefaultDataDir(env, home, nodePlatform, false); | ||
| if (relocated !== defaultDir) { | ||
| return { | ||
| dataDir: forInstance(relocated), | ||
| source: "default", | ||
| relocatedFrom: defaultDir, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| dataDir: forInstance(defaultDir), | ||
| source: "default", | ||
| }; | ||
| } | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.