Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: AiAssistantModel #9494

Merged
merged 24 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
821203c
impl AiAssistantModel
miya Dec 17, 2024
26713ca
clean code
miya Dec 17, 2024
9930e20
rm unnec opt
miya Dec 17, 2024
a8b3143
Add types (to be implemented in the future)
miya Dec 17, 2024
caf1db1
type -> types
miya Dec 17, 2024
b52554d
clean code
miya Dec 17, 2024
6a505a4
Add field (isDeleted)
miya Dec 17, 2024
b143f57
add grantedGroups
miya Dec 17, 2024
9167954
add creator
miya Dec 17, 2024
3fc841c
fix types
miya Dec 17, 2024
df996cb
optional
miya Dec 17, 2024
c5c8529
change field name
miya Dec 18, 2024
9081d95
Merge branch 'feat/growi-ai-next' into feat/159152-ai-assistant-model
miya Dec 19, 2024
c0c9bc0
not optional
miya Dec 19, 2024
5f4ba68
Revert "Add field (isDeleted)"
miya Dec 19, 2024
eafecf9
Refactor AiAssistant model to use vectorStore reference and export Ve…
miya Dec 19, 2024
a6f79e0
Add grantedUsers field to AiAssistant model for user-specific sharing
miya Dec 20, 2024
d63af50
Merge branch 'feat/growi-ai-next' into feat/159152-ai-assistant-model
miya Dec 23, 2024
815deee
Merge branch 'feat/growi-ai-next' into feat/159152-ai-assistant-model
miya Dec 24, 2024
10c7773
Rename learningScope to ownerAccessScope in AiAssistant model for cla…
miya Dec 24, 2024
f661d11
Rename AiAssistantSharingScope to AiAssistantShareScope for consistency
miya Dec 24, 2024
752768a
Update enum reference for shareScope to use AiAssistantShareScope
miya Dec 24, 2024
0775170
Add pagePaths property to AiAssistant model for enhanced functionality
miya Dec 24, 2024
c5e73e7
pagePath -> pagePathPatterns
miya Dec 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions apps/app/src/features/openai/server/models/ai-assistant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import {
type IGrantedGroup, GroupType, type IUser, type Ref,
} from '@growi/core';
import { type Model, type Document, Schema } from 'mongoose';

import { getOrCreateModel } from '~/server/util/mongoose-utils';

import type { VectorStore } from './vector-store';

/*
* Objects
*/
const AiAssistantType = {
KNOWLEDGE: 'knowledge',
// EDITOR: 'editor',
// LEARNING: 'learning',
} as const;

const AiAssistantShareScope = {
PUBLIC: 'public',
ONLY_ME: 'onlyMe',
USER_GROUP: 'userGroup',
} as const;

const AiAssistantOwnerAccessScope = {
PUBLIC: 'public',
ONLY_ME: 'onlyMe',
USER_GROUP: 'userGroup',
} as const;


/*
* Interfaces
*/
type AiAssistantType = typeof AiAssistantType[keyof typeof AiAssistantType];
type AiAssistantShareScope = typeof AiAssistantShareScope[keyof typeof AiAssistantShareScope];
type AiAssistantOwnerAccessScope = typeof AiAssistantOwnerAccessScope[keyof typeof AiAssistantOwnerAccessScope];

interface AiAssistant {
name: string;
description: string
additionalInstruction: string
pagePathPatterns: string[],
vectorStore: Ref<VectorStore>
types: AiAssistantType[]
owner: Ref<IUser>
grantedUsers?: IUser[]
grantedGroups?: IGrantedGroup[]
shareScope: AiAssistantShareScope
ownerAccessScope: AiAssistantOwnerAccessScope
}

interface AiAssistantDocument extends AiAssistant, Document {}

type AiAssistantModel = Model<AiAssistantDocument>


/*
* Schema Definition
*/
const schema = new Schema<AiAssistantDocument>(
{
name: {
type: String,
required: true,
},
description: {
type: String,
required: true,
default: '',
},
additionalInstruction: {
type: String,
required: true,
default: '',
},
pagePathPatterns: [{
type: String,
required: true,
}],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VectorStore に保存する page の path

vectorStore: {
type: Schema.Types.ObjectId,
ref: 'VectorStore',
required: true,
},
types: [{
type: String,
enum: Object.values(AiAssistantType),
required: true,
}],
owner: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true,
},
grantedUsers: [
{
type: Schema.Types.ObjectId,
ref: 'User',
required: true,
},
],
grantedGroups: {
type: [{
type: {
type: String,
enum: Object.values(GroupType),
required: true,
default: 'UserGroup',
},
item: {
type: Schema.Types.ObjectId,
refPath: 'grantedGroups.type',
required: true,
index: true,
},
}],
Copy link
Member Author

@miya miya Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Page model を踏襲

type: [{
type: {
type: String,
enum: Object.values(GroupType),
required: true,
default: 'UserGroup',
},
item: {
type: Schema.Types.ObjectId,
refPath: 'grantedGroups.type',
required: true,
index: true,
},
}],
validate: [function(arr) {
if (arr == null) return true;
const uniqueItemValues = new Set(arr.map(e => e.item));
return arr.length === uniqueItemValues.size;
}, 'grantedGroups contains non unique item'],
default: [],
required: true,
},

validate: [function(arr: IGrantedGroup[]): boolean {
if (arr == null) return true;
const uniqueItemValues = new Set(arr.map(e => e.item));
return arr.length === uniqueItemValues.size;
}, 'grantedGroups contains non unique item'],
default: [],
},
shareScope: {
type: String,
enum: Object.values(AiAssistantShareScope),
required: true,
},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

共有範囲

ownerAccessScope: {
type: String,
enum: Object.values(AiAssistantOwnerAccessScope),
required: true,
},
},
{
timestamps: true,
},
);

export default getOrCreateModel<AiAssistantDocument, AiAssistantModel>('AiAssistant', schema);
2 changes: 1 addition & 1 deletion apps/app/src/features/openai/server/models/vector-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const VectorStoreScopeType = {
export type VectorStoreScopeType = typeof VectorStoreScopeType[keyof typeof VectorStoreScopeType];

const VectorStoreScopeTypes = Object.values(VectorStoreScopeType);
interface VectorStore {
export interface VectorStore {
vectorStoreId: string
scopeType: VectorStoreScopeType
isDeleted: boolean
Expand Down
Loading