Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ make testacc # TF_ACC=1 go test ./... -timeout 120m

## Adding a new resource

**Naming.** Terraform resource types are prefixed with the Coder feature area they belong to so related resources cluster together in configs and docs. Prefer an existing prefix over inventing a new one:

- `coderd_agents_*` — Coder Agents feature area (`coderd_agents_mcp_server`, `coderd_agents_model`, `coderd_agents_system_prompt`, `coderd_default_agents_model`).
- `coderd_organization_*` — organization-scoped settings (`coderd_organization_group_sync`, `coderd_organization_sync_settings`).
- `coderd_oauth2_*`, `coderd_workspace_*`, etc. follow the same rule.

The Go file, the `TypeName` (`req.ProviderTypeName + "_<name>"`), the `examples/resources/coderd_<name>/` directory, and the generated `docs/resources/<name>.md` must all use the same `<name>`. Rename all four together if you change it.

1. Create `internal/provider/<name>_resource.go` — model struct, `Schema`, `Configure`, CRUD, and validators (implement the framework `Resource` interface).
2. Register it by adding `New<Name>Resource` to the slice returned by `Resources(ctx)` in `internal/provider/provider.go`.
3. Add an example to `examples/resources/coderd_<name>/resource.tf` (plus `import.sh` if importable) — `docs/` is generated from these, so a missing example means missing/incorrect docs.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "coderd_chat_system_prompt Resource - terraform-provider-coderd"
page_title: "coderd_agents_system_prompt Resource - terraform-provider-coderd"
subcategory: ""
description: |-
~> This resource is experimental. Changes are to be expected, and we recommend using it with caution in production environments.
Expand All @@ -15,7 +15,7 @@ description: |-
This resource requires Coder version 2.32.0 https://github.com/coder/coder/releases/tag/v2.32.0 or later, and a token with site-wide owner permissions.
---

# coderd_chat_system_prompt (Resource)
# coderd_agents_system_prompt (Resource)

~> This resource is experimental. Changes are to be expected, and we recommend using it with caution in production environments.

Expand All @@ -39,7 +39,7 @@ This resource requires Coder version [2.32.0](https://github.com/coder/coder/rel
```terraform
// Keep the prompt itself in a Markdown file next to the Terraform
// configuration so it can be reviewed like any other prose.
resource "coderd_chat_system_prompt" "this" {
resource "coderd_agents_system_prompt" "this" {
system_prompt = file("${path.module}/system-prompt.md")

// Append to Coder's built-in system prompt (the default) rather
Expand All @@ -66,7 +66,7 @@ Import is supported using the following syntax:
The [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import) can be used, for example:

```shell
# The chat system prompt is a deployment-wide singleton, so the import ID is
# The agents system prompt is a deployment-wide singleton, so the import ID is
# required by the CLI syntax but otherwise unused.
terraform import coderd_chat_system_prompt.this chat_system_prompt
terraform import coderd_agents_system_prompt.this agents_system_prompt
```
3 changes: 3 additions & 0 deletions examples/resources/coderd_agents_system_prompt/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# The agents system prompt is a deployment-wide singleton, so the import ID is
# required by the CLI syntax but otherwise unused.
terraform import coderd_agents_system_prompt.this agents_system_prompt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Keep the prompt itself in a Markdown file next to the Terraform
// configuration so it can be reviewed like any other prose.
resource "coderd_chat_system_prompt" "this" {
resource "coderd_agents_system_prompt" "this" {
system_prompt = file("${path.module}/system-prompt.md")

// Append to Coder's built-in system prompt (the default) rather
Expand Down
3 changes: 0 additions & 3 deletions examples/resources/coderd_chat_system_prompt/import.sh

This file was deleted.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import (
"github.com/stretchr/testify/require"
)

const chatSystemPromptPath = "/api/experimental/chats/config/system-prompt"
const agentsSystemPromptPath = "/api/experimental/chats/config/system-prompt"

const chatSystemPromptResourceAddr = "coderd_chat_system_prompt.test"
const agentsSystemPromptResourceAddr = "coderd_agents_system_prompt.test"

type fakeChatCoderd struct {
*httptest.Server
Expand Down Expand Up @@ -70,7 +70,7 @@ func (f *fakeChatCoderd) handle(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, codersdk.Entitlements{
Features: map[codersdk.FeatureName]codersdk.Feature{},
})
case r.URL.Path == chatSystemPromptPath && r.Method == http.MethodGet:
case r.URL.Path == agentsSystemPromptPath && r.Method == http.MethodGet:
f.mu.Lock()
status, prompt, include := f.getStatus, f.prompt, f.includeDflt
f.mu.Unlock()
Expand All @@ -83,7 +83,7 @@ func (f *fakeChatCoderd) handle(w http.ResponseWriter, r *http.Request) {
IncludeDefaultSystemPrompt: include,
DefaultSystemPrompt: "built-in prompt",
})
case r.URL.Path == chatSystemPromptPath && r.Method == http.MethodPut:
case r.URL.Path == agentsSystemPromptPath && r.Method == http.MethodPut:
f.mu.Lock()
status := f.putStatus
f.mu.Unlock()
Expand Down Expand Up @@ -112,19 +112,19 @@ func (f *fakeChatCoderd) handle(w http.ResponseWriter, r *http.Request) {
}
}

func chatSystemPromptConfig(url, prompt string, includeDefault *bool) string {
func agentsSystemPromptConfig(url, prompt string, includeDefault *bool) string {
include := ""
if includeDefault != nil {
include = fmt.Sprintf("\n\tinclude_default_system_prompt = %t", *includeDefault)
}
return oauth2SettingsProviderBlock(url) + fmt.Sprintf(`
resource "coderd_chat_system_prompt" "test" {
resource "coderd_agents_system_prompt" "test" {
system_prompt = %q%s
}
`, prompt, include)
}

func TestChatSystemPromptSemanticEquals(t *testing.T) {
func TestAgentsSystemPromptSemanticEquals(t *testing.T) {
t.Parallel()

ctx := t.Context()
Expand All @@ -142,20 +142,20 @@ func TestChatSystemPromptSemanticEquals(t *testing.T) {
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got, diags := newChatSystemPromptTextValue(tc.a).StringSemanticEquals(ctx, newChatSystemPromptTextValue(tc.b))
got, diags := newAgentsSystemPromptTextValue(tc.a).StringSemanticEquals(ctx, newAgentsSystemPromptTextValue(tc.b))
require.False(t, diags.HasError())
require.Equal(t, tc.want, got)
})
}
}

func TestChatSystemPromptLengthValidator(t *testing.T) {
func TestAgentsSystemPromptLengthValidator(t *testing.T) {
t.Parallel()

ctx := t.Context()

okPrompt := strings.Repeat("a", maxChatSystemPromptBytes) + "\n\n\n"
tooLong := strings.Repeat("a", maxChatSystemPromptBytes+1)
okPrompt := strings.Repeat("a", maxAgentsSystemPromptBytes) + "\n\n\n"
tooLong := strings.Repeat("a", maxAgentsSystemPromptBytes+1)

for _, tc := range []struct {
name string
Expand All @@ -170,7 +170,7 @@ func TestChatSystemPromptLengthValidator(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
resp := &validator.StringResponse{}
chatSystemPromptLengthValidator{}.ValidateString(ctx, validator.StringRequest{
agentsSystemPromptLengthValidator{}.ValidateString(ctx, validator.StringRequest{
Path: pathSystemPrompt,
ConfigValue: tc.value,
}, resp)
Expand All @@ -179,7 +179,7 @@ func TestChatSystemPromptLengthValidator(t *testing.T) {
}
}

func TestAccChatSystemPromptResource(t *testing.T) {
func TestAccAgentsSystemPromptResource(t *testing.T) {
t.Parallel()
if os.Getenv("TF_ACC") == "" {
t.Skip("Acceptance tests are disabled.")
Expand All @@ -194,28 +194,28 @@ func TestAccChatSystemPromptResource(t *testing.T) {
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: chatSystemPromptConfig(f.URL, "You are a helpful agent.\n", nil),
Config: agentsSystemPromptConfig(f.URL, "You are a helpful agent.\n", nil),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(
chatSystemPromptResourceAddr,
agentsSystemPromptResourceAddr,
tfjsonpath.New("include_default_system_prompt"),
knownvalue.Bool(true),
),
},
},
{
Config: chatSystemPromptConfig(f.URL, "You are a helpful agent.\n", nil),
Config: agentsSystemPromptConfig(f.URL, "You are a helpful agent.\n", nil),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
},
{
Config: chatSystemPromptConfig(f.URL, "You are a very helpful agent.\n", &includeFalse),
Config: agentsSystemPromptConfig(f.URL, "You are a very helpful agent.\n", &includeFalse),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(
chatSystemPromptResourceAddr,
agentsSystemPromptResourceAddr,
tfjsonpath.New("include_default_system_prompt"),
knownvalue.Bool(false),
),
Expand All @@ -230,7 +230,7 @@ func TestAccChatSystemPromptResource(t *testing.T) {
require.True(t, f.includeDflt)
}

func TestAccChatSystemPromptImport(t *testing.T) {
func TestAccAgentsSystemPromptImport(t *testing.T) {
t.Parallel()
if os.Getenv("TF_ACC") == "" {
t.Skip("Acceptance tests are disabled.")
Expand All @@ -247,14 +247,14 @@ func TestAccChatSystemPromptImport(t *testing.T) {
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: chatSystemPromptConfig(f.URL, "configured in the dashboard", nil),
ResourceName: chatSystemPromptResourceAddr,
Config: agentsSystemPromptConfig(f.URL, "configured in the dashboard", nil),
ResourceName: agentsSystemPromptResourceAddr,
ImportState: true,
ImportStatePersist: true,
ImportStateId: "chat_system_prompt",
ImportStateId: "agents_system_prompt",
},
{
Config: chatSystemPromptConfig(f.URL, "configured in the dashboard", nil),
Config: agentsSystemPromptConfig(f.URL, "configured in the dashboard", nil),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
Expand All @@ -269,7 +269,7 @@ func TestAccChatSystemPromptImport(t *testing.T) {
require.Equal(t, []string{""}, f.putPrompts)
}

func TestAccChatSystemPromptEndpointUnavailable(t *testing.T) {
func TestAccAgentsSystemPromptEndpointUnavailable(t *testing.T) {
t.Parallel()
if os.Getenv("TF_ACC") == "" {
t.Skip("Acceptance tests are disabled.")
Expand All @@ -287,20 +287,20 @@ func TestAccChatSystemPromptEndpointUnavailable(t *testing.T) {
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: chatSystemPromptConfig(f.URL, "prompt", nil),
Config: agentsSystemPromptConfig(f.URL, "prompt", nil),
ExpectError: regexp.MustCompile("Chat System Prompt Endpoint Unavailable"),
},
},
})
}

func TestAccChatSystemPromptRealCoderNoDrift(t *testing.T) {
func TestAccAgentsSystemPromptRealCoderNoDrift(t *testing.T) {
t.Parallel()
if os.Getenv("TF_ACC") == "" {
t.Skip("Acceptance tests are disabled.")
}
ctx := t.Context()
client := integration.StartCoder(ctx, t, "chat_system_prompt_acc")
client := integration.StartCoder(ctx, t, "agents_system_prompt_acc")
experimental := codersdk.NewExperimentalClient(client)

messyPrompt := "You are a helpful agent.\r\nBe concise.\u200b\n\n\n\nAlways cite sources.\n"
Expand All @@ -311,7 +311,7 @@ provider "coderd" {
token = %[2]q
}

resource "coderd_chat_system_prompt" "test" {
resource "coderd_agents_system_prompt" "test" {
system_prompt = %[3]q
}
`, client.URL.String(), client.SessionToken(), messyPrompt)
Expand All @@ -325,7 +325,7 @@ resource "coderd_chat_system_prompt" "test" {
Config: cfg,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(
chatSystemPromptResourceAddr,
agentsSystemPromptResourceAddr,
tfjsonpath.New("include_default_system_prompt"),
knownvalue.Bool(true),
),
Expand All @@ -344,13 +344,13 @@ resource "coderd_chat_system_prompt" "test" {
require.True(t, live.IncludeDefaultSystemPrompt)
}

func TestAccChatSystemPromptRealCoderImportNoDrift(t *testing.T) {
func TestAccAgentsSystemPromptRealCoderImportNoDrift(t *testing.T) {
t.Parallel()
if os.Getenv("TF_ACC") == "" {
t.Skip("Acceptance tests are disabled.")
}
ctx := t.Context()
client := integration.StartCoder(ctx, t, "chat_system_prompt_import_acc")
client := integration.StartCoder(ctx, t, "agents_system_prompt_import_acc")
experimental := codersdk.NewExperimentalClient(client)

includeDefault := true
Expand All @@ -367,12 +367,12 @@ provider "coderd" {
`, client.URL.String(), client.SessionToken())

cfgExact := providerBlock + `
resource "coderd_chat_system_prompt" "test" {
resource "coderd_agents_system_prompt" "test" {
system_prompt = "configured in the dashboard"
}
`
cfgTrailingNewline := providerBlock + `
resource "coderd_chat_system_prompt" "test" {
resource "coderd_agents_system_prompt" "test" {
system_prompt = "configured in the dashboard\n"
}
`
Expand All @@ -384,10 +384,10 @@ resource "coderd_chat_system_prompt" "test" {
Steps: []resource.TestStep{
{
Config: cfgExact,
ResourceName: chatSystemPromptResourceAddr,
ResourceName: agentsSystemPromptResourceAddr,
ImportState: true,
ImportStatePersist: true,
ImportStateId: "chat_system_prompt",
ImportStateId: "agents_system_prompt",
},
{
Config: cfgExact,
Expand All @@ -404,7 +404,7 @@ resource "coderd_chat_system_prompt" "test" {
})
}

func TestChatSystemPromptModifyPlan(t *testing.T) {
func TestAgentsSystemPromptModifyPlan(t *testing.T) {
t.Parallel()

objType := tftypes.Object{
Expand Down Expand Up @@ -523,7 +523,7 @@ func TestChatSystemPromptModifyPlan(t *testing.T) {
client := codersdk.New(serverURL)
client.SetSessionToken("test-token")

r := &ChatSystemPromptResource{
r := &AgentsSystemPromptResource{
CoderdProviderData: &CoderdProviderData{Client: client},
}

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (p *CoderdProvider) Resources(ctx context.Context) []func() resource.Resour
NewAgentsMCPServerResource,
NewAgentsModelResource,
NewDefaultAgentsModelResource,
NewChatSystemPromptResource,
NewAgentsSystemPromptResource,
NewOAuth2ProviderSettingsResource,
}
}
Expand Down