@segmentstream/agent 0.37.0 → 0.37.1

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,7 +4,7 @@ import { createRequire as __eveCreateRequire } from "node:module";
4
4
  const __filename = __eveFileURLToPath(import.meta.url);
5
5
  __eveDirname(__filename);
6
6
  __eveCreateRequire(import.meta.url);
7
- var version = "0.37.0";
7
+ var version = "0.37.1";
8
8
  function agentBuild(environment = process.env) {
9
9
  return {
10
10
  agent: version,
@@ -65,7 +65,9 @@ rejection or perform actions.
65
65
  question, use the answer and continue the pending task without asking for permission again.
66
66
  - Ask only when information is missing or ambiguous. In Telegram, use
67
67
  `segmentstream__questions_ask` with the question and pending task so the session keeps its intent,
68
- member and chat/topic. An answer to a pending question is never chatter and always gets a reply.
68
+ member and chat/topic. In Slack and WhatsApp, ask the question in your reply instead; the member's
69
+ answer continues the conversation. An answer to a pending question is never chatter and always
70
+ gets a reply.
69
71
  - New messages and uploads may arrive together or while you are working. Complete or explicitly
70
72
  report the status of every request and every supplied file. A later message adds work unless
71
73
  the member explicitly cancels or replaces the earlier task. If work is interrupted or blocked,
@@ -4,4 +4,4 @@ import { createRequire as __eveCreateRequire } from "node:module";
4
4
  const __filename = __eveFileURLToPath(import.meta.url);
5
5
  __eveDirname(__filename);
6
6
  __eveCreateRequire(import.meta.url);
7
- export { t as agentBuild } from "../../_chunks/build-metadata-CnjDv9p8.mjs";
7
+ export { t as agentBuild } from "../../_chunks/build-metadata-C0Kz75P3.mjs";
@@ -14,6 +14,7 @@ import { type RuntimeToken } from "../runtime-token.js";
14
14
  * which checks them with the app's signing secret and relays them here signed as Slack signs, with
15
15
  * the project's `SEGMENTSTREAM_AGENT_WEBHOOK_SECRET`. Every Web API call and file download goes
16
16
  * through the connection proxy, which adds the bot token: no Slack credential enters this process.
17
+ * Only a download Slack redirects to another host is fetched there directly, without a credential.
17
18
  *
18
19
  * eve does the rest: mentions and direct messages start a turn in a thread, the thread's later
19
20
  * replies continue it, and approvals and questions are buttons. Only paired members are answered;
@@ -82,8 +83,15 @@ export declare function relayedRequestVerifier(secret: () => string | undefined,
82
83
  /**
83
84
  * eve's Slack transport carried by the connection proxy: a Web API method becomes a `web` call
84
85
  * and a file download a `files` call. The placeholder bot token never leaves this process.
86
+ *
87
+ * Redirects are followed as `fetch` follows them, which the proxy does not do for this connection:
88
+ * Slack answers a file's `url_private` with a redirect. A location under Slack's API or files is
89
+ * requested through the proxy again, which adds the bot token as before; any other HTTPS location
90
+ * is fetched directly with no credential, as `fetch` drops the authorization on a redirect to
91
+ * another origin. Every hop, wherever it answered from, is checked the same way and counts toward
92
+ * the same limit. A 303 continues with a GET and no body.
85
93
  */
86
- export declare function proxiedSlackFetch(proxy: () => ConnectionProxy): typeof fetch;
94
+ export declare function proxiedSlackFetch(proxy: () => ConnectionProxy, directFetch?: typeof fetch): typeof fetch;
87
95
  /** Builds the channel. Everything it reads from the mount is read per request. */
88
96
  export declare function segmentstreamSlack(options: SlackChannelOptions): SlackChannel;
89
97
  /**
@@ -112,35 +112,77 @@ function relayedRequestVerifier(secret, now = () => Date.now() / 1e3) {
112
112
  return true;
113
113
  };
114
114
  }
115
- function proxiedSlackFetch(proxy) {
115
+ const MAXIMUM_SLACK_REDIRECTS = 5;
116
+ const REDIRECT_STATUSES = new Set([
117
+ 301,
118
+ 302,
119
+ 303,
120
+ 307,
121
+ 308
122
+ ]);
123
+ const NULL_BODY_STATUSES = new Set([
124
+ 101,
125
+ 204,
126
+ 205,
127
+ 304
128
+ ]);
129
+ function proxiedSlackFetch(proxy, directFetch = fetch) {
116
130
  return async (input, init) => {
117
131
  const request = new Request(input, init);
118
- const url = new URL(request.url);
119
132
  const headers = [...request.headers].filter(([name]) => name.toLowerCase() !== "authorization").map(([name, value]) => [name, value]);
120
- const body = new Uint8Array(await request.arrayBuffer());
121
- const method = request.method.toUpperCase();
122
- let reply;
123
- if (url.href.startsWith(SLACK_API)) reply = await proxy().request({
124
- api: "web",
125
- method,
126
- path: `/${url.href.slice(22)}`,
127
- headers,
128
- body
129
- }, request.signal);
130
- else if (url.href.startsWith(SLACK_FILES)) reply = await proxy().download({
131
- api: "files",
132
- method,
133
- path: `/${url.href.slice(24)}`,
134
- headers,
135
- body
136
- }, MAXIMUM_DOWNLOAD_BYTES, request.signal);
137
- else throw new SlackProxyRouteError("The Slack channel reaches only Slack's API and files.");
138
- return new Response(reply.body.byteLength === 0 ? null : Buffer.from(reply.body), {
139
- status: reply.status,
140
- headers: reply.headers.map(([name, value]) => [name, value])
141
- });
133
+ let url = new URL(request.url);
134
+ let method = request.method.toUpperCase();
135
+ let body = new Uint8Array(await request.arrayBuffer());
136
+ for (let redirects = 0;; redirects += 1) {
137
+ const slack = url.href.startsWith(SLACK_API) || url.href.startsWith(SLACK_FILES);
138
+ let response;
139
+ if (slack) {
140
+ const reply = url.href.startsWith(SLACK_API) ? await proxy().request({
141
+ api: "web",
142
+ method,
143
+ path: `/${url.href.slice(22)}`,
144
+ headers,
145
+ body
146
+ }, request.signal) : await proxy().download({
147
+ api: "files",
148
+ method,
149
+ path: `/${url.href.slice(24)}`,
150
+ headers,
151
+ body
152
+ }, MAXIMUM_DOWNLOAD_BYTES, request.signal);
153
+ response = new Response(reply.body.byteLength === 0 || NULL_BODY_STATUSES.has(reply.status) ? null : Buffer.from(reply.body), {
154
+ status: reply.status,
155
+ headers: reply.headers.map(([name, value]) => [name, value])
156
+ });
157
+ } else if (redirects === 0) throw new SlackProxyRouteError("The Slack channel reaches only Slack's API and files.");
158
+ else if (method !== "GET" && method !== "HEAD") throw new SlackProxyRouteError("Slack redirected a call with a body to another host.");
159
+ else response = await directFetch(url, {
160
+ method,
161
+ redirect: "manual",
162
+ signal: request.signal
163
+ });
164
+ const location = response.headers.get("location");
165
+ if (!REDIRECT_STATUSES.has(response.status) || location === null || redirects === MAXIMUM_SLACK_REDIRECTS) return response;
166
+ const next = followable(location, url);
167
+ if (next === void 0) throw new SlackProxyRouteError("Slack redirected the call to an address it cannot follow.");
168
+ await response.body?.cancel();
169
+ if (response.status === 303) {
170
+ method = "GET";
171
+ body = new Uint8Array();
172
+ }
173
+ url = next;
174
+ }
142
175
  };
143
176
  }
177
+ function followable(location, base) {
178
+ let next;
179
+ try {
180
+ next = new URL(location, base);
181
+ } catch {
182
+ return;
183
+ }
184
+ return next.protocol === "https:" && next.username === "" && next.password === "" ? next : void 0;
185
+ }
144
186
  function segmentstreamSlack(options) {
145
187
  const environment = options.environment ?? process.env;
146
188
  const token = options.token ?? runtimeToken;
@@ -10,7 +10,7 @@ import { groupRoutingHealth } from "../group-routing/health.mjs";
10
10
  import { dispatchStructuredMessage, structuredMessage } from "./structured.mjs";
11
11
  import { handleWorkspaceCallback, isWorkspaceCallback } from "./callbacks.mjs";
12
12
  import { telegramText, withTelegramTexts } from "./texts.mjs";
13
- import { t as agentBuild } from "../../../_chunks/build-metadata-CnjDv9p8.mjs";
13
+ import { t as agentBuild } from "../../../_chunks/build-metadata-C0Kz75P3.mjs";
14
14
  import { attachmentLifecycle } from "./lifecycle.mjs";
15
15
  import { manifestOf } from "../../config.mjs";
16
16
  import { gatewayAttachmentDescriber } from "./attachment-description.mjs";
@@ -4,7 +4,7 @@ import { createRequire as __eveCreateRequire } from "node:module";
4
4
  const __filename = __eveFileURLToPath(import.meta.url);
5
5
  __eveDirname(__filename);
6
6
  __eveCreateRequire(import.meta.url);
7
- import { t as agentBuild } from "../../../_chunks/build-metadata-CnjDv9p8.mjs";
7
+ import { t as agentBuild } from "../../../_chunks/build-metadata-C0Kz75P3.mjs";
8
8
  import { runtimeRequestFailure } from "./runtime-request.mjs";
9
9
  import { GET } from "eve/channels";
10
10
  const HEALTH_ROUTE = "/eve/v1/telegram/health";
@@ -5,7 +5,7 @@ const __filename = __eveFileURLToPath(import.meta.url);
5
5
  __eveDirname(__filename);
6
6
  __eveCreateRequire(import.meta.url);
7
7
  import { d as telegramRuntimeSource, l as principalRuntimeSource, u as runtimeEnvelope } from "../../../_chunks/principal-BIX7T6Hh.mjs";
8
- import { t as agentBuild } from "../../../_chunks/build-metadata-CnjDv9p8.mjs";
8
+ import { t as agentBuild } from "../../../_chunks/build-metadata-C0Kz75P3.mjs";
9
9
  import { n as currentInboundRequest } from "../../../_chunks/inbound-request-DgQB4g1J.mjs";
10
10
  import { object } from "../confirmation-description.mjs";
11
11
  import { needsConfirmation } from "../confirmations.mjs";
@@ -7,6 +7,7 @@ __eveCreateRequire(import.meta.url);
7
7
  import { n as workflowToolResult } from "../../_chunks/workflow-serializable-DU7WeCIW.mjs";
8
8
  import { agentTaskIdOf, agentTaskSessions } from "../lib/agent-task.mjs";
9
9
  import { stringArgument } from "../lib/attachment-tools.mjs";
10
+ import { conversationOf } from "../lib/conversation.mjs";
10
11
  import { pendingQuestions, questionKey } from "../lib/questions.mjs";
11
12
  import { i as memberIdOf } from "../../_chunks/principal-BIX7T6Hh.mjs";
12
13
  import { defineTool } from "eve/tools";
@@ -42,6 +43,8 @@ var questions_ask_default = defineTool({
42
43
  } : task);
43
44
  return workflowToolResult({ question: text }, ctx);
44
45
  }
46
+ const conversation = conversationOf(ctx.session.auth.current);
47
+ if (conversation !== void 0 && conversation.provider !== "telegram") throw new Error(`questions_ask is unavailable in ${conversation.provider}: ask the question in your reply; the member's answer continues this conversation.`);
45
48
  const memberId = memberIdOf(ctx.session.auth.current);
46
49
  const chatId = ctx.session.auth.current?.attributes.chatId;
47
50
  if (memberId === void 0 || typeof chatId !== "string") throw new Error("A member in a chat is required.");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/agent",
3
- "version": "0.37.0",
3
+ "version": "0.37.1",
4
4
  "description": "Mount a SegmentStream workspace's agent on eve: the ontology as tools, acting as the member who writes, and Telegram, WhatsApp and Slack through the workspace's connections.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "SegmentStream",