From 3e50b4067e9f6db536a1a13c2a5c8ddcd6612819 Mon Sep 17 00:00:00 2001 From: Donnie Adams Date: Tue, 11 Feb 2025 07:22:39 -0500 Subject: [PATCH] chore: fix formatting There were a bunch of small formatting oddities. I switched to a new AI agent that found and fixed them. I reviewed by hand and all look good to me. Signed-off-by: Donnie Adams --- apiclient/agent.go | 6 ++-- apiclient/emailreceiver.go | 4 +-- apiclient/run.go | 4 +-- apiclient/thread.go | 4 +-- apiclient/toolref.go | 2 +- apiclient/types/errors.go | 6 ++-- apiclient/types/time.go | 4 +-- apiclient/webhook.go | 4 +-- apiclient/workflow.go | 6 ++-- pkg/api/handlers/agent.go | 16 +++++------ pkg/api/handlers/assistants.go | 4 +-- pkg/api/handlers/files.go | 2 +- pkg/api/handlers/model.go | 2 +- pkg/api/handlers/modelprovider.go | 4 +-- pkg/api/handlers/sendgrid/webhook.go | 6 ++-- pkg/api/handlers/tasks.go | 10 +++---- pkg/api/handlers/threads.go | 6 ++-- pkg/api/handlers/webhooks.go | 2 +- pkg/api/handlers/workflows.go | 2 +- pkg/api/request.go | 2 +- pkg/gateway/server/llmproxy.go | 2 +- pkg/gateway/server/oauth.go | 16 +++++------ pkg/gateway/server/oauth_apps.go | 4 +-- pkg/gateway/server/token.go | 41 +++++++++++----------------- pkg/gateway/server/user.go | 18 ++++++------ 25 files changed, 84 insertions(+), 93 deletions(-) diff --git a/apiclient/agent.go b/apiclient/agent.go index 85a8e1219..16f778b68 100644 --- a/apiclient/agent.go +++ b/apiclient/agent.go @@ -20,7 +20,7 @@ func (c *Client) UpdateAgent(ctx context.Context, id string, manifest types.Agen } func (c *Client) GetAgent(ctx context.Context, id string) (*types.Agent, error) { - _, resp, err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/agents/"+id), nil) + _, resp, err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/agents/%s", id), nil) if err != nil { return nil, err } @@ -29,7 +29,7 @@ func (c *Client) GetAgent(ctx context.Context, id string) (*types.Agent, error) } func (c *Client) CreateAgent(ctx context.Context, agent types.AgentManifest) (*types.Agent, error) { - _, resp, err := c.postJSON(ctx, fmt.Sprintf("/agents"), agent) + _, resp, err := c.postJSON(ctx, "/agents", agent) if err != nil { return nil, err } @@ -74,7 +74,7 @@ func (c *Client) ListAgents(ctx context.Context, opts ListAgentsOptions) (result } func (c *Client) DeleteAgent(ctx context.Context, id string) error { - _, resp, err := c.doRequest(ctx, http.MethodDelete, fmt.Sprintf("/agents/"+id), nil) + _, resp, err := c.doRequest(ctx, http.MethodDelete, fmt.Sprintf("/agents/%s", id), nil) if err != nil { return err } diff --git a/apiclient/emailreceiver.go b/apiclient/emailreceiver.go index 7f4342fa0..8493d747c 100644 --- a/apiclient/emailreceiver.go +++ b/apiclient/emailreceiver.go @@ -10,7 +10,7 @@ import ( ) func (c *Client) GetEmailReceiver(ctx context.Context, id string) (*types.EmailReceiver, error) { - _, resp, err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/email-receivers/"+id), nil) + _, resp, err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/email-receivers/%s", id), nil) if err != nil { return nil, err } @@ -41,7 +41,7 @@ func (c *Client) ListEmailReceivers(ctx context.Context) (result types.EmailRece } func (c *Client) DeleteEmailReceiver(ctx context.Context, id string) error { - _, resp, err := c.doRequest(ctx, http.MethodDelete, fmt.Sprintf("/email-receivers/"+id), nil) + _, resp, err := c.doRequest(ctx, http.MethodDelete, fmt.Sprintf("/email-receivers/%s", id), nil) if err != nil { return err } diff --git a/apiclient/run.go b/apiclient/run.go index 9d68570b8..e21555cd0 100644 --- a/apiclient/run.go +++ b/apiclient/run.go @@ -44,7 +44,7 @@ func (c *Client) StreamRuns(ctx context.Context, opts ListRunsOptions) (result < } func (c *Client) GetRun(ctx context.Context, id string) (result *types.Run, err error) { - _, resp, err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/runs/"+id), nil) + _, resp, err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/runs/%s", id), nil) if err != nil { return } @@ -54,7 +54,7 @@ func (c *Client) GetRun(ctx context.Context, id string) (result *types.Run, err } func (c *Client) DeleteRun(ctx context.Context, id string) error { - _, resp, err := c.doRequest(ctx, http.MethodDelete, fmt.Sprintf("/runs/"+id), nil) + _, resp, err := c.doRequest(ctx, http.MethodDelete, fmt.Sprintf("/runs/%s", id), nil) if err != nil { return err } diff --git a/apiclient/thread.go b/apiclient/thread.go index 2e3a45952..5031dc02e 100644 --- a/apiclient/thread.go +++ b/apiclient/thread.go @@ -10,7 +10,7 @@ import ( ) func (c *Client) DeleteThread(ctx context.Context, id string) error { - _, resp, err := c.doRequest(ctx, http.MethodDelete, fmt.Sprintf("/threads/"+id), nil) + _, resp, err := c.doRequest(ctx, http.MethodDelete, fmt.Sprintf("/threads/%s", id), nil) if err != nil { return err } @@ -53,7 +53,7 @@ func (c *Client) UpdateThread(ctx context.Context, id string, thread types.Threa } func (c *Client) GetThread(ctx context.Context, threadID string) (result *types.Thread, err error) { - _, resp, err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/threads/"+threadID), nil) + _, resp, err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/threads/%s", threadID), nil) if err != nil { return } diff --git a/apiclient/toolref.go b/apiclient/toolref.go index 28e473697..71de91904 100644 --- a/apiclient/toolref.go +++ b/apiclient/toolref.go @@ -62,7 +62,7 @@ func (c *Client) DeleteToolReference(ctx context.Context, id string, toolType ty } func (c *Client) CreateToolReference(ctx context.Context, manifest types.ToolReferenceManifest) (*types.ToolReference, error) { - _, resp, err := c.postJSON(ctx, fmt.Sprintf("/tool-references"), &manifest) + _, resp, err := c.postJSON(ctx, "/tool-references", &manifest) if err != nil { return nil, err } diff --git a/apiclient/types/errors.go b/apiclient/types/errors.go index 5087af4c0..3dde450b9 100644 --- a/apiclient/types/errors.go +++ b/apiclient/types/errors.go @@ -15,7 +15,7 @@ func (e *ErrHTTP) Error() string { return fmt.Sprintf("error code %d (%s): %s", e.Code, http.StatusText(e.Code), e.Message) } -func NewErrHttp(code int, message string) *ErrHTTP { +func NewErrHTTP(code int, message string) *ErrHTTP { return &ErrHTTP{ Code: code, Message: message, @@ -23,7 +23,7 @@ func NewErrHttp(code int, message string) *ErrHTTP { } func NewErrBadRequest(message string, args ...interface{}) *ErrHTTP { - return NewErrHttp(http.StatusBadRequest, fmt.Sprintf(message, args...)) + return NewErrHTTP(http.StatusBadRequest, fmt.Sprintf(message, args...)) } func NewErrNotFound(message string, args ...any) *ErrHTTP { @@ -33,7 +33,7 @@ func NewErrNotFound(message string, args ...any) *ErrHTTP { if len(args) > 0 { message = fmt.Sprintf(message, args...) } - return NewErrHttp(http.StatusNotFound, message) + return NewErrHTTP(http.StatusNotFound, message) } func IsNotFound(err error) bool { diff --git a/apiclient/types/time.go b/apiclient/types/time.go index ad7a96b98..39e9ba69d 100644 --- a/apiclient/types/time.go +++ b/apiclient/types/time.go @@ -59,10 +59,10 @@ func (t Time) ToUnstructured() interface{} { return string(buf) } -func (_ Time) OpenAPISchemaType() []string { +func (Time) OpenAPISchemaType() []string { return []string{"string"} } -func (_ Time) OpenAPISchemaFormat() string { +func (Time) OpenAPISchemaFormat() string { return "date-time" } diff --git a/apiclient/webhook.go b/apiclient/webhook.go index ffc3f662a..68ec4d4b4 100644 --- a/apiclient/webhook.go +++ b/apiclient/webhook.go @@ -10,7 +10,7 @@ import ( ) func (c *Client) GetWebhook(ctx context.Context, id string) (result *types.Webhook, _ error) { - _, resp, err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/webhooks/"+id), nil) + _, resp, err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/webhooks/%s", id), nil) if err != nil { return nil, err } @@ -41,7 +41,7 @@ func (c *Client) ListWebhooks(ctx context.Context) (result types.WebhookList, _ } func (c *Client) DeleteWebhook(ctx context.Context, id string) error { - _, resp, err := c.doRequest(ctx, http.MethodDelete, fmt.Sprintf("/webhooks/"+id), nil) + _, resp, err := c.doRequest(ctx, http.MethodDelete, fmt.Sprintf("/webhooks/%s", id), nil) if err != nil { return err } diff --git a/apiclient/workflow.go b/apiclient/workflow.go index 3e6a9b205..997911bf0 100644 --- a/apiclient/workflow.go +++ b/apiclient/workflow.go @@ -20,7 +20,7 @@ func (c *Client) UpdateWorkflow(ctx context.Context, id string, manifest types.W } func (c *Client) GetWorkflow(ctx context.Context, id string) (*types.Workflow, error) { - _, resp, err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/workflows/"+id), nil) + _, resp, err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/workflows/%s", id), nil) if err != nil { return nil, err } @@ -29,7 +29,7 @@ func (c *Client) GetWorkflow(ctx context.Context, id string) (*types.Workflow, e } func (c *Client) CreateWorkflow(ctx context.Context, workflow types.WorkflowManifest) (*types.Workflow, error) { - _, resp, err := c.postJSON(ctx, fmt.Sprintf("/workflows"), workflow) + _, resp, err := c.postJSON(ctx, "/workflows", workflow) if err != nil { return nil, err } @@ -106,7 +106,7 @@ func (c *Client) ListWorkflows(ctx context.Context, opts ListWorkflowsOptions) ( } func (c *Client) DeleteWorkflow(ctx context.Context, id string) error { - _, resp, err := c.doRequest(ctx, http.MethodDelete, fmt.Sprintf("/workflows/"+id), nil) + _, resp, err := c.doRequest(ctx, http.MethodDelete, fmt.Sprintf("/workflows/%s", id), nil) if err != nil { return err } diff --git a/pkg/api/handlers/agent.go b/pkg/api/handlers/agent.go index d18b428e0..2e4d81c0c 100644 --- a/pkg/api/handlers/agent.go +++ b/pkg/api/handlers/agent.go @@ -448,7 +448,7 @@ func (a *AgentHandler) UploadKnowledgeFile(req api.Context) error { } if len(knowledgeSetNames) == 0 { - return types.NewErrHttp(http.StatusTooEarly, fmt.Sprintf("agent %q knowledge set is not created yet", agentName)) + return types.NewErrHTTP(http.StatusTooEarly, fmt.Sprintf("agent %q knowledge set is not created yet", agentName)) } ws, err := getWorkspaceFromKnowledgeSet(req, knowledgeSetNames[0]) @@ -493,7 +493,7 @@ func (a *AgentHandler) DeleteKnowledgeFile(req api.Context) error { } if len(knowledgeSetNames) == 0 { - return types.NewErrHttp(http.StatusTooEarly, fmt.Sprintf("agent %q knowledge set is not created yet", agentName)) + return types.NewErrHTTP(http.StatusTooEarly, fmt.Sprintf("agent %q knowledge set is not created yet", agentName)) } return deleteKnowledge(req, req.PathValue("file"), knowledgeSetNames[0]) } @@ -552,7 +552,7 @@ func (a *AgentHandler) UpdateKnowledgeSource(req api.Context) error { } if len(knowledgeSetNames) == 0 { - return types.NewErrHttp(http.StatusTooEarly, fmt.Sprintf("agent %q knowledge set is not created yet", agentName)) + return types.NewErrHTTP(http.StatusTooEarly, fmt.Sprintf("agent %q knowledge set is not created yet", agentName)) } var knowledgeSource v1.KnowledgeSource @@ -583,7 +583,7 @@ func (a *AgentHandler) ReIngestKnowledgeFile(req api.Context) error { } if len(knowledgeSetNames) == 0 { - return types.NewErrHttp(http.StatusTooEarly, fmt.Sprintf("agent %q knowledge set is not created yet", agentName)) + return types.NewErrHTTP(http.StatusTooEarly, fmt.Sprintf("agent %q knowledge set is not created yet", agentName)) } var knowledgeFile v1.KnowledgeFile @@ -627,7 +627,7 @@ func (a *AgentHandler) ReSyncKnowledgeSource(req api.Context) error { } if len(knowledgeSetNames) == 0 { - return types.NewErrHttp(http.StatusTooEarly, fmt.Sprintf("agent %q knowledge set is not created yet", agentName)) + return types.NewErrHTTP(http.StatusTooEarly, fmt.Sprintf("agent %q knowledge set is not created yet", agentName)) } var knowledgeSource v1.KnowledgeSource @@ -685,7 +685,7 @@ func (a *AgentHandler) DeleteKnowledgeSource(req api.Context) error { } if len(knowledgeSetNames) == 0 { - return types.NewErrHttp(http.StatusTooEarly, fmt.Sprintf("agent %q knowledge set is not created yet", agentName)) + return types.NewErrHTTP(http.StatusTooEarly, fmt.Sprintf("agent %q knowledge set is not created yet", agentName)) } var knowledgeSource v1.KnowledgeSource @@ -740,7 +740,7 @@ func (a *AgentHandler) EnsureCredentialForKnowledgeSource(req api.Context) error return fmt.Errorf("failed to get tool reference %v", ref) } if toolReference.Status.Tool == nil { - return types.NewErrHttp(http.StatusTooEarly, "tool reference is not ready yet") + return types.NewErrHTTP(http.StatusTooEarly, "tool reference is not ready yet") } if len(toolReference.Status.Tool.Credentials) == 0 { @@ -940,7 +940,7 @@ func runAuthForAgent(ctx context.Context, c kclient.WithWatch, invoker *invoke.I credentials = append(credentials, credentails...) } else if err := c.Get(ctx, kclient.ObjectKey{Namespace: agent.Namespace, Name: tool}, &toolRef); err == nil { if toolRef.Status.Tool == nil { - return nil, types.NewErrHttp(http.StatusTooEarly, fmt.Sprintf("tool %q is not ready", tool)) + return nil, types.NewErrHTTP(http.StatusTooEarly, fmt.Sprintf("tool %q is not ready", tool)) } credentials = append(credentials, toolRef.Status.Tool.Credentials...) diff --git a/pkg/api/handlers/assistants.go b/pkg/api/handlers/assistants.go index 3d49d7f68..c5d80b93b 100644 --- a/pkg/api/handlers/assistants.go +++ b/pkg/api/handlers/assistants.go @@ -406,7 +406,7 @@ func (a *AssistantHandler) UploadKnowledge(req api.Context) error { } if len(thread.Status.KnowledgeSetNames) == 0 { - return types.NewErrHttp(http.StatusTooEarly, "knowledge set is not available yet") + return types.NewErrHTTP(http.StatusTooEarly, "knowledge set is not available yet") } ws, err := getWorkspaceFromKnowledgeSet(req, thread.Status.KnowledgeSetNames[0]) @@ -424,7 +424,7 @@ func (a *AssistantHandler) DeleteKnowledge(req api.Context) error { } if len(thread.Status.KnowledgeSetNames) == 0 { - return types.NewErrHttp(http.StatusTooEarly, "knowledge set is not created yet") + return types.NewErrHTTP(http.StatusTooEarly, "knowledge set is not created yet") } return deleteKnowledge(req, req.PathValue("file"), thread.Status.KnowledgeSetNames[0]) diff --git a/pkg/api/handlers/files.go b/pkg/api/handlers/files.go index d1eea64b3..4c35066e8 100644 --- a/pkg/api/handlers/files.go +++ b/pkg/api/handlers/files.go @@ -32,7 +32,7 @@ func listFiles(ctx context.Context, req api.Context, gClient *gptscript.GPTScrip func listFileFromWorkspace(ctx context.Context, req api.Context, gClient *gptscript.GPTScript, opts gptscript.ListFilesInWorkspaceOptions) error { if opts.WorkspaceID == "" { - return types.NewErrHttp(http.StatusTooEarly, "workspace is not available yet") + return types.NewErrHTTP(http.StatusTooEarly, "workspace is not available yet") } files, err := gClient.ListFilesInWorkspace(ctx, opts) if err != nil { diff --git a/pkg/api/handlers/model.go b/pkg/api/handlers/model.go index 1c18e3f15..5b9aeb57e 100644 --- a/pkg/api/handlers/model.go +++ b/pkg/api/handlers/model.go @@ -171,7 +171,7 @@ func (a *ModelHandler) Delete(req api.Context) error { } if len(agents.Items) > 0 { - return types.NewErrHttp(http.StatusPreconditionFailed, fmt.Sprintf("model %q is used by %d agents", model, len(agents.Items))) + return types.NewErrHTTP(http.StatusPreconditionFailed, fmt.Sprintf("model %q is used by %d agents", model, len(agents.Items))) } return req.Delete(&v1.Model{ diff --git a/pkg/api/handlers/modelprovider.go b/pkg/api/handlers/modelprovider.go index 894381917..365ecaa47 100644 --- a/pkg/api/handlers/modelprovider.go +++ b/pkg/api/handlers/modelprovider.go @@ -180,12 +180,12 @@ func (mp *ModelProviderHandler) Validate(req api.Context) error { ref.Name, ) } - return types.NewErrHttp(http.StatusUnprocessableEntity, strings.Trim(err.Error(), "\"'")) + return types.NewErrHTTP(http.StatusUnprocessableEntity, strings.Trim(err.Error(), "\"'")) } var validationError ValidationError if json.Unmarshal([]byte(res.Output), &validationError) == nil && validationError.Err != "" { - return types.NewErrHttp(http.StatusUnprocessableEntity, validationError.Error()) + return types.NewErrHTTP(http.StatusUnprocessableEntity, validationError.Error()) } return nil diff --git a/pkg/api/handlers/sendgrid/webhook.go b/pkg/api/handlers/sendgrid/webhook.go index 982692213..3e816e944 100644 --- a/pkg/api/handlers/sendgrid/webhook.go +++ b/pkg/api/handlers/sendgrid/webhook.go @@ -26,18 +26,18 @@ func (h *InboundWebhookHandler) InboundWebhookHandler(req api.Context) error { if h.username != "" && h.password != "" { username, password, ok := req.Request.BasicAuth() if !ok || username != h.username || password != h.password { - return types.NewErrHttp(http.StatusUnauthorized, "Invalid credentials") + return types.NewErrHTTP(http.StatusUnauthorized, "Invalid credentials") } } inboundEmail, err := inbound.Parse(req.Request) if err != nil { - return types.NewErrHttp(http.StatusBadRequest, fmt.Sprintf("Failed to parse inbound email: %v", err)) + return types.NewErrHTTP(http.StatusBadRequest, fmt.Sprintf("Failed to parse inbound email: %v", err)) } subject := inboundEmail.Headers["Subject"] if err := h.emailTrigger.Handler(req.Context(), inboundEmail.Envelope.From, inboundEmail.Envelope.To, subject, []byte(inboundEmail.TextBody)); err != nil { - return types.NewErrHttp(http.StatusInternalServerError, fmt.Sprintf("Failed to handle inbound email: %v", err)) + return types.NewErrHTTP(http.StatusInternalServerError, fmt.Sprintf("Failed to handle inbound email: %v", err)) } req.WriteHeader(http.StatusOK) diff --git a/pkg/api/handlers/tasks.go b/pkg/api/handlers/tasks.go index 149c17a99..c82527d15 100644 --- a/pkg/api/handlers/tasks.go +++ b/pkg/api/handlers/tasks.go @@ -60,7 +60,7 @@ func (t *TaskHandler) Abort(req api.Context) error { } if wfe.Spec.ThreadName != userThread.Name && workflow.Name != wfe.Spec.WorkflowName { - return types.NewErrHttp(http.StatusForbidden, "task run does not belong to the thread") + return types.NewErrHTTP(http.StatusForbidden, "task run does not belong to the thread") } var thread v1.Thread @@ -99,7 +99,7 @@ func (t *TaskHandler) Events(req api.Context) error { } if wfe.Spec.ThreadName != thread.Name && workflow.Name != wfe.Spec.WorkflowName { - return types.NewErrHttp(http.StatusForbidden, "task run does not belong to the user") + return types.NewErrHTTP(http.StatusForbidden, "task run does not belong to the user") } _, events, err := t.events.Watch(req.Context(), req.Namespace(), events.WatchOptions{ @@ -187,7 +187,7 @@ func (t *TaskHandler) DeleteRun(req api.Context) error { } if wfe.Spec.ThreadName != userThread.Name || wfe.Spec.WorkflowName != workflow.Name { - return types.NewErrHttp(http.StatusForbidden, "task run does not belong to the user") + return types.NewErrHTTP(http.StatusForbidden, "task run does not belong to the user") } return req.Delete(&wfe) @@ -681,7 +681,7 @@ func (t *TaskHandler) getTask(req api.Context) (*v1.Workflow, *v1.Thread, error) } if workflow.Spec.ThreadName != thread.Name { - return nil, nil, types.NewErrHttp(http.StatusForbidden, "task does not belong to the thread") + return nil, nil, types.NewErrHTTP(http.StatusForbidden, "task does not belong to the thread") } return &workflow, thread, nil @@ -707,7 +707,7 @@ func getThreadForScope(req api.Context) (*v1.Thread, error) { return nil, err } if wfe.Spec.ThreadName != thread.Name { - return nil, types.NewErrHttp(http.StatusForbidden, "task run does not belong to the thread") + return nil, types.NewErrHTTP(http.StatusForbidden, "task run does not belong to the thread") } if wfe.Spec.WorkflowName != taskID { return nil, types.NewErrNotFound("task run not found") diff --git a/pkg/api/handlers/threads.go b/pkg/api/handlers/threads.go index d282196b6..fdf7b25f1 100644 --- a/pkg/api/handlers/threads.go +++ b/pkg/api/handlers/threads.go @@ -316,7 +316,7 @@ func (a *ThreadHandler) UploadFile(req api.Context) error { } if thread.Status.WorkspaceID == "" { - return types.NewErrHttp(http.StatusTooEarly, fmt.Sprintf("no workspace found for thread %s", req.PathValue("id"))) + return types.NewErrHTTP(http.StatusTooEarly, fmt.Sprintf("no workspace found for thread %s", req.PathValue("id"))) } _, err := uploadFileToWorkspace(req.Context(), req, a.gptscript, thread.Status.WorkspaceID, "files/") @@ -368,7 +368,7 @@ func (a *ThreadHandler) UploadKnowledge(req api.Context) error { } if len(thread.Status.KnowledgeSetNames) == 0 { - return types.NewErrHttp(http.StatusTooEarly, "knowledge set is not available yet") + return types.NewErrHTTP(http.StatusTooEarly, "knowledge set is not available yet") } ws, err := getWorkspaceFromKnowledgeSet(req, thread.Status.KnowledgeSetNames[0]) @@ -390,7 +390,7 @@ func (a *ThreadHandler) DeleteKnowledge(req api.Context) error { } if len(thread.Status.KnowledgeSetNames) == 0 { - return types.NewErrHttp(http.StatusTooEarly, fmt.Sprintf("thread %q knowledge set is not created yet", thread.Name)) + return types.NewErrHTTP(http.StatusTooEarly, fmt.Sprintf("thread %q knowledge set is not created yet", thread.Name)) } return deleteKnowledge(req, req.PathValue("file"), thread.Status.KnowledgeSetNames[0]) diff --git a/pkg/api/handlers/webhooks.go b/pkg/api/handlers/webhooks.go index 42ed88624..42e541eaf 100644 --- a/pkg/api/handlers/webhooks.go +++ b/pkg/api/handlers/webhooks.go @@ -195,7 +195,7 @@ func (a *WebhookHandler) RemoveToken(req api.Context) error { // There is a chance that an unauthorized user could sneak through our authorization because of the pattern matching we are using. // Check that the user is an admin here. if !req.UserIsAdmin() { - return types.NewErrHttp(http.StatusForbidden, "unauthorized") + return types.NewErrHTTP(http.StatusForbidden, "unauthorized") } var wh v1.Webhook diff --git a/pkg/api/handlers/workflows.go b/pkg/api/handlers/workflows.go index aa993caaf..f2ff10662 100644 --- a/pkg/api/handlers/workflows.go +++ b/pkg/api/handlers/workflows.go @@ -375,7 +375,7 @@ func (a *WorkflowHandler) EnsureCredentialForKnowledgeSource(req api.Context) er return fmt.Errorf("failed to get tool reference %v", ref) } if toolReference.Status.Tool == nil { - return types.NewErrHttp(http.StatusTooEarly, "tool reference is not ready yet") + return types.NewErrHTTP(http.StatusTooEarly, "tool reference is not ready yet") } if len(toolReference.Status.Tool.Credentials) == 0 { diff --git a/pkg/api/request.go b/pkg/api/request.go index 5a403aa8a..7fac2804d 100644 --- a/pkg/api/request.go +++ b/pkg/api/request.go @@ -112,7 +112,7 @@ type BodyOptions struct { func (r *Context) Body(opts ...BodyOptions) (_ []byte, err error) { defer func() { if maxErr := (*http.MaxBytesError)(nil); errors.As(err, &maxErr) { - err = types.NewErrHttp(http.StatusRequestEntityTooLarge, "request body too large") + err = types.NewErrHTTP(http.StatusRequestEntityTooLarge, "request body too large") } _, _ = io.Copy(io.Discard, r.Request.Body) }() diff --git a/pkg/gateway/server/llmproxy.go b/pkg/gateway/server/llmproxy.go index 3a8c04f72..e34f2d00b 100644 --- a/pkg/gateway/server/llmproxy.go +++ b/pkg/gateway/server/llmproxy.go @@ -15,7 +15,7 @@ import ( func (s *Server) llmProxy(req api.Context) error { token, err := s.tokenService.DecodeToken(strings.TrimPrefix(req.Request.Header.Get("Authorization"), "Bearer ")) if err != nil { - return types2.NewErrHttp(http.StatusUnauthorized, fmt.Sprintf("invalid token: %v", err)) + return types2.NewErrHTTP(http.StatusUnauthorized, fmt.Sprintf("invalid token: %v", err)) } if err = s.db.WithContext(req.Context()).Create(&types.LLMProxyActivity{ diff --git a/pkg/gateway/server/oauth.go b/pkg/gateway/server/oauth.go index 86f4260bd..dd128275a 100644 --- a/pkg/gateway/server/oauth.go +++ b/pkg/gateway/server/oauth.go @@ -21,17 +21,17 @@ const expirationDur = 7 * 24 * time.Hour func (s *Server) oauth(apiContext api.Context) error { namespace := apiContext.PathValue("namespace") if namespace == "" { - return types2.NewErrHttp(http.StatusBadRequest, "no namespace path parameter provided") + return types2.NewErrHTTP(http.StatusBadRequest, "no namespace path parameter provided") } name := apiContext.PathValue("name") if name == "" { - return types2.NewErrHttp(http.StatusBadRequest, "no name path parameter provided") + return types2.NewErrHTTP(http.StatusBadRequest, "no name path parameter provided") } // Check to make sure this auth provider exists. if providerList := s.dispatcher.ListConfiguredAuthProviders(namespace); !slices.Contains(providerList, name) { - return types2.NewErrHttp(http.StatusNotFound, "auth provider not found") + return types2.NewErrHTTP(http.StatusNotFound, "auth provider not found") } state, err := s.createState(apiContext.Context(), apiContext.PathValue("id")) @@ -59,28 +59,28 @@ func (s *Server) oauth(apiContext api.Context) error { func (s *Server) redirect(apiContext api.Context) error { namespace := apiContext.PathValue("namespace") if namespace == "" { - return types2.NewErrHttp(http.StatusBadRequest, "no namespace path parameter provided") + return types2.NewErrHTTP(http.StatusBadRequest, "no namespace path parameter provided") } name := apiContext.PathValue("name") if name == "" { - return types2.NewErrHttp(http.StatusBadRequest, "no name path parameter provided") + return types2.NewErrHTTP(http.StatusBadRequest, "no name path parameter provided") } // Check to make sure this auth provider exists. if providerList := s.dispatcher.ListConfiguredAuthProviders(namespace); !slices.Contains(providerList, name) { - return types2.NewErrHttp(http.StatusNotFound, "auth provider not found") + return types2.NewErrHTTP(http.StatusNotFound, "auth provider not found") } tr, err := s.verifyState(apiContext.Context(), apiContext.FormValue("state")) if err != nil { - return types2.NewErrHttp(http.StatusBadRequest, fmt.Sprintf("invalid state: %v", err)) + return types2.NewErrHTTP(http.StatusBadRequest, fmt.Sprintf("invalid state: %v", err)) } randBytes := make([]byte, tokenIDLength+randomTokenLength) if _, err := rand.Read(randBytes); err != nil { - return types2.NewErrHttp(http.StatusInternalServerError, fmt.Sprintf("could not generate token id: %v", err)) + return types2.NewErrHTTP(http.StatusInternalServerError, fmt.Sprintf("could not generate token id: %v", err)) } id := randBytes[:tokenIDLength] diff --git a/pkg/gateway/server/oauth_apps.go b/pkg/gateway/server/oauth_apps.go index c8a6ecfdb..aa75e335e 100644 --- a/pkg/gateway/server/oauth_apps.go +++ b/pkg/gateway/server/oauth_apps.go @@ -122,7 +122,7 @@ func (s *Server) createOAuthApp(apiContext api.Context) error { } if len(existingApps.Items) > 0 { - return types2.NewErrHttp(http.StatusConflict, fmt.Sprintf("OAuth app with alias %s already exists", appManifest.Alias)) + return types2.NewErrHTTP(http.StatusConflict, fmt.Sprintf("OAuth app with alias %s already exists", appManifest.Alias)) } app := v1.OAuthApp{ @@ -546,7 +546,7 @@ func (s *Server) getTokenOAuthApp(apiContext api.Context) error { if hash != challenge.Challenge { // This is an invalid request, possibly an unauthorized attempt to obtain a token. // Return a 404 to mask that this matched a real challenge. - return types2.NewErrHttp(http.StatusNotFound, "challenge not found") + return types2.NewErrHTTP(http.StatusNotFound, "challenge not found") } // Look up the token response by the state. diff --git a/pkg/gateway/server/token.go b/pkg/gateway/server/token.go index 5a1268821..b03599101 100644 --- a/pkg/gateway/server/token.go +++ b/pkg/gateway/server/token.go @@ -42,7 +42,7 @@ type refreshTokenResponse struct { func (s *Server) getTokens(apiContext api.Context) error { var tokens []types.AuthToken if err := s.db.WithContext(apiContext.Context()).Where("user_id = ?", apiContext.UserID()).Find(&tokens).Error; err != nil { - return types2.NewErrHttp(http.StatusInternalServerError, fmt.Sprintf("error getting tokens: %v", err)) + return types2.NewErrHTTP(http.StatusInternalServerError, fmt.Sprintf("error getting tokens: %v", err)) } return apiContext.Write(tokens) @@ -51,7 +51,7 @@ func (s *Server) getTokens(apiContext api.Context) error { func (s *Server) deleteToken(apiContext api.Context) error { id := apiContext.PathValue("id") if id == "" { - return types2.NewErrHttp(http.StatusBadRequest, "id path parameter is required") + return types2.NewErrHTTP(http.StatusBadRequest, "id path parameter is required") } if err := s.db.WithContext(apiContext.Context()).Where("user_id = ? AND id = ?", apiContext.UserID(), id).Delete(new(types.AuthToken)).Error; err != nil { @@ -60,7 +60,7 @@ func (s *Server) deleteToken(apiContext api.Context) error { status = http.StatusNotFound err = fmt.Errorf("not found") } - return types2.NewErrHttp(status, fmt.Sprintf("error deleting token: %v", err)) + return types2.NewErrHTTP(status, fmt.Sprintf("error deleting token: %v", err)) } return apiContext.Write(map[string]any{"deleted": true}) @@ -74,7 +74,7 @@ func (s *Server) newToken(apiContext api.Context) error { namespace, name := apiContext.AuthProviderNameAndNamespace() userID := apiContext.UserID() if namespace == "" || name == "" || userID <= 0 { - return types2.NewErrHttp(http.StatusForbidden, "forbidden") + return types2.NewErrHTTP(http.StatusForbidden, "forbidden") } var customExpiration time.Duration @@ -82,54 +82,45 @@ func (s *Server) newToken(apiContext api.Context) error { request := new(createTokenRequest) err := apiContext.Read(request) if err != nil { - return types2.NewErrHttp(http.StatusBadRequest, fmt.Sprintf("invalid create create token request body: %v", err)) + return types2.NewErrHTTP(http.StatusBadRequest, fmt.Sprintf("invalid create create token request body: %v", err)) } if request.ExpiresIn != "" { customExpiration, err = ktime.ParseDuration(request.ExpiresIn) if err != nil { - return types2.NewErrHttp(http.StatusBadRequest, fmt.Sprintf("invalid expiresIn duration: %v", err)) + return types2.NewErrHTTP(http.StatusBadRequest, fmt.Sprintf("invalid expiresIn duration: %v", err)) } } } randBytes := make([]byte, randomTokenLength+tokenIDLength) if _, err := rand.Read(randBytes); err != nil { - return types2.NewErrHttp(http.StatusInternalServerError, fmt.Sprintf("error refreshing token: %v", err)) + return types2.NewErrHTTP(http.StatusInternalServerError, fmt.Sprintf("error refreshing token: %v", err)) } id := randBytes[:tokenIDLength] token := randBytes[tokenIDLength:] - tkn := &types.AuthToken{ - ID: fmt.Sprintf("%x", id), - UserID: userID, - HashedToken: hashToken(fmt.Sprintf("%x", token)), - ExpiresAt: time.Now().Add(customExpiration), - AuthProviderNamespace: namespace, - AuthProviderName: name, - } - // Make sure the auth provider exists. if providerList := s.dispatcher.ListConfiguredAuthProviders(namespace); !slices.Contains(providerList, name) { - return types2.NewErrHttp(http.StatusNotFound, "auth provider not found") + return types2.NewErrHTTP(http.StatusNotFound, "auth provider not found") } return apiContext.Write(refreshTokenResponse{ Token: publicToken(id, token), - ExpiresAt: tkn.ExpiresAt, + ExpiresAt: time.Now().Add(customExpiration), }) } func (s *Server) tokenRequest(apiContext api.Context) error { reqObj := new(tokenRequestRequest) if err := json.NewDecoder(apiContext.Request.Body).Decode(reqObj); err != nil { - return types2.NewErrHttp(http.StatusBadRequest, fmt.Sprintf("invalid token request body: %v", err)) + return types2.NewErrHTTP(http.StatusBadRequest, fmt.Sprintf("invalid token request body: %v", err)) } if reqObj.ProviderName != "" { if providerList := s.dispatcher.ListConfiguredAuthProviders(reqObj.ProviderNamespace); !slices.Contains(providerList, reqObj.ProviderName) { - return types2.NewErrHttp(http.StatusBadRequest, fmt.Sprintf("auth provider %q not found", reqObj.ProviderName)) + return types2.NewErrHTTP(http.StatusBadRequest, fmt.Sprintf("auth provider %q not found", reqObj.ProviderName)) } } @@ -140,9 +131,9 @@ func (s *Server) tokenRequest(apiContext api.Context) error { if err := s.db.WithContext(apiContext.Context()).Create(tokenReq).Error; err != nil { if errors.Is(err, gorm.ErrDuplicatedKey) { - return types2.NewErrHttp(http.StatusConflict, "token request already exists") + return types2.NewErrHTTP(http.StatusConflict, "token request already exists") } - return types2.NewErrHttp(http.StatusInternalServerError, err.Error()) + return types2.NewErrHTTP(http.StatusInternalServerError, err.Error()) } if reqObj.ProviderName != "" { @@ -158,7 +149,7 @@ func (s *Server) redirectForTokenRequest(apiContext api.Context) error { if namespace != "" && name != "" { if providerList := s.dispatcher.ListConfiguredAuthProviders(namespace); !slices.Contains(providerList, name) { - return types2.NewErrHttp(http.StatusBadRequest, fmt.Sprintf("auth provider %q not found", name)) + return types2.NewErrHTTP(http.StatusBadRequest, fmt.Sprintf("auth provider %q not found", name)) } } @@ -167,7 +158,7 @@ func (s *Server) redirectForTokenRequest(apiContext api.Context) error { if errors.Is(err, gorm.ErrRecordNotFound) { return types2.NewErrNotFound("token not found") } - return types2.NewErrHttp(http.StatusInternalServerError, err.Error()) + return types2.NewErrHTTP(http.StatusInternalServerError, err.Error()) } return apiContext.Write(map[string]any{"token-path": fmt.Sprintf("%s/api/oauth/start/%s/%s/%s", s.baseURL, tokenReq.ID, namespace, name)}) @@ -239,7 +230,7 @@ func (s *Server) errorToken(ctx context.Context, tr *types.TokenRequest, code in } } - return types2.NewErrHttp(code, err.Error()) + return types2.NewErrHTTP(code, err.Error()) } // autoCleanupTokens will delete token requests that have been retrieved and are older than the cleanupTick. diff --git a/pkg/gateway/server/user.go b/pkg/gateway/server/user.go index 3df9ede5c..04be4ae48 100644 --- a/pkg/gateway/server/user.go +++ b/pkg/gateway/server/user.go @@ -20,7 +20,7 @@ func (s *Server) getCurrentUser(apiContext api.Context) error { user, err := s.client.User(apiContext.Context(), apiContext.User.GetName()) if errors.Is(err, gorm.ErrRecordNotFound) { // This shouldn't happen, but, if it does, then the user would be unauthorized because we can't identify them. - return types2.NewErrHttp(http.StatusUnauthorized, "unauthorized") + return types2.NewErrHTTP(http.StatusUnauthorized, "unauthorized") } else if err != nil { return err } @@ -59,7 +59,7 @@ func (s *Server) getUsers(apiContext api.Context) error { func (s *Server) getUser(apiContext api.Context) error { username := apiContext.PathValue("username") if username == "" { - return types2.NewErrHttp(http.StatusBadRequest, "username path parameter is required") + return types2.NewErrHTTP(http.StatusBadRequest, "username path parameter is required") } user, err := s.client.User(apiContext.Context(), username) @@ -79,21 +79,21 @@ func (s *Server) updateUser(apiContext api.Context) error { username := apiContext.PathValue("username") if username == "" { - return types2.NewErrHttp(http.StatusBadRequest, "username path parameter is required") + return types2.NewErrHTTP(http.StatusBadRequest, "username path parameter is required") } if !actingUserIsAdmin && requestingUsername != username { - return types2.NewErrHttp(http.StatusForbidden, "only admins can update other users") + return types2.NewErrHTTP(http.StatusForbidden, "only admins can update other users") } user := new(types.User) if err := apiContext.Read(user); err != nil { - return types2.NewErrHttp(http.StatusBadRequest, "invalid user request body") + return types2.NewErrHTTP(http.StatusBadRequest, "invalid user request body") } if user.Timezone != "" { if _, err := time.LoadLocation(user.Timezone); err != nil { - return types2.NewErrHttp(http.StatusBadRequest, "invalid timezone") + return types2.NewErrHTTP(http.StatusBadRequest, "invalid timezone") } } @@ -109,7 +109,7 @@ func (s *Server) updateUser(apiContext api.Context) error { } else if ae := (*client.AlreadyExistsError)(nil); errors.As(err, &ae) { status = http.StatusConflict } - return types2.NewErrHttp(status, fmt.Sprintf("failed to update user: %v", err)) + return types2.NewErrHTTP(status, fmt.Sprintf("failed to update user: %v", err)) } return apiContext.Write(types.ConvertUser(existingUser, s.client.IsExplicitAdmin(existingUser.Email))) @@ -118,7 +118,7 @@ func (s *Server) updateUser(apiContext api.Context) error { func (s *Server) deleteUser(apiContext api.Context) error { username := apiContext.PathValue("username") if username == "" { - return types2.NewErrHttp(http.StatusBadRequest, "username path parameter is required") + return types2.NewErrHTTP(http.StatusBadRequest, "username path parameter is required") } status := http.StatusInternalServerError @@ -129,7 +129,7 @@ func (s *Server) deleteUser(apiContext api.Context) error { } else if lae := (*client.LastAdminError)(nil); errors.As(err, &lae) { status = http.StatusBadRequest } - return types2.NewErrHttp(status, fmt.Sprintf("failed to delete user: %v", err)) + return types2.NewErrHTTP(status, fmt.Sprintf("failed to delete user: %v", err)) } return apiContext.Write(types.ConvertUser(existingUser, s.client.IsExplicitAdmin(existingUser.Email)))