@teable/agent-gateway 1.0.4 → 1.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.
@@ -4,6 +4,11 @@ interface Checkout {
4
4
  /** Prepended to the session's first prompt; null when the checkout speaks for itself. */
5
5
  rules: string | null;
6
6
  }
7
+ /**
8
+ * How every text the gateway adds to a prompt starts, so that what the agent
9
+ * makes of it (a session named after it) can be told from the person's words.
10
+ */
11
+ export declare const RULES_MARK = "[Teable]";
7
12
  /**
8
13
  * The app's checkout under `home`, cloned first if it is missing. The CLI
9
14
  * gets `env`: the person's token and where Teable is (TEABLE_TOKEN,
package/dist/cli.js CHANGED
@@ -571,9 +571,18 @@ import { existsSync, mkdirSync as mkdirSync2 } from "node:fs";
571
571
  import { join as join2 } from "node:path";
572
572
  var APP_DIRECTORY = /^[A-Za-z0-9_-]+$/;
573
573
  var CLONE_TIMEOUT_MS = 5 * 6e4;
574
+ var RULES_MARK = "[Teable]";
575
+ var where = (appId) => `${RULES_MARK} You work in a person's Teable sandbox, on the Teable app ${appId}, in this directory.`;
576
+ function newProject(appId) {
577
+ return [
578
+ where(appId),
579
+ "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}\`.`
581
+ ].join(" ");
582
+ }
574
583
  function cliMissing(appId) {
575
584
  return [
576
- `[Teable] You work in a person's Teable sandbox, on the Teable app ${appId}, in this directory.`,
585
+ where(appId),
577
586
  "The teable-next CLI is not installed here, so the directory was created empty instead of",
578
587
  "checked out. Run the CLI with `npx -y @teable/next-cli` (or install it with",
579
588
  `\`npm install -g @teable/next-cli\`): \`clone ${appId} .\` checks the app out here, and`,
@@ -582,12 +591,20 @@ function cliMissing(appId) {
582
591
  }
583
592
  function cloneFailed(appId, output) {
584
593
  return [
585
- `[Teable] You work in a person's Teable sandbox, on the Teable app ${appId}, in this directory.`,
594
+ where(appId),
586
595
  `Checking it out with \`teable-next clone ${appId}\` failed, so the directory may be empty.`,
587
596
  `Try \`teable-next clone ${appId} .\` again, or tell the person what went wrong. It said:`,
588
597
  output.trim().slice(-2e3) || "(nothing)"
589
598
  ].join(" ");
590
599
  }
600
+ function codeOf(stdout) {
601
+ try {
602
+ const result = JSON.parse(stdout);
603
+ return typeof result === "object" && result !== null && "code" in result ? result.code : void 0;
604
+ } catch {
605
+ return void 0;
606
+ }
607
+ }
591
608
  function clone(appId, dir, env) {
592
609
  return new Promise((resolve) => {
593
610
  execFile(
@@ -595,9 +612,10 @@ function clone(appId, dir, env) {
595
612
  ["clone", appId, dir],
596
613
  { env, timeout: CLONE_TIMEOUT_MS, maxBuffer: 16 << 20 },
597
614
  (error, stdout, stderr) => {
598
- if (error === null) resolve(null);
599
- else if (error.code === "ENOENT") resolve("missing");
600
- else resolve(`${stdout}${stderr}`.trim() || error.message);
615
+ if (error === null) resolve({ kind: "cloned" });
616
+ else if (error.code === "ENOENT") resolve({ kind: "missing" });
617
+ else if (codeOf(stdout) === "NO_CODE") resolve({ kind: "empty" });
618
+ else resolve({ kind: "failed", output: `${stdout}${stderr}`.trim() || error.message });
601
619
  }
602
620
  );
603
621
  });
@@ -614,15 +632,22 @@ function checkout(home2, appId, env, log2) {
614
632
  const made = (async () => {
615
633
  mkdirSync2(join2(home2, "apps"), { recursive: true });
616
634
  log2(`cloning ${appId} into ${cwd}`);
617
- const failed = await clone(appId, cwd, env);
618
- if (failed === null && existsSync(cwd)) return { cwd, rules: null };
635
+ const cloned = await clone(appId, cwd, env);
636
+ if (cloned.kind === "cloned" && existsSync(cwd)) return { cwd, rules: null };
619
637
  mkdirSync2(cwd, { recursive: true });
620
- if (failed === "missing") {
621
- log2(`teable-next is not installed: ${cwd} starts empty`);
622
- return { cwd, rules: cliMissing(appId) };
638
+ switch (cloned.kind) {
639
+ case "empty":
640
+ log2(`${appId} has no code yet: ${cwd} starts empty`);
641
+ return { cwd, rules: newProject(appId) };
642
+ case "missing":
643
+ log2(`teable-next is not installed: ${cwd} starts empty`);
644
+ return { cwd, rules: cliMissing(appId) };
645
+ case "failed":
646
+ log2(`teable-next clone ${appId} failed: ${cloned.output}`);
647
+ return { cwd, rules: cloneFailed(appId, cloned.output) };
648
+ default:
649
+ return { cwd, rules: cloneFailed(appId, "it made no directory") };
623
650
  }
624
- log2(`teable-next clone ${appId} failed: ${failed}`);
625
- return { cwd, rules: cloneFailed(appId, failed ?? "it made no directory") };
626
651
  })();
627
652
  cloning.set(cwd, made);
628
653
  return made.finally(() => cloning.delete(cwd));
@@ -645,6 +670,12 @@ var UNACKED_LIMIT = 1e4;
645
670
  function messageOf(error) {
646
671
  return error instanceof Error ? error.message : String(error);
647
672
  }
673
+ function namedAfterRules(message) {
674
+ if (!isAcpNotification(message) || message.method !== ACP.update) return false;
675
+ const { params } = message;
676
+ const update = typeof params === "object" && params !== null && "update" in params ? params.update : void 0;
677
+ 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
+ }
648
679
  function withRules(message, rules) {
649
680
  if (!isAcpRequest(message)) return message;
650
681
  const params = message.params;
@@ -1082,6 +1113,7 @@ var Gateway = class {
1082
1113
  }
1083
1114
  /** The agent's message for the hub. */
1084
1115
  #fromAgent(session, message) {
1116
+ if (namedAfterRules(message)) return;
1085
1117
  this.#send({ session: session.session, acp: message });
1086
1118
  if (isAcpResponse(message) || "method" in message && message.method === NOTICE.task) {
1087
1119
  this.#idle(session);
@@ -1,5 +1,5 @@
1
1
  /**
2
- * The old platform's two routes the gateway calls (ADR 0006 §6), with the
2
+ * The old platform's two routes the gateway calls (ADR 0006 §5), with the
3
3
  * person's own temporary token (TEABLE_TOKEN):
4
4
  *
5
5
  * - `POST /api/teable-next/agent/model-key` with `{ baseId }`: an AI proxy
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teable/agent-gateway",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "The agent gateway in a person's Teable sandbox: starts an ACP agent for each session and relays it to the person's hub",
5
5
  "license": "MIT",
6
6
  "repository": {