fix(search): inject personal integrations into chat prompts by TheodoreSpeaks · Pull Request #7751 · simstudioai/sim · GitHub
Skip to content
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
161 changes: 161 additions & 0 deletions apps/sim/lib/copilot/application/load-search-integrations.test.ts
63 changes: 63 additions & 0 deletions apps/sim/lib/copilot/application/load-search-integrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
COPILOT_APPLICATION_DELEGATION_TTL_MS,
createTrustedOrganizationCopilotPrincipal,
} from '@/lib/copilot/auth/application-delegation'
import { authorizeOrganizationChatDelegation } from '@/lib/copilot/chat/organization-chats'
import { knowledgeDelegationPolicy } from '@/lib/knowledge/application/authorization'
import { listPersonalSearchIntegrations } from '@/lib/knowledge/application/personal-search-integrations'

const MAX_INVENTORY_PAGES = 100
const MAX_INVENTORY_BYTES = 256 * 1024

interface SearchIntegrationsContext {
userId: string
organizationId: string
chatId: string
messageId: string
signal?: AbortSignal
}

type IntegrationInventory = Awaited<ReturnType<typeof listPersonalSearchIntegrations.execute>>

/** Loads the complete current person's Search inventory for one authenticated chat turn. */
export async function loadCopilotSearchIntegrations(
context: SearchIntegrationsContext
): Promise<string> {
context.signal?.throwIfAborted()
const principal = createTrustedOrganizationCopilotPrincipal(
{ ...context, delegationId: context.messageId },
{
audience: knowledgeDelegationPolicy.audience,
ttlMs: COPILOT_APPLICATION_DELEGATION_TTL_MS,
}
)
await authorizeOrganizationChatDelegation.execute({ principal })

const connections: IntegrationInventory['connections'] = []
const available = new Map<string, IntegrationInventory['available'][number]>()
const cursors = new Set<string>()
let cursor: string | undefined
for (let pageNumber = 0; pageNumber < MAX_INVENTORY_PAGES; pageNumber++) {
context.signal?.throwIfAborted()
const page = await listPersonalSearchIntegrations.execute({
principal,
input: { organizationId: context.organizationId, ...(cursor ? { cursor } : {}) },
})
context.signal?.throwIfAborted()
connections.push(...page.connections)
for (const entry of page.available) {
available.set(JSON.stringify(entry.target), entry)
}
const inventory = JSON.stringify({ connections, available: [...available.values()] })
if (Buffer.byteLength(inventory) > MAX_INVENTORY_BYTES) {
throw new Error('Search integration inventory exceeds the prompt size limit')
}
if (page.nextCursor === null) return inventory
if (cursors.has(page.nextCursor)) {
throw new Error('Search integration inventory pagination did not advance')
}
cursors.add(page.nextCursor)
cursor = page.nextCursor
}
throw new Error('Search integration inventory exceeds the pagination limit')
}
1 change: 0 additions & 1 deletion apps/sim/lib/copilot/assistant/tool-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { ToolMetadata } from '@/tools/metadata'
export const ASSISTANT_TOOLS = new Set([
'search_workspace',
'read_document',
'list_integrations',
'search_integration_tools',
'call_integration_tool',
'oauth_get_auth_link',
Expand Down
18 changes: 0 additions & 18 deletions apps/sim/lib/copilot/generated/tool-catalog-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export interface ToolCatalogEntry {
| 'knowledge'
| 'list_deployment_versions'
| 'list_integration_tools'
| 'list_integrations'
| 'list_workspace_mcp_servers'
| 'load_deployment'
| 'load_integration_tool'
Expand Down Expand Up @@ -213,7 +212,6 @@ export interface ToolCatalogEntry {
| 'knowledge'
| 'list_deployment_versions'
| 'list_integration_tools'
| 'list_integrations'
| 'list_workspace_mcp_servers'
| 'load_deployment'
| 'load_integration_tool'
Expand Down Expand Up @@ -3543,21 +3541,6 @@ export const ListIntegrationTools: ToolCatalogEntry = {
},
}

export const ListIntegrations: ToolCatalogEntry = {
id: 'list_integrations',
name: 'list_integrations',
route: 'sim',
mode: 'async',
parameters: {
additionalProperties: false,
properties: {
connectorType: { maxLength: 100, minLength: 1, type: 'string' },
cursor: { maxLength: 1024, minLength: 1, type: 'string' },
},
type: 'object',
},
}

export const ListWorkspaceMcpServers: ToolCatalogEntry = {
id: 'list_workspace_mcp_servers',
name: 'list_workspace_mcp_servers',
Expand Down Expand Up @@ -7664,7 +7647,6 @@ export const TOOL_CATALOG: Record<string, ToolCatalogEntry> = {
[Knowledge.id]: Knowledge,
[ListDeploymentVersions.id]: ListDeploymentVersions,
[ListIntegrationTools.id]: ListIntegrationTools,
[ListIntegrations.id]: ListIntegrations,
[ListWorkspaceMcpServers.id]: ListWorkspaceMcpServers,
[LoadDeployment.id]: LoadDeployment,
[LoadIntegrationTool.id]: LoadIntegrationTool,
Expand Down
19 changes: 0 additions & 19 deletions apps/sim/lib/copilot/generated/tool-schemas-v1.ts
Loading
Loading