Skip to content

Commit 98b7a73

Browse files
skoob13claude
andauthored
feat(phai): Add --claudeCodeConfig to agent server (#1232)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 76d9bd0 commit 98b7a73

4 files changed

Lines changed: 94 additions & 24 deletions

File tree

packages/agent/src/server/agent-server.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,14 @@ export class AgentServer {
639639
_meta: {
640640
sessionId: payload.run_id,
641641
taskRunId: payload.run_id,
642-
systemPrompt: { append: this.buildCloudSystemPrompt(prUrl) },
642+
systemPrompt: this.buildSessionSystemPrompt(prUrl),
643+
...(this.config.claudeCode?.plugins?.length && {
644+
claudeCode: {
645+
options: {
646+
plugins: this.config.claudeCode.plugins,
647+
},
648+
},
649+
}),
643650
},
644651
});
645652

@@ -944,6 +951,28 @@ export class AgentServer {
944951
: null;
945952
}
946953

954+
private buildSessionSystemPrompt(
955+
prUrl?: string | null,
956+
): string | { append: string } {
957+
const cloudAppend = this.buildCloudSystemPrompt(prUrl);
958+
const userPrompt = this.config.claudeCode?.systemPrompt;
959+
960+
// String override: combine user prompt with cloud instructions
961+
if (typeof userPrompt === "string") {
962+
return [userPrompt, cloudAppend].join("\n\n");
963+
}
964+
965+
// Preset with append: merge user append with cloud instructions
966+
if (typeof userPrompt === "object") {
967+
return {
968+
append: [userPrompt.append, cloudAppend].filter(Boolean).join("\n\n"),
969+
};
970+
}
971+
972+
// Default: just cloud instructions
973+
return { append: cloudAppend };
974+
}
975+
947976
private buildCloudSystemPrompt(prUrl?: string | null): string {
948977
if (prUrl) {
949978
return `

packages/agent/src/server/bin.ts

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Command } from "commander";
33
import { z } from "zod";
44
import { AgentServer } from "./agent-server";
5-
import { mcpServersSchema } from "./schemas";
5+
import { claudeCodeConfigSchema, mcpServersSchema } from "./schemas";
66

77
const envSchema = z.object({
88
JWT_PUBLIC_KEY: z
@@ -34,6 +34,30 @@ const envSchema = z.object({
3434

3535
const program = new Command();
3636

37+
function parseJsonOption<S extends z.ZodTypeAny>(
38+
raw: string | undefined,
39+
schema: S,
40+
flag: string,
41+
): z.output<S> | undefined {
42+
if (!raw) return undefined;
43+
44+
let parsed: unknown;
45+
try {
46+
parsed = JSON.parse(raw);
47+
} catch {
48+
program.error(`${flag} must be valid JSON`);
49+
}
50+
51+
const result = schema.safeParse(parsed);
52+
if (!result.success) {
53+
const errors = result.error.issues
54+
.map((issue) => ` - ${issue.path.join(".")}: ${issue.message}`)
55+
.join("\n");
56+
program.error(`${flag} validation failed:\n${errors}`);
57+
}
58+
return result.data;
59+
}
60+
3761
program
3862
.name("agent-server")
3963
.description("PostHog cloud agent server - runs in sandbox environments")
@@ -51,6 +75,10 @@ program
5175
"MCP servers config as JSON array (ACP McpServer[] format)",
5276
)
5377
.option("--baseBranch <branch>", "Base branch for PR creation")
78+
.option(
79+
"--claudeCodeConfig <json>",
80+
"Claude Code config as JSON (systemPrompt, systemPromptAppend, plugins)",
81+
)
5482
.action(async (options) => {
5583
const envResult = envSchema.safeParse(process.env);
5684

@@ -66,28 +94,16 @@ program
6694

6795
const mode = options.mode === "background" ? "background" : "interactive";
6896

69-
let mcpServers: z.infer<typeof mcpServersSchema> | undefined;
70-
if (options.mcpServers) {
71-
let parsed: unknown;
72-
try {
73-
parsed = JSON.parse(options.mcpServers);
74-
} catch {
75-
program.error("--mcpServers must be valid JSON");
76-
return;
77-
}
78-
79-
const result = mcpServersSchema.safeParse(parsed);
80-
if (!result.success) {
81-
const errors = result.error.issues
82-
.map((issue) => ` - ${issue.path.join(".")}: ${issue.message}`)
83-
.join("\n");
84-
program.error(
85-
`--mcpServers validation failed (only remote http/sse servers are supported):\n${errors}`,
86-
);
87-
return;
88-
}
89-
mcpServers = result.data;
90-
}
97+
const mcpServers = parseJsonOption(
98+
options.mcpServers,
99+
mcpServersSchema,
100+
"--mcpServers",
101+
);
102+
const claudeCode = parseJsonOption(
103+
options.claudeCodeConfig,
104+
claudeCodeConfigSchema,
105+
"--claudeCodeConfig",
106+
);
91107

92108
const server = new AgentServer({
93109
port: parseInt(options.port, 10),
@@ -101,6 +117,7 @@ program
101117
runId: options.runId,
102118
mcpServers,
103119
baseBranch: options.baseBranch,
120+
claudeCode,
104121
});
105122

106123
process.on("SIGINT", async () => {

packages/agent/src/server/schemas.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ export const mcpServersSchema = z.array(remoteMcpServerSchema);
1616

1717
export type RemoteMcpServer = z.infer<typeof remoteMcpServerSchema>;
1818

19+
export const claudeCodeConfigSchema = z.object({
20+
systemPrompt: z
21+
.union([
22+
z.string(),
23+
z.object({
24+
type: z.literal("preset"),
25+
preset: z.literal("claude_code"),
26+
append: z.string().optional(),
27+
}),
28+
])
29+
.optional(),
30+
plugins: z
31+
.array(z.object({ type: z.literal("local"), path: z.string() }))
32+
.optional(),
33+
});
34+
1935
export const jsonRpcRequestSchema = z.object({
2036
jsonrpc: z.literal("2.0"),
2137
method: z.string(),

packages/agent/src/server/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import type { AgentMode } from "../types";
22
import type { RemoteMcpServer } from "./schemas";
33

4+
export interface ClaudeCodeConfig {
5+
systemPrompt?:
6+
| string
7+
| { type: "preset"; preset: "claude_code"; append?: string };
8+
plugins?: { type: "local"; path: string }[];
9+
}
10+
411
export interface AgentServerConfig {
512
port: number;
613
repositoryPath?: string;
@@ -14,4 +21,5 @@ export interface AgentServerConfig {
1421
version?: string;
1522
mcpServers?: RemoteMcpServer[];
1623
baseBranch?: string;
24+
claudeCode?: ClaudeCodeConfig;
1725
}

0 commit comments

Comments
 (0)