Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebo committed Sep 6, 2024
1 parent 2de4535 commit 184d95a
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 10 deletions.
1 change: 1 addition & 0 deletions apps/copilot/src/cmds/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './history';
export * from './clear';
export * from './logout';
10 changes: 10 additions & 0 deletions apps/copilot/src/cmds/logout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { MessageSendActivity } from '@teams.sdk/api';
import { Context } from '@teams.sdk/apps';

import { State } from '../state';

export async function logout({ activity, storage }: Context<MessageSendActivity>) {
const state = await State.fromActivity(activity, storage);
state.user = {};
await state.save(activity, storage);
}
5 changes: 3 additions & 2 deletions apps/copilot/src/events/mention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function mention({
// if not authenticated, set the conversation
// where the auth flow began and prompt user to
// to sign in.
if (!state.user.auth?.token) {
if (!state.authenticated) {
state.user.auth = {
conversationId: activity.conversation.id,
};
Expand All @@ -29,10 +29,11 @@ export async function mention({
}

await say({ type: 'typing' });

const prompt = new RootPrompt(say, state);
const { text, attachments } = await prompt.chat(activity.text);
await state.save(activity, storage);

await state.save(activity, storage);
await say(
withAIContentLabel({
type: 'message',
Expand Down
7 changes: 4 additions & 3 deletions apps/copilot/src/events/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export async function message({
withAIContentLabel,
next,
}: Context<MessageSendActivity>) {
if (activity.conversation.isGroup) return next();
if (activity.conversation.isGroup) return;

const start = new Date();
const state = await State.fromActivity(activity, storage);

// if not authenticated, set the conversation
// where the auth flow began and prompt user to
// to sign in.
if (!state.user.auth?.token) {
if (state.authenticated) {
state.user.auth = {
conversationId: activity.conversation.id,
};
Expand All @@ -32,10 +32,11 @@ export async function message({
}

await say({ type: 'typing' });

const prompt = new RootPrompt(say, state);
const { text, attachments } = await prompt.chat(activity.text);
await state.save(activity, storage);

await state.save(activity, storage);
await say(
withAIContentLabel({
type: 'message',
Expand Down
3 changes: 2 additions & 1 deletion apps/copilot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ const app = new App({

app.message('/clear', cmds.clear);
app.message('/history', cmds.history);
app.message('/logout', cmds.logout);
app.event('signin', events.signin);

app.on('install.add', events.install);
app.on('install.remove', events.uninstall);
app.on('message', events.message);
app.on('mention', events.mention);
app.on('message', events.message);
app.on('dialog.open', events.dialogOpen);
app.on('message.submit.feedback', () => {
return {
Expand Down
4 changes: 0 additions & 4 deletions apps/copilot/src/prompts/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ export class RootPrompt {
messages: new LocalMemory({
max: 10,
messages: state.chat.messages,
collapse: {
model,
strategy: 'half',
},
}),
instructions: [
'You are an ai assistant that runs in Microsoft Teams.',
Expand Down
9 changes: 9 additions & 0 deletions apps/copilot/src/state.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { Message } from '@teams.sdk/ai';
import { Activity } from '@teams.sdk/api';
import { Storage } from '@teams.sdk/common/storage';

import * as MSGraph from '@microsoft/microsoft-graph-types';
import { isAfter } from 'date-fns';

export class State {
user: UserState;
chat: ChatState;

get authenticated() {
return (
!!this.user.auth?.token &&
(!this.user.auth.expiration || isAfter(new Date(), this.user.auth.expiration))
);
}

constructor(user: UserState, chat: ChatState) {
this.user = user;
this.chat = chat;
Expand Down

0 comments on commit 184d95a

Please sign in to comment.