Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,23 @@ public void deleteMessage(int index) {
public void clear() {
messages.clear();
}

/**
* Creates a fork (copy) of this memory.
*
* <p>The fork contains a copy of all messages at the time of invocation. Changes to the fork
* do not affect the original memory, and vice versa.
*
* @return A new InMemoryMemory instance containing copies of all messages
*/
@Override
public Memory fork() {
InMemoryMemory forked = new InMemoryMemory();
for (Msg msg : messages) {
if (msg != null) {
forked.messages.add(msg);
}
}
return forked;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,17 @@ public interface Memory extends StateModule {
* is typically irreversible unless state has been persisted.
*/
void clear();

/**
* Creates a fork (copy) of this memory.
*
* <p>The fork contains a copy of all messages at the time of invocation. Changes to the fork
* do not affect the original memory, and vice versa.
*
* <p>This is useful when you want to provide context to a sub-agent without allowing it to
* modify the parent's memory.
*
* @return A new Memory instance containing copies of all messages
*/
Memory fork();
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public class AgentSkill {
private final String skillContent;
private final Map<String, String> resources;
private final String source;
private final String model;
private final String context;

/**
* Creates an AgentSkill with explicit parameters.
Expand Down Expand Up @@ -104,6 +106,59 @@ public AgentSkill(
String skillContent,
Map<String, String> resources,
String source) {
this(name, description, skillContent, resources, source, null);
}

/**
* Creates an AgentSkill with explicit parameters, custom source, and model.
*
* <p>Use this constructor when you want to create a skill directly without parsing
* markdown. The source parameter indicates where the skill originated from.
* The model parameter specifies which AI model should be used when this skill is active.
*
* @param name Skill name (must not be null or empty)
* @param description Skill description (must not be null or empty)
* @param skillContent The skill implementation or instructions (must not be null or empty)
* @param resources Supporting resources referenced by the skill (can be null)
* @param source Source identifier for the skill (null defaults to "custom")
* @param model Model reference for the skill (can be null)
* @throws IllegalArgumentException if name, description, or skillContent is null or empty
*/
public AgentSkill(
String name,
String description,
String skillContent,
Map<String, String> resources,
String source,
String model) {
this(name, description, skillContent, resources, source, model, null);
}

/**
* Creates an AgentSkill with explicit parameters, custom source, model, and context.
*
* <p>Use this constructor when you want to create a skill directly without parsing markdown.
* The source parameter indicates where the skill originated from. The model parameter
* specifies which AI model should be used when this skill is active. The context parameter
* specifies how memory should be shared with the sub-agent.
*
* @param name Skill name (must not be null or empty)
* @param description Skill description (must not be null or empty)
* @param skillContent The skill implementation or instructions (must not be null or empty)
* @param resources Supporting resources referenced by the skill (can be null)
* @param source Source identifier for the skill (null defaults to "custom")
* @param model Model reference for the skill (can be null)
* @param context Context sharing mode: "shared" (default), "fork", or "new" (can be null)
* @throws IllegalArgumentException if name, description, or skillContent is null or empty
*/
public AgentSkill(
String name,
String description,
String skillContent,
Map<String, String> resources,
String source,
String model,
String context) {
if (name == null || name.isEmpty() || description == null || description.isEmpty()) {
throw new IllegalArgumentException(
"The skill must have `name` and `description` fields.");
Expand All @@ -117,6 +172,8 @@ public AgentSkill(
this.skillContent = skillContent;
this.resources = resources != null ? new HashMap<>(resources) : new HashMap<>();
this.source = source != null ? source : "custom";
this.model = model;
this.context = context;
}

/**
Expand Down Expand Up @@ -157,6 +214,36 @@ public String getSource() {
return source;
}

/**
* Gets the model reference for this skill.
*
* <p>The model specifies which AI model should be used when this skill is active. For example:
* "haiku", "sonnet", "openai:gpt-4o".
*
* @return The model reference, or null if no specific model is required
*/
public String getModel() {
return model;
}

/**
* Gets the context sharing mode for this skill.
*
* <p>The context specifies how memory should be shared between the parent agent and the
* sub-agent:
*
* <ul>
* <li>null or "shared" (default): Sub-agent shares the same memory with parent
* <li>"fork": Sub-agent gets a copy of parent's memory
* <li>"new": Sub-agent has completely independent memory
* </ul>
*
* @return The context mode string, or null if default (shared)
*/
public String getContext() {
return context;
}

/**
* Gets the skill resources.
*
Expand Down Expand Up @@ -261,6 +348,8 @@ public static class Builder {
private String skillContent;
private Map<String, String> resources;
private String source;
private String model;
private String context;

/**
* Creates an empty builder.
Expand All @@ -280,6 +369,8 @@ private Builder(AgentSkill baseSkill) {
this.skillContent = baseSkill.skillContent;
this.resources = new HashMap<>(baseSkill.resources);
this.source = baseSkill.source;
this.model = baseSkill.model;
this.context = baseSkill.context;
}

/**
Expand Down Expand Up @@ -370,14 +461,45 @@ public Builder source(String source) {
return this;
}

/**
* Sets the model reference.
*
* @param model The model reference (e.g., "haiku", "openai:gpt-4o")
* @return This builder
*/
public Builder model(String model) {
this.model = model;
return this;
}

/**
* Sets the context sharing mode.
*
* <p>The context specifies how memory should be shared:
*
* <ul>
* <li>null or "shared" (default): Sub-agent shares the same memory with parent
* <li>"fork": Sub-agent gets a copy of parent's memory
* <li>"new": Sub-agent has completely independent memory
* </ul>
*
* @param context The context mode string
* @return This builder
*/
public Builder context(String context) {
this.context = context;
return this;
}

/**
* Builds the AgentSkill instance.
*
* @return A new AgentSkill instance
* @throws IllegalArgumentException if required fields are missing
*/
public AgentSkill build() {
return new AgentSkill(name, description, skillContent, resources, source);
return new AgentSkill(
name, description, skillContent, resources, source, model, context);
}
}
}
Loading
Loading