Packages are accepted once by the instance. Installations configure them at the instance, an organisation or a person; bindings and credentials resolve the person's, then their organisation's, then the instance's. Decisions, approval requests and held chat actions record the installation and scope that acted. The dashboard lists packages separately from installations, shows every installation's scope, and creates, edits and disables installations at their scope.
107 lines
4.7 KiB
SQL
107 lines
4.7 KiB
SQL
-- +goose Up
|
|
CREATE SCHEMA chat;
|
|
|
|
-- A session is one conversation between the operator and Archie.
|
|
CREATE TABLE chat.sessions (
|
|
id uuid PRIMARY KEY,
|
|
org_id text NOT NULL,
|
|
title text NOT NULL DEFAULT '',
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
owner_id text NOT NULL,
|
|
tainted boolean NOT NULL DEFAULT false
|
|
);
|
|
CREATE INDEX sessions_by_owner ON chat.sessions (org_id, owner_id, updated_at DESC);
|
|
|
|
-- A turn is one message from the operator and Archie's reply to it. It is
|
|
-- stored before it is acknowledged and queued until the turn before it in the
|
|
-- same session has ended. Judgement and approvals record the turn's actions
|
|
-- with the turn's id as their run id.
|
|
CREATE TABLE chat.turns (
|
|
seq bigserial PRIMARY KEY,
|
|
id uuid NOT NULL UNIQUE,
|
|
org_id text NOT NULL,
|
|
session_id uuid NOT NULL REFERENCES chat.sessions (id) ON DELETE CASCADE,
|
|
state text NOT NULL CHECK (state IN ('queued', 'running', 'succeeded', 'failed', 'cancelled')),
|
|
text text NOT NULL,
|
|
page text NOT NULL DEFAULT '',
|
|
page_label text NOT NULL DEFAULT '',
|
|
reply text NOT NULL DEFAULT '',
|
|
reasoning text NOT NULL DEFAULT '',
|
|
error text NOT NULL DEFAULT '',
|
|
cancel boolean NOT NULL DEFAULT false,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
started_at timestamptz,
|
|
finished_at timestamptz,
|
|
input_tokens bigint NOT NULL DEFAULT 0,
|
|
output_tokens bigint NOT NULL DEFAULT 0
|
|
);
|
|
CREATE INDEX turns_by_session ON chat.turns (session_id, seq);
|
|
CREATE INDEX turns_queued ON chat.turns (seq) WHERE (state IN ('queued', 'running'));
|
|
|
|
-- An event is one item a turn streamed: reasoning, reply text, a tool call or
|
|
-- its result, an approval request, or how the turn ended. A client resumes the
|
|
-- stream by reading the events after the last seq it received.
|
|
CREATE TABLE chat.events (
|
|
seq bigserial PRIMARY KEY,
|
|
turn_id uuid NOT NULL REFERENCES chat.turns (id) ON DELETE CASCADE,
|
|
kind text NOT NULL,
|
|
data jsonb NOT NULL,
|
|
created_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
CREATE INDEX events_by_turn ON chat.events (turn_id, seq);
|
|
|
|
-- An action is a tool call by a turn's agent that judgement held for approval.
|
|
-- The answer arrives through approvals, and an approved action runs once.
|
|
CREATE TABLE chat.actions (
|
|
id uuid PRIMARY KEY,
|
|
org_id text NOT NULL,
|
|
turn_id uuid NOT NULL REFERENCES chat.turns (id) ON DELETE CASCADE,
|
|
key text NOT NULL,
|
|
decision jsonb NOT NULL,
|
|
request_id uuid NOT NULL,
|
|
path jsonb NOT NULL,
|
|
step integer NOT NULL DEFAULT 0,
|
|
step_due timestamptz NOT NULL,
|
|
state text NOT NULL CHECK (state IN ('waiting', 'approved', 'running', 'succeeded', 'failed', 'unknown', 'denied', 'expired')),
|
|
answer jsonb,
|
|
result text NOT NULL DEFAULT '',
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
binding jsonb,
|
|
binding_digest text NOT NULL DEFAULT '',
|
|
installation_id uuid NOT NULL,
|
|
installation_scope text NOT NULL CHECK (installation_scope IN ('instance', 'org', 'person')),
|
|
UNIQUE (turn_id, key)
|
|
);
|
|
CREATE INDEX actions_open ON chat.actions (state) WHERE (state IN ('waiting', 'approved', 'running'));
|
|
|
|
-- Usage counts the turns, tool calls and tokens of one person or one whole
|
|
-- organisation in a fixed window, so the limits in governance settings hold
|
|
-- across sessions, concurrent turns and restarts. A turn or tool call is
|
|
-- counted, under the limit, in the same statement that checks it.
|
|
CREATE TABLE chat.usage (
|
|
org_id text NOT NULL,
|
|
subject_kind text NOT NULL CHECK (subject_kind IN ('person', 'org')),
|
|
subject_id text NOT NULL,
|
|
metric text NOT NULL CHECK (metric IN ('turns', 'tool_calls', 'tokens')),
|
|
window_start timestamptz NOT NULL,
|
|
count bigint NOT NULL CHECK (count >= 0),
|
|
PRIMARY KEY (org_id, subject_kind, subject_id, metric, window_start),
|
|
CHECK ((subject_kind <> 'org') OR (subject_id = org_id))
|
|
);
|
|
|
|
-- A tool switched off for chat. A disabled tool is left out of a turn's tool
|
|
-- list and refused if the model calls it anyway. An empty action switches off
|
|
-- every action of the installation.
|
|
CREATE TABLE chat.disabled_tools (
|
|
installation_id uuid NOT NULL,
|
|
action text NOT NULL DEFAULT '',
|
|
reason text NOT NULL DEFAULT '',
|
|
disabled_by_id text NOT NULL,
|
|
disabled_at timestamptz NOT NULL DEFAULT now(),
|
|
PRIMARY KEY (installation_id, action)
|
|
);
|
|
|
|
-- +goose Down
|
|
DROP SCHEMA chat CASCADE;
|