Spaces:
Running
Running
File size: 5,572 Bytes
192cba8 | 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | /**
* CAPT Memory Palace - Real API Integration
*
* Connect to a running CAPT symbiote for real-time cognitive data.
* See DEPLOY.md for server setup instructions.
*/
const CAPT_API = {
endpoint: null,
token: null,
isConnected: false,
lastState: null,
pollInterval: null,
/**
* Configure API connection
*/
configure(options = {}) {
this.endpoint = options.endpoint || null;
this.token = options.token || null;
if (this.endpoint) {
console.log('[CAPT API] Configured:', this.endpoint);
}
},
/**
* Test connection to CAPT server
*/
async test() {
if (!this.endpoint) {
console.warn('[CAPT API] No endpoint configured');
return false;
}
try {
const response = await fetch(`${this.endpoint}/health`, {
headers: this.getHeaders()
});
this.isConnected = response.ok;
console.log('[CAPT API] Connection:', this.isConnected ? 'OK' : 'Failed');
return this.isConnected;
} catch (e) {
console.error('[CAPT API] Connection error:', e.message);
this.isConnected = false;
return false;
}
},
/**
* Get current CAPT state
*/
async getState() {
if (!this.endpoint) return null;
try {
const response = await fetch(`${this.endpoint}/api/state`, {
headers: this.getHeaders()
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();
this.lastState = data;
return data;
} catch (e) {
console.warn('[CAPT API] Get state failed:', e.message);
return null;
}
},
/**
* Submit a query to CAPT
*/
async query(text) {
if (!this.endpoint) {
console.warn('[CAPT API] No endpoint configured');
return null;
}
try {
const response = await fetch(`${this.endpoint}/api/cogitate`, {
method: 'POST',
headers: this.getHeaders(),
body: JSON.stringify({ query: text })
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return await response.json();
} catch (e) {
console.error('[CAPT API] Query failed:', e.message);
return null;
}
},
/**
* Get CAPT eval reports
*/
async getEvals() {
if (!this.endpoint) return null;
try {
const response = await fetch(`${this.endpoint}/api/evals`, {
headers: this.getHeaders()
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return await response.json();
} catch (e) {
console.warn('[CAPT API] Get evals failed:', e.message);
return null;
}
},
/**
* Get knowledge status
*/
async getKnowledge() {
if (!this.endpoint) return null;
try {
const response = await fetch(`${this.endpoint}/api/knowledge`, {
headers: this.getHeaders()
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return await response.json();
} catch (e) {
console.warn('[CAPT API] Get knowledge failed:', e.message);
return null;
}
},
/**
* Start polling for state updates
*/
startPolling(callback, interval = 2000) {
if (this.pollInterval) {
this.stopPolling();
}
const poll = async () => {
const state = await this.getState();
if (state && callback) {
callback(state);
}
};
poll();
this.pollInterval = setInterval(poll, interval);
},
/**
* Stop polling
*/
stopPolling() {
if (this.pollInterval) {
clearInterval(this.pollInterval);
this.pollInterval = null;
}
},
/**
* Get authorization headers
*/
getHeaders() {
const headers = {
'Content-Type': 'application/json'
};
if (this.token) {
headers['Authorization'] = `Bearer ${this.token}`;
}
return headers;
},
/**
* Parse CAPT state into palace format
*/
parseState(captState) {
if (!captState) return null;
// Extract rooms from modules
const modules = captState.modules || [];
const rooms = modules.map(m => ({
id: m.id || m.name,
label: m.label || m.id || m.name,
type: this.getModuleType(m.id || m.name),
color: this.getModuleColor(m.id || m.name),
capacity: Math.round((m.activation || 0.5) * 100),
position: {
x: m.position?.x || Math.random() * 10 - 5,
y: (m.activation || 0.5) * 3,
z: m.position?.z || Math.random() * 10 - 5
}
}));
return {
sweep: captState.sweep,
modules: rooms,
input: captState.input,
output: captState.output,
timestamp: Date.now()
};
},
/**
* Get module type from ID
*/
getModuleType(id) {
const types = {
PULSE: 'input',
NEDA: 'processing',
HMC: 'memory',
CIG: 'processing',
HDR: 'processing',
QIPC: 'quorum',
ECHO: 'memory',
META: 'processing',
IMMU: 'validation',
NDS: 'output'
};
return types[id] || 'processing';
},
/**
* Get module color from ID
*/
getModuleColor(id) {
const colors = {
PULSE: '#8b5cf6',
NEDA: '#a855f7',
HMC: '#c084fc',
CIG: '#d946ef',
HDR: '#e879f9',
QIPC: '#f472b6',
ECHO: '#fb7185',
META: '#f43f5e',
IMMU: '#10b981',
NDS: '#06b6d4'
};
return colors[id] || '#8b5cf6';
}
};
// Export
window.CAPT_API = CAPT_API;
|