|
@@ -3,6 +3,8 @@ import { json } from '@sveltejs/kit';
|
|
|
import { type ChatCompletionRequest, type ChatMessage, SESSION_ID_HEADER } from '$lib/api/agents';
|
|
import { type ChatCompletionRequest, type ChatMessage, SESSION_ID_HEADER } from '$lib/api/agents';
|
|
|
import { NotFoundError, server } from '$lib/server';
|
|
import { NotFoundError, server } from '$lib/server';
|
|
|
|
|
|
|
|
|
|
+import { env } from '$env/dynamic/private';
|
|
|
|
|
+
|
|
|
import type { RequestHandler } from './$types';
|
|
import type { RequestHandler } from './$types';
|
|
|
|
|
|
|
|
const SSE_HEADERS = {
|
|
const SSE_HEADERS = {
|
|
@@ -14,6 +16,36 @@ const SSE_HEADERS = {
|
|
|
/** Runtime template name used by agents that run the OpenCode headless server. */
|
|
/** Runtime template name used by agents that run the OpenCode headless server. */
|
|
|
const OPENCODE_RUNTIME = 'opencode';
|
|
const OPENCODE_RUNTIME = 'opencode';
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * Base URL of a Kubernetes API server (or `kubectl proxy`) reachable from this
|
|
|
|
|
+ * server. Agents publish a cluster-internal data-plane endpoint such as
|
|
|
|
|
+ * `http://agent-foo.locostack.svc.cluster.local:8080`, which only resolves
|
|
|
|
|
+ * inside the cluster. When this is set, agent traffic is routed through the
|
|
|
|
|
+ * API server's service proxy instead, which resolves the Service from within
|
|
|
|
|
+ * the cluster - this is what makes local (out-of-cluster) development work.
|
|
|
|
|
+ */
|
|
|
|
|
+const AGENT_ENDPOINT_PROXY = env.AGENT_ENDPOINT_PROXY;
|
|
|
|
|
+
|
|
|
|
|
+/** Join a base URL with a path, tolerating slashes on either side. */
|
|
|
|
|
+const joinUrl = (base: string, path: string): string =>
|
|
|
|
|
+ `${base.replace(/\/+$/, '')}/${path.replace(/^\/+/, '')}`;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Rewrite an agent endpoint for out-of-cluster access through the Kubernetes
|
|
|
|
|
+ * API server service proxy: /api/v1/namespaces/{ns}/services/{name}:{port}/proxy
|
|
|
|
|
+ * Returns the endpoint untouched when no proxy is configured.
|
|
|
|
|
+ */
|
|
|
|
|
+const resolveEndpoint = (endpoint: string, namespace: string): string => {
|
|
|
|
|
+ if (!AGENT_ENDPOINT_PROXY || !namespace) return endpoint;
|
|
|
|
|
+
|
|
|
|
|
+ const url = new URL(endpoint);
|
|
|
|
|
+ const service = url.hostname.split('.')[0];
|
|
|
|
|
+ const port = url.port || (url.protocol === 'https:' ? '443' : '80');
|
|
|
|
|
+ const base = `${AGENT_ENDPOINT_PROXY.replace(/\/+$/, '')}/api/v1/namespaces/${namespace}/services/${service}:${port}/proxy`;
|
|
|
|
|
+ const path = url.pathname.replace(/\/+$/, '');
|
|
|
|
|
+ return path ? `${base}${path}` : base;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
/** Wrap a plain string as a single OpenAI-style SSE chat completion stream. */
|
|
/** Wrap a plain string as a single OpenAI-style SSE chat completion stream. */
|
|
|
const toChatCompletionStream = (text: string): string => {
|
|
const toChatCompletionStream = (text: string): string => {
|
|
|
const chunk = { choices: [{ delta: { content: text }, index: 0 }] };
|
|
const chunk = { choices: [{ delta: { content: text }, index: 0 }] };
|
|
@@ -30,7 +62,7 @@ const proxyOpenAIChat = async (
|
|
|
messages: ChatMessage[],
|
|
messages: ChatMessage[],
|
|
|
signal: AbortSignal,
|
|
signal: AbortSignal,
|
|
|
): Promise<Response> => {
|
|
): Promise<Response> => {
|
|
|
- const resp = await fetch(new URL('/v1/chat/completions', endpoint), {
|
|
|
|
|
|
|
+ const resp = await fetch(joinUrl(endpoint, '/v1/chat/completions'), {
|
|
|
method: 'POST',
|
|
method: 'POST',
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
body: JSON.stringify({ messages, stream: true }),
|
|
body: JSON.stringify({ messages, stream: true }),
|
|
@@ -51,7 +83,7 @@ type OpenCodeSession = { id: string };
|
|
|
type OpenCodePart = { type: string; text?: string };
|
|
type OpenCodePart = { type: string; text?: string };
|
|
|
type OpenCodeMessageReply = { parts?: OpenCodePart[] };
|
|
type OpenCodeMessageReply = { parts?: OpenCodePart[] };
|
|
|
|
|
|
|
|
-const openCodeUrl = (endpoint: string, path: string): URL => new URL(path, endpoint);
|
|
|
|
|
|
|
+const openCodeUrl = (endpoint: string, path: string): string => joinUrl(endpoint, path);
|
|
|
|
|
|
|
|
const createOpenCodeSession = async (endpoint: string, signal: AbortSignal): Promise<string> => {
|
|
const createOpenCodeSession = async (endpoint: string, signal: AbortSignal): Promise<string> => {
|
|
|
const resp = await fetch(openCodeUrl(endpoint, '/session'), {
|
|
const resp = await fetch(openCodeUrl(endpoint, '/session'), {
|
|
@@ -157,7 +189,10 @@ const POST: RequestHandler = async ({ locals, params, request }) => {
|
|
|
.getWorkspaceResources(workspaceId)
|
|
.getWorkspaceResources(workspaceId)
|
|
|
.agents.getAgentStatus(params.id);
|
|
.agents.getAgentStatus(params.id);
|
|
|
|
|
|
|
|
- const endpoint = status?.endpoint;
|
|
|
|
|
|
|
+ const workspace = server.ws.list().find((ws) => ws.id === workspaceId);
|
|
|
|
|
+ const namespace = workspace?.deployment.type === 'k8s' ? workspace.deployment.namespace : '';
|
|
|
|
|
+
|
|
|
|
|
+ const endpoint = status?.endpoint ? resolveEndpoint(status.endpoint, namespace) : undefined;
|
|
|
if (!endpoint) {
|
|
if (!endpoint) {
|
|
|
return json(
|
|
return json(
|
|
|
{ code: 'ENDPOINT_UNAVAILABLE', message: 'Agent endpoint is not yet available' },
|
|
{ code: 'ENDPOINT_UNAVAILABLE', message: 'Agent endpoint is not yet available' },
|