Skip to content

Commit 811e46c

Browse files
committed
feat(skill): add dynamic context support with !command syntax
Add support for !`command` syntax in skills to inject dynamic content at load time, compatible with Claude Code's implementation. Commands are executed in the skill's directory when the skill is loaded, and output replaces the placeholder. Uses the same simple shell execution pattern as prompt.ts for consistency with the codebase's trust model (skills are user-authored config files). Fixes anomalyco#10892
1 parent b551195 commit 811e46c

2 files changed

Lines changed: 346 additions & 1 deletion

File tree

‎packages/opencode/src/tool/skill.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { $ } from "bun"
12
import path from "path"
23
import { pathToFileURL } from "url"
34
import z from "zod"
45
import { Tool } from "./tool"
56
import { Skill } from "../skill"
7+
import { ConfigMarkdown } from "../config/markdown"
68
import { PermissionNext } from "../permission/next"
79
import { Ripgrep } from "../file/ripgrep"
810
import { iife } from "@/util/iife"
@@ -76,6 +78,24 @@ export const SkillTool = Tool.define("skill", async (ctx) => {
7678
const dir = path.dirname(skill.location)
7779
const base = pathToFileURL(dir).href
7880

81+
// Process dynamic context: execute shell commands in !`command` syntax
82+
let content = skill.content
83+
const shellCommands = ConfigMarkdown.shell(content)
84+
if (shellCommands.length > 0) {
85+
const results = await Promise.all(
86+
shellCommands.map(async ([, cmd]) => {
87+
try {
88+
return await $`${{ raw: cmd }}`.quiet().nothrow().text()
89+
} catch (error) {
90+
return `Error executing command: ${error instanceof Error ? error.message : String(error)}`
91+
}
92+
}),
93+
)
94+
let index = 0
95+
const shellRegex = /!`([^`]+)`/g
96+
content = content.replace(shellRegex, () => results[index++]!)
97+
}
98+
7999
const limit = 10
80100
const files = await iife(async () => {
81101
const arr = []
@@ -102,7 +122,7 @@ export const SkillTool = Tool.define("skill", async (ctx) => {
102122
`<skill_content name="${skill.name}">`,
103123
`# Skill: ${skill.name}`,
104124
"",
105-
skill.content.trim(),
125+
content.trim(),
106126
"",
107127
`Base directory for this skill: ${base}`,
108128
"Relative paths in this skill (e.g., scripts/, reference/) are relative to this base directory.",

‎packages/opencode/test/skill/skill.test.ts‎

Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,328 @@ description: A skill in the .opencode/skills directory.
386386
},
387387
})
388388
})
389+
390+
test("executes shell commands in skill content with !`command` syntax", async () => {
391+
const originalLine = 'The answer is: [!`echo "forty-two"`]'
392+
const renderedLine = "The answer is: [forty-two\n]"
393+
394+
await using tmp = await tmpdir({
395+
git: true,
396+
init: async (dir) => {
397+
const skillDir = path.join(dir, ".opencode", "skill", "dynamic-skill")
398+
const skillContent = [
399+
"---",
400+
"name: dynamic-skill",
401+
"description: A skill with dynamic content.",
402+
"---",
403+
"",
404+
"# Dynamic Skill",
405+
"",
406+
originalLine,
407+
"",
408+
"That's it.",
409+
].join("\n")
410+
await Bun.write(path.join(skillDir, "SKILL.md"), skillContent)
411+
},
412+
})
413+
414+
await Instance.provide({
415+
directory: tmp.path,
416+
fn: async () => {
417+
const { SkillTool } = await import("../../src/tool/skill")
418+
const skillTool = await SkillTool.init()
419+
const ctx = {
420+
sessionID: "test",
421+
messageID: "",
422+
callID: "",
423+
agent: "coder",
424+
abort: AbortSignal.any([]),
425+
messages: [],
426+
metadata: () => {},
427+
ask: async () => {},
428+
}
429+
430+
const result = await skillTool.execute({ name: "dynamic-skill" }, ctx)
431+
432+
expect(result.output).not.toContain(originalLine)
433+
expect(result.output).toContain(renderedLine)
434+
},
435+
})
436+
})
437+
438+
test("handles multiple shell commands in a single skill", async () => {
439+
await using tmp = await tmpdir({
440+
git: true,
441+
init: async (dir) => {
442+
const skillDir = path.join(dir, ".opencode", "skill", "multi-cmd-skill")
443+
const skillContent = [
444+
"---",
445+
"name: multi-cmd-skill",
446+
"description: A skill with multiple dynamic commands.",
447+
"---",
448+
"",
449+
"# Multi Command Skill",
450+
"",
451+
'First value: [!`echo "one"`]',
452+
"",
453+
'Second value: [!`echo "two"`]',
454+
"",
455+
'Third value: [!`echo "three"`]',
456+
].join("\n")
457+
await Bun.write(path.join(skillDir, "SKILL.md"), skillContent)
458+
},
459+
})
460+
461+
await Instance.provide({
462+
directory: tmp.path,
463+
fn: async () => {
464+
const { SkillTool } = await import("../../src/tool/skill")
465+
const skillTool = await SkillTool.init()
466+
const ctx = {
467+
sessionID: "test",
468+
messageID: "",
469+
callID: "",
470+
agent: "coder",
471+
abort: AbortSignal.any([]),
472+
messages: [],
473+
metadata: () => {},
474+
ask: async () => {},
475+
}
476+
477+
const result = await skillTool.execute({ name: "multi-cmd-skill" }, ctx)
478+
479+
expect(result.output).toContain("First value: [one\n]")
480+
expect(result.output).toContain("Second value: [two\n]")
481+
expect(result.output).toContain("Third value: [three\n]")
482+
expect(result.output).not.toContain("!`echo")
483+
},
484+
})
485+
})
486+
487+
test("executes shell commands in the project directory", async () => {
488+
await using tmp = await tmpdir({
489+
git: true,
490+
init: async (dir) => {
491+
const skillDir = path.join(dir, ".opencode", "skill", "cwd-skill")
492+
await Bun.write(
493+
path.join(skillDir, "SKILL.md"),
494+
[
495+
"---",
496+
"name: cwd-skill",
497+
"description: A skill that checks its working directory.",
498+
"---",
499+
"",
500+
"Current dir: [!`pwd`]",
501+
].join("\n"),
502+
)
503+
},
504+
})
505+
506+
await Instance.provide({
507+
directory: tmp.path,
508+
fn: async () => {
509+
const { SkillTool } = await import("../../src/tool/skill")
510+
const skillTool = await SkillTool.init()
511+
const ctx = {
512+
sessionID: "test",
513+
messageID: "",
514+
callID: "",
515+
agent: "coder",
516+
abort: AbortSignal.any([]),
517+
messages: [],
518+
metadata: () => {},
519+
ask: async () => {},
520+
}
521+
522+
const result = await skillTool.execute({ name: "cwd-skill" }, ctx)
523+
524+
// Commands run in project directory, matching prompt.ts behavior
525+
expect(result.output).toContain(tmp.path)
526+
},
527+
})
528+
})
529+
530+
test("handles failing shell commands gracefully", async () => {
531+
await using tmp = await tmpdir({
532+
git: true,
533+
init: async (dir) => {
534+
const skillDir = path.join(dir, ".opencode", "skill", "fail-skill")
535+
await Bun.write(
536+
path.join(skillDir, "SKILL.md"),
537+
[
538+
"---",
539+
"name: fail-skill",
540+
"description: A skill with a failing command.",
541+
"---",
542+
"",
543+
"Result: [!`nonexistent_command_12345`]",
544+
].join("\n"),
545+
)
546+
},
547+
})
548+
549+
await Instance.provide({
550+
directory: tmp.path,
551+
fn: async () => {
552+
const { SkillTool } = await import("../../src/tool/skill")
553+
const skillTool = await SkillTool.init()
554+
const ctx = {
555+
sessionID: "test",
556+
messageID: "",
557+
callID: "",
558+
agent: "coder",
559+
abort: AbortSignal.any([]),
560+
messages: [],
561+
metadata: () => {},
562+
ask: async () => {},
563+
}
564+
565+
// Should not throw - failing commands are handled gracefully
566+
const result = await skillTool.execute({ name: "fail-skill" }, ctx)
567+
568+
// The output should still be returned (with empty or error output from the command)
569+
expect(result.output).toContain("Result:")
570+
// The !`command` syntax should be replaced (not left as-is)
571+
expect(result.output).not.toContain("!`nonexistent_command_12345`")
572+
},
573+
})
574+
})
575+
576+
test("handles commands that output nothing", async () => {
577+
await using tmp = await tmpdir({
578+
git: true,
579+
init: async (dir) => {
580+
const skillDir = path.join(dir, ".opencode", "skill", "empty-output-skill")
581+
await Bun.write(
582+
path.join(skillDir, "SKILL.md"),
583+
[
584+
"---",
585+
"name: empty-output-skill",
586+
"description: A skill with a command that outputs nothing.",
587+
"---",
588+
"",
589+
"Before [!`true`] After",
590+
].join("\n"),
591+
)
592+
},
593+
})
594+
595+
await Instance.provide({
596+
directory: tmp.path,
597+
fn: async () => {
598+
const { SkillTool } = await import("../../src/tool/skill")
599+
const skillTool = await SkillTool.init()
600+
const ctx = {
601+
sessionID: "test",
602+
messageID: "",
603+
callID: "",
604+
agent: "coder",
605+
abort: AbortSignal.any([]),
606+
messages: [],
607+
metadata: () => {},
608+
ask: async () => {},
609+
}
610+
611+
const result = await skillTool.execute({ name: "empty-output-skill" }, ctx)
612+
613+
// Command with no output should be replaced with empty string
614+
expect(result.output).toContain("Before [] After")
615+
expect(result.output).not.toContain("!`true`")
616+
},
617+
})
618+
})
619+
620+
test("handles multiple skill executions correctly", async () => {
621+
// This test verifies that skills can be loaded multiple times in sequence.
622+
// Uses a local regex (matching prompt.ts pattern) instead of shared ConfigMarkdown.SHELL_REGEX
623+
// for defensive coding and consistency.
624+
await using tmp = await tmpdir({
625+
git: true,
626+
init: async (dir) => {
627+
const skillDir = path.join(dir, ".opencode", "skill", "regex-test-skill")
628+
await Bun.write(
629+
path.join(skillDir, "SKILL.md"),
630+
[
631+
"---",
632+
"name: regex-test-skill",
633+
"description: A skill to test regex state handling.",
634+
"---",
635+
"",
636+
'Value: [!`echo "test-value"`]',
637+
].join("\n"),
638+
)
639+
},
640+
})
641+
642+
await Instance.provide({
643+
directory: tmp.path,
644+
fn: async () => {
645+
const { SkillTool } = await import("../../src/tool/skill")
646+
const skillTool = await SkillTool.init()
647+
const ctx = {
648+
sessionID: "test",
649+
messageID: "",
650+
callID: "",
651+
agent: "coder",
652+
abort: AbortSignal.any([]),
653+
messages: [],
654+
metadata: () => {},
655+
ask: async () => {},
656+
}
657+
658+
// Execute the skill multiple times in sequence
659+
// If using a shared global regex, lastIndex state could cause failures
660+
for (let i = 0; i < 3; i++) {
661+
const result = await skillTool.execute({ name: "regex-test-skill" }, ctx)
662+
expect(result.output).toContain("Value: [test-value\n]")
663+
expect(result.output).not.toContain("!`echo")
664+
}
665+
},
666+
})
667+
})
668+
669+
test("handles commands with special characters", async () => {
670+
await using tmp = await tmpdir({
671+
git: true,
672+
init: async (dir) => {
673+
const skillDir = path.join(dir, ".opencode", "skill", "special-chars-skill")
674+
await Bun.write(
675+
path.join(skillDir, "SKILL.md"),
676+
[
677+
"---",
678+
"name: special-chars-skill",
679+
"description: A skill with special characters in commands.",
680+
"---",
681+
"",
682+
"Quoted string: [!`echo \"hello world\"`]",
683+
"With pipe: [!`echo test | cat`]",
684+
].join("\n"),
685+
)
686+
},
687+
})
688+
689+
await Instance.provide({
690+
directory: tmp.path,
691+
fn: async () => {
692+
const { SkillTool } = await import("../../src/tool/skill")
693+
const skillTool = await SkillTool.init()
694+
const ctx = {
695+
sessionID: "test",
696+
messageID: "",
697+
callID: "",
698+
agent: "coder",
699+
abort: AbortSignal.any([]),
700+
messages: [],
701+
metadata: () => {},
702+
ask: async () => {},
703+
}
704+
705+
const result = await skillTool.execute({ name: "special-chars-skill" }, ctx)
706+
707+
// Commands with special characters should work
708+
expect(result.output).toContain("Quoted string: [hello world\n]")
709+
expect(result.output).toContain("With pipe: [test\n]")
710+
expect(result.output).not.toContain("!`echo")
711+
},
712+
})
713+
})

0 commit comments

Comments
 (0)