@stage-labs/metro 0.1.0-beta.9 → 0.1.0-beta.90
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/api.js +14 -0
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +129 -0
- package/dist/cli.js +101 -0
- package/dist/control.js +74 -0
- package/dist/hold.js +211 -0
- package/dist/local.js +93 -0
- package/dist/node-name.js +71 -0
- package/dist/plugin.js +80 -0
- package/dist/provider-flags.js +9 -0
- package/dist/runtime-install.js +87 -0
- package/dist/runtime.js +74 -0
- package/dist/serve.js +150 -0
- package/dist/service.js +242 -0
- package/dist/tail.js +119 -0
- package/dist/update.js +45 -0
- package/dist/version.js +97 -0
- package/package.json +14 -25
- package/runtime/node_modules/@metro-labs/core/package.json +43 -0
- package/runtime/node_modules/@metro-labs/core/src/address.ts +6 -0
- package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/events.ts +120 -0
- package/runtime/node_modules/@metro-labs/core/src/identity.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/core/src/lines.ts +82 -0
- package/runtime/node_modules/@metro-labs/core/src/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/protocol.ts +226 -0
- package/runtime/node_modules/@metro-labs/core/src/secure-fs.ts +57 -0
- package/runtime/node_modules/@metro-labs/core/src/station-names.ts +12 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-store.ts +60 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/attachments.ts +196 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +88 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/station-runtime.ts +64 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/types.ts +107 -0
- package/runtime/node_modules/@metro-labs/core/src/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/core/src/train-error.ts +53 -0
- package/runtime/node_modules/@metro-labs/core/src/version.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/package.json +47 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-attach.ts +19 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +368 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/allowlist.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +302 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +111 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +345 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +155 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/keys.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/map.ts +88 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/senders.ts +38 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +193 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/crash-guard.ts +49 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +14 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/paths.ts +106 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/addressed.ts +25 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/dedupe.ts +24 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/inbound.ts +377 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/media-note.ts +85 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/pending.ts +75 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/relay.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/webhook-note.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +176 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +329 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +167 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +117 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/skills.ts +154 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/api.ts +307 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/cli-api.ts +63 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/config.ts +144 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/json.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/model.ts +69 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-client.ts +159 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-discovery.ts +117 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-pending.ts +65 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-routes.ts +157 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth.ts +127 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +97 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +295 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +277 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +98 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/url.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/verify.ts +330 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/watch.ts +54 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-grant.ts +99 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-owner.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-serve.ts +121 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-api.ts +294 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-store.ts +181 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/bedrock.ts +252 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +231 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-device.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-stream.ts +288 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +203 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +220 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/eventstream.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/forward.ts +182 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +195 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +198 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +40 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +17 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/accounts.ts +139 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/call-tools.ts +216 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/channel-owner.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/ctx.ts +62 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/event-store.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/frame-scope.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/group-tools.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/index.ts +191 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/member-tools.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/raw-get-stream.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/request-identity.ts +47 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-registry.ts +172 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-route.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session.ts +215 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-dispatch.ts +215 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-schemas.ts +400 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/upload-tool.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +237 -0
- package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +389 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/http.ts +390 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/identity.ts +54 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +198 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/control.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/machine.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/update.ts +116 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-inline.ts +100 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-interactive.ts +161 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-resolve.ts +202 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-session.ts +280 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-xmtp.ts +111 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach.ts +184 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +248 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/registry.ts +35 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/runtime-deps.ts +82 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor-io.ts +97 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor.ts +320 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/train-call.ts +22 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +40 -0
- package/runtime/node_modules/@metro-labs/discord-bot/package.json +28 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +118 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +388 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/api-base.ts +1 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/attachments.ts +29 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +220 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group-actions.ts +50 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group.ts +115 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/index.ts +127 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +19 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-speak.ts +195 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-transcribe.ts +292 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice.ts +197 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/http/package.json +33 -0
- package/runtime/node_modules/@metro-labs/http/src/api-error.ts +8 -0
- package/runtime/node_modules/@metro-labs/http/src/api-http.ts +95 -0
- package/runtime/node_modules/@metro-labs/http/src/cors.ts +29 -0
- package/runtime/node_modules/@metro-labs/http/src/identity-key.ts +33 -0
- package/runtime/node_modules/@metro-labs/http/src/identity-registry.ts +13 -0
- package/runtime/node_modules/@metro-labs/http/src/mode-api.ts +32 -0
- package/runtime/node_modules/@metro-labs/http/src/return-to.ts +21 -0
- package/runtime/node_modules/@metro-labs/http/src/signed-identity.ts +36 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +26 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +254 -0
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +87 -0
- package/runtime/node_modules/@metro-labs/telegram/src/client.ts +77 -0
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +156 -0
- package/runtime/node_modules/@metro-labs/telegram/src/group.ts +261 -0
- package/runtime/node_modules/@metro-labs/telegram/src/history.ts +52 -0
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +78 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +46 -0
- package/runtime/node_modules/@metro-labs/telegram/src/login.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +75 -0
- package/runtime/node_modules/@metro-labs/telegram/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +24 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +7 -0
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +120 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +295 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +201 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +109 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +58 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +3 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +37 -0
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +27 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +66 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +178 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +72 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +129 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +327 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +138 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/outbox.ts +38 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +185 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +98 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +6 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/xmtp/package.json +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +112 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +110 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +299 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +87 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +375 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +107 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +171 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +239 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +55 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +335 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +80 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +125 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +349 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +13 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +126 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +212 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +90 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +84 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +53 -0
- package/runtime/runtime.json +3 -0
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +36 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -249
- package/dist/cache.js +0 -75
- package/dist/cli/actions.js +0 -139
- package/dist/cli/config.js +0 -185
- package/dist/cli/index.js +0 -177
- package/dist/cli/skill.js +0 -62
- package/dist/cli/util.js +0 -72
- package/dist/cli/webhook.js +0 -83
- package/dist/codex-rc.js +0 -245
- package/dist/dispatcher.js +0 -122
- package/dist/history.js +0 -99
- package/dist/ipc.js +0 -72
- package/dist/log.js +0 -15
- package/dist/paths.js +0 -68
- package/dist/registry.js +0 -48
- package/dist/stations/claude.js +0 -45
- package/dist/stations/codex.js +0 -68
- package/dist/stations/discord.js +0 -200
- package/dist/stations/index.js +0 -133
- package/dist/stations/telegram-md.js +0 -34
- package/dist/stations/telegram-upload.js +0 -113
- package/dist/stations/telegram.js +0 -213
- package/dist/stations/webhook.js +0 -100
- package/dist/tunnel.js +0 -66
- package/dist/webhooks.js +0 -40
- package/docs/agents.md +0 -216
- package/docs/uri-scheme.md +0 -125
- package/skills/metro/SKILL.md +0 -249
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const MAX_BODY_CHARS = 8 * 1024;
|
|
2
|
+
|
|
3
|
+
const HEADERS_KEPT = [
|
|
4
|
+
'x-github-event',
|
|
5
|
+
'x-github-delivery',
|
|
6
|
+
'x-intercom-topic',
|
|
7
|
+
'x-request-id',
|
|
8
|
+
'user-agent',
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
interface WebhookPayload {
|
|
12
|
+
headers?: Record<string, string>;
|
|
13
|
+
body?: unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function renderBody(body: unknown): string {
|
|
17
|
+
if (body === undefined || body === null) return '';
|
|
18
|
+
const text =
|
|
19
|
+
typeof body === 'string' ? body : JSON.stringify(body, null, 2);
|
|
20
|
+
if (text.length <= MAX_BODY_CHARS) return text;
|
|
21
|
+
return (
|
|
22
|
+
text.slice(0, MAX_BODY_CHARS) +
|
|
23
|
+
`\n[truncated: ${String(text.length - MAX_BODY_CHARS)} more characters]`
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function renderHeaders(headers: Record<string, string> | undefined): string {
|
|
28
|
+
if (!headers) return '';
|
|
29
|
+
const kept = HEADERS_KEPT.flatMap((k) =>
|
|
30
|
+
headers[k] === undefined ? [] : [`${k}: ${headers[k]}`],
|
|
31
|
+
);
|
|
32
|
+
return kept.length === 0 ? '' : kept.join('\n') + '\n';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function buildWebhookNote(
|
|
36
|
+
summary: string,
|
|
37
|
+
lineName: string | undefined,
|
|
38
|
+
payload: unknown,
|
|
39
|
+
): string {
|
|
40
|
+
const p = (
|
|
41
|
+
payload !== null && typeof payload === 'object' ? payload : {}
|
|
42
|
+
) as WebhookPayload;
|
|
43
|
+
const label = lineName ? `${lineName}: ` : '';
|
|
44
|
+
const body = renderBody(p.body);
|
|
45
|
+
return (
|
|
46
|
+
`[webhook received] ${label}${summary}\n` +
|
|
47
|
+
renderHeaders(p.headers) +
|
|
48
|
+
(body ? `\n${body}\n` : '') +
|
|
49
|
+
'Inbound only: this line takes no send, reply or react.'
|
|
50
|
+
);
|
|
51
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
3
|
+
import { apiFailure, apiSession, cors, readJsonBody, sendJson } from '@metro-labs/http/api-http';
|
|
4
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
5
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
6
|
+
import { listClaudeSettings, SETTINGS_MAX, writeClaudeSettings } from './settings.js';
|
|
7
|
+
import {
|
|
8
|
+
createClaudeSkill,
|
|
9
|
+
deleteClaudeSkill,
|
|
10
|
+
listClaudeSkills,
|
|
11
|
+
readClaudeSkill,
|
|
12
|
+
skillHomes,
|
|
13
|
+
writeClaudeSkill,
|
|
14
|
+
} from './skills.js';
|
|
15
|
+
import {
|
|
16
|
+
answerClaudeLogin,
|
|
17
|
+
claudeAccount,
|
|
18
|
+
claudeInstalled,
|
|
19
|
+
claudeLoginView,
|
|
20
|
+
endClaudeLogin,
|
|
21
|
+
startClaudeLogin,
|
|
22
|
+
type LoginDeps,
|
|
23
|
+
} from './login.js';
|
|
24
|
+
import {
|
|
25
|
+
claudeDir,
|
|
26
|
+
deleteClaudeSession,
|
|
27
|
+
listClaudeProjects,
|
|
28
|
+
listClaudeSessions,
|
|
29
|
+
listMemory,
|
|
30
|
+
readMemoryFile,
|
|
31
|
+
readTranscript,
|
|
32
|
+
} from './files.js';
|
|
33
|
+
|
|
34
|
+
const PREFIX = '/api/claude';
|
|
35
|
+
const BODY_MAX = SETTINGS_MAX + 4096;
|
|
36
|
+
const WRITABLE = new Set(['GET', 'DELETE', 'PUT', 'POST']);
|
|
37
|
+
const PAGE = 100;
|
|
38
|
+
const PAGE_MAX = 500;
|
|
39
|
+
|
|
40
|
+
export interface ClaudeApiDeps {
|
|
41
|
+
authorize: (subject: string) => void;
|
|
42
|
+
dir?: () => string;
|
|
43
|
+
login?: LoginDeps;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function projectOf(query: URLSearchParams): string {
|
|
47
|
+
const project = query.get('project');
|
|
48
|
+
if (project === null || project === '') throw new ApiError('project is required', 400);
|
|
49
|
+
return project;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function pageOf(query: URLSearchParams): { offset: number; limit: number } {
|
|
53
|
+
const offset = Math.max(0, Math.floor(Number(query.get('offset') ?? '0')) || 0);
|
|
54
|
+
const limit = Math.min(PAGE_MAX, Math.max(1, Math.floor(Number(query.get('limit') ?? String(PAGE))) || PAGE));
|
|
55
|
+
return { offset, limit };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type Handler = (query: URLSearchParams, dir: string, item: string) => unknown;
|
|
59
|
+
|
|
60
|
+
const COLLECTIONS: Record<string, Handler> = {
|
|
61
|
+
projects: (_query, dir) => ({ projects: listClaudeProjects(dir) }),
|
|
62
|
+
sessions: (query, dir) => ({ sessions: listClaudeSessions(projectOf(query), dir) }),
|
|
63
|
+
memory: (query, dir) => listMemory(projectOf(query), dir),
|
|
64
|
+
settings: (_query, dir) => ({ files: listClaudeSettings(dir) }),
|
|
65
|
+
skills: (_query, dir) => ({
|
|
66
|
+
skills: listClaudeSkills(dir),
|
|
67
|
+
places: skillHomes(dir).map((home) => ({ id: home.prefix, scope: home.scope, where: home.where })),
|
|
68
|
+
}),
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const ITEMS: Record<string, Handler> = {
|
|
72
|
+
sessions: (query, dir, id) => {
|
|
73
|
+
const { offset, limit } = pageOf(query);
|
|
74
|
+
return readTranscript(projectOf(query), id, offset, limit, dir);
|
|
75
|
+
},
|
|
76
|
+
memory: (query, dir, name) => ({ name, content: readMemoryFile(projectOf(query), name, dir) }),
|
|
77
|
+
skills: (_query, dir, id) => readClaudeSkill(decodeURIComponent(id), dir),
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const parts = (path: string): string[] => path.slice(PREFIX.length + 1).split('/').filter(Boolean);
|
|
81
|
+
|
|
82
|
+
const seenIn = (body: Record<string, unknown>): string | null | undefined =>
|
|
83
|
+
'seenAt' in body ? (typeof body.seenAt === 'string' ? body.seenAt : null) : undefined;
|
|
84
|
+
|
|
85
|
+
async function writeAnswer(req: IncomingMessage, path: string, dir: string): Promise<unknown> {
|
|
86
|
+
const [head = '', item = ''] = parts(path);
|
|
87
|
+
if ((head !== 'settings' && head !== 'skills') || item === '') throw new ApiError('method not allowed', 405);
|
|
88
|
+
const body = await readJsonBody(req, BODY_MAX);
|
|
89
|
+
if (!isRecord(body) || typeof body.text !== 'string') throw new ApiError('text is required', 400);
|
|
90
|
+
if (head === 'skills') return writeClaudeSkill(decodeURIComponent(item), body.text, seenIn(body), dir);
|
|
91
|
+
return writeClaudeSettings(item, body.text, seenIn(body), dir);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function created(req: IncomingMessage, path: string, dir: string): Promise<unknown> {
|
|
95
|
+
const rest = parts(path);
|
|
96
|
+
if (rest.length !== 1 || rest[0] !== 'skills') throw new ApiError('method not allowed', 405);
|
|
97
|
+
const body = await readJsonBody(req, BODY_MAX);
|
|
98
|
+
if (!isRecord(body) || typeof body.name !== 'string') throw new ApiError('name is required', 400);
|
|
99
|
+
const scope = typeof body.scope === 'string' ? body.scope : undefined;
|
|
100
|
+
return createClaudeSkill(body.name, scope, typeof body.text === 'string' ? body.text : undefined, dir);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const LOGIN = 'login';
|
|
104
|
+
|
|
105
|
+
async function loginAnswer(req: IncomingMessage, id: string, deps: ClaudeApiDeps): Promise<unknown> {
|
|
106
|
+
const method = req.method ?? 'GET';
|
|
107
|
+
if (id === '') {
|
|
108
|
+
if (method === 'POST') return startClaudeLogin(deps.login);
|
|
109
|
+
if (method === 'GET') return { available: claudeInstalled(), ...claudeAccount() };
|
|
110
|
+
throw new ApiError('method not allowed', 405);
|
|
111
|
+
}
|
|
112
|
+
if (method === 'GET') return claudeLoginView(id);
|
|
113
|
+
if (method === 'DELETE') return endClaudeLogin(id);
|
|
114
|
+
if (method !== 'POST') throw new ApiError('method not allowed', 405);
|
|
115
|
+
const body = await readJsonBody(req);
|
|
116
|
+
if (!isRecord(body) || typeof body.text !== 'string') throw new ApiError('text is required', 400);
|
|
117
|
+
return answerClaudeLogin(id, body.text);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function removed(rest: string[], query: URLSearchParams, dir: string): unknown {
|
|
121
|
+
const [head = '', item = ''] = rest;
|
|
122
|
+
if (rest.length !== 2) throw new ApiError('method not allowed', 405);
|
|
123
|
+
if (head === 'skills') return { deleted: deleteClaudeSkill(decodeURIComponent(item), dir) };
|
|
124
|
+
if (head !== 'sessions') throw new ApiError('method not allowed', 405);
|
|
125
|
+
deleteClaudeSession(projectOf(query), item, dir);
|
|
126
|
+
return { deleted: item };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function answer(method: string, path: string, query: URLSearchParams, dir: string): unknown {
|
|
130
|
+
const rest = parts(path);
|
|
131
|
+
const [head = '', item = ''] = rest;
|
|
132
|
+
if (method === 'DELETE') return removed(rest, query, dir);
|
|
133
|
+
if (method !== 'GET') throw new ApiError('method not allowed', 405);
|
|
134
|
+
const handler = rest.length === 1 ? COLLECTIONS[head] : rest.length === 2 ? ITEMS[head] : undefined;
|
|
135
|
+
if (handler === undefined) throw new ApiError('no such route', 404);
|
|
136
|
+
return handler(query, dir, item);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function handleClaudeRequest(
|
|
140
|
+
req: IncomingMessage,
|
|
141
|
+
res: ServerResponse,
|
|
142
|
+
deps: ClaudeApiDeps,
|
|
143
|
+
): boolean {
|
|
144
|
+
const [path = '', search = ''] = (req.url ?? '').split('?');
|
|
145
|
+
if (path !== PREFIX && !path.startsWith(`${PREFIX}/`)) return false;
|
|
146
|
+
if (req.method === 'OPTIONS') {
|
|
147
|
+
res.writeHead(204, cors(req)).end();
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
if (!WRITABLE.has(req.method ?? '')) {
|
|
151
|
+
sendJson(req, res, 405, { error: 'method not allowed' });
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
apiSession(req)
|
|
155
|
+
.then((session) => {
|
|
156
|
+
if (!session) throw new ApiError('unauthorized', 401);
|
|
157
|
+
deps.authorize(session.subject);
|
|
158
|
+
const dir = (deps.dir ?? claudeDir)();
|
|
159
|
+
const [head = '', item = ''] = parts(path);
|
|
160
|
+
if (head === LOGIN) return loginAnswer(req, item, deps);
|
|
161
|
+
if (req.method === 'PUT') return writeAnswer(req, path, dir);
|
|
162
|
+
if (req.method === 'POST') return created(req, path, dir);
|
|
163
|
+
return answer(req.method ?? 'GET', path, new URLSearchParams(search), dir);
|
|
164
|
+
})
|
|
165
|
+
.then((body) => {
|
|
166
|
+
sendJson(req, res, 200, body);
|
|
167
|
+
})
|
|
168
|
+
.catch((err: unknown) => {
|
|
169
|
+
if (err instanceof ApiError) apiFailure(req, res, err, 'claude-api');
|
|
170
|
+
else {
|
|
171
|
+
log.warn({ err: errMsg(err) }, 'claude-api: request failed');
|
|
172
|
+
if (!res.headersSent) sendJson(req, res, 500, { error: 'claude-api failed' });
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import {
|
|
2
|
+
closeSync,
|
|
3
|
+
createReadStream,
|
|
4
|
+
existsSync,
|
|
5
|
+
openSync,
|
|
6
|
+
readdirSync,
|
|
7
|
+
readSync,
|
|
8
|
+
rmSync,
|
|
9
|
+
statSync,
|
|
10
|
+
} from 'node:fs';
|
|
11
|
+
import { homedir } from 'node:os';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
import { createInterface } from 'node:readline';
|
|
14
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
15
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
16
|
+
|
|
17
|
+
const PROJECT_RE = /^[A-Za-z0-9._-]{1,200}$/;
|
|
18
|
+
const SESSION_RE = /^[A-Za-z0-9][A-Za-z0-9-]{7,63}$/;
|
|
19
|
+
const MEMORY_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,119}\.md$/;
|
|
20
|
+
const EDGE_BYTES = 64 * 1024;
|
|
21
|
+
const TEXT_CAP = 20_000;
|
|
22
|
+
const RESULT_CAP = 8_000;
|
|
23
|
+
const INPUT_CAP = 4_000;
|
|
24
|
+
const FILE_CAP = 1_000_000;
|
|
25
|
+
const NOISE = ['<task-notification>', '<system-reminder>', '<local-command', '<command-name>'];
|
|
26
|
+
|
|
27
|
+
export function claudeDir(): string {
|
|
28
|
+
const explicit = process.env.METRO_CLAUDE_DIR?.trim();
|
|
29
|
+
if (explicit !== undefined && explicit !== '') return explicit;
|
|
30
|
+
const cfg = process.env.CLAUDE_CONFIG_DIR?.trim();
|
|
31
|
+
if (cfg !== undefined && cfg !== '') return cfg;
|
|
32
|
+
return join(homedir(), '.claude');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const str = (v: unknown): string | null => (typeof v === 'string' && v !== '' ? v : null);
|
|
36
|
+
|
|
37
|
+
function safeName(value: string, re: RegExp, what: string): string {
|
|
38
|
+
if (!re.test(value) || value === '.' || value === '..')
|
|
39
|
+
throw new ApiError(`that is not a ${what}`, 400);
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function projectDir(project: string, dir: string): string {
|
|
44
|
+
const path = join(dir, 'projects', safeName(project, PROJECT_RE, 'Claude project id'));
|
|
45
|
+
if (!existsSync(path)) throw new ApiError('no such Claude project', 404);
|
|
46
|
+
return path;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function parseLines(text: string): Record<string, unknown>[] {
|
|
50
|
+
const out: Record<string, unknown>[] = [];
|
|
51
|
+
for (const line of text.split('\n')) {
|
|
52
|
+
if (line.trim() === '') continue;
|
|
53
|
+
try {
|
|
54
|
+
const parsed: unknown = JSON.parse(line);
|
|
55
|
+
if (isRecord(parsed)) out.push(parsed);
|
|
56
|
+
} catch {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function readEdge(path: string, fromEnd: boolean): Record<string, unknown>[] {
|
|
64
|
+
const size = statSync(path).size;
|
|
65
|
+
const length = Math.min(EDGE_BYTES, size);
|
|
66
|
+
const fd = openSync(path, 'r');
|
|
67
|
+
try {
|
|
68
|
+
const buffer = Buffer.alloc(length);
|
|
69
|
+
readSync(fd, buffer, 0, length, fromEnd ? size - length : 0);
|
|
70
|
+
const text = buffer.toString('utf8');
|
|
71
|
+
const cut = fromEnd && length < size ? text.indexOf('\n') + 1 : 0;
|
|
72
|
+
return parseLines(text.slice(cut));
|
|
73
|
+
} finally {
|
|
74
|
+
closeSync(fd);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface ClaudeProject {
|
|
79
|
+
id: string;
|
|
80
|
+
cwd: string | null;
|
|
81
|
+
sessions: number;
|
|
82
|
+
lastActiveAt: string | null;
|
|
83
|
+
hasMemory: boolean;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function sessionFiles(path: string): { name: string; mtime: number; size: number }[] {
|
|
87
|
+
return readdirSync(path)
|
|
88
|
+
.filter((name) => name.endsWith('.jsonl'))
|
|
89
|
+
.map((name) => {
|
|
90
|
+
const s = statSync(join(path, name));
|
|
91
|
+
return { name, mtime: s.mtimeMs, size: s.size };
|
|
92
|
+
})
|
|
93
|
+
.sort((a, b) => b.mtime - a.mtime);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function cwdOf(path: string): string | null {
|
|
97
|
+
for (const line of readEdge(path, false)) {
|
|
98
|
+
const cwd = str(line.cwd);
|
|
99
|
+
if (cwd !== null) return cwd;
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function listClaudeProjects(dir = claudeDir()): ClaudeProject[] {
|
|
105
|
+
const root = join(dir, 'projects');
|
|
106
|
+
if (!existsSync(root)) return [];
|
|
107
|
+
return readdirSync(root, { withFileTypes: true })
|
|
108
|
+
.filter((entry) => entry.isDirectory() && PROJECT_RE.test(entry.name))
|
|
109
|
+
.map((entry) => {
|
|
110
|
+
const path = join(root, entry.name);
|
|
111
|
+
const files = sessionFiles(path);
|
|
112
|
+
const newest = files[0];
|
|
113
|
+
return {
|
|
114
|
+
id: entry.name,
|
|
115
|
+
cwd: newest === undefined ? null : cwdOf(join(path, newest.name)),
|
|
116
|
+
sessions: files.length,
|
|
117
|
+
lastActiveAt: newest === undefined ? null : new Date(newest.mtime).toISOString(),
|
|
118
|
+
hasMemory: existsSync(join(path, 'memory')),
|
|
119
|
+
};
|
|
120
|
+
})
|
|
121
|
+
.sort((a, b) => (b.lastActiveAt ?? '').localeCompare(a.lastActiveAt ?? ''));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface ClaudeSession {
|
|
125
|
+
id: string;
|
|
126
|
+
title: string;
|
|
127
|
+
startedAt: string | null;
|
|
128
|
+
lastAt: string;
|
|
129
|
+
bytes: number;
|
|
130
|
+
gitBranch: string | null;
|
|
131
|
+
version: string | null;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function firstPrompt(lines: Record<string, unknown>[]): string | null {
|
|
135
|
+
for (const line of lines) {
|
|
136
|
+
if (line.type !== 'user' || line.isSidechain === true) continue;
|
|
137
|
+
const message = isRecord(line.message) ? line.message : {};
|
|
138
|
+
const text = promptText(message.content);
|
|
139
|
+
if (text !== null && !NOISE.some((n) => text.startsWith(n))) return text.slice(0, 120);
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function promptText(content: unknown): string | null {
|
|
145
|
+
if (typeof content === 'string') return content.trim() === '' ? null : content.trim();
|
|
146
|
+
if (!Array.isArray(content)) return null;
|
|
147
|
+
for (const block of content)
|
|
148
|
+
if (isRecord(block) && block.type === 'text') {
|
|
149
|
+
const text = str(block.text);
|
|
150
|
+
if (text !== null) return text.trim();
|
|
151
|
+
}
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function sessionOf(path: string, id: string, mtime: number, size: number): ClaudeSession {
|
|
156
|
+
const head = readEdge(path, false);
|
|
157
|
+
const tail = readEdge(path, true);
|
|
158
|
+
const titled = [...tail].reverse().find((l) => l.type === 'ai-title' && str(l.aiTitle) !== null);
|
|
159
|
+
const meta = head.find((l) => l.type === 'user' || l.type === 'assistant') ?? {};
|
|
160
|
+
return {
|
|
161
|
+
id,
|
|
162
|
+
title: (titled === undefined ? null : str(titled.aiTitle)) ?? firstPrompt(head) ?? 'Untitled session',
|
|
163
|
+
startedAt: str(head.find((l) => str(l.timestamp) !== null)?.timestamp) ?? null,
|
|
164
|
+
lastAt: new Date(mtime).toISOString(),
|
|
165
|
+
bytes: size,
|
|
166
|
+
gitBranch: str(meta.gitBranch),
|
|
167
|
+
version: str(meta.version),
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function listClaudeSessions(project: string, dir = claudeDir()): ClaudeSession[] {
|
|
172
|
+
const path = projectDir(project, dir);
|
|
173
|
+
return sessionFiles(path).map((f) =>
|
|
174
|
+
sessionOf(join(path, f.name), f.name.slice(0, -'.jsonl'.length), f.mtime, f.size),
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export type Block =
|
|
179
|
+
| { kind: 'text'; text: string }
|
|
180
|
+
| { kind: 'tool_use'; name: string; input: string }
|
|
181
|
+
| { kind: 'tool_result'; text: string; isError: boolean }
|
|
182
|
+
| { kind: 'thinking' }
|
|
183
|
+
| { kind: 'image' };
|
|
184
|
+
|
|
185
|
+
export interface TranscriptEntry {
|
|
186
|
+
uuid: string;
|
|
187
|
+
at: string | null;
|
|
188
|
+
role: 'user' | 'assistant';
|
|
189
|
+
blocks: Block[];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export interface TranscriptPage {
|
|
193
|
+
entries: TranscriptEntry[];
|
|
194
|
+
total: number;
|
|
195
|
+
next: number | null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const cap = (text: string, max: number): string =>
|
|
199
|
+
text.length > max ? `${text.slice(0, max)}\n… (${String(text.length - max)} more characters)` : text;
|
|
200
|
+
|
|
201
|
+
function resultText(content: unknown): string {
|
|
202
|
+
if (typeof content === 'string') return content;
|
|
203
|
+
if (!Array.isArray(content)) return '';
|
|
204
|
+
return content
|
|
205
|
+
.map((b) => (isRecord(b) && b.type === 'text' ? (str(b.text) ?? '') : ''))
|
|
206
|
+
.filter((t) => t !== '')
|
|
207
|
+
.join('\n');
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function textBlock(raw: Record<string, unknown>): Block | null {
|
|
211
|
+
const text = str(raw.text);
|
|
212
|
+
if (text === null || NOISE.some((n) => text.startsWith(n))) return null;
|
|
213
|
+
return { kind: 'text', text: cap(text, TEXT_CAP) };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const BLOCKS: Record<string, (raw: Record<string, unknown>) => Block | null> = {
|
|
217
|
+
text: textBlock,
|
|
218
|
+
tool_use: (raw) => ({
|
|
219
|
+
kind: 'tool_use',
|
|
220
|
+
name: str(raw.name) ?? 'tool',
|
|
221
|
+
input: cap(JSON.stringify(raw.input ?? {}, null, 1), INPUT_CAP),
|
|
222
|
+
}),
|
|
223
|
+
tool_result: (raw) => ({
|
|
224
|
+
kind: 'tool_result',
|
|
225
|
+
text: cap(resultText(raw.content), RESULT_CAP),
|
|
226
|
+
isError: raw.is_error === true,
|
|
227
|
+
}),
|
|
228
|
+
thinking: () => ({ kind: 'thinking' }),
|
|
229
|
+
image: () => ({ kind: 'image' }),
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
function blockOf(raw: unknown): Block | null {
|
|
233
|
+
if (!isRecord(raw) || typeof raw.type !== 'string') return null;
|
|
234
|
+
const make = BLOCKS[raw.type];
|
|
235
|
+
return make === undefined ? null : make(raw);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function entryOf(line: Record<string, unknown>): TranscriptEntry | null {
|
|
239
|
+
if ((line.type !== 'user' && line.type !== 'assistant') || line.isSidechain === true) return null;
|
|
240
|
+
const message = isRecord(line.message) ? line.message : {};
|
|
241
|
+
const content = message.content;
|
|
242
|
+
const blocks =
|
|
243
|
+
typeof content === 'string'
|
|
244
|
+
? [blockOf({ type: 'text', text: content })]
|
|
245
|
+
: Array.isArray(content)
|
|
246
|
+
? content.map(blockOf)
|
|
247
|
+
: [];
|
|
248
|
+
const kept = blocks.filter((b): b is Block => b !== null);
|
|
249
|
+
if (kept.length === 0) return null;
|
|
250
|
+
return { uuid: str(line.uuid) ?? '', at: str(line.timestamp), role: line.type, blocks: kept };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export async function readTranscript(
|
|
254
|
+
project: string,
|
|
255
|
+
session: string,
|
|
256
|
+
offset: number,
|
|
257
|
+
limit: number,
|
|
258
|
+
dir = claudeDir(),
|
|
259
|
+
): Promise<TranscriptPage> {
|
|
260
|
+
const path = join(projectDir(project, dir), `${safeName(session, SESSION_RE, 'session id')}.jsonl`);
|
|
261
|
+
if (!existsSync(path)) throw new ApiError('no such session', 404);
|
|
262
|
+
const entries: TranscriptEntry[] = [];
|
|
263
|
+
let total = 0;
|
|
264
|
+
const lines = createInterface({ input: createReadStream(path, { encoding: 'utf8' }), crlfDelay: Infinity });
|
|
265
|
+
for await (const line of lines) {
|
|
266
|
+
const parsed = parseLines(line)[0];
|
|
267
|
+
const entry = parsed === undefined ? null : entryOf(parsed);
|
|
268
|
+
if (entry === null) continue;
|
|
269
|
+
if (total >= offset && entries.length < limit) entries.push(entry);
|
|
270
|
+
total += 1;
|
|
271
|
+
}
|
|
272
|
+
return { entries, total, next: offset + limit < total ? offset + limit : null };
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export function deleteClaudeSession(project: string, session: string, dir = claudeDir()): void {
|
|
276
|
+
const root = projectDir(project, dir);
|
|
277
|
+
const id = safeName(session, SESSION_RE, 'session id');
|
|
278
|
+
const file = join(root, `${id}.jsonl`);
|
|
279
|
+
if (!existsSync(file)) throw new ApiError('no such session', 404);
|
|
280
|
+
rmSync(file);
|
|
281
|
+
rmSync(join(root, id), { recursive: true, force: true });
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export interface MemoryFile {
|
|
285
|
+
name: string;
|
|
286
|
+
bytes: number;
|
|
287
|
+
modifiedAt: string;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export interface MemoryListing {
|
|
291
|
+
files: MemoryFile[];
|
|
292
|
+
index: string | null;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function memoryDir(project: string, dir: string): string {
|
|
296
|
+
return join(projectDir(project, dir), 'memory');
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function readCapped(path: string): string {
|
|
300
|
+
const size = statSync(path).size;
|
|
301
|
+
const fd = openSync(path, 'r');
|
|
302
|
+
try {
|
|
303
|
+
const buffer = Buffer.alloc(Math.min(size, FILE_CAP));
|
|
304
|
+
readSync(fd, buffer, 0, buffer.length, 0);
|
|
305
|
+
return size > FILE_CAP ? `${buffer.toString('utf8')}\n\n… truncated` : buffer.toString('utf8');
|
|
306
|
+
} finally {
|
|
307
|
+
closeSync(fd);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export function listMemory(project: string, dir = claudeDir()): MemoryListing {
|
|
312
|
+
const path = memoryDir(project, dir);
|
|
313
|
+
if (!existsSync(path)) return { files: [], index: null };
|
|
314
|
+
const files = readdirSync(path)
|
|
315
|
+
.filter((name) => MEMORY_RE.test(name) && name !== 'MEMORY.md')
|
|
316
|
+
.map((name) => {
|
|
317
|
+
const s = statSync(join(path, name));
|
|
318
|
+
return { name, bytes: s.size, modifiedAt: s.mtime.toISOString() };
|
|
319
|
+
})
|
|
320
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
321
|
+
const index = join(path, 'MEMORY.md');
|
|
322
|
+
return { files, index: existsSync(index) ? readCapped(index) : null };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export function readMemoryFile(project: string, name: string, dir = claudeDir()): string {
|
|
326
|
+
const path = join(memoryDir(project, dir), safeName(name, MEMORY_RE, 'memory file name'));
|
|
327
|
+
if (!existsSync(path)) throw new ApiError('no such memory file', 404);
|
|
328
|
+
return readCapped(path);
|
|
329
|
+
}
|