Open History to see the last commit
1/**
2 * DevinSkillDispatch — turns `$skill` mentions in a composer prompt into the
3 * `@skills:name` invocation Devin actually runs.
4 *
5 * The composer inserts `$name` for every provider. Devin does not parse that
6 * token; Devin's documented user-side invocation is `@skills:<name>` (see
7 * docs.devin.ai/product-guides/skills). Unlike Claude Code — which requires a
8 * slash command as the first character of the last text block — Devin accepts
9 * the mention inline, so dispatch is a plain token substitution and the text
10 * around it survives untouched.
11 *
12 * The same token shape the composer and timeline chips recognise
13 * (`packages/shared/src/composerInlineTokens.ts`) is reused here, so a
14 * rendered chip and a dispatched skill are always the same set. Unknown dollar
15 * tokens stay literal: a `$HOME` in prose must not become a command, and a
16 * token glued to other text (`5$implement`) is not a mention.
17 *
18 * @module provider/Drivers/DevinSkillDispatch
19 */
20
21const SKILL_MENTION_PATTERN =
22 /(^|\s)\$(?![0-9][0-9_]*(?:[kKmMbBtT]|[eE][0-9]+)?(?:\s|$))(?=[a-zA-Z0-9:_-]*[a-zA-Z])([a-zA-Z0-9][a-zA-Z0-9:_-]*)(?=\s|$)/g;
23
24export interface DevinSkillDispatch {
25 /** The prompt with every known `$name` mention rewritten to `@skills:name`. */
26 readonly prompt: string;
27 /** The last skill name dispatched, for diagnostics. */
28 readonly skillName: string;
29}
30
31/**
32 * Rewrite every known `$skill` mention in `prompt` to `@skills:<name>`,
33 * preserving surrounding text and trailing arguments. Returns `undefined`
34 * when no known skill is mentioned, in which case the prompt goes out
35 * unchanged. Mentions that do not match a discovered skill stay literal: a
36 * `$HOME` in prose must not become a command, and a path-like token is never
37 * inferred to be a skill.
38 */
39export function planDevinSkillDispatch(
40 prompt: string,
41 skillNames: ReadonlySet<string>,
42): DevinSkillDispatch | undefined {
43 let dispatched = "";
44 let lastIndex = 0;
45 let dispatchedAny = false;
46 let lastSkillName = "";
47
48 for (const match of prompt.matchAll(SKILL_MENTION_PATTERN)) {
49 const name = match[2] ?? "";
50 if (!skillNames.has(name)) {
51 continue;
52 }
53 const leading = match[1] ?? "";
54 const tokenStart = (match.index ?? 0) + leading.length;
55
56 dispatched += prompt.slice(lastIndex, tokenStart) + `@skills:${name}`;
57 lastIndex = tokenStart + name.length + 1;
58 dispatchedAny = true;
59 lastSkillName = name;
60 }
61
62 if (!dispatchedAny) {
63 return undefined;
64 }
65 return { prompt: dispatched + prompt.slice(lastIndex), skillName: lastSkillName };
66}
67
68/**
69 * Whether `prompt` carries any `$skill`-shaped token at all. The adapter uses
70 * this to skip discovery entirely on ordinary turns — the CLI probe only runs
71 * when a candidate token exists, so prompts without a mention never pay for it.
72 */
73export function hasCandidateSkillMention(prompt: string): boolean {
74 for (const _ of prompt.matchAll(SKILL_MENTION_PATTERN)) {
75 return true;
76 }
77 return false;
78}