Merge remote-tracking branch 'shareai/main' into fix/pr-536-readline-prompt-wrap

# Conflicts:
#	web/src/data/generated/versions.json
This commit is contained in:
Haoran
2026-08-25 21:38:38 +08:00
95 changed files with 1673 additions and 922 deletions

View File

@@ -59,7 +59,9 @@ memory type は四種類ある。
```python
def write_memory_file(name, mem_type, description, body):
path = MEMORY_DIR / f"{memory_slug(name)}.md"
path.write_text(memory_document(name, mem_type, description, body))
path.write_text(
memory_document(name, mem_type, description, body), encoding="utf-8"
)
rebuild_memory_index()
return path
```
@@ -123,7 +125,7 @@ memory ファイルが増えると、重複、矛盾、古い情報が混ざる
```python
snapshot = {
path.name: path.read_text()
path.name: path.read_text(encoding="utf-8")
for path in MEMORY_DIR.glob("*.md")
if path.name != MEMORY_INDEX.name
}
@@ -137,14 +139,14 @@ try:
path.write_text(memory_document(
record["name"], record["type"],
record["description"], record["body"],
))
), encoding="utf-8")
rebuild_memory_index()
except Exception:
for path in MEMORY_DIR.glob("*.md"):
if path.name != MEMORY_INDEX.name:
path.unlink()
for filename, content in snapshot.items():
(MEMORY_DIR / filename).write_text(content)
(MEMORY_DIR / filename).write_text(content, encoding="utf-8")
rebuild_memory_index()
raise
```

View File

@@ -59,7 +59,9 @@ There are four memory types:
```python
def write_memory_file(name, mem_type, description, body):
path = MEMORY_DIR / f"{memory_slug(name)}.md"
path.write_text(memory_document(name, mem_type, description, body))
path.write_text(
memory_document(name, mem_type, description, body), encoding="utf-8"
)
rebuild_memory_index()
return path
```
@@ -123,7 +125,7 @@ The code parses and validates the new list before replacing old files. It snapsh
```python
snapshot = {
path.name: path.read_text()
path.name: path.read_text(encoding="utf-8")
for path in MEMORY_DIR.glob("*.md")
if path.name != MEMORY_INDEX.name
}
@@ -137,14 +139,14 @@ try:
path.write_text(memory_document(
record["name"], record["type"],
record["description"], record["body"],
))
), encoding="utf-8")
rebuild_memory_index()
except Exception:
for path in MEMORY_DIR.glob("*.md"):
if path.name != MEMORY_INDEX.name:
path.unlink()
for filename, content in snapshot.items():
(MEMORY_DIR / filename).write_text(content)
(MEMORY_DIR / filename).write_text(content, encoding="utf-8")
rebuild_memory_index()
raise
```

View File

@@ -59,7 +59,9 @@ User prefers using tabs, not spaces, for indentation.
```python
def write_memory_file(name, mem_type, description, body):
path = MEMORY_DIR / f"{memory_slug(name)}.md"
path.write_text(memory_document(name, mem_type, description, body))
path.write_text(
memory_document(name, mem_type, description, body), encoding="utf-8"
)
rebuild_memory_index()
return path
```
@@ -123,7 +125,7 @@ if not tool_calls:
```python
snapshot = {
path.name: path.read_text()
path.name: path.read_text(encoding="utf-8")
for path in MEMORY_DIR.glob("*.md")
if path.name != MEMORY_INDEX.name
}
@@ -137,14 +139,14 @@ try:
path.write_text(memory_document(
record["name"], record["type"],
record["description"], record["body"],
))
), encoding="utf-8")
rebuild_memory_index()
except Exception:
for path in MEMORY_DIR.glob("*.md"):
if path.name != MEMORY_INDEX.name:
path.unlink()
for filename, content in snapshot.items():
(MEMORY_DIR / filename).write_text(content)
(MEMORY_DIR / filename).write_text(content, encoding="utf-8")
rebuild_memory_index()
raise
```

View File

@@ -156,7 +156,9 @@ def write_memory_file(name: str, mem_type: str, description: str, body: str) ->
MEMORY_DIR.mkdir(parents=True, exist_ok=True)
path = memory_path(f"{memory_slug(name)}.md")
path.write_text(memory_document(name, mem_type, description, body))
path.write_text(
memory_document(name, mem_type, description, body), encoding="utf-8"
)
rebuild_memory_index()
return path
@@ -170,7 +172,7 @@ def rebuild_memory_index() -> None:
path = memory_path(path.name)
except ValueError:
continue
metadata, body = parse_frontmatter(path.read_text())
metadata, body = parse_frontmatter(path.read_text(encoding="utf-8"))
name = " ".join(str(metadata.get("name") or path.stem).split())
first_line = next((line for line in body.splitlines() if line.strip()), "")
description = " ".join(
@@ -178,7 +180,7 @@ def rebuild_memory_index() -> None:
)
lines.append(f"- [{name}]({path.name}) - {description}")
memory_path(MEMORY_INDEX.name, allow_index=True).write_text(
"\n".join(lines) + ("\n" if lines else "")
"\n".join(lines) + ("\n" if lines else ""), encoding="utf-8"
)
def read_memory_index() -> str:
@@ -186,14 +188,14 @@ def read_memory_index() -> str:
path = memory_path(MEMORY_INDEX.name, allow_index=True)
except ValueError:
return ""
return path.read_text().strip() if path.exists() else ""
return path.read_text(encoding="utf-8").strip() if path.exists() else ""
def read_memory_file(filename: str) -> str | None:
try:
path = memory_path(filename)
except ValueError:
return None
return path.read_text() if path.is_file() else None
return path.read_text(encoding="utf-8") if path.is_file() else None
def list_memory_files() -> list[dict]:
records = []
@@ -206,7 +208,7 @@ def list_memory_files() -> list[dict]:
path = memory_path(path.name)
except ValueError:
continue
metadata, body = parse_frontmatter(path.read_text())
metadata, body = parse_frontmatter(path.read_text(encoding="utf-8"))
records.append({
"filename": path.name,
"name": str(metadata.get("name") or path.stem),
@@ -489,7 +491,9 @@ def consolidate_memories() -> int:
)
snapshot = {
record["filename"]: memory_path(record["filename"]).read_text()
record["filename"]: memory_path(record["filename"]).read_text(
encoding="utf-8"
)
for record in records
}
try:
@@ -501,12 +505,15 @@ def consolidate_memories() -> int:
continue
for record in consolidated:
path = memory_path(f"{memory_slug(record['name'])}.md")
path.write_text(memory_document(
record["name"],
record["type"],
record["description"],
record["body"],
))
path.write_text(
memory_document(
record["name"],
record["type"],
record["description"],
record["body"],
),
encoding="utf-8",
)
rebuild_memory_index()
except Exception:
for path in MEMORY_DIR.glob("*.md"):
@@ -516,7 +523,7 @@ def consolidate_memories() -> int:
except ValueError:
continue
for filename, content in snapshot.items():
memory_path(filename).write_text(content)
memory_path(filename).write_text(content, encoding="utf-8")
rebuild_memory_index()
raise
@@ -548,7 +555,7 @@ def run_bash(command: str) -> str:
def run_read(path: str, limit: int | None = None) -> str:
try:
lines = (WORKDIR / path).resolve().read_text().splitlines()
lines = (WORKDIR / path).resolve().read_text(encoding="utf-8").splitlines()
if limit and limit < len(lines):
lines = lines[:limit] + [
f"... ({len(lines) - limit} more lines)"
@@ -561,7 +568,7 @@ def run_write(path: str, content: str) -> str:
try:
file_path = (WORKDIR / path).resolve()
file_path.parent.mkdir(parents=True, exist_ok=True)
file_path.write_text(content)
file_path.write_text(content, encoding="utf-8")
return f"Wrote {len(content)} bytes to {path}"
except Exception as error:
return f"Error: {error}"
@@ -569,22 +576,25 @@ def run_write(path: str, content: str) -> str:
def run_edit(path: str, old_text: str, new_text: str) -> str:
try:
file_path = (WORKDIR / path).resolve()
text = file_path.read_text()
text = file_path.read_text(encoding="utf-8")
if old_text not in text:
return f"Error: text not found in {path}"
file_path.write_text(text.replace(old_text, new_text, 1))
file_path.write_text(text.replace(old_text, new_text, 1), encoding="utf-8")
return f"Edited {path}"
except Exception as error:
return f"Error: {error}"
def run_glob(pattern: str) -> str:
try:
matches = [
matches = sorted({
match
for match in glob.glob(pattern, root_dir=WORKDIR)
for match in glob.glob(pattern, root_dir=WORKDIR, recursive=True)
if (WORKDIR / match).resolve().is_relative_to(WORKDIR)
]
return "\n".join(matches) if matches else "(no matches)"
})
shown = matches[:200]
if len(matches) > 200:
shown.append("... (more matches omitted; narrow the pattern)")
return "\n".join(shown) if shown else "(no matches)"
except Exception as error:
return f"Error: {error}"
@@ -597,7 +607,7 @@ TOOLS = [
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "content": {"type": "string"}}, "required": ["path", "content"]}},
{"name": "edit_file", "description": "Replace exact text in a file once.",
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "old_text": {"type": "string"}, "new_text": {"type": "string"}}, "required": ["path", "old_text", "new_text"]}},
{"name": "glob", "description": "Find files matching a glob pattern.",
{"name": "glob", "description": "Find files matching a glob pattern; ** matches recursively.",
"input_schema": {"type": "object", "properties": {"pattern": {"type": "string"}}, "required": ["pattern"]}},
]