Skip to content

Commit 4786b10

Browse files
committed
refactor(agents): share profile prompt identity helpers
1 parent 7c5f5d4 commit 4786b10

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { satisfies } from "semver";
1313
import { loadConfig } from "./config.js";
1414
import { runLocalAgentProvider } from "./local-agent-adapters.js";
1515
import {
16+
buildLocalAgentProfilePrompt,
1617
isLocalAgentProvider,
1718
loadLocalAgentProfiles,
1819
LOCAL_AGENT_PROVIDERS,
@@ -569,8 +570,7 @@ async function runLocalAgentProfile(
569570
record: LocalAgentRecord,
570571
prompt: string,
571572
): Promise<LocalAgentRunResult> {
572-
const body = profile.body.trim();
573-
const fullPrompt = body ? `${body}\n\nTask:\n${prompt}` : prompt;
573+
const fullPrompt = buildLocalAgentProfilePrompt(profile, prompt);
574574
return runLocalAgentProvider(profile.provider, {
575575
prompt: fullPrompt,
576576
workspace: record.workspaceRoot,

src/local-agent-profiles.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import { loadConfig } from "./config.js";
6-
import { loadLocalAgentProfiles, summarizeLocalAgentProfile } from "./local-agent-profiles.js";
6+
import {
7+
buildLocalAgentProfilePrompt,
8+
fingerprintLocalAgentProfile,
9+
loadLocalAgentProfiles,
10+
summarizeLocalAgentProfile,
11+
} from "./local-agent-profiles.js";
712
import type { ServerConfig } from "./config.js";
813

914
const root = await mkdtemp(join(tmpdir(), "devspace-agent-profiles-test-"));
@@ -73,6 +78,15 @@ try {
7378
assert.equal(profiles[0]?.model, "sonnet");
7479
assert.equal(profiles[0]?.effort, "high");
7580
assert.equal(profiles[0]?.body, "Project body.");
81+
assert.equal(
82+
buildLocalAgentProfilePrompt(profiles[0]!, "Review auth"),
83+
"Project body.\n\nTask:\nReview auth",
84+
);
85+
assert.equal(fingerprintLocalAgentProfile(profiles[0]!).length, 64);
86+
assert.notEqual(
87+
fingerprintLocalAgentProfile(profiles[0]!),
88+
fingerprintLocalAgentProfile({ ...profiles[0]!, body: "Changed body." }),
89+
);
7690
assert.deepEqual(summarizeLocalAgentProfile(profiles[0]!), {
7791
name: "reviewer",
7892
description: "Project reviewer #1.",

src/local-agent-profiles.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createHash } from "node:crypto";
12
import { existsSync } from "node:fs";
23
import { readdir, readFile } from "node:fs/promises";
34
import { basename, join, resolve } from "node:path";
@@ -77,6 +78,29 @@ export function summarizeLocalAgentProfile(
7778
};
7879
}
7980

81+
export function fingerprintLocalAgentProfile(profile: LocalAgentProfile): string {
82+
return createHash("sha256")
83+
.update(
84+
JSON.stringify({
85+
name: profile.name,
86+
description: profile.description,
87+
provider: profile.provider,
88+
model: profile.model ?? null,
89+
effort: profile.effort ?? null,
90+
body: profile.body,
91+
}),
92+
)
93+
.digest("hex");
94+
}
95+
96+
export function buildLocalAgentProfilePrompt(
97+
profile: Pick<LocalAgentProfile, "body">,
98+
task: string,
99+
): string {
100+
const body = profile.body.trim();
101+
return body ? `${body}\n\nTask:\n${task}` : task;
102+
}
103+
80104
async function loadProfilesFromDirectory(directory: string): Promise<LocalAgentProfile[]> {
81105
const resolvedDirectory = resolve(directory);
82106
if (!existsSync(resolvedDirectory)) return [];

0 commit comments

Comments
 (0)