106 lines
3.0 KiB
Markdown
106 lines
3.0 KiB
Markdown
---
|
|
import:
|
|
http: "@net"
|
|
json: "std:data/json"
|
|
---
|
|
|
|
# @exec Agent reference
|
|
|
|
## ask
|
|
|
|
Use a bounded two-turn model conversation. The mock model may request one
|
|
local tool explicitly enabled by the caller. Both turns use the same endpoint.
|
|
The result includes the model identity and tool outcome as provenance.
|
|
|
|
`params(url, query, tools = [])`
|
|
`if 长度(query) > 4096`
|
|
`return failure("agent.input_limit", "Prompt exceeds the reference agent limit")`
|
|
`end`
|
|
`first = exchange(url, [{role: "user", content: query}], tools)`
|
|
`if not first.ok`
|
|
`return first`
|
|
`end`
|
|
`reply = first.value`
|
|
`if map.has(reply, "answer")`
|
|
`return answer(reply, [])`
|
|
`end`
|
|
`if not map.has(reply, "tool") or 类型(reply.tool) != "Text" or not map.has(reply, "input")`
|
|
`return failure("agent.response", "Expected an answer or a tool request")`
|
|
`end`
|
|
`tool = local_tool(reply.tool, reply.input, tools)`
|
|
`if not tool.ok`
|
|
`return tool`
|
|
`end`
|
|
`transcript = [{role: "user", content: query}, {role: "assistant", tool: reply.tool, input: reply.input}, {role: "tool", tool: reply.tool, value: tool.value}]`
|
|
`second = exchange(url, transcript, [])`
|
|
`if not second.ok`
|
|
`return second`
|
|
`end`
|
|
`return answer(second.value, [{model: reply.model, tool: reply.tool, output: tool.value}])`
|
|
|
|
## exchange
|
|
|
|
`params(url, messages, tools)`
|
|
`request = json.格式化({messages: messages, tools: tools})`
|
|
`result = http.request(url, "POST", request, "application/json")`
|
|
`if not result.ok`
|
|
`return result`
|
|
`end`
|
|
`received = result.value`
|
|
`if received.status != 200`
|
|
`return failure("agent.http", "Model returned HTTP " + 文本(received.status))`
|
|
`end`
|
|
`parsed = json.解析(received.body)`
|
|
`if not parsed.ok`
|
|
`return failure("agent.response", "Model returned invalid JSON")`
|
|
`end`
|
|
`reply = parsed.value`
|
|
`if 类型(reply) != "Map" or not map.has(reply, "model") or 类型(reply.model) != "Text"`
|
|
`return failure("agent.response", "Model identity is missing")`
|
|
`end`
|
|
`return {ok: true, value: reply}`
|
|
|
|
## local_tool
|
|
|
|
The model describes a request. The caller supplies the explicit local
|
|
allowlist, and the library validates the data before choosing the action.
|
|
|
|
`params(name, input, tools)`
|
|
`allowed = false`
|
|
`i = 0`
|
|
`while i < 长度(tools)`
|
|
`if tools[i] == name`
|
|
`allowed = true`
|
|
`end`
|
|
`i = i + 1`
|
|
`end`
|
|
`if not allowed or name != "sum"`
|
|
`return failure("agent.tool_denied", "Tool is unavailable for this conversation")`
|
|
`end`
|
|
`if 类型(input) != "List" or 长度(input) > 32`
|
|
`return failure("agent.tool_input", "sum expects up to 32 integers")`
|
|
`end`
|
|
`sum = 0`
|
|
`i = 0`
|
|
`while i < 长度(input)`
|
|
`if 类型(input[i]) != "Int"`
|
|
`return failure("agent.tool_input", "sum expects integers")`
|
|
`end`
|
|
`sum = sum + input[i]`
|
|
`i = i + 1`
|
|
`end`
|
|
`return {ok: true, value: sum}`
|
|
|
|
## answer
|
|
|
|
`params(reply, tools)`
|
|
`if not map.has(reply, "answer") or 类型(reply.answer) != "Text"`
|
|
`return failure("agent.response", "Expected the model's final answer")`
|
|
`end`
|
|
`return {ok: true, value: {answer: reply.answer, model: reply.model, tools: tools}}`
|
|
|
|
## failure
|
|
|
|
`params(code, message)`
|
|
`return {ok: false, code: code, message: message}`
|