rust-kgdb 0.5.12 → 0.5.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +145 -0
- package/examples/fraud-detection-agent.js +157 -1
- package/examples/underwriting-agent.js +142 -2
- package/hypermind-agent.js +1199 -1
- package/index.d.ts +735 -0
- package/index.js +24 -0
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -989,3 +989,738 @@ export class AgentBuilder {
|
|
|
989
989
|
*/
|
|
990
990
|
build(): ComposedAgent
|
|
991
991
|
}
|
|
992
|
+
|
|
993
|
+
// ==============================================
|
|
994
|
+
// Memory Layer (v0.5.13+) - GraphDB-Powered Agent Memory
|
|
995
|
+
// ==============================================
|
|
996
|
+
|
|
997
|
+
/**
|
|
998
|
+
* AgentState - Lifecycle states for agent runtime
|
|
999
|
+
*/
|
|
1000
|
+
export const AgentState: {
|
|
1001
|
+
CREATED: 'CREATED'
|
|
1002
|
+
READY: 'READY'
|
|
1003
|
+
RUNNING: 'RUNNING'
|
|
1004
|
+
PAUSED: 'PAUSED'
|
|
1005
|
+
COMPLETED: 'COMPLETED'
|
|
1006
|
+
FAILED: 'FAILED'
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
export type AgentStateType = 'CREATED' | 'READY' | 'RUNNING' | 'PAUSED' | 'COMPLETED' | 'FAILED'
|
|
1010
|
+
|
|
1011
|
+
/**
|
|
1012
|
+
* Memory retrieval weight configuration
|
|
1013
|
+
*/
|
|
1014
|
+
export interface RetrievalWeights {
|
|
1015
|
+
/** Weight for recency (time decay), typically 0.3 */
|
|
1016
|
+
recency: number
|
|
1017
|
+
/** Weight for relevance (semantic similarity), typically 0.5 */
|
|
1018
|
+
relevance: number
|
|
1019
|
+
/** Weight for importance (access frequency), typically 0.2 */
|
|
1020
|
+
importance: number
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
/**
|
|
1024
|
+
* AgentRuntime configuration
|
|
1025
|
+
*/
|
|
1026
|
+
export interface AgentRuntimeConfig {
|
|
1027
|
+
/** Custom agent ID (auto-generated if not provided) */
|
|
1028
|
+
id?: string
|
|
1029
|
+
/** Agent name */
|
|
1030
|
+
name?: string
|
|
1031
|
+
/** Agent version */
|
|
1032
|
+
version?: string
|
|
1033
|
+
/** Working memory capacity (default: 100 items) */
|
|
1034
|
+
workingMemoryCapacity?: number
|
|
1035
|
+
/** Episodic memory retention in days (default: 30) */
|
|
1036
|
+
episodicRetentionDays?: number
|
|
1037
|
+
/** Memory retrieval weights */
|
|
1038
|
+
retrievalWeights?: RetrievalWeights
|
|
1039
|
+
/** Recency decay rate per hour (default: 0.995) */
|
|
1040
|
+
recencyDecayRate?: number
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
/**
|
|
1044
|
+
* AgentRuntime - Core agent identity and state management
|
|
1045
|
+
*
|
|
1046
|
+
* Provides:
|
|
1047
|
+
* - Unique agent identity (UUID)
|
|
1048
|
+
* - Lifecycle state management
|
|
1049
|
+
* - Memory layer orchestration
|
|
1050
|
+
* - Execution context tracking
|
|
1051
|
+
*
|
|
1052
|
+
* @example
|
|
1053
|
+
* ```typescript
|
|
1054
|
+
* const runtime = new AgentRuntime({
|
|
1055
|
+
* name: 'fraud-detector',
|
|
1056
|
+
* version: '1.0.0',
|
|
1057
|
+
* workingMemoryCapacity: 200
|
|
1058
|
+
* })
|
|
1059
|
+
*
|
|
1060
|
+
* runtime.ready()
|
|
1061
|
+
* const execId = runtime.startExecution('Find fraud patterns')
|
|
1062
|
+
* // ... do work ...
|
|
1063
|
+
* const execution = runtime.completeExecution({ findings: [...] }, true)
|
|
1064
|
+
* ```
|
|
1065
|
+
*/
|
|
1066
|
+
export class AgentRuntime {
|
|
1067
|
+
/** Agent unique identifier */
|
|
1068
|
+
readonly id: string
|
|
1069
|
+
/** Agent name */
|
|
1070
|
+
readonly name: string
|
|
1071
|
+
/** Agent version */
|
|
1072
|
+
readonly version: string
|
|
1073
|
+
/** Creation timestamp */
|
|
1074
|
+
readonly createdAt: string
|
|
1075
|
+
/** Current state */
|
|
1076
|
+
state: AgentStateType
|
|
1077
|
+
/** State transition history */
|
|
1078
|
+
stateHistory: Array<{ state: AgentStateType; timestamp: string; reason: string }>
|
|
1079
|
+
/** Memory configuration */
|
|
1080
|
+
memoryConfig: {
|
|
1081
|
+
workingMemoryCapacity: number
|
|
1082
|
+
episodicRetentionDays: number
|
|
1083
|
+
retrievalWeights: RetrievalWeights
|
|
1084
|
+
recencyDecayRate: number
|
|
1085
|
+
}
|
|
1086
|
+
/** Execution metrics */
|
|
1087
|
+
metrics: {
|
|
1088
|
+
totalExecutions: number
|
|
1089
|
+
successfulExecutions: number
|
|
1090
|
+
failedExecutions: number
|
|
1091
|
+
totalMemoryRetrievals: number
|
|
1092
|
+
avgExecutionTimeMs: number
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
constructor(config?: AgentRuntimeConfig)
|
|
1096
|
+
|
|
1097
|
+
/** Transition to a new state */
|
|
1098
|
+
transitionTo(newState: AgentStateType, reason?: string): this
|
|
1099
|
+
|
|
1100
|
+
/** Mark agent as ready */
|
|
1101
|
+
ready(): this
|
|
1102
|
+
|
|
1103
|
+
/** Start a new execution */
|
|
1104
|
+
startExecution(prompt: string): string
|
|
1105
|
+
|
|
1106
|
+
/** Complete current execution */
|
|
1107
|
+
completeExecution(result: unknown, success?: boolean): {
|
|
1108
|
+
id: string
|
|
1109
|
+
prompt: string
|
|
1110
|
+
startTime: number
|
|
1111
|
+
endTime: number
|
|
1112
|
+
durationMs: number
|
|
1113
|
+
result: unknown
|
|
1114
|
+
success: boolean
|
|
1115
|
+
} | undefined
|
|
1116
|
+
|
|
1117
|
+
/** Get agent identity information */
|
|
1118
|
+
getIdentity(): {
|
|
1119
|
+
id: string
|
|
1120
|
+
name: string
|
|
1121
|
+
version: string
|
|
1122
|
+
createdAt: string
|
|
1123
|
+
state: AgentStateType
|
|
1124
|
+
executionCount: number
|
|
1125
|
+
lastActiveAt: string
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
/** Get runtime metrics */
|
|
1129
|
+
getMetrics(): {
|
|
1130
|
+
totalExecutions: number
|
|
1131
|
+
successfulExecutions: number
|
|
1132
|
+
failedExecutions: number
|
|
1133
|
+
totalMemoryRetrievals: number
|
|
1134
|
+
avgExecutionTimeMs: number
|
|
1135
|
+
uptime: number
|
|
1136
|
+
currentState: AgentStateType
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
/**
|
|
1141
|
+
* WorkingMemory - Current execution context (in-memory)
|
|
1142
|
+
*
|
|
1143
|
+
* Fast, ephemeral memory for the current task:
|
|
1144
|
+
* - Current conversation context
|
|
1145
|
+
* - Active bindings and variables
|
|
1146
|
+
* - Tool execution results (recent)
|
|
1147
|
+
* - Scratchpad for reasoning
|
|
1148
|
+
*
|
|
1149
|
+
* @example
|
|
1150
|
+
* ```typescript
|
|
1151
|
+
* const memory = new WorkingMemory(100)
|
|
1152
|
+
*
|
|
1153
|
+
* memory.addContext({ type: 'user_input', content: 'Find claims' })
|
|
1154
|
+
* memory.setBinding('claim_id', 'CLM-2024-001')
|
|
1155
|
+
* memory.storeToolResult('kg.sparql.query', {}, { count: 5 })
|
|
1156
|
+
* memory.setFocus('http://example.org/claim/123')
|
|
1157
|
+
*
|
|
1158
|
+
* const recent = memory.getRecentContext(5)
|
|
1159
|
+
* memory.clear()
|
|
1160
|
+
* ```
|
|
1161
|
+
*/
|
|
1162
|
+
export class WorkingMemory {
|
|
1163
|
+
/** Maximum context items */
|
|
1164
|
+
capacity: number
|
|
1165
|
+
/** Current context items */
|
|
1166
|
+
context: Array<{ id: string; timestamp: number; [key: string]: unknown }>
|
|
1167
|
+
/** Variable bindings */
|
|
1168
|
+
bindings: Map<string, { value: unknown; timestamp: number; accessCount: number }>
|
|
1169
|
+
/** Scratchpad for reasoning */
|
|
1170
|
+
scratchpad: Map<string, unknown>
|
|
1171
|
+
/** Recent tool results */
|
|
1172
|
+
toolResults: Array<{ tool: string; args: unknown; result: unknown; timestamp: number }>
|
|
1173
|
+
/** Current focus entity */
|
|
1174
|
+
focus: { entity: string; timestamp: number } | null
|
|
1175
|
+
|
|
1176
|
+
constructor(capacity?: number)
|
|
1177
|
+
|
|
1178
|
+
/** Add context item */
|
|
1179
|
+
addContext(item: Record<string, unknown>): this
|
|
1180
|
+
|
|
1181
|
+
/** Set variable binding */
|
|
1182
|
+
setBinding(key: string, value: unknown): this
|
|
1183
|
+
|
|
1184
|
+
/** Get variable binding (returns null if not found) */
|
|
1185
|
+
getBinding(key: string): unknown | null
|
|
1186
|
+
|
|
1187
|
+
/** Store tool execution result */
|
|
1188
|
+
storeToolResult(toolName: string, args: unknown, result: unknown): this
|
|
1189
|
+
|
|
1190
|
+
/** Set current focus entity */
|
|
1191
|
+
setFocus(entity: string): this
|
|
1192
|
+
|
|
1193
|
+
/** Write to scratchpad */
|
|
1194
|
+
scratch(key: string, value: unknown): this
|
|
1195
|
+
|
|
1196
|
+
/** Read from scratchpad */
|
|
1197
|
+
readScratch(key: string): unknown | undefined
|
|
1198
|
+
|
|
1199
|
+
/** Get recent context items */
|
|
1200
|
+
getRecentContext(limit?: number): Array<Record<string, unknown>>
|
|
1201
|
+
|
|
1202
|
+
/** Clear all working memory */
|
|
1203
|
+
clear(): this
|
|
1204
|
+
|
|
1205
|
+
/** Export memory state */
|
|
1206
|
+
export(): {
|
|
1207
|
+
context: Array<Record<string, unknown>>
|
|
1208
|
+
bindings: Record<string, unknown>
|
|
1209
|
+
scratchpad: Record<string, unknown>
|
|
1210
|
+
toolResults: Array<Record<string, unknown>>
|
|
1211
|
+
focus: { entity: string; timestamp: number } | null
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
/**
|
|
1216
|
+
* EpisodicMemory - Execution history stored in GraphDB
|
|
1217
|
+
*
|
|
1218
|
+
* Persistent memory of agent executions:
|
|
1219
|
+
* - Past prompts and responses
|
|
1220
|
+
* - Tool invocation history
|
|
1221
|
+
* - Success/failure outcomes
|
|
1222
|
+
* - Temporal relationships
|
|
1223
|
+
*
|
|
1224
|
+
* @example
|
|
1225
|
+
* ```typescript
|
|
1226
|
+
* const db = new GraphDB('http://example.org/agent')
|
|
1227
|
+
* const episodic = new EpisodicMemory(db, 'http://hypermind.ai/episodic/')
|
|
1228
|
+
*
|
|
1229
|
+
* await episodic.storeEpisode('agent_123', {
|
|
1230
|
+
* id: 'exec_456',
|
|
1231
|
+
* prompt: 'Find fraud',
|
|
1232
|
+
* durationMs: 150,
|
|
1233
|
+
* success: true
|
|
1234
|
+
* })
|
|
1235
|
+
*
|
|
1236
|
+
* const episodes = await episodic.getEpisodes('agent_123', { limit: 10 })
|
|
1237
|
+
* ```
|
|
1238
|
+
*/
|
|
1239
|
+
export class EpisodicMemory {
|
|
1240
|
+
/** GraphDB instance for storage */
|
|
1241
|
+
graphDb: GraphDB | null
|
|
1242
|
+
/** RDF namespace for episodes */
|
|
1243
|
+
namespace: string
|
|
1244
|
+
|
|
1245
|
+
constructor(graphDb: GraphDB | null, namespace?: string)
|
|
1246
|
+
|
|
1247
|
+
/** Store an execution episode */
|
|
1248
|
+
storeEpisode(agentId: string, execution: {
|
|
1249
|
+
id: string
|
|
1250
|
+
prompt: string
|
|
1251
|
+
durationMs?: number
|
|
1252
|
+
success: boolean
|
|
1253
|
+
}): Promise<{ episodeUri: string; timestamp: string; stored: boolean }>
|
|
1254
|
+
|
|
1255
|
+
/** Store a tool invocation within an episode */
|
|
1256
|
+
storeToolInvocation(episodeId: string, toolName: string, args: unknown, result: unknown, success: boolean): Promise<string>
|
|
1257
|
+
|
|
1258
|
+
/** Retrieve episodes for an agent */
|
|
1259
|
+
getEpisodes(agentId: string, options?: {
|
|
1260
|
+
limit?: number
|
|
1261
|
+
since?: string
|
|
1262
|
+
}): Promise<Array<{
|
|
1263
|
+
episode: string
|
|
1264
|
+
prompt: string
|
|
1265
|
+
timestamp: string
|
|
1266
|
+
durationMs: number
|
|
1267
|
+
success: boolean
|
|
1268
|
+
}>>
|
|
1269
|
+
|
|
1270
|
+
/** Get episodes similar to a prompt */
|
|
1271
|
+
getSimilarEpisodes(prompt: string, k?: number, threshold?: number): Promise<Array<unknown>>
|
|
1272
|
+
|
|
1273
|
+
/** Calculate recency score with exponential decay */
|
|
1274
|
+
calculateRecencyScore(timestamp: string, decayRate?: number): number
|
|
1275
|
+
|
|
1276
|
+
/** Get episode count */
|
|
1277
|
+
getEpisodeCount(): number
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
/**
|
|
1281
|
+
* LongTermMemory - Source-of-truth Knowledge Graph
|
|
1282
|
+
*
|
|
1283
|
+
* Read-only access to the ingestion graph (source of truth):
|
|
1284
|
+
* - Domain ontologies and schemas
|
|
1285
|
+
* - Factual knowledge from data ingestion
|
|
1286
|
+
* - Business rules and constraints
|
|
1287
|
+
* - Clear separation from agent memory
|
|
1288
|
+
*
|
|
1289
|
+
* @example
|
|
1290
|
+
* ```typescript
|
|
1291
|
+
* const db = new GraphDB('http://example.org/source')
|
|
1292
|
+
* const longTerm = new LongTermMemory(db, {
|
|
1293
|
+
* sourceGraphUri: 'http://source.ai/graph/',
|
|
1294
|
+
* agentGraphUri: 'http://agent.ai/graph/'
|
|
1295
|
+
* })
|
|
1296
|
+
*
|
|
1297
|
+
* const facts = await longTerm.getEntityFacts('http://example.org/claim/123')
|
|
1298
|
+
* const schema = await longTerm.getSchema()
|
|
1299
|
+
* ```
|
|
1300
|
+
*/
|
|
1301
|
+
export class LongTermMemory {
|
|
1302
|
+
/** GraphDB instance */
|
|
1303
|
+
graphDb: GraphDB | null
|
|
1304
|
+
/** Source graph URI (read-only truth) */
|
|
1305
|
+
sourceGraphUri: string
|
|
1306
|
+
/** Agent graph URI (writable) */
|
|
1307
|
+
agentGraphUri: string
|
|
1308
|
+
/** Read-only mode flag */
|
|
1309
|
+
readOnly: boolean
|
|
1310
|
+
/** Access log for audit */
|
|
1311
|
+
accessLog: Array<{ operation: string; details: string; timestamp: string }>
|
|
1312
|
+
|
|
1313
|
+
constructor(graphDb: GraphDB | null, config?: {
|
|
1314
|
+
sourceGraphUri?: string
|
|
1315
|
+
agentGraphUri?: string
|
|
1316
|
+
readOnly?: boolean
|
|
1317
|
+
})
|
|
1318
|
+
|
|
1319
|
+
/** Query the source-of-truth graph (read-only) */
|
|
1320
|
+
querySource(sparql: string): Promise<Array<Record<string, unknown>>>
|
|
1321
|
+
|
|
1322
|
+
/** Get entity facts from source graph */
|
|
1323
|
+
getEntityFacts(entityUri: string, limit?: number): Promise<Array<Record<string, unknown>>>
|
|
1324
|
+
|
|
1325
|
+
/** Get entities by type from source graph */
|
|
1326
|
+
getEntitiesByType(typeUri: string, limit?: number): Promise<Array<Record<string, unknown>>>
|
|
1327
|
+
|
|
1328
|
+
/** Get related entities (1-hop neighbors) */
|
|
1329
|
+
getRelatedEntities(entityUri: string, direction?: 'in' | 'out' | 'both'): Promise<Array<Record<string, unknown>>>
|
|
1330
|
+
|
|
1331
|
+
/** Get schema/ontology information */
|
|
1332
|
+
getSchema(prefix?: string | null): Promise<Array<Record<string, unknown>>>
|
|
1333
|
+
|
|
1334
|
+
/** Get access log */
|
|
1335
|
+
getAccessLog(limit?: number): Array<{ operation: string; details: string; timestamp: string }>
|
|
1336
|
+
|
|
1337
|
+
/** Get graph URIs with separation explanation */
|
|
1338
|
+
getGraphUris(): {
|
|
1339
|
+
source: string
|
|
1340
|
+
agent: string
|
|
1341
|
+
separation: string
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
/**
|
|
1346
|
+
* MemoryManager configuration
|
|
1347
|
+
*/
|
|
1348
|
+
export interface MemoryManagerConfig {
|
|
1349
|
+
/** GraphDB instance for episodic and long-term memory */
|
|
1350
|
+
graphDb?: GraphDB | null
|
|
1351
|
+
/** Working memory capacity */
|
|
1352
|
+
workingCapacity?: number
|
|
1353
|
+
/** Episodic memory namespace */
|
|
1354
|
+
episodicNamespace?: string
|
|
1355
|
+
/** Source graph URI */
|
|
1356
|
+
sourceGraphUri?: string
|
|
1357
|
+
/** Agent graph URI */
|
|
1358
|
+
agentGraphUri?: string
|
|
1359
|
+
/** Retrieval weights */
|
|
1360
|
+
weights?: RetrievalWeights
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
/**
|
|
1364
|
+
* Memory retrieval results
|
|
1365
|
+
*/
|
|
1366
|
+
export interface MemoryRetrievalResults {
|
|
1367
|
+
working: Array<{ type: string; data: unknown; score: number; source?: string }>
|
|
1368
|
+
episodic: Array<{ episode: string; prompt: string; timestamp: string; score: number; source: string }>
|
|
1369
|
+
longTerm: Array<Record<string, unknown> & { score: number; source: string }>
|
|
1370
|
+
combined: Array<{ score: number; source: string; [key: string]: unknown }>
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
/**
|
|
1374
|
+
* MemoryManager - Unified memory retrieval with weighted scoring
|
|
1375
|
+
*
|
|
1376
|
+
* Orchestrates all memory layers with intelligent retrieval:
|
|
1377
|
+
* - Score = α × Recency + β × Relevance + γ × Importance
|
|
1378
|
+
* - α = 0.3 (time decay), β = 0.5 (semantic similarity), γ = 0.2 (access frequency)
|
|
1379
|
+
*
|
|
1380
|
+
* @example
|
|
1381
|
+
* ```typescript
|
|
1382
|
+
* const runtime = new AgentRuntime({ name: 'test' })
|
|
1383
|
+
* runtime.ready()
|
|
1384
|
+
*
|
|
1385
|
+
* const manager = new MemoryManager(runtime, {
|
|
1386
|
+
* graphDb: new GraphDB('http://example.org'),
|
|
1387
|
+
* weights: { recency: 0.3, relevance: 0.5, importance: 0.2 }
|
|
1388
|
+
* })
|
|
1389
|
+
*
|
|
1390
|
+
* // Add to working memory
|
|
1391
|
+
* manager.addToWorking({ type: 'query', content: 'find claims' })
|
|
1392
|
+
*
|
|
1393
|
+
* // Unified retrieval across all layers
|
|
1394
|
+
* const results = await manager.retrieve('fraud claims', {
|
|
1395
|
+
* includeEpisodic: true,
|
|
1396
|
+
* includeLongTerm: true,
|
|
1397
|
+
* entityUri: 'http://example.org/claim/123'
|
|
1398
|
+
* })
|
|
1399
|
+
*
|
|
1400
|
+
* console.log(results.combined) // Ranked results from all layers
|
|
1401
|
+
* ```
|
|
1402
|
+
*/
|
|
1403
|
+
export class MemoryManager {
|
|
1404
|
+
/** Associated agent runtime */
|
|
1405
|
+
runtime: AgentRuntime
|
|
1406
|
+
/** Working memory layer */
|
|
1407
|
+
working: WorkingMemory
|
|
1408
|
+
/** Episodic memory layer */
|
|
1409
|
+
episodic: EpisodicMemory
|
|
1410
|
+
/** Long-term memory layer */
|
|
1411
|
+
longTerm: LongTermMemory
|
|
1412
|
+
/** Retrieval weights */
|
|
1413
|
+
weights: RetrievalWeights
|
|
1414
|
+
/** Retrieval history */
|
|
1415
|
+
retrievalHistory: Array<{ query: string; timestamp: number; counts: Record<string, number> }>
|
|
1416
|
+
|
|
1417
|
+
constructor(runtime: AgentRuntime, config?: MemoryManagerConfig)
|
|
1418
|
+
|
|
1419
|
+
/** Unified memory retrieval with weighted scoring */
|
|
1420
|
+
retrieve(query: string, options?: {
|
|
1421
|
+
includeEpisodic?: boolean
|
|
1422
|
+
includeLongTerm?: boolean
|
|
1423
|
+
entityUri?: string
|
|
1424
|
+
}): Promise<MemoryRetrievalResults>
|
|
1425
|
+
|
|
1426
|
+
/** Store execution in episodic memory */
|
|
1427
|
+
storeExecution(execution: {
|
|
1428
|
+
id: string
|
|
1429
|
+
prompt: string
|
|
1430
|
+
success: boolean
|
|
1431
|
+
durationMs?: number
|
|
1432
|
+
}): Promise<{ episodeUri: string; timestamp: string; stored: boolean }>
|
|
1433
|
+
|
|
1434
|
+
/** Add item to working memory */
|
|
1435
|
+
addToWorking(item: Record<string, unknown>): this
|
|
1436
|
+
|
|
1437
|
+
/** Get memory statistics */
|
|
1438
|
+
getStats(): {
|
|
1439
|
+
working: { contextSize: number; bindingsCount: number; toolResultsCount: number }
|
|
1440
|
+
episodic: { episodeCount: number }
|
|
1441
|
+
longTerm: { accessLogSize: number }
|
|
1442
|
+
retrieval: { totalRetrievals: number; uniqueAccessedItems: number }
|
|
1443
|
+
weights: RetrievalWeights
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
/** Clear working memory (episodic and long-term persist) */
|
|
1447
|
+
clearWorking(): this
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
// ==============================================
|
|
1451
|
+
// Governance Layer (v0.5.13+) - Policy Engine
|
|
1452
|
+
// ==============================================
|
|
1453
|
+
|
|
1454
|
+
/**
|
|
1455
|
+
* GovernancePolicy configuration
|
|
1456
|
+
*/
|
|
1457
|
+
export interface GovernancePolicyConfig {
|
|
1458
|
+
/** Policy name */
|
|
1459
|
+
name?: string
|
|
1460
|
+
/** Policy version */
|
|
1461
|
+
version?: string
|
|
1462
|
+
/** Granted capabilities */
|
|
1463
|
+
capabilities?: string[]
|
|
1464
|
+
/** Resource limits */
|
|
1465
|
+
limits?: {
|
|
1466
|
+
maxExecutionTimeMs?: number
|
|
1467
|
+
maxMemoryMB?: number
|
|
1468
|
+
maxToolCalls?: number
|
|
1469
|
+
maxGraphQueries?: number
|
|
1470
|
+
}
|
|
1471
|
+
/** Audit settings */
|
|
1472
|
+
audit?: {
|
|
1473
|
+
logAllToolCalls?: boolean
|
|
1474
|
+
logMemoryAccess?: boolean
|
|
1475
|
+
logGraphQueries?: boolean
|
|
1476
|
+
retentionDays?: number
|
|
1477
|
+
}
|
|
1478
|
+
/** Behavioral constraints */
|
|
1479
|
+
constraints?: {
|
|
1480
|
+
allowExternalApi?: boolean
|
|
1481
|
+
allowFileAccess?: boolean
|
|
1482
|
+
requireApprovalForWrites?: boolean
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
/**
|
|
1487
|
+
* GovernancePolicy - Access control and behavioral policies
|
|
1488
|
+
*
|
|
1489
|
+
* @example
|
|
1490
|
+
* ```typescript
|
|
1491
|
+
* const policy = new GovernancePolicy({
|
|
1492
|
+
* name: 'production-policy',
|
|
1493
|
+
* capabilities: ['ReadKG', 'ExecuteTool'],
|
|
1494
|
+
* limits: { maxToolCalls: 100, maxGraphQueries: 1000 },
|
|
1495
|
+
* constraints: { allowExternalApi: false }
|
|
1496
|
+
* })
|
|
1497
|
+
*
|
|
1498
|
+
* policy.grantCapability('AccessMemory')
|
|
1499
|
+
* console.log(policy.hasCapability('WriteKG')) // false
|
|
1500
|
+
* ```
|
|
1501
|
+
*/
|
|
1502
|
+
export class GovernancePolicy {
|
|
1503
|
+
/** Policy name */
|
|
1504
|
+
name: string
|
|
1505
|
+
/** Policy version */
|
|
1506
|
+
version: string
|
|
1507
|
+
/** Granted capabilities */
|
|
1508
|
+
capabilities: Set<string>
|
|
1509
|
+
/** Resource limits */
|
|
1510
|
+
limits: Record<string, number>
|
|
1511
|
+
/** Audit settings */
|
|
1512
|
+
audit: Record<string, unknown>
|
|
1513
|
+
/** Behavioral constraints */
|
|
1514
|
+
constraints: Record<string, boolean>
|
|
1515
|
+
|
|
1516
|
+
constructor(config?: GovernancePolicyConfig)
|
|
1517
|
+
|
|
1518
|
+
/** Check if capability is granted */
|
|
1519
|
+
hasCapability(capability: string): boolean
|
|
1520
|
+
|
|
1521
|
+
/** Grant a capability */
|
|
1522
|
+
grantCapability(capability: string): this
|
|
1523
|
+
|
|
1524
|
+
/** Revoke a capability */
|
|
1525
|
+
revokeCapability(capability: string): this
|
|
1526
|
+
|
|
1527
|
+
/** Check resource limit */
|
|
1528
|
+
checkLimit(resource: string, current: number): boolean
|
|
1529
|
+
|
|
1530
|
+
/** Export policy configuration */
|
|
1531
|
+
export(): GovernancePolicyConfig
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
/**
|
|
1535
|
+
* Authorization result
|
|
1536
|
+
*/
|
|
1537
|
+
export interface AuthorizationResult {
|
|
1538
|
+
allowed: boolean
|
|
1539
|
+
reason: string
|
|
1540
|
+
capability?: string
|
|
1541
|
+
timestamp: string
|
|
1542
|
+
}
|
|
1543
|
+
|
|
1544
|
+
/**
|
|
1545
|
+
* GovernanceEngine - Policy enforcement and audit logging
|
|
1546
|
+
*
|
|
1547
|
+
* @example
|
|
1548
|
+
* ```typescript
|
|
1549
|
+
* const policy = new GovernancePolicy({ capabilities: ['ReadKG'] })
|
|
1550
|
+
* const engine = new GovernanceEngine(policy)
|
|
1551
|
+
*
|
|
1552
|
+
* const result = engine.authorize({
|
|
1553
|
+
* capability: 'WriteKG',
|
|
1554
|
+
* type: 'graphUpdate'
|
|
1555
|
+
* })
|
|
1556
|
+
*
|
|
1557
|
+
* console.log(result.allowed) // false
|
|
1558
|
+
* console.log(result.reason) // "Missing capability: WriteKG"
|
|
1559
|
+
* ```
|
|
1560
|
+
*/
|
|
1561
|
+
export class GovernanceEngine {
|
|
1562
|
+
/** Active policy */
|
|
1563
|
+
policy: GovernancePolicy
|
|
1564
|
+
/** Audit log */
|
|
1565
|
+
auditLog: Array<{ action: unknown; result: AuthorizationResult; timestamp: string }>
|
|
1566
|
+
/** Denial log */
|
|
1567
|
+
denials: Array<{ action: unknown; reason: string; timestamp: string }>
|
|
1568
|
+
|
|
1569
|
+
constructor(policy?: GovernancePolicy | null)
|
|
1570
|
+
|
|
1571
|
+
/** Authorize an action */
|
|
1572
|
+
authorize(action: {
|
|
1573
|
+
capability?: string
|
|
1574
|
+
type?: string
|
|
1575
|
+
resource?: string
|
|
1576
|
+
current?: number
|
|
1577
|
+
}): AuthorizationResult
|
|
1578
|
+
|
|
1579
|
+
/** Get audit log */
|
|
1580
|
+
getAuditLog(limit?: number): Array<{ action: unknown; result: AuthorizationResult; timestamp: string }>
|
|
1581
|
+
|
|
1582
|
+
/** Get denial log */
|
|
1583
|
+
getDenials(limit?: number): Array<{ action: unknown; reason: string; timestamp: string }>
|
|
1584
|
+
|
|
1585
|
+
/** Update policy */
|
|
1586
|
+
setPolicy(policy: GovernancePolicy): this
|
|
1587
|
+
}
|
|
1588
|
+
|
|
1589
|
+
// ==============================================
|
|
1590
|
+
// Scope Layer (v0.5.13+) - Namespace Isolation
|
|
1591
|
+
// ==============================================
|
|
1592
|
+
|
|
1593
|
+
/**
|
|
1594
|
+
* AgentScope configuration
|
|
1595
|
+
*/
|
|
1596
|
+
export interface AgentScopeConfig {
|
|
1597
|
+
/** Scope ID (auto-generated if not provided) */
|
|
1598
|
+
id?: string
|
|
1599
|
+
/** Scope name */
|
|
1600
|
+
name?: string
|
|
1601
|
+
/** Namespace configuration */
|
|
1602
|
+
namespace?: {
|
|
1603
|
+
prefix?: string
|
|
1604
|
+
graphUri?: string
|
|
1605
|
+
allowedGraphs?: string[]
|
|
1606
|
+
deniedGraphs?: string[]
|
|
1607
|
+
}
|
|
1608
|
+
/** Maximum memory in MB */
|
|
1609
|
+
maxMemoryMB?: number
|
|
1610
|
+
/** Maximum execution time in ms */
|
|
1611
|
+
maxExecutionTimeMs?: number
|
|
1612
|
+
/** Maximum tool calls */
|
|
1613
|
+
maxToolCalls?: number
|
|
1614
|
+
/** Maximum graph queries */
|
|
1615
|
+
maxGraphQueries?: number
|
|
1616
|
+
/** Maximum episodes */
|
|
1617
|
+
maxEpisodes?: number
|
|
1618
|
+
/** Share memory with other scopes */
|
|
1619
|
+
shareMemory?: boolean
|
|
1620
|
+
/** Share episodes with other scopes */
|
|
1621
|
+
shareEpisodes?: boolean
|
|
1622
|
+
/** Parent scope ID */
|
|
1623
|
+
parentScope?: string | null
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1626
|
+
/**
|
|
1627
|
+
* AgentScope - Namespace isolation and resource limits
|
|
1628
|
+
*
|
|
1629
|
+
* @example
|
|
1630
|
+
* ```typescript
|
|
1631
|
+
* const scope = new AgentScope({
|
|
1632
|
+
* name: 'fraud-detection',
|
|
1633
|
+
* maxToolCalls: 100,
|
|
1634
|
+
* namespace: {
|
|
1635
|
+
* allowedGraphs: ['http://insurance.example.org/*'],
|
|
1636
|
+
* deniedGraphs: ['http://secret.graph/']
|
|
1637
|
+
* }
|
|
1638
|
+
* })
|
|
1639
|
+
*
|
|
1640
|
+
* scope.trackUsage('toolCalls', 5)
|
|
1641
|
+
* console.log(scope.isWithinLimits('toolCalls')) // true
|
|
1642
|
+
*
|
|
1643
|
+
* const remaining = scope.getRemainingResources()
|
|
1644
|
+
* console.log(remaining.toolcalls) // 95
|
|
1645
|
+
* ```
|
|
1646
|
+
*/
|
|
1647
|
+
export class AgentScope {
|
|
1648
|
+
/** Scope unique identifier */
|
|
1649
|
+
id: string
|
|
1650
|
+
/** Scope name */
|
|
1651
|
+
name: string
|
|
1652
|
+
/** Namespace configuration */
|
|
1653
|
+
namespace: {
|
|
1654
|
+
prefix: string
|
|
1655
|
+
graphUri: string
|
|
1656
|
+
allowedGraphs: string[]
|
|
1657
|
+
deniedGraphs: string[]
|
|
1658
|
+
}
|
|
1659
|
+
/** Resource limits */
|
|
1660
|
+
resources: {
|
|
1661
|
+
maxMemoryMB: number
|
|
1662
|
+
maxExecutionTimeMs: number
|
|
1663
|
+
maxToolCalls: number
|
|
1664
|
+
maxGraphQueries: number
|
|
1665
|
+
maxEpisodes: number
|
|
1666
|
+
}
|
|
1667
|
+
/** Current usage */
|
|
1668
|
+
usage: {
|
|
1669
|
+
memoryMB: number
|
|
1670
|
+
executionTimeMs: number
|
|
1671
|
+
toolCalls: number
|
|
1672
|
+
graphQueries: number
|
|
1673
|
+
episodes: number
|
|
1674
|
+
}
|
|
1675
|
+
/** Isolation settings */
|
|
1676
|
+
isolation: {
|
|
1677
|
+
shareMemory: boolean
|
|
1678
|
+
shareEpisodes: boolean
|
|
1679
|
+
parentScope: string | null
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
constructor(config?: AgentScopeConfig)
|
|
1683
|
+
|
|
1684
|
+
/** Check if graph access is allowed */
|
|
1685
|
+
isGraphAllowed(graphUri: string): boolean
|
|
1686
|
+
|
|
1687
|
+
/** Track resource usage */
|
|
1688
|
+
trackUsage(resource: string, amount: number): this
|
|
1689
|
+
|
|
1690
|
+
/** Check if within resource limits */
|
|
1691
|
+
isWithinLimits(resource: string): boolean
|
|
1692
|
+
|
|
1693
|
+
/** Get remaining resources */
|
|
1694
|
+
getRemainingResources(): Record<string, number>
|
|
1695
|
+
|
|
1696
|
+
/** Reset usage counters */
|
|
1697
|
+
resetUsage(): this
|
|
1698
|
+
|
|
1699
|
+
/** Get scoped graph URI for agent */
|
|
1700
|
+
getScopedGraphUri(suffix?: string): string
|
|
1701
|
+
|
|
1702
|
+
/** Export scope configuration */
|
|
1703
|
+
export(): {
|
|
1704
|
+
id: string
|
|
1705
|
+
name: string
|
|
1706
|
+
namespace: {
|
|
1707
|
+
prefix: string
|
|
1708
|
+
graphUri: string
|
|
1709
|
+
allowedGraphs: string[]
|
|
1710
|
+
deniedGraphs: string[]
|
|
1711
|
+
}
|
|
1712
|
+
resources: {
|
|
1713
|
+
maxMemoryMB: number
|
|
1714
|
+
maxExecutionTimeMs: number
|
|
1715
|
+
maxToolCalls: number
|
|
1716
|
+
maxGraphQueries: number
|
|
1717
|
+
maxEpisodes: number
|
|
1718
|
+
}
|
|
1719
|
+
usage: Record<string, number>
|
|
1720
|
+
isolation: {
|
|
1721
|
+
shareMemory: boolean
|
|
1722
|
+
shareEpisodes: boolean
|
|
1723
|
+
parentScope: string | null
|
|
1724
|
+
}
|
|
1725
|
+
}
|
|
1726
|
+
}
|