Skip to content

Commit 1cf3456

Browse files
committed
wip: agent model
1 parent d5f7c35 commit 1cf3456

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

backend/devtools/create_migrate.sh

100644100755
File mode changed.

backend/internal/core/agent.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package core
2+
3+
import (
4+
"time"
5+
6+
"github.com/gofrs/uuid/v5"
7+
)
8+
9+
type Agent struct {
10+
ID uuid.UUID `db:"id"`
11+
OrganizationID uuid.UUID `db:"organization_id"`
12+
EnvironmentID uuid.UUID `db:"environment_id"`
13+
APIKeyID uuid.UUID `db:"api_key_id"`
14+
Name string `db:"name"`
15+
Description string `db:"description"`
16+
Instructions string `db:"instructions"`
17+
Model string `db:"model"`
18+
CreatedAt time.Time `db:"created_at"`
19+
UpdatedAt time.Time `db:"updated_at"`
20+
}
21+
22+
type AgentTool struct {
23+
ID uuid.UUID `db:"id"`
24+
AgentID uuid.UUID `db:"agent_id"`
25+
Name string `db:"name"`
26+
Description string `db:"description"`
27+
CreatedAt time.Time `db:"created_at"`
28+
UpdatedAt time.Time `db:"updated_at"`
29+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
BEGIN;
2+
3+
-- drop agent_tool
4+
DROP TRIGGER IF EXISTS update_agent_tool_updated_at ON "agent_tool";
5+
DROP INDEX IF EXISTS idx_agent_tool_agent_name;
6+
DROP TABLE IF EXISTS "agent_tool";
7+
8+
-- drop agent
9+
DROP TRIGGER IF EXISTS validate_agent ON "agent";
10+
DROP TRIGGER IF EXISTS update_agent_updated_at ON "agent";
11+
DROP INDEX IF EXISTS idx_agent_organization_api_key_name;
12+
DROP TABLE IF EXISTS "agent";
13+
14+
-- drop function
15+
DROP FUNCTION IF EXISTS validate_agent();
16+
17+
END;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
BEGIN;
2+
3+
-- agent table
4+
5+
CREATE OR REPLACE FUNCTION validate_agent()
6+
RETURNS TRIGGER AS $$
7+
DECLARE
8+
environment_org_id UUID;
9+
api_key_org_id UUID;
10+
BEGIN
11+
SELECT organization_id INTO environment_org_id
12+
FROM "environment"
13+
WHERE id = NEW.environment_id;
14+
15+
IF environment_org_id != NEW.organization_id THEN
16+
RAISE EXCEPTION 'Environment % must belong to organization % to create an agent', NEW.environment_id, NEW.organization_id;
17+
END IF;
18+
19+
SELECT organization_id INTO api_key_org_id
20+
FROM "api_key"
21+
WHERE id = NEW.api_key_id;
22+
23+
IF api_key_org_id != NEW.organization_id THEN
24+
RAISE EXCEPTION 'API key % must belong to organization % to create an agent', NEW.api_key_id, NEW.organization_id;
25+
END IF;
26+
27+
RETURN NEW;
28+
END;
29+
$$ LANGUAGE plpgsql;
30+
31+
CREATE TABLE "agent" (
32+
"id" UUID NOT NULL,
33+
"organization_id" UUID NOT NULL,
34+
"environment_id" UUID NOT NULL,
35+
"api_key_id" UUID NOT NULL,
36+
"name" VARCHAR(255) NOT NULL,
37+
"description" TEXT NOT NULL DEFAULT '',
38+
"instructions" TEXT NOT NULL DEFAULT '',
39+
"model" VARCHAR(255) NOT NULL,
40+
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
41+
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
42+
FOREIGN KEY ("organization_id") REFERENCES "organization"("id") ON DELETE CASCADE,
43+
FOREIGN KEY ("environment_id") REFERENCES "environment"("id") ON DELETE CASCADE,
44+
FOREIGN KEY ("api_key_id") REFERENCES "api_key"("id") ON DELETE CASCADE,
45+
PRIMARY KEY ("id")
46+
);
47+
48+
CREATE UNIQUE INDEX idx_agent_organization_api_key_name ON "agent" ("organization_id", "api_key_id", "name");
49+
50+
CREATE TRIGGER update_agent_updated_at
51+
BEFORE UPDATE ON "agent"
52+
FOR EACH ROW
53+
EXECUTE FUNCTION update_updated_at_column();
54+
55+
CREATE TRIGGER validate_agent
56+
BEFORE INSERT OR UPDATE ON "agent"
57+
FOR EACH ROW
58+
EXECUTE FUNCTION validate_agent();
59+
60+
-- agent_tool table
61+
62+
CREATE TABLE "agent_tool" (
63+
"id" UUID NOT NULL,
64+
"agent_id" UUID NOT NULL,
65+
"name" VARCHAR(255) NOT NULL,
66+
"description" TEXT NOT NULL DEFAULT '',
67+
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
68+
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
69+
FOREIGN KEY ("agent_id") REFERENCES "agent"("id") ON DELETE CASCADE,
70+
PRIMARY KEY ("id")
71+
);
72+
73+
CREATE UNIQUE INDEX idx_agent_tool_agent_name ON "agent_tool" ("agent_id", "name");
74+
75+
CREATE TRIGGER update_agent_tool_updated_at
76+
BEFORE UPDATE ON "agent_tool"
77+
FOR EACH ROW
78+
EXECUTE FUNCTION update_updated_at_column();
79+
80+
END;

0 commit comments

Comments
 (0)