3
import {
4
groupCachedConnectedAccountsByToolkit,
5
resolveDefaultConnectedAccountsByToolkit,
6
+ type SelectableConnectedAccount,
7
} from 'src/services/connected-account-selection';
7
-import type { ConnectedAccountItem } from 'src/models/connected-accounts';
8
9
type RawConnectedAccount = {
10
readonly id: string;
48
49
const normalizeConnectedAccountStatus = (
50
status?: string | null
51
-): ConnectedAccountItem['status'] => {
51
+): SelectableConnectedAccount['status'] => {
52
switch (status) {
53
case 'INITIALIZING':
54
case 'INITIATED':
59
case 'REVOKED':
60
return status;
61
default:
62
- return 'INACTIVE';
62
+ // Apollo can introduce new statuses at any time. Use a sentinel
63
+ // (`'UNKNOWN'`) instead of coercing to `'INACTIVE'`, which would
64
+ // falsely tag the account as user-disabled in selection logic.
65
+ return 'UNKNOWN';
66
}
67
};
68
94
limit: 1000,
95
})
96
).pipe(
94
- Effect.map(response => {
95
- const items = (response.items ?? []) as ReadonlyArray<RawConnectedAccount>;
96
- const normalizedItems: ConnectedAccountItem[] = items.map(
97
- item =>
98
- ({
97
+ Effect.flatMap(response =>
98
+ Effect.gen(function* () {
99
+ const items = (response.items ?? []) as ReadonlyArray<RawConnectedAccount>;
100
+ const unknownStatuses = new Set<string>();
101
+ const normalizedItems: SelectableConnectedAccount[] = items.map(item => {
102
+ const normalizedStatus = normalizeConnectedAccountStatus(item.status);
103
+ if (normalizedStatus === 'UNKNOWN' && item.status) {
104
+ unknownStatuses.add(item.status);
105
+ }
106
+ return {
107
id: item.id,
108
alias: item.alias ?? null,
109
word_id: item.word_id ?? null,
102
- status: normalizeConnectedAccountStatus(item.status),
110
+ status: normalizedStatus,
111
status_reason: null,
112
is_disabled: item.is_disabled ?? false,
113
user_id: item.user_id ?? '',
123
created_at: item.created_at ?? '',
124
updated_at: item.updated_at ?? '',
125
test_request_endpoint: '',
118
- }) satisfies ConnectedAccountItem
119
- );
120
- const connectedToolkits = new Set<string>();
121
- const explicitAccountsByToolkit = new Map<string, RawConnectedAccount>();
126
+ } satisfies SelectableConnectedAccount;
127
+ });
128
123
- for (const item of items) {
124
- const toolkit = item.toolkit?.slug?.toLowerCase().trim();
125
- if (!toolkit || item.is_disabled) continue;
129
+ if (unknownStatuses.size > 0) {
130
+ yield* Effect.logWarning(
131
+ `[ToolRouterSession] received unrecognized connected_account.status ` +
132
+ `value(s): ${[...unknownStatuses].join(', ')}. Treating as 'UNKNOWN' ` +
133
+ `(not selectable). Run "composio upgrade" to pick up the latest schema.`
134
+ );
135
+ }
136
127
- connectedToolkits.add(toolkit);
137
+ const connectedToolkits = new Set<string>();
138
+ const explicitAccountsByToolkit = new Map<string, RawConnectedAccount>();
139
129
- // Tool Router already handles Composio-managed auth well.
130
- // Explicitly pin non-managed auth configs/accounts so consumer sessions
131
- // can execute against custom-auth toolkits like PostHog.
132
- if (item.auth_config?.is_composio_managed !== false) {
133
- continue;
134
- }
140
+ for (const item of items) {
141
+ const toolkit = item.toolkit?.slug?.toLowerCase().trim();
142
+ if (!toolkit || item.is_disabled) continue;
143
+
144
+ connectedToolkits.add(toolkit);
145
136
- const current = explicitAccountsByToolkit.get(toolkit);
137
- if (!current || isNewerAccount(item, current)) {
138
- explicitAccountsByToolkit.set(toolkit, item);
146
+ // Tool Router already handles Composio-managed auth well.
147
+ // Explicitly pin non-managed auth configs/accounts so consumer sessions
148
+ // can execute against custom-auth toolkits like PostHog.
149
+ if (item.auth_config?.is_composio_managed !== false) {
150
+ continue;
151
+ }
152
+
153
+ const current = explicitAccountsByToolkit.get(toolkit);
154
+ if (!current || isNewerAccount(item, current)) {
155
+ explicitAccountsByToolkit.set(toolkit, item);
156
+ }
157
}
140
- }
158
142
- const authConfigs: Record<string, string> = {};
143
- for (const [toolkit, item] of explicitAccountsByToolkit) {
144
- const authConfigId = item.auth_config?.id?.trim();
145
- if (!authConfigId) continue;
146
- authConfigs[toolkit] = authConfigId;
147
- }
159
+ const authConfigs: Record<string, string> = {};
160
+ for (const [toolkit, item] of explicitAccountsByToolkit) {
161
+ const authConfigId = item.auth_config?.id?.trim();
162
+ if (!authConfigId) continue;
163
+ authConfigs[toolkit] = authConfigId;
164
+ }
165
149
- return {
150
- connectedToolkits: [...connectedToolkits],
151
- authConfigs: Object.keys(authConfigs).length > 0 ? authConfigs : undefined,
152
- connectedAccounts: (() => {
153
- const selected = resolveDefaultConnectedAccountsByToolkit(normalizedItems);
154
- return Object.keys(selected).length > 0 ? selected : undefined;
155
- })(),
156
- availableConnectedAccounts: (() => {
157
- const grouped = groupCachedConnectedAccountsByToolkit(normalizedItems);
158
- return Object.keys(grouped).length > 0 ? grouped : undefined;
159
- })(),
160
- } satisfies ToolRouterSessionConnectionContext;
161
- }),
166
+ return {
167
+ connectedToolkits: [...connectedToolkits],
168
+ authConfigs: Object.keys(authConfigs).length > 0 ? authConfigs : undefined,
169
+ connectedAccounts: (() => {
170
+ const selected = resolveDefaultConnectedAccountsByToolkit(normalizedItems);
171
+ return Object.keys(selected).length > 0 ? selected : undefined;
172
+ })(),
173
+ availableConnectedAccounts: (() => {
174
+ const grouped = groupCachedConnectedAccountsByToolkit(normalizedItems);
175
+ return Object.keys(grouped).length > 0 ? grouped : undefined;
176
+ })(),
177
+ } satisfies ToolRouterSessionConnectionContext;
178
+ })
179
+ ),
180
Effect.catchAll(() =>
181
Effect.succeed({
182
connectedToolkits: [],