wip(m4-003): memory/retrieval slice handoff from coder4
This commit is contained in:
84
packages/memory/src/operator-memory-plugin.ts
Normal file
84
packages/memory/src/operator-memory-plugin.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import type { Insight, InsightSearchResult, MemoryAdapter } from './types.js';
|
||||
|
||||
/** Immutable server-derived boundary; callers never choose an adapter namespace. */
|
||||
export interface OperatorMemoryScope {
|
||||
tenantId: string;
|
||||
ownerId: string;
|
||||
sessionId: string;
|
||||
namespace: string;
|
||||
}
|
||||
|
||||
export interface OperatorMemoryConfig {
|
||||
adapter: MemoryAdapter;
|
||||
instanceId: string;
|
||||
maxStartupContext?: number;
|
||||
redact(content: string): string;
|
||||
}
|
||||
|
||||
export interface OperatorMemoryResult extends InsightSearchResult {
|
||||
provenance: { instanceId: string; namespace: string; source: string };
|
||||
}
|
||||
|
||||
export interface OperatorMemoryPlugin {
|
||||
capture(
|
||||
scope: OperatorMemoryScope,
|
||||
input: { content: string; source: string; category: string },
|
||||
): Promise<Insight>;
|
||||
search(
|
||||
scope: OperatorMemoryScope,
|
||||
query: string,
|
||||
limit?: number,
|
||||
): Promise<OperatorMemoryResult[]>;
|
||||
recent(scope: OperatorMemoryScope, limit?: number): Promise<OperatorMemoryResult[]>;
|
||||
stats(scope: OperatorMemoryScope): Promise<{ namespace: string; resultCount: number }>;
|
||||
startupContext(scope: OperatorMemoryScope): Promise<OperatorMemoryResult[]>;
|
||||
}
|
||||
|
||||
function scopedUserId(scope: OperatorMemoryScope): string {
|
||||
return `${scope.tenantId}:${scope.ownerId}:${scope.namespace}`;
|
||||
}
|
||||
|
||||
/** Creates a leaf-package, replaceable memory adapter facade. */
|
||||
export function createOperatorMemoryPlugin(config: OperatorMemoryConfig): OperatorMemoryPlugin {
|
||||
const mapResult = (
|
||||
scope: OperatorMemoryScope,
|
||||
result: InsightSearchResult,
|
||||
): OperatorMemoryResult => ({
|
||||
...result,
|
||||
provenance: {
|
||||
instanceId: config.instanceId,
|
||||
namespace: scope.namespace,
|
||||
source: String(result.metadata?.['source'] ?? 'retrieval'),
|
||||
},
|
||||
});
|
||||
const search = async (
|
||||
scope: OperatorMemoryScope,
|
||||
query: string,
|
||||
limit = 10,
|
||||
): Promise<OperatorMemoryResult[]> =>
|
||||
(await config.adapter.searchInsights(scopedUserId(scope), query, { limit })).map((result) =>
|
||||
mapResult(scope, result),
|
||||
);
|
||||
return {
|
||||
async capture(scope, input) {
|
||||
return config.adapter.storeInsight({
|
||||
userId: scopedUserId(scope),
|
||||
content: config.redact(input.content),
|
||||
source: input.source,
|
||||
category: input.category,
|
||||
relevanceScore: 1,
|
||||
metadata: { namespace: scope.namespace, instanceId: config.instanceId },
|
||||
});
|
||||
},
|
||||
search,
|
||||
async recent(scope, limit = 10) {
|
||||
return search(scope, '*', limit);
|
||||
},
|
||||
async stats(scope) {
|
||||
return { namespace: scope.namespace, resultCount: (await search(scope, '*', 100)).length };
|
||||
},
|
||||
async startupContext(scope) {
|
||||
return search(scope, '*', config.maxStartupContext ?? 8);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user