Skip to content

Commit

Permalink
support今北産業
Browse files Browse the repository at this point in the history
  • Loading branch information
pyama committed Aug 27, 2024
1 parent bad4a3e commit b52e96f
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 210 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"scripts": {
"lint": "eslint src --ext .ts",
"test": "jest",
"test": "OPENAI_API_KEY=dummy_api_key jest",
"start": "npm run build:live",
"build": "tsc -p .",
"build:live": "nodemon --watch 'src/**/*.ts' --exec \"ts-node\" src/index.ts"
Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { app } from './app'
import { shortcutQuery } from './shortcut'
import { appMention } from './mention';

(async () => {
// アプリを起動します
await app.start(process.env.PORT || 3000)
})()
app.shortcut({ callback_id: 'query', type: 'message_action' }, shortcutQuery(app))
app.event('app_mention', appMention)
78 changes: 67 additions & 11 deletions src/mention.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { appMention } from './mention'

// モック関数
import { ask, generateSummary } from './utils'

const mockSay = jest.fn()
const mockClient = {
conversations: {
Expand All @@ -9,24 +10,26 @@ const mockClient = {
info: jest.fn()
}
}

const mockEvent = {
channel: 'test_channel',
thread_ts: 'test_thread_ts',
ts: 'test_ts'
ts: 'test_ts',
text: 'test_message'
}

const mockAsk = jest.fn()

jest.mock('./utils', () => ({
ask: () => mockAsk()
}))
jest.mock('./utils', () => {
return {
ask: jest.fn(),
generateSummary: jest.fn()
}
})

describe('appMention', () => {
beforeEach(() => {
mockClient.conversations.replies.mockClear()
mockSay.mockClear()
mockAsk.mockClear()
;(ask as jest.Mock).mockClear()
;(generateSummary as jest.Mock).mockClear()
})

it('should handle app mention event', async () => {
Expand All @@ -38,7 +41,7 @@ describe('appMention', () => {
}
]
})
mockAsk.mockResolvedValueOnce('GPTの回答')
;(ask as jest.Mock).mockResolvedValueOnce('GPTの回答')

const args = {
event: mockEvent as any,
Expand All @@ -60,5 +63,58 @@ describe('appMention', () => {
})
})

// 他のテストケースを追加してください
it('should handle app mention event with 今北産業', async () => {
const eventWithSummaryRequest = {
...mockEvent,
text: '今北産業'
}

mockClient.conversations.replies.mockResolvedValueOnce({
messages: [
{
text: 'test message 1',
user: 'test_user_1'
},
{
text: 'test message 2',
user: 'test_user_2'
}
]
})
;(generateSummary as jest.Mock).mockResolvedValueOnce('サマリの内容')

const args = {
event: eventWithSummaryRequest as any,
client: mockClient as any,
say: mockSay
}

await appMention(args)

expect(mockClient.conversations.replies).toHaveBeenCalledWith({
channel: 'test_channel',
ts: 'test_thread_ts'
})

expect(generateSummary).toHaveBeenCalledWith(
[
{
role: 'user',
content: [{ text: 'test message 1', type: 'text' }]
},
{
role: 'user',
content: [{ text: 'test message 2', type: 'text' }]
}
],
'gpt-4o',
null
)

expect(mockSay).toHaveBeenCalledTimes(1)
expect(mockSay).toHaveBeenNthCalledWith(1, {
text: 'サマリの内容',
thread_ts: 'test_ts'
})
})
})
42 changes: 24 additions & 18 deletions src/mention.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ask, downloadFileAsBase64 } from './utils'
import { ask, downloadFileAsBase64, generateSummary } from './utils'

export const appMention: any = async ({ event, client, say }) => {
const channelId = event.channel
Expand All @@ -10,19 +10,19 @@ export const appMention: any = async ({ event, client, say }) => {
})

if (!replies.messages) {
await say(
'スレッドが見つかりませんでした'
)

await say('スレッドが見つかりませんでした')
return
}

const nonNullable = <T>(value: T): value is NonNullable<T> => value != null
let model = process.env.GPT_MODEL || 'gpt-4-turbo'
let model = process.env.GPT_MODEL || 'gpt-4o'
let max_tokens = null

const isSummaryRequest = event.text.includes('今北産業')

const threadMessages = await Promise.all(
replies.messages.map(async (message) => {
if (message.user !== botUserId && !message.text.includes(`<@${botUserId}>`)) {
if (!isSummaryRequest && message.user !== botUserId && !message.text.includes(`<@${botUserId}>`)) {
return null
}

Expand All @@ -38,17 +38,15 @@ export const appMention: any = async ({ event, client, say }) => {
}

if (encodedImage) {
model = 'gpt-4-vision-preview'
model = 'gpt-4o'
max_tokens = 4096
contents.push(
{
image_url: {
url: 'data:image/' + filetype + ';base64,' + encodedImage,
detail: 'auto'
},
type: 'image_url'
}
)
contents.push({
image_url: {
url: 'data:image/' + filetype + ';base64,' + encodedImage,
detail: 'auto'
},
type: 'image_url'
})
}
}
}
Expand All @@ -62,9 +60,17 @@ export const appMention: any = async ({ event, client, say }) => {
})
)

if (isSummaryRequest) {
const summary = await generateSummary(threadMessages.filter(nonNullable), model, max_tokens)
await say({
text: summary,
thread_ts: event.ts
})
return
}

const gptAnswerText = await ask(threadMessages.filter(nonNullable), model, max_tokens)

/* スレッドに返信 */
await say({
text: gptAnswerText,
thread_ts: event.ts
Expand Down
89 changes: 0 additions & 89 deletions src/shortcut.test.ts

This file was deleted.

89 changes: 0 additions & 89 deletions src/shortcut.ts

This file was deleted.

Loading

0 comments on commit b52e96f

Please sign in to comment.