Skip to content

Commit

Permalink
Auth Works
Browse files Browse the repository at this point in the history
  • Loading branch information
awhiteside1 committed Sep 23, 2024
1 parent f82fbaa commit 37f3645
Show file tree
Hide file tree
Showing 21 changed files with 407 additions and 104 deletions.
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"files": {
"ignore": ["playground/**"],
"ignore": ["playground/**", "**/payload-types.ts"],
"ignoreUnknown": true
},
"vcs": {
Expand Down
1 change: 1 addition & 0 deletions packages/chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"build:ui": "vite build",
"build:plugin": "unbuild --sourcemaps",
"build": "pnpm build:plugin && pnpm build:ui",
"generate": "payload generate:types",
"tailwind": "npx tailwindcss -i ./src/ui/tailwind.css -o ./dist/ui/tailwind.css"
},
"dependencies": {
Expand Down
60 changes: 46 additions & 14 deletions packages/chat/src/collections/createMessageCollection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import type {ChatPluginConfig} from '../plugin/types'
import type {ArrayField, CollectionConfig} from 'payload'
import type {ArrayField, CollectionConfig, FieldHook} from 'payload'
import {get} from 'radash'

const setSentDate: FieldHook<
{ id: string },
Array<{ sent: Date; id: string }>
> = ({ data, value, operation }) => {
return (value ?? []).map((m) => ({ ...m, sent: m.sent ?? new Date() }))
}

export const createMessageCollection = (
config: ChatPluginConfig['generatedCollections']['chats'],
): CollectionConfig => {
Expand All @@ -10,18 +17,33 @@ export const createMessageCollection = (
const messagesField: ArrayField = {
name: 'messages',
type: 'array',
required: true,
defaultValue: [],
hooks: {
beforeChange: [setSentDate],
},
fields: [
{ name: 'sent', type: 'date', required: true },
{ name: 'text', type: 'text', required: true },
{
name: 'role',
type: 'select',
options: ['assistant', 'user'],
required: true,
type: 'row',
fields: [
{
name: 'sent',
type: 'date',
admin: { date: { pickerAppearance: 'dayAndTime' }, readOnly: true },
},
{
name: 'role',
type: 'select',
options: ['assistant', 'user'],
required: true,
},
],
},
{ name: 'text', type: 'text', required: true },
{
name: 'context',
type: 'group',
label: 'context',
type: 'collapsible',
admin: { initCollapsed: true },
fields: [
{ name: 'collection', type: 'text' },
{ name: 'view', type: 'select', options: ['list', 'record'] },
Expand All @@ -31,19 +53,29 @@ export const createMessageCollection = (
}

return {
access: {
read: ({ req, data }) => {
if (!req.user) return false
if (!data) return true
return {
'user.email': { equals: req.user.email },
}
},
},
fields: [
messagesField,
{
name: 'user',
type: 'relationship',
relationTo: ['users'],
relationTo: 'users',
hasMany: false,
required: true,
},
{
name:'description',
type:'text',
defaultValue:'A Recent Chat'
}
name: 'description',
type: 'text',
defaultValue: 'A Recent Chat',
},
],
slug,
}
Expand Down
146 changes: 146 additions & 0 deletions packages/chat/src/payload-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/* tslint:disable */
/* eslint-disable */
/**
* This file was automatically generated by Payload.
* DO NOT MODIFY IT BY HAND. Instead, modify your source Payload config,
* and re-run `payload generate:types` to regenerate this file.
*/

export interface Config {
auth: {
users: UserAuthOperations;
};
collections: {
chats: Chat;
users: User;
'payload-locked-documents': PayloadLockedDocument;
'payload-preferences': PayloadPreference;
'payload-migrations': PayloadMigration;
};
db: {
defaultIDType: string;
};
globals: {};
locale: null;
user: User & {
collection: 'users';
};
}
export interface UserAuthOperations {
forgotPassword: {
email: string;
password: string;
};
login: {
email: string;
password: string;
};
registerFirstUser: {
email: string;
password: string;
};
unlock: {
email: string;
password: string;
};
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "chats".
*/
export interface Chat {
messages: {
sent?: string | null;
role: 'assistant' | 'user';
text: string;
collection?: string | null;
view?: ('list' | 'record') | null;
id?: string | null;
}[];
user: number | User;
description?: string | null;
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "users".
*/
export interface User {
updatedAt: string;
createdAt: string;
email: string;
resetPasswordToken?: string | null;
resetPasswordExpiration?: string | null;
salt?: string | null;
hash?: string | null;
loginAttempts?: number | null;
lockUntil?: string | null;
password?: string | null;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-locked-documents".
*/
export interface PayloadLockedDocument {
document?:
| ({
relationTo: 'chats';
value: number | Chat;
} | null)
| ({
relationTo: 'users';
value: number | User;
} | null);
globalSlug?: string | null;
user: {
relationTo: 'users';
value: number | User;
};
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-preferences".
*/
export interface PayloadPreference {
user: {
relationTo: 'users';
value: number | User;
};
key?: string | null;
value?:
| {
[k: string]: unknown;
}
| unknown[]
| string
| number
| boolean
| null;
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-migrations".
*/
export interface PayloadMigration {
name?: string | null;
batch?: number | null;
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "auth".
*/
export interface Auth {
[k: string]: unknown;
}


declare module 'payload' {
export interface GeneratedTypes extends Config {}
}
17 changes: 17 additions & 0 deletions packages/chat/src/payload.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import path from "node:path";
import {fileURLToPath} from "node:url";
import type {Config} from 'payload'
import {buildConfig} from 'payload'
import {chatPlugin} from "./plugin/ChatPlugin";
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)

export default buildConfig({
collections: [],
db: {} as Config['db'],
secret: 'secret',
typescript: {
outputFile: path.resolve(dirname, 'payload-types.ts'),
},
plugins:[chatPlugin({})]
})
9 changes: 9 additions & 0 deletions packages/chat/src/payload/getUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {cookies, headers} from "next/headers";
import payload, {extractJWT} from "payload";

export const getUser =()=>{

const jwt = extractJWT({headers: headers(), payload:payload, isGraphQL:false})


}
12 changes: 5 additions & 7 deletions packages/chat/src/plugin/ChatPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,19 @@ export const chatPlugin =
components: {
views: {
chat: {
exact:true,
sensitive:false,
path:'/chat',
exact: false,
sensitive: false,
path: '/chat/:chat_id',
Component: '@payload-llm-plugins/chat/ChatView',
},
},
},
},
collections: [
createMessageCollection(
pluginConfig.generatedCollections.chats,
),
createMessageCollection(pluginConfig.generatedCollections.chats),
],
} satisfies Partial<Config>)
console.log(updatedConfig.admin.components.views.chat)

const clientConfig = pluginConfig.modelClientConfig

// @ts-ignore
Expand Down
27 changes: 27 additions & 0 deletions packages/chat/src/ui/chat/Input.client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use client'
import * as React from 'react'
import {insertMessageAction} from './insertMessage.action'

interface ChatId {
chatId?: string | number
}

export const SendMessageUI = ({ chatId }: ChatId) => {
return (
<form id="chat" className="flex" action={insertMessageAction}>
<label htmlFor="message" className="sr-only">
Input Your Query
</label>
<input
name="message"
id="message"
className="flex-grow p-2 border border-gray-300 rounded-l"
placeholder="Type your message..."
/>
<input hidden id="chatId" name="chatId" readOnly value={chatId} />
<button type="submit" className="p-2 bg-blue-500 text-white rounded-r">
Send
</button>
</form>
)
}
15 changes: 11 additions & 4 deletions packages/chat/src/ui/chat/Message.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import * as React from 'react'
import type {Message} from './types'
import type {Chat} from '../../payload-types'

export const MessageElement = ({ message }: { message: Message }) => {
export const MessageElement = ({
message,
}: { message: Chat['messages'][number] }) => {
return (
<div className={`chat-message ${message.role === 'user' ? 'text-right' : 'text-left'}`}>
<div className={`chat-bubble inline-block p-2 rounded-lg ${message.role === 'user' ? 'bg-blue-500 text-white' : 'bg-gray-200 text-gray-800'}`} data-role={message.role}>
<div
className={`chat-message ${message.role === 'user' ? 'text-right' : 'text-left'}`}
>
<div
className={`chat-bubble inline-block p-2 rounded-lg ${message.role === 'user' ? 'bg-blue-500 text-white' : 'bg-gray-200 text-gray-800'}`}
data-role={message.role}
>
{message.text}
</div>
</div>
Expand Down
Loading

0 comments on commit 37f3645

Please sign in to comment.