@letta-ai/letta-agent-sdk 0.8.79 → 0.8.80

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@letta-ai/letta-agent-sdk",
3
- "version": "0.8.79",
3
+ "version": "0.8.80",
4
4
  "packageManager": "bun@1.3.14",
5
5
  "description": "SDK for programmatic control of Letta agents",
6
6
  "type": "module",
@@ -43,10 +43,10 @@
43
43
  "ajv": "^8.20.0"
44
44
  },
45
45
  "peerDependencies": {
46
- "@letta-ai/letta-code": ">=0.34.49 <0.35.0"
46
+ "@letta-ai/letta-code": ">=0.34.50 <0.35.0"
47
47
  },
48
48
  "devDependencies": {
49
- "@letta-ai/letta-code": "0.34.49",
49
+ "@letta-ai/letta-code": "0.34.50",
50
50
  "@modelcontextprotocol/server-everything": "^2026.7.4",
51
51
  "@types/bun": "latest",
52
52
  "@types/node": "^22.0.0",
@@ -24,7 +24,7 @@ import {
24
24
  type McpToolBridge,
25
25
  } from "./mcp-runtime.js";
26
26
  import { applyUniqueRequestIds } from "./request-ids.js";
27
- import { createExternalToolCallHandler, externalToolGroups } from "./external-tools.js";
27
+ import { createExternalToolCallHandler, externalToolGroups, registerExternalToolCallHandler } from "./external-tools.js";
28
28
  import {
29
29
  clientToolsetPayload,
30
30
  mergeInheritedTools,
@@ -678,7 +678,7 @@ export class AppServerSession extends RemoteClientSessionCore {
678
678
  });
679
679
 
680
680
  if (this.externalTools.size > 0 || this.inheritedTools) {
681
- this.removeExternalToolHandler = client.onExternalToolCall(
681
+ this.removeExternalToolHandler = registerExternalToolCallHandler(client,
682
682
  createExternalToolCallHandler(this.externalTools, () => this.runtime),
683
683
  );
684
684
  }
@@ -16,7 +16,7 @@ import {
16
16
  agentToolNames,
17
17
  registerAppServerControlRequestHandler,
18
18
  } from "./app-server-session.js";
19
- import { createExternalToolCallHandler, externalToolGroups } from "./external-tools.js";
19
+ import { createExternalToolCallHandler, externalToolGroups, registerExternalToolCallHandler } from "./external-tools.js";
20
20
  import { createCloudStatusTransportConstructor } from "./cloud-status-transport.js";
21
21
  import {
22
22
  connectMcpServers,
@@ -606,7 +606,7 @@ export class CloudEnvironmentSession extends RemoteClientSessionCore {
606
606
  getOptions: () => this.currentOptions(),
607
607
  });
608
608
  if (this.externalTools.size > 0) {
609
- this.removeExternalToolHandler = client.onExternalToolCall(
609
+ this.removeExternalToolHandler = registerExternalToolCallHandler(client,
610
610
  createExternalToolCallHandler(this.externalTools, () => this.runtime),
611
611
  );
612
612
  }
@@ -1,4 +1,4 @@
1
- import type { AppServerExternalToolCallHandler } from "@letta-ai/letta-code/app-server-client";
1
+ import type { AppServerClient, AppServerExternalToolCallHandler } from "@letta-ai/letta-code/app-server-client";
2
2
  import type { RuntimeScope } from "./remote-session-protocol.js";
3
3
  import type { AgentToolCallContext, AnyAgentTool } from "./types.js";
4
4
 
@@ -7,6 +7,7 @@ export function externalToolGroups(tools: AnyAgentTool[] | undefined): Array<Rec
7
7
  if (!tools || tools.length === 0) return undefined;
8
8
  return [
9
9
  {
10
+ capabilities: { external_tool_call_cancel: true },
10
11
  tools: tools.map((tool) => ({
11
12
  name: tool.name,
12
13
  label: tool.label,
@@ -26,11 +27,11 @@ const MAX_REMEMBERED_EXTERNAL_TOOL_CALLS = 200;
26
27
  export function createExternalToolCallHandler(
27
28
  externalTools: Map<string, AnyAgentTool>,
28
29
  fallbackRuntime?: () => RuntimeScope | null,
29
- ): AppServerExternalToolCallHandler {
30
+ ): CancellableToolCallHandler {
30
31
  // A request can be delivered more than once (e.g. relayed on more than one
31
32
  // socket). Share one execution per request_id so a tool never runs twice.
32
33
  const calls = new Map<string, ReturnType<AppServerExternalToolCallHandler>>();
33
- const execute: AppServerExternalToolCallHandler = async (request) => {
34
+ const execute: CancellableToolCallHandler = async (request, signal) => {
34
35
  const tool = externalTools.get(request.tool_name);
35
36
  if (!tool) {
36
37
  throw new Error(`Unknown external tool: ${request.tool_name}`);
@@ -39,7 +40,7 @@ export function createExternalToolCallHandler(
39
40
  const context: AgentToolCallContext | undefined = runtime
40
41
  ? { toolCallId: request.tool_call_id, agentId: runtime.agent_id, conversationId: runtime.conversation_id }
41
42
  : undefined;
42
- const result = await tool.execute(request.tool_call_id, request.input, undefined, undefined, context);
43
+ const result = await tool.execute(request.tool_call_id, request.input, signal, undefined, context);
43
44
  return {
44
45
  content: result.content.map((part) => ({
45
46
  type: part.type,
@@ -50,14 +51,14 @@ export function createExternalToolCallHandler(
50
51
  ...(result.isError === true ? { is_error: true } : {}),
51
52
  };
52
53
  };
53
- return (request) => {
54
+ return (request, signal) => {
54
55
  const requestId = request.request_id;
55
56
  if (typeof requestId !== "string" || requestId.length === 0) {
56
- return execute(request);
57
+ return execute(request, signal);
57
58
  }
58
59
  const existing = calls.get(requestId);
59
60
  if (existing) return existing;
60
- const call = execute(request);
61
+ const call = execute(request, signal);
61
62
  calls.set(requestId, call);
62
63
  while (calls.size > MAX_REMEMBERED_EXTERNAL_TOOL_CALLS) {
63
64
  const oldest = calls.keys().next().value;
@@ -67,3 +68,53 @@ export function createExternalToolCallHandler(
67
68
  return call;
68
69
  };
69
70
  }
71
+
72
+ type CancellableToolCallHandler = (
73
+ request: Parameters<AppServerExternalToolCallHandler>[0],
74
+ signal?: AbortSignal,
75
+ ) => ReturnType<AppServerExternalToolCallHandler>;
76
+
77
+ /** Own response arbitration so cancellation and completion cannot both reply. */
78
+ export function registerExternalToolCallHandler(
79
+ client: AppServerClient,
80
+ handler: CancellableToolCallHandler,
81
+ ): () => void {
82
+ const pending = new Map<string, { controller: AbortController; finish: (response: Record<string, unknown>) => void }>();
83
+ const completed = new Set<string>();
84
+ const remove = client.onMessage((raw, channel) => {
85
+ if (channel !== "control") return;
86
+ const message = raw as unknown as Record<string, unknown>;
87
+ const requestId = message.request_id;
88
+ if (typeof requestId !== "string") return;
89
+ if (message.type === "external_tool_call_cancel") {
90
+ const call = pending.get(requestId);
91
+ if (!call) return;
92
+ // Claim the response before abort listeners can resolve or throw.
93
+ call.finish({ error: "cancelled" });
94
+ call.controller.abort(message.reason);
95
+ return;
96
+ }
97
+ if (message.type !== "external_tool_call_request" || pending.has(requestId) || completed.has(requestId)) return;
98
+ const controller = new AbortController();
99
+ const finish = (response: Record<string, unknown>) => {
100
+ if (!pending.delete(requestId)) return;
101
+ completed.add(requestId);
102
+ if (completed.size > MAX_REMEMBERED_EXTERNAL_TOOL_CALLS) completed.delete(completed.values().next().value!);
103
+ client.send({ type: "external_tool_call_response", request_id: requestId, ...response });
104
+ };
105
+ pending.set(requestId, { controller, finish });
106
+ try {
107
+ Promise.resolve(handler(raw as Parameters<AppServerExternalToolCallHandler>[0], controller.signal)).then(
108
+ (result) => finish({ result }),
109
+ (error: unknown) => finish({ error: error instanceof Error ? error.message : String(error) }),
110
+ );
111
+ } catch (error) {
112
+ finish({ error: error instanceof Error ? error.message : String(error) });
113
+ }
114
+ });
115
+ return () => {
116
+ remove();
117
+ for (const call of pending.values()) call.controller.abort();
118
+ pending.clear();
119
+ };
120
+ }
@@ -5,7 +5,7 @@ import {
5
5
  } from "@letta-ai/letta-code/app-server-client";
6
6
  import type { ConversationRetrieveResponseMessage } from "@letta-ai/letta-code/app-server-protocol";
7
7
  import type { AppServerSessionOptions } from "./app-server-session.js";
8
- import { createExternalToolCallHandler, externalToolGroups } from "./external-tools.js";
8
+ import { createExternalToolCallHandler, externalToolGroups, registerExternalToolCallHandler } from "./external-tools.js";
9
9
  import { applyUniqueRequestIds } from "./request-ids.js";
10
10
  import {
11
11
  runtimeToolPolicy,
@@ -215,12 +215,12 @@ export class ToolRegistry {
215
215
  ? { requestTimeoutMs: this.options.requestTimeoutMs }
216
216
  : {}),
217
217
  }));
218
- client.onExternalToolCall(async (request) => {
218
+ registerExternalToolCallHandler(client, async (request, signal) => {
219
219
  const runtime = request.runtime;
220
220
  const entry = runtime &&
221
221
  [...this.entries].find((candidate) => candidate.runtimes.some((r) => runtimeKey(r) === runtimeKey(runtime)));
222
222
  if (!entry) throw new Error(`No tools registered for conversation ${runtime?.conversation_id}`);
223
- return entry.call(request);
223
+ return entry.call(request, signal);
224
224
  });
225
225
  try {
226
226
  await client.connect();