@agent-api/app-engine 0.0.4 → 0.0.6

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.
@@ -6,10 +6,12 @@ export interface WorkbenchInputKey {
6
6
  end?: boolean;
7
7
  escape?: boolean;
8
8
  home?: boolean;
9
+ leftArrow?: boolean;
9
10
  meta?: boolean;
10
11
  pageDown?: boolean;
11
12
  pageUp?: boolean;
12
13
  return?: boolean;
14
+ rightArrow?: boolean;
13
15
  upArrow?: boolean;
14
16
  }
15
17
  export type WorkbenchInputEffect = {
@@ -30,11 +32,13 @@ export type WorkbenchInputEffect = {
30
32
  type: "ignored_busy";
31
33
  };
32
34
  export interface WorkbenchInputResult {
35
+ cursor: number;
33
36
  draft: string;
34
37
  effects: WorkbenchInputEffect[];
35
38
  }
36
39
  export interface WorkbenchInputContext {
37
40
  busy: boolean;
41
+ cursor?: number;
38
42
  draft: string;
39
43
  viewportHeight: number;
40
44
  }
@@ -3,69 +3,102 @@ export function createWorkbenchInputController() {
3
3
  const history = createInputHistory();
4
4
  return {
5
5
  handle(input, key, context) {
6
+ const cursor = clampCursor(context.cursor ?? context.draft.length, context.draft);
6
7
  if (key.ctrl && input === "c")
7
- return result(context.draft, { type: "exit" });
8
+ return result(context.draft, cursor, { type: "exit" });
8
9
  if (key.pageUp || (key.ctrl && input === "u")) {
9
- return result(context.draft, { type: "scroll", delta: Math.max(1, Math.floor(context.viewportHeight / 2)) });
10
+ return result(context.draft, cursor, { type: "scroll", delta: Math.max(1, Math.floor(context.viewportHeight / 2)) });
10
11
  }
11
12
  if (key.pageDown || (key.ctrl && input === "d")) {
12
- return result(context.draft, { type: "scroll", delta: -Math.max(1, Math.floor(context.viewportHeight / 2)) });
13
+ return result(context.draft, cursor, { type: "scroll", delta: -Math.max(1, Math.floor(context.viewportHeight / 2)) });
13
14
  }
14
- if (key.home)
15
- return result(context.draft, { type: "scroll_top" });
16
- if (key.end)
17
- return result(context.draft, { type: "scroll_bottom" });
15
+ if (key.home || (key.ctrl && input === "a"))
16
+ return result(context.draft, 0);
17
+ if (key.end || (key.ctrl && input === "e"))
18
+ return result(context.draft, context.draft.length);
19
+ if (key.leftArrow)
20
+ return result(context.draft, Math.max(0, cursor - 1));
21
+ if (key.rightArrow)
22
+ return result(context.draft, Math.min(context.draft.length, cursor + 1));
18
23
  if (key.upArrow)
19
- return result(history.previous(context.draft));
24
+ return historyResult(history.previous(context.draft));
20
25
  if (key.downArrow)
21
- return result(history.next(context.draft));
26
+ return historyResult(history.next(context.draft));
22
27
  if (context.busy) {
23
- return handleBusyInput(input, key, context.draft, history);
28
+ return handleBusyInput(input, key, context.draft, cursor, history);
24
29
  }
25
- return handleReadyInput(input, key, context.draft, history);
30
+ return handleReadyInput(input, key, context.draft, cursor, history);
26
31
  },
27
32
  };
28
33
  }
29
- function handleBusyInput(input, key, draft, history) {
34
+ function handleBusyInput(input, key, draft, cursor, history) {
30
35
  if (key.escape)
31
- return result(draft, { type: "abort" });
36
+ return result(draft, cursor, { type: "abort" });
32
37
  if (key.return) {
33
38
  const command = draft.trim();
34
39
  history.record(command);
35
40
  if (command === "/abort" || command === "/cancel")
36
- return result("", { type: "abort" });
41
+ return result("", 0, { type: "abort" });
37
42
  if (command)
38
- return result("", { type: "ignored_busy" });
39
- return result("");
43
+ return result("", 0, { type: "ignored_busy" });
44
+ return result("", 0);
40
45
  }
41
- if (key.backspace || key.delete) {
46
+ if (key.backspace) {
42
47
  history.reset();
43
- return result(draft.slice(0, -1));
48
+ return deleteBeforeCursor(draft, cursor);
49
+ }
50
+ if (key.delete) {
51
+ history.reset();
52
+ return cursor >= draft.length ? deleteBeforeCursor(draft, cursor) : deleteAtCursor(draft, cursor);
44
53
  }
45
54
  if (input && !key.ctrl && !key.meta) {
46
55
  history.reset();
47
- return result(draft + input);
56
+ return insertAtCursor(draft, cursor, input);
48
57
  }
49
- return result(draft);
58
+ return result(draft, cursor);
50
59
  }
51
- function handleReadyInput(input, key, draft, history) {
60
+ function handleReadyInput(input, key, draft, cursor, history) {
52
61
  if (key.return) {
53
62
  const prompt = draft.trim();
54
63
  if (!prompt)
55
- return result(draft);
64
+ return result(draft, cursor);
56
65
  history.record(prompt);
57
- return result("", { type: "submit", input: prompt });
66
+ return result("", 0, { type: "submit", input: prompt });
58
67
  }
59
- if (key.backspace || key.delete) {
68
+ if (key.backspace) {
60
69
  history.reset();
61
- return result(draft.slice(0, -1));
70
+ return deleteBeforeCursor(draft, cursor);
71
+ }
72
+ if (key.delete) {
73
+ history.reset();
74
+ return cursor >= draft.length ? deleteBeforeCursor(draft, cursor) : deleteAtCursor(draft, cursor);
62
75
  }
63
76
  if (input && !key.ctrl && !key.meta) {
64
77
  history.reset();
65
- return result(draft + input);
78
+ return insertAtCursor(draft, cursor, input);
66
79
  }
67
- return result(draft);
80
+ return result(draft, cursor);
81
+ }
82
+ function result(draft, cursor, ...effects) {
83
+ return { cursor: clampCursor(cursor, draft), draft, effects };
84
+ }
85
+ function historyResult(draft) {
86
+ return result(draft, draft.length);
87
+ }
88
+ function insertAtCursor(draft, cursor, input) {
89
+ const next = `${draft.slice(0, cursor)}${input}${draft.slice(cursor)}`;
90
+ return result(next, cursor + input.length);
91
+ }
92
+ function deleteBeforeCursor(draft, cursor) {
93
+ if (cursor <= 0)
94
+ return result(draft, 0);
95
+ return result(`${draft.slice(0, cursor - 1)}${draft.slice(cursor)}`, cursor - 1);
96
+ }
97
+ function deleteAtCursor(draft, cursor) {
98
+ if (cursor >= draft.length)
99
+ return result(draft, cursor);
100
+ return result(`${draft.slice(0, cursor)}${draft.slice(cursor + 1)}`, cursor);
68
101
  }
69
- function result(draft, ...effects) {
70
- return { draft, effects };
102
+ function clampCursor(cursor, draft) {
103
+ return Math.max(0, Math.min(draft.length, cursor));
71
104
  }
@@ -22,7 +22,11 @@ export interface WorkbenchRenderModel {
22
22
  workdir: string;
23
23
  };
24
24
  input: {
25
+ afterCursor: string;
26
+ beforeCursor: string;
25
27
  busy: boolean;
28
+ cursor: number;
29
+ cursorText: string;
26
30
  draft: string;
27
31
  fullAccess: boolean;
28
32
  label: string;
@@ -36,6 +40,7 @@ export interface WorkbenchRenderModel {
36
40
  visibleActivities: WorkbenchState["activities"];
37
41
  }
38
42
  export interface BuildWorkbenchRenderModelInput {
43
+ cursor?: number;
39
44
  draft: string;
40
45
  profileName: string;
41
46
  spinnerFrame: number;
@@ -15,6 +15,10 @@ export function buildWorkbenchRenderModel(input) {
15
15
  viewportHeight,
16
16
  width: transcriptWidth,
17
17
  });
18
+ const cursor = Math.max(0, Math.min(input.draft.length, input.cursor ?? input.draft.length));
19
+ const beforeCursor = input.draft.slice(0, cursor);
20
+ const cursorText = input.draft[cursor] ?? " ";
21
+ const afterCursor = input.draft.slice(cursor + (cursor < input.draft.length ? 1 : 0));
18
22
  return {
19
23
  activityHeight,
20
24
  footerText: [
@@ -39,7 +43,11 @@ export function buildWorkbenchRenderModel(input) {
39
43
  workdir: input.state.workdir?.root || input.workdirFallback,
40
44
  },
41
45
  input: {
46
+ afterCursor,
47
+ beforeCursor,
42
48
  busy: input.state.busy,
49
+ cursor,
50
+ cursorText,
43
51
  draft: input.draft,
44
52
  fullAccess: input.state.accessMode === "full",
45
53
  label: input.state.busy ? "working" : "you",
@@ -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.4",
3
+ "version": "0.0.6",
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",