@axiom-lattice/gateway 2.1.28 → 2.1.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,6 +26,8 @@ export const createRun = async (
26
26
  } = request.body as CreateRunRequest;
27
27
 
28
28
  const tenant_id = request.headers["x-tenant-id"] as string;
29
+ const workspace_id = request.headers["x-workspace-id"] as string;
30
+ const project_id = request.headers["x-project-id"] as string;
29
31
  const x_request_id = (request.headers["x-request-id"] as string) || v4();
30
32
 
31
33
  // 验证请求数据
@@ -46,6 +48,8 @@ export const createRun = async (
46
48
  thread_id: thread_id,
47
49
  command,
48
50
  tenant_id: tenant_id,
51
+ workspace_id: workspace_id,
52
+ project_id: project_id,
49
53
  run_id: x_request_id,
50
54
  });
51
55
 
@@ -77,6 +81,8 @@ export const createRun = async (
77
81
  command: command,
78
82
  thread_id: thread_id,
79
83
  tenant_id: tenant_id,
84
+ workspace_id: workspace_id,
85
+ project_id: project_id,
80
86
  run_id: x_request_id,
81
87
  });
82
88
  reply.status(200).send(result);
@@ -0,0 +1,121 @@
1
+ /**
2
+ * TenantsController
3
+ *
4
+ * Controller for tenant management
5
+ * Handles CRUD operations for tenants (top-level entities)
6
+ */
7
+
8
+ import { FastifyInstance, FastifyRequest, FastifyReply } from "fastify";
9
+ import { getStoreLattice } from "@axiom-lattice/core";
10
+ import type {
11
+ CreateTenantRequest,
12
+ UpdateTenantRequest,
13
+ } from "@axiom-lattice/protocols";
14
+ import { v4 as uuidv4 } from "uuid";
15
+
16
+ interface TenantParams {
17
+ tenantId: string;
18
+ }
19
+
20
+ export class TenantsController {
21
+ private tenantStore;
22
+
23
+ constructor() {
24
+ this.tenantStore = getStoreLattice("default", "tenant").store;
25
+ }
26
+
27
+ // ==================== Tenant CRUD ====================
28
+
29
+ async listTenants(request: FastifyRequest, reply: FastifyReply) {
30
+ const tenants = await this.tenantStore.getAllTenants();
31
+ return { success: true, data: tenants };
32
+ }
33
+
34
+ async createTenant(
35
+ request: FastifyRequest<{ Body: CreateTenantRequest }>,
36
+ reply: FastifyReply
37
+ ) {
38
+ const data = request.body;
39
+ const id = uuidv4();
40
+
41
+ const tenant = await this.tenantStore.createTenant(id, data);
42
+ return reply.status(201).send({ success: true, data: tenant });
43
+ }
44
+
45
+ async getTenant(
46
+ request: FastifyRequest<{ Params: TenantParams }>,
47
+ reply: FastifyReply
48
+ ) {
49
+ const { tenantId } = request.params;
50
+
51
+ const tenant = await this.tenantStore.getTenantById(tenantId);
52
+
53
+ if (!tenant) {
54
+ return reply.status(404).send({
55
+ success: false,
56
+ error: "Tenant not found",
57
+ });
58
+ }
59
+
60
+ return { success: true, data: tenant };
61
+ }
62
+
63
+ async updateTenant(
64
+ request: FastifyRequest<{
65
+ Params: TenantParams;
66
+ Body: UpdateTenantRequest;
67
+ }>,
68
+ reply: FastifyReply
69
+ ) {
70
+ const { tenantId } = request.params;
71
+ const updates = request.body;
72
+
73
+ const tenant = await this.tenantStore.updateTenant(tenantId, updates);
74
+
75
+ if (!tenant) {
76
+ return reply.status(404).send({
77
+ success: false,
78
+ error: "Tenant not found",
79
+ });
80
+ }
81
+
82
+ return { success: true, data: tenant };
83
+ }
84
+
85
+ async deleteTenant(
86
+ request: FastifyRequest<{ Params: TenantParams }>,
87
+ reply: FastifyReply
88
+ ) {
89
+ const { tenantId } = request.params;
90
+
91
+ // Prevent deletion of the default tenant
92
+ if (tenantId === "default") {
93
+ return reply.status(403).send({
94
+ success: false,
95
+ error: "Cannot delete the default tenant",
96
+ });
97
+ }
98
+
99
+ const deleted = await this.tenantStore.deleteTenant(tenantId);
100
+
101
+ if (!deleted) {
102
+ return reply.status(404).send({
103
+ success: false,
104
+ error: "Tenant not found",
105
+ });
106
+ }
107
+
108
+ return { success: true };
109
+ }
110
+ }
111
+
112
+ export function registerTenantRoutes(app: FastifyInstance) {
113
+ const controller = new TenantsController();
114
+
115
+ // Tenant routes
116
+ app.get("/api/tenants", controller.listTenants.bind(controller));
117
+ app.post("/api/tenants", controller.createTenant.bind(controller));
118
+ app.get("/api/tenants/:tenantId", controller.getTenant.bind(controller));
119
+ app.patch("/api/tenants/:tenantId", controller.updateTenant.bind(controller));
120
+ app.delete("/api/tenants/:tenantId", controller.deleteTenant.bind(controller));
121
+ }
@@ -0,0 +1,135 @@
1
+ import { FastifyInstance, FastifyRequest, FastifyReply } from "fastify";
2
+ import { getStoreLattice } from "@axiom-lattice/core";
3
+ import type {
4
+ CreateUserRequest,
5
+ UpdateUserRequest,
6
+ } from "@axiom-lattice/protocols";
7
+ import { v4 as uuidv4 } from "uuid";
8
+
9
+ interface UserParams {
10
+ userId: string;
11
+ }
12
+
13
+ interface ListUsersQuery {
14
+ email?: string;
15
+ }
16
+
17
+ export class UsersController {
18
+ private userStore;
19
+
20
+ constructor() {
21
+ this.userStore = getStoreLattice("default", "user").store;
22
+ }
23
+
24
+ async listUsers(
25
+ request: FastifyRequest<{ Querystring: ListUsersQuery }>,
26
+ reply: FastifyReply
27
+ ) {
28
+ const { email } = request.query;
29
+
30
+ if (email) {
31
+ const user = await this.userStore.getUserByEmail(email);
32
+ return { success: true, data: user ? [user] : [] };
33
+ }
34
+
35
+ const users = await this.userStore.getAllUsers();
36
+ return { success: true, data: users };
37
+ }
38
+
39
+ async createUser(
40
+ request: FastifyRequest<{ Body: CreateUserRequest }>,
41
+ reply: FastifyReply
42
+ ) {
43
+ const data = request.body;
44
+ const id = uuidv4();
45
+
46
+ const existingUser = await this.userStore.getUserByEmail(data.email);
47
+ if (existingUser) {
48
+ return reply.status(409).send({
49
+ success: false,
50
+ error: `User with email ${data.email} already exists`,
51
+ });
52
+ }
53
+
54
+ const user = await this.userStore.createUser(id, data);
55
+ return reply.status(201).send({ success: true, data: user });
56
+ }
57
+
58
+ async getUser(
59
+ request: FastifyRequest<{ Params: UserParams }>,
60
+ reply: FastifyReply
61
+ ) {
62
+ const { userId } = request.params;
63
+
64
+ const user = await this.userStore.getUserById(userId);
65
+
66
+ if (!user) {
67
+ return reply.status(404).send({
68
+ success: false,
69
+ error: "User not found",
70
+ });
71
+ }
72
+
73
+ return { success: true, data: user };
74
+ }
75
+
76
+ async updateUser(
77
+ request: FastifyRequest<{
78
+ Params: UserParams;
79
+ Body: UpdateUserRequest;
80
+ }>,
81
+ reply: FastifyReply
82
+ ) {
83
+ const { userId } = request.params;
84
+ const updates = request.body;
85
+
86
+ if (updates.email) {
87
+ const existingUser = await this.userStore.getUserByEmail(updates.email);
88
+ if (existingUser && existingUser.id !== userId) {
89
+ return reply.status(409).send({
90
+ success: false,
91
+ error: `User with email ${updates.email} already exists`,
92
+ });
93
+ }
94
+ }
95
+
96
+ const user = await this.userStore.updateUser(userId, updates);
97
+
98
+ if (!user) {
99
+ return reply.status(404).send({
100
+ success: false,
101
+ error: "User not found",
102
+ });
103
+ }
104
+
105
+ return { success: true, data: user };
106
+ }
107
+
108
+ async deleteUser(
109
+ request: FastifyRequest<{ Params: UserParams }>,
110
+ reply: FastifyReply
111
+ ) {
112
+ const { userId } = request.params;
113
+
114
+ const deleted = await this.userStore.deleteUser(userId);
115
+
116
+ if (!deleted) {
117
+ return reply.status(404).send({
118
+ success: false,
119
+ error: "User not found",
120
+ });
121
+ }
122
+
123
+ return { success: true };
124
+ }
125
+ }
126
+
127
+ export function registerUserRoutes(app: FastifyInstance) {
128
+ const controller = new UsersController();
129
+
130
+ app.get("/api/users", controller.listUsers.bind(controller));
131
+ app.post("/api/users", controller.createUser.bind(controller));
132
+ app.get("/api/users/:userId", controller.getUser.bind(controller));
133
+ app.patch("/api/users/:userId", controller.updateUser.bind(controller));
134
+ app.delete("/api/users/:userId", controller.deleteUser.bind(controller));
135
+ }