Skip to content

Commit 2c6bf0c

Browse files
authored
Merge pull request #121 from Liang-gong-ci-fang/main
feat: add gitcode search tool
2 parents 0e791eb + b107a7b commit 2c6bf0c

File tree

20 files changed

+3516
-0
lines changed

20 files changed

+3516
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.alibaba</groupId>
9+
<artifactId>ali-langengine</artifactId>
10+
<version>1.2.6-202508111516</version>
11+
<relativePath>../../pom.xml</relativePath>
12+
</parent>
13+
14+
<artifactId>ali-langengine-gitcode</artifactId>
15+
<packaging>jar</packaging>
16+
<name>ali-langengine-gitcode</name>
17+
<description>GitCode 搜索代码仓库支持</description>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>com.alibaba</groupId>
22+
<artifactId>ali-langengine-core</artifactId>
23+
<version>1.2.6-202508111516</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>com.squareup.okhttp3</groupId>
28+
<artifactId>okhttp</artifactId>
29+
<version>4.12.0</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>com.fasterxml.jackson.core</groupId>
34+
<artifactId>jackson-databind</artifactId>
35+
<version>2.15.2</version>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.projectlombok</groupId>
40+
<artifactId>lombok</artifactId>
41+
<scope>provided</scope>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.slf4j</groupId>
46+
<artifactId>slf4j-api</artifactId>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.apache.commons</groupId>
51+
<artifactId>commons-lang3</artifactId>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>org.mockito</groupId>
56+
<artifactId>mockito-core</artifactId>
57+
<version>4.6.1</version>
58+
<scope>test</scope>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>org.mockito</groupId>
63+
<artifactId>mockito-inline</artifactId>
64+
<version>4.6.1</version>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>net.bytebuddy</groupId>
69+
<artifactId>byte-buddy</artifactId>
70+
<version>1.12.10</version>
71+
<scope>test</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>net.bytebuddy</groupId>
75+
<artifactId>byte-buddy-agent</artifactId>
76+
<version>1.12.10</version>
77+
<scope>test</scope>
78+
</dependency>
79+
</dependencies>
80+
81+
<build>
82+
<plugins>
83+
<plugin>
84+
<artifactId>maven-source-plugin</artifactId>
85+
<executions>
86+
<execution>
87+
<id>attach-sources</id>
88+
<goals>
89+
<goal>jar-no-fork</goal>
90+
</goals>
91+
</execution>
92+
</executions>
93+
<configuration>
94+
<attach>true</attach>
95+
</configuration>
96+
</plugin>
97+
<plugin>
98+
<artifactId>maven-surefire-plugin</artifactId>
99+
<configuration>
100+
<includes>
101+
<include>com/alibaba/langengine/**/*.java</include>
102+
</includes>
103+
<systemPropertyVariables>
104+
<user.timezone>Asia/Shanghai</user.timezone>
105+
</systemPropertyVariables>
106+
</configuration>
107+
</plugin>
108+
<plugin>
109+
<groupId>org.moditect</groupId>
110+
<artifactId>moditect-maven-plugin</artifactId>
111+
</plugin>
112+
<plugin>
113+
<groupId>org.apache.maven.plugins</groupId>
114+
<artifactId>maven-compiler-plugin</artifactId>
115+
<configuration>
116+
<source>11</source>
117+
<target>11</target>
118+
</configuration>
119+
</plugin>
120+
</plugins>
121+
</build>
122+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/**
2+
* Copyright (C) 2025 AIDC-AI
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.langengine.gitcode;
18+
19+
import org.springframework.beans.factory.annotation.Value;
20+
import org.springframework.stereotype.Component;
21+
import org.springframework.util.StringUtils;
22+
23+
@Component
24+
public class GitCodeConfiguration {
25+
26+
/**
27+
* GitCode API Base URL
28+
*/
29+
public static final String GITCODE_API_URL = getEnvOrDefault("GITCODE_API_URL", "https://api.gitcode.com");
30+
31+
/**
32+
* GitCode API Token - read from environment variable first, then from config file
33+
*/
34+
@Value("${ali.langengine.community.gitcode.access_token:}")
35+
private String configAccessToken;
36+
37+
private static GitCodeConfiguration instance;
38+
39+
public GitCodeConfiguration() {
40+
instance = this;
41+
}
42+
43+
/**
44+
* Get GitCode Access Token
45+
* Priority: environment variable > config file
46+
*/
47+
public static String getAccessToken() {
48+
// Try to get from environment variable first
49+
String envToken = System.getenv("GITCODE_ACCESS_TOKEN");
50+
if (StringUtils.hasText(envToken)) {
51+
return envToken;
52+
}
53+
54+
// If environment variable is not set, get from config file
55+
if (instance != null && StringUtils.hasText(instance.configAccessToken)) {
56+
return instance.configAccessToken;
57+
}
58+
59+
return null;
60+
}
61+
62+
/**
63+
* Backward compatibility: keep original static constant, but now calls new method
64+
* @deprecated Recommend using getAccessToken() method
65+
*/
66+
@Deprecated
67+
public static final String GITCODE_ACCESS_TOKEN = getAccessToken();
68+
69+
/**
70+
* GitCode Search API URL
71+
*/
72+
public static final String GITCODE_SEARCH_API_URL = GITCODE_API_URL + "/api/v5/search";
73+
74+
/**
75+
* Default timeout in seconds
76+
*/
77+
public static final int DEFAULT_TIMEOUT_SECONDS = 30;
78+
79+
/**
80+
* Default number of results per page
81+
*/
82+
public static final int DEFAULT_PER_PAGE = 20;
83+
84+
/**
85+
* Maximum number of results per page
86+
*/
87+
public static final int MAX_PER_PAGE = 50;
88+
89+
/**
90+
* Get environment variable value, return default value if not exists
91+
*
92+
* @param key Environment variable key
93+
* @param defaultValue Default value
94+
* @return Environment variable value or default value
95+
*/
96+
private static String getEnvOrDefault(String key, String defaultValue) {
97+
String value = System.getenv(key);
98+
return value != null ? value : defaultValue;
99+
}
100+
101+
/**
102+
* Get integer environment variable value, return default value if not exists or cannot parse
103+
*
104+
* @param key Environment variable key
105+
* @param defaultValue Default value
106+
* @return Environment variable value or default value
107+
*/
108+
public static int getIntEnvOrDefault(String key, int defaultValue) {
109+
String value = System.getenv(key);
110+
if (value != null) {
111+
try {
112+
return Integer.parseInt(value);
113+
} catch (NumberFormatException e) {
114+
return defaultValue;
115+
}
116+
}
117+
return defaultValue;
118+
}
119+
120+
/**
121+
* Validate if GitCode Access Token exists
122+
*
123+
* @return true if token exists and is not empty, false otherwise
124+
*/
125+
public static boolean hasValidToken() {
126+
String token = getAccessToken();
127+
return StringUtils.hasText(token);
128+
}
129+
130+
/**
131+
* Validate if configuration is complete
132+
*
133+
* @throws IllegalStateException if configuration is incomplete
134+
*/
135+
public static void validateConfiguration() {
136+
if (!hasValidToken()) {
137+
throw new IllegalStateException(
138+
"GitCode Access Token is not configured. " +
139+
"Please set GITCODE_ACCESS_TOKEN environment variable or " +
140+
"configure ali.langengine.community.gitcode.access_token in application.yml");
141+
}
142+
143+
if (GITCODE_API_URL == null || GITCODE_API_URL.trim().isEmpty()) {
144+
throw new IllegalStateException("GitCode API URL is not configured.");
145+
}
146+
}
147+
148+
/**
149+
* Get configuration summary (without sensitive information)
150+
*
151+
* @return Configuration summary string
152+
*/
153+
public static String getConfigurationSummary() {
154+
StringBuilder summary = new StringBuilder();
155+
summary.append("GitCode Configuration:\n");
156+
summary.append(" API URL: ").append(GITCODE_API_URL).append("\n");
157+
summary.append(" Token configured: ").append(hasValidToken() ? "Yes" : "No").append("\n");
158+
summary.append(" Search API URL: ").append(GITCODE_SEARCH_API_URL).append("\n");
159+
summary.append(" Default timeout: ").append(DEFAULT_TIMEOUT_SECONDS).append("s\n");
160+
summary.append(" Default per page: ").append(DEFAULT_PER_PAGE).append("\n");
161+
summary.append(" Max per page: ").append(MAX_PER_PAGE);
162+
return summary.toString();
163+
}
164+
}

0 commit comments

Comments
 (0)