|
1 | 1 | import { |
| 2 | + AgentFile, |
| 3 | + parseAgentFile, |
2 | 4 | parseAgentFileRules, |
3 | 5 | parseAgentFileTools, |
4 | 6 | } from "@continuedev/config-yaml"; |
5 | | - |
| 7 | +import fs from "fs"; |
| 8 | +import path from "path"; |
6 | 9 | import { |
7 | 10 | agentFileProcessor, |
8 | 11 | loadModelFromHub, |
9 | 12 | loadPackageFromHub, |
10 | 13 | } from "../hubLoader.js"; |
11 | 14 | import { logger } from "../util/logger.js"; |
12 | 15 |
|
| 16 | +import { getErrorString } from "src/util/error.js"; |
| 17 | +import { fileURLToPath } from "url"; |
13 | 18 | import { BaseService, ServiceWithDependencies } from "./BaseService.js"; |
14 | 19 | import { serviceContainer } from "./ServiceContainer.js"; |
15 | 20 | import { |
@@ -44,37 +49,55 @@ export class AgentFileService |
44 | 49 | return [SERVICE_NAMES.AUTH, SERVICE_NAMES.API_CLIENT]; |
45 | 50 | } |
46 | 51 |
|
| 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 | + |
47 | 80 | /** |
48 | 81 | * Initialize the agent file service with a hub slug |
49 | 82 | */ |
50 | 83 | async doInitialize( |
51 | | - agentFileSlug: string | undefined, |
| 84 | + agentFilePath: string | undefined, |
52 | 85 | authServiceState: AuthServiceState, |
53 | 86 | apiClientState: ApiClientServiceState, |
54 | 87 | ): Promise<AgentFileServiceState> { |
55 | | - if (!agentFileSlug) { |
| 88 | + if (!agentFilePath) { |
56 | 89 | return { |
57 | 90 | ...EMPTY_AGENT_FILE_STATE, |
58 | 91 | }; |
59 | 92 | } |
60 | 93 |
|
61 | 94 | 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); |
73 | 96 |
|
74 | 97 | // Set the basic agent file state |
75 | 98 | this.setState({ |
76 | 99 | agentFile, |
77 | | - slug: agentFileSlug, |
| 100 | + slug: agentFilePath, |
78 | 101 | }); |
79 | 102 |
|
80 | 103 | if (agentFile.model) { |
|
0 commit comments