lampson 0.3.0 → 0.4.1

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/run.syn CHANGED
@@ -24,6 +24,7 @@ require random
24
24
  require env("LAMPSON_*")
25
25
  require env("OS")
26
26
  require secret("LAMPSON_*")
27
+ require db(".lampson/teams.db")
27
28
  require judge
28
29
  require file("workspace")
29
30
  require file("workspace/*")
@@ -38,6 +39,7 @@ require file("memory/*")
38
39
  require file(".lampson")
39
40
  require file(".lampson/*")
40
41
 
42
+ use "./lib/teams.syn" as teams
41
43
  use "./lib/runtime.syn" as runtime
42
44
  use "./lib/agents.syn" as agents
43
45
  use "./lib/mcp.syn" as mcp
@@ -104,6 +106,103 @@ when key_cmd != ""
104
106
  exit(2)
105
107
  exit(0)
106
108
 
109
+ -- `lampson objective add|list|show|reply|cancel` y `lampson team list` (lib/teams.syn): sin servidor, directo a la base
110
+ -- del workspace; el pulso del proceso del workspace (lib/teamrun.syn) los hace avanzar
111
+ -- LAMPSON_OBJ_CMD=add LAMPSON_MESSAGE=<texto> [LAMPSON_OBJ_AGENT | LAMPSON_OBJ_TEAM] [LAMPSON_OBJ_BUDGET] [LAMPSON_OBJ_ROUND]
112
+ -- list · show/cancel LAMPSON_OBJ_ID · reply LAMPSON_OBJ_ID LAMPSON_MESSAGE [LAMPSON_OBJ_DECISION=approve|deny]
113
+ let obj_cmd be env("LAMPSON_OBJ_CMD", "")
114
+ task obj_line(o)
115
+ give " " + "O-" + text(o["id"]) + " " + o["status"] + " " + (when o["team"] != "" then "equipo " + o["team"] + " (" + o["agent"] + ")" otherwise o["agent"]) + " · " + slice(o["text"], 0, 70) + (when length(o["text"]) > 70 then "…" otherwise "")
116
+ when obj_cmd != ""
117
+ let oid be floor(number(when matches(env("LAMPSON_OBJ_ID", ""), "[Oo]?-?[0-9]+") then regex_replace(env("LAMPSON_OBJ_ID", ""), "[^0-9]", "") otherwise "0"))
118
+ when obj_cmd == "add"
119
+ let txt be trim(env("LAMPSON_MESSAGE", ""))
120
+ let tid be lower(trim(env("LAMPSON_OBJ_TEAM", "")))
121
+ let ag be lower(trim(env("LAMPSON_OBJ_AGENT", "")))
122
+ when txt == ""
123
+ print("uso: lampson objective add \"qué tiene que lograr\" --agent ID | --team ID [--budget TOKENS] [--round SEGUNDOS]")
124
+ exit(2)
125
+ when tid != ""
126
+ let tm be teams.get(tid)
127
+ when tm == nothing
128
+ print("lampson objective: no hay un equipo '" + tid + "' · equipos: " + join(apply(teams.all(), (t) => t["id"]), ", "))
129
+ exit(2)
130
+ when ag == ""
131
+ set ag to teams.lead_of(tm)
132
+ when teams.member(tm, ag) == nothing
133
+ print("lampson objective: " + ag + " no está en el equipo " + tid)
134
+ exit(2)
135
+ when ag == "" or runtime.get(ag) == nothing
136
+ print("lampson objective: decí qué agente (--agent ID) o qué equipo (--team ID) · agentes: " + join(runtime.names(), ", "))
137
+ exit(2)
138
+ let oopts be {"created_by": "cli", "team": tid}
139
+ when env("LAMPSON_OBJ_BUDGET", "") != ""
140
+ set oopts["budget_tokens"] to int(env("LAMPSON_OBJ_BUDGET", "0"))
141
+ when env("LAMPSON_OBJ_ROUND", "") != ""
142
+ set oopts["round_seconds"] to int(env("LAMPSON_OBJ_ROUND", "20"))
143
+ let nid be teams.obj_create(ag, txt, oopts)
144
+ print("objetivo O-" + text(nid) + " creado para " + (when tid != "" then "el equipo " + tid + " (lo lidera " + ag + ")" otherwise ag) + ". Arranca en segundos si el workspace está prendido (el hub lo mantiene) · lampson objective show " + text(nid) + " · pantalla: http://127.0.0.1:8080/w/" + env("LAMPSON_WS", "<slug>") + "/equipos?o=" + text(nid))
145
+ exit(0)
146
+ when obj_cmd == "list"
147
+ let all_o be teams.obj_list()
148
+ when length(all_o) == 0
149
+ print("sin objetivos en este workspace (lampson objective add \"…\" --agent ID | --team ID)")
150
+ each o in all_o
151
+ print(obj_line(o))
152
+ exit(0)
153
+ let o be teams.obj_get(oid)
154
+ when o == nothing
155
+ print("lampson objective: no existe O-" + text(oid) + " (lampson objective list)")
156
+ exit(2)
157
+ when obj_cmd == "show"
158
+ print(obj_line(o))
159
+ print(" turnos " + text(o["turns"]) + " · gastado " + text(o["spent"]) + (when o["budget_tokens"] > 0 then " de " + text(o["budget_tokens"]) otherwise "") + " tokens")
160
+ when o["question"] != ""
161
+ print(" " + (when o["question_kind"] == "approval" then "PIDE APROBACIÓN: " otherwise "PREGUNTA: ") + o["question"] + "\n → lampson objective reply " + text(oid) + (when o["question_kind"] == "approval" then " --approve | --deny [\"nota\"]" otherwise " \"tu respuesta\""))
162
+ when o["progress"] != ""
163
+ print(" último informe: " + o["progress"])
164
+ when o["result"] != ""
165
+ print(" resultado: " + o["result"])
166
+ let ts be teams.tasks(teams.obj_scope(o))
167
+ when length(ts) > 0
168
+ print(" tablero:")
169
+ each t in ts
170
+ print(" #" + text(t["id"]) + " [" + t["status"] + "] " + t["title"] + (when t["owner"] != "" then " — " + t["owner"] otherwise ""))
171
+ exit(0)
172
+ when obj_cmd == "reply"
173
+ let dec be lower(env("LAMPSON_OBJ_DECISION", ""))
174
+ when o["question_kind"] == "approval" and dec == ""
175
+ print("lampson objective: O-" + text(oid) + " pide aprobación — respondé con --approve o --deny (y una nota opcional)")
176
+ exit(2)
177
+ when o["question_kind"] != "approval" and trim(env("LAMPSON_MESSAGE", "")) == "" and dec == ""
178
+ print("lampson objective: escribí la respuesta: lampson objective reply " + text(oid) + " \"…\"")
179
+ exit(2)
180
+ try
181
+ teams.obj_reply(oid, env("LAMPSON_MESSAGE", ""), dec == "approve", "person")
182
+ print("respondido: O-" + text(oid) + " sigue trabajando")
183
+ recover err
184
+ print("lampson objective: " + regex_replace(text(err), "^.*?:\\d+:\\d+: ", ""))
185
+ exit(2)
186
+ exit(0)
187
+ when obj_cmd == "cancel"
188
+ teams.obj_cancel(oid, "person")
189
+ print("O-" + text(oid) + " cancelado")
190
+ exit(0)
191
+ print("uso: lampson objective add \"…\" --agent ID|--team ID · objective list · objective show <id> · objective reply <id> \"…\" [--approve|--deny] · objective cancel <id>")
192
+ exit(2)
193
+
194
+ when env("LAMPSON_TEAM_CMD", "") != ""
195
+ let sc be teams.scan()
196
+ when length(sc["teams"]) == 0
197
+ print("sin equipos en este workspace: un equipo es .lampson/teams/<id>.team.json (o creálo en la pantalla: lampson --web → equipos)")
198
+ each t in sc["teams"]
199
+ print(" " + t["id"] + (when t["description"] != "" then " — " + t["description"] otherwise "") + " · rondas " + (when t["round_minutes"] > 0 then "cada " + text(floor(t["round_minutes"])) + " min" otherwise "no") + " · tope " + (when t["budget_daily_tokens"] > 0 then text(t["budget_daily_tokens"]) + "/agente/día" otherwise "sin tope"))
200
+ each m in t["members"]
201
+ print(" " + m["agent"] + " " + m["role"] + (when m["reports_to"] != "" then " → reporta a " + m["reports_to"] otherwise " (lidera)") + (when runtime.get(m["agent"]) == nothing then " ⚠ sin archivo de agente" otherwise ""))
202
+ each e in sc["errors"]
203
+ print(" ✗ " + e)
204
+ exit(0)
205
+
107
206
  -- `lampson channel list|on|off|setup [canal]`: los canales (plugins con "channel", lib/channels.syn)
108
207
  let ch_cmd be env("LAMPSON_CHANNEL_CMD", "")
109
208
  when ch_cmd != ""
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/*")
@@ -75,6 +76,8 @@ use "./lib/runtime.syn" as runtime
75
76
  use "./lib/api.syn" as api
76
77
  use "./lib/apikeys.syn" as apikeys
77
78
  use "./lib/mailbox.syn" as mailbox
79
+ use "./lib/teamrun.syn" as teamrun
80
+ use "./lib/teams.syn" as teams
78
81
  use "./lib/channels.syn" as channels
79
82
  use "./lib/mcp_server.syn" as mcp_server
80
83
 
@@ -127,6 +130,11 @@ task channel_poll_tick()
127
130
  give channels.poll_tick()
128
131
  cron_every(5, channel_poll_tick)
129
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
+
130
138
  -- TAREAS PROGRAMADAS (lib/schedule.syn): este proceso es el residente de lampson — con `lampson --daemon start`
131
139
  -- (synsema daemon) queda en background y ejecuta lo programado aunque no haya ninguna UI abierta. Un tick cada
132
140
  -- 30 s (cron_every de Synsema: intervalo puro; la hora de reloj la resuelve schedule.syn) corre lo vencido:
@@ -156,7 +164,7 @@ serve on 8080
156
164
  describe
157
165
  about: "Lampson API — agents of this workspace for your apps, channels and other agents"
158
166
  version: "1.0.0"
159
- 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"]
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"]
160
168
 
161
169
  -- la UI vive bajo /w/<slug> (BASE en js/core.js); si alguien entra por el puerto del proceso, es lo mismo
162
170
  -- Acceso DIRECTO por el puerto del proceso (sin X-Forwarded-Host, que solo agrega el hub al proxyear): la UI se
@@ -882,6 +890,59 @@ serve on 8080
882
890
  give fail(405, "this MCP server does not open a server-to-client stream; POST JSON-RPC messages")
883
891
 
884
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
+
885
946
  route "GET /w/:slug/api/keys" requires auth
886
947
  private
887
948
  let pub be approvals.public_url()
@@ -938,6 +999,58 @@ serve on 8080
938
999
  let r be channels.handle(params.name, {"method": "GET", "path": request.path, "query": query, "headers": headers of request, "body": ""})
939
1000
  give respond(r["body"], r["ctype"], r["status"])
940
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
+
941
1054
  route "GET /w/:slug/v1/jobs/:id" requires auth
942
1055
  let a be api.authorize(headers of request)
943
1056
  when not a["ok"]