Skip to content

Commit e74b4a1

Browse files
committed
feat: support paths and file URLs for agent file loadinggs
1 parent 6f992b7 commit e74b4a1

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

extensions/cli/src/services/AgentFileService.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import {
2+
AgentFile,
3+
parseAgentFile,
24
parseAgentFileRules,
35
parseAgentFileTools,
46
} from "@continuedev/config-yaml";
5-
7+
import fs from "fs";
8+
import path from "path";
69
import {
710
agentFileProcessor,
811
loadModelFromHub,
912
loadPackageFromHub,
1013
} from "../hubLoader.js";
1114
import { logger } from "../util/logger.js";
1215

16+
import { getErrorString } from "src/util/error.js";
17+
import { fileURLToPath } from "url";
1318
import { BaseService, ServiceWithDependencies } from "./BaseService.js";
1419
import { serviceContainer } from "./ServiceContainer.js";
1520
import {
@@ -44,37 +49,55 @@ export class AgentFileService
4449
return [SERVICE_NAMES.AUTH, SERVICE_NAMES.API_CLIENT];
4550
}
4651

52+
private async getAgentFile(agentPath: string): Promise<AgentFile> {
53+
try {
54+
const parts = agentPath.split("/");
55+
if (parts.length === 2 && parts[0] && parts[1]) {
56+
try {
57+
return await loadPackageFromHub(agentPath, agentFileProcessor);
58+
} catch (e) {
59+
logger.info(
60+
`Failed to load agent file from slug-like path: ${agentPath}`,
61+
);
62+
// slug COULD be path, fall back to relative path
63+
}
64+
}
65+
if (agentPath.startsWith("file:/")) {
66+
const path = fileURLToPath(agentPath);
67+
const content = fs.readFileSync(path, "utf-8");
68+
return parseAgentFile(content);
69+
}
70+
const resolvedPath = path.resolve(agentPath);
71+
const content = fs.readFileSync(resolvedPath, "utf-8");
72+
return parseAgentFile(content);
73+
} catch (e) {
74+
throw new Error(
75+
`Failed to load agent from ${agentPath}: ${getErrorString(e)}`,
76+
);
77+
}
78+
}
79+
4780
/**
4881
* Initialize the agent file service with a hub slug
4982
*/
5083
async doInitialize(
51-
agentFileSlug: string | undefined,
84+
agentFilePath: string | undefined,
5285
authServiceState: AuthServiceState,
5386
apiClientState: ApiClientServiceState,
5487
): Promise<AgentFileServiceState> {
55-
if (!agentFileSlug) {
88+
if (!agentFilePath) {
5689
return {
5790
...EMPTY_AGENT_FILE_STATE,
5891
};
5992
}
6093

6194
try {
62-
const parts = agentFileSlug.split("/");
63-
if (parts.length !== 2) {
64-
throw new Error(
65-
`Invalid agent slug format. Expected "owner/package", got: ${agentFileSlug}`,
66-
);
67-
}
68-
69-
const agentFile = await loadPackageFromHub(
70-
agentFileSlug,
71-
agentFileProcessor,
72-
);
95+
const agentFile = await this.getAgentFile(agentFilePath);
7396

7497
// Set the basic agent file state
7598
this.setState({
7699
agentFile,
77-
slug: agentFileSlug,
100+
slug: agentFilePath,
78101
});
79102

80103
if (agentFile.model) {

0 commit comments

Comments
 (0)