blun-king-cli 9.1.466 → 9.1.467

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,318 @@
1
+ const AGENT_TURNS_PATH = '/v1/agent/turns';
2
+ const PUBLIC_MODEL_ID = 'king';
3
+ const PUBLIC_ID_PATTERN = /^[A-Za-z0-9._:-]{1,128}$/u;
4
+
5
+ class AgentApiHttpAdapterError extends Error {
6
+ constructor(code, message, status = 400) {
7
+ super(message);
8
+ this.name = 'AgentApiHttpAdapterError';
9
+ this.code = code;
10
+ this.status = status;
11
+ }
12
+ }
13
+
14
+ function adapterError(code, message, status = 400) {
15
+ return new AgentApiHttpAdapterError(code, message, status);
16
+ }
17
+
18
+ function requireTrustedId(value, label) {
19
+ if (typeof value !== 'string' || value.trim().length === 0) {
20
+ throw adapterError('AGENT_API_UNAUTHENTICATED', `${label} is required`, 401);
21
+ }
22
+ return value.trim();
23
+ }
24
+
25
+ function requirePublicId(value, label) {
26
+ if (typeof value !== 'string' || !PUBLIC_ID_PATTERN.test(value)) {
27
+ throw adapterError(
28
+ 'AGENT_API_INVALID_ID',
29
+ `${label} must match ${PUBLIC_ID_PATTERN}`,
30
+ );
31
+ }
32
+ return value;
33
+ }
34
+
35
+ function requireBody(value) {
36
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
37
+ throw adapterError('AGENT_API_INVALID_BODY', 'request body must be a JSON object');
38
+ }
39
+ return value;
40
+ }
41
+
42
+ function requireKing(value) {
43
+ if (value !== PUBLIC_MODEL_ID) {
44
+ throw adapterError('AGENT_API_INVALID_MODEL', 'model must be king');
45
+ }
46
+ }
47
+
48
+ function normalizePrompt(value) {
49
+ if (typeof value !== 'string' || value.trim().length === 0) {
50
+ throw adapterError('AGENT_API_INVALID_INPUT', 'input must be a non-empty string');
51
+ }
52
+ return value;
53
+ }
54
+
55
+ function normalizeTools(value) {
56
+ if (value === undefined) return [];
57
+ if (!Array.isArray(value)) {
58
+ throw adapterError('AGENT_API_INVALID_TOOLS', 'tools must be an array');
59
+ }
60
+ return value;
61
+ }
62
+
63
+ function decodeRequestId(value) {
64
+ let decoded;
65
+ try {
66
+ decoded = decodeURIComponent(value);
67
+ } catch {
68
+ throw adapterError('AGENT_API_INVALID_ID', 'request ID is not valid URL encoding');
69
+ }
70
+ return requirePublicId(decoded, 'request ID');
71
+ }
72
+
73
+ function routeFor(method, rawPath) {
74
+ const path = String(rawPath ?? '').split('?', 1)[0];
75
+ if (method === 'POST' && path === AGENT_TURNS_PATH) return { kind: 'start' };
76
+ if (!path.startsWith(`${AGENT_TURNS_PATH}/`)) {
77
+ if (path.startsWith('/v1/agent/')) return { kind: 'unknown' };
78
+ return null;
79
+ }
80
+
81
+ const tail = path.slice(AGENT_TURNS_PATH.length + 1);
82
+ const toolSuffix = '/tool-results';
83
+ if (method === 'POST' && tail.endsWith(toolSuffix)) {
84
+ return {
85
+ kind: 'continue',
86
+ requestId: decodeRequestId(tail.slice(0, -toolSuffix.length)),
87
+ };
88
+ }
89
+ if (method === 'DELETE' && !tail.includes('/')) {
90
+ return { kind: 'cancel', requestId: decodeRequestId(tail) };
91
+ }
92
+ return { kind: 'unknown' };
93
+ }
94
+
95
+ function jsonResponse(status, body) {
96
+ return {
97
+ status,
98
+ headers: { 'content-type': 'application/json' },
99
+ body,
100
+ };
101
+ }
102
+
103
+ function publicCompletion(response) {
104
+ if (response === null || typeof response !== 'object' || Array.isArray(response)) {
105
+ throw adapterError('AGENT_API_INVALID_CONTROLLER_RESPONSE', 'Agent response is invalid', 502);
106
+ }
107
+ return { ...response, model: PUBLIC_MODEL_ID };
108
+ }
109
+
110
+ function publicErrorResponse(error) {
111
+ if (error instanceof AgentApiHttpAdapterError) {
112
+ return jsonResponse(error.status, {
113
+ error: {
114
+ type: 'invalid_request_error',
115
+ code: error.code,
116
+ message: error.message,
117
+ },
118
+ });
119
+ }
120
+
121
+ const code = typeof error?.code === 'string' ? error.code : '';
122
+ if (code === 'AGENT_API_TURN_SCOPE_MISMATCH') {
123
+ return jsonResponse(403, {
124
+ error: {
125
+ type: 'permission_error',
126
+ code,
127
+ message: 'Agent turn belongs to a different request.',
128
+ },
129
+ });
130
+ }
131
+ if (code === 'AGENT_API_SESSION_BUSY' || code === 'AGENT_API_TURN_NOT_ACTIVE') {
132
+ return jsonResponse(409, {
133
+ error: {
134
+ type: 'conflict_error',
135
+ code,
136
+ message: 'Agent session is not ready for this operation.',
137
+ },
138
+ });
139
+ }
140
+ if (code.startsWith('AGENT_API_INVALID_')) {
141
+ return jsonResponse(400, {
142
+ error: {
143
+ type: 'invalid_request_error',
144
+ code,
145
+ message: 'Agent request is invalid.',
146
+ },
147
+ });
148
+ }
149
+ if (code === 'AGENT_API_TURN_FAILED') {
150
+ return jsonResponse(502, {
151
+ error: {
152
+ type: 'upstream_error',
153
+ code,
154
+ message: 'Agent turn failed.',
155
+ },
156
+ });
157
+ }
158
+ return jsonResponse(500, {
159
+ error: {
160
+ type: 'internal_error',
161
+ code: 'AGENT_API_INTERNAL',
162
+ message: 'Agent request failed.',
163
+ },
164
+ });
165
+ }
166
+
167
+ function scopeKey(sessionId, requestId) {
168
+ return `${sessionId}\0${requestId}`;
169
+ }
170
+
171
+ function responseNeedsToolResult(response) {
172
+ return response?.choices?.[0]?.finish_reason === 'tool_calls';
173
+ }
174
+
175
+ function validateController(controller) {
176
+ if (controller === null || typeof controller !== 'object') {
177
+ throw new TypeError('controller is required');
178
+ }
179
+ for (const method of ['startTurn', 'continueTurn', 'cancelTurn']) {
180
+ if (typeof controller[method] !== 'function') {
181
+ throw new TypeError(`controller.${method} must be a function`);
182
+ }
183
+ }
184
+ return controller;
185
+ }
186
+
187
+ function createAgentApiHttpAdapter(options = {}) {
188
+ const controller = validateController(options.controller);
189
+ if (typeof options.resolveSession !== 'function') {
190
+ throw new TypeError('resolveSession must be a function');
191
+ }
192
+ const owners = new Map();
193
+
194
+ function requireOwner(principalId, sessionId, requestId) {
195
+ const key = scopeKey(sessionId, requestId);
196
+ const owner = owners.get(key);
197
+ if (owner === undefined) {
198
+ throw adapterError(
199
+ 'AGENT_API_TURN_NOT_ACTIVE',
200
+ 'Agent turn is not active.',
201
+ 409,
202
+ );
203
+ }
204
+ if (owner !== principalId) {
205
+ throw adapterError(
206
+ 'AGENT_API_FORBIDDEN',
207
+ 'Agent turn belongs to a different account.',
208
+ 403,
209
+ );
210
+ }
211
+ return key;
212
+ }
213
+
214
+ function trackScope(key, principalId, response) {
215
+ if (responseNeedsToolResult(response)) owners.set(key, principalId);
216
+ else owners.delete(key);
217
+ }
218
+
219
+ async function handleStart(input, principalId) {
220
+ const body = requireBody(input.body);
221
+ requireKing(body.model);
222
+ if (body.stream === true) {
223
+ throw adapterError(
224
+ 'AGENT_API_STREAMING_UNSUPPORTED',
225
+ 'streaming is not available on Agent turns yet',
226
+ );
227
+ }
228
+ const sessionId = requirePublicId(body.session_id, 'session_id');
229
+ const requestId = requirePublicId(body.request_id, 'request_id');
230
+ const session = await options.resolveSession({
231
+ principalId,
232
+ sessionId,
233
+ create: true,
234
+ });
235
+ if (session?.id !== sessionId) {
236
+ throw adapterError(
237
+ 'AGENT_API_SESSION_SCOPE_MISMATCH',
238
+ 'resolved session does not match session_id',
239
+ 403,
240
+ );
241
+ }
242
+ const response = publicCompletion(await controller.startTurn({
243
+ session,
244
+ requestId,
245
+ prompt: normalizePrompt(body.input),
246
+ tools: normalizeTools(body.tools),
247
+ }));
248
+ trackScope(scopeKey(sessionId, requestId), principalId, response);
249
+ return jsonResponse(200, response);
250
+ }
251
+
252
+ async function handleContinue(input, principalId, requestId) {
253
+ const body = requireBody(input.body);
254
+ const sessionId = requirePublicId(body.session_id, 'session_id');
255
+ const key = requireOwner(principalId, sessionId, requestId);
256
+ const response = publicCompletion(await controller.continueTurn({
257
+ sessionId,
258
+ requestId,
259
+ toolResults: body.tool_results,
260
+ }));
261
+ trackScope(key, principalId, response);
262
+ return jsonResponse(200, response);
263
+ }
264
+
265
+ async function handleCancel(input, principalId, requestId) {
266
+ const body = requireBody(input.body ?? {});
267
+ const sessionId = requirePublicId(body.session_id, 'session_id');
268
+ const key = requireOwner(principalId, sessionId, requestId);
269
+ await controller.cancelTurn({
270
+ sessionId,
271
+ requestId,
272
+ reason: typeof body.reason === 'string' ? body.reason : undefined,
273
+ });
274
+ owners.delete(key);
275
+ return jsonResponse(200, {
276
+ id: `agent-${requestId}`,
277
+ object: 'agent.turn.cancelled',
278
+ model: PUBLIC_MODEL_ID,
279
+ cancelled: true,
280
+ });
281
+ }
282
+
283
+ async function handleRequest(input) {
284
+ if (input === null || typeof input !== 'object') return null;
285
+ const method = String(input.method ?? '').toUpperCase();
286
+ let route;
287
+ try {
288
+ route = routeFor(method, input.path);
289
+ if (route === null) return null;
290
+ if (route.kind === 'unknown') {
291
+ return jsonResponse(404, {
292
+ error: {
293
+ type: 'invalid_request_error',
294
+ code: 'AGENT_API_ROUTE_NOT_FOUND',
295
+ message: 'Agent API route not found.',
296
+ },
297
+ });
298
+ }
299
+ const principalId = requireTrustedId(input.principalId, 'principalId');
300
+ if (route.kind === 'start') return await handleStart(input, principalId);
301
+ if (route.kind === 'continue') {
302
+ return await handleContinue(input, principalId, route.requestId);
303
+ }
304
+ return await handleCancel(input, principalId, route.requestId);
305
+ } catch (error) {
306
+ return publicErrorResponse(error);
307
+ }
308
+ }
309
+
310
+ return Object.freeze({ handleRequest });
311
+ }
312
+
313
+ export {
314
+ AGENT_TURNS_PATH,
315
+ AgentApiHttpAdapterError,
316
+ PUBLIC_MODEL_ID,
317
+ createAgentApiHttpAdapter,
318
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blun-king-cli",
3
- "version": "9.1.466",
3
+ "version": "9.1.467",
4
4
  "description": "BLUN CLI - your own AI agent with a Telegram channel. Get it done. With BLUN.",
5
5
  "license": "MIT",
6
6
  "bin": {