feat: new flag to filter blocks on creation (#132)

This commit is contained in:
Charles Packer
2025-11-27 02:12:41 -08:00
committed by GitHub
parent 135c19c7d7
commit 06ec8b9d3c
4 changed files with 251 additions and 5 deletions

View File

@@ -32,6 +32,8 @@ export async function createAgent(
parallelToolCalls = true,
enableSleeptime = false,
systemPromptId?: string,
initBlocks?: string[],
baseTools?: string[],
) {
// Resolve model identifier to handle
let modelHandle: string;
@@ -59,16 +61,42 @@ export async function createAgent(
getServerToolName(name),
);
const toolNames = [
...serverToolNames,
const defaultBaseTools = baseTools ?? [
"memory",
"web_search",
"conversation_search",
"fetch_webpage",
];
const toolNames = [...serverToolNames, ...defaultBaseTools];
// Load memory blocks from .mdx files
const defaultMemoryBlocks = await getDefaultMemoryBlocks();
const defaultMemoryBlocks =
initBlocks && initBlocks.length === 0 ? [] : await getDefaultMemoryBlocks();
// Optional filter: only initialize a subset of memory blocks on creation
const allowedBlockLabels = initBlocks
? new Set(
initBlocks.map((name) => name.trim()).filter((name) => name.length > 0),
)
: undefined;
if (allowedBlockLabels && allowedBlockLabels.size > 0) {
const knownLabels = new Set(defaultMemoryBlocks.map((b) => b.label));
for (const label of Array.from(allowedBlockLabels)) {
if (!knownLabels.has(label)) {
console.warn(
`Ignoring unknown init block "${label}". Valid blocks: ${Array.from(knownLabels).join(", ")}`,
);
allowedBlockLabels.delete(label);
}
}
}
const filteredMemoryBlocks =
allowedBlockLabels && allowedBlockLabels.size > 0
? defaultMemoryBlocks.filter((b) => allowedBlockLabels.has(b.label))
: defaultMemoryBlocks;
// Resolve absolute path for skills directory
const resolvedSkillsDirectory =
@@ -87,7 +115,7 @@ export async function createAgent(
}
// Find and update the skills memory block with discovered skills
const skillsBlock = defaultMemoryBlocks.find((b) => b.label === "skills");
const skillsBlock = filteredMemoryBlocks.find((b) => b.label === "skills");
if (skillsBlock) {
skillsBlock.value = formatSkillsForMemory(
skills,
@@ -116,6 +144,9 @@ export async function createAgent(
if (!forceNewBlocks) {
// Load global blocks (persona, human)
for (const [label, blockId] of Object.entries(globalSharedBlockIds)) {
if (allowedBlockLabels && !allowedBlockLabels.has(label)) {
continue;
}
try {
const block = await client.blocks.retrieve(blockId);
existingBlocks.set(label, block);
@@ -129,6 +160,9 @@ export async function createAgent(
// Load local blocks (style)
for (const [label, blockId] of Object.entries(localSharedBlockIds)) {
if (allowedBlockLabels && !allowedBlockLabels.has(label)) {
continue;
}
try {
const block = await client.blocks.retrieve(blockId);
existingBlocks.set(label, block);
@@ -145,7 +179,7 @@ export async function createAgent(
const blockIds: string[] = [];
const blocksToCreate: Array<{ block: CreateBlock; label: string }> = [];
for (const defaultBlock of defaultMemoryBlocks) {
for (const defaultBlock of filteredMemoryBlocks) {
const existingBlock = existingBlocks.get(defaultBlock.label);
if (existingBlock?.id) {
// Reuse existing global shared block