slack-edge
Version:
Slack app development framework for edge functions with streamlined TypeScript support
106 lines • 5.68 kB
JavaScript
;
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _DefaultAssistantThreadContextStore_instances, _DefaultAssistantThreadContextStore_client, _DefaultAssistantThreadContextStore_thisBotUserId, _DefaultAssistantThreadContextStore_findFirstAssistantReply;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultAssistantThreadContextStore = void 0;
/**
* Default implementation of AssistantThreadContextStore that persists context
* in message metadata of the bot's first reply in a thread.
*
* This implementation stores context by updating the metadata of the assistant's
* first reply message in the thread. The context can then be retrieved by reading
* the metadata from that same message.
*
* @example
* ```typescript
* const store = new DefaultAssistantThreadContextStore({
* client: slackClient,
* thisBotUserId: "U12345"
* });
* ```
*/
class DefaultAssistantThreadContextStore {
/**
* Creates a new DefaultAssistantThreadContextStore instance.
* @param options - Configuration options including the Slack API client and bot user ID
*/
constructor({ client, thisBotUserId }) {
_DefaultAssistantThreadContextStore_instances.add(this);
_DefaultAssistantThreadContextStore_client.set(this, void 0);
_DefaultAssistantThreadContextStore_thisBotUserId.set(this, void 0);
__classPrivateFieldSet(this, _DefaultAssistantThreadContextStore_client, client, "f");
__classPrivateFieldSet(this, _DefaultAssistantThreadContextStore_thisBotUserId, thisBotUserId, "f");
}
/**
* Saves the context data by updating the metadata of the bot's first reply in the thread.
* If no first reply exists yet, the save operation is skipped silently.
* @param key - The unique identifier for the thread (channel_id and thread_ts)
* @param newContext - The context data to save
*/
async save(key, newContext) {
const reply = await __classPrivateFieldGet(this, _DefaultAssistantThreadContextStore_instances, "m", _DefaultAssistantThreadContextStore_findFirstAssistantReply).call(this, key);
if (reply) {
await __classPrivateFieldGet(this, _DefaultAssistantThreadContextStore_client, "f").chat.update({
...reply,
channel: reply.channel_id,
metadata: {
// this must be placed at the bottom
event_type: "assistant_thread_context",
event_payload: { ...newContext },
},
});
}
}
/**
* Retrieves the context data from the metadata of the bot's first reply in the thread.
* @param key - The unique identifier for the thread (channel_id and thread_ts)
* @returns The stored context data, or undefined if not found
*/
async find(key) {
const reply = await __classPrivateFieldGet(this, _DefaultAssistantThreadContextStore_instances, "m", _DefaultAssistantThreadContextStore_findFirstAssistantReply).call(this, key);
if (reply) {
return reply.metadata?.event_payload;
}
return undefined;
}
}
exports.DefaultAssistantThreadContextStore = DefaultAssistantThreadContextStore;
_DefaultAssistantThreadContextStore_client = new WeakMap(), _DefaultAssistantThreadContextStore_thisBotUserId = new WeakMap(), _DefaultAssistantThreadContextStore_instances = new WeakSet(), _DefaultAssistantThreadContextStore_findFirstAssistantReply = async function _DefaultAssistantThreadContextStore_findFirstAssistantReply(key) {
try {
const response = await __classPrivateFieldGet(this, _DefaultAssistantThreadContextStore_client, "f").conversations.replies({
channel: key.channel_id,
ts: key.thread_ts,
oldest: key.thread_ts,
include_all_metadata: true,
limit: 4,
});
if (response.messages) {
for (const message of response.messages) {
if (!("subtype" in message) && message.user === __classPrivateFieldGet(this, _DefaultAssistantThreadContextStore_thisBotUserId, "f")) {
return {
channel_id: key.channel_id,
ts: message.ts,
text: message.text,
blocks: message.blocks,
metadata: message.metadata,
};
}
}
}
}
catch (e) {
console.log(`Failed to fetch conversations.replies API result: ${e}`);
}
return undefined;
};
//# sourceMappingURL=thread-context-store.js.map