mirror of
https://github.com/coder/coder.git
synced 2026-06-03 13:08:25 +00:00
b969d66978
Adds client-executed dynamic tools to the chat API. Dynamic tools are
declared by the client at chat creation time, presented to the LLM
alongside built-in tools, but executed by the client rather than chatd.
This enables external systems (Slack bots, IDE extensions, Discord bots,
CI/CD integrations) to plug custom tools into the LLM chat loop without
modifying chatd's built-in tool set.
Modeled after OpenAI's Assistants API: the chat pauses with
`requires_action` status when the LLM calls a dynamic tool, the client
POSTs results back via `POST /chats/{id}/tool-results`, and the chat
resumes.
See [this example](https://github.com/coder/coder-slackbot-poc) as a
reference for how this is used. It's highly-configurable, which would
enable creating chats from webhooks, periodically polling, or running as
a Slackbot.
<details>
<summary>Design context</summary>
### Architecture
The chatloop **exits** when it encounters dynamic tools and
**re-enters** when results arrive. No blocking channels, no pubsub for
tool results, no in-memory registry. The DB is the only coordination
mechanism.
```
Phase 1 (chatloop):
LLM response → execute built-in tools only →
Persist(assistant + built-in results) →
status = requires_action → chatloop exits
Phase 2 (POST /tool-results):
Persist(dynamic tool results) →
status = pending → wakeCh → chatloop re-enters
```
### Validation (POST /tool-results)
1. Chat status must be `requires_action` (409 if not)
2. Read chat's `dynamic_tools` → set of dynamic tool names
3. Read last assistant message → extract tool-call parts matching
dynamic tool names
4. Submitted tool_call_ids must match exactly (400 for missing/extra)
5. Persist tool-result message parts, set status to `pending`, signal
wake
### Idempotency
Tool call IDs scoped per LLM step. State machine (`requires_action` →
`pending`) is the guard. First POST wins, subsequent get 409.
### Mixed tool calls
When the LLM calls both built-in and dynamic tools in one step, built-in
tools execute immediately. Their results are persisted in phase 1.
Dynamic tool results arrive via POST in phase 2. The LLM sees all
results when the chatloop resumes.
</details>
> 🤖 Generated by Coder Agents
32 lines
1.2 KiB
SQL
32 lines
1.2 KiB
SQL
-- First update any rows using the value we're about to remove.
|
|
-- The column type is still the original chat_status at this point.
|
|
UPDATE chats SET status = 'error' WHERE status = 'requires_action';
|
|
|
|
-- Drop the column (this is independent of the enum).
|
|
ALTER TABLE chats DROP COLUMN IF EXISTS dynamic_tools;
|
|
|
|
-- Drop the partial index that references the chat_status enum type.
|
|
-- It must be removed before the rename-create-cast-drop cycle
|
|
-- because the index's WHERE clause (status = 'pending'::chat_status)
|
|
-- would otherwise cause a cross-type comparison failure.
|
|
DROP INDEX IF EXISTS idx_chats_pending;
|
|
|
|
-- Now recreate the enum without requires_action.
|
|
-- We must use the rename-create-cast-drop pattern.
|
|
ALTER TYPE chat_status RENAME TO chat_status_old;
|
|
CREATE TYPE chat_status AS ENUM (
|
|
'waiting',
|
|
'pending',
|
|
'running',
|
|
'paused',
|
|
'completed',
|
|
'error'
|
|
);
|
|
ALTER TABLE chats ALTER COLUMN status DROP DEFAULT;
|
|
ALTER TABLE chats ALTER COLUMN status TYPE chat_status USING status::text::chat_status;
|
|
ALTER TABLE chats ALTER COLUMN status SET DEFAULT 'waiting';
|
|
DROP TYPE chat_status_old;
|
|
|
|
-- Recreate the partial index.
|
|
CREATE INDEX idx_chats_pending ON chats USING btree (status) WHERE (status = 'pending'::chat_status);
|