Note: Untuk mempelajari bagaimana "zero data retention" (retensi data nol), atau ZDR, berlaku untuk fitur ini, lihat API dan retensi data.
Haijun dapat menggunakan alat editor teks berskema Juglow untuk melihat dan memodifikasi file teks, membantu Anda melakukan debug, memperbaiki, dan meningkatkan kode atau dokumen teks lainnya. Hal ini memungkinkan Haijun berinteraksi langsung dengan file Anda, memberikan bantuan praktis alih-alih hanya menyarankan perubahan.
Untuk dukungan model, lihat Referensi alat.
Kapan menggunakan alat editor teks
Beberapa contoh kapan menggunakan alat editor teks adalah:
- Debugging kode: Minta Haijun mengidentifikasi dan memperbaiki bug dalam kode Anda, mulai dari kesalahan sintaks hingga masalah logika.
- Refactoring kode: Biarkan Haijun meningkatkan struktur, keterbacaan, dan performa kode Anda melalui pengeditan yang terarah.
- Pembuatan dokumentasi: Minta Haijun menambahkan docstring, komentar, atau file README ke basis kode Anda.
- Pembuatan tes: Minta Haijun membuat unit test untuk kode Anda berdasarkan analisisnya terhadap implementasi.
Menggunakan alat editor teks
Sediakan alat editor teks (bernama str_replace_based_edit_tool) kepada Haijun menggunakan Messages API.
Anda dapat secara opsional menentukan parameter max_characters untuk mengontrol pemotongan saat melihat file berukuran besar.
Note:
max_charactershanya kompatibel dengantext_editor_20250728dan versi alat editor teks yang lebih baru.
curl https://haijun.my.id/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $JUGLOW_API_KEY" \
-H "juglow-version: 2023-06-01" \
-d '{
"model": "haijun-opus-5-5",
"max_tokens": 1024,
"tools": [
{
"type": "text_editor_20250728",
"name": "str_replace_based_edit_tool",
"max_characters": 10000
}
],
"messages": [
{
"role": "user",
"content": "There'\''s a syntax error in my primes.py file. Can you help me fix it?"
}
]
}' ant messages create \
--model haijun-opus-5-5 \
--max-tokens 1024 \
--tool '{type: text_editor_20250728, name: str_replace_based_edit_tool, max_characters: 10000}' \
--message '{role: user, content: There is a syntax error in my primes.py file. Can you help me fix it?}' client = juglow.Juglow()
response = client.messages.create(
model="haijun-opus-5-5",
max_tokens=1024,
tools=[
{
"type": "text_editor_20250728",
"name": "str_replace_based_edit_tool",
"max_characters": 10000,
}
],
messages=[
{
"role": "user",
"content": "There's a syntax error in my primes.py file. Can you help me fix it?",
}
],
)
print(response) const juglow = new Juglow();
const response = await juglow.messages.create({
model: "haijun-opus-5-5",
max_tokens: 1024,
tools: [
{
type: "text_editor_20250728",
name: "str_replace_based_edit_tool",
max_characters: 10000
}
],
messages: [
{
role: "user",
content: "There's a syntax error in my primes.py file. Can you help me fix it?"
}
]
});
console.log(response); var client = new JuglowClient();
var response = await client.Messages.Create(
new()
{
Model = Model.HaijunOpus5_5,
MaxTokens = 1024,
Tools = [new ToolTextEditor20250728 { MaxCharacters = 10000 }],
Messages =
[
new()
{
Role = Role.User,
Content = "There's a syntax error in my primes.py file. Can you help me fix it?",
},
],
}
);
Console.WriteLine(response); client := juglow.NewClient()
response, err := client.Messages.New(context.TODO(), juglow.MessageNewParams{
Model: juglow.ModelHaijunOpus5_5,
MaxTokens: 1024,
Tools: []juglow.ToolUnionParam{
{OfTextEditor20250728: &juglow.ToolTextEditor20250728Param{
MaxCharacters: juglow.Int(10000),
}},
},
Messages: []juglow.MessageParam{
juglow.NewUserMessage(juglow.NewTextBlock("There's a syntax error in my primes.py file. Can you help me fix it?")),
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(response) import com.juglow.models.messages.ToolTextEditor20250728;
// ...
void main() {
JuglowClient client = JuglowOkHttpClient.fromEnv();
ToolTextEditor20250728 editorTool =
ToolTextEditor20250728.builder()
.maxCharacters(10000L)
.build();
MessageCreateParams params = MessageCreateParams.builder()
.model(Model.HAIJUN_OPUS_5_5)
.maxTokens(1024)
.addTool(editorTool)
.addUserMessage("There's a syntax error in my primes.py file. Can you help me fix it?")
.build();
Message message = client.messages().create(params);
IO.println(message);
} $client = new Client();
$response = $client->messages->create(
model: 'haijun-opus-5-5',
maxTokens: 1024,
tools: [ToolTextEditor20250728::with(maxCharacters: 10000)],
messages: [
[
'role' => 'user',
'content' => "There's a syntax error in my primes.py file. Can you help me fix it?",
],
],
);
echo $response; client = Juglow::Client.new
response = client.messages.create(
model: "haijun-opus-5-5",
max_tokens: 1024,
tools: [
{
type: "text_editor_20250728",
name: "str_replace_based_edit_tool",
max_characters: 10000
}
],
messages: [
{
role: "user",
content: "There's a syntax error in my primes.py file. Can you help me fix it?"
}
]
)
puts responseGunakan alat editor teks dengan cara berikut:
- Berikan Haijun alat editor teks dan prompt pengguna
- Sertakan alat editor teks dalam permintaan API Anda
- Berikan prompt pengguna yang mungkin memerlukan pemeriksaan atau modifikasi file, seperti "Bisakah Anda memperbaiki kesalahan sintaks dalam kode saya?"
- Haijun menggunakan alat untuk memeriksa file atau direktori
- Haijun menilai apa yang perlu dilihatnya dan menggunakan perintah
viewuntuk memeriksa isi file atau mendaftar isi direktori
- Respons API akan berisi blok konten
tool_usedengan perintahview
- Jalankan perintah view dan kembalikan hasilnya
- Ekstrak path file atau direktori dari permintaan penggunaan alat Haijun
- Baca isi file atau daftar isi direktori
- Jika parameter
max_charactersditentukan dalam konfigurasi alat, potong isi file hingga panjang tersebut
- Kembalikan hasilnya ke Haijun dengan melanjutkan percakapan menggunakan pesan
userbaru yang berisi blok kontentool_result
- Haijun menggunakan alat untuk memodifikasi file
- Setelah memeriksa file atau direktori, Haijun dapat menggunakan perintah seperti
str_replaceuntuk membuat perubahan atauinsertuntuk menambahkan teks pada nomor baris tertentu.
- Jika Haijun menggunakan perintah
str_replace, Haijun menyusun permintaan penggunaan alat yang diformat dengan benar berisi teks lama dan teks baru sebagai penggantinya
- Jalankan pengeditan dan kembalikan hasilnya
- Ekstrak path file, teks lama, dan teks baru dari permintaan penggunaan alat Haijun
- Lakukan penggantian teks dalam file
- Kembalikan hasilnya ke Haijun
- Haijun memberikan analisis dan penjelasannya
- Setelah memeriksa dan mungkin mengedit file, Haijun memberikan penjelasan lengkap tentang apa yang ditemukannya dan perubahan apa yang dibuatnya
Perintah alat editor teks
Alat editor teks mendukung beberapa perintah untuk melihat dan memodifikasi file:
view
Perintah view memungkinkan Haijun memeriksa isi file atau mendaftar isi direktori. Perintah ini dapat membaca seluruh file atau rentang baris tertentu.
Parameter:
command: Harus "view"
path: Path ke file atau direktori yang akan dilihat
view_range(opsional): Array berisi dua bilangan bulat yang menentukan nomor baris awal dan akhir yang akan dilihat. Nomor baris dimulai dari 1, dan -1 untuk baris akhir berarti membaca hingga akhir file. Parameter ini hanya berlaku saat melihat file, bukan direktori.
Contoh perintah view
Contoh untuk melihat file:
{
"type": "tool_use",
"id": "toolu_01A09q90qw90lq917835lq9",
"name": "str_replace_based_edit_tool",
"input": {
"command": "view",
"path": "primes.py"
}
}Contoh untuk melihat direktori:
{
"type": "tool_use",
"id": "toolu_02B19r91rw91mr917835mr9",
"name": "str_replace_based_edit_tool",
"input": {
"command": "view",
"path": "src/"
}
}str\_replace
Perintah str_replace memungkinkan Haijun mengganti string tertentu dalam file dengan string baru. Perintah ini digunakan untuk melakukan pengeditan yang presisi.
Parameter:
command: Harus "str\_replace"
path: Path ke file yang akan dimodifikasi
old_str: Teks yang akan diganti (harus cocok persis, termasuk whitespace dan indentasi)
new_str: Teks baru yang akan disisipkan menggantikan teks lama
Contoh perintah str_replace
{
"type": "tool_use",
"id": "toolu_01A09q90qw90lq917835lq9",
"name": "str_replace_based_edit_tool",
"input": {
"command": "str_replace",
"path": "primes.py",
"old_str": "for num in range(2, limit + 1)",
"new_str": "for num in range(2, limit + 1):"
}
}create
Perintah create memungkinkan Haijun membuat file baru dengan konten yang ditentukan.
Parameter:
command: Harus "create"
path: Path tempat file baru akan dibuat
file_text: Konten yang akan ditulis ke file baru
Contoh perintah create
{
"type": "tool_use",
"id": "toolu_01A09q90qw90lq917835lq9",
"name": "str_replace_based_edit_tool",
"input": {
"command": "create",
"path": "test_primes.py",
"file_text": "import unittest\nimport primes\n\nclass TestPrimes(unittest.TestCase):\n def test_is_prime(self):\n self.assertTrue(primes.is_prime(2))\n self.assertTrue(primes.is_prime(3))\n self.assertFalse(primes.is_prime(4))\n\nif __name__ == '__main__':\n unittest.main()"
}
}insert
Perintah insert memungkinkan Haijun menyisipkan teks pada lokasi tertentu dalam file.
Parameter:
command: Harus "insert"
path: Path ke file yang akan dimodifikasi
insert_line: Nomor baris yang setelahnya teks akan disisipkan (0 untuk awal file)
insert_text: Teks yang akan disisipkan
Contoh perintah insert
{
"type": "tool_use",
"id": "toolu_01A09q90qw90lq917835lq9",
"name": "str_replace_based_edit_tool",
"input": {
"command": "insert",
"path": "primes.py",
"insert_line": 0,
"insert_text": "\"\"\"Module for working with prime numbers.\n\nThis module provides functions to check if a number is prime\nand to generate a list of prime numbers up to a given limit.\n\"\"\"\n"
}
}Contoh: Memperbaiki kesalahan sintaks dengan alat editor teks
Contoh ini menunjukkan bagaimana Haijun menggunakan alat editor teks untuk memperbaiki kesalahan sintaks dalam file Python.
Pertama, aplikasi Anda memberikan Haijun alat editor teks dan prompt untuk memperbaiki kesalahan sintaks:
curl https://haijun.my.id/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $JUGLOW_API_KEY" \
-H "juglow-version: 2023-06-01" \
-d '{
"model": "haijun-opus-5-5",
"max_tokens": 1024,
"tools": [
{
"type": "text_editor_20250728",
"name": "str_replace_based_edit_tool"
}
],
"messages": [
{
"role": "user",
"content": "There'\''s a syntax error in my primes.py file. Can you help me fix it?"
}
]
}' ant messages create \
--model haijun-opus-5-5 \
--max-tokens 1024 \
--tool '{type: text_editor_20250728, name: str_replace_based_edit_tool}' \
--message '{role: user, content: There is a syntax error in my primes.py file. Can you help me fix it?}' client = juglow.Juglow()
response = client.messages.create(
model="haijun-opus-5-5",
max_tokens=1024,
tools=[{"type": "text_editor_20250728", "name": "str_replace_based_edit_tool"}],
messages=[
{
"role": "user",
"content": "There's a syntax error in my primes.py file. Can you help me fix it?",
}
],
)
print(response) const juglow = new Juglow();
const response = await juglow.messages.create({
model: "haijun-opus-5-5",
max_tokens: 1024,
tools: [
{
type: "text_editor_20250728",
name: "str_replace_based_edit_tool"
}
],
messages: [
{
role: "user",
content: "There's a syntax error in my primes.py file. Can you help me fix it?"
}
]
});
console.log(response); var client = new JuglowClient();
var response = await client.Messages.Create(
new()
{
Model = Model.HaijunOpus5_5,
MaxTokens = 1024,
Tools = [new ToolTextEditor20250728()],
Messages =
[
new()
{
Role = Role.User,
Content = "There's a syntax error in my primes.py file. Can you help me fix it?",
},
],
}
);
Console.WriteLine(response); client := juglow.NewClient()
response, err := client.Messages.New(context.TODO(), juglow.MessageNewParams{
Model: juglow.ModelHaijunOpus5_5,
MaxTokens: 1024,
Tools: []juglow.ToolUnionParam{
{OfTextEditor20250728: &juglow.ToolTextEditor20250728Param{}},
},
Messages: []juglow.MessageParam{
juglow.NewUserMessage(juglow.NewTextBlock("There's a syntax error in my primes.py file. Can you help me fix it?")),
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(response) import com.juglow.models.messages.ToolTextEditor20250728;
// ...
void main() {
JuglowClient client = JuglowOkHttpClient.fromEnv();
ToolTextEditor20250728 editorTool =
ToolTextEditor20250728.builder().build();
MessageCreateParams params = MessageCreateParams.builder()
.model(Model.HAIJUN_OPUS_5_5)
.maxTokens(1024)
.addTool(editorTool)
.addUserMessage("There's a syntax error in my primes.py file. Can you help me fix it?")
.build();
Message message = client.messages().create(params);
IO.println(message);
} $client = new Client();
$response = $client->messages->create(
model: 'haijun-opus-5-5',
maxTokens: 1024,
tools: [new ToolTextEditor20250728()],
messages: [
[
'role' => 'user',
'content' => "There's a syntax error in my primes.py file. Can you help me fix it?",
],
],
);
echo $response; client = Juglow::Client.new
response = client.messages.create(
model: "haijun-opus-5-5",
max_tokens: 1024,
tools: [{type: "text_editor_20250728", name: "str_replace_based_edit_tool"}],
messages: [
{
role: "user",
content: "There's a syntax error in my primes.py file. Can you help me fix it?"
}
]
)
puts responseHaijun menggunakan alat editor teks terlebih dahulu untuk melihat file:
{
"id": "msg_01XAbCDeFgHiJkLmNoPQrStU",
"model": "haijun-opus-5-5",
"stop_reason": "tool_use",
"role": "assistant",
"content": [
{
"type": "text",
"text": "I'll help you fix the syntax error in your primes.py file. First, let me take a look at the file to identify the issue."
},
{
"type": "tool_use",
"id": "toolu_01AbCdEfGhIjKlMnOpQrStU",
"name": "str_replace_based_edit_tool",
"input": {
"command": "view",
"path": "primes.py"
}
}
]
}Aplikasi Anda kemudian harus membaca file dan mengembalikan isinya ke Haijun:
curl https://haijun.my.id/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $JUGLOW_API_KEY" \
-H "juglow-version: 2023-06-01" \
-d '{
"model": "haijun-opus-5-5",
"max_tokens": 1024,
"tools": [
{
"type": "text_editor_20250728",
"name": "str_replace_based_edit_tool"
}
],
"messages": [
{
"role": "user",
"content": "There'\''s a syntax error in my primes.py file. Can you help me fix it?"
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "I'\''ll help you fix the syntax error in your primes.py file. First, let me take a look at the file to identify the issue."
},
{
"type": "tool_use",
"id": "toolu_01AbCdEfGhIjKlMnOpQrStU",
"name": "str_replace_based_edit_tool",
"input": {
"command": "view",
"path": "primes.py"
}
}
]
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01AbCdEfGhIjKlMnOpQrStU",
"content": "1: def is_prime(n):\n2: \"\"\"Check if a number is prime.\"\"\"\n3: if n <= 1:\n4: return False\n5: if n <= 3:\n6: return True\n7: if n % 2 == 0 or n % 3 == 0:\n8: return False\n9: i = 5\n10: while i * i <= n:\n11: if n % i == 0 or n % (i + 2) == 0:\n12: return False\n13: i += 6\n14: return True\n15: \n16: def get_primes(limit):\n17: \"\"\"Generate a list of prime numbers up to the given limit.\"\"\"\n18: primes = []\n19: for num in range(2, limit + 1)\n20: if is_prime(num):\n21: primes.append(num)\n22: return primes\n23: \n24: def main():\n25: \"\"\"Main function to demonstrate prime number generation.\"\"\"\n26: limit = 100\n27: prime_list = get_primes(limit)\n28: print(f\"Prime numbers up to {limit}:\")\n29: print(prime_list)\n30: print(f\"Found {len(prime_list)} prime numbers.\")\n31: \n32: if __name__ == \"__main__\":\n33: main()"
}
]
}
]
}' ant messages create <<'YAML'
model: haijun-opus-5-5
max_tokens: 1024
tools:
- type: text_editor_20250728
name: str_replace_based_edit_tool
messages:
- role: user
content: There's a syntax error in my primes.py file. Can you help me fix it?
- role: assistant
content:
- type: text
text: >-
I'll help you fix the syntax error in your primes.py file. First,
let me take a look at the file to identify the issue.
- type: tool_use
id: toolu_01AbCdEfGhIjKlMnOpQrStU
name: str_replace_based_edit_tool
input:
command: view
path: primes.py
- role: user
content:
- type: tool_result
tool_use_id: toolu_01AbCdEfGhIjKlMnOpQrStU
content: |-
1: def is_prime(n):
2: """Check if a number is prime."""
3: if n <= 1:
4: return False
5: if n <= 3:
6: return True
7: if n % 2 == 0 or n % 3 == 0:
8: return False
9: i = 5
10: while i * i <= n:
11: if n % i == 0 or n % (i + 2) == 0:
12: return False
13: i += 6
14: return True
15:
16: def get_primes(limit):
17: """Generate a list of prime numbers up to the given limit."""
18: primes = []
19: for num in range(2, limit + 1)
20: if is_prime(num):
21: primes.append(num)
22: return primes
23:
24: def main():
25: """Main function to demonstrate prime number generation."""
26: limit = 100
27: prime_list = get_primes(limit)
28: print(f"Prime numbers up to {limit}:")
29: print(prime_list)
30: print(f"Found {len(prime_list)} prime numbers.")
31:
32: if __name__ == "__main__":
33: main()
YAML response = client.messages.create(
model="haijun-opus-5-5",
max_tokens=1024,
tools=[{"type": "text_editor_20250728", "name": "str_replace_based_edit_tool"}],
messages=[
{
"role": "user",
"content": "There's a syntax error in my primes.py file. Can you help me fix it?",
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "I'll help you fix the syntax error in your primes.py file. First, let me take a look at the file to identify the issue.",
},
{
"type": "tool_use",
"id": "toolu_01AbCdEfGhIjKlMnOpQrStU",
"name": "str_replace_based_edit_tool",
"input": {"command": "view", "path": "primes.py"},
},
],
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01AbCdEfGhIjKlMnOpQrStU",
"content": '1: def is_prime(n):\n2: """Check if a number is prime."""\n3: if n <= 1:\n4: return False\n5: if n <= 3:\n6: return True\n7: if n % 2 == 0 or n % 3 == 0:\n8: return False\n9: i = 5\n10: while i * i <= n:\n11: if n % i == 0 or n % (i + 2) == 0:\n12: return False\n13: i += 6\n14: return True\n15: \n16: def get_primes(limit):\n17: """Generate a list of prime numbers up to the given limit."""\n18: primes = []\n19: for num in range(2, limit + 1)\n20: if is_prime(num):\n21: primes.append(num)\n22: return primes\n23: \n24: def main():\n25: """Main function to demonstrate prime number generation."""\n26: limit = 100\n27: prime_list = get_primes(limit)\n28: print(f"Prime numbers up to {limit}:")\n29: print(prime_list)\n30: print(f"Found {len(prime_list)} prime numbers.")\n31: \n32: if __name__ == "__main__":\n33: main()',
}
],
},
],
)
print(response) const juglow = new Juglow();
const response = await juglow.messages.create({
model: "haijun-opus-5-5",
max_tokens: 1024,
tools: [
{
type: "text_editor_20250728",
name: "str_replace_based_edit_tool"
}
],
messages: [
{
role: "user",
content: "There's a syntax error in my primes.py file. Can you help me fix it?"
},
{
role: "assistant",
content: [
{
type: "text",
text: "I'll help you fix the syntax error in your primes.py file. First, let me take a look at the file to identify the issue."
},
{
type: "tool_use",
id: "toolu_01AbCdEfGhIjKlMnOpQrStU",
name: "str_replace_based_edit_tool",
input: {
command: "view",
path: "primes.py"
}
}
]
},
{
role: "user",
content: [
{
type: "tool_result",
tool_use_id: "toolu_01AbCdEfGhIjKlMnOpQrStU",
content:
'1: def is_prime(n):\n2: """Check if a number is prime."""\n3: if n <= 1:\n4: return False\n5: if n <= 3:\n6: return True\n7: if n % 2 == 0 or n % 3 == 0:\n8: return False\n9: i = 5\n10: while i * i <= n:\n11: if n % i == 0 or n % (i + 2) == 0:\n12: return False\n13: i += 6\n14: return True\n15: \n16: def get_primes(limit):\n17: """Generate a list of prime numbers up to the given limit."""\n18: primes = []\n19: for num in range(2, limit + 1)\n20: if is_prime(num):\n21: primes.append(num)\n22: return primes\n23: \n24: def main():\n25: """Main function to demonstrate prime number generation."""\n26: limit = 100\n27: prime_list = get_primes(limit)\n28: print(f"Prime numbers up to {limit}:")\n29: print(prime_list)\n30: print(f"Found {len(prime_list)} prime numbers.")\n31: \n32: if __name__ == "__main__":\n33: main()'
}
]
}
]
});
console.log(response); var client = new JuglowClient();
var response = await client.Messages.Create(
new()
{
Model = Model.HaijunOpus5_5,
MaxTokens = 1024,
Tools = [new ToolTextEditor20250728()],
Messages =
[
new()
{
Role = Role.User,
Content = "There's a syntax error in my primes.py file. Can you help me fix it?",
},
new()
{
Role = Role.Assistant,
Content = new MessageParamContent(new List<ContentBlockParam>
{
new ContentBlockParam(new TextBlockParam()
{
Text = "I'll help you fix the syntax error in your primes.py file. First, let me take a look at the file to identify the issue.",
}),
new ContentBlockParam(new ToolUseBlockParam()
{
ID = "toolu_01AbCdEfGhIjKlMnOpQrStU",
Name = "str_replace_based_edit_tool",
Input = new Dictionary<string, JsonElement>
{
["command"] = JsonSerializer.SerializeToElement("view"),
["path"] = JsonSerializer.SerializeToElement("primes.py"),
},
}),
}),
},
new()
{
Role = Role.User,
Content = new MessageParamContent(new List<ContentBlockParam>
{
new ContentBlockParam(new ToolResultBlockParam()
{
ToolUseID = "toolu_01AbCdEfGhIjKlMnOpQrStU",
Content = "1: def is_prime(n):\n2: \"\"\"Check if a number is prime.\"\"\"\n3: if n <= 1:\n4: return False\n5: if n <= 3:\n6: return True\n7: if n % 2 == 0 or n % 3 == 0:\n8: return False\n9: i = 5\n10: while i * i <= n:\n11: if n % i == 0 or n % (i + 2) == 0:\n12: return False\n13: i += 6\n14: return True\n15: \n16: def get_primes(limit):\n17: \"\"\"Generate a list of prime numbers up to the given limit.\"\"\"\n18: primes = []\n19: for num in range(2, limit + 1)\n20: if is_prime(num):\n21: primes.append(num)\n22: return primes\n23: \n24: def main():\n25: \"\"\"Main function to demonstrate prime number generation.\"\"\"\n26: limit = 100\n27: prime_list = get_primes(limit)\n28: print(f\"Prime numbers up to {limit}:\")\n29: print(prime_list)\n30: print(f\"Found {len(prime_list)} prime numbers.\")\n31: \n32: if __name__ == \"__main__\":\n33: main()",
}),
}),
},
],
}
);
Console.WriteLine(response); client := juglow.NewClient()
response, err := client.Messages.New(context.TODO(), juglow.MessageNewParams{
Model: juglow.ModelHaijunOpus5_5,
MaxTokens: 1024,
Tools: []juglow.ToolUnionParam{
{OfTextEditor20250728: &juglow.ToolTextEditor20250728Param{}},
},
Messages: []juglow.MessageParam{
juglow.NewUserMessage(juglow.NewTextBlock("There's a syntax error in my primes.py file. Can you help me fix it?")),
juglow.NewAssistantMessage(
juglow.NewTextBlock("I'll help you fix the syntax error in your primes.py file. First, let me take a look at the file to identify the issue."),
juglow.NewToolUseBlock(
"toolu_01AbCdEfGhIjKlMnOpQrStU",
map[string]any{"command": "view", "path": "primes.py"},
"str_replace_based_edit_tool",
),
),
juglow.NewUserMessage(
juglow.NewToolResultBlock(
"toolu_01AbCdEfGhIjKlMnOpQrStU",
"1: def is_prime(n):\n2: \"\"\"Check if a number is prime.\"\"\"\n3: if n <= 1:\n4: return False\n5: if n <= 3:\n6: return True\n7: if n % 2 == 0 or n % 3 == 0:\n8: return False\n9: i = 5\n10: while i * i <= n:\n11: if n % i == 0 or n % (i + 2) == 0:\n12: return False\n13: i += 6\n14: return True\n15: \n16: def get_primes(limit):\n17: \"\"\"Generate a list of prime numbers up to the given limit.\"\"\"\n18: primes = []\n19: for num in range(2, limit + 1)\n20: if is_prime(num):\n21: primes.append(num)\n22: return primes\n23: \n24: def main():\n25: \"\"\"Main function to demonstrate prime number generation.\"\"\"\n26: limit = 100\n27: prime_list = get_primes(limit)\n28: print(f\"Prime numbers up to {limit}:\")\n29: print(prime_list)\n30: print(f\"Found {len(prime_list)} prime numbers.\")\n31: \n32: if __name__ == \"__main__\":\n33: main()",
false,
),
),
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(response) JuglowClient client = JuglowOkHttpClient.fromEnv();
MessageCreateParams params = MessageCreateParams.builder()
.model(Model.HAIJUN_OPUS_5_5)
.maxTokens(1024)
.addTool(ToolTextEditor20250728.builder().build())
.addUserMessage("There's a syntax error in my primes.py file. Can you help me fix it?")
.addAssistantMessageOfBlockParams(
List.of(
ContentBlockParam.ofText(
TextBlockParam.builder()
.text("I'll help you fix the syntax error in your primes.py file. First, let me take a look at the file to identify the issue.")
.build()
),
ContentBlockParam.ofToolUse(
ToolUseBlockParam.builder()
.id("toolu_01AbCdEfGhIjKlMnOpQrStU")
.name("str_replace_based_edit_tool")
.input(
ToolUseBlockParam.Input.builder()
.putAdditionalProperty("command", JsonValue.from("view"))
.putAdditionalProperty("path", JsonValue.from("primes.py"))
.build()
)
.build()
)
)
)
.addUserMessageOfBlockParams(
List.of(
ContentBlockParam.ofToolResult(
ToolResultBlockParam.builder()
.toolUseId("toolu_01AbCdEfGhIjKlMnOpQrStU")
.content("1: def is_prime(n):\n2: \"\"\"Check if a number is prime.\"\"\"\n3: if n <= 1:\n4: return False\n5: if n <= 3:\n6: return True\n7: if n % 2 == 0 or n % 3 == 0:\n8: return False\n9: i = 5\n10: while i * i <= n:\n11: if n % i == 0 or n % (i + 2) == 0:\n12: return False\n13: i += 6\n14: return True\n15: \n16: def get_primes(limit):\n17: \"\"\"Generate a list of prime numbers up to the given limit.\"\"\"\n18: primes = []\n19: for num in range(2, limit + 1)\n20: if is_prime(num):\n21: primes.append(num)\n22: return primes\n23: \n24: def main():\n25: \"\"\"Main function to demonstrate prime number generation.\"\"\"\n26: limit = 100\n27: prime_list = get_primes(limit)\n28: print(f\"Prime numbers up to {limit}:\")\n29: print(prime_list)\n30: print(f\"Found {len(prime_list)} prime numbers.\")\n31: \n32: if __name__ == \"__main__\":\n33: main()")
.build()
)
)
)
.build();
Message message = client.messages().create(params);
System.out.println(message); $client = new Client();
$response = $client->messages->create(
model: 'haijun-opus-5-5',
maxTokens: 1024,
tools: [new ToolTextEditor20250728()],
messages: [
[
'role' => 'user',
'content' => "There's a syntax error in my primes.py file. Can you help me fix it?",
],
[
'role' => 'assistant',
'content' => [
[
'type' => 'text',
'text' => "I'll help you fix the syntax error in your primes.py file. First, let me take a look at the file to identify the issue.",
],
[
'type' => 'tool_use',
'id' => 'toolu_01AbCdEfGhIjKlMnOpQrStU',
'name' => 'str_replace_based_edit_tool',
'input' => ['command' => 'view', 'path' => 'primes.py'],
],
],
],
[
'role' => 'user',
'content' => [
[
'type' => 'tool_result',
'tool_use_id' => 'toolu_01AbCdEfGhIjKlMnOpQrStU',
'content' => "1: def is_prime(n):\n2: \"\"\"Check if a number is prime.\"\"\"\n3: if n <= 1:\n4: return False\n5: if n <= 3:\n6: return True\n7: if n % 2 == 0 or n % 3 == 0:\n8: return False\n9: i = 5\n10: while i * i <= n:\n11: if n % i == 0 or n % (i + 2) == 0:\n12: return False\n13: i += 6\n14: return True\n15: \n16: def get_primes(limit):\n17: \"\"\"Generate a list of prime numbers up to the given limit.\"\"\"\n18: primes = []\n19: for num in range(2, limit + 1)\n20: if is_prime(num):\n21: primes.append(num)\n22: return primes\n23: \n24: def main():\n25: \"\"\"Main function to demonstrate prime number generation.\"\"\"\n26: limit = 100\n27: prime_list = get_primes(limit)\n28: print(f\"Prime numbers up to {limit}:\")\n29: print(prime_list)\n30: print(f\"Found {len(prime_list)} prime numbers.\")\n31: \n32: if __name__ == \"__main__\":\n33: main()",
],
],
],
],
);
echo $response; client = Juglow::Client.new
response = client.messages.create(
model: "haijun-opus-5-5",
max_tokens: 1024,
tools: [{type: "text_editor_20250728", name: "str_replace_based_edit_tool"}],
messages: [
{
role: "user",
content: "There's a syntax error in my primes.py file. Can you help me fix it?"
},
{
role: "assistant",
content: [
{
type: "text",
text: "I'll help you fix the syntax error in your primes.py file. First, let me take a look at the file to identify the issue."
},
{
type: "tool_use",
id: "toolu_01AbCdEfGhIjKlMnOpQrStU",
name: "str_replace_based_edit_tool",
input: {command: "view", path: "primes.py"}
}
]
},
{
role: "user",
content: [
{
type: "tool_result",
tool_use_id: "toolu_01AbCdEfGhIjKlMnOpQrStU",
content: "1: def is_prime(n):\n2: \"\"\"Check if a number is prime.\"\"\"\n3: if n <= 1:\n4: return False\n5: if n <= 3:\n6: return True\n7: if n % 2 == 0 or n % 3 == 0:\n8: return False\n9: i = 5\n10: while i * i <= n:\n11: if n % i == 0 or n % (i + 2) == 0:\n12: return False\n13: i += 6\n14: return True\n15: \n16: def get_primes(limit):\n17: \"\"\"Generate a list of prime numbers up to the given limit.\"\"\"\n18: primes = []\n19: for num in range(2, limit + 1)\n20: if is_prime(num):\n21: primes.append(num)\n22: return primes\n23: \n24: def main():\n25: \"\"\"Main function to demonstrate prime number generation.\"\"\"\n26: limit = 100\n27: prime_list = get_primes(limit)\n28: print(f\"Prime numbers up to {limit}:\")\n29: print(prime_list)\n30: print(f\"Found {len(prime_list)} prime numbers.\")\n31: \n32: if __name__ == \"__main__\":\n33: main()"
}
]
}
]
)
puts responseTip: Nomor baris Pada contoh sebelumnya, hasil alat
viewmenyertakan isi file dengan nomor baris yang ditambahkan di awal setiap baris (misalnya, "1: def is\_prime(n):"). Nomor baris tidak wajib, tetapi sangat penting agar parameterview_rangedapat digunakan dengan sukses untuk memeriksa bagian tertentu dari file dan parameterinsert_lineuntuk menambahkan konten pada lokasi yang tepat.
Haijun mengidentifikasi kesalahan sintaks dan menggunakan perintah str_replace untuk memperbaikinya:
{
"id": "msg_01VwXyZAbCdEfGhIjKlMnO",
"model": "haijun-opus-5-5",
"stop_reason": "tool_use",
"role": "assistant",
"content": [
{
"type": "text",
"text": "I found the syntax error in your primes.py file. In the `get_primes` function, there is a missing colon (:) at the end of the for loop line. Let me fix that for you."
},
{
"type": "tool_use",
"id": "toolu_01PqRsTuVwXyZAbCdEfGh",
"name": "str_replace_based_edit_tool",
"input": {
"command": "str_replace",
"path": "primes.py",
"old_str": " for num in range(2, limit + 1)",
"new_str": " for num in range(2, limit + 1):"
}
}
]
}Aplikasi Anda kemudian harus melakukan pengeditan dan mengembalikan hasilnya:
curl https://haijun.my.id/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $JUGLOW_API_KEY" \
-H "juglow-version: 2023-06-01" \
-d '{
"model": "haijun-opus-5-5",
"max_tokens": 1024,
"tools": [
{
"type": "text_editor_20250728",
"name": "str_replace_based_edit_tool"
}
],
"messages": [
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "I found the syntax error in your primes.py file. In the `get_primes` function, there is a missing colon (:) at the end of the for loop line. Let me fix that for you."
},
{
"type": "tool_use",
"id": "toolu_01PqRsTuVwXyZAbCdEfGh",
"name": "str_replace_based_edit_tool",
"input": {
"command": "str_replace",
"path": "primes.py",
"old_str": " for num in range(2, limit + 1)",
"new_str": " for num in range(2, limit + 1):"
}
}
]
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01PqRsTuVwXyZAbCdEfGh",
"content": "Successfully replaced text at exactly one location."
}
]
}
]
}' ant messages create <<'YAML'
model: haijun-opus-5-5
max_tokens: 1024
tools:
- type: text_editor_20250728
name: str_replace_based_edit_tool
messages:
# Pesan-pesan sebelumnya...
- role: assistant
content:
- type: text
text: >-
I found the syntax error in your primes.py file. In the `get_primes`
function, there is a missing colon (:) at the end of the for loop
line. Let me fix that for you.
- type: tool_use
id: toolu_01PqRsTuVwXyZAbCdEfGh
name: str_replace_based_edit_tool
input:
command: str_replace
path: primes.py
old_str: " for num in range(2, limit + 1)"
new_str: " for num in range(2, limit + 1):"
- role: user
content:
- type: tool_result
tool_use_id: toolu_01PqRsTuVwXyZAbCdEfGh
content: Successfully replaced text at exactly one location.
YAML response = client.messages.create(
model="haijun-opus-5-5",
max_tokens=1024,
tools=[{"type": "text_editor_20250728", "name": "str_replace_based_edit_tool"}],
messages=[
# Pesan-pesan sebelumnya...
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "I found the syntax error in your primes.py file. In the `get_primes` function, there is a missing colon (:) at the end of the for loop line. Let me fix that for you.",
},
{
"type": "tool_use",
"id": "toolu_01PqRsTuVwXyZAbCdEfGh",
"name": "str_replace_based_edit_tool",
"input": {
"command": "str_replace",
"path": "primes.py",
"old_str": " for num in range(2, limit + 1)",
"new_str": " for num in range(2, limit + 1):",
},
},
],
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01PqRsTuVwXyZAbCdEfGh",
"content": "Successfully replaced text at exactly one location.",
}
],
},
],
)
print(response) const response = await client.messages.create({
model: "haijun-opus-5-5",
max_tokens: 1024,
tools: [
{
type: "text_editor_20250728",
name: "str_replace_based_edit_tool"
}
],
messages: [
// Pesan-pesan sebelumnya...
{
role: "assistant",
content: [
{
type: "text",
text: "I found the syntax error in your primes.py file. In the `get_primes` function, there is a missing colon (:) at the end of the for loop line. Let me fix that for you."
},
{
type: "tool_use",
id: "toolu_01PqRsTuVwXyZAbCdEfGh",
name: "str_replace_based_edit_tool",
input: {
command: "str_replace",
path: "primes.py",
old_str: " for num in range(2, limit + 1)",
new_str: " for num in range(2, limit + 1):"
}
}
]
},
{
role: "user",
content: [
{
type: "tool_result",
tool_use_id: "toolu_01PqRsTuVwXyZAbCdEfGh",
content: "Successfully replaced text at exactly one location."
}
]
}
]
});
console.log(response); var client = new JuglowClient();
var response = await client.Messages.Create(
new()
{
Model = Model.HaijunOpus5_5,
MaxTokens = 1024,
Tools = [new ToolTextEditor20250728()],
Messages =
[
// Pesan-pesan sebelumnya...
new()
{
Role = Role.Assistant,
Content = new MessageParamContent(new List<ContentBlockParam>
{
new ContentBlockParam(new TextBlockParam()
{
Text = "I found the syntax error in your primes.py file. In the `get_primes` function, there is a missing colon (:) at the end of the for loop line. Let me fix that for you.",
}),
new ContentBlockParam(new ToolUseBlockParam()
{
ID = "toolu_01PqRsTuVwXyZAbCdEfGh",
Name = "str_replace_based_edit_tool",
Input = new Dictionary<string, JsonElement>
{
["command"] = JsonSerializer.SerializeToElement("str_replace"),
["path"] = JsonSerializer.SerializeToElement("primes.py"),
["old_str"] = JsonSerializer.SerializeToElement(" for num in range(2, limit + 1)"),
["new_str"] = JsonSerializer.SerializeToElement(" for num in range(2, limit + 1):"),
},
}),
}),
},
new()
{
Role = Role.User,
Content = new MessageParamContent(new List<ContentBlockParam>
{
new ContentBlockParam(new ToolResultBlockParam()
{
ToolUseID = "toolu_01PqRsTuVwXyZAbCdEfGh",
Content = "Successfully replaced text at exactly one location.",
}),
}),
},
],
}
);
Console.WriteLine(response); client := juglow.NewClient()
response, err := client.Messages.New(context.TODO(), juglow.MessageNewParams{
Model: juglow.ModelHaijunOpus5_5,
MaxTokens: 1024,
Tools: []juglow.ToolUnionParam{
{OfTextEditor20250728: &juglow.ToolTextEditor20250728Param{}},
},
Messages: []juglow.MessageParam{
// Pesan-pesan sebelumnya...
juglow.NewAssistantMessage(
juglow.NewTextBlock("I found the syntax error in your primes.py file. In the `get_primes` function, there is a missing colon (:) at the end of the for loop line. Let me fix that for you."),
juglow.NewToolUseBlock(
"toolu_01PqRsTuVwXyZAbCdEfGh",
map[string]any{
"command": "str_replace",
"path": "primes.py",
"old_str": " for num in range(2, limit + 1)",
"new_str": " for num in range(2, limit + 1):",
},
"str_replace_based_edit_tool",
),
),
juglow.NewUserMessage(
juglow.NewToolResultBlock(
"toolu_01PqRsTuVwXyZAbCdEfGh",
"Successfully replaced text at exactly one location.",
false,
),
),
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(response) JuglowClient client = JuglowOkHttpClient.fromEnv();
MessageCreateParams params = MessageCreateParams.builder()
.model(Model.HAIJUN_OPUS_5_5)
.maxTokens(1024)
.addTool(ToolTextEditor20250728.builder().build())
// Pesan-pesan sebelumnya akan ditempatkan di sini
.addAssistantMessageOfBlockParams(
List.of(
ContentBlockParam.ofText(
TextBlockParam.builder()
.text(
"I found the syntax error in your primes.py file. In the `get_primes` function, there is a missing colon (:) at the end of the for loop line. Let me fix that for you."
)
.build()
),
ContentBlockParam.ofToolUse(
ToolUseBlockParam.builder()
.id("toolu_01PqRsTuVwXyZAbCdEfGh")
.name("str_replace_based_edit_tool")
.input(
ToolUseBlockParam.Input.builder()
.putAdditionalProperty("command", JsonValue.from("str_replace"))
.putAdditionalProperty("path", JsonValue.from("primes.py"))
.putAdditionalProperty(
"old_str",
JsonValue.from(" for num in range(2, limit + 1)")
)
.putAdditionalProperty(
"new_str",
JsonValue.from(" for num in range(2, limit + 1):")
)
.build()
)
.build()
)
)
)
.addUserMessageOfBlockParams(
List.of(
ContentBlockParam.ofToolResult(
ToolResultBlockParam.builder()
.toolUseId("toolu_01PqRsTuVwXyZAbCdEfGh")
.content("Successfully replaced text at exactly one location.")
.build()
)
)
)
.build();
Message message = client.messages().create(params);
System.out.println(message); $client = new Client();
$response = $client->messages->create(
model: 'haijun-opus-5-5',
maxTokens: 1024,
tools: [new ToolTextEditor20250728()],
messages: [
// Pesan-pesan sebelumnya...
[
'role' => 'assistant',
'content' => [
[
'type' => 'text',
'text' => 'I found the syntax error in your primes.py file. In the `get_primes` function, there is a missing colon (:) at the end of the for loop line. Let me fix that for you.',
],
[
'type' => 'tool_use',
'id' => 'toolu_01PqRsTuVwXyZAbCdEfGh',
'name' => 'str_replace_based_edit_tool',
'input' => [
'command' => 'str_replace',
'path' => 'primes.py',
'old_str' => ' for num in range(2, limit + 1)',
'new_str' => ' for num in range(2, limit + 1):',
],
],
],
],
[
'role' => 'user',
'content' => [
[
'type' => 'tool_result',
'tool_use_id' => 'toolu_01PqRsTuVwXyZAbCdEfGh',
'content' => 'Successfully replaced text at exactly one location.',
],
],
],
],
);
echo $response; client = Juglow::Client.new
response = client.messages.create(
model: "haijun-opus-5-5",
max_tokens: 1024,
tools: [{type: "text_editor_20250728", name: "str_replace_based_edit_tool"}],
messages: [
# Pesan-pesan sebelumnya...
{
role: "assistant",
content: [
{
type: "text",
text: "I found the syntax error in your primes.py file. In the `get_primes` function, there is a missing colon (:) at the end of the for loop line. Let me fix that for you."
},
{
type: "tool_use",
id: "toolu_01PqRsTuVwXyZAbCdEfGh",
name: "str_replace_based_edit_tool",
input: {
command: "str_replace",
path: "primes.py",
old_str: " for num in range(2, limit + 1)",
new_str: " for num in range(2, limit + 1):"
}
}
]
},
{
role: "user",
content: [
{
type: "tool_result",
tool_use_id: "toolu_01PqRsTuVwXyZAbCdEfGh",
content: "Successfully replaced text at exactly one location."
}
]
}
]
)
puts responseTerakhir, Haijun memberikan penjelasan lengkap tentang perbaikannya:
{
"id": "msg_01IjKlMnOpQrStUvWxYzAb",
"model": "haijun-opus-5-5",
"stop_reason": "end_turn",
"role": "assistant",
"content": [
{
"type": "text",
"text": "I've fixed the syntax error in your primes.py file. The issue was in the `get_primes` function at line 19. There was a missing colon (:) at the end of the for loop line.\n\nHere's what I changed:\n\nFrom:\n```python\nfor num in range(2, limit + 1)\n```\n\nTo:\n```python\nfor num in range(2, limit + 1):\n```\n\nIn Python, a colon is required at the end of statements that start a new code block, such as for loops, if statements, function definitions, etc. Without the colon, Python raises a syntax error because it doesn't know where the code block begins.\n\nYour code should now run correctly. The function will properly generate a list of prime numbers up to the specified limit."
}
]
}Mengimplementasikan alat editor teks
Alat editor teks diimplementasikan sebagai alat tanpa skema. Saat menggunakan alat ini, Anda tidak perlu menyediakan skema input seperti pada alat lainnya; skemanya sudah tertanam dalam model Haijun dan tidak dapat dimodifikasi.
Tipe alatnya adalah type: "text_editor_20250728" untuk model Haijun 4 dan yang lebih baru.
- Inisialisasi implementasi editor Anda
Buat fungsi pembantu untuk menangani operasi file seperti membaca, menulis, dan memodifikasi file. Pertimbangkan untuk mengimplementasikan fungsionalitas backup agar dapat memulihkan dari kesalahan.
- Tangani panggilan alat editor
Buat fungsi yang memproses panggilan alat dari Haijun berdasarkan jenis perintah:
def handle_editor_tool(tool_call):
input_params = tool_call.input
command = input_params.get("command", "")
file_path = input_params.get("path", "")
match command:
case "view":
# Baca dan kembalikan isi file
pass
case "str_replace":
# Ganti teks dalam file
pass
case "create":
# Buat file baru
pass
case "insert":
# Sisipkan teks di lokasi tertentu
pass function handleEditorTool(toolCall: { input: { command?: string; path?: string } }): void {
const inputParams = toolCall.input;
const command = inputParams.command ?? "";
const filePath = inputParams.path ?? "";
switch (command) {
case "view":
// Baca dan kembalikan isi file
break;
case "str_replace":
// Ganti teks dalam file
break;
case "create":
// Buat file baru
break;
case "insert":
// Sisipkan teks di lokasi tertentu
break;
}
} static string HandleEditorTool(IReadOnlyDictionary<string, JsonElement> input)
{
input.TryGetValue("command", out var commandEl);
input.TryGetValue("path", out var pathEl);
var command = commandEl.ValueKind == JsonValueKind.String ? commandEl.GetString() : null;
var filePath = pathEl.ValueKind == JsonValueKind.String ? pathEl.GetString() : null;
if (command == "view")
{
// Baca dan kembalikan isi file
}
else if (command == "str_replace")
{
// Ganti teks dalam file
}
else if (command == "create")
{
// Buat file baru
}
else if (command == "insert")
{
// Sisipkan teks di lokasi tertentu
}
return "";
} func handleEditorTool(input map[string]any) string {
command, _ := input["command"].(string)
filePath, _ := input["path"].(string)
// ...
switch command {
case "view":
// Baca dan kembalikan isi file
case "str_replace":
// Ganti teks dalam file
case "create":
// Buat file baru
case "insert":
// Sisipkan teks di lokasi tertentu
}
return ""
} static void handleEditorTool(Map<String, Object> input) {
var command = (String) input.getOrDefault("command", "");
var filePath = (String) input.getOrDefault("path", "");
if (command.equals("view")) {
// Baca dan kembalikan isi file
} else if (command.equals("str_replace")) {
// Ganti teks dalam file
} else if (command.equals("create")) {
// Buat file baru
} else if (command.equals("insert")) {
// Sisipkan teks di lokasi tertentu
}
} function handle_editor_tool(array $input): string
{
$command = $input['command'] ?? '';
$filePath = $input['path'] ?? '';
if ($command === 'view') {
// Baca dan kembalikan isi file
} elseif ($command === 'str_replace') {
// Ganti teks dalam file
} elseif ($command === 'create') {
// Buat file baru
} elseif ($command === 'insert') {
// Sisipkan teks di lokasi tertentu
}
return '';
} def handle_editor_tool(input)
command = input[:command] || ""
file_path = input[:path] || ""
case command
when "view"
# Baca dan kembalikan isi file
when "str_replace"
# Ganti teks dalam file
when "create"
# Buat file baru
when "insert"
# Sisipkan teks di lokasi tertentu
end
end- Implementasikan langkah-langkah keamanan
Tambahkan validasi dan pemeriksaan keamanan:
- Validasi path file untuk mencegah directory traversal
- Buat backup sebelum melakukan perubahan
- Tangani error dengan baik
- Implementasikan pemeriksaan izin
- Proses respons Haijun
Ekstrak dan tangani panggilan alat dari respons Haijun:
# Proses penggunaan alat dalam respons Haijun
for content in response.content:
if content.type == "tool_use":
# Jalankan alat berdasarkan perintah
result = handle_editor_tool(content)
# Kembalikan hasil ke Haijun
tool_result = {
"type": "tool_result",
"tool_use_id": content.id,
"content": result,
} // Proses penggunaan alat dalam respons Haijun
for (const block of response.content) {
if (block.type === "tool_use") {
// Jalankan alat berdasarkan perintah
const result = handleEditorTool(block);
// Kembalikan hasil ke Haijun
const toolResult = {
type: "tool_result",
tool_use_id: block.id,
content: result
};
}
} // Proses penggunaan alat dalam respons Haijun
foreach (var block in response.Content)
{
if (block.TryPickToolUse(out var toolUse))
{
var result = HandleEditorTool(toolUse.Input);
var toolResult = new ToolResultBlockParam
{
ToolUseID = toolUse.ID,
Content = result,
};
}
} // Proses penggunaan alat dalam respons Haijun
for _, block := range response.Content {
if block.Type == "tool_use" {
var input map[string]any
if err := json.Unmarshal(block.Input, &input); err != nil {
log.Fatal(err)
}
result := handleEditorTool(input)
toolResult := juglow.NewToolResultBlock(block.ID, result, false)
// ...
}
} // Proses penggunaan alat dalam respons Haijun
for (var block : response.content()) {
if (block.type().equals("tool_use")) {
// Jalankan alat berdasarkan perintah
var result = handleEditorTool(block);
// Kembalikan hasil ke Haijun
var toolResult = Map.of(
"type", "tool_result",
"tool_use_id", block.id(),
"content", result
);
}
} // Proses penggunaan alat dalam respons Haijun
foreach ($response->content as $block) {
if ($block->type === 'tool_use') {
// Jalankan alat berdasarkan perintah
$result = handle_editor_tool($block->input);
// Kembalikan hasil ke Haijun
$toolResult = [
'type' => 'tool_result',
'tool_use_id' => $block->id,
'content' => $result,
];
}
} # Proses penggunaan alat dalam respons Haijun
tool_results = response.content.filter_map do |block|
next unless block.type == :tool_use
{type: "tool_result", tool_use_id: block.id, content: handle_editor_tool(block.input)}
endWarning: Saat mengimplementasikan alat editor teks, perhatikan hal-hal berikut: 1. Keamanan: Alat ini memiliki akses ke filesystem lokal Anda, jadi implementasikan langkah-langkah keamanan yang tepat. 2. Backup: Selalu buat backup sebelum mengizinkan pengeditan pada file penting. 3. Validasi: Validasi semua input untuk mencegah perubahan yang tidak diinginkan. 4. Pencocokan unik: Pastikan penggantian cocok dengan tepat satu lokasi untuk menghindari pengeditan yang tidak diinginkan.
Menangani error
Saat menggunakan alat editor teks, berbagai error dapat terjadi. Berikut panduan cara menanganinya:
#### File tidak ditemukan
Jika Haijun mencoba melihat atau memodifikasi file yang tidak ada, kembalikan pesan error yang sesuai dalam tool_result:
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01A09q90qw90lq917835lq9",
"content": "Error: File not found",
"is_error": true
}
]
}#### Beberapa kecocokan untuk penggantian
Jika perintah str_replace Haijun cocok dengan beberapa lokasi dalam file, kembalikan pesan error yang sesuai:
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01A09q90qw90lq917835lq9",
"content": "Error: Found 3 matches for replacement text. Please provide more context to make a unique match.",
"is_error": true
}
]
}#### Tidak ada kecocokan untuk penggantian
Jika perintah str_replace Haijun tidak cocok dengan teks apa pun dalam file, kembalikan pesan error yang sesuai:
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01A09q90qw90lq917835lq9",
"content": "Error: No match found for replacement. Please check your text and try again.",
"is_error": true
}
]
}#### Error izin
Jika ada masalah izin saat membuat, membaca, atau memodifikasi file, kembalikan pesan error yang sesuai:
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01A09q90qw90lq917835lq9",
"content": "Error: Permission denied. Cannot write to file.",
"is_error": true
}
]
}Ikuti praktik terbaik implementasi
#### Berikan konteks yang jelas
Saat meminta Haijun memperbaiki atau memodifikasi kode, jelaskan secara spesifik file apa yang perlu diperiksa atau masalah apa yang perlu ditangani. Konteks yang jelas membantu Haijun mengidentifikasi file yang tepat dan membuat perubahan yang sesuai.
Prompt yang kurang membantu: "Bisakah Anda memperbaiki kode saya?"
Prompt yang lebih baik: "Ada kesalahan sintaks di file primes.py saya yang membuatnya tidak bisa dijalankan. Bisakah Anda memperbaikinya?"
#### Jelaskan path file secara eksplisit
Tentukan path file dengan jelas bila diperlukan, terutama jika Anda bekerja dengan beberapa file atau file di direktori yang berbeda.
Prompt yang kurang membantu: "Tinjau file helper saya"
Prompt yang lebih baik: "Bisakah Anda memeriksa file utils/helpers.py saya untuk masalah performa?"
#### Buat backup sebelum mengedit
Implementasikan sistem backup dalam aplikasi Anda yang membuat salinan file sebelum mengizinkan Haijun mengeditnya, terutama untuk kode penting atau kode produksi.
def backup_file(file_path):
"""Create a backup of a file before editing."""
backup_path = f"{file_path}.backup"
if os.path.exists(file_path):
with open(file_path, "r") as src, open(backup_path, "w") as dst:
dst.write(src.read()) async function backupFile(filePath: string): Promise<void> {
const backupPath = `${filePath}.backup`;
try {
await access(filePath);
await copyFile(filePath, backupPath);
} catch {
// File tidak ada; tidak ada yang perlu dicadangkan
}
} static void BackupFile(string filePath)
{
var backupPath = $"{filePath}.backup";
if (File.Exists(filePath))
{
File.Copy(filePath, backupPath, overwrite: true);
}
} func backupFile(filePath string) error {
backupPath := filePath + ".backup"
data, err := os.ReadFile(filePath)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
return os.WriteFile(backupPath, data, 0o644)
} static void backupFile(String filePath) throws IOException {
Path source = Path.of(filePath);
Path backupPath = Path.of(filePath + ".backup");
if (Files.exists(source)) {
Files.copy(source, backupPath, StandardCopyOption.REPLACE_EXISTING);
}
} function backup_file(string $filePath): void
{
$backupPath = $filePath . '.backup';
if (file_exists($filePath)) {
copy($filePath, $backupPath);
}
} def backup_file(file_path)
backup_path = "#{file_path}.backup"
FileUtils.cp(file_path, backup_path) if File.exist?(file_path)
end#### Tangani penggantian teks unik dengan hati-hati
Perintah str_replace memerlukan kecocokan persis untuk teks yang akan diganti. Aplikasi Anda harus memastikan bahwa terdapat tepat satu kecocokan untuk teks lama atau memberikan pesan error yang sesuai.
def safe_replace(file_path, old_text, new_text):
"""Replace text only if there's exactly one match."""
with open(file_path, "r") as f:
content = f.read()
count = content.count(old_text)
if count == 0:
return "Error: No match found"
elif count > 1:
return f"Error: Found {count} matches"
else:
new_content = content.replace(old_text, new_text)
with open(file_path, "w") as f:
f.write(new_content)
return "Successfully replaced text" async function safeReplace(
filePath: string,
oldText: string,
newText: string
): Promise<string> {
const content = await readFile(filePath, "utf8");
const count = content.split(oldText).length - 1;
if (count === 0) {
return "Error: No match found";
} else if (count > 1) {
return `Error: Found ${count} matches`;
} else {
const newContent = content.replace(oldText, newText);
await writeFile(filePath, newContent, "utf8");
return "Successfully replaced text";
}
} static string SafeReplace(string filePath, string oldText, string newText)
{
var content = File.ReadAllText(filePath);
var count = content.Split(oldText).Length - 1;
if (count == 0)
{
return "Error: No match found";
}
else if (count > 1)
{
return $"Error: Found {count} matches";
}
else
{
var newContent = content.Replace(oldText, newText);
File.WriteAllText(filePath, newContent);
return "Successfully replaced text";
}
} func safeReplace(filePath, oldText, newText string) string {
data, err := os.ReadFile(filePath)
if err != nil {
return fmt.Sprintf("Error: %v", err)
}
content := string(data)
count := strings.Count(content, oldText)
if count == 0 {
return "Error: No match found"
} else if count > 1 {
return fmt.Sprintf("Error: Found %d matches", count)
}
newContent := strings.Replace(content, oldText, newText, 1)
if err := os.WriteFile(filePath, []byte(newContent), 0o644); err != nil {
return fmt.Sprintf("Error: %v", err)
}
return "Successfully replaced text"
} static String safeReplace(String filePath, String oldText, String newText) throws IOException {
String content = Files.readString(Path.of(filePath));
int count = content.split(Pattern.quote(oldText), -1).length - 1;
if (count == 0) {
return "Error: No match found";
} else if (count > 1) {
return "Error: Found " + count + " matches";
} else {
String newContent = content.replace(oldText, newText);
Files.writeString(Path.of(filePath), newContent);
return "Successfully replaced text";
}
} function safe_replace(string $filePath, string $oldText, string $newText): string
{
$content = file_get_contents($filePath);
$count = substr_count($content, $oldText);
if ($count === 0) {
return 'Error: No match found';
} elseif ($count > 1) {
return "Error: Found {$count} matches";
} else {
$newContent = str_replace($oldText, $newText, $content);
file_put_contents($filePath, $newContent);
return 'Successfully replaced text';
}
} def safe_replace(file_path, old_text, new_text)
content = File.read(file_path)
count = content.scan(old_text).length
if count == 0
"Error: No match found"
elsif count > 1
"Error: Found #{count} matches"
else
new_content = content.sub(old_text) { new_text }
File.write(file_path, new_content)
"Successfully replaced text"
end
end#### Verifikasi perubahan
Setelah Haijun membuat perubahan pada file, verifikasi perubahan tersebut dengan menjalankan tes atau memeriksa bahwa kode masih berfungsi seperti yang diharapkan.
def verify_changes(file_path):
"""Run tests or checks after making changes."""
try:
# Untuk file Python, periksa kesalahan sintaks
if file_path.endswith(".py"):
import ast
with open(file_path, "r") as f:
ast.parse(f.read())
return "Syntax check passed"
except Exception as e:
return f"Verification failed: {str(e)}" function verifyChanges(filePath: string): string {
try {
// Untuk file Python, periksa kesalahan sintaks
if (filePath.endsWith(".py")) {
execFileSync("python3", ["-m", "py_compile", filePath]);
return "Syntax check passed";
}
return "No checks defined for this file type";
} catch (err) {
return `Verification failed: ${err}`;
}
} static string VerifyChanges(string filePath)
{
try
{
// Untuk file Python, periksa kesalahan sintaks
if (filePath.EndsWith(".py"))
{
var psi = new ProcessStartInfo("python3")
{
RedirectStandardError = true,
};
psi.ArgumentList.Add("-m");
psi.ArgumentList.Add("py_compile");
psi.ArgumentList.Add(filePath);
using var proc = Process.Start(psi)!;
proc.WaitForExit();
if (proc.ExitCode != 0)
{
return $"Verification failed: {proc.StandardError.ReadToEnd()}";
}
return "Syntax check passed";
}
return "No checks defined for this file type";
}
catch (Exception e)
{
return $"Verification failed: {e.Message}";
}
} func verifyChanges(filePath string) string {
// Untuk file Python, periksa kesalahan sintaks
if strings.HasSuffix(filePath, ".py") {
cmd := exec.Command("python3", "-m", "py_compile", filePath)
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Sprintf("Verification failed: %v: %s", err, out)
}
return "Syntax check passed"
}
return "No checks defined for this file type"
} static String verifyChanges(String filePath) {
try {
// Untuk file Python, periksa kesalahan sintaks
if (filePath.endsWith(".py")) {
Process proc = new ProcessBuilder("python3", "-m", "py_compile", filePath)
.redirectErrorStream(true)
.start();
if (proc.waitFor() != 0) {
return "Verification failed: " + new String(proc.getInputStream().readAllBytes());
}
return "Syntax check passed";
}
return "No checks defined for this file type";
} catch (IOException | InterruptedException e) {
return "Verification failed: " + e.getMessage();
}
} function verify_changes(string $filePath): string
{
// Untuk file Python, periksa kesalahan sintaks
if (str_ends_with($filePath, '.py')) {
exec('python3 -m py_compile ' . escapeshellarg($filePath) . ' 2>&1', $output, $exitCode);
if ($exitCode !== 0) {
return 'Verification failed: ' . implode("\n", $output);
}
return 'Syntax check passed';
}
return 'No checks defined for this file type';
} def verify_changes(file_path)
# Untuk file Python, periksa kesalahan sintaks
if file_path.end_with?(".py")
if system("python3", "-m", "py_compile", file_path)
"Syntax check passed"
else
"Verification failed: syntax error in #{file_path}"
end
else
"No checks defined for this file type"
end
endHarga dan penggunaan token
Alat editor teks menggunakan struktur harga yang sama dengan alat lain yang digunakan bersama Haijun. Alat ini mengikuti harga token input dan output standar berdasarkan model Haijun yang Anda gunakan.
Selain token dasar, token input tambahan berikut diperlukan untuk alat editor teks:
| Alat | Token input tambahan |
|---|---|
text_editor_20250429 (Haijun 4.x) | 700 token |
Untuk informasi lebih rinci tentang harga alat, lihat Harga penggunaan alat.
Mengintegrasikan alat editor teks dengan alat lain
Anda dapat menggunakan alat editor teks bersama alat Haijun lainnya. Saat menggabungkan alat, pastikan Anda:
- Mencocokkan versi alat dengan model yang Anda gunakan
- Memperhitungkan penggunaan token tambahan untuk semua alat yang disertakan dalam permintaan Anda
Log perubahan
| Tanggal | Versi | Perubahan |
|---|---|---|
| 28 Juli 2025 | text_editor_20250728 | Rilis alat editor teks yang diperbarui yang memperbaiki beberapa masalah dan menambahkan parameter opsional max_characters. Selain itu, alat ini identik dengan text_editor_20250429. |
| 29 April 2025 | text_editor_20250429 | Rilis alat editor teks untuk Haijun 4. Versi ini menghapus perintah undo_edit tetapi mempertahankan semua kemampuan lainnya. Nama alat telah diperbarui untuk mencerminkan arsitekturnya yang berbasis str\_replace. |
| 13 Maret 2025 | text_editor_20250124 | Pengenalan dokumentasi alat editor teks mandiri. Versi ini dioptimalkan untuk Haijun Sonnet 3.7 tetapi memiliki kemampuan yang identik dengan versi sebelumnya. |
| 22 Oktober 2024 | text_editor_20241022 | Rilis awal alat editor teks dengan Haijun Sonnet 3.5 (sudah dipensiunkan; lihat Penghentian model). Menyediakan kemampuan untuk melihat, membuat, dan mengedit file melalui perintah view, create, str_replace, insert, dan undo_edit. |
Langkah selanjutnya
Berikut beberapa ide tentang cara menggunakan alat editor teks dengan cara yang lebih praktis dan andal:
- Integrasikan dengan alur kerja pengembangan Anda: Bangun alat editor teks ke dalam alat pengembangan atau IDE Anda
- Buat sistem tinjauan kode: Minta Haijun meninjau kode Anda dan melakukan perbaikan
- Bangun asisten debugging: Buat sistem di mana Haijun dapat membantu Anda mendiagnosis dan memperbaiki masalah dalam kode Anda
- Implementasikan konversi format file: Biarkan Haijun membantu Anda mengonversi file dari satu format ke format lain
- Otomatiskan dokumentasi: Siapkan alur kerja agar Haijun mendokumentasikan kode Anda secara otomatis
Alat editor teks memungkinkan Haijun bekerja langsung dengan basis kode Anda, mendukung alur kerja mulai dari debugging hingga dokumentasi otomatis.
Pelajari cara mengimplementasikan alur kerja alat untuk digunakan dengan Haijun.
Jalankan perintah shell dengan Haijun.