From 188200e3b1a7c8ec8cb085b9c234a14cd08e6319 Mon Sep 17 00:00:00 2001 From: hailongzhao Date: Thu, 27 Aug 2026 11:12:55 +0800 Subject: [PATCH] feat(sandbox): inject per-user and workspace skill env vars at execution Skills declare the environment variables they read at run time, so a credential no longer has to be an admin-set value shared by everyone using the same skill image. The installer agent writes a declaration the server validates against the bundle. Admins fill workspace-wide values from the skill drawer, members fill their own under Settings, and a successful shell_exec captures the declared names a run already used for the current speaker. Resolution is keyed by Principal so IM callers do not collapse onto one synthetic account, and values are injected only into the process of that one run. Values are AES-GCM encrypted at rest and never returned by any endpoint. --- frontend/src/api/env-vars.ts | 85 +++ frontend/src/api/system/index.ts | 22 +- .../components/SandboxConfigEditorDrawer.vue | 3 - .../src/components/SandboxSkillsPanel.vue | 302 +++++++- .../src/config/deploymentCapabilities.test.ts | 8 + frontend/src/config/deploymentCapabilities.ts | 4 + frontend/src/config/settingsAccess.test.ts | 11 + frontend/src/config/settingsAccess.ts | 3 + frontend/src/i18n/locales/en-US.ts | 58 ++ frontend/src/i18n/locales/ko-KR.ts | 58 ++ frontend/src/i18n/locales/ru-RU.ts | 58 ++ frontend/src/i18n/locales/zh-CN.ts | 58 ++ .../src/views/settings/EnvVarSettings.vue | 646 ++++++++++++++++++ frontend/src/views/settings/Settings.vue | 9 +- .../src/views/settings/envVarState.test.ts | 233 +++++++ frontend/src/views/settings/envVarState.ts | 162 +++++ internal/agent/skills/env_resolver.go | 61 ++ internal/agent/skills/env_resolver_test.go | 145 ++++ internal/agent/skills/manager.go | 36 + internal/agent/skills/tenant_source_test.go | 2 + internal/agent/tools/shell_env_extract.go | 75 ++ .../agent/tools/shell_env_extract_test.go | 67 ++ internal/agent/tools/shell_exec.go | 113 ++- internal/agent/tools/shell_exec_test.go | 85 ++- .../application/repository/tenant_skill.go | 150 +++- .../repository/tenant_skill_test.go | 240 ++++++- internal/application/service/agent_service.go | 73 +- .../agent_service_install_shell_test.go | 4 +- .../service/agent_service_user_env_test.go | 87 +++ .../service/tenant_sandbox_config.go | 7 + .../service/tenant_sandbox_config_test.go | 23 +- .../application/service/tenant_skill_admin.go | 89 ++- .../service/tenant_skill_admin_test.go | 149 ++++ .../service/tenant_skill_env_declare.go | 207 ++++++ .../service/tenant_skill_env_declare_test.go | 260 +++++++ .../service/tenant_skill_install.go | 87 ++- .../service/tenant_skill_install_test.go | 367 +++++++++- .../service/tenant_skill_reaper_test.go | 24 + internal/application/service/user_env.go | 465 +++++++++++++ .../application/service/user_env_resolver.go | 116 ++++ .../service/user_env_resolver_test.go | 305 +++++++++ internal/application/service/user_env_test.go | 505 ++++++++++++++ internal/container/container.go | 5 + internal/handler/me_env_var.go | 197 ++++++ internal/handler/me_env_var_test.go | 227 ++++++ internal/handler/sandbox_skill.go | 63 +- internal/handler/sandbox_skill_env_test.go | 124 ++++ internal/handler/sandbox_skill_test.go | 34 +- internal/router/router.go | 2 + internal/router/routes_auth_tenant.go | 26 + internal/sandbox/skill_paths.go | 17 + internal/types/tenant_env_vars.go | 196 ++++++ internal/types/tenant_env_vars_test.go | 161 +++++ internal/types/tenant_skill.go | 4 + migrations/versioned/000089_env_vars.down.sql | 4 + migrations/versioned/000089_env_vars.up.sql | 49 ++ 56 files changed, 6511 insertions(+), 60 deletions(-) create mode 100644 frontend/src/api/env-vars.ts create mode 100644 frontend/src/views/settings/EnvVarSettings.vue create mode 100644 frontend/src/views/settings/envVarState.test.ts create mode 100644 frontend/src/views/settings/envVarState.ts create mode 100644 internal/agent/skills/env_resolver.go create mode 100644 internal/agent/skills/env_resolver_test.go create mode 100644 internal/agent/tools/shell_env_extract.go create mode 100644 internal/agent/tools/shell_env_extract_test.go create mode 100644 internal/application/service/agent_service_user_env_test.go create mode 100644 internal/application/service/tenant_skill_env_declare.go create mode 100644 internal/application/service/tenant_skill_env_declare_test.go create mode 100644 internal/application/service/user_env.go create mode 100644 internal/application/service/user_env_resolver.go create mode 100644 internal/application/service/user_env_resolver_test.go create mode 100644 internal/application/service/user_env_test.go create mode 100644 internal/handler/me_env_var.go create mode 100644 internal/handler/me_env_var_test.go create mode 100644 internal/handler/sandbox_skill_env_test.go create mode 100644 internal/types/tenant_env_vars.go create mode 100644 internal/types/tenant_env_vars_test.go create mode 100644 migrations/versioned/000089_env_vars.down.sql create mode 100644 migrations/versioned/000089_env_vars.up.sql diff --git a/frontend/src/api/env-vars.ts b/frontend/src/api/env-vars.ts new file mode 100644 index 00000000..5dc64c28 --- /dev/null +++ b/frontend/src/api/env-vars.ts @@ -0,0 +1,85 @@ +import { del, get, put } from '@/utils/request' + +/** + * Which value an execution would actually use for one variable. + * + * `unset` means nothing is stored anywhere, `workspace` means an admin filled + * it in for everyone, `user` means the caller filled in their own — which wins + * over the workspace one for this caller only. + */ +export type EnvVarSource = 'unset' | 'workspace' | 'user' + +/** + * One variable as its owner may see it. There is deliberately no value field: + * the endpoint reports what is missing, never what is stored. + */ +export interface EnvVarView { + name: string + description?: string + required?: boolean + source: EnvVarSource + /** Present only for the caller's own value; a workspace value has no per-variable timestamp. */ + updated_at?: string +} + +/** One skill's declared credentials. */ +export interface SkillEnvGroup { + skill_id: string + skill_name: string + vars: EnvVarView[] +} + +/** + * One sandbox config: the caller's own config-wide variables, injected into + * every execution on it, plus the credentials its skills declared. + */ +export interface ConfigEnvGroup { + sandbox_config_id: string + sandbox_config_name: string + vars: EnvVarView[] + skills: SkillEnvGroup[] +} + +export function listMyEnvVars(): Promise<{ data: ConfigEnvGroup[] }> { + return get('/api/v1/me/env-vars') as unknown as Promise<{ data: ConfigEnvGroup[] }> +} + +export function setMySkillEnv( + skillId: string, + name: string, + value: string, +): Promise<{ success: boolean }> { + return put('/api/v1/me/env-vars/skill', { skill_id: skillId, name, value }) as unknown as Promise<{ + success: boolean + }> +} + +/** + * Removes the caller's own value, after which the workspace value (if any) + * applies again. The scope and name travel in a JSON body rather than the query + * string, matching the PUT above. + */ +export function deleteMySkillEnv(skillId: string, name: string): Promise<{ success: boolean }> { + return del('/api/v1/me/env-vars/skill', { skill_id: skillId, name }) as unknown as Promise<{ + success: boolean + }> +} + +export function setMySandboxEnv( + configId: string, + name: string, + value: string, +): Promise<{ success: boolean }> { + return put('/api/v1/me/env-vars/sandbox', { + sandbox_config_id: configId, + name, + value, + }) as unknown as Promise<{ success: boolean }> +} + +export function deleteMySandboxEnv(configId: string, name: string): Promise<{ success: boolean }> { + return del('/api/v1/me/env-vars/sandbox', { + sandbox_config_id: configId, + name, + }) as unknown as Promise<{ success: boolean }> +} diff --git a/frontend/src/api/system/index.ts b/frontend/src/api/system/index.ts index 51760de1..12788104 100644 --- a/frontend/src/api/system/index.ts +++ b/frontend/src/api/system/index.ts @@ -1006,6 +1006,18 @@ export function parseSandboxConflict(err: unknown): SandboxConflict | null { export type ConfigSkillStatus = 'installing' | 'ready' | 'failed' | 'removing' | 'removed' +/** + * One environment variable the skill's installer declared. `is_set` reports + * whether a workspace-wide value exists; the value itself is never returned, + * so an editor can show that something is stored but not what. + */ +export interface ConfigSkillEnv { + name: string + description?: string + required?: boolean + is_set: boolean +} + export interface ConfigSkill { id: string name: string @@ -1023,6 +1035,9 @@ export interface ConfigSkill { install_message_id?: string created_at: string updated_at: string + // Absent for a skill whose installer declared nothing, which is how the + // panel decides whether to offer the environment variable editor at all. + envs?: ConfigSkillEnv[] } export interface ConfigSkillInstallEvent { @@ -1069,10 +1084,15 @@ export function reinstallConfigSkill( ) as unknown as Promise<{ data: { skill_id: string } }> } +/** + * Partial update: an absent field is left alone. `envs` names only the + * variables to write — an entry with an empty string clears the stored value + * while keeping the declaration, and undeclared names are ignored server-side. + */ export function patchConfigSkill( configId: string, skillId: string, - payload: { enabled: boolean }, + payload: { enabled?: boolean; envs?: Record }, ): Promise<{ data: ConfigSkill }> { return patch(`/api/v1/sandbox-configs/${configId}/skills/${skillId}`, payload) as unknown as Promise<{ data: ConfigSkill diff --git a/frontend/src/components/SandboxConfigEditorDrawer.vue b/frontend/src/components/SandboxConfigEditorDrawer.vue index 97b7ce14..35d1833e 100644 --- a/frontend/src/components/SandboxConfigEditorDrawer.vue +++ b/frontend/src/components/SandboxConfigEditorDrawer.vue @@ -472,9 +472,6 @@

{{ $t('settings.sandbox.checkPendingHint', { names: pendingCheckNames.join('、') }) }}

- diff --git a/frontend/src/components/SandboxSkillsPanel.vue b/frontend/src/components/SandboxSkillsPanel.vue index 1ccb7de6..5ee4cf11 100644 --- a/frontend/src/components/SandboxSkillsPanel.vue +++ b/frontend/src/components/SandboxSkillsPanel.vue @@ -179,6 +179,27 @@ @change="(v: any) => toggleEnabled(skill, Boolean(v))" /> + + +