mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-10-07 02:58:11 +00:00
refactor(tools): narrow memory tool input with a type guard
Replace the bare `block.input as MemoryCommand` assertions in the Anthropic example with an isMemoryCommand type guard, so unexpected tool input is skipped instead of silently mistyped.
This commit is contained in:
parent
760f458210
commit
fa8ba27b10
1 changed files with 31 additions and 2 deletions
|
|
@ -11,6 +11,27 @@ import {
|
|||
} from "../src/claude-memory"
|
||||
import "dotenv/config"
|
||||
|
||||
const MEMORY_COMMANDS: readonly string[] = [
|
||||
"view",
|
||||
"create",
|
||||
"str_replace",
|
||||
"insert",
|
||||
"delete",
|
||||
"rename",
|
||||
]
|
||||
|
||||
function isMemoryCommand(input: unknown): input is MemoryCommand {
|
||||
return (
|
||||
typeof input === "object" &&
|
||||
input !== null &&
|
||||
"command" in input &&
|
||||
"path" in input &&
|
||||
typeof input.command === "string" &&
|
||||
MEMORY_COMMANDS.includes(input.command) &&
|
||||
typeof input.path === "string"
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle Claude's memory tool calls using the Anthropic SDK
|
||||
*/
|
||||
|
|
@ -75,7 +96,11 @@ async function chatWithMemoryTool() {
|
|||
if (block.type === "text") {
|
||||
console.log("💭", block.text)
|
||||
} else if (block.type === "tool_use" && block.name === "memory") {
|
||||
const command = block.input as MemoryCommand
|
||||
const command = block.input
|
||||
if (!isMemoryCommand(command)) {
|
||||
console.log("Skipping unrecognized memory tool input:", command)
|
||||
continue
|
||||
}
|
||||
console.log("🔧 Claude is using memory tool:")
|
||||
console.log(" Command:", command.command)
|
||||
console.log(" Path:", command.path)
|
||||
|
|
@ -142,7 +167,11 @@ async function chatWithMemoryTool() {
|
|||
if (block.type === "text") {
|
||||
console.log("💭", block.text)
|
||||
} else if (block.type === "tool_use" && block.name === "memory") {
|
||||
const command = block.input as MemoryCommand
|
||||
const command = block.input
|
||||
if (!isMemoryCommand(command)) {
|
||||
console.log("Skipping unrecognized memory tool input:", command)
|
||||
continue
|
||||
}
|
||||
console.log("🔧 Claude is using memory tool again:")
|
||||
console.log(" Command:", command.command)
|
||||
console.log(" Path:", command.path)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue