Compare commits
7 Commits
main
...
feat/llm-t
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aaf6a37385 | ||
|
|
250210d32b | ||
|
|
764d79dadf | ||
|
|
bd98589b39 | ||
|
|
81cc6aefaf | ||
|
|
da3a56708f | ||
|
|
c5356e121a |
@ -5,6 +5,7 @@ Docs: https://docs.clawd.bot
|
|||||||
## 2026.1.23 (Unreleased)
|
## 2026.1.23 (Unreleased)
|
||||||
|
|
||||||
### Changes
|
### Changes
|
||||||
|
- Plugins: add optional llm-task JSON-only tool for workflows. (#1498) Thanks @vignesh07.
|
||||||
- CLI: restart the gateway by default after `clawdbot update`; add `--no-restart` to skip it.
|
- CLI: restart the gateway by default after `clawdbot update`; add `--no-restart` to skip it.
|
||||||
- CLI: add live auth probes to `clawdbot models status` for per-profile verification.
|
- CLI: add live auth probes to `clawdbot models status` for per-profile verification.
|
||||||
- Docs: add cron vs heartbeat decision guide (with Lobster workflow notes). (#1533) Thanks @JustYannicc.
|
- Docs: add cron vs heartbeat decision guide (with Lobster workflow notes). (#1533) Thanks @JustYannicc.
|
||||||
|
|||||||
@ -1000,6 +1000,8 @@
|
|||||||
"group": "Tools & Skills",
|
"group": "Tools & Skills",
|
||||||
"pages": [
|
"pages": [
|
||||||
"tools",
|
"tools",
|
||||||
|
"tools/lobster",
|
||||||
|
"tools/llm-task",
|
||||||
"plugin",
|
"plugin",
|
||||||
"plugins/voice-call",
|
"plugins/voice-call",
|
||||||
"plugins/zalouser",
|
"plugins/zalouser",
|
||||||
|
|||||||
@ -156,6 +156,7 @@ alongside tools (for example, the voice-call plugin).
|
|||||||
|
|
||||||
Optional plugin tools:
|
Optional plugin tools:
|
||||||
- [Lobster](/tools/lobster): typed workflow runtime with resumable approvals (requires the Lobster CLI on the gateway host).
|
- [Lobster](/tools/lobster): typed workflow runtime with resumable approvals (requires the Lobster CLI on the gateway host).
|
||||||
|
- [LLM Task](/tools/llm-task): JSON-only LLM step for structured workflow output (optional schema validation).
|
||||||
|
|
||||||
## Tool inventory
|
## Tool inventory
|
||||||
|
|
||||||
|
|||||||
114
docs/tools/llm-task.md
Normal file
114
docs/tools/llm-task.md
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
---
|
||||||
|
summary: "JSON-only LLM tasks for workflows (optional plugin tool)"
|
||||||
|
read_when:
|
||||||
|
- You want a JSON-only LLM step inside workflows
|
||||||
|
- You need schema-validated LLM output for automation
|
||||||
|
---
|
||||||
|
|
||||||
|
# LLM Task
|
||||||
|
|
||||||
|
`llm-task` is an **optional plugin tool** that runs a JSON-only LLM task and
|
||||||
|
returns structured output (optionally validated against JSON Schema).
|
||||||
|
|
||||||
|
This is ideal for workflow engines like Lobster: you can add a single LLM step
|
||||||
|
without writing custom Clawdbot code for each workflow.
|
||||||
|
|
||||||
|
## Enable the plugin
|
||||||
|
|
||||||
|
1) Enable the plugin:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"plugins": {
|
||||||
|
"entries": {
|
||||||
|
"llm-task": { "enabled": true }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
2) Allowlist the tool (it is registered with `optional: true`):
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"agents": {
|
||||||
|
"list": [
|
||||||
|
{
|
||||||
|
"id": "main",
|
||||||
|
"tools": { "allow": ["llm-task"] }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Config (optional)
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"plugins": {
|
||||||
|
"entries": {
|
||||||
|
"llm-task": {
|
||||||
|
"enabled": true,
|
||||||
|
"config": {
|
||||||
|
"defaultProvider": "openai-codex",
|
||||||
|
"defaultModel": "gpt-5.2",
|
||||||
|
"defaultAuthProfileId": "main",
|
||||||
|
"allowedModels": ["openai-codex/gpt-5.2"],
|
||||||
|
"maxTokens": 800,
|
||||||
|
"timeoutMs": 30000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`allowedModels` is an allowlist of `provider/model` strings. If set, any request
|
||||||
|
outside the list is rejected.
|
||||||
|
|
||||||
|
## Tool parameters
|
||||||
|
|
||||||
|
- `prompt` (string, required)
|
||||||
|
- `input` (any, optional)
|
||||||
|
- `schema` (object, optional JSON Schema)
|
||||||
|
- `provider` (string, optional)
|
||||||
|
- `model` (string, optional)
|
||||||
|
- `authProfileId` (string, optional)
|
||||||
|
- `temperature` (number, optional)
|
||||||
|
- `maxTokens` (number, optional)
|
||||||
|
- `timeoutMs` (number, optional)
|
||||||
|
|
||||||
|
## Output
|
||||||
|
|
||||||
|
Returns `details.json` containing the parsed JSON (and validates against
|
||||||
|
`schema` when provided).
|
||||||
|
|
||||||
|
## Example: Lobster workflow step
|
||||||
|
|
||||||
|
```lobster
|
||||||
|
clawd.invoke --tool llm-task --action json --args-json '{
|
||||||
|
"prompt": "Given the input email, return intent and draft.",
|
||||||
|
"input": {
|
||||||
|
"subject": "Hello",
|
||||||
|
"body": "Can you help?"
|
||||||
|
},
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"intent": { "type": "string" },
|
||||||
|
"draft": { "type": "string" }
|
||||||
|
},
|
||||||
|
"required": ["intent", "draft"],
|
||||||
|
"additionalProperties": false
|
||||||
|
}
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
## Safety notes
|
||||||
|
|
||||||
|
- The tool is **JSON-only** and instructs the model to output only JSON (no
|
||||||
|
code fences, no commentary).
|
||||||
|
- No tools are exposed to the model for this run.
|
||||||
|
- Treat output as untrusted unless you validate with `schema`.
|
||||||
|
- Put approvals before any side-effecting step (send, post, exec).
|
||||||
@ -65,6 +65,52 @@ gog.gmail.search --query 'newer_than:1d' \
|
|||||||
| clawd.invoke --tool message --action send --each --item-key message --args-json '{"provider":"telegram","to":"..."}'
|
| clawd.invoke --tool message --action send --each --item-key message --args-json '{"provider":"telegram","to":"..."}'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## JSON-only LLM steps (llm-task)
|
||||||
|
|
||||||
|
For workflows that need a **structured LLM step**, enable the optional
|
||||||
|
`llm-task` plugin tool and call it from Lobster. This keeps the workflow
|
||||||
|
deterministic while still letting you classify/summarize/draft with a model.
|
||||||
|
|
||||||
|
Enable the tool:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"plugins": {
|
||||||
|
"entries": {
|
||||||
|
"llm-task": { "enabled": true }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"agents": {
|
||||||
|
"list": [
|
||||||
|
{
|
||||||
|
"id": "main",
|
||||||
|
"tools": { "allow": ["llm-task"] }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Use it in a pipeline:
|
||||||
|
|
||||||
|
```lobster
|
||||||
|
clawd.invoke --tool llm-task --action json --args-json '{
|
||||||
|
"prompt": "Given the input email, return intent and draft.",
|
||||||
|
"input": { "subject": "Hello", "body": "Can you help?" },
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"intent": { "type": "string" },
|
||||||
|
"draft": { "type": "string" }
|
||||||
|
},
|
||||||
|
"required": ["intent", "draft"],
|
||||||
|
"additionalProperties": false
|
||||||
|
}
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
See [LLM Task](/tools/llm-task) for details and configuration options.
|
||||||
|
|
||||||
## Workflow files (.lobster)
|
## Workflow files (.lobster)
|
||||||
|
|
||||||
Lobster can run YAML/JSON workflow files with `name`, `args`, `steps`, `env`, `condition`, and `approval` fields. In Clawdbot tool calls, set `pipeline` to the file path.
|
Lobster can run YAML/JSON workflow files with `name`, `args`, `steps`, `env`, `condition`, and `approval` fields. In Clawdbot tool calls, set `pipeline` to the file path.
|
||||||
|
|||||||
97
extensions/llm-task/README.md
Normal file
97
extensions/llm-task/README.md
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
# LLM Task (plugin)
|
||||||
|
|
||||||
|
Adds an **optional** agent tool `llm-task` for running **JSON-only** LLM tasks
|
||||||
|
(drafting, summarizing, classifying) with optional JSON Schema validation.
|
||||||
|
|
||||||
|
Designed to be called from workflow engines (for example, Lobster via
|
||||||
|
`clawd.invoke --each`) without adding new Clawdbot code per workflow.
|
||||||
|
|
||||||
|
## Enable
|
||||||
|
|
||||||
|
1) Enable the plugin:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"plugins": {
|
||||||
|
"entries": {
|
||||||
|
"llm-task": { "enabled": true }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
2) Allowlist the tool (it is registered with `optional: true`):
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"agents": {
|
||||||
|
"list": [
|
||||||
|
{
|
||||||
|
"id": "main",
|
||||||
|
"tools": { "allow": ["llm-task"] }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Config (optional)
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"plugins": {
|
||||||
|
"entries": {
|
||||||
|
"llm-task": {
|
||||||
|
"enabled": true,
|
||||||
|
"config": {
|
||||||
|
"defaultProvider": "openai-codex",
|
||||||
|
"defaultModel": "gpt-5.2",
|
||||||
|
"defaultAuthProfileId": "main",
|
||||||
|
"allowedModels": ["openai-codex/gpt-5.2"],
|
||||||
|
"maxTokens": 800,
|
||||||
|
"timeoutMs": 30000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`allowedModels` is an allowlist of `provider/model` strings. If set, any request
|
||||||
|
outside the list is rejected.
|
||||||
|
|
||||||
|
## Tool API
|
||||||
|
|
||||||
|
### Parameters
|
||||||
|
|
||||||
|
- `prompt` (string, required)
|
||||||
|
- `input` (any, optional)
|
||||||
|
- `schema` (object, optional JSON Schema)
|
||||||
|
- `provider` (string, optional)
|
||||||
|
- `model` (string, optional)
|
||||||
|
- `authProfileId` (string, optional)
|
||||||
|
- `temperature` (number, optional)
|
||||||
|
- `maxTokens` (number, optional)
|
||||||
|
- `timeoutMs` (number, optional)
|
||||||
|
|
||||||
|
### Output
|
||||||
|
|
||||||
|
Returns `details.json` containing the parsed JSON (and validates against
|
||||||
|
`schema` when provided).
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
- The tool is **JSON-only** and instructs the model to output only JSON
|
||||||
|
(no code fences, no commentary).
|
||||||
|
- No tools are exposed to the model for this run.
|
||||||
|
- Side effects should be handled outside this tool (for example, approvals in
|
||||||
|
Lobster) before calling tools that send messages/emails.
|
||||||
|
|
||||||
|
## Bundled extension note
|
||||||
|
|
||||||
|
This extension depends on Clawdbot internal modules (the embedded agent runner).
|
||||||
|
It is intended to ship as a **bundled** Clawdbot extension (like `lobster`) and
|
||||||
|
be enabled via `plugins.entries` + tool allowlists.
|
||||||
|
|
||||||
|
It is **not** currently designed to be copied into
|
||||||
|
`~/.clawdbot/extensions` as a standalone plugin directory.
|
||||||
21
extensions/llm-task/clawdbot.plugin.json
Normal file
21
extensions/llm-task/clawdbot.plugin.json
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"id": "llm-task",
|
||||||
|
"name": "LLM Task",
|
||||||
|
"description": "Generic JSON-only LLM tool for structured tasks callable from workflows.",
|
||||||
|
"configSchema": {
|
||||||
|
"type": "object",
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"defaultProvider": { "type": "string" },
|
||||||
|
"defaultModel": { "type": "string" },
|
||||||
|
"defaultAuthProfileId": { "type": "string" },
|
||||||
|
"allowedModels": {
|
||||||
|
"type": "array",
|
||||||
|
"items": { "type": "string" },
|
||||||
|
"description": "Allowlist of provider/model keys like openai-codex/gpt-5.2."
|
||||||
|
},
|
||||||
|
"maxTokens": { "type": "number" },
|
||||||
|
"timeoutMs": { "type": "number" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
7
extensions/llm-task/index.ts
Normal file
7
extensions/llm-task/index.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import type { ClawdbotPluginApi } from "../../src/plugins/types.js";
|
||||||
|
|
||||||
|
import { createLlmTaskTool } from "./src/llm-task-tool.js";
|
||||||
|
|
||||||
|
export default function register(api: ClawdbotPluginApi) {
|
||||||
|
api.registerTool(createLlmTaskTool(api), { optional: true });
|
||||||
|
}
|
||||||
11
extensions/llm-task/package.json
Normal file
11
extensions/llm-task/package.json
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"name": "@clawdbot/llm-task",
|
||||||
|
"version": "2026.1.23",
|
||||||
|
"type": "module",
|
||||||
|
"description": "Clawdbot JSON-only LLM task plugin",
|
||||||
|
"clawdbot": {
|
||||||
|
"extensions": [
|
||||||
|
"./index.ts"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
117
extensions/llm-task/src/llm-task-tool.test.ts
Normal file
117
extensions/llm-task/src/llm-task-tool.test.ts
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||||
|
|
||||||
|
vi.mock("../../../src/agents/pi-embedded-runner.js", () => {
|
||||||
|
return {
|
||||||
|
runEmbeddedPiAgent: vi.fn(async () => ({
|
||||||
|
meta: { startedAt: Date.now() },
|
||||||
|
payloads: [{ text: "{}" }],
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
import { runEmbeddedPiAgent } from "../../../src/agents/pi-embedded-runner.js";
|
||||||
|
import { createLlmTaskTool } from "./llm-task-tool.js";
|
||||||
|
|
||||||
|
function fakeApi(overrides: any = {}) {
|
||||||
|
return {
|
||||||
|
id: "llm-task",
|
||||||
|
name: "llm-task",
|
||||||
|
source: "test",
|
||||||
|
config: { agents: { defaults: { workspace: "/tmp", model: { primary: "openai-codex/gpt-5.2" } } } },
|
||||||
|
pluginConfig: {},
|
||||||
|
runtime: { version: "test" },
|
||||||
|
logger: { debug() {}, info() {}, warn() {}, error() {} },
|
||||||
|
registerTool() {},
|
||||||
|
...overrides,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("llm-task tool (json-only)", () => {
|
||||||
|
beforeEach(() => vi.clearAllMocks());
|
||||||
|
|
||||||
|
it("returns parsed json", async () => {
|
||||||
|
(runEmbeddedPiAgent as any).mockResolvedValueOnce({
|
||||||
|
meta: {},
|
||||||
|
payloads: [{ text: JSON.stringify({ foo: "bar" }) }],
|
||||||
|
});
|
||||||
|
const tool = createLlmTaskTool(fakeApi() as any);
|
||||||
|
const res = await tool.execute("id", { prompt: "return foo" });
|
||||||
|
expect((res as any).details.json).toEqual({ foo: "bar" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("strips fenced json", async () => {
|
||||||
|
(runEmbeddedPiAgent as any).mockResolvedValueOnce({
|
||||||
|
meta: {},
|
||||||
|
payloads: [{ text: "```json\n{\"ok\":true}\n```" }],
|
||||||
|
});
|
||||||
|
const tool = createLlmTaskTool(fakeApi() as any);
|
||||||
|
const res = await tool.execute("id", { prompt: "return ok" });
|
||||||
|
expect((res as any).details.json).toEqual({ ok: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("validates schema", async () => {
|
||||||
|
(runEmbeddedPiAgent as any).mockResolvedValueOnce({
|
||||||
|
meta: {},
|
||||||
|
payloads: [{ text: JSON.stringify({ foo: "bar" }) }],
|
||||||
|
});
|
||||||
|
const tool = createLlmTaskTool(fakeApi() as any);
|
||||||
|
const schema = {
|
||||||
|
type: "object",
|
||||||
|
properties: { foo: { type: "string" } },
|
||||||
|
required: ["foo"],
|
||||||
|
additionalProperties: false,
|
||||||
|
};
|
||||||
|
const res = await tool.execute("id", { prompt: "return foo", schema });
|
||||||
|
expect((res as any).details.json).toEqual({ foo: "bar" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("throws on invalid json", async () => {
|
||||||
|
(runEmbeddedPiAgent as any).mockResolvedValueOnce({ meta: {}, payloads: [{ text: "not-json" }] });
|
||||||
|
const tool = createLlmTaskTool(fakeApi() as any);
|
||||||
|
await expect(tool.execute("id", { prompt: "x" })).rejects.toThrow(/invalid json/i);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("throws on schema mismatch", async () => {
|
||||||
|
(runEmbeddedPiAgent as any).mockResolvedValueOnce({
|
||||||
|
meta: {},
|
||||||
|
payloads: [{ text: JSON.stringify({ foo: 1 }) }],
|
||||||
|
});
|
||||||
|
const tool = createLlmTaskTool(fakeApi() as any);
|
||||||
|
const schema = { type: "object", properties: { foo: { type: "string" } }, required: ["foo"] };
|
||||||
|
await expect(tool.execute("id", { prompt: "x", schema })).rejects.toThrow(/match schema/i);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("passes provider/model overrides to embedded runner", async () => {
|
||||||
|
(runEmbeddedPiAgent as any).mockResolvedValueOnce({
|
||||||
|
meta: {},
|
||||||
|
payloads: [{ text: JSON.stringify({ ok: true }) }],
|
||||||
|
});
|
||||||
|
const tool = createLlmTaskTool(fakeApi() as any);
|
||||||
|
await tool.execute("id", { prompt: "x", provider: "anthropic", model: "claude-4-sonnet" });
|
||||||
|
const call = (runEmbeddedPiAgent as any).mock.calls[0]?.[0];
|
||||||
|
expect(call.provider).toBe("anthropic");
|
||||||
|
expect(call.model).toBe("claude-4-sonnet");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("enforces allowedModels", async () => {
|
||||||
|
(runEmbeddedPiAgent as any).mockResolvedValueOnce({
|
||||||
|
meta: {},
|
||||||
|
payloads: [{ text: JSON.stringify({ ok: true }) }],
|
||||||
|
});
|
||||||
|
const tool = createLlmTaskTool(fakeApi({ pluginConfig: { allowedModels: ["openai-codex/gpt-5.2"] } }) as any);
|
||||||
|
await expect(tool.execute("id", { prompt: "x", provider: "anthropic", model: "claude-4-sonnet" })).rejects.toThrow(
|
||||||
|
/not allowed/i,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("disables tools for embedded run", async () => {
|
||||||
|
(runEmbeddedPiAgent as any).mockResolvedValueOnce({
|
||||||
|
meta: {},
|
||||||
|
payloads: [{ text: JSON.stringify({ ok: true }) }],
|
||||||
|
});
|
||||||
|
const tool = createLlmTaskTool(fakeApi() as any);
|
||||||
|
await tool.execute("id", { prompt: "x" });
|
||||||
|
const call = (runEmbeddedPiAgent as any).mock.calls[0]?.[0];
|
||||||
|
expect(call.disableTools).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
218
extensions/llm-task/src/llm-task-tool.ts
Normal file
218
extensions/llm-task/src/llm-task-tool.ts
Normal file
@ -0,0 +1,218 @@
|
|||||||
|
import os from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
|
import fs from "node:fs/promises";
|
||||||
|
|
||||||
|
import Ajv from "ajv";
|
||||||
|
import { Type } from "@sinclair/typebox";
|
||||||
|
|
||||||
|
// NOTE: This extension is intended to be bundled with Clawdbot.
|
||||||
|
// When running from source (tests/dev), Clawdbot internals live under src/.
|
||||||
|
// When running from a built install, internals live under dist/ (no src/ tree).
|
||||||
|
// So we resolve internal imports dynamically with src-first, dist-fallback.
|
||||||
|
|
||||||
|
import type { ClawdbotPluginApi } from "../../../src/plugins/types.js";
|
||||||
|
|
||||||
|
type RunEmbeddedPiAgentFn = (params: Record<string, unknown>) => Promise<unknown>;
|
||||||
|
|
||||||
|
async function loadRunEmbeddedPiAgent(): Promise<RunEmbeddedPiAgentFn> {
|
||||||
|
// Source checkout (tests/dev)
|
||||||
|
try {
|
||||||
|
const mod = await import("../../../src/agents/pi-embedded-runner.js");
|
||||||
|
if (typeof (mod as any).runEmbeddedPiAgent === "function") return (mod as any).runEmbeddedPiAgent;
|
||||||
|
} catch {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bundled install (built)
|
||||||
|
const mod = await import("../../../agents/pi-embedded-runner.js");
|
||||||
|
if (typeof (mod as any).runEmbeddedPiAgent !== "function") {
|
||||||
|
throw new Error("Internal error: runEmbeddedPiAgent not available");
|
||||||
|
}
|
||||||
|
return (mod as any).runEmbeddedPiAgent;
|
||||||
|
}
|
||||||
|
|
||||||
|
function stripCodeFences(s: string): string {
|
||||||
|
const trimmed = s.trim();
|
||||||
|
const m = trimmed.match(/^```(?:json)?\s*([\s\S]*?)\s*```$/i);
|
||||||
|
if (m) return (m[1] ?? "").trim();
|
||||||
|
return trimmed;
|
||||||
|
}
|
||||||
|
|
||||||
|
function collectText(payloads: Array<{ text?: string; isError?: boolean }> | undefined): string {
|
||||||
|
const texts = (payloads ?? [])
|
||||||
|
.filter((p) => !p.isError && typeof p.text === "string")
|
||||||
|
.map((p) => p.text ?? "");
|
||||||
|
return texts.join("\n").trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
function toModelKey(provider?: string, model?: string): string | undefined {
|
||||||
|
const p = provider?.trim();
|
||||||
|
const m = model?.trim();
|
||||||
|
if (!p || !m) return undefined;
|
||||||
|
return `${p}/${m}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
type PluginCfg = {
|
||||||
|
defaultProvider?: string;
|
||||||
|
defaultModel?: string;
|
||||||
|
defaultAuthProfileId?: string;
|
||||||
|
allowedModels?: string[];
|
||||||
|
maxTokens?: number;
|
||||||
|
timeoutMs?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function createLlmTaskTool(api: ClawdbotPluginApi) {
|
||||||
|
return {
|
||||||
|
name: "llm-task",
|
||||||
|
description:
|
||||||
|
"Run a generic JSON-only LLM task and return schema-validated JSON. Designed for orchestration from Lobster workflows via clawd.invoke.",
|
||||||
|
parameters: Type.Object({
|
||||||
|
prompt: Type.String({ description: "Task instruction for the LLM." }),
|
||||||
|
input: Type.Optional(Type.Unknown({ description: "Optional input payload for the task." })),
|
||||||
|
schema: Type.Optional(Type.Unknown({ description: "Optional JSON Schema to validate the returned JSON." })),
|
||||||
|
provider: Type.Optional(Type.String({ description: "Provider override (e.g. openai-codex, anthropic)." })),
|
||||||
|
model: Type.Optional(Type.String({ description: "Model id override." })),
|
||||||
|
authProfileId: Type.Optional(Type.String({ description: "Auth profile override." })),
|
||||||
|
temperature: Type.Optional(Type.Number({ description: "Best-effort temperature override." })),
|
||||||
|
maxTokens: Type.Optional(Type.Number({ description: "Best-effort maxTokens override." })),
|
||||||
|
timeoutMs: Type.Optional(Type.Number({ description: "Timeout for the LLM run." })),
|
||||||
|
}),
|
||||||
|
|
||||||
|
async execute(_id: string, params: Record<string, unknown>) {
|
||||||
|
const prompt = String(params.prompt ?? "");
|
||||||
|
if (!prompt.trim()) throw new Error("prompt required");
|
||||||
|
|
||||||
|
const pluginCfg = (api.pluginConfig ?? {}) as PluginCfg;
|
||||||
|
|
||||||
|
const primary = api.config?.agents?.defaults?.model?.primary;
|
||||||
|
const primaryProvider = typeof primary === "string" ? primary.split("/")[0] : undefined;
|
||||||
|
const primaryModel = typeof primary === "string" ? primary.split("/").slice(1).join("/") : undefined;
|
||||||
|
|
||||||
|
const provider =
|
||||||
|
(typeof params.provider === "string" && params.provider.trim()) ||
|
||||||
|
(typeof pluginCfg.defaultProvider === "string" && pluginCfg.defaultProvider.trim()) ||
|
||||||
|
primaryProvider ||
|
||||||
|
undefined;
|
||||||
|
|
||||||
|
const model =
|
||||||
|
(typeof params.model === "string" && params.model.trim()) ||
|
||||||
|
(typeof pluginCfg.defaultModel === "string" && pluginCfg.defaultModel.trim()) ||
|
||||||
|
primaryModel ||
|
||||||
|
undefined;
|
||||||
|
|
||||||
|
const authProfileId =
|
||||||
|
(typeof (params as any).authProfileId === "string" && (params as any).authProfileId.trim()) ||
|
||||||
|
(typeof pluginCfg.defaultAuthProfileId === "string" && pluginCfg.defaultAuthProfileId.trim()) ||
|
||||||
|
undefined;
|
||||||
|
|
||||||
|
const modelKey = toModelKey(provider, model);
|
||||||
|
if (!provider || !model || !modelKey) {
|
||||||
|
throw new Error(
|
||||||
|
`provider/model could not be resolved (provider=${String(provider ?? "")}, model=${String(model ?? "")})`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const allowed = Array.isArray(pluginCfg.allowedModels) ? pluginCfg.allowedModels : undefined;
|
||||||
|
if (allowed && allowed.length > 0 && !allowed.includes(modelKey)) {
|
||||||
|
throw new Error(
|
||||||
|
`Model not allowed by llm-task plugin config: ${modelKey}. Allowed models: ${allowed.join(", ")}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const timeoutMs =
|
||||||
|
(typeof params.timeoutMs === "number" && params.timeoutMs > 0 ? params.timeoutMs : undefined) ||
|
||||||
|
(typeof pluginCfg.timeoutMs === "number" && pluginCfg.timeoutMs > 0 ? pluginCfg.timeoutMs : undefined) ||
|
||||||
|
30_000;
|
||||||
|
|
||||||
|
const streamParams = {
|
||||||
|
temperature: typeof params.temperature === "number" ? params.temperature : undefined,
|
||||||
|
maxTokens:
|
||||||
|
typeof params.maxTokens === "number"
|
||||||
|
? params.maxTokens
|
||||||
|
: typeof pluginCfg.maxTokens === "number"
|
||||||
|
? pluginCfg.maxTokens
|
||||||
|
: undefined,
|
||||||
|
};
|
||||||
|
|
||||||
|
const input = (params as any).input as unknown;
|
||||||
|
let inputJson: string;
|
||||||
|
try {
|
||||||
|
inputJson = JSON.stringify(input ?? null, null, 2);
|
||||||
|
} catch {
|
||||||
|
throw new Error("input must be JSON-serializable");
|
||||||
|
}
|
||||||
|
|
||||||
|
const system = [
|
||||||
|
"You are a JSON-only function.",
|
||||||
|
"Return ONLY a valid JSON value.",
|
||||||
|
"Do not wrap in markdown fences.",
|
||||||
|
"Do not include commentary.",
|
||||||
|
"Do not call tools.",
|
||||||
|
].join(" ");
|
||||||
|
|
||||||
|
const fullPrompt = `${system}\n\nTASK:\n${prompt}\n\nINPUT_JSON:\n${inputJson}\n`;
|
||||||
|
|
||||||
|
let tmpDir: string | null = null;
|
||||||
|
try {
|
||||||
|
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-llm-task-"));
|
||||||
|
const sessionId = `llm-task-${Date.now()}`;
|
||||||
|
const sessionFile = path.join(tmpDir, "session.json");
|
||||||
|
|
||||||
|
const runEmbeddedPiAgent = await loadRunEmbeddedPiAgent();
|
||||||
|
|
||||||
|
const result = await runEmbeddedPiAgent({
|
||||||
|
sessionId,
|
||||||
|
sessionFile,
|
||||||
|
workspaceDir: api.config?.agents?.defaults?.workspace ?? process.cwd(),
|
||||||
|
config: api.config,
|
||||||
|
prompt: fullPrompt,
|
||||||
|
timeoutMs,
|
||||||
|
runId: `llm-task-${Date.now()}`,
|
||||||
|
provider,
|
||||||
|
model,
|
||||||
|
authProfileId,
|
||||||
|
authProfileIdSource: authProfileId ? "user" : "auto",
|
||||||
|
streamParams,
|
||||||
|
disableTools: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
const text = collectText((result as any).payloads);
|
||||||
|
if (!text) throw new Error("LLM returned empty output");
|
||||||
|
|
||||||
|
const raw = stripCodeFences(text);
|
||||||
|
let parsed: unknown;
|
||||||
|
try {
|
||||||
|
parsed = JSON.parse(raw);
|
||||||
|
} catch {
|
||||||
|
throw new Error("LLM returned invalid JSON");
|
||||||
|
}
|
||||||
|
|
||||||
|
const schema = (params as any).schema as unknown;
|
||||||
|
if (schema && typeof schema === "object" && !Array.isArray(schema)) {
|
||||||
|
const ajv = new Ajv({ allErrors: true, strict: false });
|
||||||
|
const validate = ajv.compile(schema as any);
|
||||||
|
const ok = validate(parsed);
|
||||||
|
if (!ok) {
|
||||||
|
const msg =
|
||||||
|
validate.errors?.map((e) => `${e.instancePath || "<root>"} ${e.message || "invalid"}`).join("; ") ??
|
||||||
|
"invalid";
|
||||||
|
throw new Error(`LLM JSON did not match schema: ${msg}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
content: [{ type: "text", text: JSON.stringify(parsed, null, 2) }],
|
||||||
|
details: { json: parsed, provider, model },
|
||||||
|
};
|
||||||
|
} finally {
|
||||||
|
if (tmpDir) {
|
||||||
|
try {
|
||||||
|
await fs.rm(tmpDir, { recursive: true, force: true });
|
||||||
|
} catch {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -273,6 +273,7 @@ export async function runEmbeddedPiAgent(
|
|||||||
skillsSnapshot: params.skillsSnapshot,
|
skillsSnapshot: params.skillsSnapshot,
|
||||||
prompt,
|
prompt,
|
||||||
images: params.images,
|
images: params.images,
|
||||||
|
disableTools: params.disableTools,
|
||||||
provider,
|
provider,
|
||||||
modelId,
|
modelId,
|
||||||
model,
|
model,
|
||||||
|
|||||||
@ -196,30 +196,32 @@ export async function runEmbeddedAttempt(
|
|||||||
|
|
||||||
// Check if the model supports native image input
|
// Check if the model supports native image input
|
||||||
const modelHasVision = params.model.input?.includes("image") ?? false;
|
const modelHasVision = params.model.input?.includes("image") ?? false;
|
||||||
const toolsRaw = createClawdbotCodingTools({
|
const toolsRaw = params.disableTools
|
||||||
exec: {
|
? []
|
||||||
...params.execOverrides,
|
: createClawdbotCodingTools({
|
||||||
elevated: params.bashElevated,
|
exec: {
|
||||||
},
|
...params.execOverrides,
|
||||||
sandbox,
|
elevated: params.bashElevated,
|
||||||
messageProvider: params.messageChannel ?? params.messageProvider,
|
},
|
||||||
agentAccountId: params.agentAccountId,
|
sandbox,
|
||||||
messageTo: params.messageTo,
|
messageProvider: params.messageChannel ?? params.messageProvider,
|
||||||
messageThreadId: params.messageThreadId,
|
agentAccountId: params.agentAccountId,
|
||||||
sessionKey: params.sessionKey ?? params.sessionId,
|
messageTo: params.messageTo,
|
||||||
agentDir,
|
messageThreadId: params.messageThreadId,
|
||||||
workspaceDir: effectiveWorkspace,
|
sessionKey: params.sessionKey ?? params.sessionId,
|
||||||
config: params.config,
|
agentDir,
|
||||||
abortSignal: runAbortController.signal,
|
workspaceDir: effectiveWorkspace,
|
||||||
modelProvider: params.model.provider,
|
config: params.config,
|
||||||
modelId: params.modelId,
|
abortSignal: runAbortController.signal,
|
||||||
modelAuthMode: resolveModelAuthMode(params.model.provider, params.config),
|
modelProvider: params.model.provider,
|
||||||
currentChannelId: params.currentChannelId,
|
modelId: params.modelId,
|
||||||
currentThreadTs: params.currentThreadTs,
|
modelAuthMode: resolveModelAuthMode(params.model.provider, params.config),
|
||||||
replyToMode: params.replyToMode,
|
currentChannelId: params.currentChannelId,
|
||||||
hasRepliedRef: params.hasRepliedRef,
|
currentThreadTs: params.currentThreadTs,
|
||||||
modelHasVision,
|
replyToMode: params.replyToMode,
|
||||||
});
|
hasRepliedRef: params.hasRepliedRef,
|
||||||
|
modelHasVision,
|
||||||
|
});
|
||||||
const tools = sanitizeToolsForGoogle({ tools: toolsRaw, provider: params.provider });
|
const tools = sanitizeToolsForGoogle({ tools: toolsRaw, provider: params.provider });
|
||||||
logToolSchemasForGoogle({ tools, provider: params.provider });
|
logToolSchemasForGoogle({ tools, provider: params.provider });
|
||||||
|
|
||||||
|
|||||||
@ -44,6 +44,8 @@ export type RunEmbeddedPiAgentParams = {
|
|||||||
images?: ImageContent[];
|
images?: ImageContent[];
|
||||||
/** Optional client-provided tools (OpenResponses hosted tools). */
|
/** Optional client-provided tools (OpenResponses hosted tools). */
|
||||||
clientTools?: ClientToolDefinition[];
|
clientTools?: ClientToolDefinition[];
|
||||||
|
/** Disable built-in tools for this run (LLM-only mode). */
|
||||||
|
disableTools?: boolean;
|
||||||
provider?: string;
|
provider?: string;
|
||||||
model?: string;
|
model?: string;
|
||||||
authProfileId?: string;
|
authProfileId?: string;
|
||||||
|
|||||||
@ -36,6 +36,8 @@ export type EmbeddedRunAttemptParams = {
|
|||||||
images?: ImageContent[];
|
images?: ImageContent[];
|
||||||
/** Optional client-provided tools (OpenResponses hosted tools). */
|
/** Optional client-provided tools (OpenResponses hosted tools). */
|
||||||
clientTools?: ClientToolDefinition[];
|
clientTools?: ClientToolDefinition[];
|
||||||
|
/** Disable built-in tools for this run (LLM-only mode). */
|
||||||
|
disableTools?: boolean;
|
||||||
provider: string;
|
provider: string;
|
||||||
modelId: string;
|
modelId: string;
|
||||||
model: Model<Api>;
|
model: Model<Api>;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user