Skip to content

Commit 27b9b7f

Browse files
committed
refactor: adjust calling identifyMatchingSkillNames in createSession and replySession
1 parent d7d453f commit 27b9b7f

2 files changed

Lines changed: 27 additions & 20 deletions

File tree

src/session.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -901,20 +901,6 @@ The candidate skills are as follows:\n\n`;
901901
const signal = controller?.signal;
902902
this.throwIfAborted(signal);
903903

904-
if (userPrompt.text) {
905-
const skills = await this.listSkills();
906-
const skillNames = await this.identifyMatchingSkillNames(skills, userPrompt.text, { signal });
907-
this.throwIfAborted(signal);
908-
const skillSet = new Set(skillNames);
909-
const matchedSkill = skills.filter((skill) => skillSet.has(skill.name));
910-
if (Array.isArray(userPrompt.skills)) {
911-
userPrompt.skills.push(...matchedSkill);
912-
} else if (matchedSkill.length > 0) {
913-
userPrompt.skills = matchedSkill;
914-
}
915-
}
916-
userPrompt.skills = await this.normalizeSkills(userPrompt.skills);
917-
this.throwIfAborted(signal);
918904
const sessionId = crypto.randomUUID();
919905
this.ensureFileHistorySession(sessionId);
920906
const now = new Date().toISOString();
@@ -977,6 +963,21 @@ The candidate skills are as follows:\n\n`;
977963
const userMessage = this.buildUserMessage(sessionId, userPrompt);
978964
this.appendSessionMessage(sessionId, userMessage);
979965

966+
if (userPrompt.text) {
967+
const skills = await this.listSkills();
968+
const skillNames = await this.identifyMatchingSkillNames(skills, userPrompt.text, { signal });
969+
this.throwIfAborted(signal);
970+
const skillSet = new Set(skillNames);
971+
const matchedSkill = skills.filter((skill) => skillSet.has(skill.name));
972+
if (Array.isArray(userPrompt.skills)) {
973+
userPrompt.skills.push(...matchedSkill);
974+
} else if (matchedSkill.length > 0) {
975+
userPrompt.skills = matchedSkill;
976+
}
977+
}
978+
userPrompt.skills = await this.normalizeSkills(userPrompt.skills);
979+
this.throwIfAborted(signal);
980+
980981
if (userPrompt.skills && userPrompt.skills.length > 0) {
981982
for (const skill of userPrompt.skills) {
982983
if (skill.isLoaded) {
@@ -1022,6 +1023,10 @@ ${skillMd}
10221023

10231024
this.reportNewPrompt();
10241025

1026+
this.ensureFileHistorySession(sessionId);
1027+
const userMessage = this.buildUserMessage(sessionId, userPrompt);
1028+
this.appendSessionMessage(sessionId, userMessage);
1029+
10251030
if (userPrompt.text) {
10261031
const skills = await this.listSkills(sessionId);
10271032
const skillNames = await this.identifyMatchingSkillNames(skills, userPrompt.text, { signal, sessionId });
@@ -1037,10 +1042,6 @@ ${skillMd}
10371042
userPrompt.skills = await this.normalizeSkills(userPrompt.skills, sessionId);
10381043
this.throwIfAborted(signal);
10391044

1040-
this.ensureFileHistorySession(sessionId);
1041-
const userMessage = this.buildUserMessage(sessionId, userPrompt);
1042-
this.appendSessionMessage(sessionId, userMessage);
1043-
10441045
if (userPrompt.skills && userPrompt.skills.length > 0) {
10451046
for (const skill of userPrompt.skills) {
10461047
if (skill.isLoaded) {

src/tests/session.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,7 +1952,7 @@ test("SessionManager streams chat completions and counts reasoning progress", as
19521952
assert.equal(progressEvents[2]?.formattedTokens, "3");
19531953
});
19541954

1955-
test("SessionManager cancels skill matching before a session is created", async () => {
1955+
test("SessionManager persists session and user message before skill matching is cancelled", async () => {
19561956
const workspace = createTempDir("deepcode-skill-abort-workspace-");
19571957
const home = createTempDir("deepcode-skill-abort-home-");
19581958
setHomeDir(home);
@@ -1981,7 +1981,13 @@ test("SessionManager cancels skill matching before a session is created", async
19811981

19821982
await manager.handleUserPrompt({ text: "please use demo" });
19831983

1984-
assert.equal(manager.listSessions().length, 0);
1984+
// Session and user message are persisted before skill matching triggers an abort.
1985+
assert.equal(manager.listSessions().length, 1);
1986+
const [session] = manager.listSessions();
1987+
assert.equal(session?.status, "pending");
1988+
const messages = manager.listSessionMessages(session!.id);
1989+
const userMessage = messages.find((m) => m.role === "user");
1990+
assert.equal(userMessage?.content, "please use demo");
19851991
});
19861992

19871993
test("SessionManager treats OpenAI APIUserAbortError as interrupted", async () => {

0 commit comments

Comments
 (0)