deepagents 1.4.2 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -33,6 +33,9 @@ micromatch = __toESM(micromatch);
33
33
  let path = require("path");
34
34
  let zod_v3 = require("zod/v3");
35
35
  let _langchain_core_messages = require("@langchain/core/messages");
36
+ let zod = require("zod");
37
+ let yaml = require("yaml");
38
+ yaml = __toESM(yaml);
36
39
  let node_fs_promises = require("node:fs/promises");
37
40
  node_fs_promises = __toESM(node_fs_promises);
38
41
  let node_fs = require("node:fs");
@@ -44,9 +47,6 @@ let fast_glob = require("fast-glob");
44
47
  fast_glob = __toESM(fast_glob);
45
48
  let node_os = require("node:os");
46
49
  node_os = __toESM(node_os);
47
- let zod = require("zod");
48
- let yaml = require("yaml");
49
- yaml = __toESM(yaml);
50
50
 
51
51
  //#region src/backends/protocol.ts
52
52
  /**
@@ -362,23 +362,12 @@ var StateBackend = class {
362
362
  * @param limit - Maximum number of lines to read
363
363
  * @returns Formatted file content with line numbers, or error message
364
364
  */
365
- read(filePath, offset = 0, limit = 2e3) {
365
+ read(filePath, offset = 0, limit = 500) {
366
366
  const fileData = this.getFiles()[filePath];
367
367
  if (!fileData) return `Error: File '${filePath}' not found`;
368
368
  return formatReadResponse(fileData, offset, limit);
369
369
  }
370
370
  /**
371
- * Read file content as raw FileData.
372
- *
373
- * @param filePath - Absolute file path
374
- * @returns Raw file content as FileData
375
- */
376
- readRaw(filePath) {
377
- const fileData = this.getFiles()[filePath];
378
- if (!fileData) throw new Error(`File '${filePath}' not found`);
379
- return fileData;
380
- }
381
- /**
382
371
  * Create a new file with content.
383
372
  * Returns WriteResult with filesUpdate to update LangGraph state.
384
373
  */
@@ -619,7 +608,7 @@ function createReadFileTool(backend, options) {
619
608
  state: (0, _langchain_langgraph.getCurrentTaskInput)(config),
620
609
  store: config.store
621
610
  });
622
- const { file_path, offset = 0, limit = 2e3 } = input;
611
+ const { file_path, offset = 0, limit = 500 } = input;
623
612
  return await resolvedBackend.read(file_path, offset, limit);
624
613
  }, {
625
614
  name: "read_file",
@@ -627,7 +616,7 @@ function createReadFileTool(backend, options) {
627
616
  schema: zod_v4.z.object({
628
617
  file_path: zod_v4.z.string().describe("Absolute path to the file to read"),
629
618
  offset: zod_v4.z.coerce.number().optional().default(0).describe("Line offset to start reading from (0-indexed)"),
630
- limit: zod_v4.z.coerce.number().optional().default(2e3).describe("Maximum number of lines to read")
619
+ limit: zod_v4.z.coerce.number().optional().default(500).describe("Maximum number of lines to read")
631
620
  })
632
621
  });
633
622
  }
@@ -883,7 +872,7 @@ const DEFAULT_SUBAGENT_PROMPT = "In order to complete the objective that the use
883
872
  const EXCLUDED_STATE_KEYS = [
884
873
  "messages",
885
874
  "todos",
886
- "jumpTo",
875
+ "structuredResponse",
887
876
  "files"
888
877
  ];
889
878
  const DEFAULT_GENERAL_PURPOSE_DESCRIPTION = "General-purpose agent for researching complex questions, searching for files and content, and executing multi-step tasks. When you are searching for a keyword or file and are not confident that you will find the right match in the first few tries use this agent to perform the search for you. This agent has access to all tools as the main agent.";
@@ -1198,6 +1187,501 @@ function createPatchToolCallsMiddleware() {
1198
1187
  });
1199
1188
  }
1200
1189
 
1190
+ //#endregion
1191
+ //#region src/middleware/memory.ts
1192
+ /**
1193
+ * Middleware for loading agent memory/context from AGENTS.md files.
1194
+ *
1195
+ * This module implements support for the AGENTS.md specification (https://agents.md/),
1196
+ * loading memory/context from configurable sources and injecting into the system prompt.
1197
+ *
1198
+ * ## Overview
1199
+ *
1200
+ * AGENTS.md files provide project-specific context and instructions to help AI agents
1201
+ * work effectively. Unlike skills (which are on-demand workflows), memory is always
1202
+ * loaded and provides persistent context.
1203
+ *
1204
+ * ## Usage
1205
+ *
1206
+ * ```typescript
1207
+ * import { createMemoryMiddleware } from "@anthropic/deepagents";
1208
+ * import { FilesystemBackend } from "@anthropic/deepagents";
1209
+ *
1210
+ * // Security: FilesystemBackend allows reading/writing from the entire filesystem.
1211
+ * // Either ensure the agent is running within a sandbox OR add human-in-the-loop (HIL)
1212
+ * // approval to file operations.
1213
+ * const backend = new FilesystemBackend({ rootDir: "/" });
1214
+ *
1215
+ * const middleware = createMemoryMiddleware({
1216
+ * backend,
1217
+ * sources: [
1218
+ * "~/.deepagents/AGENTS.md",
1219
+ * "./.deepagents/AGENTS.md",
1220
+ * ],
1221
+ * });
1222
+ *
1223
+ * const agent = createDeepAgent({ middleware: [middleware] });
1224
+ * ```
1225
+ *
1226
+ * ## Memory Sources
1227
+ *
1228
+ * Sources are simply paths to AGENTS.md files that are loaded in order and combined.
1229
+ * Multiple sources are concatenated in order, with all content included.
1230
+ * Later sources appear after earlier ones in the combined prompt.
1231
+ *
1232
+ * ## File Format
1233
+ *
1234
+ * AGENTS.md files are standard Markdown with no required structure.
1235
+ * Common sections include:
1236
+ * - Project overview
1237
+ * - Build/test commands
1238
+ * - Code style guidelines
1239
+ * - Architecture notes
1240
+ */
1241
+ /**
1242
+ * State schema for memory middleware.
1243
+ */
1244
+ const MemoryStateSchema = zod.z.object({ memoryContents: zod.z.record(zod.z.string(), zod.z.string()).optional() });
1245
+ /**
1246
+ * Default system prompt template for memory.
1247
+ * Ported from Python's comprehensive memory guidelines.
1248
+ */
1249
+ const MEMORY_SYSTEM_PROMPT = `<agent_memory>
1250
+ {memory_contents}
1251
+ </agent_memory>
1252
+
1253
+ <memory_guidelines>
1254
+ The above <agent_memory> was loaded in from files in your filesystem. As you learn from your interactions with the user, you can save new knowledge by calling the \`edit_file\` tool.
1255
+
1256
+ **Learning from feedback:**
1257
+ - One of your MAIN PRIORITIES is to learn from your interactions with the user. These learnings can be implicit or explicit. This means that in the future, you will remember this important information.
1258
+ - When you need to remember something, updating memory must be your FIRST, IMMEDIATE action - before responding to the user, before calling other tools, before doing anything else. Just update memory immediately.
1259
+ - When user says something is better/worse, capture WHY and encode it as a pattern.
1260
+ - Each correction is a chance to improve permanently - don't just fix the immediate issue, update your instructions.
1261
+ - A great opportunity to update your memories is when the user interrupts a tool call and provides feedback. You should update your memories immediately before revising the tool call.
1262
+ - Look for the underlying principle behind corrections, not just the specific mistake.
1263
+ - The user might not explicitly ask you to remember something, but if they provide information that is useful for future use, you should update your memories immediately.
1264
+
1265
+ **Asking for information:**
1266
+ - If you lack context to perform an action (e.g. send a Slack DM, requires a user ID/email) you should explicitly ask the user for this information.
1267
+ - It is preferred for you to ask for information, don't assume anything that you do not know!
1268
+ - When the user provides information that is useful for future use, you should update your memories immediately.
1269
+
1270
+ **When to update memories:**
1271
+ - When the user explicitly asks you to remember something (e.g., "remember my email", "save this preference")
1272
+ - When the user describes your role or how you should behave (e.g., "you are a web researcher", "always do X")
1273
+ - When the user gives feedback on your work - capture what was wrong and how to improve
1274
+ - When the user provides information required for tool use (e.g., slack channel ID, email addresses)
1275
+ - When the user provides context useful for future tasks, such as how to use tools, or which actions to take in a particular situation
1276
+ - When you discover new patterns or preferences (coding styles, conventions, workflows)
1277
+
1278
+ **When to NOT update memories:**
1279
+ - When the information is temporary or transient (e.g., "I'm running late", "I'm on my phone right now")
1280
+ - When the information is a one-time task request (e.g., "Find me a recipe", "What's 25 * 4?")
1281
+ - When the information is a simple question that doesn't reveal lasting preferences (e.g., "What day is it?", "Can you explain X?")
1282
+ - When the information is an acknowledgment or small talk (e.g., "Sounds good!", "Hello", "Thanks for that")
1283
+ - When the information is stale or irrelevant in future conversations
1284
+ - Never store API keys, access tokens, passwords, or any other credentials in any file, memory, or system prompt.
1285
+ - If the user asks where to put API keys or provides an API key, do NOT echo or save it.
1286
+
1287
+ **Examples:**
1288
+ Example 1 (remembering user information):
1289
+ User: Can you connect to my google account?
1290
+ Agent: Sure, I'll connect to your google account, what's your google account email?
1291
+ User: john@example.com
1292
+ Agent: Let me save this to my memory.
1293
+ Tool Call: edit_file(...) -> remembers that the user's google account email is john@example.com
1294
+
1295
+ Example 2 (remembering implicit user preferences):
1296
+ User: Can you write me an example for creating a deep agent in LangChain?
1297
+ Agent: Sure, I'll write you an example for creating a deep agent in LangChain <example code in Python>
1298
+ User: Can you do this in JavaScript
1299
+ Agent: Let me save this to my memory.
1300
+ Tool Call: edit_file(...) -> remembers that the user prefers to get LangChain code examples in JavaScript
1301
+ Agent: Sure, here is the JavaScript example<example code in JavaScript>
1302
+
1303
+ Example 3 (do not remember transient information):
1304
+ User: I'm going to play basketball tonight so I will be offline for a few hours.
1305
+ Agent: Okay I'll add a block to your calendar.
1306
+ Tool Call: create_calendar_event(...) -> just calls a tool, does not commit anything to memory, as it is transient information
1307
+ </memory_guidelines>`;
1308
+ /**
1309
+ * Format loaded memory contents for injection into prompt.
1310
+ * Pairs memory locations with their contents for clarity.
1311
+ */
1312
+ function formatMemoryContents(contents, sources) {
1313
+ if (Object.keys(contents).length === 0) return "(No memory loaded)";
1314
+ const sections = [];
1315
+ for (const path$4 of sources) if (contents[path$4]) sections.push(`${path$4}\n${contents[path$4]}`);
1316
+ if (sections.length === 0) return "(No memory loaded)";
1317
+ return sections.join("\n\n");
1318
+ }
1319
+ /**
1320
+ * Load memory content from a backend path.
1321
+ *
1322
+ * @param backend - Backend to load from.
1323
+ * @param path - Path to the AGENTS.md file.
1324
+ * @returns File content if found, null otherwise.
1325
+ */
1326
+ async function loadMemoryFromBackend(backend, path$4) {
1327
+ const results = await backend.downloadFiles([path$4]);
1328
+ if (results.length !== 1) throw new Error(`Expected 1 response for path ${path$4}, got ${results.length}`);
1329
+ const response = results[0];
1330
+ if (response.error != null) {
1331
+ if (response.error === "file_not_found") return null;
1332
+ throw new Error(`Failed to download ${path$4}: ${response.error}`);
1333
+ }
1334
+ if (response.content != null) return new TextDecoder().decode(response.content);
1335
+ return null;
1336
+ }
1337
+ /**
1338
+ * Create middleware for loading agent memory from AGENTS.md files.
1339
+ *
1340
+ * Loads memory content from configured sources and injects into the system prompt.
1341
+ * Supports multiple sources that are combined together.
1342
+ *
1343
+ * @param options - Configuration options
1344
+ * @returns AgentMiddleware for memory loading and injection
1345
+ *
1346
+ * @example
1347
+ * ```typescript
1348
+ * const middleware = createMemoryMiddleware({
1349
+ * backend: new FilesystemBackend({ rootDir: "/" }),
1350
+ * sources: [
1351
+ * "~/.deepagents/AGENTS.md",
1352
+ * "./.deepagents/AGENTS.md",
1353
+ * ],
1354
+ * });
1355
+ * ```
1356
+ */
1357
+ function createMemoryMiddleware(options) {
1358
+ const { backend, sources } = options;
1359
+ /**
1360
+ * Resolve backend from instance or factory.
1361
+ */
1362
+ function getBackend$1(state) {
1363
+ if (typeof backend === "function") return backend({ state });
1364
+ return backend;
1365
+ }
1366
+ return (0, langchain.createMiddleware)({
1367
+ name: "MemoryMiddleware",
1368
+ stateSchema: MemoryStateSchema,
1369
+ async beforeAgent(state) {
1370
+ if ("memoryContents" in state && state.memoryContents != null) return;
1371
+ const resolvedBackend = getBackend$1(state);
1372
+ const contents = {};
1373
+ for (const path$4 of sources) try {
1374
+ const content = await loadMemoryFromBackend(resolvedBackend, path$4);
1375
+ if (content) contents[path$4] = content;
1376
+ } catch (error) {
1377
+ console.debug(`Failed to load memory from ${path$4}:`, error);
1378
+ }
1379
+ return { memoryContents: contents };
1380
+ },
1381
+ wrapModelCall(request, handler) {
1382
+ const formattedContents = formatMemoryContents(request.state?.memoryContents || {}, sources);
1383
+ const memorySection = MEMORY_SYSTEM_PROMPT.replace("{memory_contents}", formattedContents);
1384
+ const currentSystemPrompt = request.systemPrompt || "";
1385
+ const newSystemPrompt = currentSystemPrompt ? `${memorySection}\n\n${currentSystemPrompt}` : memorySection;
1386
+ return handler({
1387
+ ...request,
1388
+ systemPrompt: newSystemPrompt
1389
+ });
1390
+ }
1391
+ });
1392
+ }
1393
+
1394
+ //#endregion
1395
+ //#region src/middleware/skills.ts
1396
+ /**
1397
+ * Backend-agnostic skills middleware for loading agent skills from any backend.
1398
+ *
1399
+ * This middleware implements Anthropic's agent skills pattern with progressive disclosure,
1400
+ * loading skills from backend storage via configurable sources.
1401
+ *
1402
+ * ## Architecture
1403
+ *
1404
+ * Skills are loaded from one or more **sources** - paths in a backend where skills are
1405
+ * organized. Sources are loaded in order, with later sources overriding earlier ones
1406
+ * when skills have the same name (last one wins). This enables layering: base -> user
1407
+ * -> project -> team skills.
1408
+ *
1409
+ * The middleware uses backend APIs exclusively (no direct filesystem access), making it
1410
+ * portable across different storage backends (filesystem, state, remote storage, etc.).
1411
+ *
1412
+ * ## Usage
1413
+ *
1414
+ * ```typescript
1415
+ * import { createSkillsMiddleware, FilesystemBackend } from "@anthropic/deepagents";
1416
+ *
1417
+ * const middleware = createSkillsMiddleware({
1418
+ * backend: new FilesystemBackend({ rootDir: "/" }),
1419
+ * sources: [
1420
+ * "/skills/user/",
1421
+ * "/skills/project/",
1422
+ * ],
1423
+ * });
1424
+ *
1425
+ * const agent = createDeepAgent({ middleware: [middleware] });
1426
+ * ```
1427
+ *
1428
+ * Or use the `skills` parameter on createDeepAgent:
1429
+ *
1430
+ * ```typescript
1431
+ * const agent = createDeepAgent({
1432
+ * skills: ["/skills/user/", "/skills/project/"],
1433
+ * });
1434
+ * ```
1435
+ */
1436
+ const MAX_SKILL_FILE_SIZE = 10 * 1024 * 1024;
1437
+ const MAX_SKILL_NAME_LENGTH = 64;
1438
+ const MAX_SKILL_DESCRIPTION_LENGTH = 1024;
1439
+ /**
1440
+ * State schema for skills middleware.
1441
+ */
1442
+ const SkillsStateSchema = zod.z.object({ skillsMetadata: zod.z.array(zod.z.object({
1443
+ name: zod.z.string(),
1444
+ description: zod.z.string(),
1445
+ path: zod.z.string(),
1446
+ license: zod.z.string().nullable().optional(),
1447
+ compatibility: zod.z.string().nullable().optional(),
1448
+ metadata: zod.z.record(zod.z.string(), zod.z.string()).optional(),
1449
+ allowedTools: zod.z.array(zod.z.string()).optional()
1450
+ })).optional() });
1451
+ /**
1452
+ * Skills System Documentation prompt template.
1453
+ */
1454
+ const SKILLS_SYSTEM_PROMPT = `
1455
+ ## Skills System
1456
+
1457
+ You have access to a skills library that provides specialized capabilities and domain knowledge.
1458
+
1459
+ {skills_locations}
1460
+
1461
+ **Available Skills:**
1462
+
1463
+ {skills_list}
1464
+
1465
+ **How to Use Skills (Progressive Disclosure):**
1466
+
1467
+ Skills follow a **progressive disclosure** pattern - you know they exist (name + description above), but you only read the full instructions when needed:
1468
+
1469
+ 1. **Recognize when a skill applies**: Check if the user's task matches any skill's description
1470
+ 2. **Read the skill's full instructions**: The skill list above shows the exact path to use with read_file
1471
+ 3. **Follow the skill's instructions**: SKILL.md contains step-by-step workflows, best practices, and examples
1472
+ 4. **Access supporting files**: Skills may include Python scripts, configs, or reference docs - use absolute paths
1473
+
1474
+ **When to Use Skills:**
1475
+ - When the user's request matches a skill's domain (e.g., "research X" → web-research skill)
1476
+ - When you need specialized knowledge or structured workflows
1477
+ - When a skill provides proven patterns for complex tasks
1478
+
1479
+ **Skills are Self-Documenting:**
1480
+ - Each SKILL.md tells you exactly what the skill does and how to use it
1481
+ - The skill list above shows the full path for each skill's SKILL.md file
1482
+
1483
+ **Executing Skill Scripts:**
1484
+ Skills may contain Python scripts or other executable files. Always use absolute paths from the skill list.
1485
+
1486
+ **Example Workflow:**
1487
+
1488
+ User: "Can you research the latest developments in quantum computing?"
1489
+
1490
+ 1. Check available skills above → See "web-research" skill with its full path
1491
+ 2. Read the skill using the path shown in the list
1492
+ 3. Follow the skill's research workflow (search → organize → synthesize)
1493
+ 4. Use any helper scripts with absolute paths
1494
+
1495
+ Remember: Skills are tools to make you more capable and consistent. When in doubt, check if a skill exists for the task!
1496
+ `;
1497
+ /**
1498
+ * Validate skill name per Agent Skills specification.
1499
+ */
1500
+ function validateSkillName$1(name, directoryName) {
1501
+ if (!name) return {
1502
+ valid: false,
1503
+ error: "name is required"
1504
+ };
1505
+ if (name.length > MAX_SKILL_NAME_LENGTH) return {
1506
+ valid: false,
1507
+ error: "name exceeds 64 characters"
1508
+ };
1509
+ if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(name)) return {
1510
+ valid: false,
1511
+ error: "name must be lowercase alphanumeric with single hyphens only"
1512
+ };
1513
+ if (name !== directoryName) return {
1514
+ valid: false,
1515
+ error: `name '${name}' must match directory name '${directoryName}'`
1516
+ };
1517
+ return {
1518
+ valid: true,
1519
+ error: ""
1520
+ };
1521
+ }
1522
+ /**
1523
+ * Parse YAML frontmatter from SKILL.md content.
1524
+ */
1525
+ function parseSkillMetadataFromContent(content, skillPath, directoryName) {
1526
+ if (content.length > MAX_SKILL_FILE_SIZE) {
1527
+ console.warn(`Skipping ${skillPath}: content too large (${content.length} bytes)`);
1528
+ return null;
1529
+ }
1530
+ const match = content.match(/^---\s*\n([\s\S]*?)\n---\s*\n/);
1531
+ if (!match) {
1532
+ console.warn(`Skipping ${skillPath}: no valid YAML frontmatter found`);
1533
+ return null;
1534
+ }
1535
+ const frontmatterStr = match[1];
1536
+ let frontmatterData;
1537
+ try {
1538
+ frontmatterData = yaml.default.parse(frontmatterStr);
1539
+ } catch (e) {
1540
+ console.warn(`Invalid YAML in ${skillPath}:`, e);
1541
+ return null;
1542
+ }
1543
+ if (!frontmatterData || typeof frontmatterData !== "object") {
1544
+ console.warn(`Skipping ${skillPath}: frontmatter is not a mapping`);
1545
+ return null;
1546
+ }
1547
+ const name = frontmatterData.name;
1548
+ const description = frontmatterData.description;
1549
+ if (!name || !description) {
1550
+ console.warn(`Skipping ${skillPath}: missing required 'name' or 'description'`);
1551
+ return null;
1552
+ }
1553
+ const validation = validateSkillName$1(String(name), directoryName);
1554
+ if (!validation.valid) console.warn(`Skill '${name}' in ${skillPath} does not follow Agent Skills specification: ${validation.error}. Consider renaming for spec compliance.`);
1555
+ let descriptionStr = String(description).trim();
1556
+ if (descriptionStr.length > MAX_SKILL_DESCRIPTION_LENGTH) {
1557
+ console.warn(`Description exceeds ${MAX_SKILL_DESCRIPTION_LENGTH} characters in ${skillPath}, truncating`);
1558
+ descriptionStr = descriptionStr.slice(0, MAX_SKILL_DESCRIPTION_LENGTH);
1559
+ }
1560
+ const allowedToolsStr = frontmatterData["allowed-tools"];
1561
+ const allowedTools = allowedToolsStr ? allowedToolsStr.split(" ") : [];
1562
+ return {
1563
+ name: String(name),
1564
+ description: descriptionStr,
1565
+ path: skillPath,
1566
+ metadata: frontmatterData.metadata || {},
1567
+ license: typeof frontmatterData.license === "string" ? frontmatterData.license.trim() || null : null,
1568
+ compatibility: typeof frontmatterData.compatibility === "string" ? frontmatterData.compatibility.trim() || null : null,
1569
+ allowedTools
1570
+ };
1571
+ }
1572
+ /**
1573
+ * List all skills from a backend source.
1574
+ */
1575
+ async function listSkillsFromBackend(backend, sourcePath) {
1576
+ const skills = [];
1577
+ const normalizedPath = sourcePath.endsWith("/") ? sourcePath : `${sourcePath}/`;
1578
+ let fileInfos;
1579
+ try {
1580
+ fileInfos = await backend.lsInfo(normalizedPath);
1581
+ } catch {
1582
+ return [];
1583
+ }
1584
+ const entries = fileInfos.map((info) => ({
1585
+ name: info.path.replace(/\/$/, "").split("/").pop() || "",
1586
+ type: info.is_dir ? "directory" : "file"
1587
+ }));
1588
+ for (const entry of entries) {
1589
+ if (entry.type !== "directory") continue;
1590
+ const skillMdPath = `${normalizedPath}${entry.name}/SKILL.md`;
1591
+ const results = await backend.downloadFiles([skillMdPath]);
1592
+ if (results.length !== 1) continue;
1593
+ const response = results[0];
1594
+ if (response.error != null || response.content == null) continue;
1595
+ const metadata = parseSkillMetadataFromContent(new TextDecoder().decode(response.content), skillMdPath, entry.name);
1596
+ if (metadata) skills.push(metadata);
1597
+ }
1598
+ return skills;
1599
+ }
1600
+ /**
1601
+ * Format skills locations for display in system prompt.
1602
+ */
1603
+ function formatSkillsLocations(sources) {
1604
+ if (sources.length === 0) return "**Skills Sources:** None configured";
1605
+ const lines = ["**Skills Sources:**"];
1606
+ for (const source of sources) lines.push(`- \`${source}\``);
1607
+ return lines.join("\n");
1608
+ }
1609
+ /**
1610
+ * Format skills metadata for display in system prompt.
1611
+ */
1612
+ function formatSkillsList(skills, sources) {
1613
+ if (skills.length === 0) return `(No skills available yet. Skills can be created in ${sources.join(" or ")})`;
1614
+ const lines = [];
1615
+ for (const skill of skills) {
1616
+ lines.push(`- **${skill.name}**: ${skill.description}`);
1617
+ lines.push(` → Read \`${skill.path}\` for full instructions`);
1618
+ }
1619
+ return lines.join("\n");
1620
+ }
1621
+ /**
1622
+ * Create backend-agnostic middleware for loading and exposing agent skills.
1623
+ *
1624
+ * This middleware loads skills from configurable backend sources and injects
1625
+ * skill metadata into the system prompt. It implements the progressive disclosure
1626
+ * pattern: skill names and descriptions are shown in the prompt, but the agent
1627
+ * reads full SKILL.md content only when needed.
1628
+ *
1629
+ * @param options - Configuration options
1630
+ * @returns AgentMiddleware for skills loading and injection
1631
+ *
1632
+ * @example
1633
+ * ```typescript
1634
+ * const middleware = createSkillsMiddleware({
1635
+ * backend: new FilesystemBackend({ rootDir: "/" }),
1636
+ * sources: ["/skills/user/", "/skills/project/"],
1637
+ * });
1638
+ * ```
1639
+ */
1640
+ function createSkillsMiddleware(options) {
1641
+ const { backend, sources } = options;
1642
+ let loadedSkills = [];
1643
+ /**
1644
+ * Resolve backend from instance or factory.
1645
+ */
1646
+ function getBackend$1(state) {
1647
+ if (typeof backend === "function") return backend({ state });
1648
+ return backend;
1649
+ }
1650
+ return (0, langchain.createMiddleware)({
1651
+ name: "SkillsMiddleware",
1652
+ stateSchema: SkillsStateSchema,
1653
+ async beforeAgent(state) {
1654
+ if (loadedSkills.length > 0) return;
1655
+ if ("skillsMetadata" in state && state.skillsMetadata != null) {
1656
+ loadedSkills = state.skillsMetadata;
1657
+ return;
1658
+ }
1659
+ const resolvedBackend = getBackend$1(state);
1660
+ const allSkills = /* @__PURE__ */ new Map();
1661
+ for (const sourcePath of sources) try {
1662
+ const skills = await listSkillsFromBackend(resolvedBackend, sourcePath);
1663
+ for (const skill of skills) allSkills.set(skill.name, skill);
1664
+ } catch (error) {
1665
+ console.debug(`[BackendSkillsMiddleware] Failed to load skills from ${sourcePath}:`, error);
1666
+ }
1667
+ loadedSkills = Array.from(allSkills.values());
1668
+ return { skillsMetadata: loadedSkills };
1669
+ },
1670
+ wrapModelCall(request, handler) {
1671
+ const skillsMetadata = loadedSkills.length > 0 ? loadedSkills : request.state?.skillsMetadata || [];
1672
+ const skillsLocations = formatSkillsLocations(sources);
1673
+ const skillsList = formatSkillsList(skillsMetadata, sources);
1674
+ const skillsSection = SKILLS_SYSTEM_PROMPT.replace("{skills_locations}", skillsLocations).replace("{skills_list}", skillsList);
1675
+ const currentSystemPrompt = request.systemPrompt || "";
1676
+ const newSystemPrompt = currentSystemPrompt ? `${currentSystemPrompt}\n\n${skillsSection}` : skillsSection;
1677
+ return handler({
1678
+ ...request,
1679
+ systemPrompt: newSystemPrompt
1680
+ });
1681
+ }
1682
+ });
1683
+ }
1684
+
1201
1685
  //#endregion
1202
1686
  //#region src/backends/store.ts
1203
1687
  /**
@@ -1345,27 +1829,18 @@ var StoreBackend = class {
1345
1829
  * @param limit - Maximum number of lines to read
1346
1830
  * @returns Formatted file content with line numbers, or error message
1347
1831
  */
1348
- async read(filePath, offset = 0, limit = 2e3) {
1832
+ async read(filePath, offset = 0, limit = 500) {
1349
1833
  try {
1350
- return formatReadResponse(await this.readRaw(filePath), offset, limit);
1834
+ const store = this.getStore();
1835
+ const namespace = this.getNamespace();
1836
+ const item = await store.get(namespace, filePath);
1837
+ if (!item) return `Error: File '${filePath}' not found`;
1838
+ return formatReadResponse(this.convertStoreItemToFileData(item), offset, limit);
1351
1839
  } catch (e) {
1352
1840
  return `Error: ${e.message}`;
1353
1841
  }
1354
1842
  }
1355
1843
  /**
1356
- * Read file content as raw FileData.
1357
- *
1358
- * @param filePath - Absolute file path
1359
- * @returns Raw file content as FileData
1360
- */
1361
- async readRaw(filePath) {
1362
- const store = this.getStore();
1363
- const namespace = this.getNamespace();
1364
- const item = await store.get(namespace, filePath);
1365
- if (!item) throw new Error(`File '${filePath}' not found`);
1366
- return this.convertStoreItemToFileData(item);
1367
- }
1368
- /**
1369
1844
  * Create a new file with content.
1370
1845
  * Returns WriteResult. External storage sets filesUpdate=null.
1371
1846
  */
@@ -1639,7 +2114,7 @@ var FilesystemBackend = class {
1639
2114
  * @param limit - Maximum number of lines to read
1640
2115
  * @returns Formatted file content with line numbers, or error message
1641
2116
  */
1642
- async read(filePath, offset = 0, limit = 2e3) {
2117
+ async read(filePath, offset = 0, limit = 500) {
1643
2118
  try {
1644
2119
  const resolvedPath = this.resolvePath(filePath);
1645
2120
  let content;
@@ -1669,37 +2144,6 @@ var FilesystemBackend = class {
1669
2144
  }
1670
2145
  }
1671
2146
  /**
1672
- * Read file content as raw FileData.
1673
- *
1674
- * @param filePath - Absolute file path
1675
- * @returns Raw file content as FileData
1676
- */
1677
- async readRaw(filePath) {
1678
- const resolvedPath = this.resolvePath(filePath);
1679
- let content;
1680
- let stat;
1681
- if (SUPPORTS_NOFOLLOW) {
1682
- stat = await node_fs_promises.default.stat(resolvedPath);
1683
- if (!stat.isFile()) throw new Error(`File '${filePath}' not found`);
1684
- const fd = await node_fs_promises.default.open(resolvedPath, node_fs.default.constants.O_RDONLY | node_fs.default.constants.O_NOFOLLOW);
1685
- try {
1686
- content = await fd.readFile({ encoding: "utf-8" });
1687
- } finally {
1688
- await fd.close();
1689
- }
1690
- } else {
1691
- stat = await node_fs_promises.default.lstat(resolvedPath);
1692
- if (stat.isSymbolicLink()) throw new Error(`Symlinks are not allowed: ${filePath}`);
1693
- if (!stat.isFile()) throw new Error(`File '${filePath}' not found`);
1694
- content = await node_fs_promises.default.readFile(resolvedPath, "utf-8");
1695
- }
1696
- return {
1697
- content: content.split("\n"),
1698
- created_at: stat.ctime.toISOString(),
1699
- modified_at: stat.mtime.toISOString()
1700
- };
1701
- }
1702
- /**
1703
2147
  * Create a new file with content.
1704
2148
  * Returns WriteResult. External storage sets filesUpdate=null.
1705
2149
  */
@@ -2102,21 +2546,11 @@ var CompositeBackend = class {
2102
2546
  * @param limit - Maximum number of lines to read
2103
2547
  * @returns Formatted file content with line numbers, or error message
2104
2548
  */
2105
- async read(filePath, offset = 0, limit = 2e3) {
2549
+ async read(filePath, offset = 0, limit = 500) {
2106
2550
  const [backend, strippedKey] = this.getBackendAndKey(filePath);
2107
2551
  return await backend.read(strippedKey, offset, limit);
2108
2552
  }
2109
2553
  /**
2110
- * Read file content as raw FileData.
2111
- *
2112
- * @param filePath - Absolute file path
2113
- * @returns Raw file content as FileData
2114
- */
2115
- async readRaw(filePath) {
2116
- const [backend, strippedKey] = this.getBackendAndKey(filePath);
2117
- return await backend.readRaw(strippedKey);
2118
- }
2119
- /**
2120
2554
  * Structured search results or error string for invalid input.
2121
2555
  */
2122
2556
  async grepRaw(pattern, path$4 = "/", glob = null) {
@@ -2553,39 +2987,17 @@ var BaseSandbox = class {
2553
2987
  * @param limit - Maximum number of lines to read
2554
2988
  * @returns Formatted file content with line numbers, or error message
2555
2989
  */
2556
- async read(filePath, offset = 0, limit = 2e3) {
2990
+ async read(filePath, offset = 0, limit = 500) {
2557
2991
  const command = buildReadCommand(filePath, offset, limit);
2558
2992
  const result = await this.execute(command);
2559
2993
  if (result.exitCode !== 0) return `Error: File '${filePath}' not found`;
2560
2994
  return result.output;
2561
2995
  }
2562
2996
  /**
2563
- * Read file content as raw FileData.
2564
- *
2565
- * @param filePath - Absolute file path
2566
- * @returns Raw file content as FileData
2997
+ * Structured search results or error string for invalid input.
2567
2998
  */
2568
- async readRaw(filePath) {
2569
- const command = buildReadCommand(filePath, 0, Number.MAX_SAFE_INTEGER);
2570
- const result = await this.execute(command);
2571
- if (result.exitCode !== 0) throw new Error(`File '${filePath}' not found`);
2572
- const lines = [];
2573
- for (const line of result.output.split("\n")) {
2574
- const tabIndex = line.indexOf(" ");
2575
- if (tabIndex !== -1) lines.push(line.substring(tabIndex + 1));
2576
- }
2577
- const now = (/* @__PURE__ */ new Date()).toISOString();
2578
- return {
2579
- content: lines,
2580
- created_at: now,
2581
- modified_at: now
2582
- };
2583
- }
2584
- /**
2585
- * Structured search results or error string for invalid input.
2586
- */
2587
- async grepRaw(pattern, path$4 = "/", glob = null) {
2588
- const command = buildGrepCommand(pattern, path$4, glob);
2999
+ async grepRaw(pattern, path$4 = "/", glob = null) {
3000
+ const command = buildGrepCommand(pattern, path$4, glob);
2589
3001
  const result = await this.execute(command);
2590
3002
  if (result.exitCode === 1) {
2591
3003
  if (result.output.includes("Invalid regex:")) return result.output.trim();
@@ -2687,7 +3099,7 @@ const BASE_PROMPT = `In order to complete the objective that the user asks of yo
2687
3099
  * ```
2688
3100
  */
2689
3101
  function createDeepAgent(params = {}) {
2690
- const { model = "claude-sonnet-4-5-20250929", tools = [], systemPrompt, middleware: customMiddleware = [], subagents = [], responseFormat, contextSchema, checkpointer, store, backend, interruptOn, name } = params;
3102
+ const { model = "claude-sonnet-4-5-20250929", tools = [], systemPrompt, middleware: customMiddleware = [], subagents = [], responseFormat, contextSchema, checkpointer, store, backend, interruptOn, name, memory, skills } = params;
2691
3103
  const finalSystemPrompt = systemPrompt ? typeof systemPrompt === "string" ? `${systemPrompt}\n\n${BASE_PROMPT}` : new langchain.SystemMessage({ content: [{
2692
3104
  type: "text",
2693
3105
  text: BASE_PROMPT
@@ -2696,14 +3108,20 @@ function createDeepAgent(params = {}) {
2696
3108
  text: systemPrompt.content
2697
3109
  }] : systemPrompt.content] }) : BASE_PROMPT;
2698
3110
  const filesystemBackend = backend ? backend : (config) => new StateBackend(config);
3111
+ const skillsMiddleware = skills != null && skills.length > 0 ? [createSkillsMiddleware({
3112
+ backend: filesystemBackend,
3113
+ sources: skills
3114
+ })] : [];
2699
3115
  const builtInMiddleware = [
2700
3116
  (0, langchain.todoListMiddleware)(),
3117
+ ...skillsMiddleware,
2701
3118
  createFilesystemMiddleware({ backend: filesystemBackend }),
2702
3119
  createSubAgentMiddleware({
2703
3120
  defaultModel: model,
2704
3121
  defaultTools: tools,
2705
3122
  defaultMiddleware: [
2706
3123
  (0, langchain.todoListMiddleware)(),
3124
+ ...skillsMiddleware,
2707
3125
  createFilesystemMiddleware({ backend: filesystemBackend }),
2708
3126
  (0, langchain.summarizationMiddleware)({
2709
3127
  model,
@@ -2723,7 +3141,11 @@ function createDeepAgent(params = {}) {
2723
3141
  keep: { messages: 6 }
2724
3142
  }),
2725
3143
  (0, langchain.anthropicPromptCachingMiddleware)({ unsupportedModelBehavior: "ignore" }),
2726
- createPatchToolCallsMiddleware()
3144
+ createPatchToolCallsMiddleware(),
3145
+ ...memory != null && memory.length > 0 ? [createMemoryMiddleware({
3146
+ backend: filesystemBackend,
3147
+ sources: memory
3148
+ })] : []
2727
3149
  ];
2728
3150
  if (interruptOn) builtInMiddleware.push((0, langchain.humanInTheLoopMiddleware)({ interruptOn }));
2729
3151
  return (0, langchain.createAgent)({
@@ -2833,6 +3255,206 @@ function createSettings(options = {}) {
2833
3255
  };
2834
3256
  }
2835
3257
 
3258
+ //#endregion
3259
+ //#region src/middleware/agent-memory.ts
3260
+ /**
3261
+ * Middleware for loading agent-specific long-term memory into the system prompt.
3262
+ *
3263
+ * This middleware loads the agent's long-term memory from agent.md files
3264
+ * and injects it into the system prompt. Memory is loaded from:
3265
+ * - User memory: ~/.deepagents/{agent_name}/agent.md
3266
+ * - Project memory: {project_root}/.deepagents/agent.md
3267
+ */
3268
+ /**
3269
+ * State schema for agent memory middleware.
3270
+ */
3271
+ const AgentMemoryStateSchema = zod.z.object({
3272
+ userMemory: zod.z.string().optional(),
3273
+ projectMemory: zod.z.string().optional()
3274
+ });
3275
+ /**
3276
+ * Default template for memory injection.
3277
+ */
3278
+ const DEFAULT_MEMORY_TEMPLATE = `<user_memory>
3279
+ {user_memory}
3280
+ </user_memory>
3281
+
3282
+ <project_memory>
3283
+ {project_memory}
3284
+ </project_memory>`;
3285
+ /**
3286
+ * Long-term Memory Documentation system prompt.
3287
+ */
3288
+ const LONGTERM_MEMORY_SYSTEM_PROMPT = `
3289
+
3290
+ ## Long-term Memory
3291
+
3292
+ Your long-term memory is stored in files on the filesystem and persists across sessions.
3293
+
3294
+ **User Memory Location**: \`{agent_dir_absolute}\` (displays as \`{agent_dir_display}\`)
3295
+ **Project Memory Location**: {project_memory_info}
3296
+
3297
+ Your system prompt is loaded from TWO sources at startup:
3298
+ 1. **User agent.md**: \`{agent_dir_absolute}/agent.md\` - Your personal preferences across all projects
3299
+ 2. **Project agent.md**: Loaded from project root if available - Project-specific instructions
3300
+
3301
+ Project-specific agent.md is loaded from these locations (both combined if both exist):
3302
+ - \`[project-root]/.deepagents/agent.md\` (preferred)
3303
+ - \`[project-root]/agent.md\` (fallback, but also included if both exist)
3304
+
3305
+ **When to CHECK/READ memories (CRITICAL - do this FIRST):**
3306
+ - **At the start of ANY new session**: Check both user and project memories
3307
+ - User: \`ls {agent_dir_absolute}\`
3308
+ - Project: \`ls {project_deepagents_dir}\` (if in a project)
3309
+ - **BEFORE answering questions**: If asked "what do you know about X?" or "how do I do Y?", check project memories FIRST, then user
3310
+ - **When user asks you to do something**: Check if you have project-specific guides or examples
3311
+ - **When user references past work**: Search project memory files for related context
3312
+
3313
+ **Memory-first response pattern:**
3314
+ 1. User asks a question → Check project directory first: \`ls {project_deepagents_dir}\`
3315
+ 2. If relevant files exist → Read them with \`read_file '{project_deepagents_dir}/[filename]'\`
3316
+ 3. Check user memory if needed → \`ls {agent_dir_absolute}\`
3317
+ 4. Base your answer on saved knowledge supplemented by general knowledge
3318
+
3319
+ **When to update memories:**
3320
+ - **IMMEDIATELY when the user describes your role or how you should behave**
3321
+ - **IMMEDIATELY when the user gives feedback on your work** - Update memories to capture what was wrong and how to do it better
3322
+ - When the user explicitly asks you to remember something
3323
+ - When patterns or preferences emerge (coding styles, conventions, workflows)
3324
+ - After significant work where context would help in future sessions
3325
+
3326
+ **Learning from feedback:**
3327
+ - When user says something is better/worse, capture WHY and encode it as a pattern
3328
+ - Each correction is a chance to improve permanently - don't just fix the immediate issue, update your instructions
3329
+ - When user says "you should remember X" or "be careful about Y", treat this as HIGH PRIORITY - update memories IMMEDIATELY
3330
+ - Look for the underlying principle behind corrections, not just the specific mistake
3331
+
3332
+ ## Deciding Where to Store Memory
3333
+
3334
+ When writing or updating agent memory, decide whether each fact, configuration, or behavior belongs in:
3335
+
3336
+ ### User Agent File: \`{agent_dir_absolute}/agent.md\`
3337
+ → Describes the agent's **personality, style, and universal behavior** across all projects.
3338
+
3339
+ **Store here:**
3340
+ - Your general tone and communication style
3341
+ - Universal coding preferences (formatting, comment style, etc.)
3342
+ - General workflows and methodologies you follow
3343
+ - Tool usage patterns that apply everywhere
3344
+ - Personal preferences that don't change per-project
3345
+
3346
+ **Examples:**
3347
+ - "Be concise and direct in responses"
3348
+ - "Always use type hints in Python"
3349
+ - "Prefer functional programming patterns"
3350
+
3351
+ ### Project Agent File: \`{project_deepagents_dir}/agent.md\`
3352
+ → Describes **how this specific project works** and **how the agent should behave here only.**
3353
+
3354
+ **Store here:**
3355
+ - Project-specific architecture and design patterns
3356
+ - Coding conventions specific to this codebase
3357
+ - Project structure and organization
3358
+ - Testing strategies for this project
3359
+ - Deployment processes and workflows
3360
+ - Team conventions and guidelines
3361
+
3362
+ **Examples:**
3363
+ - "This project uses FastAPI with SQLAlchemy"
3364
+ - "Tests go in tests/ directory mirroring src/ structure"
3365
+ - "All API changes require updating OpenAPI spec"
3366
+
3367
+ ### Project Memory Files: \`{project_deepagents_dir}/*.md\`
3368
+ → Use for **project-specific reference information** and structured notes.
3369
+
3370
+ **Store here:**
3371
+ - API design documentation
3372
+ - Architecture decisions and rationale
3373
+ - Deployment procedures
3374
+ - Common debugging patterns
3375
+ - Onboarding information
3376
+
3377
+ **Examples:**
3378
+ - \`{project_deepagents_dir}/api-design.md\` - REST API patterns used
3379
+ - \`{project_deepagents_dir}/architecture.md\` - System architecture overview
3380
+ - \`{project_deepagents_dir}/deployment.md\` - How to deploy this project
3381
+
3382
+ ### File Operations:
3383
+
3384
+ **User memory:**
3385
+ \`\`\`
3386
+ ls {agent_dir_absolute} # List user memory files
3387
+ read_file '{agent_dir_absolute}/agent.md' # Read user preferences
3388
+ edit_file '{agent_dir_absolute}/agent.md' ... # Update user preferences
3389
+ \`\`\`
3390
+
3391
+ **Project memory (preferred for project-specific information):**
3392
+ \`\`\`
3393
+ ls {project_deepagents_dir} # List project memory files
3394
+ read_file '{project_deepagents_dir}/agent.md' # Read project instructions
3395
+ edit_file '{project_deepagents_dir}/agent.md' ... # Update project instructions
3396
+ write_file '{project_deepagents_dir}/agent.md' ... # Create project memory file
3397
+ \`\`\`
3398
+
3399
+ **Important**:
3400
+ - Project memory files are stored in \`.deepagents/\` inside the project root
3401
+ - Always use absolute paths for file operations
3402
+ - Check project memories BEFORE user when answering project-specific questions`;
3403
+ /**
3404
+ * Create middleware for loading agent-specific long-term memory.
3405
+ *
3406
+ * This middleware loads the agent's long-term memory from a file (agent.md)
3407
+ * and injects it into the system prompt. The memory is loaded once at the
3408
+ * start of the conversation and stored in state.
3409
+ *
3410
+ * @param options - Configuration options
3411
+ * @returns AgentMiddleware for memory loading and injection
3412
+ */
3413
+ function createAgentMemoryMiddleware(options) {
3414
+ const { settings, assistantId, systemPromptTemplate } = options;
3415
+ const agentDir = settings.getAgentDir(assistantId);
3416
+ const agentDirDisplay = `~/.deepagents/${assistantId}`;
3417
+ const agentDirAbsolute = agentDir;
3418
+ const projectRoot = settings.projectRoot;
3419
+ const projectMemoryInfo = projectRoot ? `\`${projectRoot}\` (detected)` : "None (not in a git project)";
3420
+ const projectDeepagentsDir = projectRoot ? `${projectRoot}/.deepagents` : "[project-root]/.deepagents (not in a project)";
3421
+ const template = systemPromptTemplate || DEFAULT_MEMORY_TEMPLATE;
3422
+ return (0, langchain.createMiddleware)({
3423
+ name: "AgentMemoryMiddleware",
3424
+ stateSchema: AgentMemoryStateSchema,
3425
+ beforeAgent(state) {
3426
+ const result = {};
3427
+ if (!("userMemory" in state)) {
3428
+ const userPath = settings.getUserAgentMdPath(assistantId);
3429
+ if (node_fs.default.existsSync(userPath)) try {
3430
+ result.userMemory = node_fs.default.readFileSync(userPath, "utf-8");
3431
+ } catch {}
3432
+ }
3433
+ if (!("projectMemory" in state)) {
3434
+ const projectPath = settings.getProjectAgentMdPath();
3435
+ if (projectPath && node_fs.default.existsSync(projectPath)) try {
3436
+ result.projectMemory = node_fs.default.readFileSync(projectPath, "utf-8");
3437
+ } catch {}
3438
+ }
3439
+ return Object.keys(result).length > 0 ? result : void 0;
3440
+ },
3441
+ wrapModelCall(request, handler) {
3442
+ const userMemory = request.state?.userMemory;
3443
+ const projectMemory = request.state?.projectMemory;
3444
+ const baseSystemPrompt = request.systemPrompt || "";
3445
+ const memorySection = template.replace("{user_memory}", userMemory || "(No user agent.md)").replace("{project_memory}", projectMemory || "(No project agent.md)");
3446
+ const memoryDocs = LONGTERM_MEMORY_SYSTEM_PROMPT.replaceAll("{agent_dir_absolute}", agentDirAbsolute).replaceAll("{agent_dir_display}", agentDirDisplay).replaceAll("{project_memory_info}", projectMemoryInfo).replaceAll("{project_deepagents_dir}", projectDeepagentsDir);
3447
+ let systemPrompt = memorySection;
3448
+ if (baseSystemPrompt) systemPrompt += "\n\n" + baseSystemPrompt;
3449
+ systemPrompt += "\n\n" + memoryDocs;
3450
+ return handler({
3451
+ ...request,
3452
+ systemPrompt
3453
+ });
3454
+ }
3455
+ });
3456
+ }
3457
+
2836
3458
  //#endregion
2837
3459
  //#region src/skills/loader.ts
2838
3460
  /**
@@ -2861,10 +3483,10 @@ function createSettings(options = {}) {
2861
3483
  * @see https://agentskills.io/specification
2862
3484
  */
2863
3485
  /** Maximum size for SKILL.md files (10MB) */
2864
- const MAX_SKILL_FILE_SIZE = 10 * 1024 * 1024;
3486
+ const MAX_SKILL_FILE_SIZE$1 = 10 * 1024 * 1024;
2865
3487
  /** Agent Skills spec constraints */
2866
- const MAX_SKILL_NAME_LENGTH = 64;
2867
- const MAX_SKILL_DESCRIPTION_LENGTH = 1024;
3488
+ const MAX_SKILL_NAME_LENGTH$1 = 64;
3489
+ const MAX_SKILL_DESCRIPTION_LENGTH$1 = 1024;
2868
3490
  /** Pattern for validating skill names per Agent Skills spec */
2869
3491
  const SKILL_NAME_PATTERN = /^[a-z0-9]+(-[a-z0-9]+)*$/;
2870
3492
  /** Pattern for extracting YAML frontmatter */
@@ -2908,7 +3530,7 @@ function validateSkillName(name, directoryName) {
2908
3530
  valid: false,
2909
3531
  error: "name is required"
2910
3532
  };
2911
- if (name.length > MAX_SKILL_NAME_LENGTH) return {
3533
+ if (name.length > MAX_SKILL_NAME_LENGTH$1) return {
2912
3534
  valid: false,
2913
3535
  error: "name exceeds 64 characters"
2914
3536
  };
@@ -2948,7 +3570,7 @@ function parseFrontmatter(content) {
2948
3570
  function parseSkillMetadata(skillMdPath, source) {
2949
3571
  try {
2950
3572
  const stats = node_fs.default.statSync(skillMdPath);
2951
- if (stats.size > MAX_SKILL_FILE_SIZE) {
3573
+ if (stats.size > MAX_SKILL_FILE_SIZE$1) {
2952
3574
  console.warn(`Skipping ${skillMdPath}: file too large (${stats.size} bytes)`);
2953
3575
  return null;
2954
3576
  }
@@ -2967,9 +3589,9 @@ function parseSkillMetadata(skillMdPath, source) {
2967
3589
  const validation = validateSkillName(String(name), directoryName);
2968
3590
  if (!validation.valid) console.warn(`Skill '${name}' in ${skillMdPath} does not follow Agent Skills spec: ${validation.error}. Consider renaming to be spec-compliant.`);
2969
3591
  let descriptionStr = String(description);
2970
- if (descriptionStr.length > MAX_SKILL_DESCRIPTION_LENGTH) {
2971
- console.warn(`Description exceeds ${MAX_SKILL_DESCRIPTION_LENGTH} chars in ${skillMdPath}, truncating`);
2972
- descriptionStr = descriptionStr.slice(0, MAX_SKILL_DESCRIPTION_LENGTH);
3592
+ if (descriptionStr.length > MAX_SKILL_DESCRIPTION_LENGTH$1) {
3593
+ console.warn(`Description exceeds ${MAX_SKILL_DESCRIPTION_LENGTH$1} chars in ${skillMdPath}, truncating`);
3594
+ descriptionStr = descriptionStr.slice(0, MAX_SKILL_DESCRIPTION_LENGTH$1);
2973
3595
  }
2974
3596
  return {
2975
3597
  name: String(name),
@@ -3056,375 +3678,6 @@ function listSkills(options) {
3056
3678
  return Array.from(allSkills.values());
3057
3679
  }
3058
3680
 
3059
- //#endregion
3060
- //#region src/middleware/skills.ts
3061
- /**
3062
- * Middleware for loading and exposing agent skills to the system prompt.
3063
- *
3064
- * This middleware implements Anthropic's "Agent Skills" pattern with progressive disclosure:
3065
- * 1. Parse YAML frontmatter from SKILL.md files at session start
3066
- * 2. Inject skills metadata (name + description) into system prompt
3067
- * 3. Agent reads full SKILL.md content when relevant to a task
3068
- *
3069
- * Skills directory structure (per-agent + project):
3070
- * User-level: ~/.deepagents/{AGENT_NAME}/skills/
3071
- * Project-level: {PROJECT_ROOT}/.deepagents/skills/
3072
- *
3073
- * @example
3074
- * ```
3075
- * ~/.deepagents/{AGENT_NAME}/skills/
3076
- * ├── web-research/
3077
- * │ ├── SKILL.md # Required: YAML frontmatter + instructions
3078
- * │ └── helper.py # Optional: supporting files
3079
- * ├── code-review/
3080
- * │ ├── SKILL.md
3081
- * │ └── checklist.md
3082
- *
3083
- * .deepagents/skills/
3084
- * ├── project-specific/
3085
- * │ └── SKILL.md # Project-specific skills
3086
- * ```
3087
- */
3088
- /**
3089
- * State schema for skills middleware.
3090
- */
3091
- const SkillsStateSchema = zod.z.object({ skillsMetadata: zod.z.array(zod.z.object({
3092
- name: zod.z.string(),
3093
- description: zod.z.string(),
3094
- path: zod.z.string(),
3095
- source: zod.z.enum(["user", "project"]),
3096
- license: zod.z.string().optional(),
3097
- compatibility: zod.z.string().optional(),
3098
- metadata: zod.z.record(zod.z.string(), zod.z.string()).optional(),
3099
- allowedTools: zod.z.string().optional()
3100
- })).optional() });
3101
- /**
3102
- * Skills System Documentation prompt template.
3103
- */
3104
- const SKILLS_SYSTEM_PROMPT = `
3105
-
3106
- ## Skills System
3107
-
3108
- You have access to a skills library that provides specialized capabilities and domain knowledge.
3109
-
3110
- {skills_locations}
3111
-
3112
- **Available Skills:**
3113
-
3114
- {skills_list}
3115
-
3116
- **How to Use Skills (Progressive Disclosure):**
3117
-
3118
- Skills follow a **progressive disclosure** pattern - you know they exist (name + description above), but you only read the full instructions when needed:
3119
-
3120
- 1. **Recognize when a skill applies**: Check if the user's task matches any skill's description
3121
- 2. **Read the skill's full instructions**: The skill list above shows the exact path to use with read_file
3122
- 3. **Follow the skill's instructions**: SKILL.md contains step-by-step workflows, best practices, and examples
3123
- 4. **Access supporting files**: Skills may include Python scripts, configs, or reference docs - use absolute paths
3124
-
3125
- **When to Use Skills:**
3126
- - When the user's request matches a skill's domain (e.g., "research X" → web-research skill)
3127
- - When you need specialized knowledge or structured workflows
3128
- - When a skill provides proven patterns for complex tasks
3129
-
3130
- **Skills are Self-Documenting:**
3131
- - Each SKILL.md tells you exactly what the skill does and how to use it
3132
- - The skill list above shows the full path for each skill's SKILL.md file
3133
-
3134
- **Executing Skill Scripts:**
3135
- Skills may contain Python scripts or other executable files. Always use absolute paths from the skill list.
3136
-
3137
- **Example Workflow:**
3138
-
3139
- User: "Can you research the latest developments in quantum computing?"
3140
-
3141
- 1. Check available skills above → See "web-research" skill with its full path
3142
- 2. Read the skill using the path shown in the list
3143
- 3. Follow the skill's research workflow (search → organize → synthesize)
3144
- 4. Use any helper scripts with absolute paths
3145
-
3146
- Remember: Skills are tools to make you more capable and consistent. When in doubt, check if a skill exists for the task!
3147
- `;
3148
- /**
3149
- * Format skills locations for display in system prompt.
3150
- */
3151
- function formatSkillsLocations(userSkillsDisplay, projectSkillsDir) {
3152
- const locations = [`**User Skills**: \`${userSkillsDisplay}\``];
3153
- if (projectSkillsDir) locations.push(`**Project Skills**: \`${projectSkillsDir}\` (overrides user skills)`);
3154
- return locations.join("\n");
3155
- }
3156
- /**
3157
- * Format skills metadata for display in system prompt.
3158
- */
3159
- function formatSkillsList(skills, userSkillsDisplay, projectSkillsDir) {
3160
- if (skills.length === 0) {
3161
- const locations = [userSkillsDisplay];
3162
- if (projectSkillsDir) locations.push(projectSkillsDir);
3163
- return `(No skills available yet. You can create skills in ${locations.join(" or ")})`;
3164
- }
3165
- const userSkills = skills.filter((s) => s.source === "user");
3166
- const projectSkills = skills.filter((s) => s.source === "project");
3167
- const lines = [];
3168
- if (userSkills.length > 0) {
3169
- lines.push("**User Skills:**");
3170
- for (const skill of userSkills) {
3171
- lines.push(`- **${skill.name}**: ${skill.description}`);
3172
- lines.push(` → Read \`${skill.path}\` for full instructions`);
3173
- }
3174
- lines.push("");
3175
- }
3176
- if (projectSkills.length > 0) {
3177
- lines.push("**Project Skills:**");
3178
- for (const skill of projectSkills) {
3179
- lines.push(`- **${skill.name}**: ${skill.description}`);
3180
- lines.push(` → Read \`${skill.path}\` for full instructions`);
3181
- }
3182
- }
3183
- return lines.join("\n");
3184
- }
3185
- /**
3186
- * Create middleware for loading and exposing agent skills.
3187
- *
3188
- * This middleware implements Anthropic's agent skills pattern:
3189
- * - Loads skills metadata (name, description) from YAML frontmatter at session start
3190
- * - Injects skills list into system prompt for discoverability
3191
- * - Agent reads full SKILL.md content when a skill is relevant (progressive disclosure)
3192
- *
3193
- * Supports both user-level and project-level skills:
3194
- * - User skills: ~/.deepagents/{AGENT_NAME}/skills/
3195
- * - Project skills: {PROJECT_ROOT}/.deepagents/skills/
3196
- * - Project skills override user skills with the same name
3197
- *
3198
- * @param options - Configuration options
3199
- * @returns AgentMiddleware for skills loading and injection
3200
- */
3201
- function createSkillsMiddleware(options) {
3202
- const { skillsDir, assistantId, projectSkillsDir } = options;
3203
- const userSkillsDisplay = `~/.deepagents/${assistantId}/skills`;
3204
- return (0, langchain.createMiddleware)({
3205
- name: "SkillsMiddleware",
3206
- stateSchema: SkillsStateSchema,
3207
- beforeAgent() {
3208
- return { skillsMetadata: listSkills({
3209
- userSkillsDir: skillsDir,
3210
- projectSkillsDir
3211
- }) };
3212
- },
3213
- wrapModelCall(request, handler) {
3214
- const skillsMetadata = request.state?.skillsMetadata || [];
3215
- const skillsLocations = formatSkillsLocations(userSkillsDisplay, projectSkillsDir);
3216
- const skillsList = formatSkillsList(skillsMetadata, userSkillsDisplay, projectSkillsDir);
3217
- const skillsSection = SKILLS_SYSTEM_PROMPT.replace("{skills_locations}", skillsLocations).replace("{skills_list}", skillsList);
3218
- const currentSystemPrompt = request.systemPrompt || "";
3219
- const newSystemPrompt = currentSystemPrompt ? `${currentSystemPrompt}\n\n${skillsSection}` : skillsSection;
3220
- return handler({
3221
- ...request,
3222
- systemPrompt: newSystemPrompt
3223
- });
3224
- }
3225
- });
3226
- }
3227
-
3228
- //#endregion
3229
- //#region src/middleware/agent-memory.ts
3230
- /**
3231
- * Middleware for loading agent-specific long-term memory into the system prompt.
3232
- *
3233
- * This middleware loads the agent's long-term memory from agent.md files
3234
- * and injects it into the system prompt. Memory is loaded from:
3235
- * - User memory: ~/.deepagents/{agent_name}/agent.md
3236
- * - Project memory: {project_root}/.deepagents/agent.md
3237
- */
3238
- /**
3239
- * State schema for agent memory middleware.
3240
- */
3241
- const AgentMemoryStateSchema = zod.z.object({
3242
- userMemory: zod.z.string().optional(),
3243
- projectMemory: zod.z.string().optional()
3244
- });
3245
- /**
3246
- * Default template for memory injection.
3247
- */
3248
- const DEFAULT_MEMORY_TEMPLATE = `<user_memory>
3249
- {user_memory}
3250
- </user_memory>
3251
-
3252
- <project_memory>
3253
- {project_memory}
3254
- </project_memory>`;
3255
- /**
3256
- * Long-term Memory Documentation system prompt.
3257
- */
3258
- const LONGTERM_MEMORY_SYSTEM_PROMPT = `
3259
-
3260
- ## Long-term Memory
3261
-
3262
- Your long-term memory is stored in files on the filesystem and persists across sessions.
3263
-
3264
- **User Memory Location**: \`{agent_dir_absolute}\` (displays as \`{agent_dir_display}\`)
3265
- **Project Memory Location**: {project_memory_info}
3266
-
3267
- Your system prompt is loaded from TWO sources at startup:
3268
- 1. **User agent.md**: \`{agent_dir_absolute}/agent.md\` - Your personal preferences across all projects
3269
- 2. **Project agent.md**: Loaded from project root if available - Project-specific instructions
3270
-
3271
- Project-specific agent.md is loaded from these locations (both combined if both exist):
3272
- - \`[project-root]/.deepagents/agent.md\` (preferred)
3273
- - \`[project-root]/agent.md\` (fallback, but also included if both exist)
3274
-
3275
- **When to CHECK/READ memories (CRITICAL - do this FIRST):**
3276
- - **At the start of ANY new session**: Check both user and project memories
3277
- - User: \`ls {agent_dir_absolute}\`
3278
- - Project: \`ls {project_deepagents_dir}\` (if in a project)
3279
- - **BEFORE answering questions**: If asked "what do you know about X?" or "how do I do Y?", check project memories FIRST, then user
3280
- - **When user asks you to do something**: Check if you have project-specific guides or examples
3281
- - **When user references past work**: Search project memory files for related context
3282
-
3283
- **Memory-first response pattern:**
3284
- 1. User asks a question → Check project directory first: \`ls {project_deepagents_dir}\`
3285
- 2. If relevant files exist → Read them with \`read_file '{project_deepagents_dir}/[filename]'\`
3286
- 3. Check user memory if needed → \`ls {agent_dir_absolute}\`
3287
- 4. Base your answer on saved knowledge supplemented by general knowledge
3288
-
3289
- **When to update memories:**
3290
- - **IMMEDIATELY when the user describes your role or how you should behave**
3291
- - **IMMEDIATELY when the user gives feedback on your work** - Update memories to capture what was wrong and how to do it better
3292
- - When the user explicitly asks you to remember something
3293
- - When patterns or preferences emerge (coding styles, conventions, workflows)
3294
- - After significant work where context would help in future sessions
3295
-
3296
- **Learning from feedback:**
3297
- - When user says something is better/worse, capture WHY and encode it as a pattern
3298
- - Each correction is a chance to improve permanently - don't just fix the immediate issue, update your instructions
3299
- - When user says "you should remember X" or "be careful about Y", treat this as HIGH PRIORITY - update memories IMMEDIATELY
3300
- - Look for the underlying principle behind corrections, not just the specific mistake
3301
-
3302
- ## Deciding Where to Store Memory
3303
-
3304
- When writing or updating agent memory, decide whether each fact, configuration, or behavior belongs in:
3305
-
3306
- ### User Agent File: \`{agent_dir_absolute}/agent.md\`
3307
- → Describes the agent's **personality, style, and universal behavior** across all projects.
3308
-
3309
- **Store here:**
3310
- - Your general tone and communication style
3311
- - Universal coding preferences (formatting, comment style, etc.)
3312
- - General workflows and methodologies you follow
3313
- - Tool usage patterns that apply everywhere
3314
- - Personal preferences that don't change per-project
3315
-
3316
- **Examples:**
3317
- - "Be concise and direct in responses"
3318
- - "Always use type hints in Python"
3319
- - "Prefer functional programming patterns"
3320
-
3321
- ### Project Agent File: \`{project_deepagents_dir}/agent.md\`
3322
- → Describes **how this specific project works** and **how the agent should behave here only.**
3323
-
3324
- **Store here:**
3325
- - Project-specific architecture and design patterns
3326
- - Coding conventions specific to this codebase
3327
- - Project structure and organization
3328
- - Testing strategies for this project
3329
- - Deployment processes and workflows
3330
- - Team conventions and guidelines
3331
-
3332
- **Examples:**
3333
- - "This project uses FastAPI with SQLAlchemy"
3334
- - "Tests go in tests/ directory mirroring src/ structure"
3335
- - "All API changes require updating OpenAPI spec"
3336
-
3337
- ### Project Memory Files: \`{project_deepagents_dir}/*.md\`
3338
- → Use for **project-specific reference information** and structured notes.
3339
-
3340
- **Store here:**
3341
- - API design documentation
3342
- - Architecture decisions and rationale
3343
- - Deployment procedures
3344
- - Common debugging patterns
3345
- - Onboarding information
3346
-
3347
- **Examples:**
3348
- - \`{project_deepagents_dir}/api-design.md\` - REST API patterns used
3349
- - \`{project_deepagents_dir}/architecture.md\` - System architecture overview
3350
- - \`{project_deepagents_dir}/deployment.md\` - How to deploy this project
3351
-
3352
- ### File Operations:
3353
-
3354
- **User memory:**
3355
- \`\`\`
3356
- ls {agent_dir_absolute} # List user memory files
3357
- read_file '{agent_dir_absolute}/agent.md' # Read user preferences
3358
- edit_file '{agent_dir_absolute}/agent.md' ... # Update user preferences
3359
- \`\`\`
3360
-
3361
- **Project memory (preferred for project-specific information):**
3362
- \`\`\`
3363
- ls {project_deepagents_dir} # List project memory files
3364
- read_file '{project_deepagents_dir}/agent.md' # Read project instructions
3365
- edit_file '{project_deepagents_dir}/agent.md' ... # Update project instructions
3366
- write_file '{project_deepagents_dir}/agent.md' ... # Create project memory file
3367
- \`\`\`
3368
-
3369
- **Important**:
3370
- - Project memory files are stored in \`.deepagents/\` inside the project root
3371
- - Always use absolute paths for file operations
3372
- - Check project memories BEFORE user when answering project-specific questions`;
3373
- /**
3374
- * Create middleware for loading agent-specific long-term memory.
3375
- *
3376
- * This middleware loads the agent's long-term memory from a file (agent.md)
3377
- * and injects it into the system prompt. The memory is loaded once at the
3378
- * start of the conversation and stored in state.
3379
- *
3380
- * @param options - Configuration options
3381
- * @returns AgentMiddleware for memory loading and injection
3382
- */
3383
- function createAgentMemoryMiddleware(options) {
3384
- const { settings, assistantId, systemPromptTemplate } = options;
3385
- const agentDir = settings.getAgentDir(assistantId);
3386
- const agentDirDisplay = `~/.deepagents/${assistantId}`;
3387
- const agentDirAbsolute = agentDir;
3388
- const projectRoot = settings.projectRoot;
3389
- const projectMemoryInfo = projectRoot ? `\`${projectRoot}\` (detected)` : "None (not in a git project)";
3390
- const projectDeepagentsDir = projectRoot ? `${projectRoot}/.deepagents` : "[project-root]/.deepagents (not in a project)";
3391
- const template = systemPromptTemplate || DEFAULT_MEMORY_TEMPLATE;
3392
- return (0, langchain.createMiddleware)({
3393
- name: "AgentMemoryMiddleware",
3394
- stateSchema: AgentMemoryStateSchema,
3395
- beforeAgent(state) {
3396
- const result = {};
3397
- if (!("userMemory" in state)) {
3398
- const userPath = settings.getUserAgentMdPath(assistantId);
3399
- if (node_fs.default.existsSync(userPath)) try {
3400
- result.userMemory = node_fs.default.readFileSync(userPath, "utf-8");
3401
- } catch {}
3402
- }
3403
- if (!("projectMemory" in state)) {
3404
- const projectPath = settings.getProjectAgentMdPath();
3405
- if (projectPath && node_fs.default.existsSync(projectPath)) try {
3406
- result.projectMemory = node_fs.default.readFileSync(projectPath, "utf-8");
3407
- } catch {}
3408
- }
3409
- return Object.keys(result).length > 0 ? result : void 0;
3410
- },
3411
- wrapModelCall(request, handler) {
3412
- const userMemory = request.state?.userMemory;
3413
- const projectMemory = request.state?.projectMemory;
3414
- const baseSystemPrompt = request.systemPrompt || "";
3415
- const memorySection = template.replace("{user_memory}", userMemory || "(No user agent.md)").replace("{project_memory}", projectMemory || "(No project agent.md)");
3416
- const memoryDocs = LONGTERM_MEMORY_SYSTEM_PROMPT.replaceAll("{agent_dir_absolute}", agentDirAbsolute).replaceAll("{agent_dir_display}", agentDirDisplay).replaceAll("{project_memory_info}", projectMemoryInfo).replaceAll("{project_deepagents_dir}", projectDeepagentsDir);
3417
- let systemPrompt = memorySection;
3418
- if (baseSystemPrompt) systemPrompt += "\n\n" + baseSystemPrompt;
3419
- systemPrompt += "\n\n" + memoryDocs;
3420
- return handler({
3421
- ...request,
3422
- systemPrompt
3423
- });
3424
- }
3425
- });
3426
- }
3427
-
3428
3681
  //#endregion
3429
3682
  exports.BaseSandbox = BaseSandbox;
3430
3683
  exports.CompositeBackend = CompositeBackend;
@@ -3437,6 +3690,7 @@ exports.StoreBackend = StoreBackend;
3437
3690
  exports.createAgentMemoryMiddleware = createAgentMemoryMiddleware;
3438
3691
  exports.createDeepAgent = createDeepAgent;
3439
3692
  exports.createFilesystemMiddleware = createFilesystemMiddleware;
3693
+ exports.createMemoryMiddleware = createMemoryMiddleware;
3440
3694
  exports.createPatchToolCallsMiddleware = createPatchToolCallsMiddleware;
3441
3695
  exports.createSettings = createSettings;
3442
3696
  exports.createSkillsMiddleware = createSkillsMiddleware;