Skip to content
Open
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
28 changes: 25 additions & 3 deletions backend/src/lib/chatTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ import {
type OpenAIToolSchema,
} from "./llm";
import { safeErrorMessage } from "./safeError";
import {
integrationToolDisplayName,
runConfiguredIntegrationToolCall,
withConfiguredIntegrationTools,
} from "./integrations";

const STANDARD_FONT_DATA_URL = (() => {
try {
Expand Down Expand Up @@ -3626,6 +3631,20 @@ export async function runToolCalls(
tool_call_id: tc.id,
content: JSON.stringify(toolResultPayload),
});
} else {
const integrationToolResult =
await runConfiguredIntegrationToolCall(tc, args);
if (integrationToolResult) {
toolResults.push(integrationToolResult);
} else {
toolResults.push({
role: "tool",
tool_call_id: tc.id,
content: JSON.stringify({
error: `Unknown tool: ${tc.function.name}`,
}),
});
}
}
}

Expand Down Expand Up @@ -3968,9 +3987,11 @@ export async function runLLMStream(params: {
const researchTools = includeResearchTools ? COURTLISTENER_TOOLS : [];
const mcpTools = await buildUserMcpTools(userId, db);
const baseTools = [...TOOLS, ...researchTools, ...WORKFLOW_TOOLS];
const activeTools = extraTools?.length
? [...baseTools, ...mcpTools, ...extraTools]
: [...baseTools, ...mcpTools];
const activeTools = withConfiguredIntegrationTools(
(extraTools?.length
? [...baseTools, ...mcpTools, ...extraTools]
: [...baseTools, ...mcpTools]) as OpenAIToolSchema[],
);

// Extract system prompt; pass remaining turns to the adapter as
// plain user/assistant messages.
Expand Down Expand Up @@ -4146,6 +4167,7 @@ export async function runLLMStream(params: {
`data: ${JSON.stringify({
type: "tool_call_start",
name: call.name,
displayName: integrationToolDisplayName(call.name),
})}\n\n`,
);
},
Expand Down
41 changes: 41 additions & 0 deletions backend/src/lib/integrations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { OpenAIToolSchema } from "../llm";
import { trustFoundryIntegration } from "./trustfoundry";
import type {
IntegrationToolCall,
IntegrationToolResult,
ToolIntegration,
} from "./types";

const integrations: ToolIntegration[] = [trustFoundryIntegration];

export function configuredIntegrationTools(): OpenAIToolSchema[] {
return integrations.flatMap((integration) =>
integration.isEnabled() ? integration.tools() : [],
);
}

export function withConfiguredIntegrationTools(
tools: OpenAIToolSchema[],
): OpenAIToolSchema[] {
return [...tools, ...configuredIntegrationTools()];
}

export function integrationToolDisplayName(toolName: string): string | null {
const integration = integrations.find(
(candidate) =>
candidate.isEnabled() && candidate.canHandle(toolName),
);
return integration?.displayName(toolName) ?? null;
}

export async function runConfiguredIntegrationToolCall(
toolCall: IntegrationToolCall,
args: Record<string, unknown>,
): Promise<IntegrationToolResult | null> {
const integration = integrations.find(
(candidate) =>
candidate.isEnabled() && candidate.canHandle(toolCall.function.name),
);

return integration ? integration.run(toolCall, args) : null;
}
Loading