瀏覽代碼

feat(agents): 支持创建与调试 lambdagent 运行时

Agent 表单新增 lambdagent 选项并设为默认值,同时补齐本地开发所需的
workspace 通配与集群外 Agent 访问能力,便于 PaaS 侧联调。

- 表单默认 runtime 改为 lambdagent,且不再写入 runtimeVersion
- workspace 列表支持 * 通配授权
- Agent 测试接口支持 AGENT_ENDPOINT_PROXY,经 K8s service proxy 转发
KongLingTT 1 月之前
父節點
當前提交
a9ad8d7ec2

+ 0 - 63
package-lock.json

@@ -1622,9 +1622,6 @@
 				"arm"
 			],
 			"dev": true,
-			"libc": [
-				"glibc"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -1639,9 +1636,6 @@
 				"arm"
 			],
 			"dev": true,
-			"libc": [
-				"musl"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -1656,9 +1650,6 @@
 				"arm64"
 			],
 			"dev": true,
-			"libc": [
-				"glibc"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -1673,9 +1664,6 @@
 				"arm64"
 			],
 			"dev": true,
-			"libc": [
-				"musl"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -1690,9 +1678,6 @@
 				"loong64"
 			],
 			"dev": true,
-			"libc": [
-				"glibc"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -1707,9 +1692,6 @@
 				"loong64"
 			],
 			"dev": true,
-			"libc": [
-				"musl"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -1724,9 +1706,6 @@
 				"ppc64"
 			],
 			"dev": true,
-			"libc": [
-				"glibc"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -1741,9 +1720,6 @@
 				"ppc64"
 			],
 			"dev": true,
-			"libc": [
-				"musl"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -1758,9 +1734,6 @@
 				"riscv64"
 			],
 			"dev": true,
-			"libc": [
-				"glibc"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -1775,9 +1748,6 @@
 				"riscv64"
 			],
 			"dev": true,
-			"libc": [
-				"musl"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -1792,9 +1762,6 @@
 				"s390x"
 			],
 			"dev": true,
-			"libc": [
-				"glibc"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -1809,9 +1776,6 @@
 				"x64"
 			],
 			"dev": true,
-			"libc": [
-				"glibc"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -1826,9 +1790,6 @@
 				"x64"
 			],
 			"dev": true,
-			"libc": [
-				"musl"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -2228,9 +2189,6 @@
 				"arm64"
 			],
 			"dev": true,
-			"libc": [
-				"glibc"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -2248,9 +2206,6 @@
 				"arm64"
 			],
 			"dev": true,
-			"libc": [
-				"musl"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -2268,9 +2223,6 @@
 				"x64"
 			],
 			"dev": true,
-			"libc": [
-				"glibc"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -2288,9 +2240,6 @@
 				"x64"
 			],
 			"dev": true,
-			"libc": [
-				"musl"
-			],
 			"license": "MIT",
 			"optional": true,
 			"os": [
@@ -5924,9 +5873,6 @@
 				"arm64"
 			],
 			"dev": true,
-			"libc": [
-				"glibc"
-			],
 			"license": "MPL-2.0",
 			"optional": true,
 			"os": [
@@ -5948,9 +5894,6 @@
 				"arm64"
 			],
 			"dev": true,
-			"libc": [
-				"musl"
-			],
 			"license": "MPL-2.0",
 			"optional": true,
 			"os": [
@@ -5972,9 +5915,6 @@
 				"x64"
 			],
 			"dev": true,
-			"libc": [
-				"glibc"
-			],
 			"license": "MPL-2.0",
 			"optional": true,
 			"os": [
@@ -5996,9 +5936,6 @@
 				"x64"
 			],
 			"dev": true,
-			"libc": [
-				"musl"
-			],
 			"license": "MPL-2.0",
 			"optional": true,
 			"os": [

+ 2 - 2
src/lib/components/agents/form/form.svelte

@@ -259,8 +259,8 @@
 			<DeploymentForm
 				{disabled}
 				runtimes={[
-					{ value: 'loco-agent', label: 'loco-agents', versions: [] },
-					{ value: 'opencode', label: 'OpenCode', versions: [] },
+					{ value: 'lambdagent', label: 'LambdaAgent', versions: [] },
+					{ value: 'loco-agents', label: 'LocoStack Agents', versions: [] },
 				]}
 				{workspaceId}
 				bind:form

+ 5 - 2
src/lib/components/agents/form/types.ts

@@ -26,8 +26,11 @@ const DEFAULT_AGENT_FORM_VALUE: AgentFormValue = {
 	tools: [],
 	kbs: [],
 	subagents: [],
-	runtime: 'loco-agents',
-	runtimeVersion: 'loco-simple-agent',
+	// "lambdagent" is the only runtime with a published image today. Leaving
+	// runtimeVersion unset is deliberate: any value here is written to
+	// spec.template.spec.runtime.name, which makes the operator look up that
+	// runtime and overwrite the template's image.
+	runtime: 'lambdagent',
 };
 
 const toAgentFormValue = (agent: Agent): AgentFormValue => {

+ 3 - 1
src/routes/api/workspaces/+server.ts

@@ -10,7 +10,9 @@ const GET: RequestHandler = async ({ locals }) => {
 	if (!user) {
 		throw new UnauthorizedError('Authentication required');
 	}
-	const workspaces = server.ws.list().filter((ws) => user.workspaces.includes(ws.id));
+	const workspaces = server.ws
+		.list()
+		.filter((ws) => user.workspaces.includes(ws.id) || user.workspaces.includes('*'));
 	return json(workspaces);
 };
 

+ 38 - 3
src/routes/api/workspaces/[workspaceId]/agents/[id]/test/+server.ts

@@ -3,6 +3,8 @@ import { json } from '@sveltejs/kit';
 import { type ChatCompletionRequest, type ChatMessage, SESSION_ID_HEADER } from '$lib/api/agents';
 import { NotFoundError, server } from '$lib/server';
 
+import { env } from '$env/dynamic/private';
+
 import type { RequestHandler } from './$types';
 
 const SSE_HEADERS = {
@@ -14,6 +16,36 @@ const SSE_HEADERS = {
 /** Runtime template name used by agents that run the OpenCode headless server. */
 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. */
 const toChatCompletionStream = (text: string): string => {
 	const chunk = { choices: [{ delta: { content: text }, index: 0 }] };
@@ -30,7 +62,7 @@ const proxyOpenAIChat = async (
 	messages: ChatMessage[],
 	signal: AbortSignal,
 ): Promise<Response> => {
-	const resp = await fetch(new URL('/v1/chat/completions', endpoint), {
+	const resp = await fetch(joinUrl(endpoint, '/v1/chat/completions'), {
 		method: 'POST',
 		headers: { 'Content-Type': 'application/json' },
 		body: JSON.stringify({ messages, stream: true }),
@@ -51,7 +83,7 @@ type OpenCodeSession = { id: string };
 type OpenCodePart = { type: string; text?: string };
 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 resp = await fetch(openCodeUrl(endpoint, '/session'), {
@@ -157,7 +189,10 @@ const POST: RequestHandler = async ({ locals, params, request }) => {
 		.getWorkspaceResources(workspaceId)
 		.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) {
 		return json(
 			{ code: 'ENDPOINT_UNAVAILABLE', message: 'Agent endpoint is not yet available' },