fourmis-agents-sdk 0.2.6 → 0.3.1

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.
@@ -15,6 +15,7 @@ import type { ToolRegistry } from "./tools/registry.js";
15
15
  import type { PermissionManager } from "./permissions.js";
16
16
  import type { HookManager } from "./hooks.js";
17
17
  import type { McpClientManager } from "./mcp/client.js";
18
+ import type { NativeMemoryTool } from "./memory/index.js";
18
19
  export type SessionLogger = (role: "user" | "assistant", content: NormalizedContent[] | string, parentUuid: string | null) => string;
19
20
  export type AgentLoopOptions = {
20
21
  provider: ProviderAdapter;
@@ -34,6 +35,8 @@ export type AgentLoopOptions = {
34
35
  mcpClient?: McpClientManager;
35
36
  previousMessages?: NormalizedMessage[];
36
37
  sessionLogger?: SessionLogger;
38
+ /** Native memory tool for Anthropic provider (handled specially) */
39
+ nativeMemoryTool?: NativeMemoryTool;
37
40
  };
38
41
  export declare function agentLoop(prompt: string, options: AgentLoopOptions): AsyncGenerator<AgentMessage>;
39
42
  //# sourceMappingURL=agent-loop.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"agent-loop.d.ts","sourceRoot":"","sources":["../src/agent-loop.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EACV,YAAY,EAGb,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EAAE,eAAe,EAAE,iBAAiB,EAAE,iBAAiB,EAAa,MAAM,sBAAsB,CAAC;AAC7G,OAAO,KAAK,EAAE,YAAY,EAAe,MAAM,qBAAqB,CAAC;AACrE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAC9C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,MAAM,MAAM,aAAa,GAAG,CAC1B,IAAI,EAAE,MAAM,GAAG,WAAW,EAC1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ger
125
+ sessionLogger,
126
+ nativeMemoryTool
126
127
  } = options;
127
128
  const startTime = Date.now();
128
129
  let apiTimeMs = 0;
@@ -176,13 +177,15 @@ async function* agentLoop(prompt, options) {
176
177
  let assistantTextParts = [];
177
178
  let toolCalls = [];
178
179
  let turnUsage = emptyTokenUsage();
180
+ const nativeTools = nativeMemoryTool ? [nativeMemoryTool.definition] : undefined;
179
181
  try {
180
182
  const chunks = provider.chat({
181
183
  model,
182
184
  messages,
183
185
  tools: toolDefs.length > 0 ? toolDefs : undefined,
184
186
  systemPrompt,
185
- signal
187
+ signal,
188
+ nativeTools
186
189
  });
187
190
  for await (const chunk of chunks) {
188
191
  switch (chunk.type) {
@@ -340,13 +343,24 @@ async function* agentLoop(prompt, options) {
340
343
  input: toolInput,
341
344
  uuid: uuid()
342
345
  };
343
- const toolCtx = {
344
- cwd,
345
- signal,
346
- sessionId,
347
- env
348
- };
349
- const result = await tools.execute(call.name, toolInput, toolCtx);
346
+ let result;
347
+ if (call.name === "memory" && nativeMemoryTool) {
348
+ try {
349
+ const content = await nativeMemoryTool.execute(toolInput);
350
+ result = { content, isError: content.startsWith("Error:") };
351
+ } catch (err) {
352
+ const message = err instanceof Error ? err.message : String(err);
353
+ result = { content: `Error: ${message}`, isError: true };
354
+ }
355
+ } else {
356
+ const toolCtx = {
357
+ cwd,
358
+ signal,
359
+ sessionId,
360
+ env
361
+ };
362
+ result = await tools.execute(call.name, toolInput, toolCtx);
363
+ }
350
364
  if (debug) {
351
365
  console.error(`[debug] Tool ${call.name}: ${result.isError ? "ERROR" : "OK"} (${result.content.length} chars)`);
352
366
  }
@@ -501,7 +501,8 @@ async function* agentLoop(prompt, options) {
501
501
  hooks,
502
502
  mcpClient,
503
503
  previousMessages,
504
- sessionLogger
504
+ sessionLogger,
505
+ nativeMemoryTool
505
506
  } = options;
506
507
  const startTime = Date.now();
507
508
  let apiTimeMs = 0;
@@ -555,13 +556,15 @@ async function* agentLoop(prompt, options) {
555
556
  let assistantTextParts = [];
556
557
  let toolCalls = [];
557
558
  let turnUsage = emptyTokenUsage();
559
+ const nativeTools = nativeMemoryTool ? [nativeMemoryTool.definition] : undefined;
558
560
  try {
559
561
  const chunks = provider.chat({
560
562
  model,
561
563
  messages,
562
564
  tools: toolDefs.length > 0 ? toolDefs : undefined,
563
565
  systemPrompt,
564
- signal
566
+ signal,
567
+ nativeTools
565
568
  });
566
569
  for await (const chunk of chunks) {
567
570
  switch (chunk.type) {
@@ -719,13 +722,24 @@ async function* agentLoop(prompt, options) {
719
722
  input: toolInput,
720
723
  uuid: uuid()
721
724
  };
722
- const toolCtx = {
723
- cwd,
724
- signal,
725
- sessionId,
726
- env
727
- };
728
- const result = await tools.execute(call.name, toolInput, toolCtx);
725
+ let result;
726
+ if (call.name === "memory" && nativeMemoryTool) {
727
+ try {
728
+ const content = await nativeMemoryTool.execute(toolInput);
729
+ result = { content, isError: content.startsWith("Error:") };
730
+ } catch (err) {
731
+ const message = err instanceof Error ? err.message : String(err);
732
+ result = { content: `Error: ${message}`, isError: true };
733
+ }
734
+ } else {
735
+ const toolCtx = {
736
+ cwd,
737
+ signal,
738
+ sessionId,
739
+ env
740
+ };
741
+ result = await tools.execute(call.name, toolInput, toolCtx);
742
+ }
729
743
  if (debug) {
730
744
  console.error(`[debug] Tool ${call.name}: ${result.isError ? "ERROR" : "OK"} (${result.content.length} chars)`);
731
745
  }
@@ -1040,15 +1054,29 @@ class AnthropicAdapter {
1040
1054
  } else if (request.systemPrompt) {
1041
1055
  params.system = request.systemPrompt;
1042
1056
  }
1057
+ const allTools = [];
1043
1058
  if (tools && tools.length > 0) {
1044
- params.tools = tools;
1059
+ allTools.push(...tools);
1060
+ }
1061
+ if (request.nativeTools && request.nativeTools.length > 0) {
1062
+ allTools.push(...request.nativeTools);
1063
+ }
1064
+ if (allTools.length > 0) {
1065
+ params.tools = allTools;
1045
1066
  }
1046
1067
  if (request.temperature !== undefined) {
1047
1068
  params.temperature = request.temperature;
1048
1069
  }
1049
- const stream = this.client.messages.stream(params, {
1070
+ const hasMemoryTool = request.nativeTools?.some((t) => t.type === "memory_20250818");
1071
+ const requestOptions = {
1050
1072
  signal: request.signal
1051
- });
1073
+ };
1074
+ if (hasMemoryTool) {
1075
+ requestOptions.headers = {
1076
+ "anthropic-beta": "context-management-2025-06-27"
1077
+ };
1078
+ }
1079
+ const stream = this.client.messages.stream(params, requestOptions);
1052
1080
  const toolInputBuffers = new Map;
1053
1081
  for await (const event of stream) {
1054
1082
  switch (event.type) {
@@ -501,7 +501,8 @@ async function* agentLoop(prompt, options) {
501
501
  hooks,
502
502
  mcpClient,
503
503
  previousMessages,
504
- sessionLogger
504
+ sessionLogger,
505
+ nativeMemoryTool
505
506
  } = options;
506
507
  const startTime = Date.now();
507
508
  let apiTimeMs = 0;
@@ -555,13 +556,15 @@ async function* agentLoop(prompt, options) {
555
556
  let assistantTextParts = [];
556
557
  let toolCalls = [];
557
558
  let turnUsage = emptyTokenUsage();
559
+ const nativeTools = nativeMemoryTool ? [nativeMemoryTool.definition] : undefined;
558
560
  try {
559
561
  const chunks = provider.chat({
560
562
  model,
561
563
  messages,
562
564
  tools: toolDefs.length > 0 ? toolDefs : undefined,
563
565
  systemPrompt,
564
- signal
566
+ signal,
567
+ nativeTools
565
568
  });
566
569
  for await (const chunk of chunks) {
567
570
  switch (chunk.type) {
@@ -719,13 +722,24 @@ async function* agentLoop(prompt, options) {
719
722
  input: toolInput,
720
723
  uuid: uuid()
721
724
  };
722
- const toolCtx = {
723
- cwd,
724
- signal,
725
- sessionId,
726
- env
727
- };
728
- const result = await tools.execute(call.name, toolInput, toolCtx);
725
+ let result;
726
+ if (call.name === "memory" && nativeMemoryTool) {
727
+ try {
728
+ const content = await nativeMemoryTool.execute(toolInput);
729
+ result = { content, isError: content.startsWith("Error:") };
730
+ } catch (err) {
731
+ const message = err instanceof Error ? err.message : String(err);
732
+ result = { content: `Error: ${message}`, isError: true };
733
+ }
734
+ } else {
735
+ const toolCtx = {
736
+ cwd,
737
+ signal,
738
+ sessionId,
739
+ env
740
+ };
741
+ result = await tools.execute(call.name, toolInput, toolCtx);
742
+ }
729
743
  if (debug) {
730
744
  console.error(`[debug] Tool ${call.name}: ${result.isError ? "ERROR" : "OK"} (${result.content.length} chars)`);
731
745
  }
@@ -1040,15 +1054,29 @@ class AnthropicAdapter {
1040
1054
  } else if (request.systemPrompt) {
1041
1055
  params.system = request.systemPrompt;
1042
1056
  }
1057
+ const allTools = [];
1043
1058
  if (tools && tools.length > 0) {
1044
- params.tools = tools;
1059
+ allTools.push(...tools);
1060
+ }
1061
+ if (request.nativeTools && request.nativeTools.length > 0) {
1062
+ allTools.push(...request.nativeTools);
1063
+ }
1064
+ if (allTools.length > 0) {
1065
+ params.tools = allTools;
1045
1066
  }
1046
1067
  if (request.temperature !== undefined) {
1047
1068
  params.temperature = request.temperature;
1048
1069
  }
1049
- const stream = this.client.messages.stream(params, {
1070
+ const hasMemoryTool = request.nativeTools?.some((t) => t.type === "memory_20250818");
1071
+ const requestOptions = {
1050
1072
  signal: request.signal
1051
- });
1073
+ };
1074
+ if (hasMemoryTool) {
1075
+ requestOptions.headers = {
1076
+ "anthropic-beta": "context-management-2025-06-27"
1077
+ };
1078
+ }
1079
+ const stream = this.client.messages.stream(params, requestOptions);
1052
1080
  const toolInputBuffers = new Map;
1053
1081
  for await (const event of stream) {
1054
1082
  switch (event.type) {
package/dist/api.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,KAAK,EAAqB,MAAM,YAAY,CAAC;AAoBzE;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,KAAK,CAAC,MAAM,EAAE;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,YAAY,CAAC;CACxB,GAAG,KAAK,CA8JR"}
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,KAAK,EAAqB,MAAM,YAAY,CAAC;AAwBzE;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,KAAK,CAAC,MAAM,EAAE;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,YAAY,CAAC;CACxB,GAAG,KAAK,CA+LR"}