-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathaltimate-core-optimize-context.ts
More file actions
42 lines (40 loc) · 1.73 KB
/
altimate-core-optimize-context.ts
File metadata and controls
42 lines (40 loc) · 1.73 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
import z from "zod"
import { Tool } from "../../tool/tool"
import { Dispatcher } from "../native"
export const AltimateCoreOptimizeContextTool = Tool.define("altimate_core_optimize_context", {
description:
"Optimize schema for LLM context window. Applies 5-level progressive disclosure to reduce schema size while preserving essential information. Provide schema_context or schema_path for accurate table/column resolution.",
parameters: z.object({
schema_path: z.string().optional().describe("Path to YAML/JSON schema file"),
schema_context: z.record(z.string(), z.any()).optional().describe("Inline schema definition"),
}),
async execute(args, ctx) {
try {
const result = await Dispatcher.call("altimate_core.optimize_context", {
schema_path: args.schema_path ?? "",
schema_context: args.schema_context,
})
const data = result.data as Record<string, any>
return {
title: `Optimize Context: ${data.levels?.length ?? 0} level(s)`,
metadata: { success: result.success },
output: formatOptimizeContext(data),
}
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
return { title: "Optimize Context: ERROR", metadata: { success: false }, output: `Failed: ${msg}` }
}
},
})
function formatOptimizeContext(data: Record<string, any>): string {
if (data.error) return `Error: ${data.error}`
if (data.optimized) return data.optimized
if (data.levels?.length) {
const lines = ["Progressive disclosure levels:\n"]
for (const level of data.levels) {
lines.push(` Level ${level.level}: ${level.tokens ?? "?"} tokens — ${level.description ?? ""}`)
}
return lines.join("\n")
}
return JSON.stringify(data, null, 2)
}