@@ -9,17 +9,35 @@ import {
99import type { BaseServerTool , ServerToolContext } from '@/lib/copilot/tools/server/base-tool'
1010import { upsertPersonalEnvVars , upsertWorkspaceEnvVars } from '@/lib/environment/utils'
1111
12+ type EnvironmentVariableInputValue = string | number | boolean | null | undefined
13+
14+ interface EnvironmentVariableInput {
15+ name : string
16+ value : EnvironmentVariableInputValue
17+ }
18+
1219interface SetEnvironmentVariablesParams {
13- variables : Record < string , any > | Array < { name : string ; value : string } >
20+ variables : Record < string , EnvironmentVariableInputValue > | EnvironmentVariableInput [ ]
1421 scope ?: 'personal' | 'workspace'
1522 workflowId ?: string
1623 workspaceId ?: string
1724}
1825
26+ interface SetEnvironmentVariablesResult {
27+ message : string
28+ scope : 'personal' | 'workspace'
29+ workspaceId ?: string
30+ variableCount : number
31+ variableNames : string [ ]
32+ addedVariables : string [ ]
33+ updatedVariables : string [ ]
34+ workspaceUpdatedVariables : string [ ]
35+ }
36+
1937const EnvVarSchema = z . object ( { variables : z . record ( z . string ( ) ) } )
2038
2139function normalizeVariables (
22- input : Record < string , any > | Array < { name : string ; value : string } >
40+ input : Record < string , EnvironmentVariableInputValue > | EnvironmentVariableInput [ ]
2341) : Record < string , string > {
2442 if ( Array . isArray ( input ) ) {
2543 return input . reduce (
@@ -59,73 +77,75 @@ async function resolveWorkspaceId(
5977 return getDefaultWorkspaceId ( userId )
6078}
6179
62- export const setEnvironmentVariablesServerTool : BaseServerTool < SetEnvironmentVariablesParams , any > =
63- {
64- name : SetEnvironmentVariables . id ,
65- async execute (
66- params : SetEnvironmentVariablesParams ,
67- context ?: ServerToolContext
68- ) : Promise < any > {
69- const logger = createLogger ( 'SetEnvironmentVariablesServerTool' )
70-
71- if ( ! context ?. userId ) {
72- logger . error (
73- 'Unauthorized attempt to set environment variables - no authenticated user context'
74- )
75- throw new Error ( 'Authentication required' )
76- }
77-
78- const authenticatedUserId = context . userId
79- const { variables } = params || ( { } as SetEnvironmentVariablesParams )
80- const scope = params . scope === 'personal' ? 'personal' : 'workspace'
81-
82- const normalized = normalizeVariables ( variables || { } )
83- const { variables : validatedVariables } = EnvVarSchema . parse ( { variables : normalized } )
84- const variableNames = Object . keys ( validatedVariables )
85- const added : string [ ] = [ ]
86- const updated : string [ ] = [ ]
87- let workspaceUpdated : string [ ] = [ ]
88-
89- let resolvedWorkspaceId : string | undefined
90- if ( scope === 'workspace' ) {
91- resolvedWorkspaceId = await resolveWorkspaceId ( params , context , authenticatedUserId )
92- workspaceUpdated = await upsertWorkspaceEnvVars (
93- resolvedWorkspaceId ,
94- validatedVariables ,
95- authenticatedUserId
96- )
97- } else {
98- const result = await upsertPersonalEnvVars ( authenticatedUserId , validatedVariables )
99- added . push ( ...result . added )
100- updated . push ( ...result . updated )
101- }
102-
103- const totalProcessed = added . length + updated . length + workspaceUpdated . length
104-
105- logger . info ( 'Saved environment variables' , {
106- userId : authenticatedUserId ,
107- scope,
108- addedCount : added . length ,
109- updatedCount : updated . length ,
110- workspaceUpdatedCount : workspaceUpdated . length ,
111- workspaceId : resolvedWorkspaceId ,
112- } )
113-
114- const parts : string [ ] = [ ]
115- if ( added . length > 0 ) parts . push ( `${ added . length } personal secret(s) added` )
116- if ( updated . length > 0 ) parts . push ( `${ updated . length } personal secret(s) updated` )
117- if ( workspaceUpdated . length > 0 )
118- parts . push ( `${ workspaceUpdated . length } workspace secret(s) updated` )
119-
120- return {
121- message : `Successfully processed ${ totalProcessed } secret(s): ${ parts . join ( ', ' ) } ` ,
122- scope,
123- workspaceId : resolvedWorkspaceId ,
124- variableCount : variableNames . length ,
125- variableNames,
126- addedVariables : added ,
127- updatedVariables : updated ,
128- workspaceUpdatedVariables : workspaceUpdated ,
129- }
130- } ,
131- }
80+ export const setEnvironmentVariablesServerTool : BaseServerTool <
81+ SetEnvironmentVariablesParams ,
82+ SetEnvironmentVariablesResult
83+ > = {
84+ name : SetEnvironmentVariables . id ,
85+ async execute (
86+ params : SetEnvironmentVariablesParams ,
87+ context ?: ServerToolContext
88+ ) : Promise < SetEnvironmentVariablesResult > {
89+ const logger = createLogger ( 'SetEnvironmentVariablesServerTool' )
90+
91+ if ( ! context ?. userId ) {
92+ logger . error (
93+ 'Unauthorized attempt to set environment variables - no authenticated user context'
94+ )
95+ throw new Error ( 'Authentication required' )
96+ }
97+
98+ const authenticatedUserId = context . userId
99+ const { variables } = params || ( { } as SetEnvironmentVariablesParams )
100+ const scope = params . scope === 'personal' ? 'personal' : 'workspace'
101+
102+ const normalized = normalizeVariables ( variables || { } )
103+ const { variables : validatedVariables } = EnvVarSchema . parse ( { variables : normalized } )
104+ const variableNames = Object . keys ( validatedVariables )
105+ const added : string [ ] = [ ]
106+ const updated : string [ ] = [ ]
107+ let workspaceUpdated : string [ ] = [ ]
108+
109+ let resolvedWorkspaceId : string | undefined
110+ if ( scope === 'workspace' ) {
111+ resolvedWorkspaceId = await resolveWorkspaceId ( params , context , authenticatedUserId )
112+ workspaceUpdated = await upsertWorkspaceEnvVars (
113+ resolvedWorkspaceId ,
114+ validatedVariables ,
115+ authenticatedUserId
116+ )
117+ } else {
118+ const result = await upsertPersonalEnvVars ( authenticatedUserId , validatedVariables )
119+ added . push ( ...result . added )
120+ updated . push ( ...result . updated )
121+ }
122+
123+ const totalProcessed = added . length + updated . length + workspaceUpdated . length
124+
125+ logger . info ( 'Saved environment variables' , {
126+ userId : authenticatedUserId ,
127+ scope,
128+ addedCount : added . length ,
129+ updatedCount : updated . length ,
130+ workspaceUpdatedCount : workspaceUpdated . length ,
131+ workspaceId : resolvedWorkspaceId ,
132+ } )
133+
134+ const parts : string [ ] = [ ]
135+ if ( added . length > 0 ) parts . push ( `${ added . length } personal secret(s) added` )
136+ if ( updated . length > 0 ) parts . push ( `${ updated . length } personal secret(s) updated` )
137+ if ( workspaceUpdated . length > 0 )
138+ parts . push ( `${ workspaceUpdated . length } workspace secret(s) updated` )
139+
140+ return {
141+ message : `Successfully processed ${ totalProcessed } secret(s): ${ parts . join ( ', ' ) } ` ,
142+ scope,
143+ workspaceId : resolvedWorkspaceId ,
144+ variableCount : variableNames . length ,
145+ variableNames,
146+ addedVariables : added ,
147+ updatedVariables : updated ,
148+ workspaceUpdatedVariables : workspaceUpdated ,
149+ }
150+ } ,
151+ }
0 commit comments