Skip to content

Commit 5565648

Browse files
committed
feat(anytype): rewrite memory tools for REST API
- memory-bootstrap.ts: Check Memory type exists via REST - memory-tools.ts: Full CRUD via REST API - writeMemory: Create + PATCH for properties - readMemory: GET object by ID - listMemories: GET with type_key filter - searchMemories: Client-side text search - updateMemory: PATCH name/body/properties - deleteMemory: DELETE object - Removed .bak files and CLI dependency
1 parent 83c6686 commit 5565648

File tree

5 files changed

+422
-847
lines changed

5 files changed

+422
-847
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import type { OpenClawConfig } from "openclaw/plugin-sdk";
2+
import { getAnytypeConfig, isAnytypeEnabled } from "./config-schema.js";
3+
4+
export interface MemoryTypeInfo {
5+
typeKey: string;
6+
typeName: string;
7+
properties: string[];
8+
}
9+
10+
// Memory type key (standard in Anytype)
11+
const MEMORY_TYPE_KEY = "memory";
12+
13+
/**
14+
* Check if Memory type exists in the space
15+
*/
16+
export async function findMemoryType(
17+
cfg: OpenClawConfig
18+
): Promise<MemoryTypeInfo | null> {
19+
const config = getAnytypeConfig(cfg);
20+
21+
if (!isAnytypeEnabled(cfg)) {
22+
return null;
23+
}
24+
25+
if (!config.apiUrl || !config.apiToken || !config.spaceId) {
26+
return null;
27+
}
28+
29+
try {
30+
const response = await fetch(
31+
`${config.apiUrl}/v1/spaces/${config.spaceId}/types`,
32+
{
33+
headers: {
34+
"Authorization": `Bearer ${config.apiToken}`,
35+
},
36+
}
37+
);
38+
39+
if (!response.ok) {
40+
console.error("[anytype:memory] Failed to list types:", response.status);
41+
return null;
42+
}
43+
44+
const data = await response.json() as { data: Array<{ key: string; name: string; properties?: Array<{ key: string }> }> };
45+
const memoryType = data.data?.find(t => t.key === MEMORY_TYPE_KEY);
46+
47+
if (!memoryType) {
48+
return null;
49+
}
50+
51+
return {
52+
typeKey: memoryType.key,
53+
typeName: memoryType.name,
54+
properties: memoryType.properties?.map(p => p.key) || [],
55+
};
56+
} catch (err) {
57+
console.error("[anytype:memory] Error checking memory type:", err);
58+
return null;
59+
}
60+
}
61+
62+
/**
63+
* Bootstrap the memory backend - check if Memory type exists
64+
*
65+
* Note: Creating custom types via REST API requires more fields and
66+
* the Memory type should already exist from the anytype-memory skill.
67+
* This function just validates it's available.
68+
*/
69+
export async function bootstrapMemoryBackend(
70+
cfg: OpenClawConfig
71+
): Promise<MemoryTypeInfo | null> {
72+
const existing = await findMemoryType(cfg);
73+
74+
if (existing) {
75+
console.log("[anytype:memory] ✓ Memory type found:", existing.typeName);
76+
console.log("[anytype:memory] Properties:", existing.properties.join(", "));
77+
return existing;
78+
}
79+
80+
console.warn("[anytype:memory] Memory type not found in space");
81+
console.warn("[anytype:memory] Run 'anytype-memory sync' to create it, or create manually in Anytype");
82+
83+
return null;
84+
}
85+
86+
/**
87+
* Check if memory backend is ready
88+
*/
89+
export async function isMemoryBackendReady(cfg: OpenClawConfig): Promise<boolean> {
90+
const typeInfo = await findMemoryType(cfg);
91+
return typeInfo !== null;
92+
}

extensions/anytype/src/memory-bootstrap.ts.bak

Lines changed: 0 additions & 260 deletions
This file was deleted.

0 commit comments

Comments
 (0)