@teable/agent-gateway 1.0.7 → 1.0.8
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/README.md +10 -7
- package/dist/chunks/{XGVR5ONA.js → EPBUNSHS.js} +6 -0
- package/dist/cli.js +264 -26
- package/dist/gateway.d.ts +1 -1
- package/dist/platform.d.ts +17 -1
- package/dist/protocol.d.ts +36 -1
- package/dist/protocol.js +7 -1
- package/dist/uploads.d.ts +18 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,13 +18,13 @@ code 1): then only a fresh start with a fresh token helps.
|
|
|
18
18
|
It runs with Bun or Node 22.4 and later, and installs nothing. Teable starts
|
|
19
19
|
it with:
|
|
20
20
|
|
|
21
|
-
| Variable | What
|
|
22
|
-
| --------------------------- |
|
|
23
|
-
| `TEABLE_NEXT_AGENT_URL` | The hub: `wss://<Teable host>/api/v4/agent/gateway`
|
|
24
|
-
| `TEABLE_NEXT_GATEWAY_TOKEN` | The gateway token the hub accepts
|
|
25
|
-
| `TEABLE_ENDPOINT` | Teable, as the sandbox reaches it
|
|
26
|
-
| `TEABLE_TOKEN` | The person's own token
|
|
27
|
-
| `TEABLE_NEXT_ENDPOINT` | Optional: where teable-next is served, when `TEABLE_ENDPOINT` is internal
|
|
21
|
+
| Variable | What |
|
|
22
|
+
| --------------------------- | -------------------------------------------------------------------------------- |
|
|
23
|
+
| `TEABLE_NEXT_AGENT_URL` | The hub: `wss://<Teable host>/api/v4/agent/gateway` |
|
|
24
|
+
| `TEABLE_NEXT_GATEWAY_TOKEN` | The gateway token the hub accepts |
|
|
25
|
+
| `TEABLE_ENDPOINT` | Teable, as the sandbox reaches it |
|
|
26
|
+
| `TEABLE_TOKEN` | The person's own token: model tokens, who they are (git's author), `teable-next` |
|
|
27
|
+
| `TEABLE_NEXT_ENDPOINT` | Optional: where teable-next is served, when `TEABLE_ENDPOINT` is internal |
|
|
28
28
|
|
|
29
29
|
Apps are checked out in `~/apps/<appId>` with `teable-next clone`
|
|
30
30
|
([`@teable/next-cli`](https://www.npmjs.com/package/@teable/next-cli)); the
|
|
@@ -35,6 +35,9 @@ It acts without asking, but when it asks the person a question, or to approve
|
|
|
35
35
|
its plan, the gateway puts that to them in Teable's builder and gives the
|
|
36
36
|
answer back. A message the person sends while it works goes into what it is
|
|
37
37
|
doing, and its background tasks show in the builder as they run and end.
|
|
38
|
+
Files the person attaches to a message are fetched from Teable into
|
|
39
|
+
`~/uploads/<session>/` before the agent reads it, and the message links them
|
|
40
|
+
there; an image also goes to an agent that takes images.
|
|
38
41
|
|
|
39
42
|
`@teable/agent-gateway/protocol` holds the types of what travels between the
|
|
40
43
|
gateway, the hub and the builder.
|
|
@@ -57,6 +57,9 @@ var TAB_CLOSE = {
|
|
|
57
57
|
rateLimited: 4429
|
|
58
58
|
};
|
|
59
59
|
var SESSION_ID = /^ses[0-9A-Za-z]{16}$/;
|
|
60
|
+
var AGENT_UPLOADS = "/agent/uploads";
|
|
61
|
+
var UPLOAD_LIMITS = { bytes: 20971520, files: 10 };
|
|
62
|
+
var UPLOAD_PATH = /^\/api\/v4\/apps\/[^/]+\/agent\/uploads\/[0-9a-f-]{36}\/[^/]+$/;
|
|
60
63
|
var ACP = {
|
|
61
64
|
initialize: "initialize",
|
|
62
65
|
newSession: "session/new",
|
|
@@ -101,6 +104,9 @@ export {
|
|
|
101
104
|
shrink,
|
|
102
105
|
TAB_CLOSE,
|
|
103
106
|
SESSION_ID,
|
|
107
|
+
AGENT_UPLOADS,
|
|
108
|
+
UPLOAD_LIMITS,
|
|
109
|
+
UPLOAD_PATH,
|
|
104
110
|
ACP,
|
|
105
111
|
ASK,
|
|
106
112
|
NOTICE,
|
package/dist/cli.js
CHANGED
|
@@ -12,21 +12,23 @@ import {
|
|
|
12
12
|
PONG,
|
|
13
13
|
SESSION_ID,
|
|
14
14
|
TASK_OUTPUT,
|
|
15
|
+
UPLOAD_LIMITS,
|
|
16
|
+
UPLOAD_PATH,
|
|
15
17
|
isAcpNotification,
|
|
16
18
|
isAcpRequest,
|
|
17
19
|
isAcpResponse,
|
|
18
20
|
shrink
|
|
19
|
-
} from "./chunks/
|
|
21
|
+
} from "./chunks/EPBUNSHS.js";
|
|
20
22
|
|
|
21
23
|
// src/cli.ts
|
|
22
24
|
import { readFileSync } from "node:fs";
|
|
23
25
|
import { homedir } from "node:os";
|
|
24
|
-
import { join as
|
|
26
|
+
import { join as join5 } from "node:path";
|
|
25
27
|
|
|
26
28
|
// src/gateway.ts
|
|
27
|
-
import { randomUUID } from "node:crypto";
|
|
29
|
+
import { randomUUID as randomUUID2 } from "node:crypto";
|
|
28
30
|
import { rmSync } from "node:fs";
|
|
29
|
-
import { join as
|
|
31
|
+
import { join as join4 } from "node:path";
|
|
30
32
|
|
|
31
33
|
// src/agent.ts
|
|
32
34
|
import { spawn } from "node:child_process";
|
|
@@ -573,11 +575,19 @@ var APP_DIRECTORY = /^[A-Za-z0-9_-]+$/;
|
|
|
573
575
|
var CLONE_TIMEOUT_MS = 5 * 6e4;
|
|
574
576
|
var RULES_MARK = "[Teable]";
|
|
575
577
|
var where = (appId) => `${RULES_MARK} You work in a person's Teable sandbox, on the Teable app ${appId}, in this directory.`;
|
|
578
|
+
var NAME_IT = 'once you know what the app is, name the project after it, in the person\'s language, with `teable-next rename "<name>"`.';
|
|
576
579
|
function newProject(appId) {
|
|
577
580
|
return [
|
|
578
581
|
where(appId),
|
|
579
582
|
"The project has no code yet, so the directory is empty. When you build the app, start it",
|
|
580
|
-
`here with \`teable-next init . --project ${appId}
|
|
583
|
+
`here with \`teable-next init . --project ${appId}\`. If it prints the projectName as null,`,
|
|
584
|
+
`nobody has named the project yet: ${NAME_IT}`
|
|
585
|
+
].join(" ");
|
|
586
|
+
}
|
|
587
|
+
function unnamed(appId) {
|
|
588
|
+
return [
|
|
589
|
+
where(appId),
|
|
590
|
+
`Nobody has named the project yet (Teable lists it as untitled): ${NAME_IT}`
|
|
581
591
|
].join(" ");
|
|
582
592
|
}
|
|
583
593
|
function cliMissing(appId) {
|
|
@@ -597,10 +607,10 @@ function cloneFailed(appId, output) {
|
|
|
597
607
|
output.trim().slice(-2e3) || "(nothing)"
|
|
598
608
|
].join(" ");
|
|
599
609
|
}
|
|
600
|
-
function
|
|
610
|
+
function fieldOf(stdout, field) {
|
|
601
611
|
try {
|
|
602
612
|
const result = JSON.parse(stdout);
|
|
603
|
-
return typeof result === "object" && result !== null &&
|
|
613
|
+
return typeof result === "object" && result !== null && field in result ? result[field] : void 0;
|
|
604
614
|
} catch {
|
|
605
615
|
return void 0;
|
|
606
616
|
}
|
|
@@ -612,9 +622,10 @@ function clone(appId, dir, env) {
|
|
|
612
622
|
["clone", appId, dir],
|
|
613
623
|
{ env, timeout: CLONE_TIMEOUT_MS, maxBuffer: 16 << 20 },
|
|
614
624
|
(error, stdout, stderr) => {
|
|
615
|
-
if (error === null)
|
|
625
|
+
if (error === null)
|
|
626
|
+
resolve({ kind: "cloned", named: fieldOf(stdout, "projectName") !== null });
|
|
616
627
|
else if (error.code === "ENOENT") resolve({ kind: "missing" });
|
|
617
|
-
else if (
|
|
628
|
+
else if (fieldOf(stdout, "code") === "NO_CODE") resolve({ kind: "empty" });
|
|
618
629
|
else resolve({ kind: "failed", output: `${stdout}${stderr}`.trim() || error.message });
|
|
619
630
|
}
|
|
620
631
|
);
|
|
@@ -633,7 +644,9 @@ function checkout(home2, appId, env, log2) {
|
|
|
633
644
|
mkdirSync2(join2(home2, "apps"), { recursive: true });
|
|
634
645
|
log2(`cloning ${appId} into ${cwd}`);
|
|
635
646
|
const cloned = await clone(appId, cwd, env);
|
|
636
|
-
if (cloned.kind === "cloned" && existsSync(cwd))
|
|
647
|
+
if (cloned.kind === "cloned" && existsSync(cwd)) {
|
|
648
|
+
return { cwd, rules: cloned.named ? null : unnamed(appId) };
|
|
649
|
+
}
|
|
637
650
|
mkdirSync2(cwd, { recursive: true });
|
|
638
651
|
switch (cloned.kind) {
|
|
639
652
|
case "empty":
|
|
@@ -653,6 +666,114 @@ function checkout(home2, appId, env, log2) {
|
|
|
653
666
|
return made.finally(() => cloning.delete(cwd));
|
|
654
667
|
}
|
|
655
668
|
|
|
669
|
+
// src/uploads.ts
|
|
670
|
+
import { randomUUID } from "node:crypto";
|
|
671
|
+
import { mkdir, open, readFile, rename, rm } from "node:fs/promises";
|
|
672
|
+
import { basename, join as join3 } from "node:path";
|
|
673
|
+
import { pathToFileURL } from "node:url";
|
|
674
|
+
var IMAGE_BYTES = 5 * 1024 * 1024;
|
|
675
|
+
var FETCH_MS = 6e4;
|
|
676
|
+
function uploadPath(block) {
|
|
677
|
+
if (typeof block !== "object" || block === null) return null;
|
|
678
|
+
if (block.type !== "resource_link" || typeof block.uri !== "string") return null;
|
|
679
|
+
let url;
|
|
680
|
+
try {
|
|
681
|
+
url = new URL(block.uri);
|
|
682
|
+
} catch {
|
|
683
|
+
return null;
|
|
684
|
+
}
|
|
685
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") return null;
|
|
686
|
+
return UPLOAD_PATH.test(url.pathname) ? url.pathname : null;
|
|
687
|
+
}
|
|
688
|
+
function linksUploads(prompt) {
|
|
689
|
+
return prompt.some((block) => uploadPath(block) !== null);
|
|
690
|
+
}
|
|
691
|
+
async function fetchUploads(prompt, fetching) {
|
|
692
|
+
const blocks = await Promise.all(
|
|
693
|
+
prompt.map((block) => {
|
|
694
|
+
const path = uploadPath(block);
|
|
695
|
+
return path === null ? [block] : fetchUpload(block, path, fetching);
|
|
696
|
+
})
|
|
697
|
+
);
|
|
698
|
+
return blocks.flat();
|
|
699
|
+
}
|
|
700
|
+
async function fetchUpload(link, path, fetching) {
|
|
701
|
+
const [id = "", encoded = ""] = path.split("/").slice(-2);
|
|
702
|
+
try {
|
|
703
|
+
let name = "";
|
|
704
|
+
try {
|
|
705
|
+
name = decodeURIComponent(encoded);
|
|
706
|
+
} catch {
|
|
707
|
+
}
|
|
708
|
+
if (name === "" || name === "." || name === ".." || name !== basename(name) || name.includes("\0")) {
|
|
709
|
+
throw new Error("its name is not one a file can have");
|
|
710
|
+
}
|
|
711
|
+
const response = await fetch(`${fetching.origin}${path}`, {
|
|
712
|
+
headers: { Authorization: `Bearer ${fetching.token}` },
|
|
713
|
+
// A redirect would take the token elsewhere.
|
|
714
|
+
redirect: "error",
|
|
715
|
+
signal: AbortSignal.timeout(fetching.timeoutMs ?? FETCH_MS)
|
|
716
|
+
});
|
|
717
|
+
if (!response.ok || response.body === null) {
|
|
718
|
+
throw new Error(`the runtime answered ${response.status}`);
|
|
719
|
+
}
|
|
720
|
+
const dir = join3(fetching.dir, id);
|
|
721
|
+
await mkdir(dir, { recursive: true, mode: 448 });
|
|
722
|
+
const file = join3(dir, name);
|
|
723
|
+
const { size, head } = await save(response.body, file);
|
|
724
|
+
const uri = pathToFileURL(file).href;
|
|
725
|
+
const blocks = [{ ...link, uri, size }];
|
|
726
|
+
const image = fetching.images && size <= IMAGE_BYTES ? imageType(head) : null;
|
|
727
|
+
if (image !== null) {
|
|
728
|
+
const data = (await readFile(file)).toString("base64");
|
|
729
|
+
blocks.push({ type: "image", data, mimeType: image, uri });
|
|
730
|
+
}
|
|
731
|
+
return blocks;
|
|
732
|
+
} catch (error) {
|
|
733
|
+
const why = error instanceof Error ? error.message : String(error);
|
|
734
|
+
fetching.log(`could not fetch ${link.name}: ${why}`);
|
|
735
|
+
return [
|
|
736
|
+
{
|
|
737
|
+
type: "text",
|
|
738
|
+
text: `(The person attached ${link.name}, but it could not be fetched into the sandbox: ${why}.)`
|
|
739
|
+
}
|
|
740
|
+
];
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
var HEAD = 12;
|
|
744
|
+
async function save(body, file) {
|
|
745
|
+
const partial = `${file}.${randomUUID()}.part`;
|
|
746
|
+
const handle = await open(partial, "w", 384);
|
|
747
|
+
let size = 0;
|
|
748
|
+
const head = new Uint8Array(HEAD);
|
|
749
|
+
try {
|
|
750
|
+
for await (const chunk of body) {
|
|
751
|
+
if (size < HEAD) head.set(chunk.subarray(0, HEAD - size), size);
|
|
752
|
+
size += chunk.byteLength;
|
|
753
|
+
if (size > UPLOAD_LIMITS.bytes) {
|
|
754
|
+
throw new Error(`it is larger than ${UPLOAD_LIMITS.bytes} bytes`);
|
|
755
|
+
}
|
|
756
|
+
await handle.write(chunk);
|
|
757
|
+
}
|
|
758
|
+
await handle.close();
|
|
759
|
+
await rename(partial, file);
|
|
760
|
+
} catch (error) {
|
|
761
|
+
await handle.close().catch(() => {
|
|
762
|
+
});
|
|
763
|
+
await rm(partial, { force: true });
|
|
764
|
+
throw error;
|
|
765
|
+
}
|
|
766
|
+
return { size, head: head.subarray(0, Math.min(size, HEAD)) };
|
|
767
|
+
}
|
|
768
|
+
function imageType(head) {
|
|
769
|
+
const starts = (bytes, at = 0) => bytes.every((byte, i) => head[at + i] === byte);
|
|
770
|
+
if (starts([137, 80, 78, 71, 13, 10, 26, 10])) return "image/png";
|
|
771
|
+
if (starts([255, 216, 255])) return "image/jpeg";
|
|
772
|
+
if (starts([71, 73, 70, 56])) return "image/gif";
|
|
773
|
+
if (starts([82, 73, 70, 70]) && starts([87, 69, 66, 80], 8)) return "image/webp";
|
|
774
|
+
return null;
|
|
775
|
+
}
|
|
776
|
+
|
|
656
777
|
// src/gateway.ts
|
|
657
778
|
var TIMING = {
|
|
658
779
|
reconnectMinMs: 1e3,
|
|
@@ -676,14 +797,20 @@ function namedAfterRules(message) {
|
|
|
676
797
|
const update = typeof params === "object" && params !== null && "update" in params ? params.update : void 0;
|
|
677
798
|
return typeof update === "object" && update !== null && "sessionUpdate" in update && update.sessionUpdate === "session_info_update" && "title" in update && typeof update.title === "string" && update.title.trimStart().startsWith(RULES_MARK);
|
|
678
799
|
}
|
|
679
|
-
function
|
|
800
|
+
function promptIn(message) {
|
|
801
|
+
if (!isAcpRequest(message) || message.method !== ACP.prompt) return void 0;
|
|
802
|
+
const prompt = message.params?.prompt;
|
|
803
|
+
return Array.isArray(prompt) ? prompt : void 0;
|
|
804
|
+
}
|
|
805
|
+
function withPrompt(message, prompt) {
|
|
680
806
|
if (!isAcpRequest(message)) return message;
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
807
|
+
return { ...message, params: { ...message.params, prompt } };
|
|
808
|
+
}
|
|
809
|
+
function withRules(message, rules) {
|
|
810
|
+
return withPrompt(message, [{ type: "text", text: rules }, ...promptIn(message) ?? []]);
|
|
684
811
|
}
|
|
685
812
|
var Gateway = class {
|
|
686
|
-
instance =
|
|
813
|
+
instance = randomUUID2();
|
|
687
814
|
#options;
|
|
688
815
|
#timing;
|
|
689
816
|
#sessions = /* @__PURE__ */ new Map();
|
|
@@ -693,6 +820,11 @@ var Gateway = class {
|
|
|
693
820
|
#unacked = [];
|
|
694
821
|
/** The number of the last of the hub's frames taken. */
|
|
695
822
|
#received = 0;
|
|
823
|
+
/**
|
|
824
|
+
* Per session, what the hub sent behind a prompt whose files are being
|
|
825
|
+
* fetched: it all reaches the agent in the order the hub sent it.
|
|
826
|
+
*/
|
|
827
|
+
#waiting = /* @__PURE__ */ new Map();
|
|
696
828
|
/** Agents' output waits while the hub lacks too much. */
|
|
697
829
|
#paused = false;
|
|
698
830
|
#socket = null;
|
|
@@ -704,13 +836,15 @@ var Gateway = class {
|
|
|
704
836
|
#pong = null;
|
|
705
837
|
#touching = null;
|
|
706
838
|
#lastTouch = 0;
|
|
839
|
+
/** git's author and committer for agents and the CLI (#git); null until asked, or after a failed ask. */
|
|
840
|
+
#gitEnv = null;
|
|
707
841
|
#stopped = false;
|
|
708
842
|
constructor(options) {
|
|
709
843
|
this.#options = options;
|
|
710
844
|
this.#timing = { ...TIMING, ...options.timing };
|
|
711
845
|
}
|
|
712
846
|
start() {
|
|
713
|
-
rmSync(
|
|
847
|
+
rmSync(join4(this.#options.stateDir, "sessions"), { recursive: true, force: true });
|
|
714
848
|
this.#connect();
|
|
715
849
|
}
|
|
716
850
|
/** Stop: the socket, every agent, every timer. */
|
|
@@ -972,7 +1106,7 @@ var Gateway = class {
|
|
|
972
1106
|
acpSessionId: null,
|
|
973
1107
|
rules: null,
|
|
974
1108
|
opened: null,
|
|
975
|
-
tokenFile:
|
|
1109
|
+
tokenFile: join4(stateDir, "sessions", id, "model-token"),
|
|
976
1110
|
refresh: null,
|
|
977
1111
|
idle: null,
|
|
978
1112
|
closed: false
|
|
@@ -980,7 +1114,8 @@ var Gateway = class {
|
|
|
980
1114
|
this.#sessions.set(id, session);
|
|
981
1115
|
log2(`${id}: starting ${agent} for ${appId}${acpSessionId ? `, resuming ${acpSessionId}` : ""}`);
|
|
982
1116
|
try {
|
|
983
|
-
const
|
|
1117
|
+
const env = await this.#agentEnv();
|
|
1118
|
+
const { cwd, rules } = await checkout(this.#options.home, appId, env, log2);
|
|
984
1119
|
const key = await this.#options.platform.modelKey(appId);
|
|
985
1120
|
this.#keep(session, key);
|
|
986
1121
|
const definition = AGENTS[agent];
|
|
@@ -990,7 +1125,7 @@ var Gateway = class {
|
|
|
990
1125
|
args: definition.args,
|
|
991
1126
|
cwd,
|
|
992
1127
|
env: {
|
|
993
|
-
...
|
|
1128
|
+
...env,
|
|
994
1129
|
...definition.prepare({ stateDir, key, tokenFile: session.tokenFile })
|
|
995
1130
|
},
|
|
996
1131
|
onMessage: (message) => this.#fromAgent(session, message),
|
|
@@ -1068,15 +1203,70 @@ var Gateway = class {
|
|
|
1068
1203
|
if (session.refresh) clearTimeout(session.refresh);
|
|
1069
1204
|
if (session.idle) clearTimeout(session.idle);
|
|
1070
1205
|
session.process?.kill();
|
|
1071
|
-
rmSync(
|
|
1206
|
+
rmSync(join4(this.#options.stateDir, "sessions", session.session), {
|
|
1072
1207
|
recursive: true,
|
|
1073
1208
|
force: true
|
|
1074
1209
|
});
|
|
1075
1210
|
this.#send({ type: as, session: session.session, message });
|
|
1076
1211
|
this.#touch();
|
|
1077
1212
|
}
|
|
1213
|
+
/**
|
|
1214
|
+
* Hand the hub's message for a session's agent on (`go`) in the order the
|
|
1215
|
+
* hub sent it: a prompt that links files the person sent waits until they
|
|
1216
|
+
* are in the sandbox (uploads.ts), and what the hub sends the session
|
|
1217
|
+
* meanwhile waits behind it. Anything else, with nothing waiting, goes at
|
|
1218
|
+
* once.
|
|
1219
|
+
*/
|
|
1220
|
+
#inOrder(id, prompt, go) {
|
|
1221
|
+
const before = this.#waiting.get(id);
|
|
1222
|
+
const fetching = prompt !== void 0 && this.#sessions.get(id)?.opened != null && linksUploads(prompt);
|
|
1223
|
+
if (before === void 0 && !fetching) {
|
|
1224
|
+
go(prompt);
|
|
1225
|
+
return;
|
|
1226
|
+
}
|
|
1227
|
+
const step = async () => {
|
|
1228
|
+
let fetched = prompt;
|
|
1229
|
+
if (prompt !== void 0 && fetching) {
|
|
1230
|
+
try {
|
|
1231
|
+
fetched = await this.#fetchUploads(id, prompt);
|
|
1232
|
+
} catch (error) {
|
|
1233
|
+
this.#options.log(`${id}: could not fetch the prompt's files: ${messageOf(error)}`);
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
go(fetched);
|
|
1237
|
+
};
|
|
1238
|
+
const next = (before ?? Promise.resolve()).then(step).catch((error) => this.#options.log(`${id}: ${messageOf(error)}`));
|
|
1239
|
+
this.#waiting.set(id, next);
|
|
1240
|
+
void next.then(() => {
|
|
1241
|
+
if (this.#waiting.get(id) === next) this.#waiting.delete(id);
|
|
1242
|
+
});
|
|
1243
|
+
}
|
|
1244
|
+
/** The files a prompt links, fetched for the session's agent; the prompt that links them where they are. */
|
|
1245
|
+
#fetchUploads(id, prompt) {
|
|
1246
|
+
const { hubUrl: hubUrl2, token: token2, home: home2, log: log2 } = this.#options;
|
|
1247
|
+
const initialize = this.#sessions.get(id)?.opened?.initialize;
|
|
1248
|
+
return fetchUploads(prompt, {
|
|
1249
|
+
// The runtime, where the hub is: wss: is https:, ws: http:.
|
|
1250
|
+
origin: new URL(hubUrl2.replace(/^ws/, "http")).origin,
|
|
1251
|
+
token: token2,
|
|
1252
|
+
dir: join4(home2, "uploads", id),
|
|
1253
|
+
images: initialize?.agentCapabilities?.promptCapabilities?.image === true,
|
|
1254
|
+
log: (line) => log2(`${id}: ${line}`)
|
|
1255
|
+
});
|
|
1256
|
+
}
|
|
1078
1257
|
/** The hub's message for the session's agent. */
|
|
1079
1258
|
#relay(id, message) {
|
|
1259
|
+
const prompt = promptIn(message);
|
|
1260
|
+
this.#inOrder(
|
|
1261
|
+
id,
|
|
1262
|
+
prompt,
|
|
1263
|
+
(fetched) => this.#relayNow(
|
|
1264
|
+
id,
|
|
1265
|
+
fetched === prompt || fetched === void 0 ? message : withPrompt(message, fetched)
|
|
1266
|
+
)
|
|
1267
|
+
);
|
|
1268
|
+
}
|
|
1269
|
+
#relayNow(id, message) {
|
|
1080
1270
|
const session = this.#sessions.get(id);
|
|
1081
1271
|
const child = session?.process;
|
|
1082
1272
|
if (!session || !child?.running || session.opened === null) {
|
|
@@ -1098,8 +1288,17 @@ var Gateway = class {
|
|
|
1098
1288
|
/**
|
|
1099
1289
|
* A message for the session's running turn: the agent takes it in, or
|
|
1100
1290
|
* runs it next; one whose agent does not run is answered with an error.
|
|
1291
|
+
* Its files come first, in order with the rest (#inOrder).
|
|
1101
1292
|
*/
|
|
1102
1293
|
#steer(frame) {
|
|
1294
|
+
const prompt = Array.isArray(frame.prompt) ? frame.prompt : void 0;
|
|
1295
|
+
this.#inOrder(
|
|
1296
|
+
frame.session,
|
|
1297
|
+
prompt,
|
|
1298
|
+
(fetched) => this.#steerNow({ ...frame, prompt: fetched ?? frame.prompt })
|
|
1299
|
+
);
|
|
1300
|
+
}
|
|
1301
|
+
#steerNow(frame) {
|
|
1103
1302
|
const session = this.#sessions.get(frame.session);
|
|
1104
1303
|
const child = session?.process;
|
|
1105
1304
|
if (!session || !child?.running || session.opened === null || !session.acpSessionId) {
|
|
@@ -1179,14 +1378,40 @@ var Gateway = class {
|
|
|
1179
1378
|
if (Date.now() - this.#lastTouch >= this.#timing.touchEveryMs) touch();
|
|
1180
1379
|
this.#touching = setInterval(touch, this.#timing.touchEveryMs);
|
|
1181
1380
|
}
|
|
1182
|
-
/**
|
|
1183
|
-
|
|
1381
|
+
/**
|
|
1382
|
+
* The environment of agents and the CLI: the gateway's own, without its
|
|
1383
|
+
* token or the hub's address, with the person as git's author and
|
|
1384
|
+
* committer (#git) unless the gateway's own environment names one.
|
|
1385
|
+
*/
|
|
1386
|
+
async #agentEnv() {
|
|
1184
1387
|
const {
|
|
1185
1388
|
TEABLE_NEXT_GATEWAY_TOKEN: _token,
|
|
1186
1389
|
TEABLE_NEXT_AGENT_URL: _hub,
|
|
1187
1390
|
...env
|
|
1188
1391
|
} = this.#options.env;
|
|
1189
|
-
return env;
|
|
1392
|
+
return { ...await this.#git(), ...env };
|
|
1393
|
+
}
|
|
1394
|
+
/**
|
|
1395
|
+
* The person as git's author and committer, asked of the old platform
|
|
1396
|
+
* once: a sandbox has no git identity of its own, and `teable-next init`
|
|
1397
|
+
* and the agent commit, as the person. A failed ask is logged and asked
|
|
1398
|
+
* again for the next session; until then git is left as it is.
|
|
1399
|
+
*/
|
|
1400
|
+
#git() {
|
|
1401
|
+
this.#gitEnv ??= this.#options.platform.person().then(
|
|
1402
|
+
(person) => person === null ? {} : {
|
|
1403
|
+
GIT_AUTHOR_NAME: person.name,
|
|
1404
|
+
GIT_AUTHOR_EMAIL: person.email,
|
|
1405
|
+
GIT_COMMITTER_NAME: person.name,
|
|
1406
|
+
GIT_COMMITTER_EMAIL: person.email
|
|
1407
|
+
},
|
|
1408
|
+
(error) => {
|
|
1409
|
+
this.#gitEnv = null;
|
|
1410
|
+
this.#options.log(`could not learn who the person is, for git: ${messageOf(error)}`);
|
|
1411
|
+
return {};
|
|
1412
|
+
}
|
|
1413
|
+
);
|
|
1414
|
+
return this.#gitEnv;
|
|
1190
1415
|
}
|
|
1191
1416
|
};
|
|
1192
1417
|
|
|
@@ -1201,6 +1426,12 @@ function expiryOf(value) {
|
|
|
1201
1426
|
}
|
|
1202
1427
|
return void 0;
|
|
1203
1428
|
}
|
|
1429
|
+
function personOf(user) {
|
|
1430
|
+
const { name, email } = user ?? {};
|
|
1431
|
+
if (typeof email !== "string" || !email.includes("@")) return null;
|
|
1432
|
+
const named = typeof name === "string" ? name.trim() : "";
|
|
1433
|
+
return { name: named === "" ? email.slice(0, email.indexOf("@")) : named, email };
|
|
1434
|
+
}
|
|
1204
1435
|
var Platform = class {
|
|
1205
1436
|
#endpoint;
|
|
1206
1437
|
#token;
|
|
@@ -1223,9 +1454,16 @@ var Platform = class {
|
|
|
1223
1454
|
async touch() {
|
|
1224
1455
|
await this.#post("/api/teable-next/agent/sandbox/touch", void 0);
|
|
1225
1456
|
}
|
|
1226
|
-
|
|
1457
|
+
/** Who the token's person is, as git names them; null when Teable does not say. */
|
|
1458
|
+
async person() {
|
|
1459
|
+
return personOf(await this.#request("GET", "/api/auth/user", void 0));
|
|
1460
|
+
}
|
|
1461
|
+
#post(path, body) {
|
|
1462
|
+
return this.#request("POST", path, body);
|
|
1463
|
+
}
|
|
1464
|
+
async #request(method, path, body) {
|
|
1227
1465
|
const response = await fetch(`${this.#endpoint}${path}`, {
|
|
1228
|
-
method
|
|
1466
|
+
method,
|
|
1229
1467
|
headers: {
|
|
1230
1468
|
Authorization: `Bearer ${this.#token}`,
|
|
1231
1469
|
...body !== void 0 && { "Content-Type": "application/json" }
|
|
@@ -1294,7 +1532,7 @@ var gateway = new Gateway({
|
|
|
1294
1532
|
token,
|
|
1295
1533
|
platform: new Platform(required("TEABLE_ENDPOINT"), required("TEABLE_TOKEN")),
|
|
1296
1534
|
home,
|
|
1297
|
-
stateDir:
|
|
1535
|
+
stateDir: join5(home, ".teable-agent-gateway"),
|
|
1298
1536
|
env: process.env,
|
|
1299
1537
|
version,
|
|
1300
1538
|
log,
|
package/dist/gateway.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ export interface GatewayOptions {
|
|
|
23
23
|
hubUrl: string;
|
|
24
24
|
/** TEABLE_NEXT_GATEWAY_TOKEN. */
|
|
25
25
|
token: string;
|
|
26
|
-
platform: Pick<Platform, "modelKey" | "touch">;
|
|
26
|
+
platform: Pick<Platform, "modelKey" | "touch" | "person">;
|
|
27
27
|
/** The person's home: apps are checked out in `apps/<appId>` under it. */
|
|
28
28
|
home: string;
|
|
29
29
|
/** Where the gateway keeps its files: agents' state, model tokens. */
|
package/dist/platform.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The old platform's
|
|
2
|
+
* The old platform's routes the gateway calls (ADR 0006 §5), with the
|
|
3
3
|
* person's own temporary token (TEABLE_TOKEN):
|
|
4
4
|
*
|
|
5
|
+
* - `GET /api/auth/user`: who the person is, so that git names them as the
|
|
6
|
+
* author of what their agent commits;
|
|
5
7
|
* - `POST /api/teable-next/agent/model-key` with `{ baseId }`: an AI proxy
|
|
6
8
|
* token for this person and that project (the session's app), valid for
|
|
7
9
|
* an hour, whose credits go to the project's space; 403 without
|
|
@@ -26,10 +28,24 @@ export interface ModelKey {
|
|
|
26
28
|
* of seconds, if it is too small to be milliseconds) since the epoch.
|
|
27
29
|
*/
|
|
28
30
|
export declare function expiryOf(value: unknown): number | undefined;
|
|
31
|
+
/** The person, as git names an author and a committer. */
|
|
32
|
+
interface Person {
|
|
33
|
+
name: string;
|
|
34
|
+
email: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The person `GET /api/auth/user` answers, as git names them; null when the
|
|
38
|
+
* answer has no email (a token without the email scope; the sandbox's own
|
|
39
|
+
* token has it). A person without a name goes by their email's local part.
|
|
40
|
+
*/
|
|
41
|
+
export declare function personOf(user: unknown): Person | null;
|
|
29
42
|
export declare class Platform {
|
|
30
43
|
#private;
|
|
31
44
|
constructor(endpoint: string, token: string);
|
|
32
45
|
/** A model token for the project `baseId` (an app's id). */
|
|
33
46
|
modelKey(baseId: string): Promise<ModelKey>;
|
|
34
47
|
touch(): Promise<void>;
|
|
48
|
+
/** Who the token's person is, as git names them; null when Teable does not say. */
|
|
49
|
+
person(): Promise<Person | null>;
|
|
35
50
|
}
|
|
51
|
+
export {};
|
package/dist/protocol.d.ts
CHANGED
|
@@ -101,6 +101,39 @@ export declare const TAB_CLOSE: {
|
|
|
101
101
|
};
|
|
102
102
|
/** Session ids, made by the hub: `ses` and 16 letters or digits. */
|
|
103
103
|
export declare const SESSION_ID: RegExp;
|
|
104
|
+
/**
|
|
105
|
+
* Files a person attaches to a message for their agent: a screenshot, a CSV
|
|
106
|
+
* to import, a PDF spec. The panel uploads each with
|
|
107
|
+
* `POST ${APPS_PATH}/:appId${AGENT_UPLOADS}` and the identity cookie, as the
|
|
108
|
+
* tab's socket goes: the bytes as the body, their type as Content-Type,
|
|
109
|
+
* the name URI-encoded in FILE_NAME_HEADER (both from @teable/app/protocol).
|
|
110
|
+
* The runtime keeps the file for the person, apart from every app's files,
|
|
111
|
+
* and answers an Upload; the prompt is the message's text, then a
|
|
112
|
+
* `resource_link` with each Upload's fields. Its `uri` reads the file back
|
|
113
|
+
* (UPLOAD_PATH) for the person who sent it, with their identity, and for
|
|
114
|
+
* their gateway, with its token: the gateway fetches each file into the
|
|
115
|
+
* sandbox before the agent reads the prompt. Nobody else may read it.
|
|
116
|
+
*/
|
|
117
|
+
export declare const AGENT_UPLOADS = "/agent/uploads";
|
|
118
|
+
/** At most this many bytes a file (20 MiB), and this many files a message. */
|
|
119
|
+
export declare const UPLOAD_LIMITS: {
|
|
120
|
+
readonly bytes: 20971520;
|
|
121
|
+
readonly files: 10;
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* The path of an uploaded file, on any of the runtime's hosts:
|
|
125
|
+
* `${APPS_PATH}/:appId${AGENT_UPLOADS}/<uuid>/<name>`, the name URI-encoded.
|
|
126
|
+
*/
|
|
127
|
+
export declare const UPLOAD_PATH: RegExp;
|
|
128
|
+
/** What the runtime answers an upload with: what the prompt's `resource_link` says of the file. */
|
|
129
|
+
export interface Upload {
|
|
130
|
+
/** Where the file is read back. */
|
|
131
|
+
uri: string;
|
|
132
|
+
name: string;
|
|
133
|
+
mimeType: string;
|
|
134
|
+
/** In bytes. */
|
|
135
|
+
size: number;
|
|
136
|
+
}
|
|
104
137
|
/** A JSON-RPC request id. */
|
|
105
138
|
export type JsonRpcId = string | number;
|
|
106
139
|
export interface AcpRequest {
|
|
@@ -573,7 +606,9 @@ export interface TabCommand {
|
|
|
573
606
|
/**
|
|
574
607
|
* What a command does. Starting a session, prompting and answering what the
|
|
575
608
|
* agent asks need a creator or owner of the app's space, as publishing does;
|
|
576
|
-
* any member may connect, read their own sessions and cancel.
|
|
609
|
+
* any member may connect, read their own sessions and cancel. A message
|
|
610
|
+
* (`prompt`) links at most UPLOAD_LIMITS.files files, each a `resource_link`
|
|
611
|
+
* with its `uri` and `name`.
|
|
577
612
|
*/
|
|
578
613
|
export type TabOp =
|
|
579
614
|
/** Start a session about the tab's app, with `prompt` as its first message, if given. */
|
package/dist/protocol.js
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
ACP,
|
|
3
3
|
AGENT_NAMES,
|
|
4
4
|
AGENT_PROTOCOL,
|
|
5
|
+
AGENT_UPLOADS,
|
|
5
6
|
ASK,
|
|
6
7
|
GATEWAY_CLOSE,
|
|
7
8
|
GATEWAY_PATH,
|
|
@@ -13,15 +14,18 @@ import {
|
|
|
13
14
|
SESSION_ID,
|
|
14
15
|
TAB_CLOSE,
|
|
15
16
|
TASK_OUTPUT,
|
|
17
|
+
UPLOAD_LIMITS,
|
|
18
|
+
UPLOAD_PATH,
|
|
16
19
|
isAcpNotification,
|
|
17
20
|
isAcpRequest,
|
|
18
21
|
isAcpResponse,
|
|
19
22
|
shrink
|
|
20
|
-
} from "./chunks/
|
|
23
|
+
} from "./chunks/EPBUNSHS.js";
|
|
21
24
|
export {
|
|
22
25
|
ACP,
|
|
23
26
|
AGENT_NAMES,
|
|
24
27
|
AGENT_PROTOCOL,
|
|
28
|
+
AGENT_UPLOADS,
|
|
25
29
|
ASK,
|
|
26
30
|
GATEWAY_CLOSE,
|
|
27
31
|
GATEWAY_PATH,
|
|
@@ -33,6 +37,8 @@ export {
|
|
|
33
37
|
SESSION_ID,
|
|
34
38
|
TAB_CLOSE,
|
|
35
39
|
TASK_OUTPUT,
|
|
40
|
+
UPLOAD_LIMITS,
|
|
41
|
+
UPLOAD_PATH,
|
|
36
42
|
isAcpNotification,
|
|
37
43
|
isAcpRequest,
|
|
38
44
|
isAcpResponse,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type AcpContentBlock } from "./protocol.js";
|
|
2
|
+
/** Whether a prompt links files the person uploaded. */
|
|
3
|
+
export declare function linksUploads(prompt: readonly AcpContentBlock[]): boolean;
|
|
4
|
+
interface Fetching {
|
|
5
|
+
/** The runtime's origin, the hub's: the only one the token goes to. */
|
|
6
|
+
origin: string;
|
|
7
|
+
/** The gateway token. */
|
|
8
|
+
token: string;
|
|
9
|
+
/** Where the session's files go: `~/uploads/<session>`. */
|
|
10
|
+
dir: string;
|
|
11
|
+
/** Whether the agent takes images in prompts (its `promptCapabilities.image`). */
|
|
12
|
+
images: boolean;
|
|
13
|
+
log(line: string): void;
|
|
14
|
+
timeoutMs?: number;
|
|
15
|
+
}
|
|
16
|
+
/** The prompt as the agent reads it: every file it links on the runtime, in the sandbox. */
|
|
17
|
+
export declare function fetchUploads(prompt: AcpContentBlock[], fetching: Fetching): Promise<AcpContentBlock[]>;
|
|
18
|
+
export {};
|
package/package.json
CHANGED