Skip to content

Commit e1417db

Browse files
authored
feat(gateway): use gateway service locally if specified (#247)
1 parent 5a1a5e9 commit e1417db

File tree

6 files changed

+25
-25
lines changed

6 files changed

+25
-25
lines changed

.env.example

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ APPLE_CODESIGN_KEYCHAIN_PASSWORD="xxx"
1212

1313
VITE_POSTHOG_API_KEY=xxx
1414
VITE_POSTHOG_API_HOST=xxx
15-
VITE_POSTHOG_UI_HOST=xxx
15+
VITE_POSTHOG_UI_HOST=xxx
16+
17+
# Use new LLM gateway locally (experimental, needs to be started in mprocs)
18+
LLM_GATEWAY_URL=http://localhost:3308

apps/array/.env.example

Lines changed: 0 additions & 18 deletions
This file was deleted.

apps/array/src/main/services/session-manager.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,12 @@ export class SessionManager {
411411
process.env.POSTHOG_AUTH_HEADER = `Bearer ${credentials.apiKey}`;
412412
process.env.ANTHROPIC_API_KEY = credentials.apiKey;
413413
process.env.ANTHROPIC_AUTH_TOKEN = credentials.apiKey;
414-
process.env.ANTHROPIC_BASE_URL = `${credentials.apiHost}/api/projects/${credentials.projectId}/llm_gateway`;
414+
415+
const llmGatewayUrl =
416+
process.env.LLM_GATEWAY_URL ||
417+
`${credentials.apiHost}/api/projects/${credentials.projectId}/llm_gateway`;
418+
process.env.ANTHROPIC_BASE_URL = llmGatewayUrl;
419+
415420
// process.env.ELECTRON_RUN_AS_NODE = "1";
416421
process.env.CLAUDE_CODE_EXECUTABLE = getClaudeCliPath();
417422

packages/agent/example.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ async function testAgent() {
142142
POSTHOG_AUTH_HEADER: `Bearer ${process.env.POSTHOG_API_KEY}`,
143143
ANTHROPIC_API_KEY: process.env.POSTHOG_API_KEY,
144144
ANTHROPIC_AUTH_TOKEN: process.env.POSTHOG_API_KEY,
145-
ANTHROPIC_BASE_URL: `${process.env.POSTHOG_API_URL}/api/projects/${process.env.POSTHOG_PROJECT_ID}/llm_gateway`,
145+
ANTHROPIC_BASE_URL:
146+
process.env.LLM_GATEWAY_URL ||
147+
`${process.env.POSTHOG_API_URL}/api/projects/${process.env.POSTHOG_PROJECT_ID}/llm_gateway`,
146148
},
147149
},
148150
});

packages/agent/src/adapters/claude/claude.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -673,12 +673,19 @@ export class ClaudeAcpAgent implements Agent {
673673
throw RequestError.authRequired();
674674
}
675675

676-
// For assistant messages, text/thinking are normally streamed via stream_event.
677-
// But some gateways (like LiteLLM) don't stream, so we process all content.
676+
// When using the gateway service, text/thinking is streamed via stream_event,
677+
// so skip them here to avoid duplication.
678678
const content = message.message.content;
679+
const isUsingGatewayService = !!process.env.LLM_GATEWAY_URL;
680+
const contentToProcess =
681+
isUsingGatewayService && Array.isArray(content)
682+
? content.filter(
683+
(block) => block.type !== "text" && block.type !== "thinking",
684+
)
685+
: content;
679686

680687
for (const notification of toAcpNotifications(
681-
content,
688+
contentToProcess as typeof content,
682689
message.message.role,
683690
params.sessionId,
684691
this.toolUseCache,

packages/agent/src/agent.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ export class Agent {
134134
}
135135

136136
try {
137-
const gatewayUrl = this.posthogAPI.getLlmGatewayUrl();
137+
const gatewayUrl =
138+
process.env.LLM_GATEWAY_URL || this.posthogAPI.getLlmGatewayUrl();
138139
const apiKey = this.posthogAPI.getApiKey();
139140
process.env.ANTHROPIC_BASE_URL = gatewayUrl;
140141
process.env.ANTHROPIC_AUTH_TOKEN = apiKey;

0 commit comments

Comments
 (0)