Skip to content

Commit

Permalink
Fix some typo and coding convention
Browse files Browse the repository at this point in the history
  • Loading branch information
nosyn committed Nov 22, 2023
1 parent 36596fa commit 3eaed9f
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 47 deletions.
8 changes: 4 additions & 4 deletions server/ai/anthropic/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ func (c *Client) CompletionNoStream(prompt string) (string, error) {
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errors.Wrap(err, "unable to read responce body on error: "+resp.Status)
return "", errors.Wrap(err, "unable to read response body on error: "+resp.Status)
}

return "", errors.New("non 200 responce from anthropic: " + resp.Status + "\nBody:\n" + string(body))
return "", errors.New("non 200 response from anthropic: " + resp.Status + "\nBody:\n" + string(body))
}

completionResponse := CompletionResponse{}
Expand Down Expand Up @@ -126,11 +126,11 @@ func (c *Client) Completion(prompt string) (*ai.TextStreamResult, error) {
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
errChan <- errors.Wrap(err, "unable to read responce body on error: "+resp.Status)
errChan <- errors.Wrap(err, "unable to read response body on error: "+resp.Status)
return
}

errChan <- errors.New("non 200 responce from anthropic: " + resp.Status + "\nBody:\n" + string(body))
errChan <- errors.New("non 200 response from anthropic: " + resp.Status + "\nBody:\n" + string(body))
return
}

Expand Down
2 changes: 1 addition & 1 deletion server/ai/asksage/asksage.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (s *AskSage) ChatCompletionNoStream(conversation ai.BotConversation, opts .
return response.Message, nil
}

// TODO: Implment actual token counting. For now just estimated based off OpenAI estimations
// TODO: Implement actual token counting. For now just estimated based off OpenAI estimations
func (a *AskSage) CountTokens(text string) int {
charCount := float64(len(text)) / 4.0
wordCount := float64(len(strings.Fields(text))) / 0.75
Expand Down
4 changes: 2 additions & 2 deletions server/ai/asksage/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ func (c *Client) do(method, path string, body interface{}, result interface{}) e
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "unable to read responce body on error: "+resp.Status)
return errors.Wrap(err, "unable to read response body on error: "+resp.Status)
}

return errors.New("non 200 responce from asksage: " + resp.Status + "\nBody:\n" + string(body))
return errors.New("non 200 response from asksage: " + resp.Status + "\nBody:\n" + string(body))
}

// Decode response body into specified struct
Expand Down
10 changes: 5 additions & 5 deletions server/ai/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ type ConversationContext struct {
PromptParameters map[string]string
}

func NewConversationContext(reqeustingUser *model.User, channel *model.Channel, post *model.Post) ConversationContext {
// Get current time and date formated nicely with the user's locale
func NewConversationContext(requestingUser *model.User, channel *model.Channel, post *model.Post) ConversationContext {
// Get current time and date formatted nicely with the user's locale
now := time.Now()
nowString := now.Format(time.RFC1123)
if reqeustingUser != nil {
tz := reqeustingUser.GetPreferredTimezone()
if requestingUser != nil {
tz := requestingUser.GetPreferredTimezone()
loc, err := time.LoadLocation(tz)
if err != nil || loc == nil {
loc = time.UTC
Expand All @@ -47,7 +47,7 @@ func NewConversationContext(reqeustingUser *model.User, channel *model.Channel,
}
return ConversationContext{
Time: nowString,
RequestingUser: reqeustingUser,
RequestingUser: requestingUser,
Channel: channel,
Post: post,
}
Expand Down
2 changes: 1 addition & 1 deletion server/ai/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func WithModel(model string) LanguageModelOption {
}
}

func WithmaxTokens(maxTokens int) LanguageModelOption {
func WithMaxTokens(maxTokens int) LanguageModelOption {
return func(cfg *LLMConfig) {
cfg.MaxTokens = maxTokens
}
Expand Down
4 changes: 2 additions & 2 deletions server/ai/openai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (s *OpenAI) createConfig(opts []ai.LanguageModelOption) ai.LLMConfig {
return cfg
}

func (s *OpenAI) completionReqeustFromConfig(cfg ai.LLMConfig) openaiClient.ChatCompletionRequest {
func (s *OpenAI) completionRequestFromConfig(cfg ai.LLMConfig) openaiClient.ChatCompletionRequest {
return openaiClient.ChatCompletionRequest{
Model: cfg.Model,
MaxTokens: cfg.MaxTokens,
Expand All @@ -232,7 +232,7 @@ func (s *OpenAI) completionReqeustFromConfig(cfg ai.LLMConfig) openaiClient.Chat
}

func (s *OpenAI) ChatCompletion(conversation ai.BotConversation, opts ...ai.LanguageModelOption) (*ai.TextStreamResult, error) {
request := s.completionReqeustFromConfig(s.createConfig(opts))
request := s.completionRequestFromConfig(s.createConfig(opts))
request = modifyCompletionRequestWithConversation(request, conversation)
request.Stream = true
return s.streamResult(request, conversation)
Expand Down
32 changes: 16 additions & 16 deletions server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ func TestPostRouter(t *testing.T) {
request *http.Request
expectedStatus int
config Config
envSetup func(e *TestEnviroment)
envSetup func(e *TestEnvironment)
}{
"test no permission to channel": {
request: httptest.NewRequest("POST", url, nil),
expectedStatus: http.StatusForbidden,
config: Config{
EnableUseRestrictions: false,
},
envSetup: func(e *TestEnviroment) {
envSetup: func(e *TestEnvironment) {
e.mockAPI.On("GetChannel", "channelid").Return(&model.Channel{
Id: "channelid",
Type: model.ChannelTypeOpen,
Expand All @@ -55,7 +55,7 @@ func TestPostRouter(t *testing.T) {
EnableUseRestrictions: true,
OnlyUsersOnTeam: "someotherteam",
},
envSetup: func(e *TestEnviroment) {
envSetup: func(e *TestEnvironment) {
e.mockAPI.On("GetChannel", "channelid").Return(&model.Channel{
Id: "channelid",
Type: model.ChannelTypeOpen,
Expand All @@ -72,7 +72,7 @@ func TestPostRouter(t *testing.T) {
EnableUseRestrictions: true,
AllowedTeamIDs: "someteam",
},
envSetup: func(e *TestEnviroment) {
envSetup: func(e *TestEnvironment) {
e.mockAPI.On("GetChannel", "channelid").Return(&model.Channel{
Id: "channelid",
Type: model.ChannelTypeOpen,
Expand All @@ -88,7 +88,7 @@ func TestPostRouter(t *testing.T) {
EnableUseRestrictions: true,
AllowPrivateChannels: false,
},
envSetup: func(e *TestEnviroment) {
envSetup: func(e *TestEnvironment) {
e.mockAPI.On("HasPermissionToChannel", "userid", "channelid", model.PermissionReadChannel).Return(true)
e.mockAPI.On("GetChannel", "channelid").Return(&model.Channel{
Id: "channelid",
Expand All @@ -104,7 +104,7 @@ func TestPostRouter(t *testing.T) {
EnableUseRestrictions: true,
AllowPrivateChannels: false,
},
envSetup: func(e *TestEnviroment) {
envSetup: func(e *TestEnvironment) {
e.mockAPI.On("HasPermissionToChannel", "userid", "channelid", model.PermissionReadChannel).Return(true)
e.mockAPI.On("GetChannel", "channelid").Return(&model.Channel{
Id: "channelid",
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestTextRouter(t *testing.T) {
request *http.Request
expectedStatus int
config Config
envSetup func(e *TestEnviroment)
envSetup func(e *TestEnvironment)
}{
"test user not allowed": {
request: httptest.NewRequest("POST", url, nil),
Expand All @@ -162,7 +162,7 @@ func TestTextRouter(t *testing.T) {
EnableUseRestrictions: true,
OnlyUsersOnTeam: "someotherteam",
},
envSetup: func(e *TestEnviroment) {
envSetup: func(e *TestEnvironment) {
e.mockAPI.On("HasPermissionToTeam", "userid", "someotherteam", model.PermissionViewTeam).Return(false)
},
},
Expand Down Expand Up @@ -201,15 +201,15 @@ func TestAdminRouter(t *testing.T) {
request *http.Request
expectedStatus int
config Config
envSetup func(e *TestEnviroment)
envSetup func(e *TestEnvironment)
}{
"only admins": {
request: httptest.NewRequest("GET", url, nil),
expectedStatus: http.StatusForbidden,
config: Config{
EnableUseRestrictions: false,
},
envSetup: func(e *TestEnviroment) {
envSetup: func(e *TestEnvironment) {
e.mockAPI.On("HasPermissionTo", "userid", model.PermissionManageSystem).Return(false)
},
},
Expand Down Expand Up @@ -248,15 +248,15 @@ func TestChannelRouter(t *testing.T) {
request *http.Request
expectedStatus int
config Config
envSetup func(e *TestEnviroment)
envSetup func(e *TestEnvironment)
}{
"test no permission to channel": {
request: httptest.NewRequest("POST", url, nil),
expectedStatus: http.StatusForbidden,
config: Config{
EnableUseRestrictions: false,
},
envSetup: func(e *TestEnviroment) {
envSetup: func(e *TestEnvironment) {
e.mockAPI.On("GetChannel", "channelid").Return(&model.Channel{
Id: "channelid",
Type: model.ChannelTypeOpen,
Expand All @@ -272,7 +272,7 @@ func TestChannelRouter(t *testing.T) {
EnableUseRestrictions: true,
OnlyUsersOnTeam: "someotherteam",
},
envSetup: func(e *TestEnviroment) {
envSetup: func(e *TestEnvironment) {
e.mockAPI.On("GetChannel", "channelid").Return(&model.Channel{
Id: "channelid",
Type: model.ChannelTypeOpen,
Expand All @@ -289,7 +289,7 @@ func TestChannelRouter(t *testing.T) {
EnableUseRestrictions: true,
AllowedTeamIDs: "someteam",
},
envSetup: func(e *TestEnviroment) {
envSetup: func(e *TestEnvironment) {
e.mockAPI.On("GetChannel", "channelid").Return(&model.Channel{
Id: "channelid",
Type: model.ChannelTypeOpen,
Expand All @@ -305,7 +305,7 @@ func TestChannelRouter(t *testing.T) {
EnableUseRestrictions: true,
AllowPrivateChannels: false,
},
envSetup: func(e *TestEnviroment) {
envSetup: func(e *TestEnvironment) {
e.mockAPI.On("HasPermissionToChannel", "userid", "channelid", model.PermissionReadChannel).Return(true)
e.mockAPI.On("GetChannel", "channelid").Return(&model.Channel{
Id: "channelid",
Expand All @@ -321,7 +321,7 @@ func TestChannelRouter(t *testing.T) {
EnableUseRestrictions: true,
AllowPrivateChannels: false,
},
envSetup: func(e *TestEnviroment) {
envSetup: func(e *TestEnvironment) {
e.mockAPI.On("HasPermissionToChannel", "userid", "channelid", model.PermissionReadChannel).Return(true)
e.mockAPI.On("GetChannel", "channelid").Return(&model.Channel{
Id: "channelid",
Expand Down
6 changes: 3 additions & 3 deletions server/built_in_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

type LookupMattermostUserArgs struct {
Username string `jsonschema_description:"The username of the user to lookup witout a leading '@'. Example: 'firstname.lastname'"`
Username string `jsonschema_description:"The username of the user to lookup without a leading '@'. Example: 'firstname.lastname'"`
}

func (p *Plugin) toolResolveLookupMattermostUser(context ai.ConversationContext, argsGetter ai.ToolArgumentGetter) (string, error) {
Expand Down Expand Up @@ -149,7 +149,7 @@ func (p *Plugin) getBuiltInTools() []ai.Tool {
builtInTools := []ai.Tool{
{
Name: "LookupMattermostUser",
Description: "Lookup a Mattermost user by their username. Avalable information includes: username, full name, email, nickname, position, locale, timezone, last activity, and status.",
Description: "Lookup a Mattermost user by their username. Available information includes: username, full name, email, nickname, position, locale, timezone, last activity, and status.",
Schema: LookupMattermostUserArgs{},
Resolver: p.toolResolveLookupMattermostUser,
},
Expand All @@ -168,7 +168,7 @@ func (p *Plugin) getBuiltInTools() []ai.Tool {
} else if status != nil && status.State == model.PluginStateRunning {
builtInTools = append(builtInTools, ai.Tool{
Name: "GetGithubIssue",
Description: "Retrive a single GitHub issue by owner, repo, and issue number.",
Description: "Retrieve a single GitHub issue by owner, repo, and issue number.",
Schema: GetGithubIssueArgs{},
Resolver: p.toolGetGithubIssue,
})
Expand Down
12 changes: 6 additions & 6 deletions server/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"github.com/stretchr/testify/require"
)

type TestEnviroment struct {
type TestEnvironment struct {
plugin *Plugin
mockAPI *plugintest.API
}

func SetupTestEnvironment(t *testing.T) *TestEnviroment {
func SetupTestEnvironment(t *testing.T) *TestEnvironment {
p := Plugin{}

p.botid = "botid"
Expand All @@ -26,21 +26,21 @@ func SetupTestEnvironment(t *testing.T) *TestEnviroment {

p.ffmpegPath = ""

e := &TestEnviroment{
e := &TestEnvironment{
plugin: &p,
}
e.ResetMocks(t)

return e
}

func (e *TestEnviroment) ResetMocks(t *testing.T) {
func (e *TestEnvironment) ResetMocks(t *testing.T) {
e.mockAPI = &plugintest.API{}
e.plugin.SetAPI(e.mockAPI)
e.plugin.pluginAPI = pluginapi.NewClient(e.plugin.API, e.plugin.Driver)
}

func (e *TestEnviroment) Cleanup(t *testing.T) {
func (e *TestEnvironment) Cleanup(t *testing.T) {
t.Helper()
e.mockAPI.AssertExpectations(t)
}
Expand Down Expand Up @@ -267,7 +267,7 @@ func TestHandleAudioCallsRecording(t *testing.T) {
require.ErrorIs(t, err, ErrUsageRestriction)
})

t.Run("don't respond if somone is trying to spoof the calls bot", func(t *testing.T) {
t.Run("don't respond if someone is trying to spoof the calls bot", func(t *testing.T) {
e.ResetMocks(t)
e.plugin.setConfiguration(makeConfig(Config{
EnableAutomaticCallsSummary: true,
Expand Down
14 changes: 7 additions & 7 deletions server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (p *Plugin) continueConversation(context ai.ConversationContext) error {
return err
}

// Special handing for threads started by the bot in responce to a summarization request.
// Special handing for threads started by the bot in response to a summarization request.
var result *ai.TextStreamResult
originalThreadID, ok := threadData.Posts[0].GetProp(ThreadIDProp).(string)
if ok && originalThreadID != "" {
Expand Down Expand Up @@ -169,12 +169,12 @@ func (p *Plugin) selectEmoji(postToReact *model.Post, context ai.ConversationCon
return err
}

emojiName, err := p.getLLM().ChatCompletionNoStream(prompt, ai.WithmaxTokens(25))
emojiName, err := p.getLLM().ChatCompletionNoStream(prompt, ai.WithMaxTokens(25))
if err != nil {
return err
}

// Do some emoji post processing to hopfully make this an actual emoji.
// Do some emoji post processing to hopefully make this an actual emoji.
emojiName = strings.Trim(strings.TrimSpace(emojiName), ":")

if _, found := model.GetSystemEmojiId(emojiName); !found {
Expand Down Expand Up @@ -207,7 +207,7 @@ func (p *Plugin) spellcheckMessage(message string) (*string, error) {
return nil, err
}

result, err := p.getLLM().ChatCompletionNoStream(prompt, ai.WithmaxTokens(128))
result, err := p.getLLM().ChatCompletionNoStream(prompt, ai.WithMaxTokens(128))
if err != nil {
return nil, err
}
Expand All @@ -225,7 +225,7 @@ func (p *Plugin) changeTone(tone, message string) (*string, error) {
return nil, err
}

result, err := p.getLLM().ChatCompletionNoStream(prompt, ai.WithmaxTokens(128))
result, err := p.getLLM().ChatCompletionNoStream(prompt, ai.WithMaxTokens(128))
if err != nil {
return nil, err
}
Expand All @@ -242,7 +242,7 @@ func (p *Plugin) simplifyText(message string) (*string, error) {
return nil, err
}

result, err := p.getLLM().ChatCompletionNoStream(prompt, ai.WithmaxTokens(128))
result, err := p.getLLM().ChatCompletionNoStream(prompt, ai.WithMaxTokens(128))
if err != nil {
return nil, err
}
Expand All @@ -260,7 +260,7 @@ func (p *Plugin) aiChangeText(ask, message string) (*string, error) {
return nil, err
}

result, err := p.getLLM().ChatCompletionNoStream(prompt, ai.WithmaxTokens(128))
result, err := p.getLLM().ChatCompletionNoStream(prompt, ai.WithMaxTokens(128))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 3eaed9f

Please sign in to comment.