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

[#2319] Fix Assistant utility types #2325

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 20 additions & 19 deletions docs/src/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ table tr:nth-child(even) {
}

/* changing the links to blue for accessibility */
p a, .markdown a {
p a,
.markdown a {
color: var(--slack-cloud-blue);
}

Expand Down Expand Up @@ -53,7 +54,7 @@ a:hover {
/* removing ToC line */
.table-of-contents__left-border {
border-left: none !important;
}
}

/* increasing name of SDK in sidebar */
.sidebar-title {
Expand All @@ -64,8 +65,8 @@ a:hover {

/* removing sidebar line and adding space to match ToC */
.theme-doc-sidebar-container {
border-right: none !important;
margin-right: 2rem;
border-right: none !important;
margin-right: 2rem;
}

/* announcement bar up top */
Expand Down Expand Up @@ -112,46 +113,46 @@ html[data-theme="dark"] .navbar-github-link::before {

/* Docs code bubbles */
[data-theme="light"] {
--code-link-background: #CFE9FE;
--code-link-text: rgb(21, 50, 59);
--code-link-background: #cfe9fe;
--code-link-text: rgb(21, 50, 59);

--method-link-background: #CDEFC4;
--method-link-background: #cdefc4;
--method-link-text: rgb(0, 41, 0);

--scope-link-background: #FBF3E0;
--scope-link-background: #fbf3e0;
--scope-link-text: rgb(63, 46, 0);

--event-link-background: #FDDDE3;
--event-link-text: rgb(74, 21, 75);
--event-link-background: #fddde3;
--event-link-text: rgb(74, 21, 75);
}

[data-theme="dark"] {
--code-link-text: white;
--method-link-text: white;
--scope-link-text: white;
--event-link-text: white;
--code-link-background: #1AB9FF50;
--method-link-background: #41B65850;
--scope-link-background: #FCC00350;
--event-link-background: #E3066A50;
--code-link-background: #1ab9ff50;
--method-link-background: #41b65850;
--scope-link-background: #fcc00350;
--event-link-background: #e3066a50;
}

a code {
background-color: var(--code-link-background);
color: var(--code-link-text);
color: var(--code-link-text);
}

a[href^="https://api.slack.com/methods"] > code {
background-color: var(--method-link-background);
color: var(--method-link-text);
color: var(--method-link-text);
}

a[href^="https://api.slack.com/scopes"] > code {
background-color: var(--scope-link-background);
color: var(--scope-link-text);
color: var(--scope-link-text);
}

a[href^="https://api.slack.com/events"] > code {
background-color: var(--event-link-background);
color: var(--event-link-text);
}
color: var(--event-link-text);
}
10 changes: 5 additions & 5 deletions src/Assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
type AssistantThreadContext,
type AssistantThreadContextStore,
DefaultThreadContextStore,
type GetThreadContextFn,
type SaveThreadContextFn,
} from './AssistantThreadContextStore';
import { AssistantInitializationError, AssistantMissingPropertyError } from './errors';
import processMiddleware from './middleware/process';
Expand All @@ -30,14 +28,16 @@ export interface AssistantConfig {
* Callback utilities
*/
interface AssistantUtilityArgs {
getThreadContext: GetThreadContextFn;
saveThreadContext: SaveThreadContextFn;
getThreadContext: GetThreadContextUtilFn;
saveThreadContext: SaveThreadContextUtilFn;
say: SayFn;
setStatus: SetStatusFn;
setSuggestedPrompts: SetSuggestedPromptsFn;
setTitle: SetTitleFn;
}

type GetThreadContextUtilFn = () => Promise<AssistantThreadContext>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Despite the ongoing issue with non-ack'd events, it appears to work as expected. This update corresponds to the enrich function. I believe I've gotten the pass-through correct:

// Do not pass preparedArgs (ie, do not add utilities to get/save)
preparedArgs.getThreadContext = () => threadContextStore.get(args);
preparedArgs.saveThreadContext = () => threadContextStore.save(args);

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good to me and this signature matches the assertion expectations in the matching tests 💯

type SaveThreadContextUtilFn = () => Promise<void>;
type SetStatusFn = (status: string) => Promise<AssistantThreadsSetStatusResponse>;

type SetSuggestedPromptsFn = (
Expand Down Expand Up @@ -310,7 +310,7 @@ function createSay(args: AllAssistantMiddlewareArgs): SayFn {
const { channelId: channel, threadTs: thread_ts, context } = extractThreadInfo(payload);

return async (message: Parameters<SayFn>[0]) => {
const threadContext = context.channel_id ? context : await args.getThreadContext(args);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since args is passed through in the enrich function, the removal of this additional pass-through shouldn't have an effect.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ohhh nice catch. This took me a while to parse - I kept confusing the middleware arguments' getThreadContext method with ThreadContextStore.get.

const threadContext = context.channel_id ? context : await args.getThreadContext();
const postMessageArgument: ChatPostMessageArguments =
typeof message === 'string' ? { text: message, channel, thread_ts } : { ...message, channel, thread_ts };

Expand Down
75 changes: 75 additions & 0 deletions test/types/assistant.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { expectError, expectType } from 'tsd';
import { type AllAssistantMiddlewareArgs, Assistant } from '../../src/Assistant';
import type { AssistantThreadContext } from '../../src/AssistantThreadContextStore';

// Constructor tests
const asyncNoop = () => Promise.resolve();

// missing required properties `threadStarted` and `userMessage`
expectError(new Assistant({}));

// missing required property `threadStarted`
expectError(
new Assistant({
userMessage: asyncNoop,
}),
);

// missing required property `userMessage`
expectError(
new Assistant({
threadStarted: asyncNoop,
}),
);

// happy construction
expectType<Assistant>(
new Assistant({
threadStarted: asyncNoop,
userMessage: asyncNoop,
}),
);

// threadStarted tests
new Assistant({
userMessage: asyncNoop,
threadStarted: async ({ saveThreadContext }) => {
expectType<void>(await saveThreadContext());
return Promise.resolve();
},
});

// userMessage tests
new Assistant({
userMessage: async ({ getThreadContext }) => {
expectType<AssistantThreadContext>(await getThreadContext());
return Promise.resolve();
},
threadStarted: asyncNoop,
});

// threadContextChanged tests
new Assistant({
userMessage: asyncNoop,
threadStarted: asyncNoop,
threadContextChanged: async ({ event }) => {
expectType<AssistantThreadContext>(event.assistant_thread.context);
return Promise.resolve();
},
});

// threadContextStore tests
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@filmaj Covered all class props, but let me know if I'm missing anything glaring or need to adjust the approach.

Copy link
Contributor

Choose a reason for hiding this comment

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

These tests look great, they cover all the utility functions mentioned in #2319 so I'm happy 👍

new Assistant({
threadContextStore: {
get: async (args) => {
expectType<AllAssistantMiddlewareArgs>(args);
return Promise.resolve({});
},
save: async (args) => {
expectType<AllAssistantMiddlewareArgs>(args);
return Promise.resolve();
},
},
userMessage: asyncNoop,
threadStarted: asyncNoop,
});