@stage-labs/metro 0.1.0-beta.276 → 0.1.0-beta.277
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/dist/claude.js +2 -1
- package/dist/native-recovery-scan.js +246 -0
- package/dist/native-recovery-state.js +154 -0
- package/dist/native-recovery.js +164 -0
- package/package.json +1 -1
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +1 -1
- package/runtime/marketplace/plugin/bin/policy.mjs +4 -0
- package/runtime/node_modules/@metro-labs/core/package.json +2 -0
- package/runtime/node_modules/@metro-labs/core/src/runner-activity.ts +2 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/channel-directory.ts +159 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/channel-pages.ts +129 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/types.ts +1 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +1 -1
- package/runtime/node_modules/@metro-labs/daemon/src/claude/runner-process.ts +6 -4
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +3 -3
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/channel-tool.ts +80 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-prompt.ts +12 -1
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-gate.ts +6 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-catalog.ts +4 -1
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-dispatch.ts +7 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +2 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/channel-source.ts +79 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/channels.ts +186 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +1 -0
- package/runtime/node_modules/@metro-labs/gmail/src/actions.ts +2 -1
- package/runtime/node_modules/@metro-labs/gmail/src/channels.ts +66 -0
- package/runtime/node_modules/@metro-labs/gmail/src/station.ts +1 -0
- package/runtime/node_modules/@metro-labs/outlook/src/actions.ts +2 -1
- package/runtime/node_modules/@metro-labs/outlook/src/channels.ts +60 -0
- package/runtime/node_modules/@metro-labs/outlook/src/station.ts +1 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +19 -0
- package/runtime/node_modules/@metro-labs/telegram/src/channels.ts +58 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +1 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +2 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/channel-files.ts +3 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/channels.ts +43 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +5 -34
- package/runtime/node_modules/@metro-labs/telegram-bot/src/observed-chats.ts +172 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +4 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +30 -3
- package/runtime/node_modules/@metro-labs/telegram-bot/src/updates.ts +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +13 -1
- package/runtime/node_modules/@metro-labs/threema/src/actions.ts +7 -1
- package/runtime/node_modules/@metro-labs/threema/src/channels.ts +37 -0
- package/runtime/node_modules/@metro-labs/threema/src/chats.ts +88 -0
- package/runtime/node_modules/@metro-labs/threema/src/inbound.ts +5 -0
- package/runtime/node_modules/@metro-labs/threema/src/outbound.ts +1 -0
- package/runtime/node_modules/@metro-labs/threema/src/station.ts +10 -2
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +11 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/channel-chats.ts +137 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/channels.ts +48 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client-types.ts +2 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +8 -6
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +4 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +3 -1
- package/runtime/node_modules/@metro-labs/xmtp/src/channels.ts +56 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/network.ts +4 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +1 -0
- package/runtime/runtime.json +1 -1
- package/runtime/sdk-runner/node_modules/@metro-labs/core/package.json +2 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/runner-activity.ts +2 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/channel-directory.ts +159 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/channel-pages.ts +129 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/types.ts +1 -0
- package/runtime/sdk-runner/runner.json +1 -1
- package/runtime/sdk-runner/src/activity-tasks.ts +7 -1
- package/runtime/sdk-runner/src/activity.ts +33 -3
- package/runtime/sdk-runner/src/app.ts +5 -2
- package/runtime/sdk-runner/src/inbox.ts +10 -1
- package/runtime/sdk-runner/src/input-queue.ts +6 -4
- package/runtime/sdk-runner/src/main.ts +21 -8
- package/runtime/sdk-runner/src/rules.ts +1 -0
- package/runtime/sdk-runner/src/runner.ts +18 -4
- package/runtime/sdk-runner/src/session-store.ts +19 -4
- package/runtime/sdk-runner/src/session-watch.ts +5 -1
- package/runtime/sdk-runner/src/task-ledger.ts +229 -0
- package/runtime/sdk-runner/src/task-recovery.ts +203 -0
- package/runtime/sdk-runner/src/task-state.ts +71 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { WASocket } from 'baileys';
|
|
2
|
+
import { ChannelDirectory, DIRECTORY_LIMIT, type ChannelEntry, type ChannelList } from '@metro-labs/core/stations/channel-directory';
|
|
3
|
+
import { lineOf } from './accounts.js';
|
|
4
|
+
import { KnownChats, channelName } from './channel-chats.js';
|
|
5
|
+
|
|
6
|
+
const REASON = `Participating groups come from WhatsApp. Direct chats are limited to at most ${DIRECTORY_LIMIT} chat metadata records observed or synced to this installation; WhatsApp has no all-direct-chats directory. Cleared chats are omitted, and WhatsApp channels are not enumerated.`;
|
|
7
|
+
|
|
8
|
+
export class WhatsAppChannels {
|
|
9
|
+
private readonly chats: KnownChats;
|
|
10
|
+
private readonly directory = new ChannelDirectory();
|
|
11
|
+
|
|
12
|
+
constructor(private readonly accountId: string) {
|
|
13
|
+
this.chats = new KnownChats(accountId);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
bind(sock: Pick<WASocket, 'ev'>, current: () => boolean): void {
|
|
17
|
+
sock.ev.on('chats.upsert', (chats) => {
|
|
18
|
+
if (current()) this.chats.note(chats, true);
|
|
19
|
+
});
|
|
20
|
+
sock.ev.on('chats.update', (chats) => {
|
|
21
|
+
if (current()) this.chats.note(chats, true);
|
|
22
|
+
});
|
|
23
|
+
sock.ev.on('messaging-history.set', ({ chats }) => {
|
|
24
|
+
if (current()) this.chats.note(chats);
|
|
25
|
+
});
|
|
26
|
+
sock.ev.on('chats.delete', (ids) => {
|
|
27
|
+
if (current()) this.chats.remove(ids);
|
|
28
|
+
});
|
|
29
|
+
sock.ev.on('chats.clear', ({ id }) => {
|
|
30
|
+
if (current()) this.chats.remove([id]);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
list(ready: () => Promise<Pick<WASocket, 'groupFetchAllParticipating'>>, args: Record<string, unknown>): Promise<ChannelList> {
|
|
35
|
+
return this.directory.list(this.accountId, args, async () => {
|
|
36
|
+
const groups = await (await ready()).groupFetchAllParticipating();
|
|
37
|
+
const channels: ChannelEntry[] = [];
|
|
38
|
+
for (const group of Object.values(groups)) {
|
|
39
|
+
if (!/^\d+(?:-\d+)?@g\.us$/.test(group.id) || group.id.length > 128) continue;
|
|
40
|
+
const name = channelName(group.subject);
|
|
41
|
+
channels.push({ id: group.id, line: lineOf(this.accountId, group.id), kind: 'group', ...(name ? { name } : {}) });
|
|
42
|
+
if (channels.length > DIRECTORY_LIMIT) break;
|
|
43
|
+
}
|
|
44
|
+
channels.push(...this.chats.list());
|
|
45
|
+
return { channels, capability: { supported: true, complete: false, source: 'mixed', reason: REASON } };
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type makeWASocket from 'baileys';
|
|
2
2
|
import type { fetchLatestWaWebVersion, WAMessage } from 'baileys';
|
|
3
3
|
import type { MemberList } from '@metro-labs/core/stations/types';
|
|
4
|
+
import type { ChannelList } from '@metro-labs/core/stations/channel-directory';
|
|
4
5
|
import type { ProfileChange } from '@metro-labs/core/stations/profile';
|
|
5
6
|
import type { SenderProfile } from '@metro-labs/core/stations/sender-profile';
|
|
6
7
|
import type { WhatsAppAccount } from './types.js';
|
|
@@ -27,6 +28,7 @@ export interface WAClient {
|
|
|
27
28
|
start(handlers: InboundHandlers): Promise<void>;
|
|
28
29
|
read(jid: string, options?: HistoryOptions): Promise<HistoryPage>;
|
|
29
30
|
listMembers(jid: string, limit?: number): Promise<MemberList>;
|
|
31
|
+
listChannels(args: Record<string, unknown>): Promise<ChannelList>;
|
|
30
32
|
sendText(jid: string, text: string, quotedId?: string): Promise<string>;
|
|
31
33
|
sendMedia(jid: string, media: WAMedia, quotedId?: string): Promise<string>;
|
|
32
34
|
sendReaction(jid: string, messageId: string, emoji: string): Promise<void>;
|
|
@@ -17,6 +17,7 @@ import { makeProfileCache, nonEmpty, type SenderProfile } from '@metro-labs/core
|
|
|
17
17
|
import type { SelfRef } from './parse.js';
|
|
18
18
|
import type { History } from './history.js';
|
|
19
19
|
import { createClientHistory } from './client-history.js';
|
|
20
|
+
import { WhatsAppChannels } from './channels.js';
|
|
20
21
|
import { bindMessages } from './messages.js';
|
|
21
22
|
import { listMembers } from './members.js';
|
|
22
23
|
import { SendExpiry } from './send-expiry.js';
|
|
@@ -49,17 +50,15 @@ interface State {
|
|
|
49
50
|
outbox: Outbox;
|
|
50
51
|
acks: AckWatch;
|
|
51
52
|
names: NameBook;
|
|
53
|
+
channels: WhatsAppChannels;
|
|
52
54
|
}
|
|
53
|
-
|
|
54
55
|
type SendOpts = Parameters<WASocket['sendMessage']>[2];
|
|
55
56
|
type SendContent = Parameters<WASocket['sendMessage']>[1];
|
|
56
|
-
|
|
57
57
|
function resetGate(st: State): void {
|
|
58
58
|
st.openPromise = new Promise<void>((resolve) => {
|
|
59
59
|
st.openResolve = resolve;
|
|
60
60
|
});
|
|
61
61
|
}
|
|
62
|
-
|
|
63
62
|
function selfRef(st: State, sock: WASocket): SelfRef {
|
|
64
63
|
const jids = new Set<string>();
|
|
65
64
|
const me = sock.user;
|
|
@@ -180,10 +179,10 @@ async function connect(st: State): Promise<void> {
|
|
|
180
179
|
sock.ev.on('creds.update', () => void saveCreds());
|
|
181
180
|
bindConnection(st, sock);
|
|
182
181
|
bindInbound(st, sock);
|
|
182
|
+
st.channels.bind(sock, () => !st.closed && st.sock === sock);
|
|
183
183
|
st.expiry.bind(sock, () => !st.closed && st.sock === sock);
|
|
184
184
|
bindDelivery(st, sock);
|
|
185
185
|
}
|
|
186
|
-
|
|
187
186
|
async function lidFor(st: State, sock: WASocket, jid: string): Promise<string | null> {
|
|
188
187
|
try {
|
|
189
188
|
return await sock.signalRepository.lidMapping.getLIDForPN(jid);
|
|
@@ -194,13 +193,11 @@ async function lidFor(st: State, sock: WASocket, jid: string): Promise<string |
|
|
|
194
193
|
return null;
|
|
195
194
|
}
|
|
196
195
|
}
|
|
197
|
-
|
|
198
196
|
async function ready(st: State): Promise<WASocket> {
|
|
199
197
|
await st.openPromise;
|
|
200
198
|
if (st.closed || !st.sock) throw new TrainError('whatsapp_call', 'socket not connected');
|
|
201
199
|
return st.sock;
|
|
202
200
|
}
|
|
203
|
-
|
|
204
201
|
async function send(
|
|
205
202
|
st: State,
|
|
206
203
|
jid: string,
|
|
@@ -297,6 +294,7 @@ function initialState(account: WhatsAppAccount, runtime: ClientRuntime): State {
|
|
|
297
294
|
outbox: makeOutbox(),
|
|
298
295
|
acks: makeAckWatch(),
|
|
299
296
|
names: makeNameBook(nameFiles.path(account.id)),
|
|
297
|
+
channels: new WhatsAppChannels(account.id),
|
|
300
298
|
};
|
|
301
299
|
resetGate(st);
|
|
302
300
|
return st;
|
|
@@ -335,6 +333,10 @@ export function createClient(
|
|
|
335
333
|
listMembers(jid, limit) {
|
|
336
334
|
return listMembers(jid, async (node): Promise<unknown> => await (await ready(st)).query(node), st.names, limit);
|
|
337
335
|
},
|
|
336
|
+
listChannels(args) {
|
|
337
|
+
if (st.closed) return Promise.reject(new TrainError('whatsapp_call', 'socket not connected'));
|
|
338
|
+
return st.channels.list(() => ready(st), args);
|
|
339
|
+
},
|
|
338
340
|
sendText(jid, text, quotedId) {
|
|
339
341
|
return send(
|
|
340
342
|
st,
|
|
@@ -3,6 +3,7 @@ import { PROFILE_FIELDS, type ProfileField } from '@metro-labs/core/stations/pro
|
|
|
3
3
|
import { tokenFiles } from './token-store.js';
|
|
4
4
|
import { nameFiles } from './names.js';
|
|
5
5
|
import { historyFiles } from './history-files.js';
|
|
6
|
+
import { channelFiles } from './channel-chats.js';
|
|
6
7
|
|
|
7
8
|
export const whatsappStation: Station = {
|
|
8
9
|
name: 'whatsapp',
|
|
@@ -22,15 +23,18 @@ export const whatsappStation: Station = {
|
|
|
22
23
|
profileFields: new Set<ProfileField>(PROFILE_FIELDS),
|
|
23
24
|
readsProfiles: true,
|
|
24
25
|
resolvesSenders: true,
|
|
26
|
+
discoversChannels: true,
|
|
25
27
|
forget: (accountId) => {
|
|
26
28
|
tokenFiles.forget(accountId);
|
|
27
29
|
nameFiles.forget(accountId);
|
|
28
30
|
historyFiles.forget(accountId);
|
|
31
|
+
channelFiles.forget(accountId);
|
|
29
32
|
},
|
|
30
33
|
forgetExcept: (kept) => {
|
|
31
34
|
tokenFiles.forgetExcept(kept);
|
|
32
35
|
nameFiles.forgetExcept(kept);
|
|
33
36
|
historyFiles.forgetExcept(kept);
|
|
37
|
+
channelFiles.forgetExcept(kept);
|
|
34
38
|
},
|
|
35
39
|
tools: [],
|
|
36
40
|
};
|
|
@@ -24,6 +24,7 @@ import { TrainError } from '@metro-labs/core/train-error';
|
|
|
24
24
|
import { claimNameAction, nameAction, setProfile } from './profile.js';
|
|
25
25
|
import { profileAction } from './sender.js';
|
|
26
26
|
import { resolveSenderAction } from './resolve.js';
|
|
27
|
+
import { listChannels } from './channels.js';
|
|
27
28
|
import { makeStation, respond, type CallMsg } from '@metro-labs/core/stations/station-runtime';
|
|
28
29
|
|
|
29
30
|
type Args = Record<string, unknown>;
|
|
@@ -234,6 +235,7 @@ async function accountsAction(id: string): Promise<void> {
|
|
|
234
235
|
|
|
235
236
|
const handlers: Record<string, (id: string, args: Args) => Promise<void>> = {
|
|
236
237
|
accounts: (id) => accountsAction(id),
|
|
238
|
+
listChannels,
|
|
237
239
|
set_profile: setProfile,
|
|
238
240
|
claim_name: claimNameAction,
|
|
239
241
|
name: nameAction,
|
|
@@ -256,7 +258,7 @@ export type { CallMsg };
|
|
|
256
258
|
export const handleCall = makeStation({
|
|
257
259
|
handlers: Object.fromEntries(Object.entries(handlers).map(([action, handler]) => [
|
|
258
260
|
action,
|
|
259
|
-
action === 'accounts' || action === 'callLeftovers'
|
|
261
|
+
action === 'accounts' || action === 'callLeftovers' || action === 'listChannels'
|
|
260
262
|
? handler
|
|
261
263
|
: (id: string, args: Args) => network.run(() => handler(id, args)),
|
|
262
264
|
])),
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ChannelDirectory,
|
|
3
|
+
DIRECTORY_LIMIT,
|
|
4
|
+
type ChannelSnapshot,
|
|
5
|
+
} from '@metro-labs/core/stations/channel-directory';
|
|
6
|
+
import { respond } from '@metro-labs/core/stations/station-runtime';
|
|
7
|
+
import { TrainError } from '@metro-labs/core/train-error';
|
|
8
|
+
import { ListConversationsOrderBy, type ListConversationsOptions } from '@xmtp/node-sdk';
|
|
9
|
+
import { accountForCall, lineOf, type Account } from './accounts.js';
|
|
10
|
+
import { syncConversations } from './network.js';
|
|
11
|
+
|
|
12
|
+
interface ConversationMetadata {
|
|
13
|
+
id: string;
|
|
14
|
+
name?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface ConversationSource {
|
|
18
|
+
sync: () => Promise<unknown>;
|
|
19
|
+
list: (options: ListConversationsOptions) => Promise<ConversationMetadata[]>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const directories = new WeakMap<Account, ChannelDirectory>();
|
|
23
|
+
|
|
24
|
+
export async function loadChannels(account: string, source: ConversationSource): Promise<ChannelSnapshot> {
|
|
25
|
+
await syncConversations(source);
|
|
26
|
+
const conversations = await source.list({
|
|
27
|
+
limit: DIRECTORY_LIMIT + 1,
|
|
28
|
+
includeDuplicateDms: false,
|
|
29
|
+
orderBy: ListConversationsOrderBy.CreatedAt,
|
|
30
|
+
});
|
|
31
|
+
return {
|
|
32
|
+
channels: conversations.slice(0, DIRECTORY_LIMIT + 1).map((conversation) => ({
|
|
33
|
+
id: conversation.id,
|
|
34
|
+
line: lineOf(account, conversation.id),
|
|
35
|
+
...('name' in conversation && conversation.name ? { name: conversation.name } : {}),
|
|
36
|
+
kind: 'name' in conversation ? 'group' : 'direct',
|
|
37
|
+
})),
|
|
38
|
+
capability: {
|
|
39
|
+
supported: true,
|
|
40
|
+
complete: false,
|
|
41
|
+
source: 'mixed',
|
|
42
|
+
reason: 'Only conversations known to this XMTP installation are listed; other devices may know more. Group names use locally stored metadata.' +
|
|
43
|
+
(conversations.length > DIRECTORY_LIMIT ? ` Results are limited to ${DIRECTORY_LIMIT} conversations.` : ''),
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function listChannels(id: string, args: Record<string, unknown>): Promise<void> {
|
|
49
|
+
if (typeof args.account !== 'string' || !args.account.trim())
|
|
50
|
+
throw new TrainError('INVALID_ARGS', 'listChannels requires an account');
|
|
51
|
+
const acct = accountForCall({ account: args.account });
|
|
52
|
+
const directory = directories.get(acct) ?? new ChannelDirectory();
|
|
53
|
+
directories.set(acct, directory);
|
|
54
|
+
const result = await directory.list(acct.cfg.id, args, () => loadChannels(acct.cfg.id, acct.client.conversations));
|
|
55
|
+
respond(id, { result });
|
|
56
|
+
}
|
|
@@ -108,3 +108,7 @@ const syncs = new SyncRequests();
|
|
|
108
108
|
export function syncConversation(owner: object, conv: { id: string; sync: () => Promise<unknown> }): Promise<unknown> {
|
|
109
109
|
return syncs.run(owner, conv.id, () => conv.sync());
|
|
110
110
|
}
|
|
111
|
+
|
|
112
|
+
export function syncConversations(source: { sync: () => Promise<unknown> }): Promise<unknown> {
|
|
113
|
+
return syncs.run(source, 'directory', () => source.sync());
|
|
114
|
+
}
|
|
@@ -6,6 +6,7 @@ export const xmtpStation: Station = {
|
|
|
6
6
|
name: 'xmtp',
|
|
7
7
|
hasAccounts: true,
|
|
8
8
|
hasTrain: true,
|
|
9
|
+
discoversChannels: true,
|
|
9
10
|
messageVerbs: new Set<Verb>(['send', 'reply', 'react', 'unreact', 'delete', 'read']),
|
|
10
11
|
readFilters: new Set(['message_id']),
|
|
11
12
|
groupOps: new Set<GroupOp>(['create_group', 'add_members', 'remove_members']),
|
package/runtime/runtime.json
CHANGED
|
@@ -21,6 +21,8 @@
|
|
|
21
21
|
"./lines": "./src/lines.ts",
|
|
22
22
|
"./endpoints": "./src/endpoints.ts",
|
|
23
23
|
"./stations/types": "./src/stations/types.ts",
|
|
24
|
+
"./stations/channel-directory": "./src/stations/channel-directory.ts",
|
|
25
|
+
"./stations/channel-pages": "./src/stations/channel-pages.ts",
|
|
24
26
|
"./stations/account-files": "./src/stations/account-files.ts",
|
|
25
27
|
"./stations/station-runtime": "./src/stations/station-runtime.ts",
|
|
26
28
|
"./stations/account-store": "./src/stations/account-store.ts",
|
|
@@ -91,6 +91,7 @@ export interface RunnerInput {
|
|
|
91
91
|
export interface RunnerActivity {
|
|
92
92
|
pid: number;
|
|
93
93
|
procStart?: string;
|
|
94
|
+
cancelSignal?: 'SIGUSR2';
|
|
94
95
|
runner: 'sdk';
|
|
95
96
|
phase: RunnerPhase;
|
|
96
97
|
mainPhase: RunnerPhase;
|
|
@@ -215,6 +216,7 @@ export function parseRunnerActivity(raw: unknown): RunnerActivity | null {
|
|
|
215
216
|
if (!isRecord(raw) || raw.runner !== 'sdk' || !phase(raw.phase) || count(raw.pid) === 0 || count(raw.updatedAt) === 0) return null;
|
|
216
217
|
return {
|
|
217
218
|
runner: 'sdk', pid: count(raw.pid), ...processIdentity(raw.procStart), phase: raw.phase, mainPhase: phase(raw.mainPhase) ? raw.mainPhase : raw.phase,
|
|
219
|
+
...(raw.cancelSignal === 'SIGUSR2' ? { cancelSignal: raw.cancelSignal } : {}),
|
|
218
220
|
mainStartedAt: count(raw.mainStartedAt) || null, sessionId: sessionId(raw.sessionId), updatedAt: count(raw.updatedAt),
|
|
219
221
|
pending: count(raw.pending), workers: count(raw.workers), approvals: count(raw.approvals),
|
|
220
222
|
tools: list(raw.tools, name, 20), activeTools: list(raw.activeTools, tool, 20),
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { Line } from '../lines.js';
|
|
2
|
+
import { ticketStore } from '../tickets.js';
|
|
3
|
+
import { TrainError } from '../train-error.js';
|
|
4
|
+
|
|
5
|
+
export const DIRECTORY_LIMIT = 5000;
|
|
6
|
+
const SNAPSHOT_BYTES = 2 * 1024 * 1024;
|
|
7
|
+
const CURSOR_TTL = 5 * 60 * 1000;
|
|
8
|
+
const KINDS = new Set(['direct', 'group', 'channel', 'thread']);
|
|
9
|
+
|
|
10
|
+
export interface ChannelEntry {
|
|
11
|
+
id: string;
|
|
12
|
+
line: string;
|
|
13
|
+
name?: string;
|
|
14
|
+
kind: 'direct' | 'group' | 'channel' | 'thread';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ChannelCapability {
|
|
18
|
+
supported: boolean;
|
|
19
|
+
complete: boolean;
|
|
20
|
+
source: 'remote' | 'local' | 'mixed' | 'unsupported';
|
|
21
|
+
reason?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ChannelSnapshot {
|
|
25
|
+
channels: ChannelEntry[];
|
|
26
|
+
capability: ChannelCapability;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ChannelList extends ChannelSnapshot {
|
|
30
|
+
next_cursor?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ChannelOptions {
|
|
34
|
+
query: string;
|
|
35
|
+
limit: number;
|
|
36
|
+
cursor?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function cursorOf(cursor: unknown): string | undefined {
|
|
40
|
+
if (cursor === undefined) return undefined;
|
|
41
|
+
if (typeof cursor !== 'string' || !cursor || cursor.length > 2048)
|
|
42
|
+
throw new TrainError('bad_request', 'cursor must be a nonempty string of at most 2048 characters');
|
|
43
|
+
return cursor;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function channelOptions(args: Record<string, unknown>): ChannelOptions {
|
|
47
|
+
const { query = '', limit = 50 } = args;
|
|
48
|
+
if (typeof query !== 'string' || query.length > 200)
|
|
49
|
+
throw new TrainError('bad_request', 'query must be a string of at most 200 characters');
|
|
50
|
+
if (typeof limit !== 'number' || !Number.isInteger(limit) || limit < 1 || limit > 100)
|
|
51
|
+
throw new TrainError('bad_request', 'limit must be an integer from 1 to 100');
|
|
52
|
+
const cursor = cursorOf(args.cursor);
|
|
53
|
+
return { query: query.trim().toLowerCase(), limit, ...(cursor === undefined ? {} : { cursor }) };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function channelMatches(entry: ChannelEntry, query: string): boolean {
|
|
57
|
+
return [entry.id, entry.line, entry.name ?? ''].some((value) => value.toLowerCase().includes(query));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function channelEntry(entry: ChannelEntry, account: string): ChannelEntry | undefined {
|
|
61
|
+
if (!entry.id || entry.id.length > 1024 || entry.line.length > 2048 || !KINDS.has(entry.kind))
|
|
62
|
+
return undefined;
|
|
63
|
+
const parsed = Line.parse(entry.line);
|
|
64
|
+
if (parsed?.path[0] !== account || parsed.path.length < 2) return undefined;
|
|
65
|
+
return {
|
|
66
|
+
id: entry.id,
|
|
67
|
+
line: entry.line,
|
|
68
|
+
kind: entry.kind,
|
|
69
|
+
...(entry.name ? { name: Array.from(entry.name).slice(0, 256).join('') } : {}),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function limitedCapability(capability: ChannelCapability): ChannelCapability {
|
|
74
|
+
return {
|
|
75
|
+
...capability,
|
|
76
|
+
complete: false,
|
|
77
|
+
reason: [capability.reason, 'Directory metadata exceeded the local size or entry limit; results are partial.']
|
|
78
|
+
.filter(Boolean).join(' '),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function bounded(snapshot: ChannelSnapshot, account: string, query: string): ChannelSnapshot {
|
|
83
|
+
const seen = new Set<string>();
|
|
84
|
+
const channels: ChannelEntry[] = [];
|
|
85
|
+
let bytes = 0;
|
|
86
|
+
let clipped = snapshot.channels.length > DIRECTORY_LIMIT;
|
|
87
|
+
for (const raw of snapshot.channels.slice(0, DIRECTORY_LIMIT)) {
|
|
88
|
+
const entry = channelEntry(raw, account);
|
|
89
|
+
if (!entry) {
|
|
90
|
+
clipped = true;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (seen.has(entry.line)) continue;
|
|
94
|
+
bytes += Buffer.byteLength(JSON.stringify(entry));
|
|
95
|
+
if (bytes > SNAPSHOT_BYTES) {
|
|
96
|
+
clipped = true;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
seen.add(entry.line);
|
|
100
|
+
if (channelMatches(raw, query)) channels.push(entry);
|
|
101
|
+
}
|
|
102
|
+
channels.sort((a, b) => a.line < b.line ? -1 : Number(a.line > b.line));
|
|
103
|
+
return { channels, capability: clipped ? limitedCapability(snapshot.capability) : { ...snapshot.capability } };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
interface Cursor {
|
|
107
|
+
account: string;
|
|
108
|
+
query: string;
|
|
109
|
+
snapshot: ChannelSnapshot;
|
|
110
|
+
offset: number;
|
|
111
|
+
expiresAt: number;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export class ChannelDirectory {
|
|
115
|
+
private readonly cursors = ticketStore<Cursor>(CURSOR_TTL, 32);
|
|
116
|
+
|
|
117
|
+
async list(
|
|
118
|
+
account: string,
|
|
119
|
+
args: Record<string, unknown>,
|
|
120
|
+
load: () => Promise<ChannelSnapshot>,
|
|
121
|
+
): Promise<ChannelList> {
|
|
122
|
+
const options = channelOptions(args);
|
|
123
|
+
const cursor = options.cursor === undefined
|
|
124
|
+
? {
|
|
125
|
+
account,
|
|
126
|
+
query: options.query,
|
|
127
|
+
snapshot: bounded(await load(), account, options.query),
|
|
128
|
+
offset: 0,
|
|
129
|
+
expiresAt: Date.now() + CURSOR_TTL,
|
|
130
|
+
}
|
|
131
|
+
: this.resume(options.cursor, account, options.query);
|
|
132
|
+
return this.page(cursor, options.limit);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private resume(ticket: string, account: string, query: string): Cursor {
|
|
136
|
+
const cursor = this.cursors.peek(ticket);
|
|
137
|
+
if (!cursor || cursor.account !== account || cursor.query !== query || cursor.expiresAt <= Date.now())
|
|
138
|
+
throw new TrainError('channel_cursor', 'Invalid or expired channel cursor; start a new listing for this account and query.');
|
|
139
|
+
return cursor;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private page(cursor: Cursor, limit: number): ChannelList {
|
|
143
|
+
const { snapshot, offset } = cursor;
|
|
144
|
+
const channels = snapshot.channels.slice(offset, offset + limit).map((entry) => ({ ...entry }));
|
|
145
|
+
const nextOffset = offset + channels.length;
|
|
146
|
+
if (nextOffset >= snapshot.channels.length)
|
|
147
|
+
return { channels, capability: { ...snapshot.capability } };
|
|
148
|
+
const { ticket } = this.cursors.mint({ ...cursor, offset: nextOffset });
|
|
149
|
+
return {
|
|
150
|
+
channels,
|
|
151
|
+
capability: {
|
|
152
|
+
...snapshot.capability,
|
|
153
|
+
complete: false,
|
|
154
|
+
reason: [snapshot.capability.reason, 'More channels are available; follow next_cursor.'].filter(Boolean).join(' '),
|
|
155
|
+
},
|
|
156
|
+
next_cursor: ticket,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { ticketStore } from '../tickets.js';
|
|
2
|
+
import { TrainError } from '../train-error.js';
|
|
3
|
+
import { channelEntry, channelMatches, channelOptions, DIRECTORY_LIMIT, type ChannelEntry, type ChannelList } from './channel-directory.js';
|
|
4
|
+
|
|
5
|
+
const TTL = 5 * 60 * 1000;
|
|
6
|
+
const MAX_BYTES = 2 * 1024 * 1024;
|
|
7
|
+
const CAP_REASON = 'Stopped after 5000 metadata records or pages; search does not cover the whole mailbox.';
|
|
8
|
+
const SIZE_REASON = 'Directory metadata exceeded the local size or entry limit; search is partial.';
|
|
9
|
+
|
|
10
|
+
export interface ChannelPage {
|
|
11
|
+
channels: ChannelEntry[];
|
|
12
|
+
scanned: number;
|
|
13
|
+
next?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type ChannelPageLoader = (token: string | undefined, limit: number, seen: ReadonlySet<string>) => Promise<ChannelPage>;
|
|
17
|
+
|
|
18
|
+
interface Scan {
|
|
19
|
+
next?: string;
|
|
20
|
+
channels: ChannelEntry[];
|
|
21
|
+
seen: Set<string>;
|
|
22
|
+
visited: Set<string>;
|
|
23
|
+
scanned: number;
|
|
24
|
+
bytes: number;
|
|
25
|
+
complete: boolean;
|
|
26
|
+
stopped?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface Cursor {
|
|
30
|
+
account: string;
|
|
31
|
+
query: string;
|
|
32
|
+
scope: string;
|
|
33
|
+
expiresAt: number;
|
|
34
|
+
scan: Scan;
|
|
35
|
+
loaded?: Promise<Scan>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function addEntries(scan: Scan, rows: ChannelEntry[], account: string, query: string): void {
|
|
39
|
+
for (const raw of rows) {
|
|
40
|
+
const entry = channelEntry(raw, account);
|
|
41
|
+
if (!entry) {
|
|
42
|
+
scan.stopped = SIZE_REASON;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (scan.seen.has(entry.id)) continue;
|
|
46
|
+
const size = Buffer.byteLength(JSON.stringify(entry)) + Buffer.byteLength(JSON.stringify(entry.id));
|
|
47
|
+
if (scan.bytes + size > MAX_BYTES) {
|
|
48
|
+
scan.stopped = SIZE_REASON;
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
scan.bytes += size;
|
|
52
|
+
scan.seen.add(entry.id);
|
|
53
|
+
if (channelMatches(raw, query)) scan.channels.push(entry);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function project(cursor: Cursor, page: ChannelPage): Scan {
|
|
58
|
+
const scan: Scan = {
|
|
59
|
+
next: page.next, channels: [], seen: new Set(cursor.scan.seen),
|
|
60
|
+
visited: new Set(cursor.scan.visited).add(cursor.scan.next ?? ''),
|
|
61
|
+
scanned: cursor.scan.scanned + page.scanned,
|
|
62
|
+
bytes: cursor.scan.bytes + Buffer.byteLength(page.next ?? ''), complete: !page.next,
|
|
63
|
+
};
|
|
64
|
+
if (page.next && scan.visited.has(page.next)) throw new TrainError('channel_page', 'The mailbox repeated a channel page token.');
|
|
65
|
+
addEntries(scan, page.channels, cursor.account, cursor.query);
|
|
66
|
+
if (scan.bytes > MAX_BYTES) scan.stopped = SIZE_REASON;
|
|
67
|
+
if (page.next && Math.max(scan.scanned, scan.visited.size) >= DIRECTORY_LIMIT) scan.stopped ??= CAP_REASON;
|
|
68
|
+
if (scan.stopped) {
|
|
69
|
+
scan.next = undefined;
|
|
70
|
+
scan.complete = false;
|
|
71
|
+
}
|
|
72
|
+
return scan;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export class ChannelPages {
|
|
76
|
+
private readonly cursors = ticketStore<Cursor>(TTL, 32);
|
|
77
|
+
|
|
78
|
+
async list(account: string, args: Record<string, unknown>, load: ChannelPageLoader, scope: string): Promise<ChannelList> {
|
|
79
|
+
const { query, limit, cursor: ticket } = channelOptions(args);
|
|
80
|
+
const cursor: Cursor = ticket === undefined
|
|
81
|
+
? {
|
|
82
|
+
account, query, scope, expiresAt: Date.now() + TTL,
|
|
83
|
+
scan: { channels: [], seen: new Set(), visited: new Set(), scanned: 0, bytes: Buffer.byteLength(account + query + scope), complete: false },
|
|
84
|
+
}
|
|
85
|
+
: this.resume(ticket, account, query);
|
|
86
|
+
const scan = await this.load(cursor, load);
|
|
87
|
+
if (cursor.expiresAt <= Date.now()) throw new TrainError('channel_cursor', 'Channel listing expired; start a new listing.');
|
|
88
|
+
const channels = scan.channels.slice(0, limit);
|
|
89
|
+
const remaining = scan.channels.slice(channels.length);
|
|
90
|
+
const more = remaining.length > 0 || (!scan.complete && !scan.stopped);
|
|
91
|
+
const next = more ? this.cursors.mint({ ...cursor, scan: { ...scan, channels: remaining }, loaded: undefined }).ticket : undefined;
|
|
92
|
+
return {
|
|
93
|
+
channels: channels.map((entry) => ({ ...entry })),
|
|
94
|
+
capability: {
|
|
95
|
+
supported: true, complete: scan.complete && !more, source: 'remote',
|
|
96
|
+
reason: [cursor.scope, scan.stopped, more ? 'More metadata remains; follow next_cursor, even after an empty page.' : ''].filter(Boolean).join(' '),
|
|
97
|
+
},
|
|
98
|
+
...(next === undefined ? {} : { next_cursor: next }),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private resume(ticket: string, account: string, query: string): Cursor {
|
|
103
|
+
const cursor = this.cursors.peek(ticket);
|
|
104
|
+
if (!cursor || cursor.account !== account || cursor.query !== query || cursor.expiresAt <= Date.now())
|
|
105
|
+
throw new TrainError('channel_cursor', 'Invalid or expired channel cursor; start a new listing for this account and query.');
|
|
106
|
+
return cursor;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private async load(cursor: Cursor, load: ChannelPageLoader): Promise<Scan> {
|
|
110
|
+
if (cursor.scan.channels.length || cursor.scan.complete || cursor.scan.stopped) return cursor.scan;
|
|
111
|
+
cursor.loaded ??= this.fetch(cursor, load);
|
|
112
|
+
try {
|
|
113
|
+
return await cursor.loaded;
|
|
114
|
+
} catch (err) {
|
|
115
|
+
cursor.loaded = undefined;
|
|
116
|
+
throw err;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private async fetch(cursor: Cursor, load: ChannelPageLoader): Promise<Scan> {
|
|
121
|
+
const count = Math.min(100, DIRECTORY_LIMIT - cursor.scan.scanned);
|
|
122
|
+
const page = await load(cursor.scan.next, count, new Set(cursor.scan.seen));
|
|
123
|
+
if (!Number.isInteger(page.scanned) || page.scanned < 0 || page.scanned > count || page.channels.length > page.scanned)
|
|
124
|
+
throw new TrainError('channel_page', 'The mailbox returned more channel metadata than requested.');
|
|
125
|
+
if (page.next !== undefined && (typeof page.next !== 'string' || !page.next || page.next.length > 16384))
|
|
126
|
+
throw new TrainError('channel_page', 'The mailbox returned an invalid channel page token.');
|
|
127
|
+
return project(cursor, page);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -110,6 +110,7 @@ export interface Station {
|
|
|
110
110
|
profileFields?: ReadonlySet<ProfileField>;
|
|
111
111
|
claimsName?: boolean;
|
|
112
112
|
readsProfiles?: boolean;
|
|
113
|
+
discoversChannels?: boolean;
|
|
113
114
|
readFilters?: ReadonlySet<ReadFilter>;
|
|
114
115
|
approvals?: boolean;
|
|
115
116
|
sendsFrames?: boolean;
|
|
@@ -20,6 +20,11 @@ export class ActivityTasks {
|
|
|
20
20
|
return [...this.tasks.values()].sort((a, b) => Number(live(b)) - Number(live(a)) || b.updatedAt - a.updatedAt).slice(0, 30).map((task) => ({ ...task }));
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
restore(tasks: RunnerTask[], parents: [string, string][]): void {
|
|
24
|
+
for (const task of tasks) this.tasks.set(task.id, task);
|
|
25
|
+
for (const [parent, id] of parents) this.parents.set(parent, id);
|
|
26
|
+
}
|
|
27
|
+
|
|
23
28
|
get running(): number { return [...this.tasks.values()].filter(live).length; }
|
|
24
29
|
taskFor(parent: string | null | undefined): string | null { return parent ? this.parents.get(parent) ?? null : null; }
|
|
25
30
|
known(id: string | null | undefined): string | null { return id && this.tasks.has(id) ? id : null; }
|
|
@@ -158,7 +163,8 @@ export class ActivityTasks {
|
|
|
158
163
|
task.durationMs = message.usage.duration_ms;
|
|
159
164
|
task.toolUses = message.usage.tool_uses;
|
|
160
165
|
}
|
|
161
|
-
this.status(task, message.status);
|
|
166
|
+
this.status(task, message.reason === 'worker_restart' ? 'unknown' : message.status);
|
|
167
|
+
if (message.reason === 'worker_restart') this.emit('task_failed', null, task.id);
|
|
162
168
|
}
|
|
163
169
|
|
|
164
170
|
private replace(message: Extract<System, { subtype: 'background_tasks_changed' }>): void {
|