@assembly-js/app-foundation 0.0.20 → 0.0.21
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/CLAUDE.md +2 -0
- package/dist/mint-bearer.d.ts +1 -0
- package/dist/mint-bearer.js +34 -0
- package/dist/nango.d.ts +51 -6
- package/dist/nango.js +116 -49
- package/dist/session-exchange.d.ts +12 -0
- package/dist/session-exchange.js +5 -0
- package/package.json +1 -1
- package/skills/assembly-integrations/SKILL.md +1 -1
- package/skills/assembly-integrations/providers/slack.md +24 -8
- package/skills/assembly-session-token/SKILL.md +2 -0
package/CLAUDE.md
CHANGED
|
@@ -644,6 +644,8 @@ import { loadItems } from '@/app/actions/items';
|
|
|
644
644
|
export default function MyWidget() {
|
|
645
645
|
const { data: items, isLoading, error, reload } = useServerAction(() => loadItems(), []);
|
|
646
646
|
// `deps` ([]) works like useEffect deps — list anything the action closes over.
|
|
647
|
+
// Deps must be primitives (an id, a cursor, `items.length`). A derived array or
|
|
648
|
+
// object is a new identity every render and spins the page in a fetch loop.
|
|
647
649
|
}
|
|
648
650
|
```
|
|
649
651
|
|
package/dist/mint-bearer.d.ts
CHANGED
|
@@ -21,6 +21,7 @@ export interface MintSessionOptions {
|
|
|
21
21
|
cookieSessionId?: string;
|
|
22
22
|
}
|
|
23
23
|
export declare function mintBearerFromSessionToken(token: string, options?: MintSessionOptions): Promise<MintOutcome>;
|
|
24
|
+
export declare function fetchSessionFlags(bearer: string): Promise<string[] | null>;
|
|
24
25
|
export interface MintedAppBearer {
|
|
25
26
|
bearer: string;
|
|
26
27
|
bearerExpiresAt: number;
|
package/dist/mint-bearer.js
CHANGED
|
@@ -90,6 +90,7 @@ export async function mintBearerFromSessionToken(token, options) {
|
|
|
90
90
|
notificationId: json.session?.notificationId || undefined,
|
|
91
91
|
instanceId: json.session?.instanceId || undefined,
|
|
92
92
|
sessionId: payload.tokenId,
|
|
93
|
+
flags: json.flags?.length ? json.flags : undefined,
|
|
93
94
|
},
|
|
94
95
|
};
|
|
95
96
|
}
|
|
@@ -102,6 +103,39 @@ export async function mintBearerFromSessionToken(token, options) {
|
|
|
102
103
|
clearTimeout(timeoutId);
|
|
103
104
|
}
|
|
104
105
|
}
|
|
106
|
+
// Non-destructive read; a hung call only delays one cookie refresh, so give it
|
|
107
|
+
// far less rope than the exchange.
|
|
108
|
+
const FLAGS_TIMEOUT_MS = 5_000;
|
|
109
|
+
// Re-resolves the app-foundation-visible flags for the workspace behind an
|
|
110
|
+
// already-minted bearer (GET /v1/session/flags). The freshness half of the
|
|
111
|
+
// flag channel: the exchange response seeds the proxy's flags cookie at mint
|
|
112
|
+
// time, and this refreshes it on the cookie's own cadence — no re-mint, no
|
|
113
|
+
// auth churn. Returns null when the value could not be determined (network
|
|
114
|
+
// failure, expired bearer, malformed body) so the caller can keep whatever it
|
|
115
|
+
// has rather than treating the miss as "all flags off".
|
|
116
|
+
export async function fetchSessionFlags(bearer) {
|
|
117
|
+
const base = (mintBaseUrl ?? OpenAPI.BASE).replace(/\/$/, '');
|
|
118
|
+
const controller = new AbortController();
|
|
119
|
+
const timeoutId = setTimeout(() => controller.abort(), FLAGS_TIMEOUT_MS);
|
|
120
|
+
try {
|
|
121
|
+
const resp = await fetch(`${base}/v1/session/flags`, {
|
|
122
|
+
headers: { Authorization: `Bearer ${bearer}` },
|
|
123
|
+
signal: controller.signal,
|
|
124
|
+
});
|
|
125
|
+
if (!resp.ok)
|
|
126
|
+
return null;
|
|
127
|
+
const json = (await resp.json());
|
|
128
|
+
if (!Array.isArray(json.flags))
|
|
129
|
+
return null;
|
|
130
|
+
return json.flags.filter((f) => typeof f === 'string');
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
clearTimeout(timeoutId);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
105
139
|
// Mints a short-lived, APP-scoped bearer from the app API key ALONE by calling
|
|
106
140
|
// platform-api POST /v1/token (client-credentials). There is NO session token
|
|
107
141
|
// and NO user in the loop: this is the OFFLINE/background path (cron, webhooks)
|
package/dist/nango.d.ts
CHANGED
|
@@ -12,8 +12,9 @@ export declare class ConnectorError extends Error {
|
|
|
12
12
|
readonly status: number;
|
|
13
13
|
/**
|
|
14
14
|
* For `action_failed`: what the action script threw — usually the
|
|
15
|
-
* provider-level failure
|
|
16
|
-
*
|
|
15
|
+
* provider-level failure. Scripts disagree on where the provider's error
|
|
16
|
+
* code sits inside it (`response.error`, `error`, or `message`), so don't
|
|
17
|
+
* read it by hand: for Slack use `slackErrorCode(err)`.
|
|
17
18
|
*/
|
|
18
19
|
readonly detail?: unknown;
|
|
19
20
|
constructor(args: {
|
|
@@ -120,6 +121,49 @@ export interface ResolveResourceOpts {
|
|
|
120
121
|
* }
|
|
121
122
|
*/
|
|
122
123
|
export declare function resolveResource(provider: Provider, resolverAction: string, opts: ResolveResourceOpts): Promise<ResourcePage>;
|
|
124
|
+
/**
|
|
125
|
+
* Slack's error code (e.g. 'not_in_channel', 'channel_not_found',
|
|
126
|
+
* 'is_archived') from a failed Slack action, or undefined when `err` is not an
|
|
127
|
+
* action failure. Branch on this instead of reading `err.detail` yourself: the
|
|
128
|
+
* vendored Slack scripts put the code in different places (`response.error`,
|
|
129
|
+
* `error`, or `message`), and this reads them in that order.
|
|
130
|
+
*
|
|
131
|
+
* ```typescript
|
|
132
|
+
* try {
|
|
133
|
+
* await slack.action('get-conversation-history', { channel_id, limit: 10 });
|
|
134
|
+
* } catch (err) {
|
|
135
|
+
* if (slackErrorCode(err) === 'not_in_channel') {
|
|
136
|
+
* // join with the channel ID and retry once; private channels can't be
|
|
137
|
+
* // self-joined — tell the user to invite the bot instead
|
|
138
|
+
* }
|
|
139
|
+
* throw err;
|
|
140
|
+
* }
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
export declare function slackErrorCode(err: unknown): string | undefined;
|
|
144
|
+
export interface SlackChannel {
|
|
145
|
+
/** Channel ID (`C…`/`G…`) — what every other Slack call takes. */
|
|
146
|
+
id: string;
|
|
147
|
+
/** Channel name without the leading '#'. Slack keeps these lowercase. */
|
|
148
|
+
name: string;
|
|
149
|
+
/** True for private channels (listed only when the bot is a member and the connection has `groups:read`). */
|
|
150
|
+
isPrivate: boolean;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Every channel the bot can see, sorted by name. Use this to populate a
|
|
154
|
+
* channel dropdown instead of paging the `list-channels` action yourself: that
|
|
155
|
+
* action tops out at 200 per page and a large workspace needs dozens of
|
|
156
|
+
* sequential calls, which is a 20–30 second dropdown. This walks
|
|
157
|
+
* conversations.list at Slack's maximum page size and handles rate limits and
|
|
158
|
+
* the private-channel scope fallback. Not cached: the client carries no
|
|
159
|
+
* workspace identity to key on, and an app transfer briefly serves two
|
|
160
|
+
* workspaces from one deployment.
|
|
161
|
+
*
|
|
162
|
+
* Public channels are listed whether or not the bot has joined; private
|
|
163
|
+
* channels only when it is already a member. Listing a channel does not make
|
|
164
|
+
* it readable — see slackFindChannelId for the membership rules.
|
|
165
|
+
*/
|
|
166
|
+
export declare function slackListChannels(slack: IntegrationClient): Promise<SlackChannel[]>;
|
|
123
167
|
/**
|
|
124
168
|
* Resolve a Slack channel name to its ID, walking the cursor-paginated
|
|
125
169
|
* conversations.list until found (Slack treats `limit` as a per-page cap and
|
|
@@ -134,10 +178,11 @@ export declare function resolveResource(provider: Provider, resolverAction: stri
|
|
|
134
178
|
* methods reject a `#name` with `invalid_arguments`.
|
|
135
179
|
*
|
|
136
180
|
* Membership: public channels are listed whether or not the bot joined, but
|
|
137
|
-
* posting
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
181
|
+
* everything except posting (`send-message`) needs membership — reading
|
|
182
|
+
* history, adding reactions, inviting users all fail `not_in_channel` until
|
|
183
|
+
* the bot joins. On that code, call `join-channel` with the ID and retry
|
|
184
|
+
* once. Private channels are listed only when the bot is already a member (and
|
|
185
|
+
* only when the connection has the groups:read scope — without it this helper
|
|
141
186
|
* automatically falls back to public channels only). A private channel cannot
|
|
142
187
|
* be self-joined: surface "invite the bot to #channel in Slack" to the user.
|
|
143
188
|
*/
|
package/dist/nango.js
CHANGED
|
@@ -10,8 +10,9 @@ export class ConnectorError extends Error {
|
|
|
10
10
|
status;
|
|
11
11
|
/**
|
|
12
12
|
* For `action_failed`: what the action script threw — usually the
|
|
13
|
-
* provider-level failure
|
|
14
|
-
*
|
|
13
|
+
* provider-level failure. Scripts disagree on where the provider's error
|
|
14
|
+
* code sits inside it (`response.error`, `error`, or `message`), so don't
|
|
15
|
+
* read it by hand: for Slack use `slackErrorCode(err)`.
|
|
15
16
|
*/
|
|
16
17
|
detail;
|
|
17
18
|
constructor(args) {
|
|
@@ -281,47 +282,40 @@ function sleep(ms) {
|
|
|
281
282
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
282
283
|
}
|
|
283
284
|
/**
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
* process lifetime; Slack rate limits (conversations.list is a low-tier
|
|
290
|
-
* method) are absorbed by waiting and resuming from the same cursor.
|
|
285
|
+
* Slack's error code (e.g. 'not_in_channel', 'channel_not_found',
|
|
286
|
+
* 'is_archived') from a failed Slack action, or undefined when `err` is not an
|
|
287
|
+
* action failure. Branch on this instead of reading `err.detail` yourself: the
|
|
288
|
+
* vendored Slack scripts put the code in different places (`response.error`,
|
|
289
|
+
* `error`, or `message`), and this reads them in that order.
|
|
291
290
|
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
*
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
291
|
+
* ```typescript
|
|
292
|
+
* try {
|
|
293
|
+
* await slack.action('get-conversation-history', { channel_id, limit: 10 });
|
|
294
|
+
* } catch (err) {
|
|
295
|
+
* if (slackErrorCode(err) === 'not_in_channel') {
|
|
296
|
+
* // join with the channel ID and retry once; private channels can't be
|
|
297
|
+
* // self-joined — tell the user to invite the bot instead
|
|
298
|
+
* }
|
|
299
|
+
* throw err;
|
|
300
|
+
* }
|
|
301
|
+
* ```
|
|
303
302
|
*/
|
|
304
|
-
export
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
const
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
303
|
+
export function slackErrorCode(err) {
|
|
304
|
+
if (!(err instanceof ConnectorError) || err.code !== 'action_failed')
|
|
305
|
+
return undefined;
|
|
306
|
+
const detail = err.detail;
|
|
307
|
+
const code = detail?.response?.error ?? detail?.error ?? detail?.message;
|
|
308
|
+
return typeof code === 'string' ? code : undefined;
|
|
309
|
+
}
|
|
310
|
+
// One-page fetcher over conversations.list that owns the Slack quirks a naive
|
|
311
|
+
// walk gets wrong: rate-limit waits resume from the SAME cursor (throwing would
|
|
312
|
+
// discard the walk and a retry would restart from page 1), and a missing_scope
|
|
313
|
+
// for private_channel (no groups:read) fails the WHOLE first call, so retrying
|
|
314
|
+
// public-only from the same cursor restarts the walk correctly.
|
|
315
|
+
function slackChannelPageFetcher(slack) {
|
|
317
316
|
let types = 'public_channel,private_channel';
|
|
318
317
|
let rateLimitWaits = 0;
|
|
319
|
-
|
|
320
|
-
// rate-limit waits resume from the SAME cursor (throwing would discard the
|
|
321
|
-
// walk and a retry would restart from page 1), and a missing_scope for
|
|
322
|
-
// private_channel (no groups:read) fails the WHOLE first call, so retrying
|
|
323
|
-
// public-only from the same cursor restarts the walk correctly.
|
|
324
|
-
const fetchPage = async (cursor) => {
|
|
318
|
+
return async (cursor) => {
|
|
325
319
|
for (;;) {
|
|
326
320
|
let res;
|
|
327
321
|
try {
|
|
@@ -361,14 +355,90 @@ export async function slackFindChannelId(slack, name) {
|
|
|
361
355
|
}
|
|
362
356
|
throw new Error(data.error ?? 'Slack conversations.list failed');
|
|
363
357
|
}
|
|
364
|
-
|
|
358
|
+
const channels = [];
|
|
359
|
+
for (const channel of data.channels ?? []) {
|
|
360
|
+
if (!channel.id || !channel.name)
|
|
361
|
+
continue;
|
|
362
|
+
channels.push({
|
|
363
|
+
id: channel.id,
|
|
364
|
+
name: channel.name,
|
|
365
|
+
isPrivate: channel.is_private === true,
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
// Slack sends an empty string, not an absent key, on the last page; a
|
|
369
|
+
// cursor identical to the one just used would loop forever.
|
|
370
|
+
const next = data.response_metadata?.next_cursor || undefined;
|
|
371
|
+
return { channels, nextCursor: next && next !== cursor ? next : undefined };
|
|
365
372
|
}
|
|
366
373
|
};
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Every channel the bot can see, sorted by name. Use this to populate a
|
|
377
|
+
* channel dropdown instead of paging the `list-channels` action yourself: that
|
|
378
|
+
* action tops out at 200 per page and a large workspace needs dozens of
|
|
379
|
+
* sequential calls, which is a 20–30 second dropdown. This walks
|
|
380
|
+
* conversations.list at Slack's maximum page size and handles rate limits and
|
|
381
|
+
* the private-channel scope fallback. Not cached: the client carries no
|
|
382
|
+
* workspace identity to key on, and an app transfer briefly serves two
|
|
383
|
+
* workspaces from one deployment.
|
|
384
|
+
*
|
|
385
|
+
* Public channels are listed whether or not the bot has joined; private
|
|
386
|
+
* channels only when it is already a member. Listing a channel does not make
|
|
387
|
+
* it readable — see slackFindChannelId for the membership rules.
|
|
388
|
+
*/
|
|
389
|
+
export async function slackListChannels(slack) {
|
|
390
|
+
const fetchPage = slackChannelPageFetcher(slack);
|
|
391
|
+
const channels = [];
|
|
392
|
+
let cursor;
|
|
393
|
+
for (let page = 0; page < SLACK_LIST_MAX_PAGES; page++) {
|
|
394
|
+
const result = await fetchPage(cursor);
|
|
395
|
+
channels.push(...result.channels);
|
|
396
|
+
if (!result.nextCursor)
|
|
397
|
+
break;
|
|
398
|
+
cursor = result.nextCursor;
|
|
399
|
+
}
|
|
400
|
+
channels.sort((a, b) => a.name.localeCompare(b.name));
|
|
401
|
+
return channels;
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Resolve a Slack channel name to its ID, walking the cursor-paginated
|
|
405
|
+
* conversations.list until found (Slack treats `limit` as a per-page cap and
|
|
406
|
+
* may return fewer; a single un-paginated call misses channels in large
|
|
407
|
+
* workspaces). Input is normalized (leading '#' stripped, lowercased — Slack
|
|
408
|
+
* channel names are always lowercase). Resolved IDs are memoized for the
|
|
409
|
+
* process lifetime; Slack rate limits (conversations.list is a low-tier
|
|
410
|
+
* method) are absorbed by waiting and resuming from the same cursor.
|
|
411
|
+
*
|
|
412
|
+
* Use the returned ID for every follow-up call — conversations.join /
|
|
413
|
+
* conversations.history / chat.postMessage should all be given IDs; most
|
|
414
|
+
* methods reject a `#name` with `invalid_arguments`.
|
|
415
|
+
*
|
|
416
|
+
* Membership: public channels are listed whether or not the bot joined, but
|
|
417
|
+
* everything except posting (`send-message`) needs membership — reading
|
|
418
|
+
* history, adding reactions, inviting users all fail `not_in_channel` until
|
|
419
|
+
* the bot joins. On that code, call `join-channel` with the ID and retry
|
|
420
|
+
* once. Private channels are listed only when the bot is already a member (and
|
|
421
|
+
* only when the connection has the groups:read scope — without it this helper
|
|
422
|
+
* automatically falls back to public channels only). A private channel cannot
|
|
423
|
+
* be self-joined: surface "invite the bot to #channel in Slack" to the user.
|
|
424
|
+
*/
|
|
425
|
+
export async function slackFindChannelId(slack, name) {
|
|
426
|
+
const target = name.replace(/^#/, '').trim().toLowerCase();
|
|
427
|
+
if (!target)
|
|
428
|
+
return null;
|
|
429
|
+
const cached = slackChannelIdCache.get(target);
|
|
430
|
+
if (cached)
|
|
431
|
+
return cached;
|
|
432
|
+
const fetchPage = slackChannelPageFetcher(slack);
|
|
367
433
|
// Slack channel names are always lowercase, so an exact label match is the
|
|
368
434
|
// same comparison the raw walk made against channel.name.
|
|
369
|
-
const hit = await findResource(
|
|
370
|
-
|
|
371
|
-
|
|
435
|
+
const hit = await findResource(async (cursor) => {
|
|
436
|
+
const page = await fetchPage(cursor);
|
|
437
|
+
return {
|
|
438
|
+
options: page.channels.map((channel) => ({ id: channel.id, label: channel.name })),
|
|
439
|
+
nextCursor: page.nextCursor,
|
|
440
|
+
};
|
|
441
|
+
}, (option) => option.label === target, { maxPages: SLACK_LIST_MAX_PAGES });
|
|
372
442
|
if (hit)
|
|
373
443
|
slackChannelIdCache.set(target, hit.id);
|
|
374
444
|
return hit?.id ?? null;
|
|
@@ -408,12 +478,9 @@ export async function slackPostMessage(slack, args) {
|
|
|
408
478
|
return { ok: result.ok, ts: result.ts };
|
|
409
479
|
}
|
|
410
480
|
catch (err) {
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
if (typeof slackError === 'string')
|
|
415
|
-
return { ok: false, error: slackError };
|
|
416
|
-
}
|
|
481
|
+
const slackError = slackErrorCode(err);
|
|
482
|
+
if (slackError)
|
|
483
|
+
return { ok: false, error: slackError };
|
|
417
484
|
throw err;
|
|
418
485
|
}
|
|
419
486
|
};
|
|
@@ -20,5 +20,17 @@ export interface ExchangedSession {
|
|
|
20
20
|
/** Session token this bearer was exchanged from — lets the proxy tell a
|
|
21
21
|
* reload of an already-exchanged token apart from a new open. */
|
|
22
22
|
sessionId?: string;
|
|
23
|
+
/** Keys of whichever PostHog flags — from platform-api's small, explicit
|
|
24
|
+
* app-foundation-visible-flags allowlist (session.go's appFoundationVisibleFlags) — are on
|
|
25
|
+
* for this workspace. Not every flag: most are irrelevant or sensitive and
|
|
26
|
+
* never reach this channel. Sourced from the exchange RESPONSE at mint time
|
|
27
|
+
* (see mint-bearer.ts), deliberately not a bearer claim — flags are
|
|
28
|
+
* configuration with their own freshness (refresh via fetchSessionFlags),
|
|
29
|
+
* so decodeBearer never populates this. Use hasFlag() to check one; any
|
|
30
|
+
* AND/OR combination is the caller's own logic over this raw set. */
|
|
31
|
+
flags?: string[];
|
|
23
32
|
}
|
|
33
|
+
/** Whether a decoded session carries the given flag key. Null-safe so callers
|
|
34
|
+
* can chain straight off decodeBearer()'s result without a separate guard. */
|
|
35
|
+
export declare function hasFlag(session: ExchangedSession | null, key: string): boolean;
|
|
24
36
|
export declare function decodeBearer(bearer: string): ExchangedSession | null;
|
package/dist/session-exchange.js
CHANGED
|
@@ -73,6 +73,11 @@ export function parseExchangeRejection(value) {
|
|
|
73
73
|
export function isSessionExchangeEnabled() {
|
|
74
74
|
return process.env.ENABLE_APP_SESSION_TOKEN_EXCHANGE === 'true';
|
|
75
75
|
}
|
|
76
|
+
/** Whether a decoded session carries the given flag key. Null-safe so callers
|
|
77
|
+
* can chain straight off decodeBearer()'s result without a separate guard. */
|
|
78
|
+
export function hasFlag(session, key) {
|
|
79
|
+
return session?.flags?.includes(key) ?? false;
|
|
80
|
+
}
|
|
76
81
|
// Decodes a base64url JWT segment to a UTF-8 string using only Web APIs (atob,
|
|
77
82
|
// TextDecoder), so decodeBearer runs in BOTH the Edge middleware runtime (proxy)
|
|
78
83
|
// and the Node route-handler runtime (requireAuth / getAssemblyForRequest) —
|
package/package.json
CHANGED
|
@@ -98,7 +98,7 @@ A connector call throws a **`ConnectorError`** (with `.code`, `.provider`, `.sta
|
|
|
98
98
|
- `connection_required` / `needs_reauth` / `needs_reauth_scope` — the workspace admin needs to (re)connect the provider in app settings. Render a "needs setup / reconnect" state, not a crash.
|
|
99
99
|
- `connection_throttled` — too many calls on the shared connection; back off and retry.
|
|
100
100
|
- `connector_unavailable` — the provider or Nango is temporarily unavailable.
|
|
101
|
-
- `action_failed` — the pre-built action ran but the provider rejected the operation. The provider-level failure is in `err.detail` (e.g. `
|
|
101
|
+
- `action_failed` — the pre-built action ran but the provider rejected the operation. The provider-level failure is in `err.detail`, but scripts disagree on where the provider's code sits inside it, so never read `detail` by hand. For Slack call `slackErrorCode(err)` (returns e.g. `'not_in_channel'`, `'is_archived'`) and branch on that; for other providers branch on `err.detail` the way you would on a proxy response's error field.
|
|
102
102
|
- `app_key_required` / `provider_not_declared` — a wiring bug: you called from an unauthenticated scope, or didn't declare the provider in `requiredIntegrations`. Fix the call site / manifest.
|
|
103
103
|
- `invalid_request` — malformed relay input (bad action name, unreadable body). Fix the call site.
|
|
104
104
|
|
|
@@ -1,25 +1,41 @@
|
|
|
1
1
|
# Slack
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Everything except `send-message` needs membership, not just visibility. Posting reaches any public channel without joining (`chat:write.public`); reading history, adding reactions, and inviting users all fail `not_in_channel` until the bot joins.
|
|
4
4
|
|
|
5
5
|
- **Post with `slackPostMessage` from `@assembly-js/app-foundation/server` — never hand-roll `chat.postMessage`.** It resolves a `#name` to the channel ID, posts, and on `not_in_channel` joins the channel and retries once. Slack-level failures come back as `{ ok: false, error }`; on `not_in_channel` render "invite the bot to #channel in Slack" (private channels cannot be self-joined).
|
|
6
|
+
- **List channels with `slackListChannels` from `@assembly-js/app-foundation/server` — never page the `list-channels` action yourself.** That action returns at most 200 per page, so a large workspace is dozens of sequential calls and a 20–30 second dropdown; the helper walks at Slack's maximum page size and handles rate limits and the private-channel scope fallback. It returns `{ id, name, isPrivate }` sorted by name.
|
|
6
7
|
- **Resolve channel names with `slackFindChannelId` from `@assembly-js/app-foundation/server` for every other per-channel call.** It handles cursor pagination (a single `conversations.list` misses channels in large workspaces), name normalization, scope fallback, rate limits, and caching. Use the returned ID; most Slack methods reject a `#name` with `invalid_arguments`.
|
|
7
|
-
- **
|
|
8
|
-
- **
|
|
8
|
+
- **Read the Slack error code with `slackErrorCode(err)` from `@assembly-js/app-foundation/server` — never read `err.detail` yourself.** The vendored scripts put the code in different places inside `detail`; the helper returns it (`'not_in_channel'`, `'channel_not_found'`, `'is_archived'`, …) or `undefined` when the error isn't a Slack rejection.
|
|
9
|
+
- **Every other per-channel call (history, reactions, invites):** when `slackErrorCode(err) === 'not_in_channel'`, call `join-channel` with the channel ID, then retry once. Private channels fail the join with `method_not_supported_for_channel_type` — tell the user to invite the bot instead of retrying.
|
|
10
|
+
- **Scopes:** `chat:write.public` lets posting reach any public channel without joining, so only the other calls need `join-channel` (which needs `channels:join`). Add `groups:read` if the app must find private channels.
|
|
9
11
|
|
|
10
12
|
```typescript
|
|
11
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
getIntegration,
|
|
15
|
+
slackErrorCode,
|
|
16
|
+
slackFindChannelId,
|
|
17
|
+
slackListChannels,
|
|
18
|
+
slackPostMessage,
|
|
19
|
+
} from '@assembly-js/app-foundation/server';
|
|
12
20
|
|
|
13
21
|
const slack = await getIntegration('slack');
|
|
22
|
+
const channels = await slackListChannels(slack); // dropdown options
|
|
14
23
|
const channelId = await slackFindChannelId(slack, 'general');
|
|
15
24
|
const posted = await slackPostMessage(slack, { channel: '#general', text: 'Hello' });
|
|
16
25
|
```
|
|
17
26
|
|
|
18
27
|
```typescript
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
limit: 10
|
|
22
|
-
|
|
28
|
+
// Reads need membership too: join on not_in_channel and retry once.
|
|
29
|
+
async function history(channelId: string) {
|
|
30
|
+
const input = { channel_id: channelId, limit: 10 };
|
|
31
|
+
try {
|
|
32
|
+
return await slack.action('get-conversation-history', input);
|
|
33
|
+
} catch (err) {
|
|
34
|
+
if (slackErrorCode(err) !== 'not_in_channel') throw err;
|
|
35
|
+
await slack.action('join-channel', { channel_id: channelId });
|
|
36
|
+
return slack.action('get-conversation-history', input);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
23
39
|
```
|
|
24
40
|
|
|
25
41
|
For an operation no action covers, relay through the proxy — e.g. `slack.post({ endpoint: '/chat.update', data: { channel: channelId, ts, text } })`.
|
|
@@ -66,6 +66,8 @@ export function ClientList() {
|
|
|
66
66
|
}
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
+
`deps` works like a `useEffect` dependency array, with one rule: **deps must be primitives** — an id, a cursor, a `length`, a boolean. A derived array or object (`records`, `{ baseId }`, `items.filter(...)`) is a new identity on every render, so the action re-runs, its state update re-renders, and the page spins forever without a network call or an error to point at. Pass `records.length` or the id the array came from instead.
|
|
70
|
+
|
|
69
71
|
`deps` works like a `useEffect` dependency array — list any values your action closes over so it re-runs when they change.
|
|
70
72
|
|
|
71
73
|
For an imperative one-off (a button handler, a mutation), use `callAction` from `useAssembly()` — same re-exchange-and-retry behavior, no hook:
|