@kenkaiiii/gg-agent 5.92.0 → 5.93.0

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.
Files changed (3) hide show
  1. package/NOTICE +1 -1
  2. package/README.md +112 -66
  3. package/package.json +18 -4
package/NOTICE CHANGED
@@ -1,5 +1,5 @@
1
1
  GG Coder
2
- Copyright (C) 2025-2026 KenKai
2
+ Copyright (C) 2025-2026 Ken Kai
3
3
 
4
4
  This program is free software: you can redistribute it and/or modify it under
5
5
  the terms of the GNU Affero General Public License as published by the Free
package/README.md CHANGED
@@ -1,96 +1,142 @@
1
- # @kenkaiiii/gg-agent
1
+ <h1 align="center">@kenkaiiii/gg-agent</h1>
2
2
 
3
3
  <p align="center">
4
- <strong>Agent loop with multi-turn tool execution. Build agents that think, act, and loop.</strong>
4
+ <strong>The agent loop: give a model tools, and it works until the job is done.</strong>
5
5
  </p>
6
6
 
7
7
  <p align="center">
8
- <a href="https://www.npmjs.com/package/@kenkaiiii/gg-agent"><img src="https://img.shields.io/npm/v/@kenkaiiii/gg-agent?style=for-the-badge" alt="npm version"></a>
9
- <a href="../../LICENSE"><img src="https://img.shields.io/badge/License-AGPL--3.0-blue.svg?style=for-the-badge" alt="AGPL-3.0 License"></a>
8
+ <a href="https://www.npmjs.com/package/@kenkaiiii/gg-agent"><img src="https://img.shields.io/npm/v/@kenkaiiii/gg-agent?style=for-the-badge&color=blue" alt="npm version"></a>
9
+ <a href="https://www.npmjs.com/package/@kenkaiiii/gg-agent"><img src="https://img.shields.io/npm/dm/@kenkaiiii/gg-agent?style=for-the-badge&color=2ecc71" alt="npm downloads per month"></a>
10
+ <a href="https://github.com/KenKaiii/gg-framework/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-AGPL--3.0-blue.svg?style=for-the-badge" alt="AGPL-3.0 License"></a>
10
11
  </p>
11
12
 
12
- Give an LLM tools. It calls them. Results go back in. It loops until it's done. That's it.
13
+ gg-agent calls the model, runs the tools it asks for, sends the results back, and repeats until the model stops. It handles retries, cancellation, steering mid-run, and context that grows too long. It works with every provider in [`@kenkaiiii/gg-ai`](https://www.npmjs.com/package/@kenkaiiii/gg-ai).
13
14
 
14
- Built on top of [`@kenkaiiii/gg-ai`](../gg-ai/README.md). Part of the [GG Framework](../../README.md) monorepo.
15
-
16
- ---
15
+ It's the loop that runs [GG Coder](https://github.com/KenKaiii/gg-framework), a desktop coding agent.
17
16
 
18
17
  ## Install
19
18
 
20
19
  ```bash
21
- npm i @kenkaiiii/gg-agent
20
+ npm install @kenkaiiii/gg-agent @kenkaiiii/gg-ai zod
22
21
  ```
23
22
 
24
- ---
25
-
26
- ## How it works
27
-
28
- Create an `Agent` with a provider, model, and tools. Call `agent.prompt()` to start a conversation.
29
-
30
- - **`for await`** gives you streaming events (`text_delta`, `tool_call_start`, `tool_call_end`, `agent_done`, etc.)
31
- - **`await`** gives you the final result (`message`, `totalTurns`, `totalUsage`)
32
-
33
- Same dual-nature pattern as `@kenkaiiii/gg-ai`. The `Agent` class maintains conversation history — each `prompt()` call continues the conversation.
23
+ Needs Node.js 22 or newer. Ships ESM and CommonJS builds with TypeScript types.
24
+
25
+ ## Quick start
26
+
27
+ ```ts
28
+ import { Agent, type AgentTool } from "@kenkaiiii/gg-agent";
29
+ import { z } from "zod";
30
+
31
+ const WeatherParams = z.object({ city: z.string() });
32
+
33
+ const getWeather: AgentTool<typeof WeatherParams> = {
34
+ name: "get_weather",
35
+ description: "Current weather for a city",
36
+ parameters: WeatherParams,
37
+ async execute({ city }) {
38
+ return `22°C and sunny in ${city}`;
39
+ },
40
+ };
41
+
42
+ const agent = new Agent({
43
+ provider: "anthropic",
44
+ model: "claude-sonnet-5-5",
45
+ apiKey: process.env.ANTHROPIC_API_KEY,
46
+ system: "You are a helpful assistant.",
47
+ tools: [getWeather],
48
+ });
49
+
50
+ // Iterate for live events...
51
+ const run = agent.prompt("Should I pack an umbrella for Paris?");
52
+ for await (const event of run) {
53
+ if (event.type === "text_delta") process.stdout.write(event.text);
54
+ if (event.type === "tool_call_start") console.log(`\n→ ${event.name}`, event.args);
55
+ }
56
+
57
+ // ...and await for the result.
58
+ const { message, totalTurns, totalUsage } = await run;
59
+ ```
34
60
 
35
- For full control, use `agentLoop()` directly — a pure async generator that takes a messages array and options.
61
+ Each `prompt()` continues the same conversation. `agent.getMessages()` returns the history so you can save it, and `priorMessages` restores it later.
36
62
 
37
- ### Tools
63
+ For full control without the class, `agentLoop(messages, options)` is the same loop as a plain async generator.
38
64
 
39
- Define tools with a name, description, Zod schema for parameters, and an `execute` function. The execute function receives typed args and a `ToolContext` with `signal`, `toolCallId`, and `onUpdate`.
65
+ ## Tools
40
66
 
41
- Return a string, or a `{ content, details }` object for structured results. If `execute` throws, the error becomes a tool result (not a crash). The agent sees the error and can retry or adjust.
67
+ A tool has a `name`, a `description`, a Zod schema for `parameters` and an `execute` function. `execute` gets the parsed arguments and a context with `signal`, `toolCallId` and `onUpdate` (for progress events).
42
68
 
43
- ### Safety
69
+ - **Return** a string, or `{ content, details }` when you want to keep structured data alongside what the model sees.
70
+ - **Throwing is fine.** The error becomes the tool result and the model can try again or change course.
71
+ - **Bad arguments** are caught by the schema and returned to the model to fix.
72
+ - **Parallel by default.** Tool calls from one turn run together. Set `executionMode: "sequential"` on a tool that changes state, and that batch runs in order.
73
+ - **Timeouts.** Each tool call times out after 5 minutes; set `timeoutMs` on a tool that needs longer.
44
74
 
45
- - `maxTurns` (default: 100) prevents runaway loops
46
- - `AbortSignal` support for cancellation
47
- - Zod validation on tool args
48
- - `maxContinuations` (default: 5) caps consecutive `pause_turn` continuations
75
+ ## While it runs
49
76
 
50
- ---
77
+ | Method | What it does |
78
+ | ---------------------------- | -------------------------------------------------------------------------------------------------- |
79
+ | `agent.steer(message)` | Add a message mid-run. Running tools that can be interrupted stop, and the model sees it next turn |
80
+ | `agent.followUp(message)` | Queue a message for when the agent would otherwise stop |
81
+ | `signal` (`AbortController`) | Stop the run. Call `agent.setSignal()` with a fresh signal before the next prompt |
51
82
 
52
83
  ## Events
53
84
 
54
- | Event | Description |
55
- | -------------------- | --------------------------------------------- |
56
- | `text_delta` | Incremental text output |
57
- | `thinking_delta` | Extended thinking output |
58
- | `tool_call_start` | Tool invocation started (name, args) |
59
- | `tool_call_update` | Progress update from a running tool |
60
- | `tool_call_end` | Tool finished (result, duration, isError) |
61
- | `server_tool_call` | Server-side tool invocation |
62
- | `server_tool_result` | Server-side tool result |
63
- | `turn_end` | One LLM call completed (stop reason, usage) |
64
- | `agent_done` | All turns finished (total turns, total usage) |
65
- | `error` | Fatal error |
66
-
67
- ---
85
+ | Event | When |
86
+ | ----------------------- | ------------------------------------------------------------- |
87
+ | `text_delta` | New text |
88
+ | `thinking_delta` | New reasoning text |
89
+ | `toolcall_delta` | Tool call arguments as they stream in |
90
+ | `tool_call_start` | A tool started (`name`, `args`) |
91
+ | `tool_call_update` | Progress from a running tool |
92
+ | `tool_call_end` | A tool finished (`result`, `isError`, `durationMs`) |
93
+ | `server_tool_call` | The provider ran a server-side tool |
94
+ | `server_tool_result` | Result of that server-side tool |
95
+ | `steering_message` | A steering message was added |
96
+ | `follow_up_message` | A follow-up message was added |
97
+ | `retry` | A failed model call is being retried |
98
+ | `turn_end` | One model call finished (`stopReason`, `usage`, `timing`) |
99
+ | `checkpoint` | A safe point to save the conversation |
100
+ | `truncated` | A turn ended early (output limit, refusal, provider error) |
101
+ | `max_turns` | The turn limit was reached before the model finished |
102
+ | `turn_budget_extended` | `onTurnBudgetExhausted` granted more turns |
103
+ | `stream_rule_triggered` | A `streamRules` pattern matched and the step is being retried |
104
+ | `agent_done` | Finished (`totalTurns`, `totalUsage`) |
105
+ | `error` | The run failed |
68
106
 
69
107
  ## Options
70
108
 
71
- | Option | Type | Description |
72
- | ------------------ | ------------------------------------------------ | -------------------------------------------- |
73
- | `provider` | `"anthropic" \| "openai" \| "glm" \| "moonshot"` | Required |
74
- | `model` | `string` | Required |
75
- | `system` | `string` | System prompt |
76
- | `tools` | `AgentTool[]` | Tools with Zod schemas and execute functions |
77
- | `serverTools` | `ServerToolDefinition[]` | Server-side tool definitions |
78
- | `maxTurns` | `number` | Max LLM calls (default: 100) |
79
- | `maxTokens` | `number` | Max output tokens per turn |
80
- | `temperature` | `number` | Sampling temperature |
81
- | `thinking` | `"low" \| "medium" \| "high" \| "max"` | Extended thinking |
82
- | `apiKey` | `string` | Provider API key |
83
- | `baseUrl` | `string` | Custom endpoint |
84
- | `signal` | `AbortSignal` | Cancellation |
85
- | `cacheRetention` | `"none" \| "short" \| "long"` | Prompt cache preference |
86
- | `compaction` | `boolean` | Server-side compaction (Anthropic only) |
87
- | `maxContinuations` | `number` | Max pause_turn continuations (default: 5) |
88
- | `transformContext` | `(messages) => messages` | Transform messages before each LLM call |
89
-
90
- `transformContext` is called before each LLM call. Use it for compaction, truncation, or injecting dynamic context.
91
-
92
- ---
109
+ Every [`stream()` option from gg-ai](https://www.npmjs.com/package/@kenkaiiii/gg-ai) that makes sense per call (`apiKey`, `baseUrl`, `thinking`, `maxTokens`, `temperature`, `cacheRetention`, `webSearch`, `providerOptions`, `fetch` and so on) is passed through. On top of those:
110
+
111
+ | Option | Type | What it does |
112
+ | ----------------------- | -------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
113
+ | `provider` | `Provider` | **Required.** Any gg-ai provider |
114
+ | `model` | `string` | **Required.** |
115
+ | `system` | `string` | System prompt |
116
+ | `tools` | `AgentTool[]` | Your tools |
117
+ | `priorMessages` | `Message[]` | Conversation to resume from |
118
+ | `maxTurns` | `number` | Model calls per run (default 300) |
119
+ | `onTurnBudgetExhausted` | `(ctx) => boolean \| Promise<boolean>` | Return true to grant more turns when the limit is hit mid-task |
120
+ | `transformContext` | `(messages, options) => Message[]` | Runs before every model call: trim, summarize or add context. `options.force` means the provider reported the context is full |
121
+ | `resolveCredentials` | `() => Promise<{ apiKey }>` | Fetch a fresh key or token each turn, so long runs survive token refreshes |
122
+ | `resolveTool` | `(name) => AgentTool \| undefined` | Load a tool on demand when the model calls one you haven't passed yet |
123
+ | `maxToolResultChars` | `number` | Cut any single tool result longer than this |
124
+ | `streamRules` | `StreamRulesConfig` | Regex rules on streamed output; a match discards the step and retries with a reminder |
125
+ | `maxContinuations` | `number` | Limit on back-to-back `pause_turn` continuations (default 5) |
126
+
127
+ The full list is the `AgentOptions` type.
128
+
129
+ ## The GG Framework
130
+
131
+ | Package | What it does |
132
+ | ---------------------------------------------------------------------- | ----------------------------------------------------------------- |
133
+ | [@kenkaiiii/gg-ai](https://www.npmjs.com/package/@kenkaiiii/gg-ai) | One streaming API for every provider |
134
+ | **@kenkaiiii/gg-agent** | The agent loop (this package) |
135
+ | [@kenkaiiii/gg-core](https://www.npmjs.com/package/@kenkaiiii/gg-core) | Model list, sign-in and stored credentials, local model discovery |
136
+ | [@kenkaiiii/ggcoder](https://www.npmjs.com/package/@kenkaiiii/ggcoder) | The GG Coder coding agent: CLI, tools and sessions |
137
+
138
+ Source, issues and the desktop app: [github.com/KenKaiii/gg-framework](https://github.com/KenKaiii/gg-framework).
93
139
 
94
140
  ## License
95
141
 
96
- AGPL-3.0-only. See [LICENSE](../../LICENSE).
142
+ [AGPL-3.0-only](https://github.com/KenKaiii/gg-framework/blob/main/LICENSE). You can build on it; if you distribute your changes or run them as a network service, share their source under the same license.
package/package.json CHANGED
@@ -1,17 +1,31 @@
1
1
  {
2
2
  "name": "@kenkaiiii/gg-agent",
3
- "version": "5.92.0",
3
+ "version": "5.93.0",
4
4
  "type": "module",
5
- "description": "Agentic loop system with tool execution for LLMs",
5
+ "description": "Agent loop for LLMs: runs your tools, feeds results back and loops until done, with retries, steering and cancellation. Works with every gg-ai provider.",
6
+ "author": "Ken Kai",
7
+ "keywords": [
8
+ "llm",
9
+ "ai",
10
+ "agent",
11
+ "agent-loop",
12
+ "tool-calling",
13
+ "anthropic",
14
+ "openai"
15
+ ],
6
16
  "license": "AGPL-3.0-only",
7
17
  "engines": {
8
18
  "node": ">=22"
9
19
  },
10
20
  "repository": {
11
21
  "type": "git",
12
- "url": "git+https://github.com/kenkaiiii/gg-framework.git",
22
+ "url": "git+https://github.com/KenKaiii/gg-framework.git",
13
23
  "directory": "packages/gg-agent"
14
24
  },
25
+ "homepage": "https://github.com/KenKaiii/gg-framework/tree/main/packages/gg-agent#readme",
26
+ "bugs": {
27
+ "url": "https://github.com/KenKaiii/gg-framework/issues"
28
+ },
15
29
  "exports": {
16
30
  ".": {
17
31
  "types": "./dist/index.d.ts",
@@ -28,7 +42,7 @@
28
42
  ],
29
43
  "dependencies": {
30
44
  "zod": "^4.5.4",
31
- "@kenkaiiii/gg-ai": "5.92.0"
45
+ "@kenkaiiii/gg-ai": "5.93.0"
32
46
  },
33
47
  "devDependencies": {
34
48
  "typescript": "^6.0.3",