-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(sim-search): search Slack live as the person asking, instead of crawling it #7429
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
Open
waleedlatif1
wants to merge
4
commits into
staging
Choose a base branch
from
feat/slack-federated-search
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
12c671d
feat(sim-search): search Slack live as the person asking, instead of …
waleedlatif1 aa3a1cd
chore(contracts): export the Slack search schemas alongside their typ…
waleedlatif1 1ddd530
fix(sim-search): keep Slack searchable when it is the only source, an…
waleedlatif1 a20c640
fix(sim-search): refresh the Slack search once a connection completes
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { searchSimSearchSlackContract } from '@/lib/api/contracts/knowledge' | ||
| import { | ||
| defineInternalJsonRoute, | ||
| internalRateLimits, | ||
| internalSessionAuth, | ||
| } from '@/lib/api/server/routes' | ||
| import { internalKnowledgeErrorPolicies } from '@/lib/knowledge/api/route-policies' | ||
| import { knowledgeOperations } from '@/lib/knowledge/application/operations' | ||
| import { searchSimSearchSlack } from '@/lib/knowledge/application/sim-search' | ||
|
|
||
| export const POST = defineInternalJsonRoute({ | ||
| contract: searchSimSearchSlackContract, | ||
| auth: internalSessionAuth, | ||
| operation: knowledgeOperations.simSearchFederated, | ||
| rateLimit: internalRateLimits.none({ | ||
| reason: | ||
| "Slack's own per-user limit bounds this, and a failed call degrades to no Slack results", | ||
| }), | ||
| errorPolicy: internalKnowledgeErrorPolicies.search, | ||
| mapInput: ({ body }) => ({ | ||
| workspaceId: body.workspaceId, | ||
| query: body.query, | ||
| limit: body.limit, | ||
| }), | ||
| useCase: searchSimSearchSlack, | ||
| present: (result) => ({ success: true as const, data: result }), | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...workspace/[workspaceId]/home/components/knowledge-search-results/slack-search-results.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| 'use client' | ||
|
|
||
| import type { SimSearchSlackResult } from '@/lib/api/contracts/knowledge' | ||
| import { matchSnippet } from '@/lib/knowledge/search/snippet' | ||
| import { SourceCard } from '@/app/workspace/[workspaceId]/home/components/message-content/components/source-card' | ||
| import { | ||
| isHttpUrl, | ||
| type SourceTagData, | ||
| } from '@/app/workspace/[workspaceId]/home/components/message-content/components/special-tags' | ||
| import { useSimSearchSlack } from '@/hooks/queries/kb/knowledge' | ||
|
|
||
| function toSource(result: SimSearchSlackResult, query: string): SourceTagData | null { | ||
| if (!isHttpUrl(result.permalink)) return null | ||
| return { | ||
| url: result.permalink, | ||
| title: `#${result.channelName}`, | ||
| siteName: 'Slack', | ||
| connectorType: 'slack', | ||
| snippet: matchSnippet(result.text, query), | ||
| author: result.authorName, | ||
| updatedAt: result.sentAt ?? undefined, | ||
| } | ||
| } | ||
|
|
||
| interface SlackSearchResultsProps { | ||
| workspaceId: string | ||
| query: string | ||
| /** Asks the agent about one message; the prompt names it and links to it. */ | ||
| onSummarize: (prompt: string) => void | ||
| } | ||
|
|
||
| /** | ||
| * Slack messages matching the query, searched live as the signed-in person. | ||
| * | ||
| * Its own group rather than blended into the indexed results: Slack ranks with | ||
| * its own relevance, which is not comparable to a vector distance, so | ||
| * interleaving the two lists would present an ordering that means nothing. | ||
| * Nothing renders while Slack is unconnected — the Sources strip is where a | ||
| * person connects it — but a connection that stopped working says so, because | ||
| * reconnecting is something they can act on. | ||
| */ | ||
| export function SlackSearchResults({ workspaceId, query, onSummarize }: SlackSearchResultsProps) { | ||
| const { data, isPending, isPlaceholderData } = useSimSearchSlack(workspaceId, query) | ||
|
|
||
| if (!data || isPending || isPlaceholderData) return null | ||
| if (data.status === 'not_connected') return null | ||
|
|
||
| if (data.status === 'needs_reauth') { | ||
| return ( | ||
| <p className='px-2 py-2 text-[var(--text-muted)] text-caption'> | ||
| Reconnect Slack from Sources to search it. | ||
| </p> | ||
| ) | ||
| } | ||
| if (data.status === 'unavailable') { | ||
| return ( | ||
| <p className='px-2 py-2 text-[var(--text-muted)] text-caption'> | ||
| Slack could not be searched this time. | ||
| </p> | ||
| ) | ||
| } | ||
| if (data.results.length === 0) return null | ||
|
|
||
| return ( | ||
| <div className='flex flex-col'> | ||
| <p className='px-2 py-2 text-[var(--text-muted)] text-caption'> | ||
| <span className='tabular-nums'> | ||
| {data.results.length === 1 ? '1 Slack message' : `${data.results.length} Slack messages`} | ||
| </span> | ||
| {' · searched in Slack as you'} | ||
| </p> | ||
| {data.results.map((result) => { | ||
| const source = toSource(result, query) | ||
| return source ? ( | ||
| <SourceCard | ||
| key={`${result.channelId}:${result.messageTs}`} | ||
| source={source} | ||
| query={query} | ||
| onSummarize={(cited) => | ||
| onSummarize(`Summarize this Slack message from ${cited.title} (${cited.url})`) | ||
| } | ||
| /> | ||
| ) : null | ||
| })} | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { z } from 'zod' | ||
| import { workspaceIdSchema } from '@/lib/api/contracts/primitives' | ||
| import { defineRouteContract } from '@/lib/api/contracts/types' | ||
| import { SLACK_SEARCH_MAX_LIMIT } from '@/lib/slack-search/client' | ||
|
|
||
| export const MAX_SIM_SEARCH_SLACK_QUERY_LENGTH = 2000 | ||
|
|
||
| export const slackSearchBodySchema = z.object({ | ||
| workspaceId: workspaceIdSchema, | ||
| query: z | ||
| .string() | ||
| .min(1, 'query cannot be empty') | ||
| .max( | ||
| MAX_SIM_SEARCH_SLACK_QUERY_LENGTH, | ||
| `query cannot exceed ${MAX_SIM_SEARCH_SLACK_QUERY_LENGTH} characters` | ||
| ), | ||
| limit: z.number().int().min(1).max(SLACK_SEARCH_MAX_LIMIT).optional(), | ||
| }) | ||
|
|
||
| export const slackSearchResultSchema = z.object({ | ||
| channelId: z.string(), | ||
| messageTs: z.string(), | ||
| channelName: z.string(), | ||
| authorName: z.string(), | ||
| text: z.string(), | ||
| permalink: z.string().url(), | ||
| /** ISO-8601, or null when Slack returned a timestamp that would not parse. */ | ||
| sentAt: z.string().nullable(), | ||
| }) | ||
|
|
||
| /** | ||
| * Whether Slack answered, and if not, what the person can do about it. | ||
| * `needs_reauth` is its own state because reconnecting is a real action they | ||
| * can take, where `unavailable` is nothing they can fix. | ||
| */ | ||
| export const slackSearchStatusSchema = z.enum([ | ||
| 'ok', | ||
| 'not_connected', | ||
| 'needs_reauth', | ||
| 'unavailable', | ||
| ]) | ||
|
|
||
| /** | ||
| * Searching Slack for the person asking, live, under their own Slack account. | ||
| * Nothing is indexed and no result is stored, so this is a read that returns | ||
| * exactly what that person could have found in Slack themselves. | ||
| */ | ||
| export const searchSimSearchSlackContract = defineRouteContract({ | ||
| method: 'POST', | ||
| path: '/api/knowledge/sim-search/slack', | ||
| body: slackSearchBodySchema, | ||
| response: { | ||
| mode: 'json', | ||
| schema: z.object({ | ||
| success: z.literal(true), | ||
| data: z.object({ | ||
| status: slackSearchStatusSchema, | ||
| results: z.array(slackSearchResultSchema), | ||
| }), | ||
| }), | ||
| }, | ||
| }) | ||
|
|
||
| export type SearchSimSearchSlackBody = z.input<typeof slackSearchBodySchema> | ||
| export type SimSearchSlackResult = z.output<typeof slackSearchResultSchema> | ||
| export type SimSearchSlackStatus = z.output<typeof slackSearchStatusSchema> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.