-
Notifications
You must be signed in to change notification settings - Fork 4.4k
feat: anthropic-plugin #1465
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
Closed
Closed
feat: anthropic-plugin #1465
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0df0b61
feat: add support for Anthropic (JS SDK)
abhishekpatil4 fcec923
fix: prettier
abhishekpatil4 51538a5
feat: add unit tests for AnthropicToolSet
abhishekpatil4 cf7ef46
fix: resolve conv + lint
abhishekpatil4 8c57267
feat: update package file
abhishekpatil4 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,80 @@ | ||
import { beforeAll, describe, expect, it } from "@jest/globals"; | ||
import { z } from "zod"; | ||
import { getTestConfig } from "../../config/getTestConfig"; | ||
import { ActionExecuteResponse } from "../sdk/models/actions"; | ||
import { AnthropicToolSet } from "./anthropic"; | ||
describe("AnthropicToolSet tests", () => { | ||
let anthropicToolset: AnthropicToolSet; | ||
beforeAll(() => { | ||
anthropicToolset = new AnthropicToolSet({ | ||
apiKey: getTestConfig().COMPOSIO_API_KEY, | ||
baseUrl: getTestConfig().BACKEND_HERMES_URL, | ||
}); | ||
}); | ||
|
||
it("should get tools as array", async () => { | ||
const tools = await anthropicToolset.getTools({ | ||
apps: ["github"], | ||
}); | ||
|
||
expect(tools).toBeInstanceOf(Array); | ||
}); | ||
|
||
it("should get specific tool by action name", async () => { | ||
const tools = await anthropicToolset.getTools({ | ||
actions: ["GITHUB_GITHUB_API_ROOT"], | ||
}); | ||
|
||
expect(tools.length).toBe(1); | ||
expect(tools[0]).toHaveProperty("name"); | ||
expect(tools[0]).toHaveProperty("description"); | ||
expect(tools[0]).toHaveProperty("input_schema"); | ||
}); | ||
|
||
it("should get tools with usecase limit", async () => { | ||
const tools = await anthropicToolset.getTools({ | ||
useCase: "follow user", | ||
apps: ["github"], | ||
useCaseLimit: 1, | ||
}); | ||
|
||
expect(tools.length).toBe(1); | ||
}); | ||
|
||
it("check if getTools -> actions are coming", async () => { | ||
const tools = await anthropicToolset.getTools({ | ||
actions: ["GITHUB_GITHUB_API_ROOT"], | ||
}); | ||
|
||
expect(Object.keys(tools).length).toBe(1); | ||
abhishekpatil4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
it("should create and execute custom action", async () => { | ||
abhishekpatil4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await anthropicToolset.createAction({ | ||
actionName: "starRepositoryCustomAction", | ||
toolName: "github", | ||
description: "This action stars a repository", | ||
inputParams: z.object({ | ||
owner: z.string(), | ||
repo: z.string(), | ||
}), | ||
callback: async ( | ||
inputParams, | ||
_authCredentials, | ||
executeRequest | ||
): Promise<ActionExecuteResponse> => { | ||
const res = await executeRequest({ | ||
endpoint: `/user/starred/${inputParams.owner}/${inputParams.repo}`, | ||
method: "PUT", | ||
parameters: [], | ||
}); | ||
return res; | ||
}, | ||
}); | ||
|
||
const tools = await anthropicToolset.getTools({ | ||
actions: ["starRepositoryCustomAction"], | ||
}); | ||
expect(tools.length).toBe(1); | ||
}); | ||
}); |
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,115 @@ | ||
import { Tool } from "@anthropic-ai/sdk/resources/messages/messages.js"; | ||
import { z } from "zod"; | ||
import { ComposioToolSet as BaseComposioToolSet } from "../sdk/base.toolset"; | ||
import { COMPOSIO_BASE_URL } from "../sdk/client/core/OpenAPI"; | ||
import { TELEMETRY_LOGGER } from "../sdk/utils/telemetry"; | ||
import { TELEMETRY_EVENTS } from "../sdk/utils/telemetry/events"; | ||
import { ZToolSchemaFilter } from "../types/base_toolset"; | ||
import { Optional } from "../types/util"; | ||
|
||
export class AnthropicToolSet extends BaseComposioToolSet { | ||
/** | ||
abhishekpatil4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Composio toolset for Anthropic framework. | ||
* | ||
*/ | ||
static FRAMEWORK_NAME = "anthropic"; | ||
static DEFAULT_ENTITY_ID = "default"; | ||
fileName: string = "js/src/frameworks/anthropic.ts"; | ||
|
||
constructor( | ||
config: { | ||
apiKey?: Optional<string>; | ||
baseUrl?: Optional<string>; | ||
entityId?: string; | ||
runtime?: string; | ||
} = {} | ||
) { | ||
super({ | ||
apiKey: config.apiKey || null, | ||
baseUrl: config.baseUrl || COMPOSIO_BASE_URL, | ||
runtime: config?.runtime || null, | ||
entityId: config.entityId || AnthropicToolSet.DEFAULT_ENTITY_ID, | ||
}); | ||
} | ||
|
||
private _wrapTool(schema: any, entityId: Optional<string> = null): Tool { | ||
abhishekpatil4 marked this conversation as resolved.
Show resolved
Hide resolved
abhishekpatil4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return { | ||
name: schema.name, | ||
description: schema.description, | ||
input_schema: { | ||
type: "object", | ||
properties: schema.parameters.properties || {}, | ||
required: schema.parameters.required || [], | ||
}, | ||
}; | ||
} | ||
abhishekpatil4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
async getTools( | ||
filters: z.infer<typeof ZToolSchemaFilter> = {}, | ||
entityId: Optional<string> = null | ||
): Promise<Tool[]> { | ||
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, { | ||
method: "getTools", | ||
file: this.fileName, | ||
params: { filters, entityId }, | ||
}); | ||
|
||
const tools = await this.getToolsSchema(filters, entityId); | ||
return tools.map((tool) => this._wrapTool(tool, entityId || this.entityId)); | ||
} | ||
|
||
async executeToolCall( | ||
toolCall: { name: string; arguments: string }, | ||
entityId: Optional<string> = null | ||
): Promise<string> { | ||
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, { | ||
method: "executeToolCall", | ||
file: this.fileName, | ||
params: { toolCall, entityId }, | ||
}); | ||
|
||
const toolSchema = await this.getToolsSchema({ | ||
actions: [toolCall.name], | ||
}); | ||
const appName = toolSchema[0]?.appName?.toLowerCase(); | ||
const connectedAccountId = appName && this.connectedAccountIds?.[appName]; | ||
|
||
return JSON.stringify( | ||
await this.executeAction({ | ||
action: toolCall.name, | ||
params: JSON.parse(toolCall.arguments), | ||
abhishekpatil4 marked this conversation as resolved.
Show resolved
Hide resolved
abhishekpatil4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
entityId: entityId || this.entityId, | ||
connectedAccountId: connectedAccountId, | ||
}) | ||
); | ||
} | ||
|
||
async handleToolCall( | ||
response: any, | ||
abhishekpatil4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
entityId: Optional<string> = null | ||
): Promise<string[]> { | ||
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, { | ||
method: "handleToolCall", | ||
file: this.fileName, | ||
params: { response, entityId }, | ||
}); | ||
|
||
const outputs: string[] = []; | ||
if (response.content && Array.isArray(response.content)) { | ||
for (const content of response.content) { | ||
abhishekpatil4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (content.type === "tool_use") { | ||
outputs.push( | ||
await this.executeToolCall( | ||
{ | ||
name: content.name, | ||
arguments: JSON.stringify(content.input), | ||
}, | ||
entityId | ||
) | ||
); | ||
} | ||
} | ||
} | ||
return outputs; | ||
} | ||
} |
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.
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.