initSchema creates the aibridge tables following the coder/coder data model. See: https://github.com/coder/coder/blob/c87c33f7dda82eb91ee8ba9504f749101bb367d6/coderd/database/dump.sql#L1052-L1115
(db *sql.DB)
| 5 | // initSchema creates the aibridge tables following the coder/coder data model. |
| 6 | // See: https://github.com/coder/coder/blob/c87c33f7dda82eb91ee8ba9504f749101bb367d6/coderd/database/dump.sql#L1052-L1115 |
| 7 | func initSchema(db *sql.DB) error { |
| 8 | schema := ` |
| 9 | CREATE TABLE IF NOT EXISTS aibridge_interceptions ( |
| 10 | id TEXT PRIMARY KEY, |
| 11 | initiator_id TEXT NOT NULL, |
| 12 | provider TEXT NOT NULL, |
| 13 | model TEXT NOT NULL, |
| 14 | started_at DATETIME NOT NULL, |
| 15 | ended_at DATETIME, |
| 16 | metadata TEXT |
| 17 | ); |
| 18 | |
| 19 | CREATE TABLE IF NOT EXISTS aibridge_token_usages ( |
| 20 | id TEXT PRIMARY KEY, |
| 21 | interception_id TEXT NOT NULL, |
| 22 | provider_response_id TEXT NOT NULL, |
| 23 | input_tokens INTEGER NOT NULL, |
| 24 | output_tokens INTEGER NOT NULL, |
| 25 | cache_read_input_tokens INTEGER NOT NULL DEFAULT 0, |
| 26 | cache_write_input_tokens INTEGER NOT NULL DEFAULT 0, |
| 27 | metadata TEXT, |
| 28 | created_at DATETIME NOT NULL, |
| 29 | FOREIGN KEY (interception_id) REFERENCES aibridge_interceptions(id) |
| 30 | ); |
| 31 | |
| 32 | CREATE TABLE IF NOT EXISTS aibridge_user_prompts ( |
| 33 | id TEXT PRIMARY KEY, |
| 34 | interception_id TEXT NOT NULL, |
| 35 | provider_response_id TEXT NOT NULL, |
| 36 | prompt TEXT NOT NULL, |
| 37 | metadata TEXT, |
| 38 | created_at DATETIME NOT NULL, |
| 39 | FOREIGN KEY (interception_id) REFERENCES aibridge_interceptions(id) |
| 40 | ); |
| 41 | |
| 42 | CREATE TABLE IF NOT EXISTS aibridge_tool_usages ( |
| 43 | id TEXT PRIMARY KEY, |
| 44 | interception_id TEXT NOT NULL, |
| 45 | provider_response_id TEXT NOT NULL, |
| 46 | server_url TEXT, |
| 47 | tool TEXT NOT NULL, |
| 48 | input TEXT NOT NULL, |
| 49 | injected BOOLEAN NOT NULL DEFAULT FALSE, |
| 50 | invocation_error TEXT, |
| 51 | metadata TEXT, |
| 52 | created_at DATETIME NOT NULL, |
| 53 | FOREIGN KEY (interception_id) REFERENCES aibridge_interceptions(id) |
| 54 | ); |
| 55 | |
| 56 | CREATE INDEX IF NOT EXISTS idx_interceptions_initiator ON aibridge_interceptions(initiator_id); |
| 57 | CREATE INDEX IF NOT EXISTS idx_token_usages_interception ON aibridge_token_usages(interception_id); |
| 58 | CREATE INDEX IF NOT EXISTS idx_user_prompts_interception ON aibridge_user_prompts(interception_id); |
| 59 | CREATE INDEX IF NOT EXISTS idx_tool_usages_interception ON aibridge_tool_usages(interception_id); |
| 60 | ` |
| 61 | _, err := db.Exec(schema) |
| 62 | return err |
| 63 | } |