@sctg/cline-agents 3.84.0-beta.20260524130712

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,278 @@
1
+ # [experimental] @cline/agents
2
+
3
+ `@cline/agents` is the runtime-agnostic agent loop package in the Cline SDK.
4
+ It gives you the core primitives for building tool-using LLM agents without
5
+ bringing in session storage, hub transport, or host-specific default tools.
6
+
7
+ ## What You Get
8
+
9
+ - `Agent` / `AgentRuntime` — the same class under two names — for running and
10
+ continuing tool-using agent conversations
11
+ - `createAgent` / `createAgentRuntime` — factory-function equivalents
12
+ - `AgentRuntimeHooks` for lifecycle interception (`beforeRun`, `afterRun`,
13
+ `beforeModel`, `afterModel`, `beforeTool`, `afterTool`, `onEvent`)
14
+ - Event streaming via `agent.subscribe(listener)` and the `hooks.onEvent`
15
+ callback
16
+ - Plugin setup callbacks for contributing tools and hooks at boot
17
+
18
+ ## What This Package Does Not Include
19
+
20
+ `@cline/agents` does not ship a full application runtime by itself.
21
+
22
+ - Default host tools like filesystem access, shell execution, or web fetching live in `@cline/core`
23
+ - Session persistence and stateful orchestration live in `@cline/core`
24
+ - Shared hub runtime/session transport lives in `@cline/core` (see `@cline/core/hub`)
25
+ - Sub-agent and team coordination primitives live in `@cline/core`
26
+
27
+ That split keeps this package usable in Node, browser, and custom host
28
+ environments where you want to supply your own tools and runtime policy.
29
+
30
+ ## Installation
31
+
32
+ ```bash
33
+ npm install @cline/agents @cline/shared @cline/llms
34
+ ```
35
+
36
+ ## Quick Start
37
+
38
+ ```ts
39
+ import { Agent } from "@cline/agents";
40
+ import type { AgentTool } from "@cline/shared";
41
+
42
+ const getWeather: AgentTool<{ city: string }, { forecast: string }> = {
43
+ name: "get_weather",
44
+ description: "Return the current weather for a city.",
45
+ inputSchema: {
46
+ type: "object",
47
+ properties: { city: { type: "string" } },
48
+ required: ["city"],
49
+ },
50
+ async execute({ city }) {
51
+ return { forecast: `sunny in ${city}` };
52
+ },
53
+ };
54
+
55
+ const agent = new Agent({
56
+ providerId: "anthropic",
57
+ modelId: "claude-sonnet-4-6",
58
+ apiKey: process.env.ANTHROPIC_API_KEY,
59
+ systemPrompt: "You are a concise assistant.",
60
+ tools: [getWeather],
61
+ });
62
+
63
+ const result = await agent.run("What's the weather in San Francisco?");
64
+ console.log(result.outputText);
65
+ ```
66
+
67
+ ## Two Ways to Configure
68
+
69
+ `Agent` / `AgentRuntime` accepts two config shapes:
70
+
71
+ **Provider form** — friendly entrypoint. The runtime builds an `AgentModel` for
72
+ you via `@cline/llms`:
73
+
74
+ ```ts
75
+ new Agent({
76
+ providerId: "openai",
77
+ modelId: "gpt-5",
78
+ apiKey: process.env.OPENAI_API_KEY,
79
+ // baseUrl, headers also supported
80
+ tools: [/* ... */],
81
+ });
82
+ ```
83
+
84
+ **Model form** — advanced. Supply a pre-built `AgentModel` directly. Useful
85
+ when the host already owns gateway construction (this is what `@cline/core`
86
+ uses internally):
87
+
88
+ ```ts
89
+ import { createGateway } from "@cline/llms";
90
+
91
+ const gateway = createGateway({ providerConfigs: [/* ... */] });
92
+ const model = gateway.createAgentModel({ providerId, modelId });
93
+
94
+ new Agent({
95
+ model,
96
+ tools: [/* ... */],
97
+ });
98
+ ```
99
+
100
+ ## Core Concepts
101
+
102
+ ### Tools
103
+
104
+ Tools conform to the `AgentTool<TInput, TOutput>` interface from
105
+ `@cline/shared`. Each tool has a JSON Schema `inputSchema` and an
106
+ `execute(input, context)` function that returns the tool output directly:
107
+
108
+ ```ts
109
+ import type { AgentTool } from "@cline/shared";
110
+
111
+ const summarize: AgentTool<{ text: string }, { summary: string }> = {
112
+ name: "summarize_text",
113
+ description: "Summarize text into a short preview.",
114
+ inputSchema: {
115
+ type: "object",
116
+ properties: { text: { type: "string" } },
117
+ required: ["text"],
118
+ },
119
+ async execute({ text }, context) {
120
+ // context.signal — aborts when the run is cancelled
121
+ // context.emitUpdate(...) — stream progress as `tool-updated` events
122
+ return { summary: text.slice(0, 120) };
123
+ },
124
+ };
125
+ ```
126
+
127
+ The runtime wraps successful tool outputs in an internal tool-result message.
128
+ Throw from `execute(...)` to report a tool failure, or use an `afterTool` hook
129
+ to transform the internal `AgentToolResult` envelope.
130
+
131
+ ### Events
132
+
133
+ Subscribe to the `AgentRuntimeEvent` stream in one of two ways:
134
+
135
+ ```ts
136
+ // 1. Attach a listener after construction. Returns an unsubscribe function.
137
+ const unsubscribe = agent.subscribe((event) => {
138
+ if (event.type === "assistant-text-delta") {
139
+ process.stdout.write(event.text);
140
+ }
141
+ });
142
+
143
+ // 2. Register an `onEvent` hook at construction time.
144
+ new Agent({
145
+ providerId,
146
+ modelId,
147
+ apiKey,
148
+ hooks: {
149
+ onEvent(event) {
150
+ // fires for every runtime event
151
+ },
152
+ },
153
+ });
154
+ ```
155
+
156
+ `AgentRuntimeEvent` covers run/turn boundaries, assistant text and reasoning
157
+ deltas, tool lifecycle, usage updates, and run completion/failure. See
158
+ `AgentRuntimeEvent` in `@cline/shared` for the full union.
159
+
160
+ ### Conversation Control
161
+
162
+ - `agent.run(input)` — start a run. `input` may be a string, an `AgentMessage`,
163
+ or an array of messages. Also accepts `undefined` to continue without adding
164
+ a new user turn.
165
+ - `agent.continue(input?)` — convenience alias for `run(input?)`.
166
+ - `agent.abort(reason?)` — cancel the active run. `.run()` resolves with
167
+ `status: "aborted"`.
168
+ - `agent.snapshot()` — immutable view of the current
169
+ `AgentRuntimeStateSnapshot` (messages, usage, iteration, status, etc.).
170
+ - `agent.restore(messages)` — replace the conversation with a persisted
171
+ message array. Resets run/turn state but preserves subscribers, tools,
172
+ hooks, plugins, and the model.
173
+ - `initialMessages` in the constructor seeds the conversation on boot.
174
+
175
+ ### Hooks
176
+
177
+ Pass a `hooks` bag (`AgentRuntimeHooks`) to observe or influence the loop.
178
+ All hooks may be async; any that return `{ stop: true, reason }` will halt the
179
+ run with an `aborted` status.
180
+
181
+ ```ts
182
+ new Agent({
183
+ providerId,
184
+ modelId,
185
+ apiKey,
186
+ tools: [/* ... */],
187
+ hooks: {
188
+ beforeModel({ request }) {
189
+ // mutate messages/tools/options before the model call
190
+ return { options: { temperature: 0.2 } };
191
+ },
192
+ beforeTool({ tool, input }) {
193
+ // block a tool call based on policy
194
+ if (tool.name === "get_weather" && !(input as { city?: string }).city) {
195
+ return { skip: true, reason: "city required" };
196
+ }
197
+ return undefined;
198
+ },
199
+ afterRun({ result }) {
200
+ console.log("done", result.usage);
201
+ },
202
+ },
203
+ });
204
+ ```
205
+
206
+ For richer, host-side hook orchestration (15-stage `HookEngine`,
207
+ subprocess-backed hooks, MCP extensions), use `@cline/core`.
208
+
209
+ ### Plugins
210
+
211
+ Plugins can contribute tools and hooks at setup time:
212
+
213
+ ```ts
214
+ import type { AgentRuntimePlugin } from "@cline/shared";
215
+
216
+ const loggingPlugin: AgentRuntimePlugin = {
217
+ name: "logging",
218
+ setup({ agentId }) {
219
+ return {
220
+ hooks: {
221
+ afterTool({ tool, result }) {
222
+ console.log(agentId, tool.name, result.isError);
223
+ return undefined; // hook may return an AgentAfterToolResult
224
+ },
225
+ },
226
+ };
227
+ },
228
+ };
229
+
230
+ new Agent({
231
+ providerId,
232
+ modelId,
233
+ apiKey,
234
+ plugins: [loggingPlugin],
235
+ });
236
+ ```
237
+
238
+ ### Teams and Spawn
239
+
240
+ For multi-agent workflows, use `@cline/core`:
241
+
242
+ ```ts
243
+ import {
244
+ createSpawnAgentTool,
245
+ AgentTeamsRuntime,
246
+ createAgentTeamsTools,
247
+ bootstrapAgentTeams,
248
+ } from "@cline/core";
249
+ ```
250
+
251
+ These helpers provide coordination primitives for delegated runs,
252
+ mailboxes, task management, and outcome convergence.
253
+
254
+ ## Entry Point
255
+
256
+ - `@cline/agents` — the single package entrypoint. The `package.json`
257
+ `exports` map automatically serves a browser-safe bundle when bundlers
258
+ resolve the `browser` condition.
259
+
260
+ ## Related Packages
261
+
262
+ - `@cline/shared`: shared types (`AgentTool`, `AgentMessage`,
263
+ `AgentRuntimeEvent`, `AgentRuntimeHooks`, etc.)
264
+ - `@cline/llms`: provider settings, model catalogs, and gateway/handler
265
+ creation
266
+ - `@cline/core`: stateful runtime assembly, storage, default tools,
267
+ subprocess hooks, hub transport, and MCP integration
268
+
269
+ ## More Examples
270
+
271
+ - Repo examples:
272
+ [examples/plugins](https://github.com/cline/sdk/tree/main/examples/plugins),
273
+ [examples/hooks](https://github.com/cline/sdk/tree/main/examples/hooks),
274
+ [examples/cron](https://github.com/cline/sdk/tree/main/examples/cron)
275
+ - Workspace overview: [README.md](https://github.com/cline/sdk/blob/main/README.md)
276
+ - API and architecture references:
277
+ [DOC.md](https://github.com/cline/sdk/blob/main/DOC.md),
278
+ [ARCHITECTURE.md](https://github.com/cline/sdk/blob/main/ARCHITECTURE.md)
@@ -0,0 +1,99 @@
1
+ import type { AgentMessage, AgentModel, AgentRunResult, AgentRuntimeEvent, AgentRuntimeStateSnapshot, AgentRuntimeConfig as BaseAgentRuntimeConfig } from "@cline/shared";
2
+ export type AgentRunInput = string | AgentMessage | readonly AgentMessage[];
3
+ export type AgentEventListener = (event: AgentRuntimeEvent) => void;
4
+ /**
5
+ * Advanced form: caller supplies a pre-built `AgentModel`. Used by
6
+ * `@cline/core`, which constructs models itself to share gateway/telemetry
7
+ * wiring with the rest of the session runtime.
8
+ */
9
+ export interface AgentRuntimeConfigWithModel extends BaseAgentRuntimeConfig {
10
+ model: AgentModel;
11
+ }
12
+ /**
13
+ * Friendly form: caller supplies provider/model IDs and credentials, and the
14
+ * runtime builds an `AgentModel` internally via `@cline/llms`. This is the
15
+ * entry point most standalone users want.
16
+ */
17
+ export interface AgentRuntimeConfigWithProvider extends Omit<BaseAgentRuntimeConfig, "model"> {
18
+ /** Provider ID (e.g., "anthropic", "openai") */
19
+ providerId: string;
20
+ /** Model ID to use */
21
+ modelId: string;
22
+ /** API key for the provider */
23
+ apiKey?: string;
24
+ /** Custom base URL for the API */
25
+ baseUrl?: string;
26
+ /** Additional headers for API requests */
27
+ headers?: Record<string, string>;
28
+ }
29
+ /**
30
+ * Config accepted by `new AgentRuntime(...)` / `createAgentRuntime(...)` /
31
+ * `new Agent(...)` / `createAgent(...)`. Either supply a pre-built `model`
32
+ * (advanced) or `providerId` + `modelId` (+ credentials) and the runtime will
33
+ * construct the model itself via `@cline/llms`.
34
+ */
35
+ export type AgentRuntimeConfig = AgentRuntimeConfigWithModel | AgentRuntimeConfigWithProvider;
36
+ export declare class AgentRuntime {
37
+ private config;
38
+ private readonly listeners;
39
+ private readonly tools;
40
+ private hooks;
41
+ private readonly state;
42
+ private initialization?;
43
+ private abortController?;
44
+ constructor(config: AgentRuntimeConfig);
45
+ run(input: AgentRunInput): Promise<AgentRunResult>;
46
+ continue(input?: AgentRunInput): Promise<AgentRunResult>;
47
+ abort(reason?: unknown): void;
48
+ subscribe(listener: AgentEventListener): () => void;
49
+ /**
50
+ * Replace the conversation with a fresh set of messages, discarding any
51
+ * in-flight run and usage state while preserving the underlying model,
52
+ * tools, hooks, plugins, and active event subscribers.
53
+ *
54
+ * Useful for standalone callers that persist conversations externally and
55
+ * want to re-seed the runtime from storage without recreating subscribers.
56
+ */
57
+ restore(messages: readonly AgentMessage[]): void;
58
+ snapshot(): AgentRuntimeStateSnapshot;
59
+ private ensureInitialized;
60
+ private initialize;
61
+ private registerHooks;
62
+ private getRequiredCompletionToolNames;
63
+ private getCompletionToolReminderMessage;
64
+ private getCompletionReminderMessages;
65
+ private addUserReminderMessage;
66
+ private execute;
67
+ private callBeforeRunHooks;
68
+ private callAfterRunHooks;
69
+ private generateAssistantMessage;
70
+ private prepareTurnForModelRequest;
71
+ private consumePendingUserMessage;
72
+ private updateUsage;
73
+ private executeToolCalls;
74
+ private findCompletingToolMessage;
75
+ private prepareToolExecution;
76
+ private requestToolApproval;
77
+ private executePreparedTool;
78
+ private finishRun;
79
+ private findLastAssistantMessage;
80
+ private throwIfAborted;
81
+ private normalizeAbortError;
82
+ private emit;
83
+ private applyStopControl;
84
+ }
85
+ export declare function createAgentRuntime(config: AgentRuntimeConfig): AgentRuntime;
86
+ /**
87
+ * `Agent` is the user-friendly name for `AgentRuntime`. They are the same
88
+ * class; this alias exists so standalone callers can write:
89
+ *
90
+ * const agent = new Agent({ providerId, modelId, apiKey });
91
+ * await agent.run("hello");
92
+ *
93
+ * while `@cline/core` (which owns model construction) continues to use
94
+ * the `AgentRuntime` name with `{ model, ... }` configs.
95
+ */
96
+ export declare const Agent: typeof AgentRuntime;
97
+ export type Agent = AgentRuntime;
98
+ export declare function createAgent(config: AgentRuntimeConfig): AgentRuntime;
99
+ //# sourceMappingURL=agent-runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-runtime.d.ts","sourceRoot":"","sources":["../src/agent-runtime.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAIX,YAAY,EAEZ,UAAU,EAGV,cAAc,EACd,iBAAiB,EAEjB,yBAAyB,EAOzB,kBAAkB,IAAI,sBAAsB,EAI5C,MAAM,eAAe,CAAC;AAavB,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,YAAY,GAAG,SAAS,YAAY,EAAE,CAAC;AAC5E,MAAM,MAAM,kBAAkB,GAAG,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;AAEpE;;;;GAIG;AACH,MAAM,WAAW,2BAA4B,SAAQ,sBAAsB;IAC1E,KAAK,EAAE,UAAU,CAAC;CAClB;AAED;;;;GAIG;AACH,MAAM,WAAW,8BAChB,SAAQ,IAAI,CAAC,sBAAsB,EAAE,OAAO,CAAC;IAC7C,gDAAgD;IAChD,UAAU,EAAE,MAAM,CAAC;IACnB,sBAAsB;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,+BAA+B;IAC/B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,kCAAkC;IAClC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,0CAA0C;IAC1C,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACjC;AA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AAI;IAwDlB,OAAO,CAAC,gBAAgB;CAWxB;AAkHD,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,kBAAkB,GAAG,YAAY,CAE3E;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,KAAK,qBAAe,CAAC;AAClC,MAAM,MAAM,KAAK,GAAG,YAAY,CAAC;AAEjC,wBAAgB,WAAW,CAAC,MAAM,EAAE,kBAAkB,GAAG,YAAY,CAEpE"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @cline/agents
3
+ *
4
+ * Browser-safe agent runtime for the next-generation Cline SDK.
5
+ *
6
+ * Exports:
7
+ * - `AgentRuntime` / `Agent` — the agentic loop class (two names for the
8
+ * same class). Use `Agent` when supplying provider/model IDs, or
9
+ * `AgentRuntime` when supplying a pre-built `AgentModel`.
10
+ * - `createAgentRuntime` / `createAgent` — factory-function equivalents.
11
+ * - `AgentRuntimeConfig` and its two variants (`AgentRuntimeConfigWithModel`,
12
+ * `AgentRuntimeConfigWithProvider`) — the discriminated config union.
13
+ * - `AgentRunInput` / `AgentEventListener` — convenience type aliases.
14
+ * - `createTool` — re-exported from `@cline/shared` for authoring tools.
15
+ *
16
+ * Shared types (`AgentMessage`, `AgentRunResult`, etc.) should be imported
17
+ * directly from `@cline/shared`.
18
+ */
19
+ export type { AgentAfterToolResult, AgentBeforeModelResult, AgentBeforeToolResult, AgentMessage, AgentMessagePart, AgentModel, AgentModelFinishReason, AgentModelRequest, AgentRunResult, AgentRuntimeConfig as BaseAgentRuntimeConfig, AgentRuntimeEvent, AgentRuntimeHooks, AgentRuntimeStateSnapshot, AgentStopControl, AgentTool, AgentToolCallPart, AgentToolDefinition, AgentToolResult, AgentUsage, ToolApprovalResult, ToolPolicy, } from "@cline/shared";
20
+ export { createTool } from "@cline/shared";
21
+ export type { AgentEventListener, AgentRunInput, AgentRuntimeConfig, AgentRuntimeConfigWithModel, AgentRuntimeConfigWithProvider, } from "./agent-runtime";
22
+ export { Agent, AgentRuntime, createAgent, createAgentRuntime, } from "./agent-runtime";
23
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,YAAY,EACX,oBAAoB,EACpB,sBAAsB,EACtB,qBAAqB,EACrB,YAAY,EACZ,gBAAgB,EAChB,UAAU,EACV,sBAAsB,EACtB,iBAAiB,EACjB,cAAc,EACd,kBAAkB,IAAI,sBAAsB,EAC5C,iBAAiB,EACjB,iBAAiB,EACjB,yBAAyB,EACzB,gBAAgB,EAChB,SAAS,EACT,iBAAiB,EACjB,mBAAmB,EACnB,eAAe,EACf,UAAU,EACV,kBAAkB,EAClB,UAAU,GACV,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,YAAY,EACX,kBAAkB,EAClB,aAAa,EACb,kBAAkB,EAClB,2BAA2B,EAC3B,8BAA8B,GAC9B,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACN,KAAK,EACL,YAAY,EACZ,WAAW,EACX,kBAAkB,GAClB,MAAM,iBAAiB,CAAC"}