@lowire/loop 0.0.10 → 0.0.12

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/lib/loop.d.ts CHANGED
@@ -19,12 +19,12 @@ export type LoopEvents = {
19
19
  onBeforeTurn?: (params: {
20
20
  conversation: types.Conversation;
21
21
  totalUsage: types.Usage;
22
- budgetTokens: number;
22
+ budgetTokens?: number;
23
23
  }) => PromiseOrValue<'continue' | 'break' | void>;
24
24
  onAfterTurn?: (params: {
25
25
  assistantMessage: types.AssistantMessage;
26
26
  totalUsage: types.Usage;
27
- budgetTokens: number;
27
+ budgetTokens?: number;
28
28
  }) => PromiseOrValue<'continue' | 'break' | void>;
29
29
  onBeforeToolCall?: (params: {
30
30
  assistantMessage: types.AssistantMessage;
@@ -45,7 +45,6 @@ export type LoopOptions = types.CompletionOptions & LoopEvents & {
45
45
  tools?: types.Tool[];
46
46
  callTool?: types.ToolCallback;
47
47
  maxTurns?: number;
48
- resultSchema?: types.Schema;
49
48
  cache?: {
50
49
  messages: types.ReplayCache;
51
50
  secrets: Record<string, string>;
@@ -57,10 +56,10 @@ export declare class Loop {
57
56
  private _loopOptions;
58
57
  private _cacheOutput;
59
58
  constructor(loopName: 'openai' | 'github' | 'anthropic' | 'google', options: LoopOptions);
60
- run<T>(task: string, runOptions?: Omit<LoopOptions, 'model'> & {
59
+ run(task: string, runOptions?: Omit<LoopOptions, 'model'> & {
61
60
  model?: string;
62
61
  }): Promise<{
63
- result?: T;
62
+ result?: types.ToolResult;
64
63
  status: 'ok' | 'break';
65
64
  usage: types.Usage;
66
65
  turns: number;
package/lib/loop.js CHANGED
@@ -29,12 +29,7 @@ class Loop {
29
29
  }
30
30
  async run(task, runOptions = {}) {
31
31
  const options = { ...this._loopOptions, ...runOptions };
32
- const allTools = [...options.tools || []];
33
- allTools.push({
34
- name: 'report_result',
35
- description: 'Report the result of the task.',
36
- inputSchema: options.resultSchema ?? defaultResultSchema,
37
- });
32
+ const allTools = [...(options.tools || []).map(wrapToolWithIsDone)];
38
33
  const conversation = {
39
34
  systemPrompt,
40
35
  messages: [
@@ -43,12 +38,12 @@ class Loop {
43
38
  tools: allTools,
44
39
  };
45
40
  const debug = options.debug;
46
- let budgetTokens = options.maxTokens ?? 100_000;
41
+ let budgetTokens = options.maxTokens;
47
42
  const totalUsage = { input: 0, output: 0 };
48
43
  debug?.('lowire:loop')(`Starting ${this._provider.name} loop`, task);
49
44
  const maxTurns = options.maxTurns || 100;
50
45
  for (let turns = 0; turns < maxTurns; ++turns) {
51
- if (budgetTokens <= 0)
46
+ if (options.maxTokens && budgetTokens !== undefined && budgetTokens <= 0)
52
47
  throw new Error(`Budget tokens ${options.maxTokens} exhausted`);
53
48
  debug?.('lowire:loop')(`Turn ${turns + 1} of (max ${maxTurns})`);
54
49
  const caches = options.cache ? {
@@ -68,7 +63,8 @@ class Loop {
68
63
  const intent = assistantMessage.content.filter(part => part.type === 'text').map(part => part.text).join('\n');
69
64
  totalUsage.input += usage.input;
70
65
  totalUsage.output += usage.output;
71
- budgetTokens -= usage.input + usage.output;
66
+ if (budgetTokens !== undefined)
67
+ budgetTokens -= usage.input + usage.output;
72
68
  debug?.('lowire:loop')('Usage', `input: ${usage.input}, output: ${usage.output}`);
73
69
  debug?.('lowire:loop')('Assistant', intent, JSON.stringify(assistantMessage.content, null, 2));
74
70
  const afterStatus = await options.onAfterTurn?.({ assistantMessage, totalUsage, budgetTokens });
@@ -83,8 +79,6 @@ class Loop {
83
79
  for (const toolCall of toolCalls) {
84
80
  const { name, arguments: args } = toolCall;
85
81
  debug?.('lowire:loop')('Call tool', name, JSON.stringify(args, null, 2));
86
- if (name === 'report_result')
87
- return { result: args, status: 'ok', usage: totalUsage, turns };
88
82
  const status = await options.onBeforeToolCall?.({ assistantMessage, toolCall });
89
83
  if (status === 'break')
90
84
  return { status: 'break', usage: totalUsage, turns };
@@ -120,6 +114,8 @@ class Loop {
120
114
  continue;
121
115
  }
122
116
  toolCall.result = result;
117
+ if (args._is_done)
118
+ return { result, status: 'ok', usage: totalUsage, turns };
123
119
  }
124
120
  catch (error) {
125
121
  const errorMessage = `Error while executing tool "${name}": ${error instanceof Error ? error.message : String(error)}\n\nPlease try to recover and complete the task.`;
@@ -152,15 +148,18 @@ class Loop {
152
148
  }
153
149
  }
154
150
  exports.Loop = Loop;
155
- const defaultResultSchema = {
156
- type: 'object',
157
- properties: {
158
- result: {
159
- type: 'string',
160
- },
161
- },
162
- required: ['result'],
163
- };
151
+ function wrapToolWithIsDone(tool) {
152
+ const inputSchema = { ...tool.inputSchema };
153
+ inputSchema.properties = {
154
+ ...inputSchema.properties,
155
+ _is_done: { type: 'boolean', description: 'Whether the task is complete. If false, agentic loop will continue to perform the task.' },
156
+ };
157
+ inputSchema.required = [...(inputSchema.required || []), '_is_done'];
158
+ return {
159
+ ...tool,
160
+ inputSchema,
161
+ };
162
+ }
164
163
  const systemPrompt = `
165
164
  You are an autonomous agent designed to complete tasks by interacting with tools. Perform the user task.
166
165
  `;
@@ -19,7 +19,7 @@ exports.Anthropic = void 0;
19
19
  class Anthropic {
20
20
  name = 'anthropic';
21
21
  async complete(conversation, options) {
22
- const maxTokens = Math.min(options.maxTokens ?? 32768, 32768);
22
+ const maxTokens = Math.min(options.maxTokens ?? 32_768, 32_768);
23
23
  const response = await create({
24
24
  model: options.model,
25
25
  max_tokens: maxTokens,
package/lib/types.d.ts CHANGED
@@ -15,7 +15,7 @@
15
15
  */
16
16
  export type Schema = {
17
17
  type: 'object';
18
- properties?: unknown | null;
18
+ properties?: Record<string, unknown> | null;
19
19
  required?: Array<string> | null;
20
20
  };
21
21
  export type Tool = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowire/loop",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "description": "Small agentic loop",
5
5
  "repository": {
6
6
  "type": "git",