-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
feat: add LlamaIndex TS support to JS SDK #1178
base: master
Are you sure you want to change the base?
Changes from all commits
10895d3
f33976d
b7a8481
769477f
57535ff
d602522
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { beforeAll, describe, expect, it } from "@jest/globals"; | ||
import { z } from "zod"; | ||
import { getTestConfig } from "../../config/getTestConfig"; | ||
import { ActionExecuteResponse } from "../sdk/models/actions"; | ||
import { LlamaIndexToolSet } from "./llamaindex"; | ||
|
||
describe("Apps class tests", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test suite description |
||
let llamaindexToolSet: LlamaIndexToolSet; | ||
beforeAll(() => { | ||
llamaindexToolSet = new LlamaIndexToolSet({ | ||
apiKey: getTestConfig().COMPOSIO_API_KEY, | ||
baseUrl: getTestConfig().BACKEND_HERMES_URL, | ||
}); | ||
}); | ||
|
||
it("getools", async () => { | ||
const tools = await llamaindexToolSet.getTools({ | ||
apps: ["github"], | ||
}); | ||
expect(tools).toBeInstanceOf(Array); | ||
}); | ||
Comment on lines
+16
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test
Comment on lines
+16
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test |
||
it("check if tools are coming", async () => { | ||
const tools = await llamaindexToolSet.getTools({ | ||
actions: ["GITHUB_GITHUB_API_ROOT"], | ||
}); | ||
expect(tools.length).toBe(1); | ||
}); | ||
it("check if getTools, actions are coming", async () => { | ||
const tools = await llamaindexToolSet.getTools({ | ||
actions: ["GITHUB_GITHUB_API_ROOT"], | ||
}); | ||
|
||
expect(tools.length).toBe(1); | ||
}); | ||
Comment on lines
+22
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant tests in |
||
it("Should create custom action to star a repository", async () => { | ||
await llamaindexToolSet.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; | ||
}, | ||
}); | ||
Comment on lines
+36
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
const tools = await llamaindexToolSet.getTools({ | ||
actions: ["starRepositoryCustomAction"], | ||
}); | ||
|
||
await expect(tools.length).toBe(1); | ||
const actionOuput = await llamaindexToolSet.executeAction({ | ||
action: "starRepositoryCustomAction", | ||
params: { | ||
owner: "composioHQ", | ||
repo: "composio", | ||
}, | ||
entityId: "default", | ||
connectedAccountId: "9442cab3-d54f-4903-976c-ee67ef506c9b", | ||
Comment on lines
+66
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test |
||
}); | ||
|
||
expect(actionOuput).toHaveProperty("successful", true); | ||
}); | ||
Comment on lines
+35
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test suite in |
||
}); | ||
Comment on lines
+1
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant plus signs should be removed. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { FunctionTool, type JSONValue } from "llamaindex"; | ||
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, Sequence } from "../types/util"; | ||
|
||
type ToolSchema = { | ||
name: string; | ||
description: string; | ||
parameters: Record<string, unknown>; | ||
}; | ||
|
||
export class LlamaIndexToolSet extends BaseComposioToolSet { | ||
/** | ||
* Composio toolset for LlamaIndex framework. | ||
* | ||
*/ | ||
static FRAMEWORK_NAME = "llamaindex"; | ||
static DEFAULT_ENTITY_ID = "default"; | ||
fileName: string = "js/src/frameworks/llamaindex.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 || LlamaIndexToolSet.DEFAULT_ENTITY_ID, | ||
}); | ||
} | ||
|
||
private _wrapTool( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The interface ToolSchema {
name: string;
description: string;
parameters: {
properties?: Record<string, unknown>;
required?: string[];
};
} |
||
schema: ToolSchema, | ||
entityId: Optional<string> = null | ||
): FunctionTool<Record<string, unknown>, JSONValue | Promise<JSONValue>> { | ||
return FunctionTool.from( | ||
async (params: Record<string, unknown>) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding error handling for the JSON parsing operation: try {
return JSON.parse(JSON.stringify(result)) as JSONValue;
} catch (error) {
throw new Error(`Failed to parse tool result: ${error.message}`);
} This would provide better error messages and make debugging easier. |
||
const result = await this.executeAction({ | ||
action: schema.name, | ||
params, | ||
entityId: entityId || this.entityId, | ||
}); | ||
return JSON.parse(JSON.stringify(result)) as JSONValue; | ||
}, | ||
{ | ||
name: schema.name, | ||
description: schema.description, | ||
parameters: { | ||
type: "object", | ||
properties: schema.parameters.properties || {}, | ||
required: schema.parameters.required || [], | ||
Comment on lines
+57
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
}, | ||
Comment on lines
+57
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Comment on lines
+57
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} | ||
); | ||
} | ||
|
||
async getTools( | ||
filters: z.infer<typeof ZToolSchemaFilter> = {}, | ||
entityId: Optional<string> = null | ||
): Promise< | ||
Sequence< | ||
FunctionTool<Record<string, unknown>, JSONValue | Promise<JSONValue>> | ||
> | ||
> { | ||
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) => { | ||
const wrappedTool = this._wrapTool(tool, entityId || this.entityId); | ||
Object.assign(wrappedTool, { | ||
name: tool.name, | ||
description: tool.description, | ||
parameters: tool.parameters, | ||
}); | ||
return wrappedTool; | ||
}); | ||
} | ||
} | ||
Comment on lines
+1
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant plus signs should be removed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The plus signs in the added line are redundant and should be removed.
📝 Committable Code Suggestion