t27.aiEnglish

Руки: инструменты, которые вызывает агент

Вы узнаете

Что такое инструмент, какие инструменты агент запускает сразу и какой ждёт вашего согласия.

Агент только читает и пишет текст. Сам он не может открыть файл или запустить тест. Инструмент это функция, которую запускает за него хост: агент называет инструмент, хост сверяет имя со списком, запускает его и возвращает результат текстом. В этой спецификации 3 инструмента: 0 прочитать файл, 1 запустить тесты и 2 записать файл. Чтение и тесты ничего не меняют, поэтому идут сразу; запись меняет ваш проект, поэтому ждёт вашего согласия. Цикл тоже ограничен: из 9 вызовов, запрошенных за один ход, хост пропускает 5, так что застрявший агент не будет вызывать инструменты вечно. Внесите ошибку: поставьте FREE_BELOW равным 3, и тест writing_waits_for_a_yes покраснеет: запись пошла бы без вопроса.

Попробуйте

Откройте виджет и назовите два инструмента, которые идут сразу; затем запустите спецификацию, поставьте FREE_BELOW равным 3 и прочитайте, какой тест падает. Верните строку и запустите снова.

Открыть интерактивный урок →

Vibecoding 20: tools an agent can call ↗

Three tools, which run at once and which wait for a yes, and the cap on calls in one turn. Lesson 20 of the Vibecoding from zero course.

specs/vibecoding/agent_hands.t27

// SPDX-License-Identifier: Apache-2.0
; specs/vibecoding/agent_hands.t27 -- vibecoding lesson 20: the hands of an agent, tools it can call.
; An agent only reads and writes text. A tool is a function the host runs for it: the agent names
; the tool, the host checks the name against a list, runs it and sends the result back. Tool ids:
; 0 read a file, 1 run the tests, 2 write a file. The first two run at once; writing waits for the
; owner to say yes. The loop is capped so a stuck agent cannot call a tool forever.
; Run the tests, then plant the bug named at the bottom and watch one test turn red.

module vibecoding_agent_hands;

pub const TOOL_READ : u8 = 0;
pub const TOOL_TEST : u8 = 1;
pub const TOOL_WRITE : u8 = 2;
pub const TOOL_COUNT : u8 = 3;

; A tool with an id below this one runs without asking.
pub const FREE_BELOW : u8 = 2;

; The agent asked for this many calls in one turn; the host lets this many through.
pub const MAX_CALLS : u8 = 5;
pub const ASKED_CALLS : u8 = 9;
pub const RUN_CALLS : u8 = 5;

test reading_runs_at_once {
    assert TOOL_READ < FREE_BELOW;
}

test running_the_tests_runs_at_once {
    assert TOOL_TEST < FREE_BELOW;
}

; The negative control. Writing is not free: its id is not below FREE_BELOW, so the host asks
; the owner first. Plant the bug by setting FREE_BELOW to 3: this test turns red.
test writing_waits_for_a_yes {
    assert TOOL_WRITE >= FREE_BELOW;
}

test every_tool_has_an_id_in_the_list {
    assert TOOL_WRITE < TOOL_COUNT;
    assert TOOL_COUNT == TOOL_WRITE + 1;
}

test the_loop_is_capped {
    assert ASKED_CALLS > MAX_CALLS;
    assert RUN_CALLS == MAX_CALLS;
}

Открыть спеку урока в плеере ↗

Все уроки