feat: initialize server structure and database schema

- Added initial server setup with Cargo.toml defining dependencies and project structure.
- Created build.rs for generating protobuf bindings and validating wire contracts.
- Established database schema with initial migration files for conversations, messages, and runs.
- Introduced tools and prompts for Cursor functionality, enhancing user interaction capabilities.
This commit is contained in:
leookun
2026-08-30 01:33:39 +08:00
parent d200b3791d
commit 44e2d8057a
206 changed files with 37923 additions and 21 deletions
+278
View File
@@ -0,0 +1,278 @@
PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS conversations (
conversation_id TEXT PRIMARY KEY,
current_revision_id INTEGER,
active_run_id TEXT,
updated_at_ms INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS messages (
conversation_id TEXT NOT NULL,
message_id TEXT NOT NULL,
role TEXT NOT NULL,
origin TEXT NOT NULL,
payload_json TEXT NOT NULL,
runtime_event_id TEXT,
created_at_ms INTEGER NOT NULL,
PRIMARY KEY (conversation_id, message_id),
FOREIGN KEY (conversation_id) REFERENCES conversations(conversation_id)
);
CREATE UNIQUE INDEX IF NOT EXISTS messages_runtime_event
ON messages(conversation_id, runtime_event_id)
WHERE runtime_event_id IS NOT NULL;
CREATE TABLE IF NOT EXISTS conversation_revisions (
revision_id INTEGER PRIMARY KEY AUTOINCREMENT,
conversation_id TEXT NOT NULL,
parent_revision_id INTEGER,
state_digest BLOB NOT NULL CHECK(length(state_digest) = 32),
created_at_ms INTEGER NOT NULL,
UNIQUE (conversation_id, state_digest),
FOREIGN KEY (conversation_id) REFERENCES conversations(conversation_id),
FOREIGN KEY (parent_revision_id) REFERENCES conversation_revisions(revision_id)
);
CREATE INDEX IF NOT EXISTS conversation_revisions_parent
ON conversation_revisions(conversation_id, parent_revision_id);
CREATE TABLE IF NOT EXISTS revision_messages (
revision_id INTEGER NOT NULL,
ordinal INTEGER NOT NULL,
conversation_id TEXT NOT NULL,
message_id TEXT NOT NULL,
PRIMARY KEY (revision_id, ordinal),
UNIQUE (revision_id, message_id),
FOREIGN KEY (revision_id) REFERENCES conversation_revisions(revision_id),
FOREIGN KEY (conversation_id, message_id) REFERENCES messages(conversation_id, message_id)
);
CREATE TABLE IF NOT EXISTS runs (
run_id TEXT PRIMARY KEY,
conversation_id TEXT NOT NULL,
base_revision_id INTEGER NOT NULL,
head_revision_id INTEGER NOT NULL,
parent_run_id TEXT,
parent_tool_call_id TEXT,
run_kind TEXT NOT NULL,
subagent_kind TEXT,
status TEXT NOT NULL,
provider_call_index INTEGER NOT NULL DEFAULT -1,
turn_usage_json TEXT NOT NULL DEFAULT 'null',
failure_category TEXT,
failure_summary TEXT,
created_at_ms INTEGER NOT NULL,
updated_at_ms INTEGER NOT NULL,
FOREIGN KEY (conversation_id) REFERENCES conversations(conversation_id),
FOREIGN KEY (base_revision_id) REFERENCES conversation_revisions(revision_id),
FOREIGN KEY (head_revision_id) REFERENCES conversation_revisions(revision_id)
);
CREATE INDEX IF NOT EXISTS runs_conversation_status
ON runs(conversation_id, status);
CREATE TABLE IF NOT EXISTS tool_rounds (
round_id TEXT PRIMARY KEY,
run_id TEXT NOT NULL,
base_revision_id INTEGER NOT NULL,
assistant_json TEXT NOT NULL,
status TEXT NOT NULL,
version INTEGER NOT NULL DEFAULT 0,
next_completion_seq INTEGER NOT NULL DEFAULT 0,
created_at_ms INTEGER NOT NULL,
updated_at_ms INTEGER NOT NULL,
FOREIGN KEY (run_id) REFERENCES runs(run_id),
FOREIGN KEY (base_revision_id) REFERENCES conversation_revisions(revision_id)
);
CREATE INDEX IF NOT EXISTS tool_rounds_run_status
ON tool_rounds(run_id, status);
CREATE TABLE IF NOT EXISTS tool_round_calls (
round_id TEXT NOT NULL,
call_index INTEGER NOT NULL,
call_id TEXT NOT NULL,
model_call_id TEXT NOT NULL,
name TEXT NOT NULL,
arguments_json TEXT NOT NULL,
status TEXT NOT NULL,
completion_seq INTEGER,
result_content TEXT,
result_is_error INTEGER,
committed_revision_id INTEGER,
completed_at_ms INTEGER,
PRIMARY KEY (round_id, call_index),
UNIQUE (round_id, call_id),
UNIQUE (round_id, completion_seq),
FOREIGN KEY (round_id) REFERENCES tool_rounds(round_id),
FOREIGN KEY (committed_revision_id) REFERENCES conversation_revisions(revision_id)
);
CREATE TABLE IF NOT EXISTS blobs (
blob_id BLOB PRIMARY KEY CHECK(length(blob_id) = 32),
data BLOB NOT NULL,
created_at_ms INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS blob_edges (
parent_blob_id BLOB NOT NULL,
child_blob_id BLOB NOT NULL,
field_name TEXT NOT NULL,
PRIMARY KEY (parent_blob_id, child_blob_id, field_name),
FOREIGN KEY (parent_blob_id) REFERENCES blobs(blob_id),
FOREIGN KEY (child_blob_id) REFERENCES blobs(blob_id)
);
CREATE INDEX IF NOT EXISTS blob_edges_child ON blob_edges(child_blob_id);
CREATE TABLE IF NOT EXISTS input_anchors (
conversation_id TEXT NOT NULL,
input_id TEXT NOT NULL,
base_revision_id INTEGER NOT NULL,
created_at_ms INTEGER NOT NULL,
PRIMARY KEY (conversation_id, input_id),
FOREIGN KEY (conversation_id) REFERENCES conversations(conversation_id),
FOREIGN KEY (base_revision_id) REFERENCES conversation_revisions(revision_id)
);
CREATE TABLE IF NOT EXISTS provider_endpoints (
provider_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
provider_type TEXT NOT NULL,
base_url TEXT NOT NULL,
api_key TEXT NOT NULL,
custom_headers_json TEXT NOT NULL DEFAULT '{}',
extra_params_json TEXT NOT NULL DEFAULT '{}',
created_at_ms INTEGER NOT NULL,
updated_at_ms INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS provider_models (
model_hash TEXT PRIMARY KEY CHECK(length(model_hash) = 8),
provider_id INTEGER NOT NULL,
model_id TEXT NOT NULL,
display_name TEXT NOT NULL,
endpoint_type TEXT NOT NULL,
request_url TEXT NOT NULL DEFAULT '',
enabled INTEGER NOT NULL DEFAULT 1,
sort_order INTEGER NOT NULL DEFAULT 0,
context_window_tokens INTEGER,
max_output_tokens INTEGER,
reasoning_enabled INTEGER NOT NULL DEFAULT 0,
reasoning_effort TEXT,
supports_image_generation INTEGER NOT NULL DEFAULT 0,
created_at_ms INTEGER NOT NULL,
updated_at_ms INTEGER NOT NULL,
UNIQUE(provider_id, model_id),
FOREIGN KEY(provider_id) REFERENCES provider_endpoints(provider_id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS provider_models_enabled_sort
ON provider_models(enabled, sort_order, display_name);
CREATE TABLE IF NOT EXISTS service_settings (
setting_key TEXT PRIMARY KEY,
value_json TEXT NOT NULL,
updated_at_ms INTEGER NOT NULL
);
INSERT OR IGNORE INTO service_settings(setting_key, value_json, updated_at_ms)
VALUES ('llm_detailed_logging', 'false', unixepoch('subsec') * 1000);
CREATE TABLE IF NOT EXISTS llm_calls (
call_id TEXT PRIMARY KEY,
run_id TEXT NOT NULL,
conversation_id TEXT NOT NULL,
provider_call_index INTEGER NOT NULL,
model_hash TEXT,
provider_type TEXT NOT NULL,
provider_url TEXT NOT NULL,
request_type TEXT NOT NULL,
request_url TEXT NOT NULL,
model_id TEXT NOT NULL,
display_name TEXT NOT NULL,
status TEXT NOT NULL,
finish_reason TEXT,
created_at_ms INTEGER NOT NULL,
request_started_at_ms INTEGER,
response_headers_at_ms INTEGER,
first_event_at_ms INTEGER,
first_text_at_ms INTEGER,
finished_at_ms INTEGER,
queue_ms INTEGER,
ttfb_ms INTEGER,
ttft_ms INTEGER,
duration_ms INTEGER,
input_tokens INTEGER,
output_tokens INTEGER,
total_tokens INTEGER,
cache_read_tokens INTEGER,
cache_write_tokens INTEGER,
reasoning_tokens INTEGER,
usage_json TEXT,
message_count INTEGER NOT NULL,
tool_count INTEGER NOT NULL,
request_bytes INTEGER,
response_bytes INTEGER NOT NULL DEFAULT 0,
stream_event_count INTEGER NOT NULL DEFAULT 0,
http_status INTEGER,
error_kind TEXT,
error_message TEXT,
detailed INTEGER NOT NULL,
FOREIGN KEY(model_hash) REFERENCES provider_models(model_hash)
);
CREATE INDEX IF NOT EXISTS llm_calls_created ON llm_calls(created_at_ms DESC);
CREATE INDEX IF NOT EXISTS llm_calls_run ON llm_calls(run_id, provider_call_index);
CREATE INDEX IF NOT EXISTS llm_calls_model ON llm_calls(model_hash, created_at_ms DESC);
CREATE TABLE IF NOT EXISTS llm_call_requests (
call_id TEXT PRIMARY KEY,
headers_json TEXT NOT NULL,
body_json TEXT NOT NULL,
byte_count INTEGER NOT NULL,
FOREIGN KEY(call_id) REFERENCES llm_calls(call_id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS llm_call_response_chunks (
call_id TEXT NOT NULL,
seq INTEGER NOT NULL,
received_offset_ms INTEGER NOT NULL,
data BLOB NOT NULL,
byte_count INTEGER NOT NULL,
PRIMARY KEY(call_id, seq),
FOREIGN KEY(call_id) REFERENCES llm_calls(call_id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS cursor_run_traces (
request_id TEXT PRIMARY KEY,
conversation_id TEXT,
route TEXT NOT NULL CHECK(route IN ('local_byok', 'cursor_official')),
model_id TEXT,
status TEXT NOT NULL,
request_bytes INTEGER NOT NULL DEFAULT 0,
response_bytes INTEGER NOT NULL DEFAULT 0,
response_event_count INTEGER NOT NULL DEFAULT 0,
http_status INTEGER,
received_at_ms INTEGER NOT NULL,
first_response_at_ms INTEGER,
finished_at_ms INTEGER,
error_message TEXT
);
CREATE INDEX IF NOT EXISTS cursor_run_traces_received
ON cursor_run_traces(received_at_ms DESC);
CREATE TABLE IF NOT EXISTS cursor_run_trace_artifacts (
request_id TEXT NOT NULL,
seq INTEGER NOT NULL,
artifact_type TEXT NOT NULL,
source TEXT NOT NULL CHECK(source IN ('cursor_client', 'byok_server', 'cursor_official')),
blob_id BLOB NOT NULL CHECK(length(blob_id) = 32),
metadata_json TEXT NOT NULL DEFAULT '{}',
created_at_ms INTEGER NOT NULL,
PRIMARY KEY(request_id, seq),
FOREIGN KEY(request_id) REFERENCES cursor_run_traces(request_id) ON DELETE CASCADE,
FOREIGN KEY(blob_id) REFERENCES blobs(blob_id)
);
@@ -0,0 +1,3 @@
-- Persist the effective Cursor model options for each local provider call.
ALTER TABLE llm_calls ADD COLUMN reasoning_effort TEXT;
ALTER TABLE llm_calls ADD COLUMN fast INTEGER NOT NULL DEFAULT 0 CHECK (fast IN (0, 1));
@@ -0,0 +1,6 @@
-- Cursor may reuse one transport request id for multiple queued executions.
-- Keep that id as an association key while each local Run keeps its own identity.
ALTER TABLE runs ADD COLUMN cursor_request_id TEXT;
CREATE INDEX idx_runs_cursor_request_active
ON runs(cursor_request_id, status, created_at_ms DESC);
@@ -0,0 +1,197 @@
PRAGMA defer_foreign_keys = ON;
CREATE TABLE model_configs (
model_hash TEXT PRIMARY KEY,
sort_order INTEGER NOT NULL DEFAULT 0,
display_name TEXT NOT NULL,
model_type TEXT NOT NULL CHECK(model_type IN ('openai', 'anthropic')),
base_url TEXT NOT NULL,
use_full_url INTEGER NOT NULL DEFAULT 0 CHECK(use_full_url IN (0, 1)),
api_key TEXT NOT NULL,
tooltip_data TEXT NOT NULL,
model_id TEXT NOT NULL,
reasoning_effort TEXT,
openai_endpoint TEXT NOT NULL DEFAULT '',
openai_extra_params_enabled INTEGER NOT NULL DEFAULT 0 CHECK(openai_extra_params_enabled IN (0, 1)),
openai_extra_params_json TEXT NOT NULL DEFAULT '{}',
custom_headers_enabled INTEGER NOT NULL DEFAULT 0 CHECK(custom_headers_enabled IN (0, 1)),
custom_headers_json TEXT NOT NULL DEFAULT '{}',
anthropic_extra_params_enabled INTEGER NOT NULL DEFAULT 0 CHECK(anthropic_extra_params_enabled IN (0, 1)),
anthropic_extra_params_json TEXT NOT NULL DEFAULT '{}',
context_window_tokens INTEGER,
max_completion_tokens INTEGER,
anthropic_max_tokens INTEGER,
anthropic_thinking_effort TEXT,
thinking_budget_tokens INTEGER,
created_at_ms INTEGER NOT NULL,
updated_at_ms INTEGER NOT NULL
);
INSERT INTO model_configs (
model_hash,
sort_order,
display_name,
model_type,
base_url,
use_full_url,
api_key,
tooltip_data,
model_id,
reasoning_effort,
openai_endpoint,
openai_extra_params_enabled,
openai_extra_params_json,
custom_headers_enabled,
custom_headers_json,
anthropic_extra_params_enabled,
anthropic_extra_params_json,
context_window_tokens,
max_completion_tokens,
anthropic_max_tokens,
anthropic_thinking_effort,
thinking_budget_tokens,
created_at_ms,
updated_at_ms
)
SELECT
model.model_hash,
model.sort_order,
model.display_name,
CASE model.endpoint_type WHEN 'anthropic' THEN 'anthropic' ELSE 'openai' END,
CASE
WHEN model.request_url = '' THEN endpoint.base_url
WHEN model.request_url LIKE 'http://%' OR model.request_url LIKE 'https://%' THEN model.request_url
ELSE replace(rtrim(endpoint.base_url, '/') || '/' || ltrim(model.request_url, '/'), '/v1/v1/', '/v1/')
END,
CASE WHEN model.request_url = '' THEN 0 ELSE 1 END,
endpoint.api_key,
model.display_name,
model.model_id,
CASE
WHEN model.endpoint_type != 'anthropic' AND model.reasoning_enabled = 1
THEN COALESCE(NULLIF(trim(model.reasoning_effort), ''), 'medium')
ELSE NULL
END,
CASE model.endpoint_type
WHEN 'openai-responses' THEN '/v1/responses'
WHEN 'openai-chat' THEN '/v1/chat/completions'
ELSE ''
END,
CASE WHEN model.endpoint_type != 'anthropic' AND endpoint.extra_params_json != '{}' THEN 1 ELSE 0 END,
CASE WHEN model.endpoint_type != 'anthropic' THEN endpoint.extra_params_json ELSE '{}' END,
CASE WHEN endpoint.custom_headers_json != '{}' THEN 1 ELSE 0 END,
endpoint.custom_headers_json,
CASE WHEN model.endpoint_type = 'anthropic' AND endpoint.extra_params_json != '{}' THEN 1 ELSE 0 END,
CASE WHEN model.endpoint_type = 'anthropic' THEN endpoint.extra_params_json ELSE '{}' END,
model.context_window_tokens,
CASE WHEN model.endpoint_type != 'anthropic' THEN model.max_output_tokens ELSE NULL END,
CASE WHEN model.endpoint_type = 'anthropic' THEN model.max_output_tokens ELSE NULL END,
CASE WHEN model.endpoint_type = 'anthropic' THEN 'xhigh' ELSE NULL END,
NULL,
model.created_at_ms,
model.updated_at_ms
FROM provider_models AS model
JOIN provider_endpoints AS endpoint ON endpoint.provider_id = model.provider_id;
CREATE TABLE llm_calls_new (
call_id TEXT PRIMARY KEY,
run_id TEXT NOT NULL,
conversation_id TEXT NOT NULL,
provider_call_index INTEGER NOT NULL,
model_hash TEXT,
provider_type TEXT NOT NULL,
provider_url TEXT NOT NULL,
request_type TEXT NOT NULL,
request_url TEXT NOT NULL,
model_id TEXT NOT NULL,
display_name TEXT NOT NULL,
status TEXT NOT NULL,
finish_reason TEXT,
created_at_ms INTEGER NOT NULL,
request_started_at_ms INTEGER,
response_headers_at_ms INTEGER,
first_event_at_ms INTEGER,
first_text_at_ms INTEGER,
finished_at_ms INTEGER,
queue_ms INTEGER,
ttfb_ms INTEGER,
ttft_ms INTEGER,
duration_ms INTEGER,
input_tokens INTEGER,
output_tokens INTEGER,
total_tokens INTEGER,
cache_read_tokens INTEGER,
cache_write_tokens INTEGER,
reasoning_tokens INTEGER,
usage_json TEXT,
message_count INTEGER NOT NULL,
tool_count INTEGER NOT NULL,
request_bytes INTEGER,
response_bytes INTEGER NOT NULL DEFAULT 0,
stream_event_count INTEGER NOT NULL DEFAULT 0,
http_status INTEGER,
error_kind TEXT,
error_message TEXT,
detailed INTEGER NOT NULL,
reasoning_effort TEXT,
fast INTEGER NOT NULL DEFAULT 0 CHECK (fast IN (0, 1)),
FOREIGN KEY(model_hash) REFERENCES model_configs(model_hash)
);
INSERT INTO llm_calls_new (
call_id, run_id, conversation_id, provider_call_index, model_hash, provider_type,
provider_url, request_type, request_url, model_id, display_name, status, finish_reason,
created_at_ms, request_started_at_ms, response_headers_at_ms, first_event_at_ms,
first_text_at_ms, finished_at_ms, queue_ms, ttfb_ms, ttft_ms, duration_ms,
input_tokens, output_tokens, total_tokens, cache_read_tokens, cache_write_tokens,
reasoning_tokens, usage_json, message_count, tool_count, request_bytes, response_bytes,
stream_event_count, http_status, error_kind, error_message, detailed, reasoning_effort, fast
)
SELECT
call_id, run_id, conversation_id, provider_call_index, model_hash, provider_type,
provider_url, request_type, request_url, model_id, display_name, status, finish_reason,
created_at_ms, request_started_at_ms, response_headers_at_ms, first_event_at_ms,
first_text_at_ms, finished_at_ms, queue_ms, ttfb_ms, ttft_ms, duration_ms,
input_tokens, output_tokens, total_tokens, cache_read_tokens, cache_write_tokens,
reasoning_tokens, usage_json, message_count, tool_count, request_bytes, response_bytes,
stream_event_count, http_status, error_kind, error_message, detailed, reasoning_effort, fast
FROM llm_calls;
CREATE TABLE llm_call_requests_new (
call_id TEXT PRIMARY KEY,
headers_json TEXT NOT NULL,
body_json TEXT NOT NULL,
byte_count INTEGER NOT NULL,
FOREIGN KEY(call_id) REFERENCES llm_calls_new(call_id) ON DELETE CASCADE
);
INSERT INTO llm_call_requests_new(call_id, headers_json, body_json, byte_count)
SELECT call_id, headers_json, body_json, byte_count FROM llm_call_requests;
CREATE TABLE llm_call_response_chunks_new (
call_id TEXT NOT NULL,
seq INTEGER NOT NULL,
received_offset_ms INTEGER NOT NULL,
data BLOB NOT NULL,
byte_count INTEGER NOT NULL,
PRIMARY KEY(call_id, seq),
FOREIGN KEY(call_id) REFERENCES llm_calls_new(call_id) ON DELETE CASCADE
);
INSERT INTO llm_call_response_chunks_new(call_id, seq, received_offset_ms, data, byte_count)
SELECT call_id, seq, received_offset_ms, data, byte_count FROM llm_call_response_chunks;
DROP TABLE llm_call_requests;
DROP TABLE llm_call_response_chunks;
DROP TABLE llm_calls;
DROP TABLE provider_models;
DROP TABLE provider_endpoints;
ALTER TABLE llm_calls_new RENAME TO llm_calls;
ALTER TABLE llm_call_requests_new RENAME TO llm_call_requests;
ALTER TABLE llm_call_response_chunks_new RENAME TO llm_call_response_chunks;
CREATE INDEX model_configs_sort ON model_configs(sort_order, display_name);
CREATE INDEX llm_calls_created ON llm_calls(created_at_ms DESC);
CREATE INDEX llm_calls_run ON llm_calls(run_id, provider_call_index);
CREATE INDEX llm_calls_model ON llm_calls(model_hash, created_at_ms DESC);
@@ -0,0 +1,2 @@
ALTER TABLE llm_calls ADD COLUMN first_valid_response_at_ms INTEGER;
ALTER TABLE llm_calls ADD COLUMN ttfr_ms INTEGER;
@@ -0,0 +1,18 @@
-- Renames persisted Conversation history terminology without changing identities or data.
ALTER TABLE conversation_revisions RENAME TO conversation_checkpoints;
ALTER TABLE conversation_checkpoints RENAME COLUMN revision_id TO checkpoint_id;
ALTER TABLE conversation_checkpoints RENAME COLUMN parent_revision_id TO parent_checkpoint_id;
ALTER TABLE revision_messages RENAME TO checkpoint_messages;
ALTER TABLE checkpoint_messages RENAME COLUMN revision_id TO checkpoint_id;
ALTER TABLE conversations RENAME COLUMN current_revision_id TO current_checkpoint_id;
ALTER TABLE runs RENAME COLUMN base_revision_id TO base_checkpoint_id;
ALTER TABLE runs RENAME COLUMN head_revision_id TO head_checkpoint_id;
ALTER TABLE tool_rounds RENAME COLUMN base_revision_id TO base_checkpoint_id;
ALTER TABLE tool_round_calls RENAME COLUMN committed_revision_id TO committed_checkpoint_id;
ALTER TABLE input_anchors RENAME COLUMN base_revision_id TO base_checkpoint_id;
DROP INDEX conversation_revisions_parent;
CREATE INDEX conversation_checkpoints_parent
ON conversation_checkpoints(conversation_id, parent_checkpoint_id);