Skip to content
This repository was archived by the owner on Aug 6, 2026. It is now read-only.

Commit baaaa7f

Browse files
feat: add durable cloud Pi runtime sessions (#3719)
Co-authored-by: posthog[bot] <206114724+posthog[bot]@users.noreply.github.com>
1 parent 7408a79 commit baaaa7f

103 files changed

Lines changed: 9502 additions & 1936 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apps/code/src/renderer/di/bindings.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import {
1313
type AutoresearchSessionClient,
1414
type AutoresearchStorageClient,
1515
} from "@posthog/core/autoresearch/identifiers";
16+
import {
17+
CLOUD_TASK_CLIENT,
18+
type CloudTaskClient,
19+
} from "@posthog/core/cloud-task/cloudTaskClient";
1620
import {
1721
CODE_REVIEW_WORKSPACE_CLIENT,
1822
REVERT_HUNK_SERVICE,
@@ -76,8 +80,10 @@ import {
7680
import { PI_RUNNER } from "@posthog/core/pi-runtime/identifiers";
7781
import type { PiRunner } from "@posthog/core/pi-runtime/piRunner";
7882
import {
79-
PI_SESSION_CLIENT,
80-
type PiSessionClient,
83+
LOCAL_PI_SESSION_FACTORY,
84+
PI_SESSION_PROVIDER,
85+
type PiSessionFactory,
86+
type PiSessionProvider,
8187
} from "@posthog/core/pi-runtime/piSessionController";
8288
import {
8389
type BundleLocalSkill,
@@ -295,7 +301,9 @@ export interface RendererBindings {
295301
[ANALYTICS_TRACKER]: AnalyticsTracker;
296302
[TASK_CREATION_HOST]: ITaskCreationHost;
297303
[PI_RUNNER]: PiRunner;
298-
[PI_SESSION_CLIENT]: PiSessionClient;
304+
[PI_SESSION_PROVIDER]: PiSessionProvider;
305+
[LOCAL_PI_SESSION_FACTORY]: PiSessionFactory;
306+
[CLOUD_TASK_CLIENT]: CloudTaskClient;
299307
[TASK_CREATION_EFFECTS]: TaskCreationEffects;
300308
[RENDERER_TASK_SERVICE]: TaskService;
301309
[TASK_SERVICE]: TaskService;

apps/code/src/renderer/di/container.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import "reflect-metadata";
22
import { useDevFlagsStore } from "@features/dev-toolbar/devFlagsStore";
33
import { TypedContainer } from "@inversifyjs/strongly-typed";
44
import type { TrpcRouter } from "@main/trpc/router";
5+
import {
6+
CLOUD_TASK_CLIENT,
7+
type CloudTaskClient,
8+
} from "@posthog/core/cloud-task/cloudTaskClient";
59
import {
610
CODE_REVIEW_WORKSPACE_CLIENT,
711
REVERT_HUNK_SERVICE,
@@ -35,7 +39,7 @@ import type { LocalMcpWorkspaceClient } from "@posthog/core/local-mcp/localMcpIm
3539
import { PI_RUNNER } from "@posthog/core/pi-runtime/identifiers";
3640
import { piRuntimeModule } from "@posthog/core/pi-runtime/pi-runtime.module";
3741
import type { PiRunner } from "@posthog/core/pi-runtime/piRunner";
38-
import { PI_SESSION_CLIENT } from "@posthog/core/pi-runtime/piSessionController";
42+
import { LOCAL_PI_SESSION_FACTORY } from "@posthog/core/pi-runtime/piSessionController";
3943
import {
4044
CLOUD_ARTIFACT_BUNDLE_LOCAL_SKILL,
4145
CLOUD_ARTIFACT_READ_FILE_AS_BASE64,
@@ -89,7 +93,9 @@ import {
8993
import { WorkspaceSetupService } from "@posthog/core/workspace/WorkspaceSetupService";
9094
import { setRootContainer } from "@posthog/di/container";
9195
import { HOST_TRPC_CLIENT } from "@posthog/host-router/client";
92-
import { TrpcPiSessionClient } from "@posthog/host-router/pi-session-client";
96+
import { TrpcCloudTaskClient } from "@posthog/host-router/cloud-task-client";
97+
import { TrpcPiRunner } from "@posthog/host-router/pi-runner";
98+
import { TrpcPiSessionFactory } from "@posthog/host-router/pi-session-factory";
9399
import {
94100
BROWSER_TABS_CLIENT,
95101
type BrowserTabsClient,
@@ -156,7 +162,6 @@ import { trpcClient } from "@renderer/trpc";
156162
import { hostTrpcClient } from "@renderer/trpc/client";
157163
import type { TRPCClient } from "@trpc/client";
158164
import { hostLog, logger } from "@utils/logger";
159-
import { TrpcPiRunner } from "../platform-adapters/trpc-pi-runner";
160165
import type { RendererBindings } from "./bindings";
161166
import { TASK_SERVICE as RENDERER_TASK_SERVICE, TRPC_CLIENT } from "./tokens";
162167

@@ -297,7 +302,8 @@ container
297302
// Bind services
298303
container.bind<ITaskCreationHost>(TASK_CREATION_HOST).to(TrpcTaskCreationHost);
299304
container.bind<PiRunner>(PI_RUNNER).to(TrpcPiRunner);
300-
container.bind(PI_SESSION_CLIENT).to(TrpcPiSessionClient);
305+
container.bind(LOCAL_PI_SESSION_FACTORY).to(TrpcPiSessionFactory);
306+
container.bind<CloudTaskClient>(CLOUD_TASK_CLIENT).to(TrpcCloudTaskClient);
301307
container.load(piRuntimeModule);
302308
container.bind(TASK_CREATION_EFFECTS).toConstantValue(taskCreationEffects);
303309
container.bind<TaskService>(RENDERER_TASK_SERVICE).to(TaskService);

apps/web/src/web-container.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ import { canvasCoreModule } from "@posthog/core/canvas/canvas.module";
2929
import { taskThreadCoreModule } from "@posthog/core/canvas/taskThread.module";
3030
import type { CloudTaskService } from "@posthog/core/cloud-task/cloud-task";
3131
import { cloudTaskModule } from "@posthog/core/cloud-task/cloud-task.module";
32+
import {
33+
CLOUD_TASK_CLIENT,
34+
type CloudTaskClient,
35+
} from "@posthog/core/cloud-task/cloudTaskClient";
3236
import {
3337
CLOUD_TASK_AUTH,
3438
CLOUD_TASK_SERVICE,
@@ -86,10 +90,14 @@ import {
8690
type GithubConnectClient as OnboardingGithubConnectContract,
8791
} from "@posthog/core/onboarding/identifiers";
8892
import { onboardingModule } from "@posthog/core/onboarding/onboarding.module";
93+
import { PI_RUNNER } from "@posthog/core/pi-runtime/identifiers";
8994
import { piRuntimeModule } from "@posthog/core/pi-runtime/pi-runtime.module";
95+
import type { PiRunner } from "@posthog/core/pi-runtime/piRunner";
9096
import {
91-
PI_SESSION_CLIENT,
92-
type PiSessionClient,
97+
LOCAL_PI_SESSION_FACTORY,
98+
PI_SESSION_PROVIDER,
99+
type PiSessionFactory,
100+
type PiSessionProvider,
93101
} from "@posthog/core/pi-runtime/piSessionController";
94102
import {
95103
type BundleLocalSkill,
@@ -161,7 +169,9 @@ import {
161169
HOST_TRPC_CLIENT,
162170
type HostTrpcClient,
163171
} from "@posthog/host-router/client";
164-
import { TrpcPiSessionClient } from "@posthog/host-router/pi-session-client";
172+
import { TrpcCloudTaskClient } from "@posthog/host-router/cloud-task-client";
173+
import { TrpcPiRunner } from "@posthog/host-router/pi-runner";
174+
import { TrpcPiSessionFactory } from "@posthog/host-router/pi-session-factory";
165175
import {
166176
ANALYTICS_SERVICE,
167177
type IAnalytics,
@@ -314,7 +324,10 @@ import { hostTrpcClient } from "./web-trpc";
314324

315325
interface WebBindings {
316326
[HOST_TRPC_CLIENT]: HostTrpcClient;
317-
[PI_SESSION_CLIENT]: PiSessionClient;
327+
[PI_SESSION_PROVIDER]: PiSessionProvider;
328+
[LOCAL_PI_SESSION_FACTORY]: PiSessionFactory;
329+
[CLOUD_TASK_CLIENT]: CloudTaskClient;
330+
[PI_RUNNER]: PiRunner;
318331
[ROOT_LOGGER]: RootLogger;
319332
[HOST_LOGGER]: HostLogger;
320333
[FEATURE_FLAGS]: FeatureFlags;
@@ -395,7 +408,9 @@ export const container = new TypedContainer<WebBindings>({
395408
// Keystone: the same typed host client the renderer binds — served in-process
396409
// here (web-trpc.ts) instead of over Electron IPC.
397410
container.bind(HOST_TRPC_CLIENT).toConstantValue(hostTrpcClient);
398-
container.bind(PI_SESSION_CLIENT).to(TrpcPiSessionClient);
411+
container.bind(LOCAL_PI_SESSION_FACTORY).to(TrpcPiSessionFactory);
412+
container.bind(CLOUD_TASK_CLIENT).to(TrpcCloudTaskClient);
413+
container.bind(PI_RUNNER).to(TrpcPiRunner);
399414
container.load(piRuntimeModule);
400415

401416
// Logger: web uses console; electron uses electron-log. Same RootLogger shape.

apps/web/src/web-host-router.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { fetchPosthogPiModelCatalog } from "@posthog/agent/pi/model-catalog";
2+
import { getLlmGatewayUrl } from "@posthog/agent/posthog-api";
3+
import type { AuthService } from "@posthog/core/auth/auth";
4+
import { AUTH_SERVICE } from "@posthog/core/auth/auth.module";
15
import { TEAM_SKILLS_SERVICE } from "@posthog/core/skills/identifiers";
26
import type { TeamSkillsService } from "@posthog/core/skills/teamSkillsService";
37
import { resolveService } from "@posthog/di/container";
@@ -134,6 +138,19 @@ const agentStubRouter = router({
134138
}),
135139
// Called by resetSessionService() on logout/project switch.
136140
resetAll: publicProcedure.mutation(() => undefined),
141+
getPiModelCatalog: publicProcedure
142+
.input(
143+
z.object({ apiHost: z.string(), region: z.enum(["us", "eu", "dev"]) }),
144+
)
145+
.query(async ({ input }) => {
146+
const auth = resolveService<AuthService>(AUTH_SERVICE);
147+
const { accessToken } = await auth.getValidAccessToken();
148+
return fetchPosthogPiModelCatalog(
149+
getLlmGatewayUrl(input.apiHost),
150+
input.region,
151+
accessToken,
152+
);
153+
}),
137154
// Model/mode/effort options for the task-input preview + cloud run creation
138155
// (a cloud run requires a model). Real: fetched from the CORS-open PostHog LLM
139156
// gateway, same logic the desktop main process runs (see web-agent-config.ts).

packages/agent/package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
"types": "./dist/pi/rpc-client.d.ts",
3333
"import": "./dist/pi/rpc-client.js"
3434
},
35+
"./pi/rpc-transport": {
36+
"types": "./dist/pi/rpc-transport.d.ts",
37+
"import": "./dist/pi/rpc-transport.js"
38+
},
39+
"./pi/remote-rpc-client": {
40+
"types": "./dist/pi/remote-rpc-client.d.ts",
41+
"import": "./dist/pi/remote-rpc-client.js"
42+
},
3543
"./pi/conversation": {
3644
"types": "./dist/pi/conversation/translatePiConversation.d.ts",
3745
"import": "./dist/pi/conversation/translatePiConversation.js"
@@ -44,6 +52,10 @@
4452
"types": "./dist/pi/types.d.ts",
4553
"import": "./dist/pi/types.js"
4654
},
55+
"./pi/model-catalog": {
56+
"types": "./dist/pi/model-catalog.d.ts",
57+
"import": "./dist/pi/model-catalog.js"
58+
},
4759
"./pr-url-detector": {
4860
"types": "./dist/pr-url-detector.d.ts",
4961
"import": "./dist/pr-url-detector.js"
@@ -114,7 +126,7 @@
114126
}
115127
},
116128
"bin": {
117-
"agent-server": "./dist/server/bin.cjs"
129+
"agent-server": "./dist/server/bin.js"
118130
},
119131
"type": "module",
120132
"keywords": [

0 commit comments

Comments
 (0)