diff --git a/cli/server.go b/cli/server.go index 580dae369446c..48ec8492f0a55 100644 --- a/cli/server.go +++ b/cli/server.go @@ -61,6 +61,7 @@ import ( "github.com/coder/serpent" "github.com/coder/wgtunnel/tunnelsdk" + "github.com/coder/coder/v2/coderd/ai" "github.com/coder/coder/v2/coderd/entitlements" "github.com/coder/coder/v2/coderd/notifications/reports" "github.com/coder/coder/v2/coderd/runtimeconfig" @@ -610,6 +611,22 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd. ) } + aiProviders, err := ReadAIProvidersFromEnv(os.Environ()) + if err != nil { + return xerrors.Errorf("read ai providers from env: %w", err) + } + vals.AI.Value.Providers = append(vals.AI.Value.Providers, aiProviders...) + for _, provider := range aiProviders { + logger.Debug( + ctx, "loaded ai provider", + slog.F("type", provider.Type), + ) + } + languageModels, err := ai.ModelsFromConfig(ctx, vals.AI.Value.Providers) + if err != nil { + return xerrors.Errorf("create language models: %w", err) + } + realIPConfig, err := httpmw.ParseRealIPConfig(vals.ProxyTrustedHeaders, vals.ProxyTrustedOrigins) if err != nil { return xerrors.Errorf("parse real ip config: %w", err) @@ -640,6 +657,7 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd. CacheDir: cacheDir, GoogleTokenValidator: googleTokenValidator, ExternalAuthConfigs: externalAuthConfigs, + LanguageModels: languageModels, RealIPConfig: realIPConfig, SSHKeygenAlgorithm: sshKeygenAlgorithm, TracerProvider: tracerProvider, @@ -2621,6 +2639,77 @@ func redirectHTTPToHTTPSDeprecation(ctx context.Context, logger slog.Logger, inv } } +func ReadAIProvidersFromEnv(environ []string) ([]codersdk.AIProviderConfig, error) { + // The index numbers must be in-order. + sort.Strings(environ) + + var providers []codersdk.AIProviderConfig + for _, v := range serpent.ParseEnviron(environ, "CODER_AI_PROVIDER_") { + tokens := strings.SplitN(v.Name, "_", 2) + if len(tokens) != 2 { + return nil, xerrors.Errorf("invalid env var: %s", v.Name) + } + + providerNum, err := strconv.Atoi(tokens[0]) + if err != nil { + return nil, xerrors.Errorf("parse number: %s", v.Name) + } + + var provider codersdk.AIProviderConfig + switch { + case len(providers) < providerNum: + return nil, xerrors.Errorf( + "provider num %v skipped: %s", + len(providers), + v.Name, + ) + case len(providers) == providerNum: + // At the next next provider. + providers = append(providers, provider) + case len(providers) == providerNum+1: + // At the current provider. + provider = providers[providerNum] + } + + key := tokens[1] + switch key { + case "TYPE": + provider.Type = v.Value + case "API_KEY": + provider.APIKey = v.Value + case "BASE_URL": + provider.BaseURL = v.Value + case "MODELS": + provider.Models = strings.Split(v.Value, ",") + } + providers[providerNum] = provider + } + for _, envVar := range environ { + tokens := strings.SplitN(envVar, "=", 2) + if len(tokens) != 2 { + continue + } + switch tokens[0] { + case "OPENAI_API_KEY": + providers = append(providers, codersdk.AIProviderConfig{ + Type: "openai", + APIKey: tokens[1], + }) + case "ANTHROPIC_API_KEY": + providers = append(providers, codersdk.AIProviderConfig{ + Type: "anthropic", + APIKey: tokens[1], + }) + case "GOOGLE_API_KEY": + providers = append(providers, codersdk.AIProviderConfig{ + Type: "google", + APIKey: tokens[1], + }) + } + } + return providers, nil +} + // ReadExternalAuthProvidersFromEnv is provided for compatibility purposes with // the viper CLI. func ReadExternalAuthProvidersFromEnv(environ []string) ([]codersdk.ExternalAuthConfig, error) { diff --git a/cli/testdata/server-config.yaml.golden b/cli/testdata/server-config.yaml.golden index 8f34ee8cbe7be..fc76a6c2ec8a0 100644 --- a/cli/testdata/server-config.yaml.golden +++ b/cli/testdata/server-config.yaml.golden @@ -519,6 +519,9 @@ client: # Support links to display in the top right drop down menu. # (default: , type: struct[[]codersdk.LinkConfig]) supportLinks: [] +# Configure AI providers. +# (default: , type: struct[codersdk.AIConfig]) +ai: {} # External Authentication providers. # (default: , type: struct[[]codersdk.ExternalAuthConfig]) externalAuthProviders: [] diff --git a/coderd/ai/ai.go b/coderd/ai/ai.go new file mode 100644 index 0000000000000..97c825ae44c06 --- /dev/null +++ b/coderd/ai/ai.go @@ -0,0 +1,167 @@ +package ai + +import ( + "context" + + "github.com/anthropics/anthropic-sdk-go" + anthropicoption "github.com/anthropics/anthropic-sdk-go/option" + "github.com/kylecarbs/aisdk-go" + "github.com/openai/openai-go" + openaioption "github.com/openai/openai-go/option" + "golang.org/x/xerrors" + "google.golang.org/genai" + + "github.com/coder/coder/v2/codersdk" +) + +type LanguageModel struct { + codersdk.LanguageModel + StreamFunc StreamFunc +} + +type StreamOptions struct { + SystemPrompt string + Model string + Messages []aisdk.Message + Thinking bool + Tools []aisdk.Tool +} + +type StreamFunc func(ctx context.Context, options StreamOptions) (aisdk.DataStream, error) + +// LanguageModels is a map of language model ID to language model. +type LanguageModels map[string]LanguageModel + +func ModelsFromConfig(ctx context.Context, configs []codersdk.AIProviderConfig) (LanguageModels, error) { + models := make(LanguageModels) + + for _, config := range configs { + var streamFunc StreamFunc + + switch config.Type { + case "openai": + opts := []openaioption.RequestOption{ + openaioption.WithAPIKey(config.APIKey), + } + if config.BaseURL != "" { + opts = append(opts, openaioption.WithBaseURL(config.BaseURL)) + } + client := openai.NewClient(opts...) + streamFunc = func(ctx context.Context, options StreamOptions) (aisdk.DataStream, error) { + openaiMessages, err := aisdk.MessagesToOpenAI(options.Messages) + if err != nil { + return nil, err + } + tools := aisdk.ToolsToOpenAI(options.Tools) + if options.SystemPrompt != "" { + openaiMessages = append([]openai.ChatCompletionMessageParamUnion{ + openai.SystemMessage(options.SystemPrompt), + }, openaiMessages...) + } + + return aisdk.OpenAIToDataStream(client.Chat.Completions.NewStreaming(ctx, openai.ChatCompletionNewParams{ + Messages: openaiMessages, + Model: options.Model, + Tools: tools, + MaxTokens: openai.Int(8192), + })), nil + } + if config.Models == nil { + models, err := client.Models.List(ctx) + if err != nil { + return nil, err + } + config.Models = make([]string, len(models.Data)) + for i, model := range models.Data { + config.Models[i] = model.ID + } + } + case "anthropic": + client := anthropic.NewClient(anthropicoption.WithAPIKey(config.APIKey)) + streamFunc = func(ctx context.Context, options StreamOptions) (aisdk.DataStream, error) { + anthropicMessages, systemMessage, err := aisdk.MessagesToAnthropic(options.Messages) + if err != nil { + return nil, err + } + if options.SystemPrompt != "" { + systemMessage = []anthropic.TextBlockParam{ + *anthropic.NewTextBlock(options.SystemPrompt).OfRequestTextBlock, + } + } + return aisdk.AnthropicToDataStream(client.Messages.NewStreaming(ctx, anthropic.MessageNewParams{ + Messages: anthropicMessages, + Model: options.Model, + System: systemMessage, + Tools: aisdk.ToolsToAnthropic(options.Tools), + MaxTokens: 8192, + })), nil + } + if config.Models == nil { + models, err := client.Models.List(ctx, anthropic.ModelListParams{}) + if err != nil { + return nil, err + } + config.Models = make([]string, len(models.Data)) + for i, model := range models.Data { + config.Models[i] = model.ID + } + } + case "google": + client, err := genai.NewClient(ctx, &genai.ClientConfig{ + APIKey: config.APIKey, + Backend: genai.BackendGeminiAPI, + }) + if err != nil { + return nil, err + } + streamFunc = func(ctx context.Context, options StreamOptions) (aisdk.DataStream, error) { + googleMessages, err := aisdk.MessagesToGoogle(options.Messages) + if err != nil { + return nil, err + } + tools, err := aisdk.ToolsToGoogle(options.Tools) + if err != nil { + return nil, err + } + var systemInstruction *genai.Content + if options.SystemPrompt != "" { + systemInstruction = &genai.Content{ + Parts: []*genai.Part{ + genai.NewPartFromText(options.SystemPrompt), + }, + Role: "model", + } + } + return aisdk.GoogleToDataStream(client.Models.GenerateContentStream(ctx, options.Model, googleMessages, &genai.GenerateContentConfig{ + SystemInstruction: systemInstruction, + Tools: tools, + })), nil + } + if config.Models == nil { + models, err := client.Models.List(ctx, &genai.ListModelsConfig{}) + if err != nil { + return nil, err + } + config.Models = make([]string, len(models.Items)) + for i, model := range models.Items { + config.Models[i] = model.Name + } + } + default: + return nil, xerrors.Errorf("unsupported model type: %s", config.Type) + } + + for _, model := range config.Models { + models[model] = LanguageModel{ + LanguageModel: codersdk.LanguageModel{ + ID: model, + DisplayName: model, + Provider: config.Type, + }, + StreamFunc: streamFunc, + } + } + } + + return models, nil +} diff --git a/coderd/apidoc/docs.go b/coderd/apidoc/docs.go index daef10a90d422..fb5ae20e448c8 100644 --- a/coderd/apidoc/docs.go +++ b/coderd/apidoc/docs.go @@ -343,6 +343,173 @@ const docTemplate = `{ } } }, + "/chats": { + "get": { + "security": [ + { + "CoderSessionToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "Chat" + ], + "summary": "List chats", + "operationId": "list-chats", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/codersdk.Chat" + } + } + } + } + }, + "post": { + "security": [ + { + "CoderSessionToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "Chat" + ], + "summary": "Create a chat", + "operationId": "create-a-chat", + "responses": { + "201": { + "description": "Created", + "schema": { + "$ref": "#/definitions/codersdk.Chat" + } + } + } + } + }, + "/chats/{chat}": { + "get": { + "security": [ + { + "CoderSessionToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "Chat" + ], + "summary": "Get a chat", + "operationId": "get-a-chat", + "parameters": [ + { + "type": "string", + "description": "Chat ID", + "name": "chat", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/codersdk.Chat" + } + } + } + } + }, + "/chats/{chat}/messages": { + "get": { + "security": [ + { + "CoderSessionToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "Chat" + ], + "summary": "Get chat messages", + "operationId": "get-chat-messages", + "parameters": [ + { + "type": "string", + "description": "Chat ID", + "name": "chat", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/aisdk.Message" + } + } + } + } + }, + "post": { + "security": [ + { + "CoderSessionToken": [] + } + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Chat" + ], + "summary": "Create a chat message", + "operationId": "create-a-chat-message", + "parameters": [ + { + "type": "string", + "description": "Chat ID", + "name": "chat", + "in": "path", + "required": true + }, + { + "description": "Request body", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/codersdk.CreateChatMessageRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": {} + } + } + } + } + }, "/csp/reports": { "post": { "security": [ @@ -659,6 +826,31 @@ const docTemplate = `{ } } }, + "/deployment/llms": { + "get": { + "security": [ + { + "CoderSessionToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "General" + ], + "summary": "Get language models", + "operationId": "get-language-models", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/codersdk.LanguageModelConfig" + } + } + } + } + }, "/deployment/ssh": { "get": { "security": [ @@ -10297,6 +10489,190 @@ const docTemplate = `{ } } }, + "aisdk.Attachment": { + "type": "object", + "properties": { + "contentType": { + "type": "string" + }, + "name": { + "type": "string" + }, + "url": { + "type": "string" + } + } + }, + "aisdk.Message": { + "type": "object", + "properties": { + "annotations": { + "type": "array", + "items": {} + }, + "content": { + "type": "string" + }, + "createdAt": { + "type": "array", + "items": { + "type": "integer" + } + }, + "experimental_attachments": { + "type": "array", + "items": { + "$ref": "#/definitions/aisdk.Attachment" + } + }, + "id": { + "type": "string" + }, + "parts": { + "type": "array", + "items": { + "$ref": "#/definitions/aisdk.Part" + } + }, + "role": { + "type": "string" + } + } + }, + "aisdk.Part": { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "type": "integer" + } + }, + "details": { + "type": "array", + "items": { + "$ref": "#/definitions/aisdk.ReasoningDetail" + } + }, + "mimeType": { + "description": "Type: \"file\"", + "type": "string" + }, + "reasoning": { + "description": "Type: \"reasoning\"", + "type": "string" + }, + "source": { + "description": "Type: \"source\"", + "allOf": [ + { + "$ref": "#/definitions/aisdk.SourceInfo" + } + ] + }, + "text": { + "description": "Type: \"text\"", + "type": "string" + }, + "toolInvocation": { + "description": "Type: \"tool-invocation\"", + "allOf": [ + { + "$ref": "#/definitions/aisdk.ToolInvocation" + } + ] + }, + "type": { + "$ref": "#/definitions/aisdk.PartType" + } + } + }, + "aisdk.PartType": { + "type": "string", + "enum": [ + "text", + "reasoning", + "tool-invocation", + "source", + "file", + "step-start" + ], + "x-enum-varnames": [ + "PartTypeText", + "PartTypeReasoning", + "PartTypeToolInvocation", + "PartTypeSource", + "PartTypeFile", + "PartTypeStepStart" + ] + }, + "aisdk.ReasoningDetail": { + "type": "object", + "properties": { + "data": { + "type": "string" + }, + "signature": { + "type": "string" + }, + "text": { + "type": "string" + }, + "type": { + "type": "string" + } + } + }, + "aisdk.SourceInfo": { + "type": "object", + "properties": { + "contentType": { + "type": "string" + }, + "data": { + "type": "string" + }, + "metadata": { + "type": "object", + "additionalProperties": {} + }, + "uri": { + "type": "string" + } + } + }, + "aisdk.ToolInvocation": { + "type": "object", + "properties": { + "args": {}, + "result": {}, + "state": { + "$ref": "#/definitions/aisdk.ToolInvocationState" + }, + "step": { + "type": "integer" + }, + "toolCallId": { + "type": "string" + }, + "toolName": { + "type": "string" + } + } + }, + "aisdk.ToolInvocationState": { + "type": "string", + "enum": [ + "call", + "partial-call", + "result" + ], + "x-enum-varnames": [ + "ToolInvocationStateCall", + "ToolInvocationStatePartialCall", + "ToolInvocationStateResult" + ] + }, "coderd.SCIMUser": { "type": "object", "properties": { @@ -10388,6 +10764,37 @@ const docTemplate = `{ } } }, + "codersdk.AIConfig": { + "type": "object", + "properties": { + "providers": { + "type": "array", + "items": { + "$ref": "#/definitions/codersdk.AIProviderConfig" + } + } + } + }, + "codersdk.AIProviderConfig": { + "type": "object", + "properties": { + "base_url": { + "description": "BaseURL is the base URL to use for the API provider.", + "type": "string" + }, + "models": { + "description": "Models is the list of models to use for the API provider.", + "type": "array", + "items": { + "type": "string" + } + }, + "type": { + "description": "Type is the type of the API provider.", + "type": "string" + } + } + }, "codersdk.APIKey": { "type": "object", "required": [ @@ -10973,6 +11380,62 @@ const docTemplate = `{ } } }, + "codersdk.Chat": { + "type": "object", + "properties": { + "created_at": { + "type": "string", + "format": "date-time" + }, + "id": { + "type": "string", + "format": "uuid" + }, + "title": { + "type": "string" + }, + "updated_at": { + "type": "string", + "format": "date-time" + } + } + }, + "codersdk.ChatMessage": { + "type": "object", + "properties": { + "annotations": { + "type": "array", + "items": {} + }, + "content": { + "type": "string" + }, + "createdAt": { + "type": "array", + "items": { + "type": "integer" + } + }, + "experimental_attachments": { + "type": "array", + "items": { + "$ref": "#/definitions/aisdk.Attachment" + } + }, + "id": { + "type": "string" + }, + "parts": { + "type": "array", + "items": { + "$ref": "#/definitions/aisdk.Part" + } + }, + "role": { + "type": "string" + } + } + }, "codersdk.ConnectionLatency": { "type": "object", "properties": { @@ -11006,6 +11469,20 @@ const docTemplate = `{ } } }, + "codersdk.CreateChatMessageRequest": { + "type": "object", + "properties": { + "message": { + "$ref": "#/definitions/codersdk.ChatMessage" + }, + "model": { + "type": "string" + }, + "thinking": { + "type": "boolean" + } + } + }, "codersdk.CreateFirstUserRequest": { "type": "object", "required": [ @@ -11293,7 +11770,73 @@ const docTemplate = `{ } }, "codersdk.CreateTestAuditLogRequest": { - "type": "object" + "type": "object", + "properties": { + "action": { + "enum": [ + "create", + "write", + "delete", + "start", + "stop" + ], + "allOf": [ + { + "$ref": "#/definitions/codersdk.AuditAction" + } + ] + }, + "additional_fields": { + "type": "array", + "items": { + "type": "integer" + } + }, + "build_reason": { + "enum": [ + "autostart", + "autostop", + "initiator" + ], + "allOf": [ + { + "$ref": "#/definitions/codersdk.BuildReason" + } + ] + }, + "organization_id": { + "type": "string", + "format": "uuid" + }, + "request_id": { + "type": "string", + "format": "uuid" + }, + "resource_id": { + "type": "string", + "format": "uuid" + }, + "resource_type": { + "enum": [ + "template", + "template_version", + "user", + "workspace", + "workspace_build", + "git_ssh_key", + "auditable_group" + ], + "allOf": [ + { + "$ref": "#/definitions/codersdk.ResourceType" + } + ] + }, + "time": { + "type": "string", + "format": "date-time" + } + } }, "codersdk.CreateTokenRequest": { "type": "object", @@ -11742,6 +12285,9 @@ const docTemplate = `{ "agent_stat_refresh_interval": { "type": "integer" }, + "ai": { + "$ref": "#/definitions/serpent.Struct-codersdk_AIConfig" + }, "allow_workspace_renames": { "type": "boolean" }, @@ -12009,9 +12555,11 @@ const docTemplate = `{ "workspace-usage", "web-push", "dynamic-parameters", - "workspace-prebuilds" + "workspace-prebuilds", + "agentic-chat" ], "x-enum-comments": { + "ExperimentAgenticChat": "Enables the new agentic AI chat feature.", "ExperimentAutoFillParameters": "This should not be taken out of experiments until we have redesigned the feature.", "ExperimentDynamicParameters": "Enables dynamic parameters when creating a workspace.", "ExperimentExample": "This isn't used for anything.", @@ -12027,7 +12575,8 @@ const docTemplate = `{ "ExperimentWorkspaceUsage", "ExperimentWebPush", "ExperimentDynamicParameters", - "ExperimentWorkspacePrebuilds" + "ExperimentWorkspacePrebuilds", + "ExperimentAgenticChat" ] }, "codersdk.ExternalAuth": { @@ -12538,6 +13087,33 @@ const docTemplate = `{ "RequiredTemplateVariables" ] }, + "codersdk.LanguageModel": { + "type": "object", + "properties": { + "display_name": { + "type": "string" + }, + "id": { + "description": "ID is used by the provider to identify the LLM.", + "type": "string" + }, + "provider": { + "description": "Provider is the provider of the LLM. e.g. openai, anthropic, etc.", + "type": "string" + } + } + }, + "codersdk.LanguageModelConfig": { + "type": "object", + "properties": { + "models": { + "type": "array", + "items": { + "$ref": "#/definitions/codersdk.LanguageModel" + } + } + } + }, "codersdk.License": { "type": "object", "properties": { @@ -14272,6 +14848,7 @@ const docTemplate = `{ "assign_org_role", "assign_role", "audit_log", + "chat", "crypto_key", "debug_info", "deployment_config", @@ -14310,6 +14887,7 @@ const docTemplate = `{ "ResourceAssignOrgRole", "ResourceAssignRole", "ResourceAuditLog", + "ResourceChat", "ResourceCryptoKey", "ResourceDebugInfo", "ResourceDeploymentConfig", @@ -18250,6 +18828,14 @@ const docTemplate = `{ } } }, + "serpent.Struct-codersdk_AIConfig": { + "type": "object", + "properties": { + "value": { + "$ref": "#/definitions/codersdk.AIConfig" + } + } + }, "serpent.URL": { "type": "object", "properties": { diff --git a/coderd/apidoc/swagger.json b/coderd/apidoc/swagger.json index 3a7bc4c2c71ed..8420c9ea0f812 100644 --- a/coderd/apidoc/swagger.json +++ b/coderd/apidoc/swagger.json @@ -291,6 +291,151 @@ } } }, + "/chats": { + "get": { + "security": [ + { + "CoderSessionToken": [] + } + ], + "produces": ["application/json"], + "tags": ["Chat"], + "summary": "List chats", + "operationId": "list-chats", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/codersdk.Chat" + } + } + } + } + }, + "post": { + "security": [ + { + "CoderSessionToken": [] + } + ], + "produces": ["application/json"], + "tags": ["Chat"], + "summary": "Create a chat", + "operationId": "create-a-chat", + "responses": { + "201": { + "description": "Created", + "schema": { + "$ref": "#/definitions/codersdk.Chat" + } + } + } + } + }, + "/chats/{chat}": { + "get": { + "security": [ + { + "CoderSessionToken": [] + } + ], + "produces": ["application/json"], + "tags": ["Chat"], + "summary": "Get a chat", + "operationId": "get-a-chat", + "parameters": [ + { + "type": "string", + "description": "Chat ID", + "name": "chat", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/codersdk.Chat" + } + } + } + } + }, + "/chats/{chat}/messages": { + "get": { + "security": [ + { + "CoderSessionToken": [] + } + ], + "produces": ["application/json"], + "tags": ["Chat"], + "summary": "Get chat messages", + "operationId": "get-chat-messages", + "parameters": [ + { + "type": "string", + "description": "Chat ID", + "name": "chat", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/aisdk.Message" + } + } + } + } + }, + "post": { + "security": [ + { + "CoderSessionToken": [] + } + ], + "consumes": ["application/json"], + "produces": ["application/json"], + "tags": ["Chat"], + "summary": "Create a chat message", + "operationId": "create-a-chat-message", + "parameters": [ + { + "type": "string", + "description": "Chat ID", + "name": "chat", + "in": "path", + "required": true + }, + { + "description": "Request body", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/codersdk.CreateChatMessageRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": {} + } + } + } + } + }, "/csp/reports": { "post": { "security": [ @@ -563,6 +708,27 @@ } } }, + "/deployment/llms": { + "get": { + "security": [ + { + "CoderSessionToken": [] + } + ], + "produces": ["application/json"], + "tags": ["General"], + "summary": "Get language models", + "operationId": "get-language-models", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/codersdk.LanguageModelConfig" + } + } + } + } + }, "/deployment/ssh": { "get": { "security": [ @@ -9134,6 +9300,186 @@ } } }, + "aisdk.Attachment": { + "type": "object", + "properties": { + "contentType": { + "type": "string" + }, + "name": { + "type": "string" + }, + "url": { + "type": "string" + } + } + }, + "aisdk.Message": { + "type": "object", + "properties": { + "annotations": { + "type": "array", + "items": {} + }, + "content": { + "type": "string" + }, + "createdAt": { + "type": "array", + "items": { + "type": "integer" + } + }, + "experimental_attachments": { + "type": "array", + "items": { + "$ref": "#/definitions/aisdk.Attachment" + } + }, + "id": { + "type": "string" + }, + "parts": { + "type": "array", + "items": { + "$ref": "#/definitions/aisdk.Part" + } + }, + "role": { + "type": "string" + } + } + }, + "aisdk.Part": { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "type": "integer" + } + }, + "details": { + "type": "array", + "items": { + "$ref": "#/definitions/aisdk.ReasoningDetail" + } + }, + "mimeType": { + "description": "Type: \"file\"", + "type": "string" + }, + "reasoning": { + "description": "Type: \"reasoning\"", + "type": "string" + }, + "source": { + "description": "Type: \"source\"", + "allOf": [ + { + "$ref": "#/definitions/aisdk.SourceInfo" + } + ] + }, + "text": { + "description": "Type: \"text\"", + "type": "string" + }, + "toolInvocation": { + "description": "Type: \"tool-invocation\"", + "allOf": [ + { + "$ref": "#/definitions/aisdk.ToolInvocation" + } + ] + }, + "type": { + "$ref": "#/definitions/aisdk.PartType" + } + } + }, + "aisdk.PartType": { + "type": "string", + "enum": [ + "text", + "reasoning", + "tool-invocation", + "source", + "file", + "step-start" + ], + "x-enum-varnames": [ + "PartTypeText", + "PartTypeReasoning", + "PartTypeToolInvocation", + "PartTypeSource", + "PartTypeFile", + "PartTypeStepStart" + ] + }, + "aisdk.ReasoningDetail": { + "type": "object", + "properties": { + "data": { + "type": "string" + }, + "signature": { + "type": "string" + }, + "text": { + "type": "string" + }, + "type": { + "type": "string" + } + } + }, + "aisdk.SourceInfo": { + "type": "object", + "properties": { + "contentType": { + "type": "string" + }, + "data": { + "type": "string" + }, + "metadata": { + "type": "object", + "additionalProperties": {} + }, + "uri": { + "type": "string" + } + } + }, + "aisdk.ToolInvocation": { + "type": "object", + "properties": { + "args": {}, + "result": {}, + "state": { + "$ref": "#/definitions/aisdk.ToolInvocationState" + }, + "step": { + "type": "integer" + }, + "toolCallId": { + "type": "string" + }, + "toolName": { + "type": "string" + } + } + }, + "aisdk.ToolInvocationState": { + "type": "string", + "enum": ["call", "partial-call", "result"], + "x-enum-varnames": [ + "ToolInvocationStateCall", + "ToolInvocationStatePartialCall", + "ToolInvocationStateResult" + ] + }, "coderd.SCIMUser": { "type": "object", "properties": { @@ -9225,6 +9571,37 @@ } } }, + "codersdk.AIConfig": { + "type": "object", + "properties": { + "providers": { + "type": "array", + "items": { + "$ref": "#/definitions/codersdk.AIProviderConfig" + } + } + } + }, + "codersdk.AIProviderConfig": { + "type": "object", + "properties": { + "base_url": { + "description": "BaseURL is the base URL to use for the API provider.", + "type": "string" + }, + "models": { + "description": "Models is the list of models to use for the API provider.", + "type": "array", + "items": { + "type": "string" + } + }, + "type": { + "description": "Type is the type of the API provider.", + "type": "string" + } + } + }, "codersdk.APIKey": { "type": "object", "required": [ @@ -9771,6 +10148,62 @@ } } }, + "codersdk.Chat": { + "type": "object", + "properties": { + "created_at": { + "type": "string", + "format": "date-time" + }, + "id": { + "type": "string", + "format": "uuid" + }, + "title": { + "type": "string" + }, + "updated_at": { + "type": "string", + "format": "date-time" + } + } + }, + "codersdk.ChatMessage": { + "type": "object", + "properties": { + "annotations": { + "type": "array", + "items": {} + }, + "content": { + "type": "string" + }, + "createdAt": { + "type": "array", + "items": { + "type": "integer" + } + }, + "experimental_attachments": { + "type": "array", + "items": { + "$ref": "#/definitions/aisdk.Attachment" + } + }, + "id": { + "type": "string" + }, + "parts": { + "type": "array", + "items": { + "$ref": "#/definitions/aisdk.Part" + } + }, + "role": { + "type": "string" + } + } + }, "codersdk.ConnectionLatency": { "type": "object", "properties": { @@ -9801,6 +10234,20 @@ } } }, + "codersdk.CreateChatMessageRequest": { + "type": "object", + "properties": { + "message": { + "$ref": "#/definitions/codersdk.ChatMessage" + }, + "model": { + "type": "string" + }, + "thinking": { + "type": "boolean" + } + } + }, "codersdk.CreateFirstUserRequest": { "type": "object", "required": ["email", "password", "username"], @@ -10069,7 +10516,63 @@ } }, "codersdk.CreateTestAuditLogRequest": { - "type": "object" + "type": "object", + "properties": { + "action": { + "enum": ["create", "write", "delete", "start", "stop"], + "allOf": [ + { + "$ref": "#/definitions/codersdk.AuditAction" + } + ] + }, + "additional_fields": { + "type": "array", + "items": { + "type": "integer" + } + }, + "build_reason": { + "enum": ["autostart", "autostop", "initiator"], + "allOf": [ + { + "$ref": "#/definitions/codersdk.BuildReason" + } + ] + }, + "organization_id": { + "type": "string", + "format": "uuid" + }, + "request_id": { + "type": "string", + "format": "uuid" + }, + "resource_id": { + "type": "string", + "format": "uuid" + }, + "resource_type": { + "enum": [ + "template", + "template_version", + "user", + "workspace", + "workspace_build", + "git_ssh_key", + "auditable_group" + ], + "allOf": [ + { + "$ref": "#/definitions/codersdk.ResourceType" + } + ] + }, + "time": { + "type": "string", + "format": "date-time" + } + } }, "codersdk.CreateTokenRequest": { "type": "object", @@ -10500,6 +11003,9 @@ "agent_stat_refresh_interval": { "type": "integer" }, + "ai": { + "$ref": "#/definitions/serpent.Struct-codersdk_AIConfig" + }, "allow_workspace_renames": { "type": "boolean" }, @@ -10763,9 +11269,11 @@ "workspace-usage", "web-push", "dynamic-parameters", - "workspace-prebuilds" + "workspace-prebuilds", + "agentic-chat" ], "x-enum-comments": { + "ExperimentAgenticChat": "Enables the new agentic AI chat feature.", "ExperimentAutoFillParameters": "This should not be taken out of experiments until we have redesigned the feature.", "ExperimentDynamicParameters": "Enables dynamic parameters when creating a workspace.", "ExperimentExample": "This isn't used for anything.", @@ -10781,7 +11289,8 @@ "ExperimentWorkspaceUsage", "ExperimentWebPush", "ExperimentDynamicParameters", - "ExperimentWorkspacePrebuilds" + "ExperimentWorkspacePrebuilds", + "ExperimentAgenticChat" ] }, "codersdk.ExternalAuth": { @@ -11276,6 +11785,33 @@ "enum": ["REQUIRED_TEMPLATE_VARIABLES"], "x-enum-varnames": ["RequiredTemplateVariables"] }, + "codersdk.LanguageModel": { + "type": "object", + "properties": { + "display_name": { + "type": "string" + }, + "id": { + "description": "ID is used by the provider to identify the LLM.", + "type": "string" + }, + "provider": { + "description": "Provider is the provider of the LLM. e.g. openai, anthropic, etc.", + "type": "string" + } + } + }, + "codersdk.LanguageModelConfig": { + "type": "object", + "properties": { + "models": { + "type": "array", + "items": { + "$ref": "#/definitions/codersdk.LanguageModel" + } + } + } + }, "codersdk.License": { "type": "object", "properties": { @@ -12930,6 +13466,7 @@ "assign_org_role", "assign_role", "audit_log", + "chat", "crypto_key", "debug_info", "deployment_config", @@ -12968,6 +13505,7 @@ "ResourceAssignOrgRole", "ResourceAssignRole", "ResourceAuditLog", + "ResourceChat", "ResourceCryptoKey", "ResourceDebugInfo", "ResourceDeploymentConfig", @@ -16705,6 +17243,14 @@ } } }, + "serpent.Struct-codersdk_AIConfig": { + "type": "object", + "properties": { + "value": { + "$ref": "#/definitions/codersdk.AIConfig" + } + } + }, "serpent.URL": { "type": "object", "properties": { diff --git a/coderd/chat.go b/coderd/chat.go new file mode 100644 index 0000000000000..b10211075cfe6 --- /dev/null +++ b/coderd/chat.go @@ -0,0 +1,366 @@ +package coderd + +import ( + "encoding/json" + "io" + "net/http" + "time" + + "github.com/kylecarbs/aisdk-go" + + "github.com/coder/coder/v2/coderd/ai" + "github.com/coder/coder/v2/coderd/database" + "github.com/coder/coder/v2/coderd/database/db2sdk" + "github.com/coder/coder/v2/coderd/database/dbtime" + "github.com/coder/coder/v2/coderd/httpapi" + "github.com/coder/coder/v2/coderd/httpmw" + "github.com/coder/coder/v2/coderd/util/strings" + "github.com/coder/coder/v2/codersdk" + "github.com/coder/coder/v2/codersdk/toolsdk" +) + +// postChats creates a new chat. +// +// @Summary Create a chat +// @ID create-a-chat +// @Security CoderSessionToken +// @Produce json +// @Tags Chat +// @Success 201 {object} codersdk.Chat +// @Router /chats [post] +func (api *API) postChats(w http.ResponseWriter, r *http.Request) { + apiKey := httpmw.APIKey(r) + ctx := r.Context() + + chat, err := api.Database.InsertChat(ctx, database.InsertChatParams{ + OwnerID: apiKey.UserID, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + Title: "New Chat", + }) + if err != nil { + httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ + Message: "Failed to create chat", + Detail: err.Error(), + }) + return + } + + httpapi.Write(ctx, w, http.StatusCreated, db2sdk.Chat(chat)) +} + +// listChats lists all chats for a user. +// +// @Summary List chats +// @ID list-chats +// @Security CoderSessionToken +// @Produce json +// @Tags Chat +// @Success 200 {array} codersdk.Chat +// @Router /chats [get] +func (api *API) listChats(w http.ResponseWriter, r *http.Request) { + apiKey := httpmw.APIKey(r) + ctx := r.Context() + + chats, err := api.Database.GetChatsByOwnerID(ctx, apiKey.UserID) + if err != nil { + httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ + Message: "Failed to list chats", + Detail: err.Error(), + }) + return + } + + httpapi.Write(ctx, w, http.StatusOK, db2sdk.Chats(chats)) +} + +// chat returns a chat by ID. +// +// @Summary Get a chat +// @ID get-a-chat +// @Security CoderSessionToken +// @Produce json +// @Tags Chat +// @Param chat path string true "Chat ID" +// @Success 200 {object} codersdk.Chat +// @Router /chats/{chat} [get] +func (*API) chat(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + chat := httpmw.ChatParam(r) + httpapi.Write(ctx, w, http.StatusOK, db2sdk.Chat(chat)) +} + +// chatMessages returns the messages of a chat. +// +// @Summary Get chat messages +// @ID get-chat-messages +// @Security CoderSessionToken +// @Produce json +// @Tags Chat +// @Param chat path string true "Chat ID" +// @Success 200 {array} aisdk.Message +// @Router /chats/{chat}/messages [get] +func (api *API) chatMessages(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + chat := httpmw.ChatParam(r) + rawMessages, err := api.Database.GetChatMessagesByChatID(ctx, chat.ID) + if err != nil { + httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ + Message: "Failed to get chat messages", + Detail: err.Error(), + }) + return + } + messages := make([]aisdk.Message, len(rawMessages)) + for i, message := range rawMessages { + var msg aisdk.Message + err = json.Unmarshal(message.Content, &msg) + if err != nil { + httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ + Message: "Failed to unmarshal chat message", + Detail: err.Error(), + }) + return + } + messages[i] = msg + } + + httpapi.Write(ctx, w, http.StatusOK, messages) +} + +// postChatMessages creates a new chat message and streams the response. +// +// @Summary Create a chat message +// @ID create-a-chat-message +// @Security CoderSessionToken +// @Accept json +// @Produce json +// @Tags Chat +// @Param chat path string true "Chat ID" +// @Param request body codersdk.CreateChatMessageRequest true "Request body" +// @Success 200 {array} aisdk.DataStreamPart +// @Router /chats/{chat}/messages [post] +func (api *API) postChatMessages(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + chat := httpmw.ChatParam(r) + var req codersdk.CreateChatMessageRequest + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + httpapi.Write(ctx, w, http.StatusBadRequest, codersdk.Response{ + Message: "Failed to decode chat message", + Detail: err.Error(), + }) + return + } + + dbMessages, err := api.Database.GetChatMessagesByChatID(ctx, chat.ID) + if err != nil { + httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ + Message: "Failed to get chat messages", + Detail: err.Error(), + }) + return + } + + messages := make([]codersdk.ChatMessage, 0) + for _, dbMsg := range dbMessages { + var msg codersdk.ChatMessage + err = json.Unmarshal(dbMsg.Content, &msg) + if err != nil { + httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ + Message: "Failed to unmarshal chat message", + Detail: err.Error(), + }) + return + } + messages = append(messages, msg) + } + messages = append(messages, req.Message) + + client := codersdk.New(api.AccessURL) + client.SetSessionToken(httpmw.APITokenFromRequest(r)) + + tools := make([]aisdk.Tool, 0) + handlers := map[string]toolsdk.GenericHandlerFunc{} + for _, tool := range toolsdk.All { + if tool.Name == "coder_report_task" { + continue // This tool requires an agent to run. + } + tools = append(tools, tool.Tool) + handlers[tool.Tool.Name] = tool.Handler + } + + provider, ok := api.LanguageModels[req.Model] + if !ok { + httpapi.Write(ctx, w, http.StatusBadRequest, codersdk.Response{ + Message: "Model not found", + }) + return + } + + // If it's the user's first message, generate a title for the chat. + if len(messages) == 1 { + var acc aisdk.DataStreamAccumulator + stream, err := provider.StreamFunc(ctx, ai.StreamOptions{ + Model: req.Model, + SystemPrompt: `- You will generate a short title based on the user's message. +- It should be maximum of 40 characters. +- Do not use quotes, colons, special characters, or emojis.`, + Messages: messages, + Tools: []aisdk.Tool{}, // This initial stream doesn't use tools. + }) + if err != nil { + httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ + Message: "Failed to create stream", + Detail: err.Error(), + }) + return + } + stream = stream.WithAccumulator(&acc) + err = stream.Pipe(io.Discard) + if err != nil { + httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ + Message: "Failed to pipe stream", + Detail: err.Error(), + }) + return + } + var newTitle string + accMessages := acc.Messages() + // If for some reason the stream didn't return any messages, use the + // original message as the title. + if len(accMessages) == 0 { + newTitle = strings.Truncate(messages[0].Content, 40) + } else { + newTitle = strings.Truncate(accMessages[0].Content, 40) + } + err = api.Database.UpdateChatByID(ctx, database.UpdateChatByIDParams{ + ID: chat.ID, + Title: newTitle, + UpdatedAt: dbtime.Now(), + }) + if err != nil { + httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ + Message: "Failed to update chat title", + Detail: err.Error(), + }) + return + } + } + + // Write headers for the data stream! + aisdk.WriteDataStreamHeaders(w) + + // Insert the user-requested message into the database! + raw, err := json.Marshal([]aisdk.Message{req.Message}) + if err != nil { + httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ + Message: "Failed to marshal chat message", + Detail: err.Error(), + }) + return + } + _, err = api.Database.InsertChatMessages(ctx, database.InsertChatMessagesParams{ + ChatID: chat.ID, + CreatedAt: dbtime.Now(), + Model: req.Model, + Provider: provider.Provider, + Content: raw, + }) + if err != nil { + httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ + Message: "Failed to insert chat messages", + Detail: err.Error(), + }) + return + } + + deps, err := toolsdk.NewDeps(client) + if err != nil { + httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ + Message: "Failed to create tool dependencies", + Detail: err.Error(), + }) + return + } + + for { + var acc aisdk.DataStreamAccumulator + stream, err := provider.StreamFunc(ctx, ai.StreamOptions{ + Model: req.Model, + Messages: messages, + Tools: tools, + SystemPrompt: `You are a chat assistant for Coder - an open-source platform for creating and managing cloud development environments on any infrastructure. You are expected to be precise, concise, and helpful. + +You are running as an agent - please keep going until the user's query is completely resolved, before ending your turn and yielding back to the user. Only terminate your turn when you are sure that the problem is solved. Do NOT guess or make up an answer.`, + }) + if err != nil { + httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ + Message: "Failed to create stream", + Detail: err.Error(), + }) + return + } + stream = stream.WithToolCalling(func(toolCall aisdk.ToolCall) aisdk.ToolCallResult { + tool, ok := handlers[toolCall.Name] + if !ok { + return nil + } + toolArgs, err := json.Marshal(toolCall.Args) + if err != nil { + return nil + } + result, err := tool(ctx, deps, toolArgs) + if err != nil { + return map[string]any{ + "error": err.Error(), + } + } + return result + }).WithAccumulator(&acc) + + err = stream.Pipe(w) + if err != nil { + // The client disppeared! + api.Logger.Error(ctx, "stream pipe error", "error", err) + return + } + + // acc.Messages() may sometimes return nil. Serializing this + // will cause a pq error: "cannot extract elements from a scalar". + newMessages := append([]aisdk.Message{}, acc.Messages()...) + if len(newMessages) > 0 { + raw, err := json.Marshal(newMessages) + if err != nil { + httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ + Message: "Failed to marshal chat message", + Detail: err.Error(), + }) + return + } + messages = append(messages, newMessages...) + + // Insert these messages into the database! + _, err = api.Database.InsertChatMessages(ctx, database.InsertChatMessagesParams{ + ChatID: chat.ID, + CreatedAt: dbtime.Now(), + Model: req.Model, + Provider: provider.Provider, + Content: raw, + }) + if err != nil { + httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ + Message: "Failed to insert chat messages", + Detail: err.Error(), + }) + return + } + } + + if acc.FinishReason() == aisdk.FinishReasonToolCalls { + continue + } + + break + } +} diff --git a/coderd/chat_test.go b/coderd/chat_test.go new file mode 100644 index 0000000000000..71e7b99ab3720 --- /dev/null +++ b/coderd/chat_test.go @@ -0,0 +1,125 @@ +package coderd_test + +import ( + "net/http" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/coder/coder/v2/coderd/coderdtest" + "github.com/coder/coder/v2/coderd/database" + "github.com/coder/coder/v2/coderd/database/dbgen" + "github.com/coder/coder/v2/coderd/database/dbtime" + "github.com/coder/coder/v2/codersdk" + "github.com/coder/coder/v2/testutil" +) + +func TestChat(t *testing.T) { + t.Parallel() + + t.Run("ExperimentAgenticChatDisabled", func(t *testing.T) { + t.Parallel() + + client, _ := coderdtest.NewWithDatabase(t, nil) + owner := coderdtest.CreateFirstUser(t, client) + memberClient, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID) + + // Hit the endpoint to get the chat. It should return a 404. + ctx := testutil.Context(t, testutil.WaitShort) + _, err := memberClient.ListChats(ctx) + require.Error(t, err, "list chats should fail") + var sdkErr *codersdk.Error + require.ErrorAs(t, err, &sdkErr, "request should fail with an SDK error") + require.Equal(t, http.StatusForbidden, sdkErr.StatusCode()) + }) + + t.Run("ChatCRUD", func(t *testing.T) { + t.Parallel() + + dv := coderdtest.DeploymentValues(t) + dv.Experiments = []string{string(codersdk.ExperimentAgenticChat)} + dv.AI.Value = codersdk.AIConfig{ + Providers: []codersdk.AIProviderConfig{ + { + Type: "fake", + APIKey: "", + BaseURL: "http://localhost", + Models: []string{"fake-model"}, + }, + }, + } + client, db := coderdtest.NewWithDatabase(t, &coderdtest.Options{ + DeploymentValues: dv, + }) + owner := coderdtest.CreateFirstUser(t, client) + memberClient, memberUser := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID) + + // Seed the database with some data. + dbChat := dbgen.Chat(t, db, database.Chat{ + OwnerID: memberUser.ID, + CreatedAt: dbtime.Now().Add(-time.Hour), + UpdatedAt: dbtime.Now().Add(-time.Hour), + Title: "This is a test chat", + }) + _ = dbgen.ChatMessage(t, db, database.ChatMessage{ + ChatID: dbChat.ID, + CreatedAt: dbtime.Now().Add(-time.Hour), + Content: []byte(`[{"content": "Hello world"}]`), + Model: "fake model", + Provider: "fake", + }) + + ctx := testutil.Context(t, testutil.WaitShort) + + // Listing chats should return the chat we just inserted. + chats, err := memberClient.ListChats(ctx) + require.NoError(t, err, "list chats should succeed") + require.Len(t, chats, 1, "response should have one chat") + require.Equal(t, dbChat.ID, chats[0].ID, "unexpected chat ID") + require.Equal(t, dbChat.Title, chats[0].Title, "unexpected chat title") + require.Equal(t, dbChat.CreatedAt.UTC(), chats[0].CreatedAt.UTC(), "unexpected chat created at") + require.Equal(t, dbChat.UpdatedAt.UTC(), chats[0].UpdatedAt.UTC(), "unexpected chat updated at") + + // Fetching a single chat by ID should return the same chat. + chat, err := memberClient.Chat(ctx, dbChat.ID) + require.NoError(t, err, "get chat should succeed") + require.Equal(t, chats[0], chat, "get chat should return the same chat") + + // Listing chat messages should return the message we just inserted. + messages, err := memberClient.ChatMessages(ctx, dbChat.ID) + require.NoError(t, err, "list chat messages should succeed") + require.Len(t, messages, 1, "response should have one message") + require.Equal(t, "Hello world", messages[0].Content, "response should have the correct message content") + + // Creating a new chat will fail because the model does not exist. + // TODO: Test the message streaming functionality with a mock model. + // Inserting a chat message will fail due to the model not existing. + _, err = memberClient.CreateChatMessage(ctx, dbChat.ID, codersdk.CreateChatMessageRequest{ + Model: "echo", + Message: codersdk.ChatMessage{ + Role: "user", + Content: "Hello world", + }, + Thinking: false, + }) + require.Error(t, err, "create chat message should fail") + var sdkErr *codersdk.Error + require.ErrorAs(t, err, &sdkErr, "create chat should fail with an SDK error") + require.Equal(t, http.StatusBadRequest, sdkErr.StatusCode(), "create chat should fail with a 400 when model does not exist") + + // Creating a new chat message with malformed content should fail. + res, err := memberClient.Request(ctx, http.MethodPost, "/api/v2/chats/"+dbChat.ID.String()+"/messages", strings.NewReader(`{malformed json}`)) + require.NoError(t, err) + defer res.Body.Close() + apiErr := codersdk.ReadBodyAsError(res) + require.Contains(t, apiErr.Error(), "Failed to decode chat message") + + _, err = memberClient.CreateChat(ctx) + require.NoError(t, err, "create chat should succeed") + chats, err = memberClient.ListChats(ctx) + require.NoError(t, err, "list chats should succeed") + require.Len(t, chats, 2, "response should have two chats") + }) +} diff --git a/coderd/coderd.go b/coderd/coderd.go index 288671c6cb6e9..123e58feb642a 100644 --- a/coderd/coderd.go +++ b/coderd/coderd.go @@ -41,6 +41,7 @@ import ( "github.com/coder/quartz" "github.com/coder/serpent" + "github.com/coder/coder/v2/coderd/ai" "github.com/coder/coder/v2/coderd/cryptokeys" "github.com/coder/coder/v2/coderd/entitlements" "github.com/coder/coder/v2/coderd/files" @@ -155,6 +156,7 @@ type Options struct { Authorizer rbac.Authorizer AzureCertificates x509.VerifyOptions GoogleTokenValidator *idtoken.Validator + LanguageModels ai.LanguageModels GithubOAuth2Config *GithubOAuth2Config OIDCConfig *OIDCConfig PrometheusRegistry *prometheus.Registry @@ -851,7 +853,7 @@ func New(options *Options) *API { next.ServeHTTP(w, r) }) }, - httpmw.CSRF(options.DeploymentValues.HTTPCookies), + // httpmw.CSRF(options.DeploymentValues.HTTPCookies), ) // This incurs a performance hit from the middleware, but is required to make sure @@ -956,6 +958,7 @@ func New(options *Options) *API { r.Get("/config", api.deploymentValues) r.Get("/stats", api.deploymentStats) r.Get("/ssh", api.sshConfig) + r.Get("/llms", api.deploymentLLMs) }) r.Route("/experiments", func(r chi.Router) { r.Use(apiKeyMiddleware) @@ -998,6 +1001,21 @@ func New(options *Options) *API { r.Get("/{fileID}", api.fileByID) r.Post("/", api.postFile) }) + // Chats are an experimental feature + r.Route("/chats", func(r chi.Router) { + r.Use( + apiKeyMiddleware, + httpmw.RequireExperiment(api.Experiments, codersdk.ExperimentAgenticChat), + ) + r.Get("/", api.listChats) + r.Post("/", api.postChats) + r.Route("/{chat}", func(r chi.Router) { + r.Use(httpmw.ExtractChatParam(options.Database)) + r.Get("/", api.chat) + r.Get("/messages", api.chatMessages) + r.Post("/messages", api.postChatMessages) + }) + }) r.Route("/external-auth", func(r chi.Router) { r.Use( apiKeyMiddleware, diff --git a/coderd/database/db2sdk/db2sdk.go b/coderd/database/db2sdk/db2sdk.go index 7efcd009c6ef9..18d1d8a6ac788 100644 --- a/coderd/database/db2sdk/db2sdk.go +++ b/coderd/database/db2sdk/db2sdk.go @@ -751,3 +751,16 @@ func AgentProtoConnectionActionToAuditAction(action database.AuditAction) (agent return agentproto.Connection_ACTION_UNSPECIFIED, xerrors.Errorf("unknown agent connection action %q", action) } } + +func Chat(chat database.Chat) codersdk.Chat { + return codersdk.Chat{ + ID: chat.ID, + Title: chat.Title, + CreatedAt: chat.CreatedAt, + UpdatedAt: chat.UpdatedAt, + } +} + +func Chats(chats []database.Chat) []codersdk.Chat { + return List(chats, Chat) +} diff --git a/coderd/database/dbauthz/dbauthz.go b/coderd/database/dbauthz/dbauthz.go index ceb5ba7f2a15a..2ed230dd7a8f3 100644 --- a/coderd/database/dbauthz/dbauthz.go +++ b/coderd/database/dbauthz/dbauthz.go @@ -1269,6 +1269,10 @@ func (q *querier) DeleteApplicationConnectAPIKeysByUserID(ctx context.Context, u return q.db.DeleteApplicationConnectAPIKeysByUserID(ctx, userID) } +func (q *querier) DeleteChat(ctx context.Context, id uuid.UUID) error { + return deleteQ(q.log, q.auth, q.db.GetChatByID, q.db.DeleteChat)(ctx, id) +} + func (q *querier) DeleteCoordinator(ctx context.Context, id uuid.UUID) error { if err := q.authorizeContext(ctx, policy.ActionDelete, rbac.ResourceTailnetCoordinator); err != nil { return err @@ -1686,6 +1690,22 @@ func (q *querier) GetAuthorizationUserRoles(ctx context.Context, userID uuid.UUI return q.db.GetAuthorizationUserRoles(ctx, userID) } +func (q *querier) GetChatByID(ctx context.Context, id uuid.UUID) (database.Chat, error) { + return fetch(q.log, q.auth, q.db.GetChatByID)(ctx, id) +} + +func (q *querier) GetChatMessagesByChatID(ctx context.Context, chatID uuid.UUID) ([]database.ChatMessage, error) { + c, err := q.GetChatByID(ctx, chatID) + if err != nil { + return nil, err + } + return q.db.GetChatMessagesByChatID(ctx, c.ID) +} + +func (q *querier) GetChatsByOwnerID(ctx context.Context, ownerID uuid.UUID) ([]database.Chat, error) { + return fetchWithPostFilter(q.auth, policy.ActionRead, q.db.GetChatsByOwnerID)(ctx, ownerID) +} + func (q *querier) GetCoordinatorResumeTokenSigningKey(ctx context.Context) (string, error) { if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceSystem); err != nil { return "", err @@ -3315,6 +3335,21 @@ func (q *querier) InsertAuditLog(ctx context.Context, arg database.InsertAuditLo return insert(q.log, q.auth, rbac.ResourceAuditLog, q.db.InsertAuditLog)(ctx, arg) } +func (q *querier) InsertChat(ctx context.Context, arg database.InsertChatParams) (database.Chat, error) { + return insert(q.log, q.auth, rbac.ResourceChat.WithOwner(arg.OwnerID.String()), q.db.InsertChat)(ctx, arg) +} + +func (q *querier) InsertChatMessages(ctx context.Context, arg database.InsertChatMessagesParams) ([]database.ChatMessage, error) { + c, err := q.db.GetChatByID(ctx, arg.ChatID) + if err != nil { + return nil, err + } + if err := q.authorizeContext(ctx, policy.ActionUpdate, c); err != nil { + return nil, err + } + return q.db.InsertChatMessages(ctx, arg) +} + func (q *querier) InsertCryptoKey(ctx context.Context, arg database.InsertCryptoKeyParams) (database.CryptoKey, error) { if err := q.authorizeContext(ctx, policy.ActionCreate, rbac.ResourceCryptoKey); err != nil { return database.CryptoKey{}, err @@ -3963,6 +3998,13 @@ func (q *querier) UpdateAPIKeyByID(ctx context.Context, arg database.UpdateAPIKe return update(q.log, q.auth, fetch, q.db.UpdateAPIKeyByID)(ctx, arg) } +func (q *querier) UpdateChatByID(ctx context.Context, arg database.UpdateChatByIDParams) error { + fetch := func(ctx context.Context, arg database.UpdateChatByIDParams) (database.Chat, error) { + return q.db.GetChatByID(ctx, arg.ID) + } + return update(q.log, q.auth, fetch, q.db.UpdateChatByID)(ctx, arg) +} + func (q *querier) UpdateCryptoKeyDeletesAt(ctx context.Context, arg database.UpdateCryptoKeyDeletesAtParams) (database.CryptoKey, error) { if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceCryptoKey); err != nil { return database.CryptoKey{}, err diff --git a/coderd/database/dbauthz/dbauthz_test.go b/coderd/database/dbauthz/dbauthz_test.go index e562bbd1f7160..6dc9a32f03943 100644 --- a/coderd/database/dbauthz/dbauthz_test.go +++ b/coderd/database/dbauthz/dbauthz_test.go @@ -5307,3 +5307,77 @@ func (s *MethodTestSuite) TestResourcesProvisionerdserver() { }).Asserts(rbac.ResourceWorkspaceAgentDevcontainers, policy.ActionCreate) })) } + +func (s *MethodTestSuite) TestChat() { + createChat := func(t *testing.T, db database.Store) (database.User, database.Chat, database.ChatMessage) { + t.Helper() + + usr := dbgen.User(t, db, database.User{}) + chat := dbgen.Chat(s.T(), db, database.Chat{ + OwnerID: usr.ID, + }) + msg := dbgen.ChatMessage(s.T(), db, database.ChatMessage{ + ChatID: chat.ID, + }) + + return usr, chat, msg + } + + s.Run("DeleteChat", s.Subtest(func(db database.Store, check *expects) { + _, c, _ := createChat(s.T(), db) + check.Args(c.ID).Asserts(c, policy.ActionDelete) + })) + + s.Run("GetChatByID", s.Subtest(func(db database.Store, check *expects) { + _, c, _ := createChat(s.T(), db) + check.Args(c.ID).Asserts(c, policy.ActionRead).Returns(c) + })) + + s.Run("GetChatMessagesByChatID", s.Subtest(func(db database.Store, check *expects) { + _, c, m := createChat(s.T(), db) + check.Args(c.ID).Asserts(c, policy.ActionRead).Returns([]database.ChatMessage{m}) + })) + + s.Run("GetChatsByOwnerID", s.Subtest(func(db database.Store, check *expects) { + u1, u1c1, _ := createChat(s.T(), db) + u1c2 := dbgen.Chat(s.T(), db, database.Chat{ + OwnerID: u1.ID, + CreatedAt: u1c1.CreatedAt.Add(time.Hour), + }) + _, _, _ = createChat(s.T(), db) // other user's chat + check.Args(u1.ID).Asserts(u1c2, policy.ActionRead, u1c1, policy.ActionRead).Returns([]database.Chat{u1c2, u1c1}) + })) + + s.Run("InsertChat", s.Subtest(func(db database.Store, check *expects) { + usr := dbgen.User(s.T(), db, database.User{}) + check.Args(database.InsertChatParams{ + OwnerID: usr.ID, + Title: "test chat", + CreatedAt: dbtime.Now(), + UpdatedAt: dbtime.Now(), + }).Asserts(rbac.ResourceChat.WithOwner(usr.ID.String()), policy.ActionCreate) + })) + + s.Run("InsertChatMessages", s.Subtest(func(db database.Store, check *expects) { + usr := dbgen.User(s.T(), db, database.User{}) + chat := dbgen.Chat(s.T(), db, database.Chat{ + OwnerID: usr.ID, + }) + check.Args(database.InsertChatMessagesParams{ + ChatID: chat.ID, + CreatedAt: dbtime.Now(), + Model: "test-model", + Provider: "test-provider", + Content: []byte(`[]`), + }).Asserts(chat, policy.ActionUpdate) + })) + + s.Run("UpdateChatByID", s.Subtest(func(db database.Store, check *expects) { + _, c, _ := createChat(s.T(), db) + check.Args(database.UpdateChatByIDParams{ + ID: c.ID, + Title: "new title", + UpdatedAt: dbtime.Now(), + }).Asserts(c, policy.ActionUpdate) + })) +} diff --git a/coderd/database/dbgen/dbgen.go b/coderd/database/dbgen/dbgen.go index 854c7c2974fe6..55c2fe4cf6965 100644 --- a/coderd/database/dbgen/dbgen.go +++ b/coderd/database/dbgen/dbgen.go @@ -142,6 +142,30 @@ func APIKey(t testing.TB, db database.Store, seed database.APIKey) (key database return key, fmt.Sprintf("%s-%s", key.ID, secret) } +func Chat(t testing.TB, db database.Store, seed database.Chat) database.Chat { + chat, err := db.InsertChat(genCtx, database.InsertChatParams{ + OwnerID: takeFirst(seed.OwnerID, uuid.New()), + CreatedAt: takeFirst(seed.CreatedAt, dbtime.Now()), + UpdatedAt: takeFirst(seed.UpdatedAt, dbtime.Now()), + Title: takeFirst(seed.Title, "Test Chat"), + }) + require.NoError(t, err, "insert chat") + return chat +} + +func ChatMessage(t testing.TB, db database.Store, seed database.ChatMessage) database.ChatMessage { + msg, err := db.InsertChatMessages(genCtx, database.InsertChatMessagesParams{ + CreatedAt: takeFirst(seed.CreatedAt, dbtime.Now()), + ChatID: takeFirst(seed.ChatID, uuid.New()), + Model: takeFirst(seed.Model, "train"), + Provider: takeFirst(seed.Provider, "thomas"), + Content: takeFirstSlice(seed.Content, []byte(`[{"text": "Choo choo!"}]`)), + }) + require.NoError(t, err, "insert chat message") + require.Len(t, msg, 1, "insert one chat message did not return exactly one message") + return msg[0] +} + func WorkspaceAgentPortShare(t testing.TB, db database.Store, orig database.WorkspaceAgentPortShare) database.WorkspaceAgentPortShare { ps, err := db.UpsertWorkspaceAgentPortShare(genCtx, database.UpsertWorkspaceAgentPortShareParams{ WorkspaceID: takeFirst(orig.WorkspaceID, uuid.New()), diff --git a/coderd/database/dbmem/dbmem.go b/coderd/database/dbmem/dbmem.go index 1359d2e63484d..6bae4455a89ef 100644 --- a/coderd/database/dbmem/dbmem.go +++ b/coderd/database/dbmem/dbmem.go @@ -215,6 +215,8 @@ type data struct { // New tables auditLogs []database.AuditLog + chats []database.Chat + chatMessages []database.ChatMessage cryptoKeys []database.CryptoKey dbcryptKeys []database.DBCryptKey files []database.File @@ -1885,6 +1887,19 @@ func (q *FakeQuerier) DeleteApplicationConnectAPIKeysByUserID(_ context.Context, return nil } +func (q *FakeQuerier) DeleteChat(ctx context.Context, id uuid.UUID) error { + q.mutex.Lock() + defer q.mutex.Unlock() + + for i, chat := range q.chats { + if chat.ID == id { + q.chats = append(q.chats[:i], q.chats[i+1:]...) + return nil + } + } + return sql.ErrNoRows +} + func (*FakeQuerier) DeleteCoordinator(context.Context, uuid.UUID) error { return ErrUnimplemented } @@ -2866,6 +2881,47 @@ func (q *FakeQuerier) GetAuthorizationUserRoles(_ context.Context, userID uuid.U }, nil } +func (q *FakeQuerier) GetChatByID(ctx context.Context, id uuid.UUID) (database.Chat, error) { + q.mutex.RLock() + defer q.mutex.RUnlock() + + for _, chat := range q.chats { + if chat.ID == id { + return chat, nil + } + } + return database.Chat{}, sql.ErrNoRows +} + +func (q *FakeQuerier) GetChatMessagesByChatID(ctx context.Context, chatID uuid.UUID) ([]database.ChatMessage, error) { + q.mutex.RLock() + defer q.mutex.RUnlock() + + messages := []database.ChatMessage{} + for _, chatMessage := range q.chatMessages { + if chatMessage.ChatID == chatID { + messages = append(messages, chatMessage) + } + } + return messages, nil +} + +func (q *FakeQuerier) GetChatsByOwnerID(ctx context.Context, ownerID uuid.UUID) ([]database.Chat, error) { + q.mutex.RLock() + defer q.mutex.RUnlock() + + chats := []database.Chat{} + for _, chat := range q.chats { + if chat.OwnerID == ownerID { + chats = append(chats, chat) + } + } + sort.Slice(chats, func(i, j int) bool { + return chats[i].CreatedAt.After(chats[j].CreatedAt) + }) + return chats, nil +} + func (q *FakeQuerier) GetCoordinatorResumeTokenSigningKey(_ context.Context) (string, error) { q.mutex.RLock() defer q.mutex.RUnlock() @@ -8385,6 +8441,66 @@ func (q *FakeQuerier) InsertAuditLog(_ context.Context, arg database.InsertAudit return alog, nil } +func (q *FakeQuerier) InsertChat(ctx context.Context, arg database.InsertChatParams) (database.Chat, error) { + err := validateDatabaseType(arg) + if err != nil { + return database.Chat{}, err + } + + q.mutex.Lock() + defer q.mutex.Unlock() + + chat := database.Chat{ + ID: uuid.New(), + CreatedAt: arg.CreatedAt, + UpdatedAt: arg.UpdatedAt, + OwnerID: arg.OwnerID, + Title: arg.Title, + } + q.chats = append(q.chats, chat) + + return chat, nil +} + +func (q *FakeQuerier) InsertChatMessages(ctx context.Context, arg database.InsertChatMessagesParams) ([]database.ChatMessage, error) { + err := validateDatabaseType(arg) + if err != nil { + return nil, err + } + + q.mutex.Lock() + defer q.mutex.Unlock() + + id := int64(0) + if len(q.chatMessages) > 0 { + id = q.chatMessages[len(q.chatMessages)-1].ID + } + + messages := make([]database.ChatMessage, 0) + + rawMessages := make([]json.RawMessage, 0) + err = json.Unmarshal(arg.Content, &rawMessages) + if err != nil { + return nil, err + } + + for _, content := range rawMessages { + id++ + _ = content + messages = append(messages, database.ChatMessage{ + ID: id, + ChatID: arg.ChatID, + CreatedAt: arg.CreatedAt, + Model: arg.Model, + Provider: arg.Provider, + Content: content, + }) + } + + q.chatMessages = append(q.chatMessages, messages...) + return messages, nil +} + func (q *FakeQuerier) InsertCryptoKey(_ context.Context, arg database.InsertCryptoKeyParams) (database.CryptoKey, error) { err := validateDatabaseType(arg) if err != nil { @@ -10342,6 +10458,27 @@ func (q *FakeQuerier) UpdateAPIKeyByID(_ context.Context, arg database.UpdateAPI return sql.ErrNoRows } +func (q *FakeQuerier) UpdateChatByID(ctx context.Context, arg database.UpdateChatByIDParams) error { + err := validateDatabaseType(arg) + if err != nil { + return err + } + + q.mutex.Lock() + defer q.mutex.Unlock() + + for i, chat := range q.chats { + if chat.ID == arg.ID { + q.chats[i].Title = arg.Title + q.chats[i].UpdatedAt = arg.UpdatedAt + q.chats[i] = chat + return nil + } + } + + return sql.ErrNoRows +} + func (q *FakeQuerier) UpdateCryptoKeyDeletesAt(_ context.Context, arg database.UpdateCryptoKeyDeletesAtParams) (database.CryptoKey, error) { err := validateDatabaseType(arg) if err != nil { diff --git a/coderd/database/dbmetrics/querymetrics.go b/coderd/database/dbmetrics/querymetrics.go index b76d70c764cf6..128e741da1d76 100644 --- a/coderd/database/dbmetrics/querymetrics.go +++ b/coderd/database/dbmetrics/querymetrics.go @@ -249,6 +249,13 @@ func (m queryMetricsStore) DeleteApplicationConnectAPIKeysByUserID(ctx context.C return err } +func (m queryMetricsStore) DeleteChat(ctx context.Context, id uuid.UUID) error { + start := time.Now() + r0 := m.s.DeleteChat(ctx, id) + m.queryLatencies.WithLabelValues("DeleteChat").Observe(time.Since(start).Seconds()) + return r0 +} + func (m queryMetricsStore) DeleteCoordinator(ctx context.Context, id uuid.UUID) error { start := time.Now() r0 := m.s.DeleteCoordinator(ctx, id) @@ -627,6 +634,27 @@ func (m queryMetricsStore) GetAuthorizationUserRoles(ctx context.Context, userID return row, err } +func (m queryMetricsStore) GetChatByID(ctx context.Context, id uuid.UUID) (database.Chat, error) { + start := time.Now() + r0, r1 := m.s.GetChatByID(ctx, id) + m.queryLatencies.WithLabelValues("GetChatByID").Observe(time.Since(start).Seconds()) + return r0, r1 +} + +func (m queryMetricsStore) GetChatMessagesByChatID(ctx context.Context, chatID uuid.UUID) ([]database.ChatMessage, error) { + start := time.Now() + r0, r1 := m.s.GetChatMessagesByChatID(ctx, chatID) + m.queryLatencies.WithLabelValues("GetChatMessagesByChatID").Observe(time.Since(start).Seconds()) + return r0, r1 +} + +func (m queryMetricsStore) GetChatsByOwnerID(ctx context.Context, ownerID uuid.UUID) ([]database.Chat, error) { + start := time.Now() + r0, r1 := m.s.GetChatsByOwnerID(ctx, ownerID) + m.queryLatencies.WithLabelValues("GetChatsByOwnerID").Observe(time.Since(start).Seconds()) + return r0, r1 +} + func (m queryMetricsStore) GetCoordinatorResumeTokenSigningKey(ctx context.Context) (string, error) { start := time.Now() r0, r1 := m.s.GetCoordinatorResumeTokenSigningKey(ctx) @@ -1992,6 +2020,20 @@ func (m queryMetricsStore) InsertAuditLog(ctx context.Context, arg database.Inse return log, err } +func (m queryMetricsStore) InsertChat(ctx context.Context, arg database.InsertChatParams) (database.Chat, error) { + start := time.Now() + r0, r1 := m.s.InsertChat(ctx, arg) + m.queryLatencies.WithLabelValues("InsertChat").Observe(time.Since(start).Seconds()) + return r0, r1 +} + +func (m queryMetricsStore) InsertChatMessages(ctx context.Context, arg database.InsertChatMessagesParams) ([]database.ChatMessage, error) { + start := time.Now() + r0, r1 := m.s.InsertChatMessages(ctx, arg) + m.queryLatencies.WithLabelValues("InsertChatMessages").Observe(time.Since(start).Seconds()) + return r0, r1 +} + func (m queryMetricsStore) InsertCryptoKey(ctx context.Context, arg database.InsertCryptoKeyParams) (database.CryptoKey, error) { start := time.Now() key, err := m.s.InsertCryptoKey(ctx, arg) @@ -2517,6 +2559,13 @@ func (m queryMetricsStore) UpdateAPIKeyByID(ctx context.Context, arg database.Up return err } +func (m queryMetricsStore) UpdateChatByID(ctx context.Context, arg database.UpdateChatByIDParams) error { + start := time.Now() + r0 := m.s.UpdateChatByID(ctx, arg) + m.queryLatencies.WithLabelValues("UpdateChatByID").Observe(time.Since(start).Seconds()) + return r0 +} + func (m queryMetricsStore) UpdateCryptoKeyDeletesAt(ctx context.Context, arg database.UpdateCryptoKeyDeletesAtParams) (database.CryptoKey, error) { start := time.Now() key, err := m.s.UpdateCryptoKeyDeletesAt(ctx, arg) diff --git a/coderd/database/dbmock/dbmock.go b/coderd/database/dbmock/dbmock.go index 10adfd7c5a408..17b263dfb2e07 100644 --- a/coderd/database/dbmock/dbmock.go +++ b/coderd/database/dbmock/dbmock.go @@ -376,6 +376,20 @@ func (mr *MockStoreMockRecorder) DeleteApplicationConnectAPIKeysByUserID(ctx, us return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteApplicationConnectAPIKeysByUserID", reflect.TypeOf((*MockStore)(nil).DeleteApplicationConnectAPIKeysByUserID), ctx, userID) } +// DeleteChat mocks base method. +func (m *MockStore) DeleteChat(ctx context.Context, id uuid.UUID) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteChat", ctx, id) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteChat indicates an expected call of DeleteChat. +func (mr *MockStoreMockRecorder) DeleteChat(ctx, id any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteChat", reflect.TypeOf((*MockStore)(nil).DeleteChat), ctx, id) +} + // DeleteCoordinator mocks base method. func (m *MockStore) DeleteCoordinator(ctx context.Context, id uuid.UUID) error { m.ctrl.T.Helper() @@ -1234,6 +1248,51 @@ func (mr *MockStoreMockRecorder) GetAuthorizedWorkspacesAndAgentsByOwnerID(ctx, return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAuthorizedWorkspacesAndAgentsByOwnerID", reflect.TypeOf((*MockStore)(nil).GetAuthorizedWorkspacesAndAgentsByOwnerID), ctx, ownerID, prepared) } +// GetChatByID mocks base method. +func (m *MockStore) GetChatByID(ctx context.Context, id uuid.UUID) (database.Chat, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetChatByID", ctx, id) + ret0, _ := ret[0].(database.Chat) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetChatByID indicates an expected call of GetChatByID. +func (mr *MockStoreMockRecorder) GetChatByID(ctx, id any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetChatByID", reflect.TypeOf((*MockStore)(nil).GetChatByID), ctx, id) +} + +// GetChatMessagesByChatID mocks base method. +func (m *MockStore) GetChatMessagesByChatID(ctx context.Context, chatID uuid.UUID) ([]database.ChatMessage, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetChatMessagesByChatID", ctx, chatID) + ret0, _ := ret[0].([]database.ChatMessage) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetChatMessagesByChatID indicates an expected call of GetChatMessagesByChatID. +func (mr *MockStoreMockRecorder) GetChatMessagesByChatID(ctx, chatID any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetChatMessagesByChatID", reflect.TypeOf((*MockStore)(nil).GetChatMessagesByChatID), ctx, chatID) +} + +// GetChatsByOwnerID mocks base method. +func (m *MockStore) GetChatsByOwnerID(ctx context.Context, ownerID uuid.UUID) ([]database.Chat, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetChatsByOwnerID", ctx, ownerID) + ret0, _ := ret[0].([]database.Chat) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetChatsByOwnerID indicates an expected call of GetChatsByOwnerID. +func (mr *MockStoreMockRecorder) GetChatsByOwnerID(ctx, ownerID any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetChatsByOwnerID", reflect.TypeOf((*MockStore)(nil).GetChatsByOwnerID), ctx, ownerID) +} + // GetCoordinatorResumeTokenSigningKey mocks base method. func (m *MockStore) GetCoordinatorResumeTokenSigningKey(ctx context.Context) (string, error) { m.ctrl.T.Helper() @@ -4203,6 +4262,36 @@ func (mr *MockStoreMockRecorder) InsertAuditLog(ctx, arg any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InsertAuditLog", reflect.TypeOf((*MockStore)(nil).InsertAuditLog), ctx, arg) } +// InsertChat mocks base method. +func (m *MockStore) InsertChat(ctx context.Context, arg database.InsertChatParams) (database.Chat, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "InsertChat", ctx, arg) + ret0, _ := ret[0].(database.Chat) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// InsertChat indicates an expected call of InsertChat. +func (mr *MockStoreMockRecorder) InsertChat(ctx, arg any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InsertChat", reflect.TypeOf((*MockStore)(nil).InsertChat), ctx, arg) +} + +// InsertChatMessages mocks base method. +func (m *MockStore) InsertChatMessages(ctx context.Context, arg database.InsertChatMessagesParams) ([]database.ChatMessage, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "InsertChatMessages", ctx, arg) + ret0, _ := ret[0].([]database.ChatMessage) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// InsertChatMessages indicates an expected call of InsertChatMessages. +func (mr *MockStoreMockRecorder) InsertChatMessages(ctx, arg any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InsertChatMessages", reflect.TypeOf((*MockStore)(nil).InsertChatMessages), ctx, arg) +} + // InsertCryptoKey mocks base method. func (m *MockStore) InsertCryptoKey(ctx context.Context, arg database.InsertCryptoKeyParams) (database.CryptoKey, error) { m.ctrl.T.Helper() @@ -5337,6 +5426,20 @@ func (mr *MockStoreMockRecorder) UpdateAPIKeyByID(ctx, arg any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateAPIKeyByID", reflect.TypeOf((*MockStore)(nil).UpdateAPIKeyByID), ctx, arg) } +// UpdateChatByID mocks base method. +func (m *MockStore) UpdateChatByID(ctx context.Context, arg database.UpdateChatByIDParams) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateChatByID", ctx, arg) + ret0, _ := ret[0].(error) + return ret0 +} + +// UpdateChatByID indicates an expected call of UpdateChatByID. +func (mr *MockStoreMockRecorder) UpdateChatByID(ctx, arg any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateChatByID", reflect.TypeOf((*MockStore)(nil).UpdateChatByID), ctx, arg) +} + // UpdateCryptoKeyDeletesAt mocks base method. func (m *MockStore) UpdateCryptoKeyDeletesAt(ctx context.Context, arg database.UpdateCryptoKeyDeletesAtParams) (database.CryptoKey, error) { m.ctrl.T.Helper() diff --git a/coderd/database/dump.sql b/coderd/database/dump.sql index 968b6a24d4bf8..9ce3b0171d2d4 100644 --- a/coderd/database/dump.sql +++ b/coderd/database/dump.sql @@ -755,6 +755,32 @@ CREATE TABLE audit_logs ( resource_icon text NOT NULL ); +CREATE TABLE chat_messages ( + id bigint NOT NULL, + chat_id uuid NOT NULL, + created_at timestamp with time zone DEFAULT now() NOT NULL, + model text NOT NULL, + provider text NOT NULL, + content jsonb NOT NULL +); + +CREATE SEQUENCE chat_messages_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + +ALTER SEQUENCE chat_messages_id_seq OWNED BY chat_messages.id; + +CREATE TABLE chats ( + id uuid DEFAULT gen_random_uuid() NOT NULL, + owner_id uuid NOT NULL, + created_at timestamp with time zone DEFAULT now() NOT NULL, + updated_at timestamp with time zone DEFAULT now() NOT NULL, + title text NOT NULL +); + CREATE TABLE crypto_keys ( feature crypto_key_feature NOT NULL, sequence integer NOT NULL, @@ -2195,6 +2221,8 @@ CREATE VIEW workspaces_expanded AS COMMENT ON VIEW workspaces_expanded IS 'Joins in the display name information such as username, avatar, and organization name.'; +ALTER TABLE ONLY chat_messages ALTER COLUMN id SET DEFAULT nextval('chat_messages_id_seq'::regclass); + ALTER TABLE ONLY licenses ALTER COLUMN id SET DEFAULT nextval('licenses_id_seq'::regclass); ALTER TABLE ONLY provisioner_job_logs ALTER COLUMN id SET DEFAULT nextval('provisioner_job_logs_id_seq'::regclass); @@ -2216,6 +2244,12 @@ ALTER TABLE ONLY api_keys ALTER TABLE ONLY audit_logs ADD CONSTRAINT audit_logs_pkey PRIMARY KEY (id); +ALTER TABLE ONLY chat_messages + ADD CONSTRAINT chat_messages_pkey PRIMARY KEY (id); + +ALTER TABLE ONLY chats + ADD CONSTRAINT chats_pkey PRIMARY KEY (id); + ALTER TABLE ONLY crypto_keys ADD CONSTRAINT crypto_keys_pkey PRIMARY KEY (feature, sequence); @@ -2699,6 +2733,12 @@ CREATE TRIGGER user_status_change_trigger AFTER INSERT OR UPDATE ON users FOR EA ALTER TABLE ONLY api_keys ADD CONSTRAINT api_keys_user_id_uuid_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; +ALTER TABLE ONLY chat_messages + ADD CONSTRAINT chat_messages_chat_id_fkey FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE; + +ALTER TABLE ONLY chats + ADD CONSTRAINT chats_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES users(id) ON DELETE CASCADE; + ALTER TABLE ONLY crypto_keys ADD CONSTRAINT crypto_keys_secret_key_id_fkey FOREIGN KEY (secret_key_id) REFERENCES dbcrypt_keys(active_key_digest); diff --git a/coderd/database/foreign_key_constraint.go b/coderd/database/foreign_key_constraint.go index 3f5ce963e6fdb..0db3e9522547e 100644 --- a/coderd/database/foreign_key_constraint.go +++ b/coderd/database/foreign_key_constraint.go @@ -7,6 +7,8 @@ type ForeignKeyConstraint string // ForeignKeyConstraint enums. const ( ForeignKeyAPIKeysUserIDUUID ForeignKeyConstraint = "api_keys_user_id_uuid_fkey" // ALTER TABLE ONLY api_keys ADD CONSTRAINT api_keys_user_id_uuid_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; + ForeignKeyChatMessagesChatID ForeignKeyConstraint = "chat_messages_chat_id_fkey" // ALTER TABLE ONLY chat_messages ADD CONSTRAINT chat_messages_chat_id_fkey FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE; + ForeignKeyChatsOwnerID ForeignKeyConstraint = "chats_owner_id_fkey" // ALTER TABLE ONLY chats ADD CONSTRAINT chats_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES users(id) ON DELETE CASCADE; ForeignKeyCryptoKeysSecretKeyID ForeignKeyConstraint = "crypto_keys_secret_key_id_fkey" // ALTER TABLE ONLY crypto_keys ADD CONSTRAINT crypto_keys_secret_key_id_fkey FOREIGN KEY (secret_key_id) REFERENCES dbcrypt_keys(active_key_digest); ForeignKeyGitAuthLinksOauthAccessTokenKeyID ForeignKeyConstraint = "git_auth_links_oauth_access_token_key_id_fkey" // ALTER TABLE ONLY external_auth_links ADD CONSTRAINT git_auth_links_oauth_access_token_key_id_fkey FOREIGN KEY (oauth_access_token_key_id) REFERENCES dbcrypt_keys(active_key_digest); ForeignKeyGitAuthLinksOauthRefreshTokenKeyID ForeignKeyConstraint = "git_auth_links_oauth_refresh_token_key_id_fkey" // ALTER TABLE ONLY external_auth_links ADD CONSTRAINT git_auth_links_oauth_refresh_token_key_id_fkey FOREIGN KEY (oauth_refresh_token_key_id) REFERENCES dbcrypt_keys(active_key_digest); diff --git a/coderd/database/migrations/000319_chat.down.sql b/coderd/database/migrations/000319_chat.down.sql new file mode 100644 index 0000000000000..9bab993f500f5 --- /dev/null +++ b/coderd/database/migrations/000319_chat.down.sql @@ -0,0 +1,3 @@ +DROP TABLE IF EXISTS chat_messages; + +DROP TABLE IF EXISTS chats; diff --git a/coderd/database/migrations/000319_chat.up.sql b/coderd/database/migrations/000319_chat.up.sql new file mode 100644 index 0000000000000..a53942239c9e2 --- /dev/null +++ b/coderd/database/migrations/000319_chat.up.sql @@ -0,0 +1,17 @@ +CREATE TABLE IF NOT EXISTS chats ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + owner_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), + title TEXT NOT NULL +); + +CREATE TABLE IF NOT EXISTS chat_messages ( + -- BIGSERIAL is auto-incrementing so we know the exact order of messages. + id BIGSERIAL PRIMARY KEY, + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), + model TEXT NOT NULL, + provider TEXT NOT NULL, + content JSONB NOT NULL +); diff --git a/coderd/database/migrations/testdata/fixtures/000319_chat.up.sql b/coderd/database/migrations/testdata/fixtures/000319_chat.up.sql new file mode 100644 index 0000000000000..123a62c4eb722 --- /dev/null +++ b/coderd/database/migrations/testdata/fixtures/000319_chat.up.sql @@ -0,0 +1,6 @@ +INSERT INTO chats (id, owner_id, created_at, updated_at, title) VALUES +('00000000-0000-0000-0000-000000000001', '0ed9befc-4911-4ccf-a8e2-559bf72daa94', '2023-10-01 12:00:00+00', '2023-10-01 12:00:00+00', 'Test Chat 1'); + +INSERT INTO chat_messages (id, chat_id, created_at, model, provider, content) VALUES +(1, '00000000-0000-0000-0000-000000000001', '2023-10-01 12:00:00+00', 'annie-oakley', 'cowboy-coder', '{"role":"user","content":"Hello"}'), +(2, '00000000-0000-0000-0000-000000000001', '2023-10-01 12:01:00+00', 'annie-oakley', 'cowboy-coder', '{"role":"assistant","content":"Howdy pardner! What can I do ya for?"}'); diff --git a/coderd/database/modelmethods.go b/coderd/database/modelmethods.go index 896fdd4af17e9..b3f6deed9eff0 100644 --- a/coderd/database/modelmethods.go +++ b/coderd/database/modelmethods.go @@ -568,3 +568,8 @@ func (m WorkspaceAgentVolumeResourceMonitor) Debounce( return m.DebouncedUntil, false } + +func (c Chat) RBACObject() rbac.Object { + return rbac.ResourceChat.WithID(c.ID). + WithOwner(c.OwnerID.String()) +} diff --git a/coderd/database/models.go b/coderd/database/models.go index f817ff2712d54..c8ac71e8b9398 100644 --- a/coderd/database/models.go +++ b/coderd/database/models.go @@ -2570,6 +2570,23 @@ type AuditLog struct { ResourceIcon string `db:"resource_icon" json:"resource_icon"` } +type Chat struct { + ID uuid.UUID `db:"id" json:"id"` + OwnerID uuid.UUID `db:"owner_id" json:"owner_id"` + CreatedAt time.Time `db:"created_at" json:"created_at"` + UpdatedAt time.Time `db:"updated_at" json:"updated_at"` + Title string `db:"title" json:"title"` +} + +type ChatMessage struct { + ID int64 `db:"id" json:"id"` + ChatID uuid.UUID `db:"chat_id" json:"chat_id"` + CreatedAt time.Time `db:"created_at" json:"created_at"` + Model string `db:"model" json:"model"` + Provider string `db:"provider" json:"provider"` + Content json.RawMessage `db:"content" json:"content"` +} + type CryptoKey struct { Feature CryptoKeyFeature `db:"feature" json:"feature"` Sequence int32 `db:"sequence" json:"sequence"` diff --git a/coderd/database/querier.go b/coderd/database/querier.go index 9fbfbde410d40..d0f74ee609724 100644 --- a/coderd/database/querier.go +++ b/coderd/database/querier.go @@ -79,6 +79,7 @@ type sqlcQuerier interface { // be recreated. DeleteAllWebpushSubscriptions(ctx context.Context) error DeleteApplicationConnectAPIKeysByUserID(ctx context.Context, userID uuid.UUID) error + DeleteChat(ctx context.Context, id uuid.UUID) error DeleteCoordinator(ctx context.Context, id uuid.UUID) error DeleteCryptoKey(ctx context.Context, arg DeleteCryptoKeyParams) (CryptoKey, error) DeleteCustomRole(ctx context.Context, arg DeleteCustomRoleParams) error @@ -151,6 +152,9 @@ type sqlcQuerier interface { // This function returns roles for authorization purposes. Implied member roles // are included. GetAuthorizationUserRoles(ctx context.Context, userID uuid.UUID) (GetAuthorizationUserRolesRow, error) + GetChatByID(ctx context.Context, id uuid.UUID) (Chat, error) + GetChatMessagesByChatID(ctx context.Context, chatID uuid.UUID) ([]ChatMessage, error) + GetChatsByOwnerID(ctx context.Context, ownerID uuid.UUID) ([]Chat, error) GetCoordinatorResumeTokenSigningKey(ctx context.Context) (string, error) GetCryptoKeyByFeatureAndSequence(ctx context.Context, arg GetCryptoKeyByFeatureAndSequenceParams) (CryptoKey, error) GetCryptoKeys(ctx context.Context) ([]CryptoKey, error) @@ -447,6 +451,8 @@ type sqlcQuerier interface { // every member of the org. InsertAllUsersGroup(ctx context.Context, organizationID uuid.UUID) (Group, error) InsertAuditLog(ctx context.Context, arg InsertAuditLogParams) (AuditLog, error) + InsertChat(ctx context.Context, arg InsertChatParams) (Chat, error) + InsertChatMessages(ctx context.Context, arg InsertChatMessagesParams) ([]ChatMessage, error) InsertCryptoKey(ctx context.Context, arg InsertCryptoKeyParams) (CryptoKey, error) InsertCustomRole(ctx context.Context, arg InsertCustomRoleParams) (CustomRole, error) InsertDBCryptKey(ctx context.Context, arg InsertDBCryptKeyParams) error @@ -540,6 +546,7 @@ type sqlcQuerier interface { UnarchiveTemplateVersion(ctx context.Context, arg UnarchiveTemplateVersionParams) error UnfavoriteWorkspace(ctx context.Context, id uuid.UUID) error UpdateAPIKeyByID(ctx context.Context, arg UpdateAPIKeyByIDParams) error + UpdateChatByID(ctx context.Context, arg UpdateChatByIDParams) error UpdateCryptoKeyDeletesAt(ctx context.Context, arg UpdateCryptoKeyDeletesAtParams) (CryptoKey, error) UpdateCustomRole(ctx context.Context, arg UpdateCustomRoleParams) (CustomRole, error) UpdateExternalAuthLink(ctx context.Context, arg UpdateExternalAuthLinkParams) (ExternalAuthLink, error) diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index 3908dab715e31..cd5b297c85e07 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -766,6 +766,207 @@ func (q *sqlQuerier) InsertAuditLog(ctx context.Context, arg InsertAuditLogParam return i, err } +const deleteChat = `-- name: DeleteChat :exec +DELETE FROM chats WHERE id = $1 +` + +func (q *sqlQuerier) DeleteChat(ctx context.Context, id uuid.UUID) error { + _, err := q.db.ExecContext(ctx, deleteChat, id) + return err +} + +const getChatByID = `-- name: GetChatByID :one +SELECT id, owner_id, created_at, updated_at, title FROM chats +WHERE id = $1 +` + +func (q *sqlQuerier) GetChatByID(ctx context.Context, id uuid.UUID) (Chat, error) { + row := q.db.QueryRowContext(ctx, getChatByID, id) + var i Chat + err := row.Scan( + &i.ID, + &i.OwnerID, + &i.CreatedAt, + &i.UpdatedAt, + &i.Title, + ) + return i, err +} + +const getChatMessagesByChatID = `-- name: GetChatMessagesByChatID :many +SELECT id, chat_id, created_at, model, provider, content FROM chat_messages +WHERE chat_id = $1 +ORDER BY created_at ASC +` + +func (q *sqlQuerier) GetChatMessagesByChatID(ctx context.Context, chatID uuid.UUID) ([]ChatMessage, error) { + rows, err := q.db.QueryContext(ctx, getChatMessagesByChatID, chatID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []ChatMessage + for rows.Next() { + var i ChatMessage + if err := rows.Scan( + &i.ID, + &i.ChatID, + &i.CreatedAt, + &i.Model, + &i.Provider, + &i.Content, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getChatsByOwnerID = `-- name: GetChatsByOwnerID :many +SELECT id, owner_id, created_at, updated_at, title FROM chats +WHERE owner_id = $1 +ORDER BY created_at DESC +` + +func (q *sqlQuerier) GetChatsByOwnerID(ctx context.Context, ownerID uuid.UUID) ([]Chat, error) { + rows, err := q.db.QueryContext(ctx, getChatsByOwnerID, ownerID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Chat + for rows.Next() { + var i Chat + if err := rows.Scan( + &i.ID, + &i.OwnerID, + &i.CreatedAt, + &i.UpdatedAt, + &i.Title, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const insertChat = `-- name: InsertChat :one +INSERT INTO chats (owner_id, created_at, updated_at, title) +VALUES ($1, $2, $3, $4) +RETURNING id, owner_id, created_at, updated_at, title +` + +type InsertChatParams struct { + OwnerID uuid.UUID `db:"owner_id" json:"owner_id"` + CreatedAt time.Time `db:"created_at" json:"created_at"` + UpdatedAt time.Time `db:"updated_at" json:"updated_at"` + Title string `db:"title" json:"title"` +} + +func (q *sqlQuerier) InsertChat(ctx context.Context, arg InsertChatParams) (Chat, error) { + row := q.db.QueryRowContext(ctx, insertChat, + arg.OwnerID, + arg.CreatedAt, + arg.UpdatedAt, + arg.Title, + ) + var i Chat + err := row.Scan( + &i.ID, + &i.OwnerID, + &i.CreatedAt, + &i.UpdatedAt, + &i.Title, + ) + return i, err +} + +const insertChatMessages = `-- name: InsertChatMessages :many +INSERT INTO chat_messages (chat_id, created_at, model, provider, content) +SELECT + $1 :: uuid AS chat_id, + $2 :: timestamptz AS created_at, + $3 :: VARCHAR(127) AS model, + $4 :: VARCHAR(127) AS provider, + jsonb_array_elements($5 :: jsonb) AS content +RETURNING chat_messages.id, chat_messages.chat_id, chat_messages.created_at, chat_messages.model, chat_messages.provider, chat_messages.content +` + +type InsertChatMessagesParams struct { + ChatID uuid.UUID `db:"chat_id" json:"chat_id"` + CreatedAt time.Time `db:"created_at" json:"created_at"` + Model string `db:"model" json:"model"` + Provider string `db:"provider" json:"provider"` + Content json.RawMessage `db:"content" json:"content"` +} + +func (q *sqlQuerier) InsertChatMessages(ctx context.Context, arg InsertChatMessagesParams) ([]ChatMessage, error) { + rows, err := q.db.QueryContext(ctx, insertChatMessages, + arg.ChatID, + arg.CreatedAt, + arg.Model, + arg.Provider, + arg.Content, + ) + if err != nil { + return nil, err + } + defer rows.Close() + var items []ChatMessage + for rows.Next() { + var i ChatMessage + if err := rows.Scan( + &i.ID, + &i.ChatID, + &i.CreatedAt, + &i.Model, + &i.Provider, + &i.Content, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const updateChatByID = `-- name: UpdateChatByID :exec +UPDATE chats +SET title = $2, updated_at = $3 +WHERE id = $1 +` + +type UpdateChatByIDParams struct { + ID uuid.UUID `db:"id" json:"id"` + Title string `db:"title" json:"title"` + UpdatedAt time.Time `db:"updated_at" json:"updated_at"` +} + +func (q *sqlQuerier) UpdateChatByID(ctx context.Context, arg UpdateChatByIDParams) error { + _, err := q.db.ExecContext(ctx, updateChatByID, arg.ID, arg.Title, arg.UpdatedAt) + return err +} + const deleteCryptoKey = `-- name: DeleteCryptoKey :one UPDATE crypto_keys SET secret = NULL, secret_key_id = NULL diff --git a/coderd/database/queries/chat.sql b/coderd/database/queries/chat.sql new file mode 100644 index 0000000000000..68f662d8a886b --- /dev/null +++ b/coderd/database/queries/chat.sql @@ -0,0 +1,36 @@ +-- name: InsertChat :one +INSERT INTO chats (owner_id, created_at, updated_at, title) +VALUES ($1, $2, $3, $4) +RETURNING *; + +-- name: UpdateChatByID :exec +UPDATE chats +SET title = $2, updated_at = $3 +WHERE id = $1; + +-- name: GetChatsByOwnerID :many +SELECT * FROM chats +WHERE owner_id = $1 +ORDER BY created_at DESC; + +-- name: GetChatByID :one +SELECT * FROM chats +WHERE id = $1; + +-- name: InsertChatMessages :many +INSERT INTO chat_messages (chat_id, created_at, model, provider, content) +SELECT + @chat_id :: uuid AS chat_id, + @created_at :: timestamptz AS created_at, + @model :: VARCHAR(127) AS model, + @provider :: VARCHAR(127) AS provider, + jsonb_array_elements(@content :: jsonb) AS content +RETURNING chat_messages.*; + +-- name: GetChatMessagesByChatID :many +SELECT * FROM chat_messages +WHERE chat_id = $1 +ORDER BY created_at ASC; + +-- name: DeleteChat :exec +DELETE FROM chats WHERE id = $1; diff --git a/coderd/database/unique_constraint.go b/coderd/database/unique_constraint.go index 2b91f38c88d42..4c9c8cedcba23 100644 --- a/coderd/database/unique_constraint.go +++ b/coderd/database/unique_constraint.go @@ -9,6 +9,8 @@ const ( UniqueAgentStatsPkey UniqueConstraint = "agent_stats_pkey" // ALTER TABLE ONLY workspace_agent_stats ADD CONSTRAINT agent_stats_pkey PRIMARY KEY (id); UniqueAPIKeysPkey UniqueConstraint = "api_keys_pkey" // ALTER TABLE ONLY api_keys ADD CONSTRAINT api_keys_pkey PRIMARY KEY (id); UniqueAuditLogsPkey UniqueConstraint = "audit_logs_pkey" // ALTER TABLE ONLY audit_logs ADD CONSTRAINT audit_logs_pkey PRIMARY KEY (id); + UniqueChatMessagesPkey UniqueConstraint = "chat_messages_pkey" // ALTER TABLE ONLY chat_messages ADD CONSTRAINT chat_messages_pkey PRIMARY KEY (id); + UniqueChatsPkey UniqueConstraint = "chats_pkey" // ALTER TABLE ONLY chats ADD CONSTRAINT chats_pkey PRIMARY KEY (id); UniqueCryptoKeysPkey UniqueConstraint = "crypto_keys_pkey" // ALTER TABLE ONLY crypto_keys ADD CONSTRAINT crypto_keys_pkey PRIMARY KEY (feature, sequence); UniqueCustomRolesUniqueKey UniqueConstraint = "custom_roles_unique_key" // ALTER TABLE ONLY custom_roles ADD CONSTRAINT custom_roles_unique_key UNIQUE (name, organization_id); UniqueDbcryptKeysActiveKeyDigestKey UniqueConstraint = "dbcrypt_keys_active_key_digest_key" // ALTER TABLE ONLY dbcrypt_keys ADD CONSTRAINT dbcrypt_keys_active_key_digest_key UNIQUE (active_key_digest); diff --git a/coderd/deployment.go b/coderd/deployment.go index 4c78563a80456..60988aeb2ce5a 100644 --- a/coderd/deployment.go +++ b/coderd/deployment.go @@ -1,8 +1,11 @@ package coderd import ( + "context" "net/http" + "github.com/kylecarbs/aisdk-go" + "github.com/coder/coder/v2/coderd/httpapi" "github.com/coder/coder/v2/coderd/rbac" "github.com/coder/coder/v2/coderd/rbac/policy" @@ -84,3 +87,25 @@ func buildInfoHandler(resp codersdk.BuildInfoResponse) http.HandlerFunc { func (api *API) sshConfig(rw http.ResponseWriter, r *http.Request) { httpapi.Write(r.Context(), rw, http.StatusOK, api.SSHConfig) } + +type LanguageModel struct { + codersdk.LanguageModel + Provider func(ctx context.Context, messages []aisdk.Message, thinking bool) (aisdk.DataStream, error) +} + +// @Summary Get language models +// @ID get-language-models +// @Security CoderSessionToken +// @Produce json +// @Tags General +// @Success 200 {object} codersdk.LanguageModelConfig +// @Router /deployment/llms [get] +func (api *API) deploymentLLMs(rw http.ResponseWriter, r *http.Request) { + models := make([]codersdk.LanguageModel, 0, len(api.LanguageModels)) + for _, model := range api.LanguageModels { + models = append(models, model.LanguageModel) + } + httpapi.Write(r.Context(), rw, http.StatusOK, codersdk.LanguageModelConfig{ + Models: models, + }) +} diff --git a/coderd/httpmw/chat.go b/coderd/httpmw/chat.go new file mode 100644 index 0000000000000..c92fa5038ab22 --- /dev/null +++ b/coderd/httpmw/chat.go @@ -0,0 +1,59 @@ +package httpmw + +import ( + "context" + "net/http" + + "github.com/go-chi/chi/v5" + "github.com/google/uuid" + + "github.com/coder/coder/v2/coderd/database" + "github.com/coder/coder/v2/coderd/httpapi" + "github.com/coder/coder/v2/codersdk" +) + +type chatContextKey struct{} + +func ChatParam(r *http.Request) database.Chat { + chat, ok := r.Context().Value(chatContextKey{}).(database.Chat) + if !ok { + panic("developer error: chat param middleware not provided") + } + return chat +} + +func ExtractChatParam(db database.Store) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + ctx := r.Context() + arg := chi.URLParam(r, "chat") + if arg == "" { + httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ + Message: "\"chat\" must be provided.", + }) + return + } + chatID, err := uuid.Parse(arg) + if err != nil { + httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ + Message: "Invalid chat ID.", + }) + return + } + chat, err := db.GetChatByID(ctx, chatID) + if httpapi.Is404Error(err) { + httpapi.ResourceNotFound(rw) + return + } + if err != nil { + httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ + Message: "Failed to get chat.", + Detail: err.Error(), + }) + return + } + ctx = context.WithValue(ctx, chatContextKey{}, chat) + next.ServeHTTP(rw, r.WithContext(ctx)) + }) + } +} diff --git a/coderd/httpmw/chat_test.go b/coderd/httpmw/chat_test.go new file mode 100644 index 0000000000000..a8bad05f33797 --- /dev/null +++ b/coderd/httpmw/chat_test.go @@ -0,0 +1,150 @@ +package httpmw_test + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/go-chi/chi/v5" + "github.com/google/uuid" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/coder/coder/v2/coderd/database" + "github.com/coder/coder/v2/coderd/database/dbgen" + "github.com/coder/coder/v2/coderd/database/dbmem" + "github.com/coder/coder/v2/coderd/database/dbtime" + "github.com/coder/coder/v2/coderd/httpmw" + "github.com/coder/coder/v2/codersdk" +) + +func TestExtractChat(t *testing.T) { + t.Parallel() + + setupAuthentication := func(db database.Store) (*http.Request, database.User) { + r := httptest.NewRequest("GET", "/", nil) + + user := dbgen.User(t, db, database.User{ + ID: uuid.New(), + }) + _, token := dbgen.APIKey(t, db, database.APIKey{ + UserID: user.ID, + }) + r.Header.Set(codersdk.SessionTokenHeader, token) + r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, chi.NewRouteContext())) + return r, user + } + + t.Run("None", func(t *testing.T) { + t.Parallel() + var ( + db = dbmem.New() + rw = httptest.NewRecorder() + r, _ = setupAuthentication(db) + rtr = chi.NewRouter() + ) + rtr.Use( + httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{ + DB: db, + RedirectToLogin: false, + }), + httpmw.ExtractChatParam(db), + ) + rtr.Get("/", nil) + rtr.ServeHTTP(rw, r) + res := rw.Result() + defer res.Body.Close() + require.Equal(t, http.StatusBadRequest, res.StatusCode) + }) + + t.Run("InvalidUUID", func(t *testing.T) { + t.Parallel() + var ( + db = dbmem.New() + rw = httptest.NewRecorder() + r, _ = setupAuthentication(db) + rtr = chi.NewRouter() + ) + chi.RouteContext(r.Context()).URLParams.Add("chat", "not-a-uuid") + rtr.Use( + httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{ + DB: db, + RedirectToLogin: false, + }), + httpmw.ExtractChatParam(db), + ) + rtr.Get("/", nil) + rtr.ServeHTTP(rw, r) + res := rw.Result() + defer res.Body.Close() + require.Equal(t, http.StatusBadRequest, res.StatusCode) // Changed from NotFound in org test to BadRequest as per chat.go + }) + + t.Run("NotFound", func(t *testing.T) { + t.Parallel() + var ( + db = dbmem.New() + rw = httptest.NewRecorder() + r, _ = setupAuthentication(db) + rtr = chi.NewRouter() + ) + chi.RouteContext(r.Context()).URLParams.Add("chat", uuid.NewString()) + rtr.Use( + httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{ + DB: db, + RedirectToLogin: false, + }), + httpmw.ExtractChatParam(db), + ) + rtr.Get("/", nil) + rtr.ServeHTTP(rw, r) + res := rw.Result() + defer res.Body.Close() + require.Equal(t, http.StatusNotFound, res.StatusCode) + }) + + t.Run("Success", func(t *testing.T) { + t.Parallel() + var ( + db = dbmem.New() + rw = httptest.NewRecorder() + r, user = setupAuthentication(db) + rtr = chi.NewRouter() + ) + + // Create a test chat + testChat := dbgen.Chat(t, db, database.Chat{ + ID: uuid.New(), + OwnerID: user.ID, + CreatedAt: dbtime.Now(), + UpdatedAt: dbtime.Now(), + Title: "Test Chat", + }) + + rtr.Use( + httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{ + DB: db, + RedirectToLogin: false, + }), + httpmw.ExtractChatParam(db), + ) + rtr.Get("/", func(rw http.ResponseWriter, r *http.Request) { + chat := httpmw.ChatParam(r) + require.NotZero(t, chat) + assert.Equal(t, testChat.ID, chat.ID) + assert.WithinDuration(t, testChat.CreatedAt, chat.CreatedAt, time.Second) + assert.WithinDuration(t, testChat.UpdatedAt, chat.UpdatedAt, time.Second) + assert.Equal(t, testChat.Title, chat.Title) + rw.WriteHeader(http.StatusOK) + }) + + // Try by ID + chi.RouteContext(r.Context()).URLParams.Add("chat", testChat.ID.String()) + rtr.ServeHTTP(rw, r) + res := rw.Result() + defer res.Body.Close() + require.Equal(t, http.StatusOK, res.StatusCode, "by id") + }) +} diff --git a/coderd/rbac/object_gen.go b/coderd/rbac/object_gen.go index 7c0933c4241b0..40b7dc87a56f8 100644 --- a/coderd/rbac/object_gen.go +++ b/coderd/rbac/object_gen.go @@ -54,6 +54,16 @@ var ( Type: "audit_log", } + // ResourceChat + // Valid Actions + // - "ActionCreate" :: create a chat + // - "ActionDelete" :: delete a chat + // - "ActionRead" :: read a chat + // - "ActionUpdate" :: update a chat + ResourceChat = Object{ + Type: "chat", + } + // ResourceCryptoKey // Valid Actions // - "ActionCreate" :: create crypto keys @@ -354,6 +364,7 @@ func AllResources() []Objecter { ResourceAssignOrgRole, ResourceAssignRole, ResourceAuditLog, + ResourceChat, ResourceCryptoKey, ResourceDebugInfo, ResourceDeploymentConfig, diff --git a/coderd/rbac/policy/policy.go b/coderd/rbac/policy/policy.go index 5b661243dc127..35da0892abfdb 100644 --- a/coderd/rbac/policy/policy.go +++ b/coderd/rbac/policy/policy.go @@ -104,6 +104,14 @@ var RBACPermissions = map[string]PermissionDefinition{ ActionRead: actDef("read and use a workspace proxy"), }, }, + "chat": { + Actions: map[Action]ActionDefinition{ + ActionCreate: actDef("create a chat"), + ActionRead: actDef("read a chat"), + ActionDelete: actDef("delete a chat"), + ActionUpdate: actDef("update a chat"), + }, + }, "license": { Actions: map[Action]ActionDefinition{ ActionCreate: actDef("create a license"), diff --git a/coderd/rbac/roles.go b/coderd/rbac/roles.go index 6b99cb4e871a2..56124faee44e2 100644 --- a/coderd/rbac/roles.go +++ b/coderd/rbac/roles.go @@ -299,6 +299,8 @@ func ReloadBuiltinRoles(opts *RoleOptions) { ResourceOrganizationMember.Type: {policy.ActionRead}, // Users can create provisioner daemons scoped to themselves. ResourceProvisionerDaemon.Type: {policy.ActionRead, policy.ActionCreate, policy.ActionRead, policy.ActionUpdate}, + // Users can create, read, update, and delete their own agentic chat messages. + ResourceChat.Type: {policy.ActionCreate, policy.ActionRead, policy.ActionUpdate, policy.ActionDelete}, })..., ), }.withCachedRegoValue() diff --git a/coderd/rbac/roles_test.go b/coderd/rbac/roles_test.go index 1080903637ac5..e90c89914fdec 100644 --- a/coderd/rbac/roles_test.go +++ b/coderd/rbac/roles_test.go @@ -831,6 +831,37 @@ func TestRolePermissions(t *testing.T) { }, }, }, + // Members may read their own chats. + { + Name: "CreateReadUpdateDeleteMyChats", + Actions: []policy.Action{policy.ActionCreate, policy.ActionRead, policy.ActionUpdate, policy.ActionDelete}, + Resource: rbac.ResourceChat.WithOwner(currentUser.String()), + AuthorizeMap: map[bool][]hasAuthSubjects{ + true: {memberMe, orgMemberMe, owner}, + false: { + userAdmin, orgUserAdmin, templateAdmin, + orgAuditor, orgTemplateAdmin, + otherOrgMember, otherOrgAuditor, otherOrgUserAdmin, otherOrgTemplateAdmin, + orgAdmin, otherOrgAdmin, + }, + }, + }, + // Only owners can create, read, update, and delete other users' chats. + { + Name: "CreateReadUpdateDeleteOtherUserChats", + Actions: []policy.Action{policy.ActionCreate, policy.ActionRead, policy.ActionUpdate, policy.ActionDelete}, + Resource: rbac.ResourceChat.WithOwner(uuid.NewString()), // some other user + AuthorizeMap: map[bool][]hasAuthSubjects{ + true: {owner}, + false: { + memberMe, orgMemberMe, + userAdmin, orgUserAdmin, templateAdmin, + orgAuditor, orgTemplateAdmin, + otherOrgMember, otherOrgAuditor, otherOrgUserAdmin, otherOrgTemplateAdmin, + orgAdmin, otherOrgAdmin, + }, + }, + }, } // We expect every permission to be tested above. diff --git a/codersdk/chat.go b/codersdk/chat.go new file mode 100644 index 0000000000000..2093adaff95e8 --- /dev/null +++ b/codersdk/chat.go @@ -0,0 +1,153 @@ +package codersdk + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "time" + + "github.com/google/uuid" + "github.com/kylecarbs/aisdk-go" + "golang.org/x/xerrors" +) + +// CreateChat creates a new chat. +func (c *Client) CreateChat(ctx context.Context) (Chat, error) { + res, err := c.Request(ctx, http.MethodPost, "/api/v2/chats", nil) + if err != nil { + return Chat{}, xerrors.Errorf("execute request: %w", err) + } + if res.StatusCode != http.StatusCreated { + return Chat{}, ReadBodyAsError(res) + } + defer res.Body.Close() + var chat Chat + return chat, json.NewDecoder(res.Body).Decode(&chat) +} + +type Chat struct { + ID uuid.UUID `json:"id" format:"uuid"` + CreatedAt time.Time `json:"created_at" format:"date-time"` + UpdatedAt time.Time `json:"updated_at" format:"date-time"` + Title string `json:"title"` +} + +// ListChats lists all chats. +func (c *Client) ListChats(ctx context.Context) ([]Chat, error) { + res, err := c.Request(ctx, http.MethodGet, "/api/v2/chats", nil) + if err != nil { + return nil, xerrors.Errorf("execute request: %w", err) + } + defer res.Body.Close() + if res.StatusCode != http.StatusOK { + return nil, ReadBodyAsError(res) + } + + var chats []Chat + return chats, json.NewDecoder(res.Body).Decode(&chats) +} + +// Chat returns a chat by ID. +func (c *Client) Chat(ctx context.Context, id uuid.UUID) (Chat, error) { + res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/chats/%s", id), nil) + if err != nil { + return Chat{}, xerrors.Errorf("execute request: %w", err) + } + defer res.Body.Close() + if res.StatusCode != http.StatusOK { + return Chat{}, ReadBodyAsError(res) + } + var chat Chat + return chat, json.NewDecoder(res.Body).Decode(&chat) +} + +// ChatMessages returns the messages of a chat. +func (c *Client) ChatMessages(ctx context.Context, id uuid.UUID) ([]ChatMessage, error) { + res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/chats/%s/messages", id), nil) + if err != nil { + return nil, xerrors.Errorf("execute request: %w", err) + } + defer res.Body.Close() + if res.StatusCode != http.StatusOK { + return nil, ReadBodyAsError(res) + } + var messages []ChatMessage + return messages, json.NewDecoder(res.Body).Decode(&messages) +} + +type ChatMessage = aisdk.Message + +type CreateChatMessageRequest struct { + Model string `json:"model"` + Message ChatMessage `json:"message"` + Thinking bool `json:"thinking"` +} + +// CreateChatMessage creates a new chat message and streams the response. +// If the provided message has a conflicting ID with an existing message, +// it will be overwritten. +func (c *Client) CreateChatMessage(ctx context.Context, id uuid.UUID, req CreateChatMessageRequest) (<-chan aisdk.DataStreamPart, error) { + res, err := c.Request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/chats/%s/messages", id), req) + defer func() { + if res != nil && res.Body != nil { + _ = res.Body.Close() + } + }() + if err != nil { + return nil, xerrors.Errorf("execute request: %w", err) + } + if res.StatusCode != http.StatusOK { + return nil, ReadBodyAsError(res) + } + nextEvent := ServerSentEventReader(ctx, res.Body) + + wc := make(chan aisdk.DataStreamPart, 256) + go func() { + defer close(wc) + defer res.Body.Close() + + for { + select { + case <-ctx.Done(): + return + default: + sse, err := nextEvent() + if err != nil { + return + } + if sse.Type != ServerSentEventTypeData { + continue + } + var part aisdk.DataStreamPart + b, ok := sse.Data.([]byte) + if !ok { + return + } + err = json.Unmarshal(b, &part) + if err != nil { + return + } + select { + case <-ctx.Done(): + return + case wc <- part: + } + } + } + }() + + return wc, nil +} + +func (c *Client) DeleteChat(ctx context.Context, id uuid.UUID) error { + res, err := c.Request(ctx, http.MethodDelete, fmt.Sprintf("/api/v2/chats/%s", id), nil) + if err != nil { + return xerrors.Errorf("execute request: %w", err) + } + defer res.Body.Close() + if res.StatusCode != http.StatusNoContent { + return ReadBodyAsError(res) + } + return nil +} diff --git a/codersdk/deployment.go b/codersdk/deployment.go index 154d7f6cb92e4..0741bf9e3844a 100644 --- a/codersdk/deployment.go +++ b/codersdk/deployment.go @@ -383,6 +383,7 @@ type DeploymentValues struct { DisablePasswordAuth serpent.Bool `json:"disable_password_auth,omitempty" typescript:",notnull"` Support SupportConfig `json:"support,omitempty" typescript:",notnull"` ExternalAuthConfigs serpent.Struct[[]ExternalAuthConfig] `json:"external_auth,omitempty" typescript:",notnull"` + AI serpent.Struct[AIConfig] `json:"ai,omitempty" typescript:",notnull"` SSHConfig SSHConfig `json:"config_ssh,omitempty" typescript:",notnull"` WgtunnelHost serpent.String `json:"wgtunnel_host,omitempty" typescript:",notnull"` DisableOwnerWorkspaceExec serpent.Bool `json:"disable_owner_workspace_exec,omitempty" typescript:",notnull"` @@ -2660,6 +2661,15 @@ Write out the current server config as YAML to stdout.`, Value: &c.Support.Links, Hidden: false, }, + { + // Env handling is done in cli.ReadAIProvidersFromEnv + Name: "AI", + Description: "Configure AI providers.", + YAML: "ai", + Value: &c.AI, + // Hidden because this is experimental. + Hidden: true, + }, { // Env handling is done in cli.ReadGitAuthFromEnvironment Name: "External Auth Providers", @@ -3081,6 +3091,21 @@ Write out the current server config as YAML to stdout.`, return opts } +type AIProviderConfig struct { + // Type is the type of the API provider. + Type string `json:"type" yaml:"type"` + // APIKey is the API key to use for the API provider. + APIKey string `json:"-" yaml:"api_key"` + // Models is the list of models to use for the API provider. + Models []string `json:"models" yaml:"models"` + // BaseURL is the base URL to use for the API provider. + BaseURL string `json:"base_url" yaml:"base_url"` +} + +type AIConfig struct { + Providers []AIProviderConfig `json:"providers,omitempty" yaml:"providers,omitempty"` +} + type SupportConfig struct { Links serpent.Struct[[]LinkConfig] `json:"links" typescript:",notnull"` } @@ -3303,6 +3328,7 @@ const ( ExperimentWebPush Experiment = "web-push" // Enables web push notifications through the browser. ExperimentDynamicParameters Experiment = "dynamic-parameters" // Enables dynamic parameters when creating a workspace. ExperimentWorkspacePrebuilds Experiment = "workspace-prebuilds" // Enables the new workspace prebuilds feature. + ExperimentAgenticChat Experiment = "agentic-chat" // Enables the new agentic AI chat feature. ) // ExperimentsSafe should include all experiments that are safe for @@ -3517,6 +3543,32 @@ func (c *Client) SSHConfiguration(ctx context.Context) (SSHConfigResponse, error return sshConfig, json.NewDecoder(res.Body).Decode(&sshConfig) } +type LanguageModelConfig struct { + Models []LanguageModel `json:"models"` +} + +// LanguageModel is a language model that can be used for chat. +type LanguageModel struct { + // ID is used by the provider to identify the LLM. + ID string `json:"id"` + DisplayName string `json:"display_name"` + // Provider is the provider of the LLM. e.g. openai, anthropic, etc. + Provider string `json:"provider"` +} + +func (c *Client) LanguageModelConfig(ctx context.Context) (LanguageModelConfig, error) { + res, err := c.Request(ctx, http.MethodGet, "/api/v2/deployment/llms", nil) + if err != nil { + return LanguageModelConfig{}, err + } + defer res.Body.Close() + if res.StatusCode != http.StatusOK { + return LanguageModelConfig{}, ReadBodyAsError(res) + } + var llms LanguageModelConfig + return llms, json.NewDecoder(res.Body).Decode(&llms) +} + type CryptoKeyFeature string const ( diff --git a/codersdk/rbacresources_gen.go b/codersdk/rbacresources_gen.go index 7f1bd5da4eb3c..54f65767928d6 100644 --- a/codersdk/rbacresources_gen.go +++ b/codersdk/rbacresources_gen.go @@ -9,6 +9,7 @@ const ( ResourceAssignOrgRole RBACResource = "assign_org_role" ResourceAssignRole RBACResource = "assign_role" ResourceAuditLog RBACResource = "audit_log" + ResourceChat RBACResource = "chat" ResourceCryptoKey RBACResource = "crypto_key" ResourceDebugInfo RBACResource = "debug_info" ResourceDeploymentConfig RBACResource = "deployment_config" @@ -69,6 +70,7 @@ var RBACResourceActions = map[RBACResource][]RBACAction{ ResourceAssignOrgRole: {ActionAssign, ActionCreate, ActionDelete, ActionRead, ActionUnassign, ActionUpdate}, ResourceAssignRole: {ActionAssign, ActionRead, ActionUnassign}, ResourceAuditLog: {ActionCreate, ActionRead}, + ResourceChat: {ActionCreate, ActionDelete, ActionRead, ActionUpdate}, ResourceCryptoKey: {ActionCreate, ActionDelete, ActionRead, ActionUpdate}, ResourceDebugInfo: {ActionRead}, ResourceDeploymentConfig: {ActionRead, ActionUpdate}, diff --git a/codersdk/toolsdk/toolsdk.go b/codersdk/toolsdk/toolsdk.go index 166bde730efc5..985475d211fa3 100644 --- a/codersdk/toolsdk/toolsdk.go +++ b/codersdk/toolsdk/toolsdk.go @@ -591,7 +591,7 @@ This resource provides the following fields: - init_script: The script to run on provisioned infrastructure to fetch and start the agent. - token: Set the environment variable CODER_AGENT_TOKEN to this value to authenticate the agent. -The agent MUST be installed and started using the init_script. +The agent MUST be installed and started using the init_script. A utility like curl or wget to fetch the agent binary must exist in the provisioned infrastructure. Expose terminal or HTTP applications running in a workspace with: @@ -711,13 +711,20 @@ resource "google_compute_instance" "dev" { auto_delete = false source = google_compute_disk.root.name } + // In order to use google-instance-identity, a service account *must* be provided. service_account { email = data.google_compute_default_service_account.default.email scopes = ["cloud-platform"] } + # ONLY FOR WINDOWS: + # metadata = { + # windows-startup-script-ps1 = coder_agent.main.init_script + # } # The startup script runs as root with no $HOME environment set up, so instead of directly # running the agent init script, create a user (with a homedir, default shell and sudo # permissions) and execute the init script as that user. + # + # The agent MUST be started in here. metadata_startup_script = < 200 Response + +```json +[ + { + "created_at": "2019-08-24T14:15:22Z", + "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08", + "title": "string", + "updated_at": "2019-08-24T14:15:22Z" + } +] +``` + +### Responses + +| Status | Meaning | Description | Schema | +|--------|---------------------------------------------------------|-------------|---------------------------------------------------| +| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | array of [codersdk.Chat](schemas.md#codersdkchat) | + +

Response Schema

+ +Status Code **200** + +| Name | Type | Required | Restrictions | Description | +|----------------|-------------------|----------|--------------|-------------| +| `[array item]` | array | false | | | +| `» created_at` | string(date-time) | false | | | +| `» id` | string(uuid) | false | | | +| `» title` | string | false | | | +| `» updated_at` | string(date-time) | false | | | + +To perform this operation, you must be authenticated. [Learn more](authentication.md). + +## Create a chat + +### Code samples + +```shell +# Example request using curl +curl -X POST http://coder-server:8080/api/v2/chats \ + -H 'Accept: application/json' \ + -H 'Coder-Session-Token: API_KEY' +``` + +`POST /chats` + +### Example responses + +> 201 Response + +```json +{ + "created_at": "2019-08-24T14:15:22Z", + "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08", + "title": "string", + "updated_at": "2019-08-24T14:15:22Z" +} +``` + +### Responses + +| Status | Meaning | Description | Schema | +|--------|--------------------------------------------------------------|-------------|------------------------------------------| +| 201 | [Created](https://tools.ietf.org/html/rfc7231#section-6.3.2) | Created | [codersdk.Chat](schemas.md#codersdkchat) | + +To perform this operation, you must be authenticated. [Learn more](authentication.md). + +## Get a chat + +### Code samples + +```shell +# Example request using curl +curl -X GET http://coder-server:8080/api/v2/chats/{chat} \ + -H 'Accept: application/json' \ + -H 'Coder-Session-Token: API_KEY' +``` + +`GET /chats/{chat}` + +### Parameters + +| Name | In | Type | Required | Description | +|--------|------|--------|----------|-------------| +| `chat` | path | string | true | Chat ID | + +### Example responses + +> 200 Response + +```json +{ + "created_at": "2019-08-24T14:15:22Z", + "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08", + "title": "string", + "updated_at": "2019-08-24T14:15:22Z" +} +``` + +### Responses + +| Status | Meaning | Description | Schema | +|--------|---------------------------------------------------------|-------------|------------------------------------------| +| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [codersdk.Chat](schemas.md#codersdkchat) | + +To perform this operation, you must be authenticated. [Learn more](authentication.md). + +## Get chat messages + +### Code samples + +```shell +# Example request using curl +curl -X GET http://coder-server:8080/api/v2/chats/{chat}/messages \ + -H 'Accept: application/json' \ + -H 'Coder-Session-Token: API_KEY' +``` + +`GET /chats/{chat}/messages` + +### Parameters + +| Name | In | Type | Required | Description | +|--------|------|--------|----------|-------------| +| `chat` | path | string | true | Chat ID | + +### Example responses + +> 200 Response + +```json +[ + { + "annotations": [ + null + ], + "content": "string", + "createdAt": [ + 0 + ], + "experimental_attachments": [ + { + "contentType": "string", + "name": "string", + "url": "string" + } + ], + "id": "string", + "parts": [ + { + "data": [ + 0 + ], + "details": [ + { + "data": "string", + "signature": "string", + "text": "string", + "type": "string" + } + ], + "mimeType": "string", + "reasoning": "string", + "source": { + "contentType": "string", + "data": "string", + "metadata": { + "property1": null, + "property2": null + }, + "uri": "string" + }, + "text": "string", + "toolInvocation": { + "args": null, + "result": null, + "state": "call", + "step": 0, + "toolCallId": "string", + "toolName": "string" + }, + "type": "text" + } + ], + "role": "string" + } +] +``` + +### Responses + +| Status | Meaning | Description | Schema | +|--------|---------------------------------------------------------|-------------|---------------------------------------------------| +| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | array of [aisdk.Message](schemas.md#aisdkmessage) | + +

Response Schema

+ +Status Code **200** + +| Name | Type | Required | Restrictions | Description | +|------------------------------|------------------------------------------------------------------|----------|--------------|-------------------------| +| `[array item]` | array | false | | | +| `» annotations` | array | false | | | +| `» content` | string | false | | | +| `» createdAt` | array | false | | | +| `» experimental_attachments` | array | false | | | +| `»» contentType` | string | false | | | +| `»» name` | string | false | | | +| `»» url` | string | false | | | +| `» id` | string | false | | | +| `» parts` | array | false | | | +| `»» data` | array | false | | | +| `»» details` | array | false | | | +| `»»» data` | string | false | | | +| `»»» signature` | string | false | | | +| `»»» text` | string | false | | | +| `»»» type` | string | false | | | +| `»» mimeType` | string | false | | Type: "file" | +| `»» reasoning` | string | false | | Type: "reasoning" | +| `»» source` | [aisdk.SourceInfo](schemas.md#aisdksourceinfo) | false | | Type: "source" | +| `»»» contentType` | string | false | | | +| `»»» data` | string | false | | | +| `»»» metadata` | object | false | | | +| `»»»» [any property]` | any | false | | | +| `»»» uri` | string | false | | | +| `»» text` | string | false | | Type: "text" | +| `»» toolInvocation` | [aisdk.ToolInvocation](schemas.md#aisdktoolinvocation) | false | | Type: "tool-invocation" | +| `»»» args` | any | false | | | +| `»»» result` | any | false | | | +| `»»» state` | [aisdk.ToolInvocationState](schemas.md#aisdktoolinvocationstate) | false | | | +| `»»» step` | integer | false | | | +| `»»» toolCallId` | string | false | | | +| `»»» toolName` | string | false | | | +| `»» type` | [aisdk.PartType](schemas.md#aisdkparttype) | false | | | +| `» role` | string | false | | | + +#### Enumerated Values + +| Property | Value | +|----------|-------------------| +| `state` | `call` | +| `state` | `partial-call` | +| `state` | `result` | +| `type` | `text` | +| `type` | `reasoning` | +| `type` | `tool-invocation` | +| `type` | `source` | +| `type` | `file` | +| `type` | `step-start` | + +To perform this operation, you must be authenticated. [Learn more](authentication.md). + +## Create a chat message + +### Code samples + +```shell +# Example request using curl +curl -X POST http://coder-server:8080/api/v2/chats/{chat}/messages \ + -H 'Content-Type: application/json' \ + -H 'Accept: application/json' \ + -H 'Coder-Session-Token: API_KEY' +``` + +`POST /chats/{chat}/messages` + +> Body parameter + +```json +{ + "message": { + "annotations": [ + null + ], + "content": "string", + "createdAt": [ + 0 + ], + "experimental_attachments": [ + { + "contentType": "string", + "name": "string", + "url": "string" + } + ], + "id": "string", + "parts": [ + { + "data": [ + 0 + ], + "details": [ + { + "data": "string", + "signature": "string", + "text": "string", + "type": "string" + } + ], + "mimeType": "string", + "reasoning": "string", + "source": { + "contentType": "string", + "data": "string", + "metadata": { + "property1": null, + "property2": null + }, + "uri": "string" + }, + "text": "string", + "toolInvocation": { + "args": null, + "result": null, + "state": "call", + "step": 0, + "toolCallId": "string", + "toolName": "string" + }, + "type": "text" + } + ], + "role": "string" + }, + "model": "string", + "thinking": true +} +``` + +### Parameters + +| Name | In | Type | Required | Description | +|--------|------|----------------------------------------------------------------------------------|----------|--------------| +| `chat` | path | string | true | Chat ID | +| `body` | body | [codersdk.CreateChatMessageRequest](schemas.md#codersdkcreatechatmessagerequest) | true | Request body | + +### Example responses + +> 200 Response + +```json +[ + null +] +``` + +### Responses + +| Status | Meaning | Description | Schema | +|--------|---------------------------------------------------------|-------------|--------------------| +| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | array of undefined | + +

Response Schema

+ +To perform this operation, you must be authenticated. [Learn more](authentication.md). diff --git a/docs/reference/api/general.md b/docs/reference/api/general.md index 3c27ddb6dea1d..c14c317066a39 100644 --- a/docs/reference/api/general.md +++ b/docs/reference/api/general.md @@ -161,6 +161,19 @@ curl -X GET http://coder-server:8080/api/v2/deployment/config \ "user": {} }, "agent_stat_refresh_interval": 0, + "ai": { + "value": { + "providers": [ + { + "base_url": "string", + "models": [ + "string" + ], + "type": "string" + } + ] + } + }, "allow_workspace_renames": true, "autobuild_poll_interval": 0, "browser_only": true, @@ -570,6 +583,43 @@ curl -X GET http://coder-server:8080/api/v2/deployment/config \ To perform this operation, you must be authenticated. [Learn more](authentication.md). +## Get language models + +### Code samples + +```shell +# Example request using curl +curl -X GET http://coder-server:8080/api/v2/deployment/llms \ + -H 'Accept: application/json' \ + -H 'Coder-Session-Token: API_KEY' +``` + +`GET /deployment/llms` + +### Example responses + +> 200 Response + +```json +{ + "models": [ + { + "display_name": "string", + "id": "string", + "provider": "string" + } + ] +} +``` + +### Responses + +| Status | Meaning | Description | Schema | +|--------|---------------------------------------------------------|-------------|------------------------------------------------------------------------| +| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [codersdk.LanguageModelConfig](schemas.md#codersdklanguagemodelconfig) | + +To perform this operation, you must be authenticated. [Learn more](authentication.md). + ## SSH Config ### Code samples diff --git a/docs/reference/api/members.md b/docs/reference/api/members.md index 972313001f3ea..a58a597d1ea2a 100644 --- a/docs/reference/api/members.md +++ b/docs/reference/api/members.md @@ -185,6 +185,7 @@ Status Code **200** | `resource_type` | `assign_org_role` | | `resource_type` | `assign_role` | | `resource_type` | `audit_log` | +| `resource_type` | `chat` | | `resource_type` | `crypto_key` | | `resource_type` | `debug_info` | | `resource_type` | `deployment_config` | @@ -351,6 +352,7 @@ Status Code **200** | `resource_type` | `assign_org_role` | | `resource_type` | `assign_role` | | `resource_type` | `audit_log` | +| `resource_type` | `chat` | | `resource_type` | `crypto_key` | | `resource_type` | `debug_info` | | `resource_type` | `deployment_config` | @@ -517,6 +519,7 @@ Status Code **200** | `resource_type` | `assign_org_role` | | `resource_type` | `assign_role` | | `resource_type` | `audit_log` | +| `resource_type` | `chat` | | `resource_type` | `crypto_key` | | `resource_type` | `debug_info` | | `resource_type` | `deployment_config` | @@ -652,6 +655,7 @@ Status Code **200** | `resource_type` | `assign_org_role` | | `resource_type` | `assign_role` | | `resource_type` | `audit_log` | +| `resource_type` | `chat` | | `resource_type` | `crypto_key` | | `resource_type` | `debug_info` | | `resource_type` | `deployment_config` | @@ -1009,6 +1013,7 @@ Status Code **200** | `resource_type` | `assign_org_role` | | `resource_type` | `assign_role` | | `resource_type` | `audit_log` | +| `resource_type` | `chat` | | `resource_type` | `crypto_key` | | `resource_type` | `debug_info` | | `resource_type` | `deployment_config` | diff --git a/docs/reference/api/schemas.md b/docs/reference/api/schemas.md index e2ba1373aa613..6ca005b4ec69c 100644 --- a/docs/reference/api/schemas.md +++ b/docs/reference/api/schemas.md @@ -182,6 +182,250 @@ | `icon` | string | false | | | | `id` | string | false | | ID is a unique identifier for the log source. It is scoped to a workspace agent, and can be statically defined inside code to prevent duplicate sources from being created for the same agent. | +## aisdk.Attachment + +```json +{ + "contentType": "string", + "name": "string", + "url": "string" +} +``` + +### Properties + +| Name | Type | Required | Restrictions | Description | +|---------------|--------|----------|--------------|-------------| +| `contentType` | string | false | | | +| `name` | string | false | | | +| `url` | string | false | | | + +## aisdk.Message + +```json +{ + "annotations": [ + null + ], + "content": "string", + "createdAt": [ + 0 + ], + "experimental_attachments": [ + { + "contentType": "string", + "name": "string", + "url": "string" + } + ], + "id": "string", + "parts": [ + { + "data": [ + 0 + ], + "details": [ + { + "data": "string", + "signature": "string", + "text": "string", + "type": "string" + } + ], + "mimeType": "string", + "reasoning": "string", + "source": { + "contentType": "string", + "data": "string", + "metadata": { + "property1": null, + "property2": null + }, + "uri": "string" + }, + "text": "string", + "toolInvocation": { + "args": null, + "result": null, + "state": "call", + "step": 0, + "toolCallId": "string", + "toolName": "string" + }, + "type": "text" + } + ], + "role": "string" +} +``` + +### Properties + +| Name | Type | Required | Restrictions | Description | +|----------------------------|-----------------------------------------------|----------|--------------|-------------| +| `annotations` | array of undefined | false | | | +| `content` | string | false | | | +| `createdAt` | array of integer | false | | | +| `experimental_attachments` | array of [aisdk.Attachment](#aisdkattachment) | false | | | +| `id` | string | false | | | +| `parts` | array of [aisdk.Part](#aisdkpart) | false | | | +| `role` | string | false | | | + +## aisdk.Part + +```json +{ + "data": [ + 0 + ], + "details": [ + { + "data": "string", + "signature": "string", + "text": "string", + "type": "string" + } + ], + "mimeType": "string", + "reasoning": "string", + "source": { + "contentType": "string", + "data": "string", + "metadata": { + "property1": null, + "property2": null + }, + "uri": "string" + }, + "text": "string", + "toolInvocation": { + "args": null, + "result": null, + "state": "call", + "step": 0, + "toolCallId": "string", + "toolName": "string" + }, + "type": "text" +} +``` + +### Properties + +| Name | Type | Required | Restrictions | Description | +|------------------|---------------------------------------------------------|----------|--------------|-------------------------| +| `data` | array of integer | false | | | +| `details` | array of [aisdk.ReasoningDetail](#aisdkreasoningdetail) | false | | | +| `mimeType` | string | false | | Type: "file" | +| `reasoning` | string | false | | Type: "reasoning" | +| `source` | [aisdk.SourceInfo](#aisdksourceinfo) | false | | Type: "source" | +| `text` | string | false | | Type: "text" | +| `toolInvocation` | [aisdk.ToolInvocation](#aisdktoolinvocation) | false | | Type: "tool-invocation" | +| `type` | [aisdk.PartType](#aisdkparttype) | false | | | + +## aisdk.PartType + +```json +"text" +``` + +### Properties + +#### Enumerated Values + +| Value | +|-------------------| +| `text` | +| `reasoning` | +| `tool-invocation` | +| `source` | +| `file` | +| `step-start` | + +## aisdk.ReasoningDetail + +```json +{ + "data": "string", + "signature": "string", + "text": "string", + "type": "string" +} +``` + +### Properties + +| Name | Type | Required | Restrictions | Description | +|-------------|--------|----------|--------------|-------------| +| `data` | string | false | | | +| `signature` | string | false | | | +| `text` | string | false | | | +| `type` | string | false | | | + +## aisdk.SourceInfo + +```json +{ + "contentType": "string", + "data": "string", + "metadata": { + "property1": null, + "property2": null + }, + "uri": "string" +} +``` + +### Properties + +| Name | Type | Required | Restrictions | Description | +|--------------------|--------|----------|--------------|-------------| +| `contentType` | string | false | | | +| `data` | string | false | | | +| `metadata` | object | false | | | +| » `[any property]` | any | false | | | +| `uri` | string | false | | | + +## aisdk.ToolInvocation + +```json +{ + "args": null, + "result": null, + "state": "call", + "step": 0, + "toolCallId": "string", + "toolName": "string" +} +``` + +### Properties + +| Name | Type | Required | Restrictions | Description | +|--------------|--------------------------------------------------------|----------|--------------|-------------| +| `args` | any | false | | | +| `result` | any | false | | | +| `state` | [aisdk.ToolInvocationState](#aisdktoolinvocationstate) | false | | | +| `step` | integer | false | | | +| `toolCallId` | string | false | | | +| `toolName` | string | false | | | + +## aisdk.ToolInvocationState + +```json +"call" +``` + +### Properties + +#### Enumerated Values + +| Value | +|----------------| +| `call` | +| `partial-call` | +| `result` | + ## coderd.SCIMUser ```json @@ -305,6 +549,48 @@ | `groups` | array of [codersdk.Group](#codersdkgroup) | false | | | | `users` | array of [codersdk.ReducedUser](#codersdkreduceduser) | false | | | +## codersdk.AIConfig + +```json +{ + "providers": [ + { + "base_url": "string", + "models": [ + "string" + ], + "type": "string" + } + ] +} +``` + +### Properties + +| Name | Type | Required | Restrictions | Description | +|-------------|-----------------------------------------------------------------|----------|--------------|-------------| +| `providers` | array of [codersdk.AIProviderConfig](#codersdkaiproviderconfig) | false | | | + +## codersdk.AIProviderConfig + +```json +{ + "base_url": "string", + "models": [ + "string" + ], + "type": "string" +} +``` + +### Properties + +| Name | Type | Required | Restrictions | Description | +|------------|-----------------|----------|--------------|-----------------------------------------------------------| +| `base_url` | string | false | | Base URL is the base URL to use for the API provider. | +| `models` | array of string | false | | Models is the list of models to use for the API provider. | +| `type` | string | false | | Type is the type of the API provider. | + ## codersdk.APIKey ```json @@ -1038,6 +1324,97 @@ AuthorizationObject can represent a "set" of objects, such as: all workspaces in | `one_time_passcode` | string | true | | | | `password` | string | true | | | +## codersdk.Chat + +```json +{ + "created_at": "2019-08-24T14:15:22Z", + "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08", + "title": "string", + "updated_at": "2019-08-24T14:15:22Z" +} +``` + +### Properties + +| Name | Type | Required | Restrictions | Description | +|--------------|--------|----------|--------------|-------------| +| `created_at` | string | false | | | +| `id` | string | false | | | +| `title` | string | false | | | +| `updated_at` | string | false | | | + +## codersdk.ChatMessage + +```json +{ + "annotations": [ + null + ], + "content": "string", + "createdAt": [ + 0 + ], + "experimental_attachments": [ + { + "contentType": "string", + "name": "string", + "url": "string" + } + ], + "id": "string", + "parts": [ + { + "data": [ + 0 + ], + "details": [ + { + "data": "string", + "signature": "string", + "text": "string", + "type": "string" + } + ], + "mimeType": "string", + "reasoning": "string", + "source": { + "contentType": "string", + "data": "string", + "metadata": { + "property1": null, + "property2": null + }, + "uri": "string" + }, + "text": "string", + "toolInvocation": { + "args": null, + "result": null, + "state": "call", + "step": 0, + "toolCallId": "string", + "toolName": "string" + }, + "type": "text" + } + ], + "role": "string" +} +``` + +### Properties + +| Name | Type | Required | Restrictions | Description | +|----------------------------|-----------------------------------------------|----------|--------------|-------------| +| `annotations` | array of undefined | false | | | +| `content` | string | false | | | +| `createdAt` | array of integer | false | | | +| `experimental_attachments` | array of [aisdk.Attachment](#aisdkattachment) | false | | | +| `id` | string | false | | | +| `parts` | array of [aisdk.Part](#aisdkpart) | false | | | +| `role` | string | false | | | + ## codersdk.ConnectionLatency ```json @@ -1070,6 +1447,77 @@ AuthorizationObject can represent a "set" of objects, such as: all workspaces in | `password` | string | true | | | | `to_type` | [codersdk.LoginType](#codersdklogintype) | true | | To type is the login type to convert to. | +## codersdk.CreateChatMessageRequest + +```json +{ + "message": { + "annotations": [ + null + ], + "content": "string", + "createdAt": [ + 0 + ], + "experimental_attachments": [ + { + "contentType": "string", + "name": "string", + "url": "string" + } + ], + "id": "string", + "parts": [ + { + "data": [ + 0 + ], + "details": [ + { + "data": "string", + "signature": "string", + "text": "string", + "type": "string" + } + ], + "mimeType": "string", + "reasoning": "string", + "source": { + "contentType": "string", + "data": "string", + "metadata": { + "property1": null, + "property2": null + }, + "uri": "string" + }, + "text": "string", + "toolInvocation": { + "args": null, + "result": null, + "state": "call", + "step": 0, + "toolCallId": "string", + "toolName": "string" + }, + "type": "text" + } + ], + "role": "string" + }, + "model": "string", + "thinking": true +} +``` + +### Properties + +| Name | Type | Required | Restrictions | Description | +|------------|----------------------------------------------|----------|--------------|-------------| +| `message` | [codersdk.ChatMessage](#codersdkchatmessage) | false | | | +| `model` | string | false | | | +| `thinking` | boolean | false | | | + ## codersdk.CreateFirstUserRequest ```json @@ -1334,12 +1782,52 @@ This is required on creation to enable a user-flow of validating a template work ## codersdk.CreateTestAuditLogRequest ```json -{} +{ + "action": "create", + "additional_fields": [ + 0 + ], + "build_reason": "autostart", + "organization_id": "7c60d51f-b44e-4682-87d6-449835ea4de6", + "request_id": "266ea41d-adf5-480b-af50-15b940c2b846", + "resource_id": "4d5215ed-38bb-48ed-879a-fdb9ca58522f", + "resource_type": "template", + "time": "2019-08-24T14:15:22Z" +} ``` ### Properties -None +| Name | Type | Required | Restrictions | Description | +|---------------------|------------------------------------------------|----------|--------------|-------------| +| `action` | [codersdk.AuditAction](#codersdkauditaction) | false | | | +| `additional_fields` | array of integer | false | | | +| `build_reason` | [codersdk.BuildReason](#codersdkbuildreason) | false | | | +| `organization_id` | string | false | | | +| `request_id` | string | false | | | +| `resource_id` | string | false | | | +| `resource_type` | [codersdk.ResourceType](#codersdkresourcetype) | false | | | +| `time` | string | false | | | + +#### Enumerated Values + +| Property | Value | +|-----------------|--------------------| +| `action` | `create` | +| `action` | `write` | +| `action` | `delete` | +| `action` | `start` | +| `action` | `stop` | +| `build_reason` | `autostart` | +| `build_reason` | `autostop` | +| `build_reason` | `initiator` | +| `resource_type` | `template` | +| `resource_type` | `template_version` | +| `resource_type` | `user` | +| `resource_type` | `workspace` | +| `resource_type` | `workspace_build` | +| `resource_type` | `git_ssh_key` | +| `resource_type` | `auditable_group` | ## codersdk.CreateTokenRequest @@ -1812,6 +2300,19 @@ CreateWorkspaceRequest provides options for creating a new workspace. Only one o "user": {} }, "agent_stat_refresh_interval": 0, + "ai": { + "value": { + "providers": [ + { + "base_url": "string", + "models": [ + "string" + ], + "type": "string" + } + ] + } + }, "allow_workspace_renames": true, "autobuild_poll_interval": 0, "browser_only": true, @@ -2297,6 +2798,19 @@ CreateWorkspaceRequest provides options for creating a new workspace. Only one o "user": {} }, "agent_stat_refresh_interval": 0, + "ai": { + "value": { + "providers": [ + { + "base_url": "string", + "models": [ + "string" + ], + "type": "string" + } + ] + } + }, "allow_workspace_renames": true, "autobuild_poll_interval": 0, "browser_only": true, @@ -2673,6 +3187,7 @@ CreateWorkspaceRequest provides options for creating a new workspace. Only one o | `address` | [serpent.HostPort](#serpenthostport) | false | | Deprecated: Use HTTPAddress or TLS.Address instead. | | `agent_fallback_troubleshooting_url` | [serpent.URL](#serpenturl) | false | | | | `agent_stat_refresh_interval` | integer | false | | | +| `ai` | [serpent.Struct-codersdk_AIConfig](#serpentstruct-codersdk_aiconfig) | false | | | | `allow_workspace_renames` | boolean | false | | | | `autobuild_poll_interval` | integer | false | | | | `browser_only` | boolean | false | | | @@ -2829,6 +3344,7 @@ CreateWorkspaceRequest provides options for creating a new workspace. Only one o | `web-push` | | `dynamic-parameters` | | `workspace-prebuilds` | +| `agentic-chat` | ## codersdk.ExternalAuth @@ -3446,6 +3962,44 @@ Git clone makes use of this by parsing the URL from: 'Username for "https://gith |-------------------------------| | `REQUIRED_TEMPLATE_VARIABLES` | +## codersdk.LanguageModel + +```json +{ + "display_name": "string", + "id": "string", + "provider": "string" +} +``` + +### Properties + +| Name | Type | Required | Restrictions | Description | +|----------------|--------|----------|--------------|-------------------------------------------------------------------| +| `display_name` | string | false | | | +| `id` | string | false | | ID is used by the provider to identify the LLM. | +| `provider` | string | false | | Provider is the provider of the LLM. e.g. openai, anthropic, etc. | + +## codersdk.LanguageModelConfig + +```json +{ + "models": [ + { + "display_name": "string", + "id": "string", + "provider": "string" + } + ] +} +``` + +### Properties + +| Name | Type | Required | Restrictions | Description | +|----------|-----------------------------------------------------------|----------|--------------|-------------| +| `models` | array of [codersdk.LanguageModel](#codersdklanguagemodel) | false | | | + ## codersdk.License ```json @@ -5354,6 +5908,7 @@ Git clone makes use of this by parsing the URL from: 'Username for "https://gith | `assign_org_role` | | `assign_role` | | `audit_log` | +| `chat` | | `crypto_key` | | `debug_info` | | `deployment_config` | @@ -11118,6 +11673,30 @@ None |---------|-----------------------------------------------------|----------|--------------|-------------| | `value` | array of [codersdk.LinkConfig](#codersdklinkconfig) | false | | | +## serpent.Struct-codersdk_AIConfig + +```json +{ + "value": { + "providers": [ + { + "base_url": "string", + "models": [ + "string" + ], + "type": "string" + } + ] + } +} +``` + +### Properties + +| Name | Type | Required | Restrictions | Description | +|---------|----------------------------------------|----------|--------------|-------------| +| `value` | [codersdk.AIConfig](#codersdkaiconfig) | false | | | + ## serpent.URL ```json diff --git a/go.mod b/go.mod index 8ff0ba1fa2376..ce41f23e02e05 100644 --- a/go.mod +++ b/go.mod @@ -487,10 +487,13 @@ require ( ) require ( + github.com/anthropics/anthropic-sdk-go v0.2.0-beta.3 github.com/coder/preview v0.0.1 github.com/fsnotify/fsnotify v1.9.0 - github.com/kylecarbs/aisdk-go v0.0.5 + github.com/kylecarbs/aisdk-go v0.0.8 github.com/mark3labs/mcp-go v0.23.1 + github.com/openai/openai-go v0.1.0-beta.6 + google.golang.org/genai v0.7.0 ) require ( @@ -502,7 +505,6 @@ require ( github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.26.0 // indirect github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.50.0 // indirect github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.50.0 // indirect - github.com/anthropics/anthropic-sdk-go v0.2.0-beta.3 // indirect github.com/aquasecurity/go-version v0.0.1 // indirect github.com/aquasecurity/trivy v0.58.2 // indirect github.com/aws/aws-sdk-go v1.55.6 // indirect @@ -516,7 +518,6 @@ require ( github.com/hashicorp/go-safetemp v1.0.0 // indirect github.com/klauspost/cpuid/v2 v2.2.10 // indirect github.com/moby/sys/user v0.3.0 // indirect - github.com/openai/openai-go v0.1.0-beta.6 // indirect github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect github.com/samber/lo v1.49.1 // indirect github.com/spiffe/go-spiffe/v2 v2.5.0 // indirect @@ -527,6 +528,5 @@ require ( go.opentelemetry.io/contrib/detectors/gcp v1.34.0 // indirect go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.60.0 // indirect go.opentelemetry.io/otel/sdk/metric v1.35.0 // indirect - google.golang.org/genai v0.7.0 // indirect k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect ) diff --git a/go.sum b/go.sum index fc05152d34122..09bd945ec4898 100644 --- a/go.sum +++ b/go.sum @@ -1467,8 +1467,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/kylecarbs/aisdk-go v0.0.5 h1:e4HE/SMBUUZn7AS/luiIYbEtHbbtUBzJS95R6qHDYVE= -github.com/kylecarbs/aisdk-go v0.0.5/go.mod h1:3nAhClwRNo6ZfU44GrBZ8O2fCCrxJdaHb9JIz+P3LR8= +github.com/kylecarbs/aisdk-go v0.0.8 h1:hnKVbLM6U8XqX3t5I26J8k5saXdra595bGt1HP0PvKA= +github.com/kylecarbs/aisdk-go v0.0.8/go.mod h1:3nAhClwRNo6ZfU44GrBZ8O2fCCrxJdaHb9JIz+P3LR8= github.com/kylecarbs/chroma/v2 v2.0.0-20240401211003-9e036e0631f3 h1:Z9/bo5PSeMutpdiKYNt/TTSfGM1Ll0naj3QzYX9VxTc= github.com/kylecarbs/chroma/v2 v2.0.0-20240401211003-9e036e0631f3/go.mod h1:BUGjjsD+ndS6eX37YgTchSEG+Jg9Jv1GiZs9sqPqztk= github.com/kylecarbs/opencensus-go v0.23.1-0.20220307014935-4d0325a68f8b/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E= diff --git a/site/src/api/rbacresourcesGenerated.ts b/site/src/api/rbacresourcesGenerated.ts index ffb5b541e3a4a..079dcb4a87a61 100644 --- a/site/src/api/rbacresourcesGenerated.ts +++ b/site/src/api/rbacresourcesGenerated.ts @@ -31,6 +31,12 @@ export const RBACResourceActions: Partial< create: "create new audit log entries", read: "read audit logs", }, + chat: { + create: "create a chat", + delete: "delete a chat", + read: "read a chat", + update: "update a chat", + }, crypto_key: { create: "create crypto keys", delete: "delete crypto keys", diff --git a/site/src/api/typesGenerated.ts b/site/src/api/typesGenerated.ts index d879c09d119b2..b1fcb296de4e8 100644 --- a/site/src/api/typesGenerated.ts +++ b/site/src/api/typesGenerated.ts @@ -6,6 +6,18 @@ export interface ACLAvailable { readonly groups: readonly Group[]; } +// From codersdk/deployment.go +export interface AIConfig { + readonly providers?: readonly AIProviderConfig[]; +} + +// From codersdk/deployment.go +export interface AIProviderConfig { + readonly type: string; + readonly models: readonly string[]; + readonly base_url: string; +} + // From codersdk/apikey.go export interface APIKey { readonly id: string; @@ -291,6 +303,28 @@ export interface ChangePasswordWithOneTimePasscodeRequest { readonly one_time_passcode: string; } +// From codersdk/chat.go +export interface Chat { + readonly id: string; + readonly created_at: string; + readonly updated_at: string; + readonly title: string; +} + +// From codersdk/chat.go +export interface ChatMessage { + readonly id: string; + readonly createdAt?: Record; + readonly content: string; + readonly role: string; + // external type "github.com/kylecarbs/aisdk-go.Part", to include this type the package must be explicitly included in the parsing + readonly parts?: readonly unknown[]; + // empty interface{} type, falling back to unknown + readonly annotations?: readonly unknown[]; + // external type "github.com/kylecarbs/aisdk-go.Attachment", to include this type the package must be explicitly included in the parsing + readonly experimental_attachments?: readonly unknown[]; +} + // From codersdk/client.go export const CoderDesktopTelemetryHeader = "Coder-Desktop-Telemetry"; @@ -312,6 +346,14 @@ export interface ConvertLoginRequest { readonly password: string; } +// From codersdk/chat.go +export interface CreateChatMessageRequest { + readonly model: string; + // embedded anonymous struct, please fix by naming it + readonly message: unknown; + readonly thinking: boolean; +} + // From codersdk/users.go export interface CreateFirstUserRequest { readonly email: string; @@ -677,6 +719,7 @@ export interface DeploymentValues { readonly disable_password_auth?: boolean; readonly support?: SupportConfig; readonly external_auth?: SerpentStruct; + readonly ai?: SerpentStruct; readonly config_ssh?: SSHConfig; readonly wgtunnel_host?: string; readonly disable_owner_workspace_exec?: boolean; @@ -769,6 +812,7 @@ export const EntitlementsWarningHeader = "X-Coder-Entitlements-Warning"; // From codersdk/deployment.go export type Experiment = + | "agentic-chat" | "auto-fill-parameters" | "dynamic-parameters" | "example" @@ -1186,6 +1230,18 @@ export type JobErrorCode = "REQUIRED_TEMPLATE_VARIABLES"; export const JobErrorCodes: JobErrorCode[] = ["REQUIRED_TEMPLATE_VARIABLES"]; +// From codersdk/deployment.go +export interface LanguageModel { + readonly id: string; + readonly display_name: string; + readonly provider: string; +} + +// From codersdk/deployment.go +export interface LanguageModelConfig { + readonly models: readonly LanguageModel[]; +} + // From codersdk/licenses.go export interface License { readonly id: number; @@ -2061,6 +2117,7 @@ export type RBACResource = | "assign_org_role" | "assign_role" | "audit_log" + | "chat" | "crypto_key" | "debug_info" | "deployment_config" @@ -2099,6 +2156,7 @@ export const RBACResources: RBACResource[] = [ "assign_org_role", "assign_role", "audit_log", + "chat", "crypto_key", "debug_info", "deployment_config",