@agent-api/app-engine 0.0.5 → 0.0.7

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.
@@ -115,3 +115,4 @@ export declare function agentResponseFailureMessage(response: AgentResponse): st
115
115
  export declare function agentTurnEventFromStreamEvent(event: ResponseStreamEvent): AgentTurnEvent | null;
116
116
  export declare function resolveAgentRequestTools(client: PresetToolCatalogClient, preset?: string, tools?: readonly Tool[], options?: ResolveAgentRequestToolsOptions): Promise<Tool[] | undefined>;
117
117
  export declare function clearPresetToolCatalogCache(baseURL?: string): void;
118
+ export declare function localToolExecutionErrorResult(toolName: string, args: Record<string, unknown>, error: unknown): Record<string, unknown>;
@@ -425,11 +425,19 @@ async function executeLocalFunctionCalls(response, registry, abortSignal, onEven
425
425
  const outputs = [];
426
426
  for (const call of pendingFunctionCalls(response)) {
427
427
  throwIfAborted(abortSignal);
428
- if (!registry.has(call.name)) {
429
- throw new Error(`no local handler registered for function ${call.name}`);
428
+ let args = {};
429
+ let result;
430
+ try {
431
+ args = call.arguments ? JSON.parse(call.arguments) : {};
432
+ if (!registry.has(call.name)) {
433
+ throw new Error(`no local handler registered for function ${call.name}`);
434
+ }
435
+ result = await registry.execute(call.name, args, abortSignal);
436
+ }
437
+ catch (error) {
438
+ throwIfAborted(abortSignal);
439
+ result = localToolExecutionErrorResult(call.name, args, error);
430
440
  }
431
- const args = call.arguments ? JSON.parse(call.arguments) : {};
432
- const result = await registry.execute(call.name, args, abortSignal);
433
441
  throwIfAborted(abortSignal);
434
442
  const action = typeof result.action === "string"
435
443
  ? result.action
@@ -461,11 +469,34 @@ async function executeLocalFunctionCalls(response, registry, abortSignal, onEven
461
469
  }
462
470
  return { outputs };
463
471
  }
472
+ export function localToolExecutionErrorResult(toolName, args, error) {
473
+ return {
474
+ ok: false,
475
+ tool: toolName,
476
+ action: typeof args.action === "string" ? args.action : undefined,
477
+ error: {
478
+ message: userFacingError(error),
479
+ name: error instanceof Error ? error.name : undefined,
480
+ code: errorCode(error),
481
+ },
482
+ };
483
+ }
464
484
  function throwIfAborted(signal) {
465
485
  if (!signal?.aborted)
466
486
  return;
467
487
  throw new Error("Agent turn aborted.");
468
488
  }
489
+ function userFacingError(error) {
490
+ if (error instanceof Error)
491
+ return error.message;
492
+ return String(error);
493
+ }
494
+ function errorCode(error) {
495
+ if (typeof error !== "object" || error === null)
496
+ return undefined;
497
+ const code = error.code;
498
+ return typeof code === "string" || typeof code === "number" ? code : undefined;
499
+ }
469
500
  function requestAbortOptions(signal) {
470
501
  return signal ? { signal } : undefined;
471
502
  }
package/dist/agent.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { type AgentRunOptions, type AgentTurnEvent, type AgentTurnResult, type LocalToolApprovalRequest, type WorkdirAccessMode, agentResponseFailureMessage, agentTurnEventFromStreamEvent, clearPresetToolCatalogCache, isAvailablePreset, listAvailablePresets, resolveAgentRequestTools, resumeAgentAfterLocalApproval, runAgent, runAgentTurn, } from "./agent/runner.js";
1
+ export { type AgentRunOptions, type AgentTurnEvent, type AgentTurnResult, type LocalToolApprovalRequest, type WorkdirAccessMode, agentResponseFailureMessage, agentTurnEventFromStreamEvent, clearPresetToolCatalogCache, isAvailablePreset, listAvailablePresets, localToolExecutionErrorResult, resolveAgentRequestTools, resumeAgentAfterLocalApproval, runAgent, runAgentTurn, } from "./agent/runner.js";
2
2
  export { conversationSummary, deleteConversation, ensureConversation, getConversation, listConversations, startFreshConversation, } from "./conversation/index.js";
package/dist/agent.js CHANGED
@@ -1,2 +1,2 @@
1
- export { agentResponseFailureMessage, agentTurnEventFromStreamEvent, clearPresetToolCatalogCache, isAvailablePreset, listAvailablePresets, resolveAgentRequestTools, resumeAgentAfterLocalApproval, runAgent, runAgentTurn, } from "./agent/runner.js";
1
+ export { agentResponseFailureMessage, agentTurnEventFromStreamEvent, clearPresetToolCatalogCache, isAvailablePreset, listAvailablePresets, localToolExecutionErrorResult, resolveAgentRequestTools, resumeAgentAfterLocalApproval, runAgent, runAgentTurn, } from "./agent/runner.js";
2
2
  export { conversationSummary, deleteConversation, ensureConversation, getConversation, listConversations, startFreshConversation, } from "./conversation/index.js";
@@ -1,7 +1,7 @@
1
1
  export type { AgentEngineApp, AgentEngineAppOptions, AgentEngineLifecycleOptions, } from "./agent-engine.js";
2
2
  export { createAgentEngine, } from "./agent-engine.js";
3
3
  export type { AgentRunOptions, AgentTurnEvent, LocalToolApprovalRequest, WorkdirAccessMode, } from "../agent.js";
4
- export { agentResponseFailureMessage, agentTurnEventFromStreamEvent, clearPresetToolCatalogCache, conversationSummary, deleteConversation, ensureConversation, getConversation, isAvailablePreset, listAvailablePresets, listConversations, resumeAgentAfterLocalApproval, runAgent, resolveAgentRequestTools, runAgentTurn, startFreshConversation, } from "../agent.js";
4
+ export { agentResponseFailureMessage, agentTurnEventFromStreamEvent, clearPresetToolCatalogCache, conversationSummary, deleteConversation, ensureConversation, getConversation, isAvailablePreset, listAvailablePresets, listConversations, localToolExecutionErrorResult, resumeAgentAfterLocalApproval, runAgent, resolveAgentRequestTools, runAgentTurn, startFreshConversation, } from "../agent.js";
5
5
  export type { ChatOptions } from "../chat-options.js";
6
6
  export { normalizeChatOptions } from "../chat-options.js";
7
7
  export type { AgentEngineServices } from "./services.js";
@@ -1,5 +1,5 @@
1
1
  export { createAgentEngine, } from "./agent-engine.js";
2
- export { agentResponseFailureMessage, agentTurnEventFromStreamEvent, clearPresetToolCatalogCache, conversationSummary, deleteConversation, ensureConversation, getConversation, isAvailablePreset, listAvailablePresets, listConversations, resumeAgentAfterLocalApproval, runAgent, resolveAgentRequestTools, runAgentTurn, startFreshConversation, } from "../agent.js";
2
+ export { agentResponseFailureMessage, agentTurnEventFromStreamEvent, clearPresetToolCatalogCache, conversationSummary, deleteConversation, ensureConversation, getConversation, isAvailablePreset, listAvailablePresets, listConversations, localToolExecutionErrorResult, resumeAgentAfterLocalApproval, runAgent, resolveAgentRequestTools, runAgentTurn, startFreshConversation, } from "../agent.js";
3
3
  export { normalizeChatOptions } from "../chat-options.js";
4
4
  export { defaultBaseURL } from "../config.js";
5
5
  export { activeProfile, loadAppConfiguration, loadConfig, loadConversationConfiguration, loadWorkbenchPreferences, redactSecret, saveAppConfiguration, saveConfig, saveConversationConfiguration, updateWorkbenchPreferences, } from "../config.js";
@@ -49,7 +49,7 @@ function handleBusyInput(input, key, draft, cursor, history) {
49
49
  }
50
50
  if (key.delete) {
51
51
  history.reset();
52
- return deleteAtCursor(draft, cursor);
52
+ return cursor >= draft.length ? deleteBeforeCursor(draft, cursor) : deleteAtCursor(draft, cursor);
53
53
  }
54
54
  if (input && !key.ctrl && !key.meta) {
55
55
  history.reset();
@@ -71,7 +71,7 @@ function handleReadyInput(input, key, draft, cursor, history) {
71
71
  }
72
72
  if (key.delete) {
73
73
  history.reset();
74
- return deleteAtCursor(draft, cursor);
74
+ return cursor >= draft.length ? deleteBeforeCursor(draft, cursor) : deleteAtCursor(draft, cursor);
75
75
  }
76
76
  if (input && !key.ctrl && !key.meta) {
77
77
  history.reset();
@@ -93,18 +93,72 @@ function wrapTranscriptText(text, width) {
93
93
  const max = Math.max(12, width);
94
94
  if (text.length === 0)
95
95
  return [""];
96
+ if (displayWidth(text) <= max)
97
+ return [text];
96
98
  const lines = [];
97
99
  let rest = text;
98
- while (rest.length > max) {
99
- const hard = rest.slice(0, max);
100
- const softBreak = Math.max(hard.lastIndexOf(" "), hard.lastIndexOf("\t"));
101
- const index = softBreak > Math.floor(max * 0.45) ? softBreak : max;
102
- lines.push(rest.slice(0, index).trimEnd());
100
+ while (displayWidth(rest) > max) {
101
+ const hard = takeColumns(rest, max);
102
+ const softBreak = Math.max(hard.text.lastIndexOf(" "), hard.text.lastIndexOf("\t"));
103
+ const soft = softBreak > 0 ? hard.text.slice(0, softBreak) : "";
104
+ const useSoftBreak = soft && displayWidth(soft) > Math.floor(max * 0.45);
105
+ const chunk = useSoftBreak ? soft : hard.text;
106
+ const index = useSoftBreak ? softBreak : hard.length;
107
+ lines.push(chunk.trimEnd());
103
108
  rest = rest.slice(index).trimStart();
104
109
  }
105
110
  lines.push(rest);
106
111
  return lines;
107
112
  }
113
+ function takeColumns(text, maxColumns) {
114
+ let length = 0;
115
+ let output = "";
116
+ let columns = 0;
117
+ for (const char of Array.from(text)) {
118
+ const width = charWidth(char);
119
+ if (output && columns + width > maxColumns)
120
+ break;
121
+ output += char;
122
+ length += char.length;
123
+ columns += width;
124
+ if (columns >= maxColumns)
125
+ break;
126
+ }
127
+ return { length, text: output };
128
+ }
129
+ function displayWidth(text) {
130
+ let width = 0;
131
+ for (const char of Array.from(text)) {
132
+ width += charWidth(char);
133
+ }
134
+ return width;
135
+ }
136
+ function charWidth(char) {
137
+ if (!char)
138
+ return 0;
139
+ const code = char.codePointAt(0) ?? 0;
140
+ if (code === 0)
141
+ return 0;
142
+ if (code < 32 || (code >= 0x7f && code < 0xa0))
143
+ return 0;
144
+ if (/^\p{Mark}$/u.test(char))
145
+ return 0;
146
+ return isWideCodePoint(code) ? 2 : 1;
147
+ }
148
+ function isWideCodePoint(code) {
149
+ return (code >= 0x1100 && (code <= 0x115f ||
150
+ code === 0x2329 ||
151
+ code === 0x232a ||
152
+ (code >= 0x2e80 && code <= 0xa4cf && code !== 0x303f) ||
153
+ (code >= 0xac00 && code <= 0xd7a3) ||
154
+ (code >= 0xf900 && code <= 0xfaff) ||
155
+ (code >= 0xfe10 && code <= 0xfe19) ||
156
+ (code >= 0xfe30 && code <= 0xfe6f) ||
157
+ (code >= 0xff00 && code <= 0xff60) ||
158
+ (code >= 0xffe0 && code <= 0xffe6) ||
159
+ (code >= 0x1f300 && code <= 0x1faff) ||
160
+ (code >= 0x20000 && code <= 0x3fffd)));
161
+ }
108
162
  function roleLabel(role) {
109
163
  if (role === "user")
110
164
  return "You";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agent-api/app-engine",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "Renderer-neutral application engine for Agent API apps",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/scalebox-dev/agent-tui#readme",
@@ -29,7 +29,7 @@
29
29
  "pack:local": "npm pack --pack-destination ./artifacts"
30
30
  },
31
31
  "dependencies": {
32
- "@agent-api/sdk": "^1.3.0",
32
+ "@agent-api/sdk": "^1.3.1",
33
33
  "zod": "^4.4.3"
34
34
  },
35
35
  "devDependencies": {