@narimangardi/agent-loop 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nariman Gardi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # agent-loop
2
+
3
+ A tiny, zero-dependency, provider-agnostic **agent tool-loop** for TypeScript.
4
+ The whole thing is one small class: ask an LLM, run the tools it asks for, feed
5
+ the results back, repeat until it answers. Bring your own model.
6
+
7
+ I built an AI-agent workshop ([Atelier](https://gardi.dev/projects/atelier))
8
+ where agents do real work on a git repo — and wrote the agent loop from scratch
9
+ rather than reaching for an SDK. This is that core, pulled out and generalized:
10
+ no framework, no provider lock-in, nothing to learn but one interface.
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ npm i @narimangardi/agent-loop
16
+ ```
17
+
18
+ ## Use it
19
+
20
+ Define some tools, plug in a provider, and run:
21
+
22
+ ```ts
23
+ import { Agent } from '@narimangardi/agent-loop';
24
+ import type { Tool } from '@narimangardi/agent-loop';
25
+
26
+ const getWeather: Tool<{ city: string }> = {
27
+ name: 'get_weather',
28
+ description: 'Get the current weather for a city.',
29
+ parameters: {
30
+ type: 'object',
31
+ properties: { city: { type: 'string' } },
32
+ required: ['city'],
33
+ },
34
+ execute: async ({ city }) => `It's 24°C and clear in ${city}.`,
35
+ };
36
+
37
+ const agent = new Agent({
38
+ provider: openai, // see "Plug in a model" below
39
+ tools: [getWeather],
40
+ system: 'You are a concise assistant.',
41
+ });
42
+
43
+ const result = await agent.run('What should I wear in Erbil today?');
44
+ console.log(result.text); // the final answer
45
+ console.log(result.steps); // how many round-trips it took
46
+ ```
47
+
48
+ ## How it works
49
+
50
+ `run()` is the entire loop:
51
+
52
+ 1. Send the conversation + tool specs to the provider.
53
+ 2. If the provider returns a **message**, that's the answer — return it.
54
+ 3. If it returns **tool calls**, run each tool and append the results.
55
+ 4. Go back to step 1 (up to `maxSteps`, default 10).
56
+
57
+ Unknown tools and thrown errors are handed back to the model as text, so it can
58
+ recover instead of crashing the loop.
59
+
60
+ ## Plug in a model
61
+
62
+ There's one interface to implement — map your LLM's tool-calling API to a
63
+ `CompletionResponse`:
64
+
65
+ ```ts
66
+ import type { Provider } from '@narimangardi/agent-loop';
67
+
68
+ const openai: Provider = {
69
+ async complete({ messages, tools }) {
70
+ const res = await fetch('https://api.openai.com/v1/chat/completions', {
71
+ method: 'POST',
72
+ headers: {
73
+ 'content-type': 'application/json',
74
+ authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
75
+ },
76
+ body: JSON.stringify({
77
+ model: 'gpt-4o-mini',
78
+ messages: toOpenAiMessages(messages), // small adapter you write
79
+ tools: tools.map((t) => ({ type: 'function', function: t })),
80
+ }),
81
+ });
82
+
83
+ const { choices } = await res.json();
84
+ const message = choices[0].message;
85
+
86
+ if (message.tool_calls?.length) {
87
+ return {
88
+ kind: 'tool_calls',
89
+ toolCalls: message.tool_calls.map((c: any) => ({
90
+ id: c.id,
91
+ name: c.function.name,
92
+ arguments: JSON.parse(c.function.arguments),
93
+ })),
94
+ };
95
+ }
96
+
97
+ return { kind: 'message', content: message.content };
98
+ },
99
+ };
100
+ ```
101
+
102
+ The same shape works for Anthropic, Gemini, Ollama, or anything else — it's just
103
+ "messages + tools in, message-or-tool-calls out."
104
+
105
+ ## Limitations
106
+
107
+ Deliberately small. Worth knowing:
108
+
109
+ - **No streaming.** `run()` resolves once, with the final text.
110
+ - **One run per call.** Multi-turn chat is yours to drive (keep calling `run`,
111
+ or hold the returned `messages`).
112
+ - **No retries / rate-limit handling.** Wrap your `Provider` for that.
113
+ - **No argument validation.** Tools receive whatever the model sends; validate
114
+ inside `execute` if you need to.
115
+
116
+ ## Development
117
+
118
+ ```bash
119
+ npm install
120
+ npm test # vitest
121
+ npm run build # tsup → dist (esm + cjs + d.ts)
122
+ ```
123
+
124
+ ## License
125
+
126
+ MIT — see [LICENSE](LICENSE).
package/dist/index.cjs ADDED
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ Agent: () => Agent,
24
+ MaxStepsExceededError: () => MaxStepsExceededError,
25
+ defineTool: () => defineTool
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+
29
+ // src/agent.ts
30
+ var MaxStepsExceededError = class extends Error {
31
+ constructor(steps, messages) {
32
+ super(`Agent did not finish within ${steps} steps`);
33
+ this.steps = steps;
34
+ this.messages = messages;
35
+ this.name = "MaxStepsExceededError";
36
+ }
37
+ steps;
38
+ messages;
39
+ };
40
+ var Agent = class {
41
+ provider;
42
+ tools;
43
+ toolSpecs;
44
+ system;
45
+ maxSteps;
46
+ constructor(options) {
47
+ const tools = options.tools ?? [];
48
+ this.provider = options.provider;
49
+ this.tools = new Map(tools.map((tool) => [tool.name, tool]));
50
+ this.toolSpecs = tools.map(({ name, description, parameters }) => ({ name, description, parameters }));
51
+ this.system = options.system;
52
+ this.maxSteps = options.maxSteps ?? 10;
53
+ }
54
+ async run(prompt) {
55
+ const messages = [];
56
+ if (this.system !== void 0) messages.push({ role: "system", content: this.system });
57
+ messages.push({ role: "user", content: prompt });
58
+ for (let step = 1; step <= this.maxSteps; step++) {
59
+ const response = await this.provider.complete({ messages, tools: this.toolSpecs });
60
+ if (response.kind === "message") {
61
+ messages.push({ role: "assistant", content: response.content });
62
+ return { text: response.content, steps: step, messages };
63
+ }
64
+ messages.push({ role: "assistant", content: null, toolCalls: response.toolCalls });
65
+ for (const call of response.toolCalls) {
66
+ messages.push({
67
+ role: "tool",
68
+ toolCallId: call.id,
69
+ name: call.name,
70
+ content: await this.runTool(call)
71
+ });
72
+ }
73
+ }
74
+ throw new MaxStepsExceededError(this.maxSteps, messages);
75
+ }
76
+ /** Run one tool. Unknown tools and thrown errors come back as text the model can recover from. */
77
+ async runTool(call) {
78
+ const tool = this.tools.get(call.name);
79
+ if (tool === void 0) {
80
+ return `Error: unknown tool "${call.name}"`;
81
+ }
82
+ try {
83
+ return await tool.execute(call.arguments);
84
+ } catch (error) {
85
+ return `Error: ${error instanceof Error ? error.message : String(error)}`;
86
+ }
87
+ }
88
+ };
89
+
90
+ // src/tool.ts
91
+ function defineTool(tool) {
92
+ return tool;
93
+ }
94
+ // Annotate the CommonJS export names for ESM import in node:
95
+ 0 && (module.exports = {
96
+ Agent,
97
+ MaxStepsExceededError,
98
+ defineTool
99
+ });
@@ -0,0 +1,101 @@
1
+ /** A single tool call the model wants to make. */
2
+ interface ToolCall {
3
+ id: string;
4
+ name: string;
5
+ arguments: Record<string, unknown>;
6
+ }
7
+ /** A message in the running conversation. */
8
+ type Message = {
9
+ role: 'system';
10
+ content: string;
11
+ } | {
12
+ role: 'user';
13
+ content: string;
14
+ } | {
15
+ role: 'assistant';
16
+ content: string | null;
17
+ toolCalls?: ToolCall[];
18
+ } | {
19
+ role: 'tool';
20
+ toolCallId: string;
21
+ name: string;
22
+ content: string;
23
+ };
24
+ /** A tool the agent can call. `execute` returns the result the model sees. */
25
+ interface Tool<A extends Record<string, unknown> = Record<string, unknown>> {
26
+ name: string;
27
+ description: string;
28
+ /** JSON Schema describing the arguments. */
29
+ parameters: Record<string, unknown>;
30
+ execute(args: A): string | Promise<string>;
31
+ }
32
+ /** The tool shape sent to the provider (everything except `execute`). */
33
+ interface ToolSpec {
34
+ name: string;
35
+ description: string;
36
+ parameters: Record<string, unknown>;
37
+ }
38
+ interface CompletionRequest {
39
+ messages: Message[];
40
+ tools: ToolSpec[];
41
+ }
42
+ /** What a provider returns: either a final message, or tool calls to run. */
43
+ type CompletionResponse = {
44
+ kind: 'message';
45
+ content: string;
46
+ } | {
47
+ kind: 'tool_calls';
48
+ toolCalls: ToolCall[];
49
+ };
50
+ /**
51
+ * The one thing you implement to plug in an LLM. Map your provider's
52
+ * chat/tool-calling API to a CompletionResponse and you're done.
53
+ */
54
+ interface Provider {
55
+ complete(request: CompletionRequest): Promise<CompletionResponse>;
56
+ }
57
+
58
+ interface AgentOptions {
59
+ provider: Provider;
60
+ tools?: Tool[];
61
+ system?: string;
62
+ /** Max provider round-trips before giving up. Default 10. */
63
+ maxSteps?: number;
64
+ }
65
+ interface AgentResult {
66
+ /** The assistant's final text answer. */
67
+ text: string;
68
+ /** How many provider round-trips it took. */
69
+ steps: number;
70
+ /** The full history, including tool calls and their results. */
71
+ messages: Message[];
72
+ }
73
+ declare class MaxStepsExceededError extends Error {
74
+ readonly steps: number;
75
+ readonly messages: Message[];
76
+ constructor(steps: number, messages: Message[]);
77
+ }
78
+ /**
79
+ * The whole agent: ask the provider, run any tools it calls, feed the results
80
+ * back, repeat until it returns a final answer (or maxSteps is hit).
81
+ */
82
+ declare class Agent {
83
+ private readonly provider;
84
+ private readonly tools;
85
+ private readonly toolSpecs;
86
+ private readonly system?;
87
+ private readonly maxSteps;
88
+ constructor(options: AgentOptions);
89
+ run(prompt: string): Promise<AgentResult>;
90
+ /** Run one tool. Unknown tools and thrown errors come back as text the model can recover from. */
91
+ private runTool;
92
+ }
93
+
94
+ /**
95
+ * Identity helper that gives you argument typing when defining a tool inline:
96
+ *
97
+ * const t = defineTool<{ city: string }>({ ... execute(args) { args.city } })
98
+ */
99
+ declare function defineTool<A extends Record<string, unknown> = Record<string, unknown>>(tool: Tool<A>): Tool<A>;
100
+
101
+ export { Agent, type AgentOptions, type AgentResult, type CompletionRequest, type CompletionResponse, MaxStepsExceededError, type Message, type Provider, type Tool, type ToolCall, type ToolSpec, defineTool };
@@ -0,0 +1,101 @@
1
+ /** A single tool call the model wants to make. */
2
+ interface ToolCall {
3
+ id: string;
4
+ name: string;
5
+ arguments: Record<string, unknown>;
6
+ }
7
+ /** A message in the running conversation. */
8
+ type Message = {
9
+ role: 'system';
10
+ content: string;
11
+ } | {
12
+ role: 'user';
13
+ content: string;
14
+ } | {
15
+ role: 'assistant';
16
+ content: string | null;
17
+ toolCalls?: ToolCall[];
18
+ } | {
19
+ role: 'tool';
20
+ toolCallId: string;
21
+ name: string;
22
+ content: string;
23
+ };
24
+ /** A tool the agent can call. `execute` returns the result the model sees. */
25
+ interface Tool<A extends Record<string, unknown> = Record<string, unknown>> {
26
+ name: string;
27
+ description: string;
28
+ /** JSON Schema describing the arguments. */
29
+ parameters: Record<string, unknown>;
30
+ execute(args: A): string | Promise<string>;
31
+ }
32
+ /** The tool shape sent to the provider (everything except `execute`). */
33
+ interface ToolSpec {
34
+ name: string;
35
+ description: string;
36
+ parameters: Record<string, unknown>;
37
+ }
38
+ interface CompletionRequest {
39
+ messages: Message[];
40
+ tools: ToolSpec[];
41
+ }
42
+ /** What a provider returns: either a final message, or tool calls to run. */
43
+ type CompletionResponse = {
44
+ kind: 'message';
45
+ content: string;
46
+ } | {
47
+ kind: 'tool_calls';
48
+ toolCalls: ToolCall[];
49
+ };
50
+ /**
51
+ * The one thing you implement to plug in an LLM. Map your provider's
52
+ * chat/tool-calling API to a CompletionResponse and you're done.
53
+ */
54
+ interface Provider {
55
+ complete(request: CompletionRequest): Promise<CompletionResponse>;
56
+ }
57
+
58
+ interface AgentOptions {
59
+ provider: Provider;
60
+ tools?: Tool[];
61
+ system?: string;
62
+ /** Max provider round-trips before giving up. Default 10. */
63
+ maxSteps?: number;
64
+ }
65
+ interface AgentResult {
66
+ /** The assistant's final text answer. */
67
+ text: string;
68
+ /** How many provider round-trips it took. */
69
+ steps: number;
70
+ /** The full history, including tool calls and their results. */
71
+ messages: Message[];
72
+ }
73
+ declare class MaxStepsExceededError extends Error {
74
+ readonly steps: number;
75
+ readonly messages: Message[];
76
+ constructor(steps: number, messages: Message[]);
77
+ }
78
+ /**
79
+ * The whole agent: ask the provider, run any tools it calls, feed the results
80
+ * back, repeat until it returns a final answer (or maxSteps is hit).
81
+ */
82
+ declare class Agent {
83
+ private readonly provider;
84
+ private readonly tools;
85
+ private readonly toolSpecs;
86
+ private readonly system?;
87
+ private readonly maxSteps;
88
+ constructor(options: AgentOptions);
89
+ run(prompt: string): Promise<AgentResult>;
90
+ /** Run one tool. Unknown tools and thrown errors come back as text the model can recover from. */
91
+ private runTool;
92
+ }
93
+
94
+ /**
95
+ * Identity helper that gives you argument typing when defining a tool inline:
96
+ *
97
+ * const t = defineTool<{ city: string }>({ ... execute(args) { args.city } })
98
+ */
99
+ declare function defineTool<A extends Record<string, unknown> = Record<string, unknown>>(tool: Tool<A>): Tool<A>;
100
+
101
+ export { Agent, type AgentOptions, type AgentResult, type CompletionRequest, type CompletionResponse, MaxStepsExceededError, type Message, type Provider, type Tool, type ToolCall, type ToolSpec, defineTool };
package/dist/index.js ADDED
@@ -0,0 +1,70 @@
1
+ // src/agent.ts
2
+ var MaxStepsExceededError = class extends Error {
3
+ constructor(steps, messages) {
4
+ super(`Agent did not finish within ${steps} steps`);
5
+ this.steps = steps;
6
+ this.messages = messages;
7
+ this.name = "MaxStepsExceededError";
8
+ }
9
+ steps;
10
+ messages;
11
+ };
12
+ var Agent = class {
13
+ provider;
14
+ tools;
15
+ toolSpecs;
16
+ system;
17
+ maxSteps;
18
+ constructor(options) {
19
+ const tools = options.tools ?? [];
20
+ this.provider = options.provider;
21
+ this.tools = new Map(tools.map((tool) => [tool.name, tool]));
22
+ this.toolSpecs = tools.map(({ name, description, parameters }) => ({ name, description, parameters }));
23
+ this.system = options.system;
24
+ this.maxSteps = options.maxSteps ?? 10;
25
+ }
26
+ async run(prompt) {
27
+ const messages = [];
28
+ if (this.system !== void 0) messages.push({ role: "system", content: this.system });
29
+ messages.push({ role: "user", content: prompt });
30
+ for (let step = 1; step <= this.maxSteps; step++) {
31
+ const response = await this.provider.complete({ messages, tools: this.toolSpecs });
32
+ if (response.kind === "message") {
33
+ messages.push({ role: "assistant", content: response.content });
34
+ return { text: response.content, steps: step, messages };
35
+ }
36
+ messages.push({ role: "assistant", content: null, toolCalls: response.toolCalls });
37
+ for (const call of response.toolCalls) {
38
+ messages.push({
39
+ role: "tool",
40
+ toolCallId: call.id,
41
+ name: call.name,
42
+ content: await this.runTool(call)
43
+ });
44
+ }
45
+ }
46
+ throw new MaxStepsExceededError(this.maxSteps, messages);
47
+ }
48
+ /** Run one tool. Unknown tools and thrown errors come back as text the model can recover from. */
49
+ async runTool(call) {
50
+ const tool = this.tools.get(call.name);
51
+ if (tool === void 0) {
52
+ return `Error: unknown tool "${call.name}"`;
53
+ }
54
+ try {
55
+ return await tool.execute(call.arguments);
56
+ } catch (error) {
57
+ return `Error: ${error instanceof Error ? error.message : String(error)}`;
58
+ }
59
+ }
60
+ };
61
+
62
+ // src/tool.ts
63
+ function defineTool(tool) {
64
+ return tool;
65
+ }
66
+ export {
67
+ Agent,
68
+ MaxStepsExceededError,
69
+ defineTool
70
+ };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@narimangardi/agent-loop",
3
+ "version": "0.1.0",
4
+ "description": "A tiny, zero-dependency, provider-agnostic agent tool-loop for TypeScript. Build your own AI agent in ~100 lines.",
5
+ "keywords": ["ai", "agent", "tool-use", "function-calling", "llm", "typescript"],
6
+ "license": "MIT",
7
+ "author": "Nariman Gardi <narimanmuhsin0@gmail.com>",
8
+ "repository": { "type": "git", "url": "git+https://github.com/NarimanGardi/agent-loop.git" },
9
+ "homepage": "https://github.com/NarimanGardi/agent-loop#readme",
10
+ "bugs": "https://github.com/NarimanGardi/agent-loop/issues",
11
+ "type": "module",
12
+ "main": "./dist/index.cjs",
13
+ "module": "./dist/index.js",
14
+ "types": "./dist/index.d.ts",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "import": "./dist/index.js",
19
+ "require": "./dist/index.cjs"
20
+ }
21
+ },
22
+ "files": ["dist"],
23
+ "scripts": {
24
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean",
25
+ "test": "vitest run",
26
+ "typecheck": "tsc --noEmit",
27
+ "prepublishOnly": "npm run build"
28
+ },
29
+ "devDependencies": {
30
+ "tsup": "^8.0.0",
31
+ "typescript": "^5.6.0",
32
+ "vitest": "^3.0.0"
33
+ }
34
+ }