@polka-codes/cli 0.10.21 → 0.10.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,176 +0,0 @@
1
- import {
2
- FIX_SYSTEM_PROMPT,
3
- getDefaultContext,
4
- getFixUserPrompt
5
- } from "./chunk-2LRQ2QH6.js";
6
-
7
- // src/workflows/fix.workflow.ts
8
- import {
9
- agentWorkflow,
10
- executeCommand,
11
- fetchUrl,
12
- listFiles,
13
- readBinaryFile,
14
- readFile,
15
- removeFile,
16
- renameFile,
17
- replaceInFile,
18
- searchFiles,
19
- writeToFile
20
- } from "@polka-codes/core";
21
- import { z } from "zod";
22
- var FixIterationSummarySchema = z.object({
23
- summary: z.string().nullish(),
24
- bailReason: z.string().nullish()
25
- }).refine((data) => data.summary != null !== (data.bailReason != null), {
26
- message: "Either summary or bailReason must be provided, but not both"
27
- });
28
- var fixWorkflow = async (input, context) => {
29
- const { tools, logger, step } = context;
30
- const { command: inputCommand, task, prompt, interactive = true, additionalTools } = input;
31
- let command = inputCommand;
32
- const summaries = [];
33
- let formatCommand;
34
- const buildScriptCommand = (scriptName) => {
35
- const runtime = process.argv[0];
36
- const script = process.argv[1];
37
- return `"${runtime}" "${script}" run ${scriptName}`;
38
- };
39
- if (!command) {
40
- const config = input.config;
41
- const check = config?.scripts?.check;
42
- const test = config?.scripts?.test;
43
- const format = config?.scripts?.format;
44
- let checkCommand;
45
- if (typeof check === "string") {
46
- checkCommand = check;
47
- } else if (check && "command" in check) {
48
- checkCommand = check.command;
49
- } else if (check && "script" in check) {
50
- checkCommand = buildScriptCommand("check");
51
- } else if (check && "workflow" in check) {
52
- logger.warn("Workflow scripts are not yet supported in fix workflow");
53
- }
54
- let testCommand;
55
- if (typeof test === "string") {
56
- testCommand = test;
57
- } else if (test && "command" in test) {
58
- testCommand = test.command;
59
- } else if (test && "script" in test) {
60
- testCommand = buildScriptCommand("test");
61
- } else if (test && "workflow" in test) {
62
- logger.warn("Workflow scripts are not yet supported in fix workflow");
63
- }
64
- if (typeof format === "string") {
65
- formatCommand = format;
66
- } else if (format && "command" in format) {
67
- formatCommand = format.command;
68
- } else if (format && "script" in format) {
69
- formatCommand = buildScriptCommand("format");
70
- } else if (format && "workflow" in format) {
71
- logger.warn("Workflow scripts are not yet supported in fix workflow");
72
- }
73
- let defaultCommand;
74
- if (checkCommand && testCommand) {
75
- defaultCommand = `${checkCommand} && ${testCommand}`;
76
- } else if (checkCommand) {
77
- defaultCommand = checkCommand;
78
- } else if (testCommand) {
79
- defaultCommand = testCommand;
80
- }
81
- if (interactive) {
82
- command = await tools.input({
83
- message: "Please enter the command to run to identify issues:",
84
- default: defaultCommand
85
- });
86
- if (!command) {
87
- throw new Error("No command provided. Aborting.");
88
- }
89
- } else {
90
- command = defaultCommand;
91
- }
92
- if (!command) {
93
- logger.info("No command to run.");
94
- return { success: true, summaries };
95
- }
96
- }
97
- for (let i = 0; i < 10; i++) {
98
- logger.info(`Running command (attempt ${i + 1}/10): ${command}`);
99
- if (formatCommand) {
100
- await tools.executeCommand({ command: formatCommand, shell: true, pipe: true });
101
- }
102
- const { exitCode, stdout, stderr } = await tools.executeCommand({
103
- command,
104
- shell: true,
105
- pipe: true
106
- });
107
- if (exitCode === 0) {
108
- logger.info("Command succeeded!");
109
- return { success: true, summaries };
110
- }
111
- logger.info(`Command failed with exit code ${exitCode}. Asking agent to fix it...`);
112
- const result = await step(`fix-${i}`, async () => {
113
- const { context: defaultContext } = await getDefaultContext(input.config, "fix");
114
- const memoryContext = await tools.getMemoryContext();
115
- const userPrompt = getFixUserPrompt(command, exitCode, stdout, stderr, task, prompt);
116
- const agentTools = [
117
- readFile,
118
- writeToFile,
119
- replaceInFile,
120
- searchFiles,
121
- listFiles,
122
- executeCommand,
123
- fetchUrl,
124
- readBinaryFile,
125
- removeFile,
126
- renameFile
127
- ];
128
- if (additionalTools?.search) {
129
- agentTools.push(additionalTools.search);
130
- }
131
- if (additionalTools?.mcpTools) {
132
- agentTools.push(...additionalTools.mcpTools);
133
- }
134
- return await agentWorkflow(
135
- {
136
- systemPrompt: FIX_SYSTEM_PROMPT,
137
- userMessage: [
138
- {
139
- role: "user",
140
- content: `${userPrompt}
141
-
142
- ${defaultContext}
143
- ${memoryContext}`
144
- }
145
- ],
146
- tools: agentTools,
147
- outputSchema: FixIterationSummarySchema
148
- },
149
- context
150
- );
151
- });
152
- const res = await step(`fix-summary-${i}`, async () => {
153
- if (result.type === "Exit" && result.object) {
154
- const { summary, bailReason } = result.object;
155
- if (bailReason) {
156
- logger.warn(`Agent bailed: ${bailReason}`);
157
- return { bailReason };
158
- }
159
- if (summary) {
160
- summaries.push(summary);
161
- await tools.updateMemory({ operation: "append", content: `Summary of changes for fix attempt ${i + 1}: ${summary}` });
162
- logger.info(`Summary of changes: ${summary}`);
163
- }
164
- }
165
- });
166
- if (res?.bailReason) {
167
- return { success: false, summaries, reason: res.bailReason };
168
- }
169
- }
170
- logger.error("Failed to fix the issue after maximum attempts.");
171
- return { success: false, summaries, reason: "Failed to fix the issue after maximum attempts." };
172
- };
173
-
174
- export {
175
- fixWorkflow
176
- };
@@ -1,40 +0,0 @@
1
- // src/mcp/errors.ts
2
- var McpError = class extends Error {
3
- serverName;
4
- retryable;
5
- constructor(serverName, message, retryable) {
6
- super(message);
7
- this.name = this.constructor.name;
8
- this.serverName = serverName;
9
- this.retryable = retryable;
10
- }
11
- };
12
- var McpConnectionError = class extends McpError {
13
- constructor(serverName, cause) {
14
- super(
15
- serverName,
16
- `Failed to connect to MCP server '${serverName}'. Please check the server configuration and ensure it is available.`,
17
- false
18
- );
19
- this.cause = cause;
20
- }
21
- };
22
- var McpToolError = class extends McpError {
23
- constructor(serverName, toolName, message, cause) {
24
- super(serverName, `Tool '${toolName}' on server '${serverName}' failed: ${message}`, false);
25
- this.toolName = toolName;
26
- this.cause = cause;
27
- }
28
- };
29
- var McpServerError = class extends McpError {
30
- constructor(serverName) {
31
- super(serverName, `MCP server '${serverName}' not found in configuration.`, false);
32
- }
33
- };
34
-
35
- export {
36
- McpError,
37
- McpConnectionError,
38
- McpToolError,
39
- McpServerError
40
- };
@@ -1,10 +0,0 @@
1
- import {
2
- codeWorkflow
3
- } from "./chunk-HB7PTE3H.js";
4
- import "./chunk-ZS4K5RFU.js";
5
- import "./chunk-YPUL66UK.js";
6
- import "./chunk-LLMPMGV3.js";
7
- import "./chunk-2LRQ2QH6.js";
8
- export {
9
- codeWorkflow
10
- };
@@ -1,9 +0,0 @@
1
- import {
2
- commitWorkflow
3
- } from "./chunk-FSNPWI3C.js";
4
- import "./chunk-LLMPMGV3.js";
5
- import "./chunk-UEEU3SCC.js";
6
- import "./chunk-2LRQ2QH6.js";
7
- export {
8
- commitWorkflow
9
- };
@@ -1,7 +0,0 @@
1
- import {
2
- fixWorkflow
3
- } from "./chunk-ZS4K5RFU.js";
4
- import "./chunk-2LRQ2QH6.js";
5
- export {
6
- fixWorkflow
7
- };
@@ -1,8 +0,0 @@
1
- import {
2
- planWorkflow
3
- } from "./chunk-YPUL66UK.js";
4
- import "./chunk-LLMPMGV3.js";
5
- import "./chunk-2LRQ2QH6.js";
6
- export {
7
- planWorkflow
8
- };
@@ -1,8 +0,0 @@
1
- import {
2
- reviewWorkflow
3
- } from "./chunk-NRDSZGMF.js";
4
- import "./chunk-UEEU3SCC.js";
5
- import "./chunk-2LRQ2QH6.js";
6
- export {
7
- reviewWorkflow
8
- };
@@ -1,155 +0,0 @@
1
- import {
2
- McpConnectionError,
3
- McpToolError
4
- } from "./chunk-ZU4UU65A.js";
5
-
6
- // src/mcp/sdk-client.ts
7
- import { Client } from "@modelcontextprotocol/sdk/client/index.js";
8
- import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
9
- var SdkMcpClient = class {
10
- constructor(serverName, config) {
11
- this.serverName = serverName;
12
- this.config = config;
13
- }
14
- client = null;
15
- transport = null;
16
- initialized = false;
17
- /**
18
- * Connect to the MCP server and initialize
19
- */
20
- async connect() {
21
- if (!this.config.command) {
22
- throw new McpConnectionError(this.serverName, new Error("No command specified for MCP server"));
23
- }
24
- try {
25
- this.transport = new StdioClientTransport({
26
- command: this.config.command,
27
- args: this.config.args || [],
28
- env: this.config.env
29
- });
30
- this.client = new Client(
31
- {
32
- name: `polka-codes-${this.serverName}`,
33
- version: "1.0.0"
34
- },
35
- {
36
- capabilities: {}
37
- }
38
- );
39
- await this.client.connect(this.transport);
40
- this.initialized = true;
41
- } catch (error) {
42
- throw new McpConnectionError(this.serverName, error instanceof Error ? error : new Error(String(error)));
43
- }
44
- }
45
- /**
46
- * Check if connected
47
- */
48
- isConnected() {
49
- return this.initialized && this.client !== null;
50
- }
51
- /**
52
- * Disconnect from the server
53
- */
54
- async disconnect() {
55
- if (this.client) {
56
- await this.client.close();
57
- this.client = null;
58
- }
59
- this.transport = null;
60
- this.initialized = false;
61
- }
62
- /**
63
- * List available tools from the server
64
- */
65
- async listTools() {
66
- if (!this.client || !this.isConnected()) {
67
- throw new McpConnectionError(this.serverName, new Error("Client not connected"));
68
- }
69
- try {
70
- const response = await this.client.listTools({});
71
- return response.tools.map((tool) => ({
72
- name: tool.name,
73
- description: tool.description || "",
74
- inputSchema: tool.inputSchema
75
- }));
76
- } catch (error) {
77
- throw new McpConnectionError(this.serverName, error instanceof Error ? error : new Error(String(error)));
78
- }
79
- }
80
- /**
81
- * Call a tool on the server
82
- */
83
- async callTool(name, args) {
84
- if (!this.client || !this.isConnected()) {
85
- throw new McpConnectionError(this.serverName, new Error("Client not connected"));
86
- }
87
- try {
88
- const response = await this.client.callTool({
89
- name,
90
- arguments: args
91
- });
92
- const content = "content" in response ? response.content : [];
93
- const textParts = content.filter((c) => c.type === "text").map((c) => c.text);
94
- const text = textParts.length > 0 ? textParts.join("\n") : JSON.stringify(response, null, 2);
95
- return {
96
- content: [
97
- {
98
- type: "text",
99
- text
100
- }
101
- ],
102
- isError: "isError" in response ? response.isError : void 0
103
- };
104
- } catch (error) {
105
- if (error instanceof McpToolError) {
106
- throw error;
107
- }
108
- const errorMessage = error instanceof Error ? `Failed to call tool '${name}': ${error.message}` : `Failed to call tool '${name}': ${String(error)}`;
109
- throw new McpConnectionError(this.serverName, new Error(errorMessage));
110
- }
111
- }
112
- /**
113
- * List available resources from the server
114
- */
115
- async listResources() {
116
- if (!this.client || !this.isConnected()) {
117
- throw new McpConnectionError(this.serverName, new Error("Client not connected"));
118
- }
119
- try {
120
- const response = await this.client.listResources({});
121
- return response.resources.map((resource) => ({
122
- uri: resource.uri,
123
- name: resource.name,
124
- description: resource.description,
125
- mimeType: resource.mimeType
126
- }));
127
- } catch (error) {
128
- throw new McpConnectionError(this.serverName, error instanceof Error ? error : new Error(String(error)));
129
- }
130
- }
131
- /**
132
- * Read a resource from the server
133
- */
134
- async readResource(uri) {
135
- if (!this.client || !this.isConnected()) {
136
- throw new McpConnectionError(this.serverName, new Error("Client not connected"));
137
- }
138
- try {
139
- const response = await this.client.readResource({ uri });
140
- const textParts = response.contents?.filter((c) => "text" in c).map((c) => c.text) || [];
141
- return textParts.length > 0 ? textParts.join("\n") : JSON.stringify(response, null, 2);
142
- } catch (error) {
143
- throw new McpConnectionError(this.serverName, error instanceof Error ? error : new Error(String(error)));
144
- }
145
- }
146
- /**
147
- * Get the server name
148
- */
149
- getServerName() {
150
- return this.serverName;
151
- }
152
- };
153
- export {
154
- SdkMcpClient
155
- };