Skip to content

Commit

Permalink
Merge pull request #1425 from Oneirocom/api-name-fix
Browse files Browse the repository at this point in the history
changes chatMessage endpoint to chatMessages
  • Loading branch information
benbot authored Dec 13, 2023
2 parents 2681b24 + eba1cbe commit faf02f4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 65 deletions.
2 changes: 1 addition & 1 deletion packages/plugins/discord/server/src/connectors/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export class DiscordConnector {
this.guildId = message.guild
this.message = message

app.service('chatMessage').create({
app.service('chatMessages').create({
agentId: this.agent.id,
connector: 'discord',
content: message.content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const agentHttp = (app: Application) => {
})

const recordMessage = (context: HookContext) => {
app.service('chatMessage').create({
app.service('chatMessages').create({
agentId: context.params.query.agentId || context.data?.agentId,
content: context.params.query.content || context.data?.content,
sender: context.params.query.sender || context.data?.sender,
Expand Down
44 changes: 2 additions & 42 deletions packages/server/core/src/services/messages/messages.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,14 @@

import type { Application, Params } from '@feathersjs/feathers'
import type { KnexAdapterOptions, KnexAdapterParams } from '@feathersjs/knex'
import { KnexAdapter } from '@feathersjs/knex'
import { KnexService } from '@feathersjs/knex'
import type { MessageData, MessagePatch, MessageQuery } from './messages.schema'

export type MessageParams = KnexAdapterParams<MessageQuery>

export class ChatMessageService<
ServiceParams extends Params = MessageParams
> extends KnexAdapter<MessageParams, MessageData, ServiceParams, MessagePatch> {
/**
* Create a new event.
* Currently, this function simply returns the provided event data immediately.
* @param {ChatMessageData} data - The event data object.
* @returns {Promise<any>} - The created event data.
*/
async create(data: MessageData): Promise<any> {
return this._create(data)
}

/**
* Patch an event.
* Currently, this function simply returns the provided event data immediately.
* @param {string} id - The ID of the event to patch.
* @param {MessagePatch} data - The event patch data object.
* @returns {Promise<any>} - The patched event data.
*/
async patch(id: string, data: MessagePatch) {
return this._patch(id, data)
}

async get(id: string, params?: ServiceParams) {
return this._get(id, params)
}

async remove(id: string, params?: ServiceParams) {
return this._remove(id, params)
}

/**
* Find events.
* This function searches for events in the database given an embedding and other query parameters.
* @param {ServiceParams} [params] - The query parameters for the search.
* @returns {Promise<any>} - The search results.
*/
// @ts-ignore
async find(params?: ServiceParams) {
this._find(params)
}
}
> extends KnexService<MessageParams, MessageData, ServiceParams, MessagePatch> { }

/**
* Get options for the message service.
Expand Down
4 changes: 2 additions & 2 deletions packages/server/core/src/services/messages/messages.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const messageDataValidator = getDataValidator(
messageDataSchema,
dataValidator
)
export const messageDataResolver = resolve<MessageData, HookContext>({})
export const messageDataResolver = resolve<Message, HookContext>({})

// Define the messagePatchSchema to update existing entries
export const messagePatchSchema = Type.Partial(messageDataSchema, {
Expand All @@ -66,7 +66,7 @@ export const messagePatchValidator = getDataValidator(
messagePatchSchema,
dataValidator
)
export const messagePatchResolver = resolve<MessagePatch, HookContext>({})
export const messagePatchResolver = resolve<Message, HookContext>({})

// Define the messageQueryProperties and messageQuerySchema
export const messageQueryProperties = Type.Pick(messageSchema, [
Expand Down
36 changes: 17 additions & 19 deletions packages/server/core/src/services/messages/messages.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// DOCUMENTED
import { hooks as schemaHooks } from '@feathersjs/schema'
import pgvector from 'pgvector/pg'
import {
messageExternalResolver,
messagePatchResolver,
messagePatchValidator,
messageDataResolver,
messageDataValidator,
messageQueryResolver,
messageQueryValidator,
messageResolver,
} from './messages.schema'

import { Application, HookContext } from '../../declarations'
import { Application } from '../../declarations'
import { ChatMessageService, getOptions } from './messages.class'
import { checkPermissions } from '../../lib/feathersPermissions'

Expand All @@ -20,22 +21,19 @@ import { checkPermissions } from '../../lib/feathersPermissions'
export * from './messages.class'
export * from './messages.schema'

// Null array with 1536 values
const nullArray = new Array(1536).fill(0)

/**
* A configure function that registers the service and its hooks via `app.configure`
* @param app {Application}
*/
export const chatMessages = (app: Application) => {
// Register our service on the Feathers application
app.use('chatMessage', new ChatMessageService(getOptions(app)), {
app.use('chatMessages', new ChatMessageService(getOptions(app)), {
methods: ['find', 'get', 'create', 'patch', 'remove'],
})

// Initialize hooks

app.service('chatMessage').hooks({
app.service('chatMessages').hooks({
around: {
all: [
schemaHooks.resolveExternal(messageExternalResolver),
Expand All @@ -47,27 +45,27 @@ export const chatMessages = (app: Application) => {
checkPermissions({
roles: ['owner', 'messages'],
}),
],
find: [
schemaHooks.validateQuery(messageQueryValidator),
schemaHooks.resolveQuery(messageQueryResolver),
],
find: [],
get: [
(context: HookContext) => {
const { getEmbedding } = context.params.query
if (getEmbedding) {
context.params.query.$limit = 1
context.params.query.embedding = { $ne: pgvector.toSql(nullArray) }
}
return context
},
schemaHooks.validateQuery(messageQueryValidator),
schemaHooks.resolveQuery(messageQueryResolver),
],
create: [
],
schemaHooks.validateData(messageDataValidator),
schemaHooks.resolveQuery(messageDataResolver),
],
patch: [
schemaHooks.validateData(messagePatchValidator),
schemaHooks.resolveData(messagePatchResolver),
],
remove: [],
remove: [
schemaHooks.validateQuery(messageQueryValidator),
schemaHooks.resolveQuery(messageQueryResolver),
],
},
after: {
create: [],
Expand All @@ -82,6 +80,6 @@ export const chatMessages = (app: Application) => {
// Add this service to the service type index
declare module '../../declarations' {
interface ServiceTypes {
chatMessage: ChatMessageService
chatMessages: ChatMessageService
}
}

0 comments on commit faf02f4

Please sign in to comment.