Skip to content
Kward Search API index

Module: Kward::Prompts

Defined in:
lib/kward/prompts.rb,
lib/kward/prompts/templates.rb

Overview

Prompt template discovery from configured markdown files.

Defined Under Namespace

Classes: Templates

Class Method Summary collapse

Class Method Details

.base_promptObject



57
58
59
60
61
62
63
64
65
# File 'lib/kward/prompts.rb', line 57

def base_prompt
  <<~PROMPT.strip
    You are Kward, a concise practical CLI coding agent. Use tools to understand and modify software projects. Only call tools advertised for the current turn. Inspect files before changing them, make the smallest correct change, preserve existing style, and summarize what changed. Be honest about limitations.

    When web tools are available, use web_search to discover sources, fetch_content for important human-readable pages, and fetch_raw for machine-readable resources such as JSON, YAML, XML, RSS, OpenAPI specs, and plain text. Prefer official or primary sources and cite or mention the URLs you relied on. Use code_search for package, GitHub repository, and source-code research.

    Manage code context deliberately. Prefer context_for_task, summarize_file_structure, and read_file mode="outline"/"preview" before broad reads. Escalate to read_file mode="range" for exact lines, and use mode="full" only when focused context is insufficient. If output is compacted or replaced with a duplicate reference, use retrieve_tool_output to inspect the needed original detail. Use context_budget_stats when asked about context savings. When a material requirement is ambiguous, use ask_user_question when it is available; otherwise state the assumption.
  PROMPT
end

.config_agents_promptObject



67
68
69
# File 'lib/kward/prompts.rb', line 67

def config_agents_prompt
  ConfigFiles.agents_prompt
end

.config_agents_prompt_labelObject



71
72
73
74
75
76
# File 'lib/kward/prompts.rb', line 71

def config_agents_prompt_label
  return "Config principles" if File.exist?(ConfigFiles.config_principles_path)
  return "Config AGENTS.md alias" if File.exist?(ConfigFiles.config_agents_path)

  "Config principles"
end

.config_agents_prompt_sourceObject



78
79
80
81
82
83
84
# File 'lib/kward/prompts.rb', line 78

def config_agents_prompt_source
  return nil unless ConfigFiles.include_config_principles?
  return ConfigFiles.config_principles_path if File.exist?(ConfigFiles.config_principles_path)
  return ConfigFiles.config_agents_path if File.exist?(ConfigFiles.config_agents_path)

  nil
end

.persona_prompt(workspace_root = Dir.pwd, model: nil, reasoning_effort: nil, now: Time.now) ⇒ Object



86
87
88
# File 'lib/kward/prompts.rb', line 86

def persona_prompt(workspace_root = Dir.pwd, model: nil, reasoning_effort: nil, now: Time.now)
  ConfigFiles.persona_prompt(workspace_root, model: model, reasoning_effort: reasoning_effort, now: now)
end

.prompt_parts(workspace_root: Dir.pwd, include_workspace_personality: true, model: nil, reasoning_effort: nil, now: Time.now, memory_context: nil, plugin_context: nil, execution_profile_context: nil) ⇒ Object



32
33
34
# File 'lib/kward/prompts.rb', line 32

def prompt_parts(workspace_root: Dir.pwd, include_workspace_personality: true, model: nil, reasoning_effort: nil, now: Time.now, memory_context: nil, plugin_context: nil, execution_profile_context: nil)
  prompt_sections(workspace_root: workspace_root, include_workspace_personality: include_workspace_personality, model: model, reasoning_effort: reasoning_effort, now: now, memory_context: memory_context, plugin_context: plugin_context, execution_profile_context: execution_profile_context).map { |section| section[:content] }
end

.prompt_section(label, content, source: nil) ⇒ Object



127
128
129
130
131
# File 'lib/kward/prompts.rb', line 127

def prompt_section(label, content, source: nil)
  return nil if content.to_s.empty?

  { label: label, content: content, source: source }
end

.prompt_sections(workspace_root: Dir.pwd, include_workspace_personality: true, model: nil, reasoning_effort: nil, now: Time.now, memory_context: nil, plugin_context: nil, execution_profile_context: nil) ⇒ Array<Hash>

Returns labeled prompt sections for inspection by CLI and RPC frontends.

Returns:

  • (Array<Hash>)

    section hashes with label, content, and optional source



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kward/prompts.rb', line 40

def prompt_sections(workspace_root: Dir.pwd, include_workspace_personality: true, model: nil, reasoning_effort: nil, now: Time.now, memory_context: nil, plugin_context: nil, execution_profile_context: nil)
  return replacement_prompt_sections if ConfigFiles.replacement_system_prompt?

  sections = [prompt_section("Built-in system prompt", base_prompt)]
  sections << prompt_section("Execution profile", execution_profile_context) unless execution_profile_context.to_s.empty?
  sections << prompt_section(config_agents_prompt_label, config_agents_prompt, source: config_agents_prompt_source)
  sections << prompt_section("Memory context", memory_context) unless memory_context.to_s.empty?
  if include_workspace_personality
    sections << prompt_section("Persona", persona_prompt(workspace_root, model: model, reasoning_effort: reasoning_effort, now: now))
    sections << prompt_section("Plugin context", plugin_context) unless plugin_context.to_s.empty?
  end
  skills = ConfigFiles.skills(workspace_root: workspace_root)
  sections << prompt_section("Configured skills", skills_prompt(skills), source: skills.empty? ? nil : File.join(ConfigFiles.config_dir, "skills"))
  sections << prompt_section(workspace_agents_context_label(workspace_root), workspace_agents_context(workspace_root), source: ConfigFiles.workspace_agents_file?(workspace_root) ? ConfigFiles.workspace_agents_path(workspace_root) : nil)
  sections.compact
end

.replacement_prompt_sectionsObject



119
120
121
122
123
124
125
# File 'lib/kward/prompts.rb', line 119

def replacement_prompt_sections
  path = ConfigFiles.system_prompt_file_path
  content = ConfigFiles.system_prompt_file
  return [] if content.to_s.empty?

  [prompt_section("Custom system prompt (replacement)", content, source: path)]
end

.skills_prompt(skills = ConfigFiles.skills) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/kward/prompts.rb', line 133

def skills_prompt(skills = ConfigFiles.skills)
  return nil if skills.empty?

  lines = [
    "Agent Skills are available.",
    "When read_skill is available and a task matches a skill's description, use it to load the instructions before proceeding.",
    "When using read_skill, use a relative path to read referenced files inside a skill directory.",
    "Available skills:"
  ]
  skills.each do |skill|
    lines << "- #{skill.name}: #{skill.description}"
  end
  lines.join("\n")
end

.system_message(workspace_root: Dir.pwd, include_workspace_personality: true, model: nil, reasoning_effort: nil, now: Time.now, memory_context: nil, plugin_context: nil, execution_profile_context: nil) ⇒ Hash

Builds the system message sent to a model provider.

Parameters:

  • workspace_root (String) (defaults to: Dir.pwd)

    active workspace root

  • include_workspace_personality (Boolean) (defaults to: true)

    include persona and plugin context

  • model (String, nil) (defaults to: nil)

    model used for persona matching

  • reasoning_effort (String, nil) (defaults to: nil)

    reasoning effort used for persona matching

  • now (Time) (defaults to: Time.now)

    time used for persona modifiers

  • memory_context (String, nil) (defaults to: nil)

    retrieved memory text

  • plugin_context (String, nil) (defaults to: nil)

    trusted plugin-provided instructions

Returns:

  • (Hash)

    role/content system message



25
26
27
28
29
30
# File 'lib/kward/prompts.rb', line 25

def system_message(workspace_root: Dir.pwd, include_workspace_personality: true, model: nil, reasoning_effort: nil, now: Time.now, memory_context: nil, plugin_context: nil, execution_profile_context: nil)
  {
    role: "system",
    content: prompt_parts(workspace_root: workspace_root, include_workspace_personality: include_workspace_personality, model: model, reasoning_effort: reasoning_effort, now: now, memory_context: memory_context, plugin_context: plugin_context, execution_profile_context: execution_profile_context).compact.join("\n\n")
  }
end

.workspace_agents_context(workspace_root = Dir.pwd) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/kward/prompts.rb', line 90

def workspace_agents_context(workspace_root = Dir.pwd)
  if ConfigFiles.enforce_workspace_agents_file?
    ConfigFiles.workspace_agents_prompt(workspace_root)
  else
    workspace_agents_hint(workspace_root)
  end
end

.workspace_agents_context_label(workspace_root = Dir.pwd) ⇒ Object



112
113
114
115
116
117
# File 'lib/kward/prompts.rb', line 112

def workspace_agents_context_label(workspace_root = Dir.pwd)
  return "Workspace AGENTS.md" unless ConfigFiles.workspace_agents_file?(workspace_root)
  return "Workspace AGENTS.md" if ConfigFiles.enforce_workspace_agents_file?

  "Workspace AGENTS.md hint"
end

.workspace_agents_hint(workspace_root = Dir.pwd) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/kward/prompts.rb', line 102

def workspace_agents_hint(workspace_root = Dir.pwd)
  return nil unless ConfigFiles.workspace_agents_file?(workspace_root)

  path = ConfigFiles.workspace_agents_path(workspace_root)
  <<~PROMPT.strip
    Workspace guidance is available in AGENTS.md at the workspace root: #{path}
    For tasks involving this repository, read it before analyzing or modifying project files, and follow it when it does not conflict with higher-priority instructions or the user's request.
  PROMPT
end

.workspace_agents_prompt(workspace_root = Dir.pwd) ⇒ Object



98
99
100
# File 'lib/kward/prompts.rb', line 98

def workspace_agents_prompt(workspace_root = Dir.pwd)
  ConfigFiles.workspace_agents_prompt(workspace_root)
end