- Notifications
You must be signed in to change notification settings - Fork913
feat: add database tables and API routes for agentic chat feature#17570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
+4,264 −16
Merged
Changes fromall commits
Commits
Show all changes
18 commits Select commitHold shift + click to select a range
f762a76
Initial chat schema
kylecarbsdd506e6
And we have chat!
kylecarbsd848a94
Add more MCP stuff
kylecarbs1134736
Add tools
kylecarbs6a583e0
Add tool invocation stories stuff
kylecarbs725d738
Add more tools
kylecarbs9068d8b
Improve chat
kylecarbs10b534c
chore: make gen, fmt, lint
johnstcn3faa0b4
site: remove chat-related changes for separate PR
johnstcn7a5c2d2
coderd/rbac: update chat rbac
johnstcnd3f1cf4
coderd/database: only generate ID at database side, order messages by…
johnstcnd9aab39
fix(coderd/database): fix down migration and add fixture
johnstcn8071f9c
chore: add experiment agentic-chat, add tests for chat API routes
johnstcn40c3d5e
chore: update kylecarbs/aisdk-go to v0.0.8
johnstcn3cf8f60
fix(coderd/ai): support overriding base URL for OpenAI provider, requ…
johnstcnccefabe
fix(coderd): avoid serializing null message content in postChatMessages
johnstcn05fa48d
fix(coderd): chat: avoid panic if no messages are accumulated
johnstcn8481ad5
fix(coderd): chat: do not pass tools to model for generating chat title
johnstcnFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
89 changes: 89 additions & 0 deletionscli/server.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletionscli/testdata/server-config.yaml.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletionscoderd/ai/ai.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
) | ||
typeLanguageModelstruct { | ||
codersdk.LanguageModel | ||
StreamFuncStreamFunc | ||
} | ||
typeStreamOptionsstruct { | ||
SystemPromptstring | ||
Modelstring | ||
Messages []aisdk.Message | ||
Thinkingbool | ||
Tools []aisdk.Tool | ||
} | ||
typeStreamFuncfunc(ctx context.Context,optionsStreamOptions) (aisdk.DataStream,error) | ||
// LanguageModels is a map of language model ID to language model. | ||
typeLanguageModelsmap[string]LanguageModel | ||
funcModelsFromConfig(ctx context.Context,configs []codersdk.AIProviderConfig) (LanguageModels,error) { | ||
models:=make(LanguageModels) | ||
for_,config:=rangeconfigs { | ||
varstreamFuncStreamFunc | ||
switchconfig.Type { | ||
case"openai": | ||
opts:= []openaioption.RequestOption{ | ||
openaioption.WithAPIKey(config.APIKey), | ||
} | ||
ifconfig.BaseURL!="" { | ||
opts=append(opts,openaioption.WithBaseURL(config.BaseURL)) | ||
} | ||
client:=openai.NewClient(opts...) | ||
streamFunc=func(ctx context.Context,optionsStreamOptions) (aisdk.DataStream,error) { | ||
openaiMessages,err:=aisdk.MessagesToOpenAI(options.Messages) | ||
iferr!=nil { | ||
returnnil,err | ||
} | ||
tools:=aisdk.ToolsToOpenAI(options.Tools) | ||
ifoptions.SystemPrompt!="" { | ||
openaiMessages=append([]openai.ChatCompletionMessageParamUnion{ | ||
openai.SystemMessage(options.SystemPrompt), | ||
},openaiMessages...) | ||
} | ||
returnaisdk.OpenAIToDataStream(client.Chat.Completions.NewStreaming(ctx, openai.ChatCompletionNewParams{ | ||
Messages:openaiMessages, | ||
Model:options.Model, | ||
Tools:tools, | ||
MaxTokens:openai.Int(8192), | ||
})),nil | ||
} | ||
ifconfig.Models==nil { | ||
models,err:=client.Models.List(ctx) | ||
iferr!=nil { | ||
returnnil,err | ||
} | ||
config.Models=make([]string,len(models.Data)) | ||
fori,model:=rangemodels.Data { | ||
config.Models[i]=model.ID | ||
} | ||
} | ||
case"anthropic": | ||
client:=anthropic.NewClient(anthropicoption.WithAPIKey(config.APIKey)) | ||
streamFunc=func(ctx context.Context,optionsStreamOptions) (aisdk.DataStream,error) { | ||
anthropicMessages,systemMessage,err:=aisdk.MessagesToAnthropic(options.Messages) | ||
iferr!=nil { | ||
returnnil,err | ||
} | ||
ifoptions.SystemPrompt!="" { | ||
systemMessage= []anthropic.TextBlockParam{ | ||
*anthropic.NewTextBlock(options.SystemPrompt).OfRequestTextBlock, | ||
} | ||
} | ||
returnaisdk.AnthropicToDataStream(client.Messages.NewStreaming(ctx, anthropic.MessageNewParams{ | ||
Messages:anthropicMessages, | ||
Model:options.Model, | ||
System:systemMessage, | ||
Tools:aisdk.ToolsToAnthropic(options.Tools), | ||
MaxTokens:8192, | ||
})),nil | ||
} | ||
ifconfig.Models==nil { | ||
models,err:=client.Models.List(ctx, anthropic.ModelListParams{}) | ||
iferr!=nil { | ||
returnnil,err | ||
} | ||
config.Models=make([]string,len(models.Data)) | ||
fori,model:=rangemodels.Data { | ||
config.Models[i]=model.ID | ||
} | ||
} | ||
case"google": | ||
client,err:=genai.NewClient(ctx,&genai.ClientConfig{ | ||
APIKey:config.APIKey, | ||
Backend:genai.BackendGeminiAPI, | ||
}) | ||
iferr!=nil { | ||
returnnil,err | ||
} | ||
streamFunc=func(ctx context.Context,optionsStreamOptions) (aisdk.DataStream,error) { | ||
googleMessages,err:=aisdk.MessagesToGoogle(options.Messages) | ||
iferr!=nil { | ||
returnnil,err | ||
} | ||
tools,err:=aisdk.ToolsToGoogle(options.Tools) | ||
iferr!=nil { | ||
returnnil,err | ||
} | ||
varsystemInstruction*genai.Content | ||
ifoptions.SystemPrompt!="" { | ||
systemInstruction=&genai.Content{ | ||
Parts: []*genai.Part{ | ||
genai.NewPartFromText(options.SystemPrompt), | ||
}, | ||
Role:"model", | ||
} | ||
} | ||
returnaisdk.GoogleToDataStream(client.Models.GenerateContentStream(ctx,options.Model,googleMessages,&genai.GenerateContentConfig{ | ||
SystemInstruction:systemInstruction, | ||
Tools:tools, | ||
})),nil | ||
} | ||
ifconfig.Models==nil { | ||
models,err:=client.Models.List(ctx,&genai.ListModelsConfig{}) | ||
iferr!=nil { | ||
returnnil,err | ||
} | ||
config.Models=make([]string,len(models.Items)) | ||
fori,model:=rangemodels.Items { | ||
config.Models[i]=model.Name | ||
} | ||
} | ||
default: | ||
returnnil,xerrors.Errorf("unsupported model type: %s",config.Type) | ||
} | ||
for_,model:=rangeconfig.Models { | ||
models[model]=LanguageModel{ | ||
LanguageModel: codersdk.LanguageModel{ | ||
ID:model, | ||
DisplayName:model, | ||
Provider:config.Type, | ||
}, | ||
StreamFunc:streamFunc, | ||
} | ||
} | ||
} | ||
returnmodels,nil | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.