-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(tool): conditional description for read file #603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ | |
|
|
||
| ## 未发布 | ||
|
|
||
| - Tool:让 `ReadFile` 工具描述根据模型能力动态反映图片/视频支持情况 | ||
|
|
||
| ## 0.75 (2026-01-09) | ||
|
|
||
| - Tool:改进 `ReadFile` 工具描述 | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,9 +6,9 @@ | |||||
| from kosong.tooling import CallableTool2, ToolError, ToolOk, ToolReturnValue | ||||||
| from pydantic import BaseModel, Field | ||||||
|
|
||||||
| from kimi_cli.soul.agent import BuiltinSystemPromptArgs | ||||||
| from kimi_cli.soul.agent import Runtime | ||||||
| from kimi_cli.tools.file.utils import MEDIA_SNIFF_BYTES, FileType, detect_file_type | ||||||
| from kimi_cli.tools.utils import load_desc, truncate_line | ||||||
| from kimi_cli.tools.utils import load_desc_jinja, truncate_line | ||||||
| from kimi_cli.utils.path import is_within_directory | ||||||
| from kimi_cli.wire.types import ImageURLPart, VideoURLPart | ||||||
|
|
||||||
|
|
@@ -52,20 +52,22 @@ class Params(BaseModel): | |||||
|
|
||||||
| class ReadFile(CallableTool2[Params]): | ||||||
| name: str = "ReadFile" | ||||||
| description: str = load_desc( | ||||||
| Path(__file__).parent / "read.md", | ||||||
| { | ||||||
| "MAX_LINES": str(MAX_LINES), | ||||||
| "MAX_LINE_LENGTH": str(MAX_LINE_LENGTH), | ||||||
| "MAX_BYTES": str(MAX_BYTES), | ||||||
| "MAX_MEDIA_BYTES": str(MAX_MEDIA_BYTES), | ||||||
| }, | ||||||
| ) | ||||||
| params: type[Params] = Params | ||||||
|
|
||||||
| def __init__(self, builtin_args: BuiltinSystemPromptArgs) -> None: | ||||||
| super().__init__() | ||||||
| self._work_dir = builtin_args.KIMI_WORK_DIR | ||||||
| def __init__(self, runtime: Runtime) -> None: | ||||||
| capabilities = runtime.llm.capabilities if runtime.llm else set[str]() | ||||||
|
||||||
| capabilities = runtime.llm.capabilities if runtime.llm else set[str]() | |
| capabilities = runtime.llm.capabilities if runtime.llm else set() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| from __future__ import annotations | ||
| from typing import cast | ||
|
|
||
| # ruff: noqa | ||
|
|
||
| import pytest | ||
| from inline_snapshot import snapshot | ||
|
|
||
| from kimi_cli.llm import ModelCapability | ||
| from kimi_cli.soul.agent import Runtime | ||
| from kimi_cli.tools.file.read import ReadFile | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("capabilities", "expected"), | ||
| [ | ||
| ( | ||
| {"image_in", "video_in"}, | ||
| snapshot( | ||
| """\ | ||
| Read content from a file. | ||
|
|
||
| **Tips:** | ||
| - Make sure you follow the description of each tool parameter. | ||
| - A `<system>` tag will be given before the read file content. | ||
| - The system will notify you when there is anything wrong when reading the file. | ||
| - This tool is a tool that you typically want to use in parallel. Always read multiple files in one response when possible. | ||
| - This tool can only read text, image and video files. To list directories, you must use the Glob tool or `ls` command via the Shell tool. To read other file types, use appropriate commands via the Shell tool. | ||
| - If the file doesn't exist or path is invalid, an error will be returned. | ||
| - If you want to search for a certain content/pattern, prefer Grep tool over ReadFile. | ||
| - For text files: | ||
| - Content will be returned with a line number before each line like `cat -n` format. | ||
| - Use `line_offset` and `n_lines` parameters when you only need to read a part of the file. | ||
| - The maximum number of lines that can be read at once is 1000. | ||
| - Any lines longer than 2000 characters will be truncated, ending with "...". | ||
| - For image and video files: | ||
| - Content will be returned in a form that you can view and understand. Feel confident to read image/video files with this tool. | ||
| - The maximum size that can be read is 83886080 bytes. An error will be returned if the file is larger than this limit. | ||
| """ | ||
| ), | ||
| ), | ||
| ( | ||
| {"image_in"}, | ||
| snapshot( | ||
| """\ | ||
| Read content from a file. | ||
|
|
||
| **Tips:** | ||
| - Make sure you follow the description of each tool parameter. | ||
| - A `<system>` tag will be given before the read file content. | ||
| - The system will notify you when there is anything wrong when reading the file. | ||
| - This tool is a tool that you typically want to use in parallel. Always read multiple files in one response when possible. | ||
| - This tool can only read text, image and video files. To list directories, you must use the Glob tool or `ls` command via the Shell tool. To read other file types, use appropriate commands via the Shell tool. | ||
| - If the file doesn't exist or path is invalid, an error will be returned. | ||
| - If you want to search for a certain content/pattern, prefer Grep tool over ReadFile. | ||
| - For text files: | ||
| - Content will be returned with a line number before each line like `cat -n` format. | ||
| - Use `line_offset` and `n_lines` parameters when you only need to read a part of the file. | ||
| - The maximum number of lines that can be read at once is 1000. | ||
| - Any lines longer than 2000 characters will be truncated, ending with "...". | ||
| - For image files: | ||
| - Content will be returned in a form that you can view and understand. Feel confident to read image files with this tool. | ||
| - The maximum size that can be read is 83886080 bytes. An error will be returned if the file is larger than this limit. | ||
| - Other media files (e.g., video, PDFs) are not supported by this tool. Use other proper tools to process them. | ||
| """ | ||
| ), | ||
| ), | ||
| ( | ||
| {"video_in"}, | ||
| snapshot( | ||
| """\ | ||
| Read content from a file. | ||
|
|
||
| **Tips:** | ||
| - Make sure you follow the description of each tool parameter. | ||
| - A `<system>` tag will be given before the read file content. | ||
| - The system will notify you when there is anything wrong when reading the file. | ||
| - This tool is a tool that you typically want to use in parallel. Always read multiple files in one response when possible. | ||
| - This tool can only read text, image and video files. To list directories, you must use the Glob tool or `ls` command via the Shell tool. To read other file types, use appropriate commands via the Shell tool. | ||
| - If the file doesn't exist or path is invalid, an error will be returned. | ||
| - If you want to search for a certain content/pattern, prefer Grep tool over ReadFile. | ||
| - For text files: | ||
| - Content will be returned with a line number before each line like `cat -n` format. | ||
| - Use `line_offset` and `n_lines` parameters when you only need to read a part of the file. | ||
| - The maximum number of lines that can be read at once is 1000. | ||
| - Any lines longer than 2000 characters will be truncated, ending with "...". | ||
| - For video files: | ||
| - Content will be returned in a form that you can view and understand. Feel confident to read video files with this tool. | ||
| - The maximum size that can be read is 83886080 bytes. An error will be returned if the file is larger than this limit. | ||
| - Other media files (e.g., image, PDFs) are not supported by this tool. Use other proper tools to process them. | ||
| """ | ||
| ), | ||
| ), | ||
| ( | ||
| set(), | ||
| snapshot( | ||
| """\ | ||
| Read content from a file. | ||
|
|
||
| **Tips:** | ||
| - Make sure you follow the description of each tool parameter. | ||
| - A `<system>` tag will be given before the read file content. | ||
| - The system will notify you when there is anything wrong when reading the file. | ||
| - This tool is a tool that you typically want to use in parallel. Always read multiple files in one response when possible. | ||
| - This tool can only read text, image and video files. To list directories, you must use the Glob tool or `ls` command via the Shell tool. To read other file types, use appropriate commands via the Shell tool. | ||
| - If the file doesn't exist or path is invalid, an error will be returned. | ||
| - If you want to search for a certain content/pattern, prefer Grep tool over ReadFile. | ||
| - For text files: | ||
| - Content will be returned with a line number before each line like `cat -n` format. | ||
| - Use `line_offset` and `n_lines` parameters when you only need to read a part of the file. | ||
| - The maximum number of lines that can be read at once is 1000. | ||
| - Any lines longer than 2000 characters will be truncated, ending with "...". | ||
| - Media files (e.g., image, video, PDFs) are not supported by this tool. Use other proper tools to process them. | ||
| """ | ||
| ), | ||
| ), | ||
| ], | ||
| ) | ||
| def test_read_file_description_by_capabilities( | ||
| runtime: Runtime, capabilities: set[str], expected: str | ||
| ) -> None: | ||
| assert runtime.llm is not None | ||
| runtime.llm.capabilities = cast(set[ModelCapability], capabilities) | ||
| assert ReadFile(runtime).base.description == expected |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These Makefile changes (updating prompt file paths from
./docs/prompts/to.kimi/prompts/and changing file extensions from.txtto.md) appear to be unrelated to the PR's stated purpose of adding conditional ReadFile descriptions. These changes should either be explained in the PR description or moved to a separate commit/PR for better change tracking.