snapkitty
developer-tools
c++
File size: 6,318 Bytes
0196580
 
 
662b148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
% SPDX-License-Identifier: LicenseRef-Sovereign-Leviathan-SL-AGPL3-001
% Licensing: see LICENSE in the repository root.

%%%-------------------------------------------------------------------
%%% @doc `/agents' API domain.
%%%
%%%   POST   /api/agents                  create an agent
%%%   GET    /api/agents                  list agents
%%%   GET    /api/agents/:id              describe one agent
%%%   POST   /api/agents/:id/objective    assign its objective
%%%   POST   /api/agents/:id/actions      run one loop iteration (a tool call)
%%%   POST   /api/agents/:id/complete     mark completed
%%%   POST   /api/agents/:id/terminate    terminate
%%%
%%% Every POST here goes through `hk_api_util:run_operation/4', so
%%% every mutating call returns an `operation_id' alongside its
%%% result -- including a failed one, per the "every mutating
%%% operation should return an operation identifier" requirement
%%% (an operation identifier is returned whether the operation
%%% succeeded or not; only its `status' differs).
%%% @end
%%%-------------------------------------------------------------------
-module(hk_api_agents_h).

-export([init/2]).

init(Req0 = #{method := <<"POST">>}, collection) ->
    RequestId = hk_api_util:request_id(Req0),
    {ok, Body, Req1} = hk_api_util:read_json_body(Req0),
    Capabilities = parse_capabilities(maps:get(<<"capabilities">>, Body, [])),
    Limits = maps:get(<<"limits">>, Body, #{}),
    Req = hk_api_util:run_operation(agent_create, RequestId, fun() ->
        AgentId = hk_id:new(<<"agent">>),
        case hk_agent_fsm_sup:start_agent(#{agent_id => AgentId, capabilities => Capabilities,
                                              limits => atomize_limit_keys(Limits)}) of
            {ok, _Pid} -> {ok, {agent, AgentId}, #{agent_id => AgentId}};
            {error, Reason} -> {error, Reason}
        end
    end, Req1),
    {ok, Req, collection};

init(Req0 = #{method := <<"GET">>}, collection) ->
    Described = [describe_or_skip(Id) || {Id, _Pid} <- hk_agent_registry:list_agents()],
    Agents = [M || {true, M} <- Described],
    Req = hk_api_util:reply_json(200, #{agents => Agents}, Req0),
    {ok, Req, collection};

init(Req0 = #{method := <<"GET">>}, item) ->
    AgentId = cowboy_req:binding(id, Req0),
    Req = case lookup_agent(AgentId) of
              {ok, Pid} ->
                  {ok, Agent} = hk_agent_fsm:describe(Pid),
                  hk_api_util:reply_json(200, hk_schema:to_map(Agent), Req0);
              {error, not_found} ->
                  hk_api_util:reply_error(404, agent_not_found, Req0)
          end,
    {ok, Req, item};

init(Req0 = #{method := <<"POST">>}, objective) ->
    AgentId = cowboy_req:binding(id, Req0),
    RequestId = hk_api_util:request_id(Req0),
    {ok, Body, Req1} = hk_api_util:read_json_body(Req0),
    Description = maps:get(<<"description">>, Body, <<>>),
    Req = hk_api_util:run_operation(agent_assign_objective, RequestId, fun() ->
        case lookup_agent(AgentId) of
            {ok, Pid} ->
                case hk_agent_fsm:assign_objective(Pid, Description) of
                    ok -> {ok, {agent, AgentId}, #{agent_id => AgentId}};
                    {error, Reason} -> {error, Reason}
                end;
            {error, Reason} -> {error, Reason}
        end
    end, Req1),
    {ok, Req, objective};

init(Req0 = #{method := <<"POST">>}, actions) ->
    AgentId = cowboy_req:binding(id, Req0),
    RequestId = hk_api_util:request_id(Req0),
    {ok, Body, Req1} = hk_api_util:read_json_body(Req0),
    Tool = binary_to_atom(maps:get(<<"tool">>, Body, <<"browser">>), utf8),
    Op = binary_to_atom(maps:get(<<"op">>, Body, <<"read_page">>), utf8),
    Args = atomize_arg_keys(maps:get(<<"args">>, Body, #{})),
    Req = hk_api_util:run_operation(agent_action, RequestId, fun() ->
        case lookup_agent(AgentId) of
            {ok, Pid} ->
                case hk_agent_fsm:next_action(Pid, Tool, Op, Args) of
                    {ok, Result} -> {ok, {agent, AgentId}, #{observation => Result}};
                    {error, Reason} -> {error, Reason}
                end;
            {error, Reason} -> {error, Reason}
        end
    end, Req1),
    {ok, Req, actions};

init(Req0 = #{method := <<"POST">>}, complete) ->
    AgentId = cowboy_req:binding(id, Req0),
    RequestId = hk_api_util:request_id(Req0),
    {ok, Body, Req1} = hk_api_util:read_json_body(Req0),
    Result = maps:get(<<"result">>, Body, null),
    Req = hk_api_util:run_operation(agent_complete, RequestId, fun() ->
        case lookup_agent(AgentId) of
            {ok, Pid} -> ok = hk_agent_fsm:complete(Pid, Result), {ok, {agent, AgentId}, #{}};
            {error, Reason} -> {error, Reason}
        end
    end, Req1),
    {ok, Req, complete};

init(Req0 = #{method := <<"POST">>}, terminate) ->
    AgentId = cowboy_req:binding(id, Req0),
    RequestId = hk_api_util:request_id(Req0),
    {ok, Body, Req1} = hk_api_util:read_json_body(Req0),
    Reason0 = maps:get(<<"reason">>, Body, <<"operator_requested">>),
    Req = hk_api_util:run_operation(agent_terminate, RequestId, fun() ->
        case lookup_agent(AgentId) of
            {ok, Pid} -> ok = hk_agent_fsm:terminate_agent(Pid, Reason0), {ok, {agent, AgentId}, #{}};
            {error, Reason} -> {error, Reason}
        end
    end, Req1),
    {ok, Req, terminate};

init(Req0, State) ->
    Req = hk_api_util:reply_error(405, method_not_allowed, Req0),
    {ok, Req, State}.

%%% internal

lookup_agent(undefined) -> {error, missing_agent_id};
lookup_agent(AgentId) -> hk_agent_registry:lookup(AgentId).

describe_or_skip(AgentId) ->
    case lookup_agent(AgentId) of
        {ok, Pid} ->
            case catch hk_agent_fsm:describe(Pid) of
                {ok, Agent} -> {true, hk_schema:to_map(Agent)};
                _ -> false
            end;
        _ -> false
    end.

parse_capabilities(List) when is_list(List) ->
    [binary_to_atom(C, utf8) || C <- List, is_binary(C)];
parse_capabilities(_) ->
    [].

atomize_limit_keys(Map) when is_map(Map) ->
    maps:fold(fun(K, V, Acc) -> Acc#{binary_to_atom(K, utf8) => V} end, #{}, Map);
atomize_limit_keys(_) -> #{}.

atomize_arg_keys(Map) when is_map(Map) ->
    maps:fold(fun(K, V, Acc) -> Acc#{binary_to_atom(K, utf8) => V} end, #{}, Map);
atomize_arg_keys(_) -> #{}.