mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add experimental agents support (#22290)
feat: add AI chat system with agent tools and chat UI Introduce the chatd subsystem and Agents UI for AI-powered chat within Coder workspaces. - Add chatd package with chat loop, message compaction, prompt management, and LLM provider integration (OpenAI, Anthropic) - Add agent tools: create workspace, list/read templates, read/write/ edit files, execute commands - Add chat API endpoints with streaming, message editing, and durable reconnection - Add database schema and migrations for chats, chat messages, chat providers, and chat model configs - Add RBAC policies and dbauthz enforcement for chat resources - Add Agents UI pages with conversation timeline, queued messages list, diff viewer, and model configuration panel - Add comprehensive test coverage including coderd integration tests, chatd unit tests, and Storybook stories - Gate feature behind experiments flag --------- Co-authored-by: Cian Johnston <cian@coder.com> Co-authored-by: Danielle Maywood <danielle@themaywoods.com> Co-authored-by: Jeremy Ruppel <jeremy@coder.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Cian Johnston
Danielle Maywood
Jeremy Ruppel
Claude Sonnet 4.6
parent
67da4e8b56
commit
edee917d88
@@ -0,0 +1,8 @@
|
||||
DROP TABLE IF EXISTS chat_queued_messages;
|
||||
DROP TABLE IF EXISTS chat_diff_statuses;
|
||||
DROP TABLE IF EXISTS chat_messages;
|
||||
DROP TABLE IF EXISTS chats;
|
||||
DROP TABLE IF EXISTS chat_model_configs;
|
||||
DROP TABLE IF EXISTS chat_providers;
|
||||
DROP TYPE IF EXISTS chat_message_visibility;
|
||||
DROP TYPE IF EXISTS chat_status;
|
||||
@@ -0,0 +1,167 @@
|
||||
CREATE TYPE chat_status AS ENUM (
|
||||
'waiting',
|
||||
'pending',
|
||||
'running',
|
||||
'paused',
|
||||
'completed',
|
||||
'error'
|
||||
);
|
||||
|
||||
CREATE TYPE chat_message_visibility AS ENUM (
|
||||
'user',
|
||||
'model',
|
||||
'both'
|
||||
);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
owner_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
workspace_id UUID REFERENCES workspaces(id) ON DELETE SET NULL,
|
||||
workspace_agent_id UUID REFERENCES workspace_agents(id) ON DELETE SET NULL,
|
||||
title TEXT NOT NULL DEFAULT 'New Chat',
|
||||
status chat_status NOT NULL DEFAULT 'waiting',
|
||||
worker_id UUID,
|
||||
started_at TIMESTAMPTZ,
|
||||
heartbeat_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
parent_chat_id UUID REFERENCES chats(id) ON DELETE SET NULL,
|
||||
root_chat_id UUID REFERENCES chats(id) ON DELETE SET NULL,
|
||||
last_model_config_id UUID NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX idx_chats_owner ON chats(owner_id);
|
||||
CREATE INDEX idx_chats_workspace ON chats(workspace_id);
|
||||
CREATE INDEX idx_chats_pending ON chats(status) WHERE status = 'pending';
|
||||
CREATE INDEX idx_chats_parent_chat_id ON chats(parent_chat_id);
|
||||
CREATE INDEX idx_chats_root_chat_id ON chats(root_chat_id);
|
||||
CREATE INDEX idx_chats_last_model_config_id ON chats(last_model_config_id);
|
||||
|
||||
CREATE TABLE chat_messages (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
model_config_id UUID,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
role TEXT NOT NULL,
|
||||
content JSONB,
|
||||
visibility chat_message_visibility NOT NULL DEFAULT 'both',
|
||||
input_tokens BIGINT,
|
||||
output_tokens BIGINT,
|
||||
total_tokens BIGINT,
|
||||
reasoning_tokens BIGINT,
|
||||
cache_creation_tokens BIGINT,
|
||||
cache_read_tokens BIGINT,
|
||||
context_limit BIGINT,
|
||||
compressed BOOLEAN NOT NULL DEFAULT FALSE
|
||||
);
|
||||
|
||||
CREATE INDEX idx_chat_messages_chat ON chat_messages(chat_id);
|
||||
CREATE INDEX idx_chat_messages_chat_created ON chat_messages(chat_id, created_at);
|
||||
CREATE INDEX idx_chat_messages_compressed_summary_boundary
|
||||
ON chat_messages(chat_id, created_at DESC, id DESC)
|
||||
WHERE compressed = TRUE
|
||||
AND role = 'system'
|
||||
AND visibility IN ('model', 'both');
|
||||
|
||||
CREATE TABLE chat_diff_statuses (
|
||||
chat_id UUID PRIMARY KEY REFERENCES chats(id) ON DELETE CASCADE,
|
||||
url TEXT,
|
||||
pull_request_state TEXT,
|
||||
changes_requested BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
additions INTEGER NOT NULL DEFAULT 0,
|
||||
deletions INTEGER NOT NULL DEFAULT 0,
|
||||
changed_files INTEGER NOT NULL DEFAULT 0,
|
||||
refreshed_at TIMESTAMPTZ,
|
||||
stale_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
git_branch TEXT NOT NULL DEFAULT '',
|
||||
git_remote_origin TEXT NOT NULL DEFAULT ''
|
||||
);
|
||||
|
||||
CREATE INDEX idx_chat_diff_statuses_stale_at ON chat_diff_statuses(stale_at);
|
||||
|
||||
CREATE TABLE chat_providers (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
provider TEXT NOT NULL UNIQUE,
|
||||
display_name TEXT NOT NULL DEFAULT '',
|
||||
api_key TEXT NOT NULL DEFAULT '',
|
||||
api_key_key_id TEXT REFERENCES dbcrypt_keys(active_key_digest),
|
||||
created_by UUID REFERENCES users(id),
|
||||
enabled BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
base_url TEXT NOT NULL DEFAULT '',
|
||||
CONSTRAINT chat_providers_provider_check CHECK (
|
||||
provider = ANY (
|
||||
ARRAY[
|
||||
'anthropic'::text,
|
||||
'azure'::text,
|
||||
'bedrock'::text,
|
||||
'google'::text,
|
||||
'openai'::text,
|
||||
'openai-compat'::text,
|
||||
'openrouter'::text,
|
||||
'vercel'::text
|
||||
]
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
COMMENT ON COLUMN chat_providers.api_key_key_id IS 'The ID of the key used to encrypt the provider API key. If this is NULL, the API key is not encrypted';
|
||||
|
||||
CREATE INDEX idx_chat_providers_enabled ON chat_providers(enabled);
|
||||
|
||||
CREATE TABLE chat_model_configs (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
provider TEXT NOT NULL REFERENCES chat_providers(provider) ON DELETE CASCADE,
|
||||
model TEXT NOT NULL,
|
||||
display_name TEXT NOT NULL DEFAULT '',
|
||||
created_by UUID REFERENCES users(id),
|
||||
updated_by UUID REFERENCES users(id),
|
||||
enabled BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
is_default BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
deleted_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
context_limit BIGINT NOT NULL,
|
||||
compression_threshold INTEGER NOT NULL,
|
||||
options JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
CONSTRAINT chat_model_configs_context_limit_check
|
||||
CHECK (context_limit > 0),
|
||||
CONSTRAINT chat_model_configs_compression_threshold_check
|
||||
CHECK (compression_threshold >= 0 AND compression_threshold <= 100)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_chat_model_configs_enabled ON chat_model_configs(enabled);
|
||||
CREATE INDEX idx_chat_model_configs_provider ON chat_model_configs(provider);
|
||||
CREATE INDEX idx_chat_model_configs_provider_model
|
||||
ON chat_model_configs(provider, model);
|
||||
CREATE UNIQUE INDEX idx_chat_model_configs_single_default
|
||||
ON chat_model_configs ((1))
|
||||
WHERE is_default = TRUE
|
||||
AND deleted = FALSE;
|
||||
|
||||
ALTER TABLE chat_messages
|
||||
ADD CONSTRAINT chat_messages_model_config_id_fkey
|
||||
FOREIGN KEY (model_config_id) REFERENCES chat_model_configs(id);
|
||||
|
||||
ALTER TABLE chats
|
||||
ADD CONSTRAINT chats_last_model_config_id_fkey
|
||||
FOREIGN KEY (last_model_config_id) REFERENCES chat_model_configs(id);
|
||||
|
||||
CREATE TABLE chat_queued_messages (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
content JSONB NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_chat_queued_messages_chat_id ON chat_queued_messages(chat_id);
|
||||
|
||||
ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'chat:create';
|
||||
ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'chat:read';
|
||||
ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'chat:update';
|
||||
ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'chat:delete';
|
||||
ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'chat:*';
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
INSERT INTO chat_providers (
|
||||
id,
|
||||
provider,
|
||||
display_name,
|
||||
api_key,
|
||||
api_key_key_id,
|
||||
enabled,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
'0a8b2f84-b5a8-4c44-8c9f-e58c44a534a7',
|
||||
'openai',
|
||||
'OpenAI',
|
||||
'',
|
||||
NULL,
|
||||
TRUE,
|
||||
'2024-01-01 00:00:00+00',
|
||||
'2024-01-01 00:00:00+00'
|
||||
);
|
||||
|
||||
INSERT INTO chat_model_configs (
|
||||
id,
|
||||
provider,
|
||||
model,
|
||||
display_name,
|
||||
enabled,
|
||||
context_limit,
|
||||
compression_threshold,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
'9af5f8d5-6a57-4505-8a69-3d6c787b95fd',
|
||||
'openai',
|
||||
'gpt-5.2',
|
||||
'GPT 5.2',
|
||||
TRUE,
|
||||
200000,
|
||||
70,
|
||||
'2024-01-01 00:00:00+00',
|
||||
'2024-01-01 00:00:00+00'
|
||||
);
|
||||
|
||||
INSERT INTO chats (
|
||||
id,
|
||||
owner_id,
|
||||
last_model_config_id,
|
||||
title,
|
||||
status,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
SELECT
|
||||
'72c0438a-18eb-4688-ab80-e4c6a126ef96',
|
||||
id,
|
||||
'9af5f8d5-6a57-4505-8a69-3d6c787b95fd',
|
||||
'Fixture Chat',
|
||||
'completed',
|
||||
'2024-01-01 00:00:00+00',
|
||||
'2024-01-01 00:00:00+00'
|
||||
FROM users
|
||||
ORDER BY created_at, id
|
||||
LIMIT 1;
|
||||
|
||||
INSERT INTO chat_messages (
|
||||
chat_id,
|
||||
created_at,
|
||||
role,
|
||||
content
|
||||
) VALUES (
|
||||
'72c0438a-18eb-4688-ab80-e4c6a126ef96',
|
||||
'2024-01-01 00:00:00+00',
|
||||
'assistant',
|
||||
'{"type":"text","text":"fixture"}'::jsonb
|
||||
);
|
||||
|
||||
INSERT INTO chat_diff_statuses (
|
||||
chat_id,
|
||||
url,
|
||||
pull_request_state,
|
||||
changes_requested,
|
||||
additions,
|
||||
deletions,
|
||||
changed_files,
|
||||
refreshed_at,
|
||||
stale_at,
|
||||
created_at,
|
||||
updated_at,
|
||||
git_branch,
|
||||
git_remote_origin
|
||||
) VALUES (
|
||||
'72c0438a-18eb-4688-ab80-e4c6a126ef96',
|
||||
'https://example.com/pr/1',
|
||||
'open',
|
||||
FALSE,
|
||||
1,
|
||||
0,
|
||||
1,
|
||||
'2024-01-01 00:00:00+00',
|
||||
'2024-01-01 00:00:00+00',
|
||||
'2024-01-01 00:00:00+00',
|
||||
'2024-01-01 00:00:00+00',
|
||||
'main',
|
||||
'origin'
|
||||
);
|
||||
|
||||
INSERT INTO chat_queued_messages (
|
||||
chat_id,
|
||||
content,
|
||||
created_at
|
||||
) VALUES (
|
||||
'72c0438a-18eb-4688-ab80-e4c6a126ef96',
|
||||
'{"type":"text","text":"queued fixture"}'::jsonb,
|
||||
'2024-01-01 00:00:00+00'
|
||||
);
|
||||
Reference in New Issue
Block a user