|
| 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 | +} |
0 commit comments