-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSqliteExtensions.cs
More file actions
79 lines (70 loc) · 2.62 KB
/
SqliteExtensions.cs
File metadata and controls
79 lines (70 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using Dapper;
using MaIN.Domain.Repositories;
using MaIN.Infrastructure.Repositories.Sqlite;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.DependencyInjection;
using System.Data;
namespace MaIN.Infrastructure.Configuration;
public static class SqliteRegistrationExtensions
{
public static IServiceCollection AddSqliteRepositories(
this IServiceCollection services,
string connectionString)
{
// Register SQLite connection
services.AddScoped<IDbConnection>(_ =>
{
var connection = new SqliteConnection(connectionString);
connection.Open();
return connection;
});
// Register repositories
services.AddScoped<IChatRepository, SqliteChatRepository>()
.AddScoped<IAgentFlowRepository, SqliteAgentFlowRepository>()
.AddScoped<IAgentRepository, SqliteAgentRepository>();
InitializeSqliteDatabase(connectionString);
return services;
}
public static void InitializeSqliteDatabase(string connectionString)
{
using var connection = new SqliteConnection(connectionString);
connection.Open();
// Create tables if they don't exist
connection.Execute(@"
CREATE TABLE IF NOT EXISTS Chats (
Id TEXT PRIMARY KEY,
Name TEXT NOT NULL,
Model TEXT NOT NULL,
Messages TEXT NOT NULL, -- Stored as JSON array
Type TEXT NOT NULL, -- Stored as JSON
Properties TEXT, -- Stored as JSON
Visual INTEGER NOT NULL DEFAULT 0,
ConvState TEXT, -- Stored as JSON
InferenceParams TEXT, -- Stored as JSON
MemoryParams TEXT, -- Stored as JSON
Interactive INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS AgentFlows (
Id TEXT PRIMARY KEY,
Name TEXT NOT NULL,
Agents TEXT NOT NULL, -- Stored as JSON array
Description TEXT
);
CREATE TABLE IF NOT EXISTS Agents (
Id TEXT PRIMARY KEY,
Name TEXT NOT NULL,
Model TEXT NOT NULL,
Description TEXT,
Started INTEGER NOT NULL DEFAULT 0,
Context TEXT, -- Stored as JSON
ChatId TEXT NOT NULL,
[Order] INTEGER NOT NULL DEFAULT 0,
BackendType INTEGER NOT NULL DEFAULT 0,
Behaviours TEXT, -- Stored as JSON
CurrentBehaviour TEXT,
Flow INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (ChatId) REFERENCES Chats(Id)
);
");
}
}