lampson 0.2.10 → 0.4.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.
- package/README.md +52 -1
- package/bin/lampson.js +2 -2
- package/chat.syn +40 -24
- package/cli.syn +6 -3
- package/hub.tpl.syn +15 -0
- package/lampson.ps1 +85 -11
- package/lampson.sh +82 -24
- package/lib/api.syn +292 -0
- package/lib/apikeys.syn +115 -0
- package/lib/approvals.syn +7 -0
- package/lib/channels.syn +340 -0
- package/lib/loop.syn +77 -3
- package/lib/mailbox.syn +440 -0
- package/lib/mcp.syn +128 -9
- package/lib/mcp_server.syn +235 -0
- package/lib/plugins.syn +94 -7
- package/lib/runtime.syn +630 -0
- package/lib/scope.syn +62 -0
- package/lib/session.syn +11 -1
- package/lib/teamrun.syn +142 -0
- package/lib/teams.syn +802 -0
- package/lib/tools.syn +1 -1
- package/lib/workspaces.syn +78 -3
- package/lib/worktree.syn +105 -0
- package/package.json +16 -2
- package/plugins/chatwoot/plugin.json +11 -0
- package/plugins/chatwoot/plugin.syn +143 -0
- package/plugins/orders/INSTRUCTIONS.md +16 -0
- package/plugins/orders/plugin.json +81 -0
- package/plugins/orders/plugin.syn +342 -0
- package/plugins/telegram/plugin.json +11 -0
- package/plugins/telegram/plugin.syn +272 -0
- package/public/css/panel.css +2 -0
- package/public/css/teams.css +159 -0
- package/public/index.html +10 -0
- package/public/js/app.js +1 -1
- package/public/js/engine.js +149 -0
- package/public/js/sessions.js +5 -1
- package/public/js/teams.js +301 -0
- package/public/teams.html +75 -0
- package/run.syn +493 -0
- package/sdk/index.d.ts +94 -0
- package/sdk/index.js +250 -0
- package/sdk/index.mjs +4 -0
- package/web.syn +428 -47
package/web.syn
CHANGED
|
@@ -30,6 +30,7 @@ require random
|
|
|
30
30
|
require env("LAMPSON_*")
|
|
31
31
|
require env("OS")
|
|
32
32
|
require secret("LAMPSON_*")
|
|
33
|
+
require db(".lampson/teams.db")
|
|
33
34
|
require judge
|
|
34
35
|
require file("workspace")
|
|
35
36
|
require file("workspace/*")
|
|
@@ -37,6 +38,8 @@ require file.read("skills")
|
|
|
37
38
|
require file.read("skills/*")
|
|
38
39
|
require file.read("plugins")
|
|
39
40
|
require file.read("plugins/*")
|
|
41
|
+
require file.read("agents")
|
|
42
|
+
require file.read("agents/*")
|
|
40
43
|
require file("memory")
|
|
41
44
|
require file("memory/*")
|
|
42
45
|
require file(".lampson")
|
|
@@ -46,8 +49,6 @@ require file.read("public/*")
|
|
|
46
49
|
|
|
47
50
|
use "./lib/provider.syn" as provider
|
|
48
51
|
use "./lib/tools.syn" as tools
|
|
49
|
-
use "./lib/prompt.syn" as prompt
|
|
50
|
-
use "./lib/loop.syn" as loop
|
|
51
52
|
use "./lib/session.syn" as session
|
|
52
53
|
use "./lib/agents.syn" as agents
|
|
53
54
|
use "./lib/tree.syn" as tree
|
|
@@ -71,6 +72,14 @@ use "./lib/computer.syn" as computer
|
|
|
71
72
|
use "./lib/workspaces.syn" as workspaces
|
|
72
73
|
use "./lib/judge.syn" as jdg
|
|
73
74
|
use "./lib/origin.syn" as origin
|
|
75
|
+
use "./lib/runtime.syn" as runtime
|
|
76
|
+
use "./lib/api.syn" as api
|
|
77
|
+
use "./lib/apikeys.syn" as apikeys
|
|
78
|
+
use "./lib/mailbox.syn" as mailbox
|
|
79
|
+
use "./lib/teamrun.syn" as teamrun
|
|
80
|
+
use "./lib/teams.syn" as teams
|
|
81
|
+
use "./lib/channels.syn" as channels
|
|
82
|
+
use "./lib/mcp_server.syn" as mcp_server
|
|
74
83
|
|
|
75
84
|
let APPROVAL_TIMEOUT be 180
|
|
76
85
|
|
|
@@ -93,6 +102,10 @@ task check_client(token, request)
|
|
|
93
102
|
let want be env("LAMPSON_WEB_TOKEN", "")
|
|
94
103
|
when want != "" and token == want
|
|
95
104
|
give {"local": false}
|
|
105
|
+
-- API key de /v1 (lib/apikeys.syn): vale SOLO para /w/<slug>/v1/… — nunca para la UI, la terminal o el chat
|
|
106
|
+
when token != nothing and starts_with(text(token), apikeys.PREFIX) and contains(text(request.path), "/v1/")
|
|
107
|
+
when apikeys.check(token) != nothing
|
|
108
|
+
give {"local": false, "api_key": true}
|
|
96
109
|
give nothing
|
|
97
110
|
|
|
98
111
|
-- salida del pty como texto (la doc de 0.6.8 ya lo dice así; is_bytes por si cambia)
|
|
@@ -101,21 +114,27 @@ task as_text(x)
|
|
|
101
114
|
give decode(x, "utf8_lossy")
|
|
102
115
|
give x
|
|
103
116
|
|
|
104
|
-
task boot(profile, mode, ask_fn, pname, model)
|
|
105
|
-
let cfg be provider.config_for(pname, model)
|
|
106
|
-
-- worker es interno de delegate: por la API tampoco se elige como perfil de sesión
|
|
107
|
-
let p be when contains(agents.names(), profile) then profile otherwise "build"
|
|
108
|
-
let opts be loop.default_opts(agents.registry_for(p), agents.catalog_for(p), ask_fn)
|
|
109
|
-
set opts to loop.with_steps(opts, agents.steps_for(p))
|
|
110
|
-
set opts to loop.with_inbox(opts, agents.parent_inbox, "")
|
|
111
|
-
set opts to loop.with_mode(opts, mode)
|
|
112
|
-
give {"cfg": cfg, "system": {"role": "system", "content": prompt.build(agents.env_info(cfg), agents.addendum_for(p))}, "opts": opts, "profile": p}
|
|
113
|
-
|
|
114
117
|
-- Subagente en background: `agent Child` (lib/agents.syn) solo puede llamar tasks TOP-LEVEL de este
|
|
115
118
|
-- programa; esta es su puerta de entrada. Bajo serve el agente sobrevive al request que lo lanzó.
|
|
116
119
|
task lampson_subagent(spec_json)
|
|
117
120
|
give agents.run_child_json(spec_json)
|
|
118
121
|
|
|
122
|
+
-- Worker del buzón de una conversación (lib/mailbox.syn): `agent MailboxWorker` solo llama tasks TOP-LEVEL; esta es su puerta
|
|
123
|
+
task lampson_mailbox_worker(spec_json)
|
|
124
|
+
give mailbox.work(spec_json)
|
|
125
|
+
|
|
126
|
+
-- Canales en modo sin URL pública (lib/channels.syn): un agente ChannelPoller por canal; el tick lo mantiene vivo
|
|
127
|
+
task lampson_channel_poller(name)
|
|
128
|
+
give channels.poll_loop(name)
|
|
129
|
+
task channel_poll_tick()
|
|
130
|
+
give channels.poll_tick()
|
|
131
|
+
cron_every(5, channel_poll_tick)
|
|
132
|
+
|
|
133
|
+
-- EQUIPOS (lib/teamrun.syn): entrega los mensajes entre agentes (tool send) por el buzón y lanza las rondas
|
|
134
|
+
task team_tick()
|
|
135
|
+
give teamrun.tick()
|
|
136
|
+
cron_every(teamrun.TICK_SECONDS, team_tick)
|
|
137
|
+
|
|
119
138
|
-- TAREAS PROGRAMADAS (lib/schedule.syn): este proceso es el residente de lampson — con `lampson --daemon start`
|
|
120
139
|
-- (synsema daemon) queda en background y ejecuta lo programado aunque no haya ninguna UI abierta. Un tick cada
|
|
121
140
|
-- 30 s (cron_every de Synsema: intervalo puro; la hora de reloj la resuelve schedule.syn) corre lo vencido:
|
|
@@ -141,46 +160,62 @@ serve on 8080
|
|
|
141
160
|
max_streams 50
|
|
142
161
|
auth with check_client
|
|
143
162
|
static "./public"
|
|
163
|
+
-- lo que publican /openapi.json, /llms.txt, /docs y la agent card: SOLO la API /v1 (todo lo demás lleva `private`)
|
|
164
|
+
describe
|
|
165
|
+
about: "Lampson API — agents of this workspace for your apps, channels and other agents"
|
|
166
|
+
version: "1.0.0"
|
|
167
|
+
api: ["POST /w/:slug/v1/messages -- send a message to an agent and get its answer (JSON); with \"callback\", 202 now and the answer POSTed later. Body: {to, thread, text, images?, from?, meta?, sent_at?, session?, callback?}", "POST /w/:slug/v1/messages/stream -- the same, as Server-Sent Events: session, the agent's steps, done", "GET /w/:slug/v1/agents -- the agents this API key can use", "GET /w/:slug/v1/jobs/:id -- status and result of a message sent with a callback", "POST /w/:slug/v1/mcp -- Model Context Protocol (Streamable HTTP, JSON responses): tools ask, agents and history, for other AI agents", "POST /w/:slug/v1/mcp/stream -- the same MCP server answering with Server-Sent Events: with params._meta.progressToken, each step of the agent arrives as notifications/progress before the result", "GET /w/:slug/v1/mcp -- 405: this MCP server opens no server-to-client stream", "POST /w/:slug/v1/objectives -- give an agent (or a team) an objective to work on by itself until done; it plans on a board, works in rounds and asks before anything irreversible. Body: {text, agent? | team?, budget_tokens?, round_seconds?}", "GET /w/:slug/v1/objectives -- the objectives this API key can see", "GET /w/:slug/v1/objectives/:id -- one objective: status, last report, question or approval it waits for, result, board and record", "POST /w/:slug/v1/objectives/:id/reply -- answer its question, or approve/deny what it asked: {text?, approved?}", "POST /w/:slug/v1/objectives/:id/cancel -- stop it"]
|
|
144
168
|
|
|
145
169
|
-- la UI vive bajo /w/<slug> (BASE en js/core.js); si alguien entra por el puerto del proceso, es lo mismo
|
|
146
170
|
-- Acceso DIRECTO por el puerto del proceso (sin X-Forwarded-Host, que solo agrega el hub al proxyear): la UI se
|
|
147
171
|
-- usa siempre por el hub → redirigir a http://127.0.0.1:8080/w/<slug>. Standalone sin hub (docker) no hay
|
|
148
172
|
-- LAMPSON_WS y se sirve acá mismo.
|
|
149
173
|
route "GET /w/:slug"
|
|
174
|
+
private
|
|
150
175
|
let h be headers of request
|
|
151
176
|
when env("LAMPSON_WS", "") != "" and not contains(h, "x-forwarded-host")
|
|
152
177
|
give redirect("http://127.0.0.1:8080/w/" + params.slug)
|
|
153
178
|
give html(read_file("public/index.html"))
|
|
154
179
|
route "GET /"
|
|
180
|
+
private
|
|
155
181
|
when env("LAMPSON_WS", "") == ""
|
|
156
182
|
give redirect("/w/local")
|
|
157
183
|
give redirect("http://127.0.0.1:8080/w/" + env("LAMPSON_WS", ""))
|
|
158
184
|
route "GET /api/hub"
|
|
185
|
+
private
|
|
159
186
|
proxy to "http://127.0.0.1:8080"
|
|
160
187
|
route "GET /api/workspaces"
|
|
188
|
+
private
|
|
161
189
|
proxy to "http://127.0.0.1:8080"
|
|
162
190
|
route "GET /api/workspaces/*path"
|
|
191
|
+
private
|
|
163
192
|
proxy to "http://127.0.0.1:8080"
|
|
164
193
|
route "POST /api/workspaces"
|
|
194
|
+
private
|
|
165
195
|
proxy to "http://127.0.0.1:8080"
|
|
166
196
|
route "POST /api/workspaces/*path"
|
|
197
|
+
private
|
|
167
198
|
proxy to "http://127.0.0.1:8080"
|
|
168
199
|
|
|
169
200
|
-- salud para el hub/supervisor y la terminal (último uso = chat, terminal, eventos o abrir la UI)
|
|
170
201
|
route "GET /w/:slug/api/health"
|
|
202
|
+
private
|
|
171
203
|
give {"alive": true, "slug": env("LAMPSON_WS", ""), "last_used": state_get("lampson:last_used", nothing), "schedules_on": length(where(schedule.all(), (t) => t["enabled"] == true)), "version": env("LAMPSON_VERSION", "")}
|
|
172
204
|
|
|
173
205
|
route "GET /w/:slug/api/sessions" requires auth
|
|
206
|
+
private
|
|
174
207
|
state_set("lampson:last_used", now())
|
|
175
208
|
give {"sessions": session.list()}
|
|
176
209
|
|
|
177
210
|
-- buscador de sesiones (modal «ver todas»): ?q=texto busca en títulos y mensajes de este proyecto
|
|
178
211
|
route "GET /w/:slug/api/sessions/search" requires auth
|
|
212
|
+
private
|
|
179
213
|
let q be when contains(query, "q") then text(query.q) otherwise ""
|
|
180
214
|
let n be when contains(query, "limit") then floor(number(query.limit)) otherwise 200
|
|
181
215
|
give {"q": q, "sessions": session.search(q, n)}
|
|
182
216
|
|
|
183
217
|
route "GET /w/:slug/api/sessions/:id" requires auth
|
|
218
|
+
private
|
|
184
219
|
when not session.exists(params.id)
|
|
185
220
|
give not_found("session not found")
|
|
186
221
|
let doc be session.load(params.id)
|
|
@@ -188,11 +223,13 @@ serve on 8080
|
|
|
188
223
|
give {"id": doc["id"], "updated": doc["updated"], "meta": doc["meta"], "messages": visible}
|
|
189
224
|
|
|
190
225
|
route "GET /w/:slug/api/config" requires auth
|
|
226
|
+
private
|
|
191
227
|
let cfg be provider.config()
|
|
192
228
|
give {"provider": cfg["provider"], "model": cfg["model"], "wire": cfg["wire"], "vision": provider.supports_vision(cfg), "providers": provider.providers(), "configured": provider.configured(), "permission": lower(env("LAMPSON_PERMISSION", "ask")), "agents": agents.names(), "workspace": env("LAMPSON_WORKSPACE", "./workspace"), "profiles": agents.PROFILES}
|
|
193
229
|
|
|
194
230
|
-- proveedor/modelo por defecto y API keys → .lampson/config.json (local). La key nunca vuelve al cliente.
|
|
195
231
|
route "POST /w/:slug/api/settings" requires auth
|
|
232
|
+
private
|
|
196
233
|
expect body {provider: text}
|
|
197
234
|
let b be json of request
|
|
198
235
|
let name be lower(text(b["provider"]))
|
|
@@ -206,11 +243,13 @@ serve on 8080
|
|
|
206
243
|
give {"ok": true, "providers": provider.providers(), "provider": name, "model": ncfg["model"], "vision": provider.supports_vision(ncfg), "configured": provider.configured()}
|
|
207
244
|
|
|
208
245
|
route "POST /w/:slug/api/sessions/delete" requires auth
|
|
246
|
+
private
|
|
209
247
|
expect body {id: text}
|
|
210
248
|
let b be json of request
|
|
211
249
|
give {"result": session.delete(text(b["id"]))}
|
|
212
250
|
|
|
213
251
|
route "GET /w/:slug/api/tree" requires auth
|
|
252
|
+
private
|
|
214
253
|
let t be tree.tree(6)
|
|
215
254
|
-- ruta real del proyecto (lampson.ps1/.sh exportan LAMPSON_WORKSPACE): «copiar ruta absoluta» en el explorador
|
|
216
255
|
set t["root"] to env("LAMPSON_WORKSPACE", "")
|
|
@@ -223,6 +262,7 @@ serve on 8080
|
|
|
223
262
|
-- Servidor → navegador: "o" + salida cruda del pty | "c" + JSON de control (hello | exit).
|
|
224
263
|
-- Sin ningún socket enganchado por 30 min, el supervisor recoge la shell; todo muere con lampson.
|
|
225
264
|
route "GET /w/:slug/api/term" requires auth
|
|
265
|
+
private
|
|
226
266
|
socket
|
|
227
267
|
state_set("lampson:last_used", now())
|
|
228
268
|
let id be when contains(query, "id") then text(query.id) otherwise ""
|
|
@@ -270,28 +310,35 @@ serve on 8080
|
|
|
270
310
|
|
|
271
311
|
-- las terminales vivas: la UI las repinta como pestañas al cargar (después de un F5 siguen ahí)
|
|
272
312
|
route "GET /w/:slug/api/terms" requires auth
|
|
313
|
+
private
|
|
273
314
|
give {"terminals": term.list(), "max": term.MAX_TERMS}
|
|
274
315
|
|
|
275
316
|
route "GET /w/:slug/api/update" requires auth
|
|
317
|
+
private
|
|
276
318
|
give update.check()
|
|
277
319
|
|
|
278
320
|
route "POST /w/:slug/api/update" requires auth
|
|
321
|
+
private
|
|
279
322
|
give {"result": update.apply()}
|
|
280
323
|
|
|
281
324
|
route "GET /w/:slug/api/git" requires auth
|
|
325
|
+
private
|
|
282
326
|
give git.status()
|
|
283
327
|
|
|
284
328
|
route "GET /w/:slug/api/memory" requires auth
|
|
329
|
+
private
|
|
285
330
|
-- ok=false: el link memory/ del workspace está roto (la UI lo dice; el agente ya lo sabe por el prompt)
|
|
286
331
|
give {"dir": memo.dir(), "ok": memo.available(), "notes": memo.list()}
|
|
287
332
|
|
|
288
333
|
route "GET /w/:slug/api/memory/note" requires auth
|
|
334
|
+
private
|
|
289
335
|
when not contains(query, "name")
|
|
290
336
|
give fail(400, "name required")
|
|
291
337
|
give {"name": query.name, "content": memo.note_read(query.name)}
|
|
292
338
|
|
|
293
339
|
-- traza legible de una sesión (ver lib/trace.syn)
|
|
294
340
|
route "GET /w/:slug/api/trace" requires auth
|
|
341
|
+
private
|
|
295
342
|
when not contains(query, "session")
|
|
296
343
|
give fail(400, "session required")
|
|
297
344
|
when not matches(query.session, "[A-Za-z0-9_-]{1,40}")
|
|
@@ -301,21 +348,25 @@ serve on 8080
|
|
|
301
348
|
|
|
302
349
|
-- modelos que declara la API del proveedor (para no tipear nombres a ciegas: DeepSeek es case-sensitive)
|
|
303
350
|
route "GET /w/:slug/api/models" requires auth
|
|
351
|
+
private
|
|
304
352
|
let pname be when contains(query, "provider") then lower(text(query.provider)) otherwise ""
|
|
305
353
|
let cfg be provider.config_for(pname, "")
|
|
306
354
|
give provider.list_models(cfg)
|
|
307
355
|
|
|
308
356
|
route "GET /w/:slug/api/todo" requires auth
|
|
357
|
+
private
|
|
309
358
|
-- ?session=<id> → la lista de esa sesión; sin query, la de la sesión en curso
|
|
310
359
|
when contains(query, "session") and matches(query.session, "[A-Za-z0-9_-]{1,40}")
|
|
311
360
|
give {"items": todo.load(query.session)}
|
|
312
361
|
give {"items": todo.load(nothing)}
|
|
313
362
|
|
|
314
363
|
route "GET /w/:slug/api/mcp" requires auth
|
|
364
|
+
private
|
|
315
365
|
give {"servers": mcp.summary(), "global": mcp.GLOBAL_CONFIG, "project": mcp.PROJECT_CONFIG}
|
|
316
366
|
|
|
317
367
|
-- conectar un server MCP desde la UI: se escribe en el mcp.json del scope y arranca su supervisor
|
|
318
368
|
route "POST /w/:slug/api/mcp/add" requires auth
|
|
369
|
+
private
|
|
319
370
|
expect body {name: text, command: text}
|
|
320
371
|
let b be json of request
|
|
321
372
|
let scope be when contains(b, "scope") then lower(text(b["scope"])) otherwise "global"
|
|
@@ -326,6 +377,7 @@ serve on 8080
|
|
|
326
377
|
give fail(400, text(err))
|
|
327
378
|
|
|
328
379
|
route "POST /w/:slug/api/mcp/remove" requires auth
|
|
380
|
+
private
|
|
329
381
|
expect body {name: text}
|
|
330
382
|
let b be json of request
|
|
331
383
|
try
|
|
@@ -335,9 +387,11 @@ serve on 8080
|
|
|
335
387
|
|
|
336
388
|
-- language servers (navegación semántica): listar, agregar (preset o comando propio), quitar
|
|
337
389
|
route "GET /w/:slug/api/lsp" requires auth
|
|
390
|
+
private
|
|
338
391
|
give {"servers": lsp.summary(), "presets": lsp.PRESETS, "global": lsp.GLOBAL_CONFIG, "project": lsp.PROJECT_CONFIG}
|
|
339
392
|
|
|
340
393
|
route "POST /w/:slug/api/lsp/add" requires auth
|
|
394
|
+
private
|
|
341
395
|
expect body {name: text}
|
|
342
396
|
let b be json of request
|
|
343
397
|
let scope be when contains(b, "scope") then lower(text(b["scope"])) otherwise "global"
|
|
@@ -349,6 +403,7 @@ serve on 8080
|
|
|
349
403
|
give fail(400, text(err))
|
|
350
404
|
|
|
351
405
|
route "POST /w/:slug/api/lsp/remove" requires auth
|
|
406
|
+
private
|
|
352
407
|
expect body {name: text}
|
|
353
408
|
let b be json of request
|
|
354
409
|
try
|
|
@@ -358,10 +413,12 @@ serve on 8080
|
|
|
358
413
|
|
|
359
414
|
-- plugins (tools propias): listar y encender/apagar desde la barra superior de la UI
|
|
360
415
|
route "GET /w/:slug/api/plugins" requires auth
|
|
416
|
+
private
|
|
361
417
|
give {"plugins": plugins.summary(), "global": plugins.GLOBAL_DIR, "project": plugins.PROJECT_DIR}
|
|
362
418
|
|
|
363
419
|
-- el usuario corre una tool de un plugin encendido él mismo (sin pasar por el modelo)
|
|
364
420
|
route "POST /w/:slug/api/plugins/run" requires auth
|
|
421
|
+
private
|
|
365
422
|
expect body {tool: text}
|
|
366
423
|
let b be json of request
|
|
367
424
|
let args be when contains(b, "args") then b["args"] otherwise {}
|
|
@@ -371,6 +428,7 @@ serve on 8080
|
|
|
371
428
|
give fail(400, text(err))
|
|
372
429
|
|
|
373
430
|
route "POST /w/:slug/api/plugins/remove" requires auth
|
|
431
|
+
private
|
|
374
432
|
expect body {name: text}
|
|
375
433
|
let b be json of request
|
|
376
434
|
try
|
|
@@ -379,6 +437,7 @@ serve on 8080
|
|
|
379
437
|
give fail(400, text(err))
|
|
380
438
|
|
|
381
439
|
route "POST /w/:slug/api/plugins/toggle" requires auth
|
|
440
|
+
private
|
|
382
441
|
expect body {name: text}
|
|
383
442
|
let b be json of request
|
|
384
443
|
let on be contains(b, "enabled") and b["enabled"] == true
|
|
@@ -388,6 +447,7 @@ serve on 8080
|
|
|
388
447
|
give fail(400, text(err))
|
|
389
448
|
|
|
390
449
|
route "GET /w/:slug/api/agents" requires auth
|
|
450
|
+
private
|
|
391
451
|
agents.prune(10, 1800)
|
|
392
452
|
give {"agents": agents.list_children()}
|
|
393
453
|
|
|
@@ -395,6 +455,7 @@ serve on 8080
|
|
|
395
455
|
-- cambios de estado) y los subagentes (subagent.started|done|<id>). Un bus por programa; cada cliente
|
|
396
456
|
-- tiene su cola acotada (drop_oldest). Heartbeat cada 25 s para que el proxy/navegador no cierre.
|
|
397
457
|
route "GET /w/:slug/api/events" requires auth
|
|
458
|
+
private
|
|
398
459
|
stream
|
|
399
460
|
state_set("lampson:last_used", now())
|
|
400
461
|
let sub be bus_subscribe(["proc.*", "subagent.*", "mcp.*", "approval.*", "schedule.*"], {"max_queue": 512})
|
|
@@ -411,6 +472,7 @@ serve on 8080
|
|
|
411
472
|
bus_unsubscribe(sub)
|
|
412
473
|
|
|
413
474
|
route "GET /w/:slug/api/agents/log" requires auth
|
|
475
|
+
private
|
|
414
476
|
when not contains(query, "id")
|
|
415
477
|
give fail(400, "id required")
|
|
416
478
|
when not matches(query.id, "[a-z]+-[0-9]+")
|
|
@@ -426,6 +488,7 @@ serve on 8080
|
|
|
426
488
|
give fail(404, "no such sub-agent")
|
|
427
489
|
|
|
428
490
|
route "POST /w/:slug/api/agents/stop" requires auth
|
|
491
|
+
private
|
|
429
492
|
expect body {id: text}
|
|
430
493
|
let b be json of request
|
|
431
494
|
when not matches(b["id"], "[a-z]+-[0-9]+")
|
|
@@ -435,28 +498,34 @@ serve on 8080
|
|
|
435
498
|
give {"result": "stop requested for " + b["id"]}
|
|
436
499
|
|
|
437
500
|
route "GET /w/:slug/api/proc" requires auth
|
|
501
|
+
private
|
|
438
502
|
give {"processes": proc.list()}
|
|
439
503
|
|
|
440
504
|
route "GET /w/:slug/api/proc/log" requires auth
|
|
505
|
+
private
|
|
441
506
|
when not contains(query, "name")
|
|
442
507
|
give fail(400, "name required")
|
|
443
508
|
let n be when contains(query, "tail") then number(query.tail) otherwise 200
|
|
444
509
|
give {"name": query.name, "running": proc.alive(query.name), "log": proc.logs(query.name, n)}
|
|
445
510
|
|
|
446
511
|
route "GET /w/:slug/api/ports" requires auth
|
|
512
|
+
private
|
|
447
513
|
give {"ports": proc.listeners()}
|
|
448
514
|
|
|
449
515
|
route "POST /w/:slug/api/ports/kill" requires auth
|
|
516
|
+
private
|
|
450
517
|
expect body {pid: number}
|
|
451
518
|
let b be json of request
|
|
452
519
|
give {"result": proc.kill_pid(b["pid"])}
|
|
453
520
|
|
|
454
521
|
route "POST /w/:slug/api/proc/stop" requires auth
|
|
522
|
+
private
|
|
455
523
|
expect body {name: text}
|
|
456
524
|
let b be json of request
|
|
457
525
|
give {"result": proc.halt(b["name"])}
|
|
458
526
|
|
|
459
527
|
route "GET /w/:slug/api/file" requires auth
|
|
528
|
+
private
|
|
460
529
|
when not contains(query, "path")
|
|
461
530
|
give fail(400, "path required")
|
|
462
531
|
give tree.file_content(query.path)
|
|
@@ -464,6 +533,7 @@ serve on 8080
|
|
|
464
533
|
-- explorador: nuevo archivo/carpeta, renombrar, mover, copiar/duplicar, borrar (lib/fs.syn; solo dentro
|
|
465
534
|
-- del workspace, nunca pisa un destino). {op, path, name?, to?, content?} → {ok, result: {path, is_dir}}
|
|
466
535
|
route "POST /w/:slug/api/fs" requires auth
|
|
536
|
+
private
|
|
467
537
|
expect body {op: text, path: text}
|
|
468
538
|
let b be json of request
|
|
469
539
|
try
|
|
@@ -473,9 +543,11 @@ serve on 8080
|
|
|
473
543
|
|
|
474
544
|
-- configuración general (rueda de la UI): zona horaria, URL pública, webhook… → .lampson/config.json
|
|
475
545
|
route "GET /w/:slug/api/settings/values" requires auth
|
|
546
|
+
private
|
|
476
547
|
give {"values": settings.values(), "tz_detected": schedule.tz_offset()}
|
|
477
548
|
|
|
478
549
|
route "POST /w/:slug/api/settings/values" requires auth
|
|
550
|
+
private
|
|
479
551
|
let b be json of request
|
|
480
552
|
try
|
|
481
553
|
each k in keys(b)
|
|
@@ -489,9 +561,11 @@ serve on 8080
|
|
|
489
561
|
-- computer use (lib/computer.syn): estado del driver externo (instalado, versión, doctor) y encendido/apagado.
|
|
490
562
|
-- Lampson no instala el driver: si falta, `install` trae el comando oficial y la UI lo muestra.
|
|
491
563
|
route "GET /w/:slug/api/computer" requires auth
|
|
564
|
+
private
|
|
492
565
|
give computer.status()
|
|
493
566
|
|
|
494
567
|
route "POST /w/:slug/api/computer" requires auth
|
|
568
|
+
private
|
|
495
569
|
let b be json of request
|
|
496
570
|
try
|
|
497
571
|
when contains(b, "enabled")
|
|
@@ -510,9 +584,11 @@ serve on 8080
|
|
|
510
584
|
-- el juez de decisión (lib/judge.syn): una credencial de Lampson, no de computer use. El runtime lo lee al
|
|
511
585
|
-- arrancar el proceso, así que cambiarlo reinicia este workspace solo en ~3 s (la UI espera y recarga).
|
|
512
586
|
route "GET /w/:slug/api/judge" requires auth
|
|
587
|
+
private
|
|
513
588
|
give jdg.status()
|
|
514
589
|
|
|
515
590
|
route "POST /w/:slug/api/judge" requires auth
|
|
591
|
+
private
|
|
516
592
|
let b be json of request
|
|
517
593
|
try
|
|
518
594
|
let cur be jdg.status()
|
|
@@ -532,16 +608,19 @@ serve on 8080
|
|
|
532
608
|
|
|
533
609
|
-- aprobaciones (lib/approvals.syn): las del chat y las de tareas programadas comparten la cola
|
|
534
610
|
route "POST /w/:slug/api/approve" requires auth
|
|
611
|
+
private
|
|
535
612
|
expect body {id: text, decision: bool}
|
|
536
613
|
let b be json of request
|
|
537
614
|
give {"ok": approvals.answer(text(b["id"]), b["decision"])}
|
|
538
615
|
|
|
539
616
|
route "GET /w/:slug/api/approvals" requires auth
|
|
617
|
+
private
|
|
540
618
|
give {"approvals": approvals.list(), "public_url": approvals.public_url(), "webhook": approvals.webhook_url() != ""}
|
|
541
619
|
|
|
542
620
|
-- link de decisión (PÚBLICO a propósito: lo abre el humano desde el teléfono; el token de un solo uso es la
|
|
543
621
|
-- autorización, como en el `approve` nativo de Synsema bajo serve). GET porque un link en un chat es un GET.
|
|
544
622
|
route "GET /w/:slug/approve/:id/:token"
|
|
623
|
+
private
|
|
545
624
|
let d be when contains(query, "d") then lower(text(query.d)) otherwise ""
|
|
546
625
|
when d != "yes" and d != "no"
|
|
547
626
|
give html(approvals.html_page("¿Permitir?", "<a href='?d=yes' style='display:inline-block;padding:10px 18px;border-radius:4px;background:#1d1c1a;color:#fff;text-decoration:none;margin-right:10px'>Sí, permitir</a> <a href='?d=no' style='display:inline-block;padding:10px 18px;border-radius:4px;border:1px solid #a33;color:#a33;text-decoration:none'>No, denegar</a>"))
|
|
@@ -552,11 +631,13 @@ serve on 8080
|
|
|
552
631
|
|
|
553
632
|
-- tareas programadas (lib/schedule.syn): listar, crear, quitar, encender/apagar, correr ahora, log
|
|
554
633
|
route "GET /w/:slug/api/schedules" requires auth
|
|
634
|
+
private
|
|
555
635
|
let age be schedule.daemon_age()
|
|
556
636
|
-- este proceso tiene su propio tick (cron_every): las tareas corren mientras la web esté abierta
|
|
557
637
|
give {"tasks": schedule.summary(), "tick": schedule.TICK_SECONDS, "alive": true, "heartbeat_age": age, "mount_ok": schedule.mount_ok(), "workspace": env("LAMPSON_WORKSPACE", ""), "tz": schedule.tz_offset(), "plugins": plugins.summary()}
|
|
558
638
|
|
|
559
639
|
route "POST /w/:slug/api/schedules/add" requires auth
|
|
640
|
+
private
|
|
560
641
|
let b be json of request
|
|
561
642
|
try
|
|
562
643
|
let t be schedule.add(b)
|
|
@@ -565,6 +646,7 @@ serve on 8080
|
|
|
565
646
|
give fail(400, text(err))
|
|
566
647
|
|
|
567
648
|
route "POST /w/:slug/api/schedules/remove" requires auth
|
|
649
|
+
private
|
|
568
650
|
expect body {id: text}
|
|
569
651
|
let b be json of request
|
|
570
652
|
try
|
|
@@ -573,6 +655,7 @@ serve on 8080
|
|
|
573
655
|
give fail(400, text(err))
|
|
574
656
|
|
|
575
657
|
route "POST /w/:slug/api/schedules/toggle" requires auth
|
|
658
|
+
private
|
|
576
659
|
expect body {id: text}
|
|
577
660
|
let b be json of request
|
|
578
661
|
let on be contains(b, "enabled") and b["enabled"] == true
|
|
@@ -584,6 +667,7 @@ serve on 8080
|
|
|
584
667
|
-- correr ahora: la marca run_now la levanta el próximo tick (así el request no se queda minutos abierto
|
|
585
668
|
-- y la corrida vive en el hilo del cron, no en el del request)
|
|
586
669
|
route "POST /w/:slug/api/schedules/run" requires auth
|
|
670
|
+
private
|
|
587
671
|
expect body {id: text}
|
|
588
672
|
let b be json of request
|
|
589
673
|
try
|
|
@@ -593,12 +677,14 @@ serve on 8080
|
|
|
593
677
|
give fail(400, text(err))
|
|
594
678
|
|
|
595
679
|
route "GET /w/:slug/api/schedules/log" requires auth
|
|
680
|
+
private
|
|
596
681
|
when not contains(query, "id")
|
|
597
682
|
give fail(400, "id required")
|
|
598
683
|
let n be when contains(query, "tail") then floor(number(query.tail)) otherwise 300
|
|
599
684
|
give {"id": query.id, "log": schedule.log_tail(query.id, n), "task": schedule.get(query.id)}
|
|
600
685
|
|
|
601
686
|
route "POST /w/:slug/api/chat" requires auth
|
|
687
|
+
private
|
|
602
688
|
expect body {message: text}
|
|
603
689
|
let b be json of request
|
|
604
690
|
let message be b["message"]
|
|
@@ -611,8 +697,8 @@ serve on 8080
|
|
|
611
697
|
let pmodel be when contains(b, "model") then text(b["model"]) otherwise ""
|
|
612
698
|
state_set("lampson:last_used", now())
|
|
613
699
|
stream
|
|
700
|
+
-- el runtime (lib/runtime.syn) ya traza cada evento en .lampson/trace/<sid>.log: acá solo se reenvían por SSE
|
|
614
701
|
task emit(kind, data, tag)
|
|
615
|
-
trace.event(sid, kind, data, tag)
|
|
616
702
|
send {"kind": kind, "data": data, "tag": tag} as "event"
|
|
617
703
|
-- aprobación humana: la cola de lib/approvals.syn (id + token) — la UI responde por SSE/POST /api/approve,
|
|
618
704
|
-- y si hay webhook/LAMPSON_PUBLIC_URL también llega un link al canal del usuario (aprobar desde el móvil)
|
|
@@ -623,38 +709,30 @@ serve on 8080
|
|
|
623
709
|
let ok be approvals.wait(id, APPROVAL_TIMEOUT)
|
|
624
710
|
send {"kind": "approval_result", "data": {"id": id, "approved": ok, "timeout": (not ok) and now() - t0 >= APPROVAL_TIMEOUT - 1}} as "event"
|
|
625
711
|
give ok
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
when session.from_other_run(prev_doc)
|
|
636
|
-
let note be proc.resume_note(sid)
|
|
637
|
-
when note != ""
|
|
638
|
-
set messages to append(messages, {"role": "user", "content": note})
|
|
639
|
-
trace.event(sid, "inbox", note, "")
|
|
640
|
-
send {"kind": "inbox", "data": note, "tag": ""} as "event"
|
|
641
|
-
otherwise
|
|
642
|
-
set sid to ""
|
|
643
|
-
when sid == ""
|
|
644
|
-
set sid to session.new_id()
|
|
645
|
-
-- sesión actual (blackboard): la tool todo guarda su lista por sesión (lib/tools/todo.syn)
|
|
646
|
-
share {"id": sid} as "lampson:session"
|
|
647
|
-
send {"kind": "session", "data": sid} as "event"
|
|
648
|
-
send {"kind": "mode", "data": {"agent": ctx["profile"], "permission": mode, "provider": ctx["cfg"]["provider"], "model": ctx["cfg"]["model"], "has_key": ctx["cfg"]["has_key"]}} as "event"
|
|
712
|
+
task on_start(info)
|
|
713
|
+
set sid to info["session"]
|
|
714
|
+
send {"kind": "session", "data": sid} as "event"
|
|
715
|
+
send {"kind": "mode", "data": {"agent": info["agent"], "permission": info["mode"], "provider": info["provider"], "model": info["model"], "has_key": info["has_key"]}} as "event"
|
|
716
|
+
-- worker es interno de delegate: por la API tampoco se elige como agente de sesión
|
|
717
|
+
when not contains(runtime.names(), want)
|
|
718
|
+
set want to "build"
|
|
719
|
+
let sobre be {"to": want, "session": sid, "from": {"id": "", "name": "", "kind": "human"}, "text": message, "images": [], "channel": "web", "meta": {}}
|
|
720
|
+
let hooks be {"mode": mode, "provider": pname, "model": pmodel, "ask_fn": ask_web, "on_event": emit, "on_start": on_start}
|
|
649
721
|
when starts_with(trim(message), "!")
|
|
650
722
|
-- comando del USUARIO: sin política de permisos, salida al contexto; el modelo no interviene
|
|
723
|
+
let o be runtime.open(sobre, hooks)
|
|
724
|
+
let cfg be o["ctx"]["cfg"]
|
|
725
|
+
on_start({"session": o["session"], "agent": o["ctx"]["agent"], "thread": o["thread"], "mode": o["mode"], "provider": cfg["provider"], "model": cfg["model"], "has_key": cfg["has_key"]})
|
|
726
|
+
when o["note"] != ""
|
|
727
|
+
trace.event(sid, "inbox", o["note"], "")
|
|
728
|
+
send {"kind": "inbox", "data": o["note"], "tag": ""} as "event"
|
|
651
729
|
let cmd be trim(slice(trim(message), 1, length(trim(message))))
|
|
652
730
|
let bash_tool be agents.registry_for("build")["bash"]
|
|
653
731
|
send {"kind": "tool_call", "data": {"id": "user", "name": "bash", "args": {"command": cmd}}} as "event"
|
|
654
732
|
let out be call_tool(bash_tool, {"command": cmd, "timeout": 600})
|
|
655
733
|
send {"kind": "tool_result", "data": {"call": {"id": "user", "name": "bash", "args": {"command": cmd}}, "output": out}} as "event"
|
|
656
|
-
|
|
657
|
-
|
|
734
|
+
let messages be append(o["messages"], {"role": "user", "content": "[The user ran this command in the shell themselves]\n$ " + cmd + "\n" + tools.truncate(out, 6000)})
|
|
735
|
+
runtime.save(o, messages)
|
|
658
736
|
send {"kind": "done", "data": {"text": "", "steps": 0, "usage": {"input": 0, "output": 0}, "stopped": "user_command", "session": sid}} as "event"
|
|
659
737
|
otherwise
|
|
660
738
|
-- imágenes pegadas en la UI: [{media_type, data(base64)}] — el cliente ya las redujo; tope 5 MB por mensaje
|
|
@@ -669,12 +747,315 @@ serve on 8080
|
|
|
669
747
|
when contains(im, "w") and contains(im, "h")
|
|
670
748
|
set entry to {"media_type": text(im["media_type"]), "data": text(im["data"]), "w": floor(number(im["w"])), "h": floor(number(im["h"]))}
|
|
671
749
|
set images to append(images, entry)
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
750
|
+
set sobre["images"] to images
|
|
751
|
+
let result be runtime.deliver(sobre, hooks)
|
|
752
|
+
send {"kind": "done", "data": {"text": result["text"], "steps": result["steps"], "usage": result["usage"], "stopped": result["stopped"], "session": result["session"]}} as "event"
|
|
753
|
+
|
|
754
|
+
-- ---------- API /v1 para apps (lib/api.syn · spec/engine.md fase 2) ----------
|
|
755
|
+
-- Sin `requires auth`: bajo el hub todo llega por loopback y check_client lo daría por bueno. La API exige SIEMPRE
|
|
756
|
+
-- una API key (Authorization: Bearer lk_…), que valida api.authorize.
|
|
757
|
+
-- el OpenAPI de la API pública (solo /v1: el resto lleva `private`), por la ruta /v1 que el hub sí reenvía
|
|
758
|
+
route "GET /w/:slug/v1/openapi.json"
|
|
759
|
+
private
|
|
760
|
+
give respond(openapi_json(), "application/json")
|
|
761
|
+
|
|
762
|
+
route "GET /w/:slug/v1/agents" requires auth
|
|
763
|
+
let a be api.authorize(headers of request)
|
|
764
|
+
when not a["ok"]
|
|
765
|
+
give api.refuse(a)
|
|
766
|
+
give {"agents": api.agents_for(a["key"])}
|
|
767
|
+
|
|
768
|
+
route "POST /w/:slug/v1/messages" requires auth
|
|
769
|
+
expect body {text: text}
|
|
770
|
+
let a be api.authorize(headers of request)
|
|
771
|
+
when not a["ok"]
|
|
772
|
+
give api.refuse(a)
|
|
773
|
+
let p be api.parse(json of request, a["key"])
|
|
774
|
+
when not p["ok"]
|
|
775
|
+
give fail(p["status"], p["error"])
|
|
776
|
+
state_set("lampson:last_used", now())
|
|
777
|
+
-- todo pasa por el buzón de la conversación (lib/mailbox.syn): fila, espera de silencio, mitad de turno
|
|
778
|
+
let mo be api.mailbox_opts(p["sobre"])
|
|
779
|
+
when p["callback"] != ""
|
|
780
|
+
let jid be mailbox.new_id()
|
|
781
|
+
api.create_job(jid, a["key"], p["sobre"], p["callback"])
|
|
782
|
+
mailbox.submit(p["sobre"], merge(mo, {"id": jid, "callback": p["callback"], "job": true}))
|
|
783
|
+
give respond(json_encode({"job": jid, "status": "queued"}), "application/json", 202)
|
|
784
|
+
let s be mailbox.submit(p["sobre"], mo)
|
|
785
|
+
give mailbox.wait(s["id"])
|
|
786
|
+
|
|
787
|
+
route "POST /w/:slug/v1/messages/stream" requires auth
|
|
788
|
+
expect body {text: text}
|
|
789
|
+
let a be api.authorize(headers of request)
|
|
790
|
+
when not a["ok"]
|
|
791
|
+
give api.refuse(a)
|
|
792
|
+
let p be api.parse(json of request, a["key"])
|
|
793
|
+
when not p["ok"]
|
|
794
|
+
give fail(p["status"], p["error"])
|
|
795
|
+
state_set("lampson:last_used", now())
|
|
796
|
+
let sobre be p["sobre"]
|
|
797
|
+
let mo be api.mailbox_opts(sobre)
|
|
798
|
+
-- suscripto ANTES de encolar: el worker publica en el bus los eventos de la conversación
|
|
799
|
+
let mid be mailbox.new_id()
|
|
800
|
+
let sub be bus_subscribe("lampson.mb." + mailbox.key_of(sobre, mid))
|
|
801
|
+
mailbox.submit(sobre, merge(mo, {"id": mid}))
|
|
802
|
+
stream
|
|
803
|
+
let finished be false
|
|
804
|
+
let deadline be now() + mailbox.WAIT_TIMEOUT
|
|
805
|
+
while not finished and now() < deadline
|
|
806
|
+
let ev be bus_recv(sub, 1)
|
|
807
|
+
when ev != nothing
|
|
808
|
+
let d be ev["data"]
|
|
809
|
+
when d["kind"] == "done"
|
|
810
|
+
-- el done de un lote anterior de la misma conversación no es el nuestro
|
|
811
|
+
when contains(d["data"]["batch"], mid)
|
|
812
|
+
state_delete("mb:res:" + mid)
|
|
813
|
+
send d as "event"
|
|
814
|
+
set finished to true
|
|
815
|
+
otherwise
|
|
816
|
+
send d as "event"
|
|
817
|
+
-- por si el done pasó mientras no estábamos escuchando
|
|
818
|
+
when not finished
|
|
819
|
+
let r be state_get("mb:res:" + mid, nothing)
|
|
820
|
+
when r != nothing
|
|
821
|
+
state_delete("mb:res:" + mid)
|
|
822
|
+
send {"kind": "done", "data": r, "tag": "", "agent": r["agent"], "thread": r["thread"]} as "event"
|
|
823
|
+
set finished to true
|
|
824
|
+
bus_unsubscribe(sub)
|
|
825
|
+
|
|
826
|
+
-- MCP por HTTP (lib/mcp_server.syn, "Streamable HTTP" con respuestas JSON): Claude, Cursor u otro agente usan los
|
|
827
|
+
-- agentes de este workspace desde cualquier lado, con la API key. Una notificación → 202 sin cuerpo.
|
|
828
|
+
route "POST /w/:slug/v1/mcp" requires auth
|
|
829
|
+
expect body {jsonrpc: text, method: text}
|
|
830
|
+
let a be api.authorize(headers of request)
|
|
831
|
+
when not a["ok"]
|
|
832
|
+
give with_header(fail(a["status"], a["error"]), "WWW-Authenticate", "Bearer")
|
|
833
|
+
state_set("lampson:last_used", now())
|
|
834
|
+
let offered be apply((x) => x["id"], api.agents_for(a["key"]))
|
|
835
|
+
when length(offered) == 0
|
|
836
|
+
give fail(403, "this API key has no agents")
|
|
837
|
+
let resp be mcp_server.handle(json of request, offered, api.mcp_deliver)
|
|
838
|
+
when resp == nothing
|
|
839
|
+
give respond("", "application/json", 202)
|
|
840
|
+
give resp
|
|
841
|
+
|
|
842
|
+
-- la misma conversación MCP pero con respuestas SSE: si el pedido trae params._meta.progressToken, cada paso del
|
|
843
|
+
-- agente sale como notifications/progress antes de la respuesta. Ruta aparte porque en Synsema una ruta transmite
|
|
844
|
+
-- o no (un `stream` dentro de un `when` no transmite) y /v1/mcp debe contestar 202 vacío a las notificaciones.
|
|
845
|
+
route "POST /w/:slug/v1/mcp/stream" requires auth
|
|
846
|
+
expect body {jsonrpc: text, method: text}
|
|
847
|
+
let a be api.authorize(headers of request)
|
|
848
|
+
let offered be when a["ok"] then apply((x) => x["id"], api.agents_for(a["key"])) otherwise []
|
|
849
|
+
let req be json of request
|
|
850
|
+
let ptoken be nothing
|
|
851
|
+
when contains(req, "params") and type_of(req["params"]) == "map" and contains(req["params"], "_meta") and type_of(req["params"]["_meta"]) == "map" and contains(req["params"]["_meta"], "progressToken")
|
|
852
|
+
set ptoken to req["params"]["_meta"]["progressToken"]
|
|
853
|
+
stream
|
|
854
|
+
-- un ask por el buzón, mirando el bus de la conversación para el progreso
|
|
855
|
+
task deliver_sse(sobre, mode)
|
|
856
|
+
let mid be mailbox.new_id()
|
|
857
|
+
let sub be bus_subscribe("lampson.mb." + mailbox.key_of(sobre, mid))
|
|
858
|
+
mailbox.submit(sobre, {"mode": mode, "buffer": mailbox.buffer_of(runtime.get(sobre["to"]), false), "id": mid})
|
|
859
|
+
let n be 0
|
|
860
|
+
let result be nothing
|
|
861
|
+
let deadline be now() + mailbox.WAIT_TIMEOUT
|
|
862
|
+
while result == nothing and now() < deadline
|
|
863
|
+
let ev be bus_recv(sub, 1)
|
|
864
|
+
when ev != nothing
|
|
865
|
+
let d be ev["data"]
|
|
866
|
+
when d["kind"] == "done" and contains(d["data"]["batch"], mid)
|
|
867
|
+
set result to d["data"]
|
|
868
|
+
otherwise when ptoken != nothing and (d["kind"] == "assistant" or d["kind"] == "tool_call")
|
|
869
|
+
set n to n + 1
|
|
870
|
+
let what be when d["kind"] == "tool_call" then "using " + text(d["data"]["name"]) otherwise slice(text(d["data"]), 0, 200)
|
|
871
|
+
send {"jsonrpc": "2.0", "method": "notifications/progress", "params": {"progressToken": ptoken, "progress": n, "message": what}} as "message"
|
|
872
|
+
when result == nothing
|
|
873
|
+
let r be state_get("mb:res:" + mid, nothing)
|
|
874
|
+
when r != nothing
|
|
875
|
+
set result to r
|
|
876
|
+
state_delete("mb:res:" + mid)
|
|
877
|
+
bus_unsubscribe(sub)
|
|
878
|
+
give when result == nothing then {"agent": sobre["to"], "thread": sobre["thread"], "session": "", "text": "", "steps": 0, "usage": {"input": 0, "output": 0, "cached": 0}, "stopped": "error: timed out"} otherwise result
|
|
879
|
+
when not a["ok"]
|
|
880
|
+
send {"jsonrpc": "2.0", "id": nothing, "error": {"code": -32001, "message": a["error"]}} as "message"
|
|
881
|
+
otherwise when length(offered) == 0
|
|
882
|
+
send {"jsonrpc": "2.0", "id": nothing, "error": {"code": -32001, "message": "this API key has no agents"}} as "message"
|
|
883
|
+
otherwise
|
|
884
|
+
state_set("lampson:last_used", now())
|
|
885
|
+
let resp be mcp_server.handle(req, offered, deliver_sse)
|
|
886
|
+
when resp != nothing
|
|
887
|
+
send resp as "message"
|
|
888
|
+
|
|
889
|
+
route "GET /w/:slug/v1/mcp"
|
|
890
|
+
give fail(405, "this MCP server does not open a server-to-client stream; POST JSON-RPC messages")
|
|
891
|
+
|
|
892
|
+
-- ---------- panel de la UI: API keys y canales (privadas: son de la UI, autenticadas por el hub/local) ----------
|
|
893
|
+
-- ---------- EQUIPOS Y OBJETIVOS: su pantalla propia (public/teams.html) y la API privada que la alimenta ----------
|
|
894
|
+
route "GET /w/:slug/equipos"
|
|
895
|
+
private
|
|
896
|
+
let h be headers of request
|
|
897
|
+
when env("LAMPSON_WS", "") != "" and not contains(h, "x-forwarded-host")
|
|
898
|
+
give redirect("http://127.0.0.1:8080/w/" + params.slug + "/equipos")
|
|
899
|
+
give html(read_file("public/teams.html"))
|
|
900
|
+
|
|
901
|
+
route "GET /w/:slug/api/objectives" requires auth
|
|
902
|
+
private
|
|
903
|
+
let rows be apply(teams.obj_list(), (o) => {"id": o["id"], "ref": "O-" + text(o["id"]), "agent": o["agent"], "team": o["team"], "text": o["text"], "status": o["status"], "progress": o["progress"], "question": o["question"], "question_kind": o["question_kind"], "turns": o["turns"], "spent": o["spent"], "budget_tokens": o["budget_tokens"], "updated_at": o["updated_at"], "working": mailbox.working(o["agent"], teams.obj_scope(o))})
|
|
904
|
+
give {"objectives": rows, "teams": apply(teams.all(), (t) => {"id": t["id"], "lead": teams.lead_of(t), "description": t["description"]}), "agents": apply(where(runtime.names(), (n) => not runtime.get(n)["builtin"]), (n) => {"id": n, "description": runtime.get(n)["description"]})}
|
|
905
|
+
|
|
906
|
+
route "GET /w/:slug/api/objectives/:id" requires auth
|
|
907
|
+
private
|
|
908
|
+
let o be teams.obj_get(floor(number(params.id)))
|
|
909
|
+
when o == nothing
|
|
910
|
+
give fail(404, "no such objective")
|
|
911
|
+
let v be api.objective_view(o)
|
|
912
|
+
set v["working"] to mailbox.working(o["agent"], teams.obj_scope(o))
|
|
913
|
+
give v
|
|
914
|
+
|
|
915
|
+
route "POST /w/:slug/api/objectives/add" requires auth
|
|
916
|
+
private
|
|
917
|
+
expect body {text: text}
|
|
918
|
+
let c be api.create_objective(json of request, {"name": "person", "agents": ["*"]})
|
|
919
|
+
when not c["ok"]
|
|
920
|
+
give fail(c["status"], c["error"])
|
|
921
|
+
give api.objective_view(teams.obj_get(c["id"]))
|
|
922
|
+
|
|
923
|
+
route "POST /w/:slug/api/objectives/:id/reply" requires auth
|
|
924
|
+
private
|
|
925
|
+
let b be json of request
|
|
926
|
+
try
|
|
927
|
+
give api.objective_view(teams.obj_reply(floor(number(params.id)), when contains(b, "text") and b["text"] != nothing then text(b["text"]) otherwise "", contains(b, "approved") and b["approved"] == true, "person"))
|
|
928
|
+
recover err
|
|
929
|
+
give fail(409, regex_replace(text(err), "^.*?:\\d+:\\d+: ", ""))
|
|
930
|
+
|
|
931
|
+
route "POST /w/:slug/api/objectives/:id/cancel" requires auth
|
|
932
|
+
private
|
|
933
|
+
give api.objective_view(teams.obj_cancel(floor(number(params.id)), "person"))
|
|
934
|
+
|
|
935
|
+
route "GET /w/:slug/api/teams" requires auth
|
|
936
|
+
private
|
|
937
|
+
give api.teams_view()
|
|
938
|
+
|
|
939
|
+
route "POST /w/:slug/api/teams/save" requires auth
|
|
940
|
+
private
|
|
941
|
+
let r be api.save_team(json of request)
|
|
942
|
+
when not r["ok"]
|
|
943
|
+
give fail(400, r["error"])
|
|
944
|
+
give api.teams_view()
|
|
945
|
+
|
|
946
|
+
route "GET /w/:slug/api/keys" requires auth
|
|
947
|
+
private
|
|
948
|
+
let pub be approvals.public_url()
|
|
949
|
+
let base be (when pub != "" then pub otherwise "http://127.0.0.1:8080") + "/w/" + env("LAMPSON_WS", "local") + "/v1"
|
|
950
|
+
give {"keys": apikeys.list(), "api_url": base + "/messages", "mcp_url": base + "/mcp", "public": pub != "", "agents": runtime.names()}
|
|
951
|
+
|
|
952
|
+
route "POST /w/:slug/api/keys/add" requires auth
|
|
953
|
+
private
|
|
954
|
+
let b be json of request
|
|
955
|
+
let ag be when b != nothing and contains(b, "agents") and type_of(b["agents"]) == "list" then b["agents"] otherwise []
|
|
956
|
+
each a in ag
|
|
957
|
+
when a != "*" and not contains(runtime.names(), text(a))
|
|
958
|
+
give fail(400, "no agent named '" + text(a) + "'")
|
|
959
|
+
let cbs be when b != nothing and contains(b, "callbacks") and type_of(b["callbacks"]) == "list" then b["callbacks"] otherwise []
|
|
960
|
+
each cb in cbs
|
|
961
|
+
when not (starts_with(text(cb), "https://") or starts_with(text(cb), "http://"))
|
|
962
|
+
give fail(400, "a callback prefix must be an http(s) URL")
|
|
963
|
+
let ko be {"callbacks": cbs}
|
|
964
|
+
when b != nothing and contains(b, "rate") and b["rate"] != nothing and text(b["rate"]) != ""
|
|
965
|
+
set ko["rate"] to floor(number(b["rate"]))
|
|
966
|
+
give apikeys.create_with(when b != nothing and contains(b, "name") then text(b["name"]) otherwise "", ag, ko)
|
|
967
|
+
|
|
968
|
+
route "POST /w/:slug/api/keys/remove" requires auth
|
|
969
|
+
private
|
|
970
|
+
expect body {id: text}
|
|
971
|
+
give {"ok": apikeys.remove(text((json of request)["id"]))}
|
|
972
|
+
|
|
973
|
+
route "GET /w/:slug/api/channels" requires auth
|
|
974
|
+
private
|
|
975
|
+
give {"channels": channels.summary(approvals.public_url(), env("LAMPSON_WS", "local")), "public": approvals.public_url() != ""}
|
|
976
|
+
|
|
977
|
+
route "POST /w/:slug/api/channels/toggle" requires auth
|
|
978
|
+
private
|
|
979
|
+
expect body {name: text, on: bool}
|
|
980
|
+
let b be json of request
|
|
981
|
+
give {"ok": plugins.set_enabled(text(b["name"]), b["on"])}
|
|
982
|
+
|
|
983
|
+
route "POST /w/:slug/api/channels/setup" requires auth
|
|
984
|
+
private
|
|
985
|
+
expect body {name: text}
|
|
986
|
+
give channels.setup_channel(text((json of request)["name"]), approvals.public_url(), env("LAMPSON_WS", "local"))
|
|
987
|
+
|
|
988
|
+
-- ---------- canales (lib/channels.syn · spec/engine.md fase 4) ----------
|
|
989
|
+
-- El webhook de la plataforma. Sin auth de Lampson: la firma de la plataforma la verifica el plugin en `receive`
|
|
990
|
+
-- (recibe el body exacto). GET para las verificaciones de webhook (Meta, Slack…), que el plugin contesta con "http".
|
|
991
|
+
route "POST /w/:slug/ch/:name"
|
|
992
|
+
private
|
|
993
|
+
state_set("lampson:last_used", now())
|
|
994
|
+
let r be channels.handle(params.name, {"method": "POST", "path": request.path, "query": query, "headers": headers of request, "body": read_body()})
|
|
995
|
+
give respond(r["body"], r["ctype"], r["status"])
|
|
996
|
+
|
|
997
|
+
route "GET /w/:slug/ch/:name"
|
|
998
|
+
private
|
|
999
|
+
let r be channels.handle(params.name, {"method": "GET", "path": request.path, "query": query, "headers": headers of request, "body": ""})
|
|
1000
|
+
give respond(r["body"], r["ctype"], r["status"])
|
|
1001
|
+
|
|
1002
|
+
-- OBJETIVOS (lib/teams.syn): lo que le pedís a un agente para que trabaje solo hasta terminarlo
|
|
1003
|
+
route "POST /w/:slug/v1/objectives" requires auth
|
|
1004
|
+
expect body {text: text}
|
|
1005
|
+
let a be api.authorize(headers of request)
|
|
1006
|
+
when not a["ok"]
|
|
1007
|
+
give api.refuse(a)
|
|
1008
|
+
let c be api.create_objective(json of request, a["key"])
|
|
1009
|
+
when not c["ok"]
|
|
1010
|
+
give fail(c["status"], c["error"])
|
|
1011
|
+
state_set("lampson:last_used", now())
|
|
1012
|
+
give respond(json_encode(api.objective_view(teams.obj_get(c["id"]))), "application/json", 201)
|
|
1013
|
+
|
|
1014
|
+
route "GET /w/:slug/v1/objectives" requires auth
|
|
1015
|
+
let a be api.authorize(headers of request)
|
|
1016
|
+
when not a["ok"]
|
|
1017
|
+
give api.refuse(a)
|
|
1018
|
+
let mine be where(teams.obj_list(), (o) => apikeys.allows(a["key"], o["agent"]))
|
|
1019
|
+
give {"objectives": apply(mine, (o) => {"id": o["id"], "ref": "O-" + text(o["id"]), "agent": o["agent"], "text": o["text"], "status": o["status"], "progress": o["progress"], "question": o["question"], "turns": o["turns"], "spent": o["spent"], "updated_at": o["updated_at"]})}
|
|
1020
|
+
|
|
1021
|
+
route "GET /w/:slug/v1/objectives/:id" requires auth
|
|
1022
|
+
let a be api.authorize(headers of request)
|
|
1023
|
+
when not a["ok"]
|
|
1024
|
+
give api.refuse(a)
|
|
1025
|
+
let o be api.objective_for(params.id, a["key"])
|
|
1026
|
+
when o == nothing
|
|
1027
|
+
give fail(404, "no such objective")
|
|
1028
|
+
give api.objective_view(o)
|
|
1029
|
+
|
|
1030
|
+
route "POST /w/:slug/v1/objectives/:id/reply" requires auth
|
|
1031
|
+
let a be api.authorize(headers of request)
|
|
1032
|
+
when not a["ok"]
|
|
1033
|
+
give api.refuse(a)
|
|
1034
|
+
let o be api.objective_for(params.id, a["key"])
|
|
1035
|
+
when o == nothing
|
|
1036
|
+
give fail(404, "no such objective")
|
|
1037
|
+
let b be json of request
|
|
1038
|
+
let txt be when contains(b, "text") and b["text"] != nothing then text(b["text"]) otherwise ""
|
|
1039
|
+
let ok be contains(b, "approved") and b["approved"] == true
|
|
1040
|
+
try
|
|
1041
|
+
give api.objective_view(teams.obj_reply(o["id"], txt, ok, "api:" + a["key"]["name"]))
|
|
1042
|
+
recover err
|
|
1043
|
+
give fail(409, regex_replace(text(err), "^.*?:\\d+:\\d+: ", ""))
|
|
1044
|
+
|
|
1045
|
+
route "POST /w/:slug/v1/objectives/:id/cancel" requires auth
|
|
1046
|
+
let a be api.authorize(headers of request)
|
|
1047
|
+
when not a["ok"]
|
|
1048
|
+
give api.refuse(a)
|
|
1049
|
+
let o be api.objective_for(params.id, a["key"])
|
|
1050
|
+
when o == nothing
|
|
1051
|
+
give fail(404, "no such objective")
|
|
1052
|
+
give api.objective_view(teams.obj_cancel(o["id"], "api:" + a["key"]["name"]))
|
|
1053
|
+
|
|
1054
|
+
route "GET /w/:slug/v1/jobs/:id" requires auth
|
|
1055
|
+
let a be api.authorize(headers of request)
|
|
1056
|
+
when not a["ok"]
|
|
1057
|
+
give api.refuse(a)
|
|
1058
|
+
let j be api.job(params.id)
|
|
1059
|
+
when j == nothing or j["key"] != a["key"]["id"]
|
|
1060
|
+
give fail(404, "no such job")
|
|
1061
|
+
give j
|