@celljs/ta-app-use 0.0.0-stage → 4.18.0

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/host/index.mjs ADDED
@@ -0,0 +1,156 @@
1
+ // web-src/app-protocol.ts
2
+ var NAME = "@celljs/ta-app-use";
3
+ var DONE_KEY = "done";
4
+ var DONE_MAX = 20;
5
+
6
+ // web-src/app-util.ts
7
+ var SCROLL_STEPS = ["down", "up", "top", "bottom"];
8
+ var DATA_CHARS = 8000;
9
+ var AppUseUtil;
10
+ ((AppUseUtil) => {
11
+ function find(commands, query) {
12
+ const words = (query ?? "").toLowerCase().split(/\s+/).filter(Boolean);
13
+ if (!words.length) {
14
+ return [...commands];
15
+ }
16
+ return commands.filter((command) => {
17
+ const text = `${command.id} ${command.description}`.toLowerCase();
18
+ return words.some((word) => text.includes(word));
19
+ });
20
+ }
21
+ AppUseUtil.find = find;
22
+ function listText(commands) {
23
+ if (!commands.length) {
24
+ return "No action matches; app_commands without a query lists them all.";
25
+ }
26
+ return commands.map((command) => {
27
+ const properties = command.input?.properties;
28
+ const required = command.input?.required;
29
+ const input = properties ? ` Input: ${JSON.stringify(properties)}${Array.isArray(required) && required.length ? `, required ${required.join(", ")}` : ""}.` : " No input.";
30
+ return `- ${command.id} (${command.risk}${command.owner ? `, of ${command.owner}` : ""}): ${command.description}${input}`;
31
+ }).join(`
32
+ `);
33
+ }
34
+ AppUseUtil.listText = listText;
35
+ function resultText(result) {
36
+ const lines = [result.text];
37
+ if (result.data !== undefined) {
38
+ const data = JSON.stringify(result.data);
39
+ lines.push(data.length > DATA_CHARS ? `${data.slice(0, DATA_CHARS)}… (cut; narrow it down)` : data);
40
+ }
41
+ if (result.notice) {
42
+ lines.push(`The user's pages show it already, and a notice with Undo.`);
43
+ }
44
+ return lines.join(`
45
+ `);
46
+ }
47
+ AppUseUtil.resultText = resultText;
48
+ function scroll(value) {
49
+ if (value === undefined || value === null || value === "") {
50
+ return;
51
+ }
52
+ if (typeof value === "string" && SCROLL_STEPS.includes(value.trim().toLowerCase())) {
53
+ return value.trim().toLowerCase();
54
+ }
55
+ if (value && typeof value === "object" && !Array.isArray(value)) {
56
+ const { item, find: find2 } = value;
57
+ if (typeof item === "string" && item.trim() && find2 === undefined) {
58
+ return { item: item.trim() };
59
+ }
60
+ if (typeof find2 === "string" && find2.trim() && item === undefined) {
61
+ return { find: find2.trim() };
62
+ }
63
+ }
64
+ throw new Error(`scroll takes ${SCROLL_STEPS.join(", ")}, { "item": "p1.7" } or { "find": "words of a row" }, not ${JSON.stringify(value)}.`);
65
+ }
66
+ AppUseUtil.scroll = scroll;
67
+ function add(done, entry) {
68
+ return [...entries(done), entry].slice(-DONE_MAX);
69
+ }
70
+ AppUseUtil.add = add;
71
+ function entries(done) {
72
+ return Array.isArray(done) ? done.filter((one) => !!one && typeof one === "object" && typeof one.id === "string" && typeof one.notice === "string" && typeof one.at === "number") : [];
73
+ }
74
+ AppUseUtil.entries = entries;
75
+ function fresh(done, since) {
76
+ return entries(done).filter((entry) => entry.at > since && !entry.undone);
77
+ }
78
+ AppUseUtil.fresh = fresh;
79
+ })(AppUseUtil ||= {});
80
+
81
+ // web-src/app-host.ts
82
+ var SCREEN_ASK_MS = 150000;
83
+
84
+ class AppUseHost {
85
+ host;
86
+ now;
87
+ writing = Promise.resolve();
88
+ count = 0;
89
+ constructor(host, now = () => Date.now()) {
90
+ this.host = host;
91
+ this.now = now;
92
+ }
93
+ commands(query) {
94
+ return AppUseUtil.find(this.host.commands.list(), query);
95
+ }
96
+ async run(id, input, sessionFile) {
97
+ const result = await this.host.commands.run(id, input ?? {}, sessionFile ? { sessionFile } : {});
98
+ if (result.notice) {
99
+ const entry = {
100
+ id: `${this.now().toString(36)}-${(this.count++).toString(36)}`,
101
+ at: this.now(),
102
+ command: id,
103
+ notice: result.notice,
104
+ ...result.changes?.length ? { changes: result.changes } : {},
105
+ ...result.undo ? { undo: result.undo } : {},
106
+ ...sessionFile ? { sessionFile } : {}
107
+ };
108
+ await this.change((done) => AppUseUtil.add(done, entry));
109
+ }
110
+ return result;
111
+ }
112
+ async screen(ask, sessionFile) {
113
+ const session = sessionFile ? (await this.host.listSessions()).find((one) => one.sessionFile === sessionFile) : undefined;
114
+ const from = { ...session ? { sessionId: session.sessionId } : {}, name: session?.sessionName || session?.firstMessage || "Ta" };
115
+ const answer = await this.host.pages.ask("screen", { ...ask, from }, { ...sessionFile ? { sessionFile } : {}, timeoutMs: SCREEN_ASK_MS });
116
+ if (answer && "command" in answer) {
117
+ const result = await this.run(answer.command, answer.input, sessionFile);
118
+ return `${result.text} (done by ${answer.command}, without the screen)`;
119
+ }
120
+ return answer?.text ?? "";
121
+ }
122
+ async undo(entryId) {
123
+ const entry = AppUseUtil.entries(await this.host.getExtensionData(NAME, DONE_KEY)).find((one) => one.id === entryId);
124
+ if (!entry?.undo) {
125
+ throw new Error("There is nothing to undo.");
126
+ }
127
+ if (entry.undone) {
128
+ return "Undone already.";
129
+ }
130
+ const result = await this.host.commands.run(entry.undo.command, entry.undo.input, { principal: this.host.team.current() });
131
+ await this.change((done) => AppUseUtil.entries(done).map((one) => one.id === entryId ? { ...one, undone: true } : one));
132
+ return result.text;
133
+ }
134
+ change(update) {
135
+ const next = this.writing.then(async () => {
136
+ const done = await this.host.getExtensionData(NAME, DONE_KEY);
137
+ await this.host.setExtensionData(NAME, DONE_KEY, update(done));
138
+ });
139
+ this.writing = next.catch(() => {
140
+ return;
141
+ });
142
+ return next;
143
+ }
144
+ }
145
+
146
+ // web-src/host.ts
147
+ function activate(host) {
148
+ const app = new AppUseHost(host);
149
+ return {
150
+ undo: (entryId) => app.undo(String(entryId)),
151
+ service: app
152
+ };
153
+ }
154
+ export {
155
+ activate as default
156
+ };
package/package.json CHANGED
@@ -1,6 +1,63 @@
1
1
  {
2
2
  "name": "@celljs/ta-app-use",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "4.18.0",
4
+ "description": "A pi package for Ta: agents do to Ta what the user does with its pages, through the catalog of actions of ta serve (settings, conversations, extensions, what waits for the user) with a notice and an Undo, or on a screen of Ta's own with a cursor, as the user would",
5
+ "keywords": [
6
+ "pi-package"
7
+ ],
8
+ "homepage": "https://github.com/cellbang/ta",
9
+ "bugs": {
10
+ "url": "https://github.com/cellbang/ta/issues"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/cellbang/cell.git"
15
+ },
16
+ "license": "MIT",
17
+ "files": [
18
+ "web",
19
+ "host",
20
+ "extension",
21
+ "!**/*.map"
22
+ ],
23
+ "scripts": {
24
+ "build": "tsc -b && bun build web-src/index.ts --format esm --target browser --outfile web/index.js && bun build web-src/host.ts --format esm --target node --outfile host/index.mjs && bun build web-src/extension.ts --format esm --target node --outfile extension/index.mjs",
25
+ "clean": "rm -rf lib web host extension *.tsbuildinfo",
26
+ "test": "bun test ./lib",
27
+ "watch": "tsc -b -w --preserveWatchOutput"
28
+ },
29
+ "devDependencies": {
30
+ "@celljs/ta-protocol": "4.18.0",
31
+ "@celljs/ta-web": "4.18.0"
32
+ },
33
+ "publishConfig": {
34
+ "access": "public"
35
+ },
36
+ "pi": {
37
+ "title": {
38
+ "en": "Ta does it for you",
39
+ "zh-CN": "替你操作",
40
+ "zh-TW": "替你操作",
41
+ "ja-JP": "代わりに操作"
42
+ },
43
+ "summary": {
44
+ "en": "Say \"turn on captions\" or \"pin that conversation\" and Ta does it at once, as you would on its pages; you see what it did, with an Undo. What only the screen can do, it does on a little screen of its own, which you can watch and stop.",
45
+ "zh-CN": "说一句\"打开字幕\"\"把那个对话置顶\",Ta 立刻替你做好,和你在界面上操作的结果一样;做了什么会提示你,还能撤销。只有界面能做的,它在自己的小屏幕上替你点,你随时能看、能停。",
46
+ "zh-TW": "說一句「打開字幕」「把那個對話置頂」,Ta 立刻替你做好,和你在介面上操作的結果一樣;做了什麼會提示你,還能復原。只有介面能做的,它在自己的小螢幕上替你點,你隨時能看、能停。",
47
+ "ja-JP": "「字幕をオンに」「その会話をピン留め」と言えば、Ta がすぐに画面で操作したのと同じように実行します。何をしたかは通知され、元に戻せます。画面でしかできないことは、自分の小さな画面で代わりに操作し、いつでも見たり止めたりできます。"
48
+ },
49
+ "icon": "MousePointerClick",
50
+ "web": [
51
+ "./web/index.js"
52
+ ],
53
+ "host": "./host/index.mjs",
54
+ "extensions": [
55
+ "./extension/index.mjs"
56
+ ],
57
+ "permissions": {
58
+ "host": {
59
+ "*": "extensions.use"
60
+ }
61
+ }
62
+ }
6
63
  }