@posthog/agent 2.3.24 → 2.3.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/claude/session/jsonl-hydration.js +14 -1
- package/dist/adapters/claude/session/jsonl-hydration.js.map +1 -1
- package/dist/agent.js +97 -26
- package/dist/agent.js.map +1 -1
- package/dist/gateway-models.d.ts +3 -3
- package/dist/gateway-models.js +8 -11
- package/dist/gateway-models.js.map +1 -1
- package/dist/posthog-api.js +1 -1
- package/dist/posthog-api.js.map +1 -1
- package/dist/server/agent-server.js +86 -12
- package/dist/server/agent-server.js.map +1 -1
- package/dist/server/bin.cjs +86 -12
- package/dist/server/bin.cjs.map +1 -1
- package/dist/types.d.ts +1 -0
- package/package.json +3 -3
- package/src/adapters/claude/claude-agent.ts +67 -12
- package/src/adapters/claude/session/jsonl-hydration.ts +6 -1
- package/src/adapters/claude/session/models.ts +50 -0
- package/src/agent.ts +5 -5
- package/src/gateway-models.ts +15 -18
- package/src/types.ts +1 -0
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@posthog/agent",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.43",
|
|
4
4
|
"repository": "https://github.com/PostHog/code",
|
|
5
5
|
"description": "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
|
|
6
6
|
"exports": {
|
|
@@ -74,8 +74,8 @@
|
|
|
74
74
|
"tsx": "^4.20.6",
|
|
75
75
|
"typescript": "^5.5.0",
|
|
76
76
|
"vitest": "^2.1.8",
|
|
77
|
-
"@posthog/
|
|
78
|
-
"@posthog/
|
|
77
|
+
"@posthog/shared": "1.0.0",
|
|
78
|
+
"@posthog/git": "1.0.0"
|
|
79
79
|
},
|
|
80
80
|
"dependencies": {
|
|
81
81
|
"@agentclientprotocol/sdk": "0.15.0",
|
|
@@ -59,7 +59,12 @@ import { fetchMcpToolMetadata } from "./mcp/tool-metadata";
|
|
|
59
59
|
import { canUseTool } from "./permissions/permission-handlers";
|
|
60
60
|
import { getAvailableSlashCommands } from "./session/commands";
|
|
61
61
|
import { parseMcpServers } from "./session/mcp-config";
|
|
62
|
-
import {
|
|
62
|
+
import {
|
|
63
|
+
DEFAULT_MODEL,
|
|
64
|
+
getDefaultContextWindow,
|
|
65
|
+
getEffortOptions,
|
|
66
|
+
toSdkModelId,
|
|
67
|
+
} from "./session/models";
|
|
63
68
|
import {
|
|
64
69
|
buildSessionOptions,
|
|
65
70
|
buildSystemPrompt,
|
|
@@ -337,7 +342,9 @@ export class ClaudeAcpAgent extends BaseAcpAgent {
|
|
|
337
342
|
(m) => m.contextWindow,
|
|
338
343
|
);
|
|
339
344
|
const contextWindowSize =
|
|
340
|
-
contextWindows.length > 0
|
|
345
|
+
contextWindows.length > 0
|
|
346
|
+
? Math.min(...contextWindows)
|
|
347
|
+
: getDefaultContextWindow(this.session.modelId ?? "");
|
|
341
348
|
|
|
342
349
|
// Send usage_update notification
|
|
343
350
|
if (lastAssistantTotalUsage !== null) {
|
|
@@ -509,6 +516,7 @@ export class ClaudeAcpAgent extends BaseAcpAgent {
|
|
|
509
516
|
const sdkModelId = toSdkModelId(params.modelId);
|
|
510
517
|
await this.session.query.setModel(sdkModelId);
|
|
511
518
|
this.session.modelId = params.modelId;
|
|
519
|
+
this.rebuildEffortConfigOption(params.modelId);
|
|
512
520
|
await this.updateConfigOption("model", params.modelId);
|
|
513
521
|
return {};
|
|
514
522
|
}
|
|
@@ -559,6 +567,7 @@ export class ClaudeAcpAgent extends BaseAcpAgent {
|
|
|
559
567
|
const sdkModelId = toSdkModelId(params.value);
|
|
560
568
|
await this.session.query.setModel(sdkModelId);
|
|
561
569
|
this.session.modelId = params.value;
|
|
570
|
+
this.rebuildEffortConfigOption(params.value);
|
|
562
571
|
} else if (params.configId === "effort") {
|
|
563
572
|
const newEffort = params.value as EffortLevel;
|
|
564
573
|
this.session.effort = newEffort;
|
|
@@ -865,7 +874,7 @@ export class ClaudeAcpAgent extends BaseAcpAgent {
|
|
|
865
874
|
description: mode.description ?? undefined,
|
|
866
875
|
}));
|
|
867
876
|
|
|
868
|
-
|
|
877
|
+
const configOptions: SessionConfigOption[] = [
|
|
869
878
|
{
|
|
870
879
|
id: "mode",
|
|
871
880
|
name: "Approval Preset",
|
|
@@ -885,21 +894,67 @@ export class ClaudeAcpAgent extends BaseAcpAgent {
|
|
|
885
894
|
category: "model" as SessionConfigOptionCategory,
|
|
886
895
|
description: "Choose which model Claude should use",
|
|
887
896
|
},
|
|
888
|
-
|
|
897
|
+
];
|
|
898
|
+
|
|
899
|
+
const effortOptions = getEffortOptions(modelOptions.currentModelId);
|
|
900
|
+
if (effortOptions) {
|
|
901
|
+
configOptions.push({
|
|
889
902
|
id: "effort",
|
|
890
903
|
name: "Effort",
|
|
891
904
|
type: "select",
|
|
892
905
|
currentValue: currentEffort,
|
|
893
|
-
options:
|
|
894
|
-
{ value: "low", name: "Low" },
|
|
895
|
-
{ value: "medium", name: "Medium" },
|
|
896
|
-
{ value: "high", name: "High" },
|
|
897
|
-
{ value: "max", name: "Max" },
|
|
898
|
-
],
|
|
906
|
+
options: effortOptions,
|
|
899
907
|
category: "thought_level" as SessionConfigOptionCategory,
|
|
900
908
|
description: "Controls how much effort Claude puts into its response",
|
|
901
|
-
}
|
|
902
|
-
|
|
909
|
+
});
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
return configOptions;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
private rebuildEffortConfigOption(modelId: string): void {
|
|
916
|
+
const effortOptions = getEffortOptions(modelId);
|
|
917
|
+
const existingEffort = this.session.configOptions.find(
|
|
918
|
+
(o) => o.id === "effort",
|
|
919
|
+
);
|
|
920
|
+
|
|
921
|
+
if (!effortOptions) {
|
|
922
|
+
this.session.configOptions = this.session.configOptions.filter(
|
|
923
|
+
(o) => o.id !== "effort",
|
|
924
|
+
);
|
|
925
|
+
if (this.session.effort) {
|
|
926
|
+
this.session.effort = undefined;
|
|
927
|
+
this.session.queryOptions.effort = undefined;
|
|
928
|
+
}
|
|
929
|
+
return;
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
const currentValue = existingEffort?.currentValue ?? "high";
|
|
933
|
+
const isValidValue = effortOptions.some((o) => o.value === currentValue);
|
|
934
|
+
const resolvedValue = isValidValue ? currentValue : "high";
|
|
935
|
+
|
|
936
|
+
if (resolvedValue !== currentValue && this.session.effort) {
|
|
937
|
+
this.session.effort = resolvedValue as EffortLevel;
|
|
938
|
+
this.session.queryOptions.effort = resolvedValue as EffortLevel;
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
const effortConfig: SessionConfigOption = {
|
|
942
|
+
id: "effort",
|
|
943
|
+
name: "Effort",
|
|
944
|
+
type: "select",
|
|
945
|
+
currentValue: resolvedValue,
|
|
946
|
+
options: effortOptions,
|
|
947
|
+
category: "thought_level" as SessionConfigOptionCategory,
|
|
948
|
+
description: "Controls how much effort Claude puts into its response",
|
|
949
|
+
};
|
|
950
|
+
|
|
951
|
+
if (existingEffort) {
|
|
952
|
+
this.session.configOptions = this.session.configOptions.map((o) =>
|
|
953
|
+
o.id === "effort" ? effortConfig : o,
|
|
954
|
+
);
|
|
955
|
+
} else {
|
|
956
|
+
this.session.configOptions.push(effortConfig);
|
|
957
|
+
}
|
|
903
958
|
}
|
|
904
959
|
|
|
905
960
|
private async sendAvailableCommandsUpdate(): Promise<void> {
|
|
@@ -5,6 +5,7 @@ import * as path from "node:path";
|
|
|
5
5
|
import type { ContentBlock } from "@agentclientprotocol/sdk";
|
|
6
6
|
import type { PostHogAPIClient } from "../../../posthog-api";
|
|
7
7
|
import type { StoredEntry } from "../../../types";
|
|
8
|
+
import { supports1MContext } from "./models";
|
|
8
9
|
|
|
9
10
|
interface ConversationTurn {
|
|
10
11
|
role: "user" | "assistant";
|
|
@@ -188,6 +189,7 @@ export function rebuildConversation(
|
|
|
188
189
|
|
|
189
190
|
const CHARS_PER_TOKEN = 4;
|
|
190
191
|
const DEFAULT_MAX_TOKENS = 150_000;
|
|
192
|
+
const LARGE_CONTEXT_MAX_TOKENS = 800_000;
|
|
191
193
|
|
|
192
194
|
function estimateTurnTokens(turn: ConversationTurn): number {
|
|
193
195
|
let chars = 0;
|
|
@@ -546,7 +548,10 @@ export async function hydrateSessionJsonl(params: {
|
|
|
546
548
|
return;
|
|
547
549
|
}
|
|
548
550
|
|
|
549
|
-
const
|
|
551
|
+
const maxTokens = supports1MContext(params.model ?? "")
|
|
552
|
+
? LARGE_CONTEXT_MAX_TOKENS
|
|
553
|
+
: DEFAULT_MAX_TOKENS;
|
|
554
|
+
const conversation = selectRecentTurns(allTurns, maxTokens);
|
|
550
555
|
log.info("Selected recent turns for hydration", {
|
|
551
556
|
totalTurns: allTurns.length,
|
|
552
557
|
selectedTurns: conversation.length,
|
|
@@ -11,3 +11,53 @@ const GATEWAY_TO_SDK_MODEL: Record<string, string> = {
|
|
|
11
11
|
export function toSdkModelId(modelId: string): string {
|
|
12
12
|
return GATEWAY_TO_SDK_MODEL[modelId] ?? modelId;
|
|
13
13
|
}
|
|
14
|
+
|
|
15
|
+
const MODELS_WITH_1M_CONTEXT = new Set([
|
|
16
|
+
"claude-opus-4-6",
|
|
17
|
+
"claude-sonnet-4-6",
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
export function supports1MContext(modelId: string): boolean {
|
|
21
|
+
return MODELS_WITH_1M_CONTEXT.has(modelId);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function getDefaultContextWindow(modelId: string): number {
|
|
25
|
+
return supports1MContext(modelId) ? 1_000_000 : 200_000;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const MODELS_WITH_EFFORT = new Set([
|
|
29
|
+
"claude-opus-4-5",
|
|
30
|
+
"claude-opus-4-6",
|
|
31
|
+
"claude-sonnet-4-6",
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
const MODELS_WITH_MAX_EFFORT = new Set(["claude-opus-4-6"]);
|
|
35
|
+
|
|
36
|
+
export function supportsEffort(modelId: string): boolean {
|
|
37
|
+
return MODELS_WITH_EFFORT.has(modelId);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function supportsMaxEffort(modelId: string): boolean {
|
|
41
|
+
return MODELS_WITH_MAX_EFFORT.has(modelId);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface EffortOption {
|
|
45
|
+
value: string;
|
|
46
|
+
name: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function getEffortOptions(modelId: string): EffortOption[] | null {
|
|
50
|
+
if (!supportsEffort(modelId)) return null;
|
|
51
|
+
|
|
52
|
+
const options: EffortOption[] = [
|
|
53
|
+
{ value: "low", name: "Low" },
|
|
54
|
+
{ value: "medium", name: "Medium" },
|
|
55
|
+
{ value: "high", name: "High" },
|
|
56
|
+
];
|
|
57
|
+
|
|
58
|
+
if (supportsMaxEffort(modelId)) {
|
|
59
|
+
options.push({ value: "max", name: "Max" });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return options;
|
|
63
|
+
}
|
package/src/agent.ts
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
import {
|
|
6
6
|
BLOCKED_MODELS,
|
|
7
7
|
DEFAULT_GATEWAY_MODEL,
|
|
8
|
-
|
|
8
|
+
fetchModelsList,
|
|
9
9
|
} from "./gateway-models";
|
|
10
10
|
import { PostHogAPIClient, type TaskRunUpdate } from "./posthog-api";
|
|
11
11
|
import { SessionLogWriter } from "./session-log-writer";
|
|
@@ -45,7 +45,7 @@ export class Agent {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
private _configureLlmGateway(
|
|
48
|
+
private _configureLlmGateway(overrideUrl?: string): {
|
|
49
49
|
gatewayUrl: string;
|
|
50
50
|
apiKey: string;
|
|
51
51
|
} | null {
|
|
@@ -54,7 +54,7 @@ export class Agent {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
try {
|
|
57
|
-
const gatewayUrl = this.posthogAPI.getLlmGatewayUrl();
|
|
57
|
+
const gatewayUrl = overrideUrl ?? this.posthogAPI.getLlmGatewayUrl();
|
|
58
58
|
const apiKey = this.posthogAPI.getApiKey();
|
|
59
59
|
|
|
60
60
|
process.env.OPENAI_BASE_URL = `${gatewayUrl}/v1`;
|
|
@@ -74,7 +74,7 @@ export class Agent {
|
|
|
74
74
|
taskRunId: string,
|
|
75
75
|
options: TaskExecutionOptions = {},
|
|
76
76
|
): Promise<InProcessAcpConnection> {
|
|
77
|
-
const gatewayConfig = this._configureLlmGateway(options.
|
|
77
|
+
const gatewayConfig = this._configureLlmGateway(options.gatewayUrl);
|
|
78
78
|
this.logger.info("Configured LLM gateway", {
|
|
79
79
|
adapter: options.adapter,
|
|
80
80
|
});
|
|
@@ -86,7 +86,7 @@ export class Agent {
|
|
|
86
86
|
? options.model
|
|
87
87
|
: undefined;
|
|
88
88
|
if (options.adapter === "codex" && gatewayConfig) {
|
|
89
|
-
const models = await
|
|
89
|
+
const models = await fetchModelsList({
|
|
90
90
|
gatewayUrl: gatewayConfig.gatewayUrl,
|
|
91
91
|
});
|
|
92
92
|
const codexModelIds = models
|
package/src/gateway-models.ts
CHANGED
|
@@ -19,7 +19,7 @@ export const DEFAULT_GATEWAY_MODEL = "claude-opus-4-6";
|
|
|
19
19
|
|
|
20
20
|
export const BLOCKED_MODELS = new Set(["gpt-5-mini", "openai/gpt-5-mini"]);
|
|
21
21
|
|
|
22
|
-
type
|
|
22
|
+
type ModelsListResponse =
|
|
23
23
|
| {
|
|
24
24
|
data?: Array<{ id?: string; owned_by?: string }>;
|
|
25
25
|
models?: Array<{ id?: string; owned_by?: string }>;
|
|
@@ -79,53 +79,50 @@ export function isAnthropicModel(model: GatewayModel): boolean {
|
|
|
79
79
|
return model.id.startsWith("claude-") || model.id.startsWith("anthropic/");
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
export interface
|
|
82
|
+
export interface ModelInfo {
|
|
83
83
|
id: string;
|
|
84
84
|
owned_by?: string;
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
let
|
|
88
|
-
models:
|
|
87
|
+
let modelsListCache: {
|
|
88
|
+
models: ModelInfo[];
|
|
89
89
|
expiry: number;
|
|
90
90
|
url: string;
|
|
91
91
|
} | null = null;
|
|
92
92
|
|
|
93
|
-
export async function
|
|
93
|
+
export async function fetchModelsList(
|
|
94
94
|
options?: FetchGatewayModelsOptions,
|
|
95
|
-
): Promise<
|
|
95
|
+
): Promise<ModelInfo[]> {
|
|
96
96
|
const gatewayUrl = options?.gatewayUrl ?? process.env.ANTHROPIC_BASE_URL;
|
|
97
97
|
if (!gatewayUrl) {
|
|
98
98
|
return [];
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
if (
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
Date.now() <
|
|
102
|
+
modelsListCache &&
|
|
103
|
+
modelsListCache.url === gatewayUrl &&
|
|
104
|
+
Date.now() < modelsListCache.expiry
|
|
105
105
|
) {
|
|
106
|
-
return
|
|
106
|
+
return modelsListCache.models;
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
try {
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
base.search = "";
|
|
113
|
-
base.hash = "";
|
|
114
|
-
const response = await fetch(base.toString());
|
|
110
|
+
const modelsUrl = `${gatewayUrl}/v1/models`;
|
|
111
|
+
const response = await fetch(modelsUrl);
|
|
115
112
|
if (!response.ok) {
|
|
116
113
|
return [];
|
|
117
114
|
}
|
|
118
|
-
const data = (await response.json()) as
|
|
115
|
+
const data = (await response.json()) as ModelsListResponse;
|
|
119
116
|
const models = Array.isArray(data)
|
|
120
117
|
? data
|
|
121
118
|
: (data.data ?? data.models ?? []);
|
|
122
|
-
const results:
|
|
119
|
+
const results: ModelInfo[] = [];
|
|
123
120
|
for (const model of models) {
|
|
124
121
|
const id = model?.id ? String(model.id) : "";
|
|
125
122
|
if (!id) continue;
|
|
126
123
|
results.push({ id, owned_by: model?.owned_by });
|
|
127
124
|
}
|
|
128
|
-
|
|
125
|
+
modelsListCache = {
|
|
129
126
|
models: results,
|
|
130
127
|
expiry: Date.now() + CACHE_TTL,
|
|
131
128
|
url: gatewayUrl,
|
package/src/types.ts
CHANGED