-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenrich.ts
More file actions
67 lines (62 loc) · 1.83 KB
/
enrich.ts
File metadata and controls
67 lines (62 loc) · 1.83 KB
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
import type { ISdk } from "iii-sdk";
import type { RetrievalContextItem, Session } from "../types.js";
import { KV } from "../state/schema.js";
import type { StateKV } from "../state/kv.js";
import { logger } from "../logger.js";
const MAX_CONTEXT_LENGTH = 4000;
export function registerEnrichFunction(sdk: ISdk, kv: StateKV): void {
sdk.registerFunction(
"mem::enrich",
async (data: {
sessionId: string;
files: string[];
terms?: string[];
toolName?: string;
}) => {
const session = await kv.get<Session>(KV.sessions, data.sessionId).catch(() => null);
const result = (await sdk.trigger({
function_id: "mem::context",
payload: {
sessionId: data.sessionId,
project: session?.project,
intent: "file_enrich",
files: data.files || [],
terms: data.terms || [],
budget: Math.max(1, Math.floor(MAX_CONTEXT_LENGTH / 3)),
maxBlocks: 8,
},
})) as {
context: string;
items?: RetrievalContextItem[];
blocks: number;
skipped?: boolean;
reason?: string;
pressure?: unknown;
trace: unknown;
};
let context = result.context;
let truncated = false;
if (context.length > MAX_CONTEXT_LENGTH) {
context = context.slice(0, MAX_CONTEXT_LENGTH);
truncated = true;
}
logger.info("Enrichment completed", {
sessionId: data.sessionId,
toolName: data.toolName,
fileCount: data.files.length,
contextLength: context.length,
truncated,
});
return {
context,
truncated,
items: result.items || [],
blocks: result.blocks,
skipped: result.skipped,
reason: result.reason,
pressure: result.pressure,
trace: result.trace,
};
},
);
}