Skip to content

Commit 8563c39

Browse files
committed
ai chat entities, store and api extensions
1 parent 4a7b76d commit 8563c39

File tree

11 files changed

+1354
-18
lines changed

11 files changed

+1354
-18
lines changed

common/schemas/ai-api.json

Lines changed: 889 additions & 15 deletions
Large diffs are not rendered by default.

common/schemas/ai-entities.json

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"definitions": {
4+
"AiChatMessage": {
5+
"type": "object",
6+
"properties": {
7+
"id": {
8+
"type": "string",
9+
"description": "The unique identifier for the AI chat message"
10+
},
11+
"content": {
12+
"type": "string",
13+
"description": "The content of the AI chat message"
14+
},
15+
"createdAt": {
16+
"type": "string",
17+
"format": "date-time",
18+
"description": "The timestamp when the AI chat message was created"
19+
},
20+
"aiChatId": {
21+
"type": "string",
22+
"description": "The id of the AI chat this message belongs to This is used to associate the message with a specific AI chat."
23+
},
24+
"role": {
25+
"type": "string",
26+
"enum": ["user", "assistant"],
27+
"description": "The role of the message sender, either 'user' or 'assistant' This indicates whether the message was sent by the user or generated by the AI assistant."
28+
}
29+
},
30+
"required": ["id", "content", "createdAt", "aiChatId", "role"],
31+
"additionalProperties": false
32+
},
33+
"AiChat": {
34+
"type": "object",
35+
"properties": {
36+
"id": {
37+
"type": "string",
38+
"description": "The unique identifier for the AI chat."
39+
},
40+
"name": {
41+
"type": "string",
42+
"description": "The name of the AI chat. It can be used as a context information for the AI model and is also displayed in the UI."
43+
},
44+
"description": {
45+
"type": "string",
46+
"description": "The description of the AI chat in markdown format. This can be used to provide additional context or instructions for the AI model, and is also displayed in the UI."
47+
},
48+
"createdAt": {
49+
"type": "string",
50+
"format": "date-time",
51+
"description": "The date the AI chat was created."
52+
},
53+
"owner": {
54+
"type": "string",
55+
"description": "The owner username of the AI chat."
56+
},
57+
"model": {
58+
"type": "string",
59+
"description": "The model used by the AI chat, such as 'gpt-3.5-turbo' or 'gpt-4' This is the model that will be used to generate responses in the chat."
60+
},
61+
"status": {
62+
"type": "string",
63+
"enum": ["active", "archived"],
64+
"description": "The status of the AI chat, indicating whether it is active or archived Archived chats can no longer be used for new messages, but can still be viewed and referenced."
65+
},
66+
"visibility": {
67+
"type": "string",
68+
"enum": ["private", "public"],
69+
"description": "Defines if the chat can be viewed by its owner only or if it's public to anyone who knows the ID."
70+
}
71+
},
72+
"required": ["id", "name", "createdAt", "owner", "model", "status", "visibility"],
73+
"additionalProperties": false
74+
}
75+
}
76+
}

common/src/apis/ai.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type { RestApi } from '@furystack/rest'
1+
import type { GetCollectionEndpoint, GetEntityEndpoint, PatchEndpoint, PostEndpoint, RestApi } from '@furystack/rest'
22
import type { ChatRequest, ChatResponse, ModelResponse } from 'ollama'
3+
import type { AiChat } from '../models/index.js'
34

45
export type GetModelsAction = {
56
result: ModelResponse[]
@@ -13,8 +14,18 @@ export type ChatAction = {
1314
export interface AiApi extends RestApi {
1415
GET: {
1516
'/models': GetModelsAction
17+
'/ai-chats': GetCollectionEndpoint<AiChat>
18+
'/ai-chats/:id': GetEntityEndpoint<AiChat, 'id'>
19+
'/ai-chat-messages': GetCollectionEndpoint<AiChat>
20+
'/ai-chat-messages/:id': GetEntityEndpoint<AiChat, 'id'>
1621
}
1722
POST: {
23+
// @deprecated
1824
'/chat': ChatAction
25+
'/ai-chats': PostEndpoint<AiChat, 'id'>
26+
'/ai-chat-messages': PostEndpoint<AiChat, 'id'>
27+
}
28+
PATCH: {
29+
'/ai-chats/:id': PatchEndpoint<AiChat, 'id'>
1930
}
2031
}

common/src/bin/create-schemas.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ export interface SchemaGenerationSetting {
1212
* Entity schemas, e.g. User, Session, etc...
1313
*/
1414
export const entityValues: SchemaGenerationSetting[] = [
15+
{
16+
inputFile: './src/models/ai/*.ts',
17+
outputFile: './schemas/ai-entities.json',
18+
type: '*',
19+
},
1520
{
1621
inputFile: './src/models/chat/*.ts',
1722
outputFile: './schemas/chat-entities.json',
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export class AiChatMessage {
2+
/**
3+
* The unique identifier for the AI chat message
4+
*/
5+
declare id: string
6+
7+
/**
8+
* The content of the AI chat message
9+
*/
10+
declare content: string
11+
12+
/**
13+
* The timestamp when the AI chat message was created
14+
*/
15+
declare createdAt: Date
16+
17+
/**
18+
* The id of the AI chat this message belongs to
19+
* This is used to associate the message with a specific AI chat.
20+
*/
21+
declare aiChatId: string
22+
23+
/**
24+
* The role of the message sender, either 'user' or 'assistant'
25+
* This indicates whether the message was sent by the user or generated by the AI assistant.
26+
*/
27+
declare role: 'user' | 'assistant'
28+
29+
/**
30+
* Defines if the AI Chat is public or private
31+
* Denormalized field due performance reasons.
32+
*/
33+
declare visibility: 'public' | 'private'
34+
35+
/**
36+
* The owner of the AI chat message
37+
* This is used to identify the user who created the message.
38+
* Denormalized field due performance reasons.
39+
*/
40+
declare owner: string
41+
}

common/src/models/ai/ai-chat.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export class AiChat {
2+
/**
3+
* The unique identifier for the AI chat.
4+
*/
5+
declare id: string
6+
/**
7+
* The name of the AI chat. It can be used as a context information for the AI model
8+
* and is also displayed in the UI.
9+
*/
10+
declare name: string
11+
12+
/**
13+
* The description of the AI chat in markdown format.
14+
* This can be used to provide additional context or instructions
15+
* for the AI model, and is also displayed in the UI.
16+
*/
17+
declare description?: string
18+
19+
/**
20+
* The date the AI chat was created.
21+
*/
22+
declare createdAt: Date
23+
24+
/**
25+
* The owner username of the AI chat.
26+
*/
27+
declare owner: string
28+
29+
/**
30+
* The model used by the AI chat, such as 'gpt-3.5-turbo' or 'gpt-4'
31+
* This is the model that will be used to generate responses in the chat.
32+
*/
33+
declare model: string
34+
35+
/**
36+
* The status of the AI chat, indicating whether it is active or archived
37+
* Archived chats can no longer be used for new messages, but can still be viewed
38+
* and referenced.
39+
*/
40+
declare status: 'active' | 'archived'
41+
42+
/**
43+
* Defines if the chat can be viewed by its owner only or if it's public to anyone who knows the ID.
44+
*/
45+
declare visibility: 'private' | 'public'
46+
}

common/src/models/ai/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './ai-chat-message.js'
2+
export * from './ai-chat.js'

common/src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './ai/index.js'
12
export * from './chat/index.js'
23
export * from './config/index.js'
34
export * from './dashboard/index.js'

service/src/ai/setup-ai-rest-api.ts

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import type { Injector } from '@furystack/inject'
2-
import { useRestService, Validate } from '@furystack/rest-service'
3-
import type { AiApi } from 'common'
2+
import {
3+
createGetCollectionEndpoint,
4+
createGetEntityEndpoint,
5+
createPatchEndpoint,
6+
createPostEndpoint,
7+
useRestService,
8+
Validate,
9+
} from '@furystack/rest-service'
10+
import { AiChat, type AiApi } from 'common'
411
import schema from 'common/schemas/ai-api.json' with { type: 'json' }
512
import { getCorsOptions } from '../get-cors-options.js'
613
import { getPort } from '../get-port.js'
@@ -19,12 +26,77 @@ export const setupAiRestApi = async (injector: Injector) => {
1926
schema,
2027
schemaName: 'GetModelsAction',
2128
})(GetModelsAction),
29+
'/ai-chats': Validate({
30+
schema,
31+
schemaName: 'GetCollectionEndpoint<AiChat>',
32+
})(
33+
createGetCollectionEndpoint({
34+
model: AiChat,
35+
primaryKey: 'id',
36+
}),
37+
),
38+
'/ai-chats/:id': Validate({
39+
schema,
40+
schemaName: 'GetEntityEndpoint<AiChat,"id">',
41+
})(
42+
createGetEntityEndpoint({
43+
model: AiChat,
44+
primaryKey: 'id',
45+
}),
46+
),
47+
'/ai-chat-messages': Validate({
48+
schema,
49+
schemaName: 'GetCollectionEndpoint<AiChat>',
50+
})(
51+
createGetCollectionEndpoint({
52+
model: AiChat,
53+
primaryKey: 'id',
54+
}),
55+
),
56+
'/ai-chat-messages/:id': Validate({
57+
schema,
58+
schemaName: 'GetEntityEndpoint<AiChat,"id">',
59+
})(
60+
createGetEntityEndpoint({
61+
model: AiChat,
62+
primaryKey: 'id',
63+
}),
64+
),
2265
},
2366
POST: {
2467
'/chat': Validate({
2568
schema,
2669
schemaName: 'ChatAction',
2770
})(ChatAction),
71+
'/ai-chats': Validate({
72+
schema,
73+
schemaName: 'PostEndpoint<AiChat,"id">',
74+
})(
75+
createPostEndpoint({
76+
model: AiChat,
77+
primaryKey: 'id',
78+
}),
79+
),
80+
'/ai-chat-messages': Validate({
81+
schema,
82+
schemaName: 'PostEndpoint<AiChat,"id">',
83+
})(
84+
createPostEndpoint({
85+
model: AiChat,
86+
primaryKey: 'id',
87+
}),
88+
),
89+
},
90+
PATCH: {
91+
'/ai-chats/:id': Validate({
92+
schema,
93+
schemaName: 'PatchEndpoint<AiChat,"id">',
94+
})(
95+
createPatchEndpoint({
96+
model: AiChat,
97+
primaryKey: 'id',
98+
}),
99+
),
28100
},
29101
},
30102
})

0 commit comments

Comments
 (0)