@agent-socket/sdk 0.1.0

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.
@@ -0,0 +1,210 @@
1
+ export interface Tool {
2
+ /** HTTP method. Defaults to "POST" if omitted. */
3
+ method?: string;
4
+ /**
5
+ * URL path including the leading slash. Identifier and routing target.
6
+ * In v0 must be static (no `:id` params).
7
+ */
8
+ path: string;
9
+ description: string;
10
+ /** Optional JSON Schema describing the request body. */
11
+ input_schema?: unknown;
12
+ /**
13
+ * Handler invoked when an agent calls this tool. Body is the agent's
14
+ * raw HTTP body as a string — typically JSON; the handler should parse.
15
+ */
16
+ handler: ToolHandler;
17
+ }
18
+ export interface ToolCallContext {
19
+ method: string;
20
+ path: string;
21
+ body: string;
22
+ headers: Record<string, string>;
23
+ }
24
+ export type ToolResult = {
25
+ status?: number;
26
+ body?: unknown;
27
+ taskId?: string;
28
+ /**
29
+ * Optional response headers. The relay only honors `content-type`
30
+ * in v0 (and only when `body` is a string — the relay sends the
31
+ * string verbatim with the declared content-type). Other headers
32
+ * are accepted-but-ignored; non-string bodies fall back to JSON.
33
+ *
34
+ * Use for tools that serve HTML, plain text, CSV, shell scripts,
35
+ * etc. — anything where the AI's HTTP client needs a specific
36
+ * content-type to render or parse correctly.
37
+ */
38
+ headers?: Record<string, string>;
39
+ } | unknown;
40
+ export type ToolHandler = (ctx: ToolCallContext) => Promise<ToolResult> | ToolResult;
41
+ export interface ConnectOptions {
42
+ /** Free-form app label, [A-Za-z0-9_.-]{1,64} (e.g. "as_app_anon"); shown in tools.json. */
43
+ appId: string;
44
+ /** Markdown briefing served at GET /v1/t/<token>/agents.md. */
45
+ agentsMd: string;
46
+ /** Optional 1–3 sentence app description, surfaced in tools.json. */
47
+ appDescription?: string;
48
+ tools: Tool[];
49
+ /**
50
+ * Base URL of the relay. Default: "https://agentsocket.dev".
51
+ * Override for self-hosted or local dev.
52
+ * Note: "https://aisocket.dev" is also served by the same Worker.
53
+ */
54
+ baseUrl?: string;
55
+ /**
56
+ * When true (default), the SDK auto-reconnects after WS drops. It first
57
+ * resumes the same session, so every agent URL keeps working; only if the
58
+ * relay refuses (the session ended, e.g. the app was away longer than the
59
+ * relay's grace window) does it open a fresh session and re-mint the
60
+ * previously-minted agent-tokens under it, reporting {oldUrl → newUrl} via
61
+ * onSessionChanged.
62
+ * When false, the SDK neither reconnects nor re-mints; onDisconnect
63
+ * (if given) still fires and may call reconnect() itself, which resumes
64
+ * the same way but doesn't re-mint if it lands in a fresh session.
65
+ */
66
+ autoReconnect?: boolean;
67
+ /**
68
+ * Resume a session this app opened earlier (e.g. before a page or
69
+ * service-worker restart): pass the `sessionId` and `resumeSecret` it had.
70
+ * If the relay refuses (the session ended), connect() opens a fresh
71
+ * session instead — compare `session.sessionId` to tell. Store the secret
72
+ * only where the session's own agent URLs could be stored.
73
+ */
74
+ resume?: {
75
+ sessionId: string;
76
+ secret: string;
77
+ };
78
+ /**
79
+ * Called when the WS drops, and again after each failed reconnect.
80
+ * App decides when (or whether) to reconnect. Not called when the
81
+ * initial connect() fails — that promise rejects instead.
82
+ * Default: exponentialBackoff() (or giveUp with autoReconnect:false).
83
+ */
84
+ onDisconnect?: DisconnectHandler;
85
+ /**
86
+ * Called after a reconnect when agent URLs changed: the resume failed and
87
+ * the SDK opened a fresh session (sessionId changed), or it re-minted
88
+ * tokens an earlier interrupted re-mint missed. Not called after a
89
+ * successful resume. tokensRemapped is non-empty only when autoReconnect:true.
90
+ */
91
+ onSessionChanged?: SessionChangedHandler;
92
+ /**
93
+ * Called after every successful reconnect, once any re-minting is done.
94
+ * `resumed` is true when the same session (and every agent URL) survived.
95
+ */
96
+ onReconnect?: ReconnectHandler;
97
+ /**
98
+ * Optional: heartbeat send interval (ms). Default 25000.
99
+ */
100
+ heartbeatIntervalMs?: number;
101
+ /**
102
+ * Optional: max time to wait for a pong before closing as dead (ms).
103
+ * Default 50000.
104
+ */
105
+ heartbeatTimeoutMs?: number;
106
+ }
107
+ export interface DisconnectInfo {
108
+ reason: string;
109
+ /** Attempt number (1 for first reconnect attempt after a drop). */
110
+ attempt: number;
111
+ /** Call this to attempt the next reconnect. */
112
+ reconnect: () => void;
113
+ /** Call this to give up; SDK won't try to reconnect. */
114
+ giveUp: () => void;
115
+ }
116
+ export type DisconnectHandler = (info: DisconnectInfo) => void | Promise<void>;
117
+ export interface SessionChangedInfo {
118
+ priorSessionId: string;
119
+ sessionId: string;
120
+ /**
121
+ * Map of old paste-link URL → new paste-link URL. With autoReconnect:true,
122
+ * the SDK has already re-minted; the app should update any UI showing old URLs.
123
+ * With autoReconnect:false this is empty (SDK didn't re-mint).
124
+ */
125
+ tokensRemapped: Map<string, string>;
126
+ }
127
+ export type SessionChangedHandler = (info: SessionChangedInfo) => void | Promise<void>;
128
+ export interface ReconnectInfo {
129
+ sessionId: string;
130
+ /** True when the reconnect resumed the same session. */
131
+ resumed: boolean;
132
+ }
133
+ export type ReconnectHandler = (info: ReconnectInfo) => void | Promise<void>;
134
+ export interface AgentToken {
135
+ /** Full agent-token string. Used as the URL secret AND the revoke handle. */
136
+ token: string;
137
+ /** Pre-formatted paste URL (host-rewritten). */
138
+ url: string;
139
+ label: string;
140
+ expiresAt: number | null;
141
+ }
142
+ export interface ListedToken extends AgentToken {
143
+ mintedAt: number;
144
+ }
145
+ /**
146
+ * Public Session interface returned from connect(). Methods are async.
147
+ */
148
+ export interface Session {
149
+ /** Current session-id (changes only when a reconnect can't resume). */
150
+ readonly sessionId: string;
151
+ /**
152
+ * Secret that lets this app resume the session after a drop (see
153
+ * ConnectOptions.resume). The SDK uses it itself on auto-reconnect; read
154
+ * it only to persist the session across a restart. Anyone holding it and
155
+ * the session-id can take the session over, so treat it like the
156
+ * session's agent URLs. null when there is no session to resume.
157
+ */
158
+ readonly resumeSecret: string | null;
159
+ /** Whether the WebSocket is open and the session registered (or resumed). */
160
+ readonly connected: boolean;
161
+ /** Mint a new agent-token. */
162
+ mintAgentToken(opts: {
163
+ label: string;
164
+ }): Promise<AgentToken>;
165
+ /** Revoke an agent-token by its full token string. */
166
+ revokeAgentToken(token: string): Promise<{
167
+ ok: boolean;
168
+ }>;
169
+ /** List currently-active agent-tokens. */
170
+ listAgentTokens(): Promise<ListedToken[]>;
171
+ /**
172
+ * Replace the session's tools (and agents.md, if given) without changing
173
+ * any agent URL: the relay serves the new list in tools.json and routes
174
+ * calls to the new handlers. The relay validates the list exactly like
175
+ * connect()'s; if it refuses (e.g. a duplicate or reserved path) this
176
+ * rejects with an Error whose `code` is the relay's error code, and the
177
+ * old tools stay live. Once it resolves, later resumes and fresh sessions
178
+ * register the new set.
179
+ *
180
+ * Calls run one at a time in call order. While disconnected it waits for
181
+ * the reconnect and then applies; it rejects if the session is closed (or
182
+ * onDisconnect gives up) first.
183
+ */
184
+ updateTools(tools: Tool[], agentsMd?: string): Promise<void>;
185
+ /**
186
+ * Complete an async task. The handler must have previously returned
187
+ * `{ status: 202, taskId }`. The agent's poll on `<URL>/_as_tasks/<taskId>`
188
+ * will then return the supplied status + body.
189
+ *
190
+ * Fire-and-forget — no reply frame. Throws if the WS is not currently
191
+ * open, or if taskId is missing. Async tasks survive a resumed
192
+ * reconnect but not a fresh session (the relay's task map lives in DO
193
+ * memory); completing a task from a prior session is a no-op on the relay.
194
+ */
195
+ completeTask(taskId: string, result?: {
196
+ status?: number;
197
+ body?: unknown;
198
+ headers?: Record<string, string>;
199
+ }): void;
200
+ /**
201
+ * Send a heartbeat ping immediately. No-op if the WS isn't open or
202
+ * there's already a ping in flight. Intended for environments where
203
+ * the runtime can suspend setTimeout-based heartbeats (e.g. Chrome MV3
204
+ * service workers, which use chrome.alarms to wake periodically and
205
+ * call ping() to exercise the WS path).
206
+ */
207
+ ping(): void;
208
+ /** Close the WS and give up. */
209
+ close(): void;
210
+ }
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ // Public SDK types.
2
+ export {};
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@agent-socket/sdk",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "license": "Apache-2.0",
6
+ "description": "Client SDK for agent-socket — connect any web app to AI chats via paste-able URLs.",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/blitzdotdev/agent-socket.git",
10
+ "directory": "sdk"
11
+ },
12
+ "homepage": "https://agentsocket.dev",
13
+ "bugs": "https://github.com/blitzdotdev/agent-socket/issues",
14
+ "engines": {
15
+ "node": ">=22"
16
+ },
17
+ "sideEffects": false,
18
+ "main": "dist/index.js",
19
+ "types": "dist/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.js",
24
+ "default": "./dist/index.js"
25
+ }
26
+ },
27
+ "files": ["dist", "README.md", "LICENSE"],
28
+ "scripts": {
29
+ "build": "tsc",
30
+ "check": "tsc --noEmit",
31
+ "watch": "tsc --watch",
32
+ "test": "tsc && node --test \"test/*.test.mjs\"",
33
+ "prepublishOnly": "npm run build"
34
+ },
35
+ "devDependencies": {
36
+ "typescript": "^5.5.0"
37
+ }
38
+ }