GeezerVison is showing github.com — fix(server): skills named in a Claude question answer now run by snnbotchway · Pull Request #33523 · pingdotgg/t3code
fix(server): skills named in a Claude question answer now run
#33523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter
Filter by extension
.ts
(4)
All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -404,13 +404,29 @@ describe("ClaudeAdapterV2 runtime query policy", () => {
],
);
const sdkAnswers: NonNullable<AskUserQuestionInput["answers"]> =
ClaudeAdapterV2.claudeSdkUserInputAnswers({
"Which approach?": ["Simple", "Safe"],
"Deploy now?": "Yes",
});
ClaudeAdapterV2.claudeSdkUserInputAnswers(
{
"Which approach?": ["Simple", "Safe"],
"Deploy now?": "Yes",
"Anything else?": "$wait-what then $review it",
"Typed command?": "/wait-what please",
"A path?": "/srv/wait-what",
"A path and a skill?": "/srv/app then $review it",
},
new Map([
["wait-what", { path: "/skills/wait-what/SKILL.md", userInvocationOnly: true }],
["review", { path: "/skills/review/SKILL.md" }],
]),
);
assert.deepEqual(sdkAnswers, {
"Which approach?": "Simple, Safe",
"Deploy now?": "Yes",
"Anything else?":
"/wait-what [The user invoked this skill. Follow the instructions at: /skills/wait-what/SKILL.md] then /review it",
"Typed command?":
"/wait-what [The user invoked this skill. Follow the instructions at: /skills/wait-what/SKILL.md] please",
"A path?": "/srv/wait-what",
"A path and a skill?": "/srv/app then /review it",
});
assert.isTrue(
ClaudeAdapterV2.ClaudeProviderCapabilitiesV2.planning.supportsStructuredQuestions,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -160,6 +160,8 @@ import { resolveAttachmentPath } from "../../attachmentStore.ts";
import { snapshotMcpApp } from "../../mcpApps/McpAppSnapshot.ts";
import { resolveClaudeSdkExecutablePath } from "../../provider/Drivers/ClaudeExecutable.ts";
import {
answerMayMentionClaudeSkill,
rewriteClaudeSkillMentionsInAnswer,
planClaudeSkillDispatch,
type ClaudeSkillTarget,
} from "../../provider/Drivers/ClaudeSkillDispatch.ts";
Expand Down
Expand Up
@@ -4641,15 +4643,19 @@ export function claudeUserInputQuestions(
export function claudeSdkUserInputAnswers(
answers: ProviderUserInputAnswers,
skills: ReadonlyMap<string, ClaudeSkillTarget>,
): NonNullable<AskUserQuestionInput["answers"]> {
return Object.fromEntries(
Object.entries(answers).map(([question, answer]) => [
question,
Array.isArray(answer)
? answer.filter((value): value is string => typeof value === "string").join(", ")
: typeof answer === "string"
? answer
: String(answer ?? ""),
rewriteClaudeSkillMentionsInAnswer(
Array.isArray(answer)
? answer.filter((value): value is string => typeof value === "string").join(", ")
: typeof answer === "string"
? answer
: String(answer ?? ""),
skills,
),
]),
);
}
Expand Down
Expand Up
@@ -10431,7 +10437,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
behavior: "allow",
updatedInput: {
questions: toolInput.questions,
answers: claudeSdkUserInputAnswers(resolvedAnswers),
answers: claudeSdkUserInputAnswers(
resolvedAnswers,
Object.values(claudeSdkUserInputAnswers(resolvedAnswers, new Map())).some(
answerMayMentionClaudeSkill,
)
? yield* userInvocableSkills(context.input.runtimePolicy.cwd)
: new Map(),
),
},
toolUseID: callbackOptions.toolUseID,
} satisfies PermissionResult);
Expand Down
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,11 @@
import { describe, expect, it } from "vite-plus/test";
import { planClaudeSkillDispatch, type ClaudeSkillTarget } from "./ClaudeSkillDispatch.ts";
import {
answerMayMentionClaudeSkill,
planClaudeSkillDispatch,
rewriteClaudeSkillMentionsInAnswer,
type ClaudeSkillTarget,
} from "./ClaudeSkillDispatch.ts";
const skillPath = (name: string) => `/home/me/.claude/skills/${name}/SKILL.md`;
Expand Down
Expand Up
@@ -83,4 +88,15 @@ describe("planClaudeSkillDispatch", () => {
commandText: "/implement the fixes",
});
});
it("pre-check passes every answer the rewrite would change", () => {
const userOnly = new Map([["review", { path: skillPath("review"), userInvocationOnly: true }]]);
for (const answer of ["$review", "ok €review now", "/review", "\n /review it"]) {
expect(rewriteClaudeSkillMentionsInAnswer(answer, userOnly)).not.toBe(answer);
expect(answerMayMentionClaudeSkill(answer)).toBe(true);
}
for (const answer of ["Yes", "Simple, Safe", "see docs/review", ""]) {
expect(answerMayMentionClaudeSkill(answer)).toBe(false);
}
});
});
Original file line number
Diff line number
Diff line change
Expand Up
@@ -19,40 +19,74 @@ export interface ClaudeSkillDispatch {
readonly commandText: string;
}
export function planClaudeSkillDispatch(
prompt: string,
skills: ReadonlyMap<string, ClaudeSkillTarget>,
): ClaudeSkillDispatch | undefined {
const mentions = [...prompt.matchAll(SKILL_MENTION_PATTERN)].flatMap((match) => {
function matchSkillMentions(prompt: string, skills: ReadonlyMap<string, ClaudeSkillTarget>) {
return [...prompt.matchAll(SKILL_MENTION_PATTERN)].flatMap((match) => {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
const name = match[2] ?? "";
const skill = skills.get(name);
if (!skill) return [];
const start = (match.index ?? 0) + (match[1]?.length ?? 0);
return [{ name, skill, start, end: (match.index ?? 0) + match[0].length }];
});
const last = mentions.at(-1);
if (!last) {
return undefined;
}
}
// Only the final skill gets native expansion. Earlier user-only skills need
// a fallback to their raw file because Claude's Skill tool refuses them.
const annotated = new Set([last.name]);
let promptWithInlineSlashes = "";
// Claude's Skill tool refuses user-only skills, so any mention Claude will not
// expand natively points at the skill's raw file instead.
function inlineSkillMentions(
prompt: string,
mentions: ReturnType<typeof matchSkillMentions>,
nativelyExpanded: string | undefined,
): string {
const annotated = new Set(nativelyExpanded === undefined ? [] : [nativelyExpanded]);
let text = "";
let cursor = 0;
for (const mention of mentions) {
promptWithInlineSlashes += `${prompt.slice(cursor, mention.start)}/${mention.name}`;
text += `${prompt.slice(cursor, mention.start)}/${mention.name}`;
if (mention.skill.userInvocationOnly === true && !annotated.has(mention.name)) {
promptWithInlineSlashes += ` [The user invoked this skill. Follow the instructions at: ${mention.skill.path}]`;
text += ` [The user invoked this skill. Follow the instructions at: ${mention.skill.path}]`;
annotated.add(mention.name);
}
cursor = mention.end;
}
promptWithInlineSlashes = `${promptWithInlineSlashes}${prompt.slice(cursor)}`.trimEnd();
return `${text}${prompt.slice(cursor)}`.trimEnd();
}
export function planClaudeSkillDispatch(
prompt: string,
skills: ReadonlyMap<string, ClaudeSkillTarget>,
): ClaudeSkillDispatch | undefined {
const mentions = matchSkillMentions(prompt, skills);
const last = mentions.at(-1);
if (!last) {
return undefined;
}
return {
leadingText:
prompt.slice(0, last.start).trim().length > 0 ? promptWithInlineSlashes : undefined,
prompt.slice(0, last.start).trim().length > 0
? inlineSkillMentions(prompt, mentions, last.name)
: undefined,
commandText: `/${last.name}${prompt.slice(last.end)}`.trimEnd(),
};
}
/** Cheap pre-check so an answer with no skill syntax skips skill discovery. */
export function answerMayMentionClaudeSkill(answer: string): boolean {
return /\p{Sc}|^\s*\//u.test(answer);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
}
/** Tool results never expand skills, so an answer only names them for the agent to run. */
export function rewriteClaudeSkillMentionsInAnswer(
answer: string,
skills: ReadonlyMap<string, ClaudeSkillTarget>,
): string {
// A leading `/name` invokes a skill in the composer, so it counts here too.
const leadingSlash = /^(\s*)\/(?=\S)/.exec(answer);
if (leadingSlash) {
const asMention = `${leadingSlash[1]}$${answer.slice(leadingSlash[0].length)}`;
const mentions = matchSkillMentions(asMention, skills);
if (mentions[0]?.start === leadingSlash[1]?.length) {
return inlineSkillMentions(asMention, mentions, undefined);
}
}
const mentions = matchSkillMentions(answer, skills);
return mentions.length === 0 ? answer : inlineSkillMentions(answer, mentions, undefined);
}
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.