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/lib/channels.syn
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
-- lib/channels.syn — canales: WhatsApp, Telegram, Slack, Chatwoot… como PLUGINS (spec/engine.md, fase 4)
|
|
2
|
+
--
|
|
3
|
+
-- Lampson no sabe qué es ninguna plataforma. Define un contrato y cada canal es un plugin (cualquier lenguaje, o
|
|
4
|
+
-- Synsema con techo de capacidades) con "channel" en su plugin.json y dos acciones:
|
|
5
|
+
--
|
|
6
|
+
-- receive Lampson le pasa el webhook CRUDO de la plataforma (PLUGIN_ARGS):
|
|
7
|
+
-- {"method", "path", "query", "headers", "body"} body = el texto exacto (para verificar firmas)
|
|
8
|
+
-- y el plugin imprime por stdout UN JSON:
|
|
9
|
+
-- {"messages": [{"thread", "text", "sent_at"?, "from"?, "images"?, "meta"?, "agent"?}],
|
|
10
|
+
-- "http": {"status", "body", "content_type"}?} ← lo que Lampson le responde a la plataforma
|
|
11
|
+
-- Sin mensajes (un evento que no interesa, una verificación de webhook) → {"messages": []} o un "http".
|
|
12
|
+
-- send Lampson le pasa la respuesta del agente: {"thread", "text", "meta", "agent", "session", "batch"}
|
|
13
|
+
-- (thread y meta = los que devolvió receive para el último mensaje del lote) y el plugin la manda.
|
|
14
|
+
-- Imprime lo que quiera; una salida que empieza con "ERROR" cuenta como fallo (queda en el log).
|
|
15
|
+
-- typing (opcional, con "channel": {"typing": true}) el "escribiendo…" de la plataforma: {"thread", "meta"}.
|
|
16
|
+
-- Lampson la llama al empezar cada turno y la renueva cada ~4,5 s mientras el modelo trabaja.
|
|
17
|
+
-- setup (opcional) {"mode": "webhook", "webhook_url"} registra el webhook en la plataforma; {"mode": "poll"} lo
|
|
18
|
+
-- quita (los dos modos no conviven en Telegram). Lo llama `lampson channel setup <canal>`.
|
|
19
|
+
-- poll (opcional, con "channel": {"poll": true}) MODO SIN URL PÚBLICA: {"cursor"} → el plugin le pide a la
|
|
20
|
+
-- plataforma los mensajes nuevos (espera larga) e imprime {"messages": [...], "cursor": <el siguiente>}.
|
|
21
|
+
-- `lampson channel setup` lo elige solo cuando este Lampson no tiene URL pública; el proceso del
|
|
22
|
+
-- workspace mantiene un agente por canal pidiendo en bucle (cursor en .lampson/channels/<canal>.cursor).
|
|
23
|
+
-- Imágenes: un mensaje puede traer "images": [{"media_type": "image/jpeg", "data": "<base64>"}] — los modelos con
|
|
24
|
+
-- visión las ven; a los demás el proveedor les deja una nota.
|
|
25
|
+
--
|
|
26
|
+
-- Lampson pone todo lo demás: la ruta del webhook (POST/GET /w/<slug>/ch/<canal>, que el hub reenvía sin passkey:
|
|
27
|
+
-- la firma de la plataforma la verifica el plugin en receive), el buzón de cada conversación (lib/mailbox.syn:
|
|
28
|
+
-- fila, espera de silencio, mensajes a mitad de turno), las sesiones por cliente, los permisos y UNA respuesta por
|
|
29
|
+
-- lote. A la plataforma se le contesta enseguida (no espera al agente).
|
|
30
|
+
--
|
|
31
|
+
-- El thread de la sesión es "<canal>:<thread>" (un mismo id en dos canales no se mezcla). El agente: el que diga el
|
|
32
|
+
-- mensaje, si no LAMPSON_CHANNEL_<CANAL>_AGENT (.env), si no el del manifiesto ("channel": {"agent"}), si no el
|
|
33
|
+
-- agente por defecto. Secretos del canal: "config" en plugin.json (PLUGIN_<X> = LAMPSON_<X>).
|
|
34
|
+
-- Log por canal: .lampson/channels/<canal>.log (lo que entró y lo que salió).
|
|
35
|
+
|
|
36
|
+
use "./plugins.syn" as plugins
|
|
37
|
+
use "./mailbox.syn" as mailbox
|
|
38
|
+
use "./runtime.syn" as runtime
|
|
39
|
+
|
|
40
|
+
task log(name, line)
|
|
41
|
+
require time
|
|
42
|
+
require file(".lampson")
|
|
43
|
+
require file(".lampson/*")
|
|
44
|
+
let f be mailbox.CHANNEL_LOG_DIR + "/" + name + ".log"
|
|
45
|
+
when not file_exists(f)
|
|
46
|
+
write_file(f, "")
|
|
47
|
+
append_file(f, format_time(now()) + " " + replace(line, "\n", " ") + "\n")
|
|
48
|
+
|
|
49
|
+
task jresp(status, m)
|
|
50
|
+
give {"status": status, "body": json_encode(m), "ctype": "application/json", "accepted": 0}
|
|
51
|
+
|
|
52
|
+
-- la salida del plugin → {ok, data, error}: el stdout entero como JSON, o su última línea JSON (por si imprimió
|
|
53
|
+
-- algo antes)
|
|
54
|
+
export task parse_output(raw)
|
|
55
|
+
let t be trim(text(raw))
|
|
56
|
+
when starts_with(t, "ERROR")
|
|
57
|
+
give {"ok": false, "data": nothing, "error": slice(t, 0, 300)}
|
|
58
|
+
try
|
|
59
|
+
give {"ok": true, "data": json_decode(t), "error": ""}
|
|
60
|
+
recover err
|
|
61
|
+
let lines be where(split(replace(t, "\r", ""), "\n"), (l) => starts_with(trim(l), "{"))
|
|
62
|
+
when length(lines) > 0
|
|
63
|
+
try
|
|
64
|
+
give {"ok": true, "data": json_decode(trim(lines[length(lines) - 1])), "error": ""}
|
|
65
|
+
recover err2
|
|
66
|
+
give {"ok": false, "data": nothing, "error": "receive did not print JSON: " + slice(t, 0, 200)}
|
|
67
|
+
give {"ok": false, "data": nothing, "error": "receive did not print JSON: " + slice(t, 0, 200)}
|
|
68
|
+
|
|
69
|
+
export task agent_for(name, l, m)
|
|
70
|
+
require env("LAMPSON_*")
|
|
71
|
+
when contains(m, "agent") and m["agent"] != nothing and text(m["agent"]) != ""
|
|
72
|
+
give lower(text(m["agent"]))
|
|
73
|
+
let e be env("LAMPSON_CHANNEL_" + upper(replace(name, "-", "_")) + "_AGENT", "")
|
|
74
|
+
when e != ""
|
|
75
|
+
give lower(e)
|
|
76
|
+
when l["channel"]["agent"] != ""
|
|
77
|
+
give l["channel"]["agent"]
|
|
78
|
+
give runtime.default_agent()
|
|
79
|
+
|
|
80
|
+
-- un webhook entrante → {status, body, ctype, accepted}
|
|
81
|
+
export task handle(name, req)
|
|
82
|
+
require exec
|
|
83
|
+
require time
|
|
84
|
+
require random
|
|
85
|
+
require file(".lampson")
|
|
86
|
+
require file(".lampson/*")
|
|
87
|
+
require file.read("plugins")
|
|
88
|
+
require file.read("plugins/*")
|
|
89
|
+
require file.read("agents")
|
|
90
|
+
require file.read("agents/*")
|
|
91
|
+
require file("workspace")
|
|
92
|
+
require file("workspace/*")
|
|
93
|
+
require env("LAMPSON_*")
|
|
94
|
+
let l be plugins.channel(name)
|
|
95
|
+
when l == nothing
|
|
96
|
+
give jresp(404, {"error": "no enabled channel named '" + name + "' (turn it on: /plugins on " + name + ")"})
|
|
97
|
+
let p be parse_output(plugins.invoke(l, "receive", req))
|
|
98
|
+
when not p["ok"]
|
|
99
|
+
log(name, "RECV-ERROR " + p["error"])
|
|
100
|
+
give jresp(500, {"error": "the channel plugin failed: " + p["error"]})
|
|
101
|
+
let d be p["data"]
|
|
102
|
+
let msgs be when type_of(d) == "map" and contains(d, "messages") and type_of(d["messages"]) == "list" then d["messages"] otherwise []
|
|
103
|
+
let accepted be route_messages(name, l, msgs)
|
|
104
|
+
-- lo que el plugin quiere que se le responda a la plataforma (verificación de webhook, un 200 específico…)
|
|
105
|
+
when type_of(d) == "map" and contains(d, "http") and type_of(d["http"]) == "map"
|
|
106
|
+
let h be d["http"]
|
|
107
|
+
let body be when contains(h, "body") and h["body"] != nothing then (when type_of(h["body"]) == "text" then h["body"] otherwise json_encode(h["body"])) otherwise ""
|
|
108
|
+
give {"status": when contains(h, "status") then floor(number(h["status"])) otherwise 200, "body": body, "ctype": when contains(h, "content_type") then text(h["content_type"]) otherwise "text/plain", "accepted": accepted}
|
|
109
|
+
give {"status": 200, "body": json_encode({"ok": true, "accepted": accepted}), "ctype": "application/json", "accepted": accepted}
|
|
110
|
+
|
|
111
|
+
-- los mensajes que devolvió receive o poll → al buzón de cada conversación; cuántos entraron
|
|
112
|
+
export task route_messages(name, l, msgs)
|
|
113
|
+
require exec
|
|
114
|
+
require time
|
|
115
|
+
require random
|
|
116
|
+
require file(".lampson")
|
|
117
|
+
require file(".lampson/*")
|
|
118
|
+
require file.read("plugins")
|
|
119
|
+
require file.read("plugins/*")
|
|
120
|
+
require file.read("agents")
|
|
121
|
+
require file.read("agents/*")
|
|
122
|
+
require file("workspace")
|
|
123
|
+
require file("workspace/*")
|
|
124
|
+
require env("LAMPSON_*")
|
|
125
|
+
let names be runtime.names()
|
|
126
|
+
let accepted be 0
|
|
127
|
+
each m in msgs
|
|
128
|
+
let th be when contains(m, "thread") and m["thread"] != nothing then text(m["thread"]) otherwise ""
|
|
129
|
+
let tx be when contains(m, "text") and m["text"] != nothing then text(m["text"]) otherwise ""
|
|
130
|
+
let ag be agent_for(name, l, m)
|
|
131
|
+
when th == "" or trim(tx) == ""
|
|
132
|
+
log(name, "SKIP a message without thread or text")
|
|
133
|
+
otherwise when not contains(names, ag)
|
|
134
|
+
log(name, "SKIP no agent named '" + ag + "' (set LAMPSON_CHANNEL_" + upper(replace(name, "-", "_")) + "_AGENT or channel.agent)")
|
|
135
|
+
otherwise
|
|
136
|
+
let meta be when contains(m, "meta") and type_of(m["meta"]) == "map" then m["meta"] otherwise {}
|
|
137
|
+
when contains(m, "sent_at") and m["sent_at"] != nothing
|
|
138
|
+
set meta["sent_at"] to m["sent_at"]
|
|
139
|
+
let sobre be {"to": ag, "thread": name + ":" + th, "session": "", "from": when contains(m, "from") and type_of(m["from"]) == "map" then m["from"] otherwise {}, "text": tx, "images": when contains(m, "images") and type_of(m["images"]) == "list" then m["images"] otherwise [], "channel": name, "meta": meta}
|
|
140
|
+
let spec be runtime.get(ag)
|
|
141
|
+
mailbox.submit(sobre, {"mode": when spec["permission"] != "" then "" otherwise "strict", "buffer": mailbox.buffer_of(spec, mailbox.DEFAULT_BUFFER), "reply_channel": name, "reply_thread": th})
|
|
142
|
+
log(name, "RECV " + th + " → " + ag + " " + slice(tx, 0, 120))
|
|
143
|
+
set accepted to accepted + 1
|
|
144
|
+
give accepted
|
|
145
|
+
|
|
146
|
+
-- ---------- configuración (CLI `lampson channel …` y el panel Canales de la web) ----------
|
|
147
|
+
|
|
148
|
+
-- la URL del webhook de un canal: la pública si hay, si no la local del hub
|
|
149
|
+
export task webhook_url(name, pub, slug)
|
|
150
|
+
give (when pub != "" then pub otherwise "http://127.0.0.1:8080") + "/w/" + (when slug == "" then "local" otherwise slug) + "/ch/" + name
|
|
151
|
+
|
|
152
|
+
-- `setup`: con URL pública registra el webhook; sin ella, modo poll si el canal lo sabe hacer → {code, out}
|
|
153
|
+
export task setup_channel(name, pub, slug)
|
|
154
|
+
require exec
|
|
155
|
+
require time
|
|
156
|
+
require random
|
|
157
|
+
require file(".lampson")
|
|
158
|
+
require file(".lampson/*")
|
|
159
|
+
require file.read("plugins")
|
|
160
|
+
require file.read("plugins/*")
|
|
161
|
+
require file.read("agents")
|
|
162
|
+
require file.read("agents/*")
|
|
163
|
+
require file("workspace")
|
|
164
|
+
require file("workspace/*")
|
|
165
|
+
require env("LAMPSON_*")
|
|
166
|
+
let l be plugins.channel(name)
|
|
167
|
+
when l == nothing
|
|
168
|
+
give {"code": 2, "out": "the channel " + name + " is off (or is not a channel): turn it on first"}
|
|
169
|
+
when pub == "" and l["channel"]["poll"]
|
|
170
|
+
set_mode(name, "poll")
|
|
171
|
+
let pout be trim(text(plugins.invoke(l, "setup", {"mode": "poll"})))
|
|
172
|
+
give {"code": when starts_with(pout, "ERROR") then 1 otherwise 0, "out": pout + "\nmodo sin URL pública: el proceso del workspace le pide los mensajes a " + name + " (arranca solo con el workspace; `lampson --hub start` si está apagado)"}
|
|
173
|
+
when pub == ""
|
|
174
|
+
give {"code": 2, "out": "hace falta una URL pública (https) para que " + name + " llegue a este Lampson: `lampson --domain …`, un túnel, o LAMPSON_PUBLIC_URL en .env"}
|
|
175
|
+
set_mode(name, "webhook")
|
|
176
|
+
let out be trim(text(plugins.invoke(l, "setup", {"mode": "webhook", "webhook_url": webhook_url(name, pub, slug)})))
|
|
177
|
+
when contains(out, "unknown action 'setup'")
|
|
178
|
+
give {"code": 0, "out": "el canal " + name + " se configura desde la plataforma: pegá esta URL como webhook → " + webhook_url(name, pub, slug)}
|
|
179
|
+
give {"code": when starts_with(out, "ERROR") then 1 otherwise 0, "out": out}
|
|
180
|
+
|
|
181
|
+
-- cada canal instalado, para la UI: {name, description, enabled, error, agent, mode, webhook_url, poll, typing, log}
|
|
182
|
+
export task summary(pub, slug)
|
|
183
|
+
require exec
|
|
184
|
+
require time
|
|
185
|
+
require random
|
|
186
|
+
require file(".lampson")
|
|
187
|
+
require file(".lampson/*")
|
|
188
|
+
require file.read("plugins")
|
|
189
|
+
require file.read("plugins/*")
|
|
190
|
+
require file.read("agents")
|
|
191
|
+
require file.read("agents/*")
|
|
192
|
+
require file("workspace")
|
|
193
|
+
require file("workspace/*")
|
|
194
|
+
require env("LAMPSON_*")
|
|
195
|
+
let out be []
|
|
196
|
+
each l in where(plugins.all(), (x) => x["channel"] != nothing)
|
|
197
|
+
let lines be []
|
|
198
|
+
try
|
|
199
|
+
let raw be read_file(mailbox.CHANNEL_LOG_DIR + "/" + l["name"] + ".log")
|
|
200
|
+
let all_lines be where(split(replace(raw, "\r", ""), "\n"), (x) => trim(x) != "")
|
|
201
|
+
set lines to when length(all_lines) > 12 then slice(all_lines, length(all_lines) - 12, length(all_lines)) otherwise all_lines
|
|
202
|
+
recover err
|
|
203
|
+
set lines to []
|
|
204
|
+
set out to append(out, {"name": l["name"], "description": l["description"], "enabled": l["enabled"], "error": l["error"], "agent": when l["error"] == nothing then agent_for(l["name"], l, {}) otherwise "", "mode": mode_of(l["name"]), "webhook_url": webhook_url(l["name"], pub, slug), "public": pub != "", "poll": l["channel"]["poll"], "typing": l["channel"]["typing"], "scope": l["scope"], "log": lines})
|
|
205
|
+
give out
|
|
206
|
+
|
|
207
|
+
-- ---------- modo sin URL pública (poll) ----------
|
|
208
|
+
|
|
209
|
+
task modes_file()
|
|
210
|
+
give mailbox.CHANNEL_LOG_DIR + "/modes.json"
|
|
211
|
+
|
|
212
|
+
export task mode_of(name)
|
|
213
|
+
require file.read(".lampson")
|
|
214
|
+
require file.read(".lampson/*")
|
|
215
|
+
try
|
|
216
|
+
let d be json_decode(read_file(modes_file()))
|
|
217
|
+
when contains(d, name)
|
|
218
|
+
give text(d[name])
|
|
219
|
+
recover err
|
|
220
|
+
give "webhook"
|
|
221
|
+
give "webhook"
|
|
222
|
+
|
|
223
|
+
export task set_mode(name, mode)
|
|
224
|
+
require file(".lampson")
|
|
225
|
+
require file(".lampson/*")
|
|
226
|
+
let d be {}
|
|
227
|
+
try
|
|
228
|
+
set d to json_decode(read_file(modes_file()))
|
|
229
|
+
recover err
|
|
230
|
+
set d to {}
|
|
231
|
+
set d[name] to mode
|
|
232
|
+
write_file(modes_file(), json_encode(d))
|
|
233
|
+
give mode
|
|
234
|
+
|
|
235
|
+
task cursor_file(name)
|
|
236
|
+
give mailbox.CHANNEL_LOG_DIR + "/" + name + ".cursor"
|
|
237
|
+
|
|
238
|
+
-- una vuelta: pide los mensajes nuevos al plugin y los manda al buzón → {ok, n, error}
|
|
239
|
+
export task poll_once(name)
|
|
240
|
+
require exec
|
|
241
|
+
require time
|
|
242
|
+
require random
|
|
243
|
+
require file(".lampson")
|
|
244
|
+
require file(".lampson/*")
|
|
245
|
+
require file.read("plugins")
|
|
246
|
+
require file.read("plugins/*")
|
|
247
|
+
require file.read("agents")
|
|
248
|
+
require file.read("agents/*")
|
|
249
|
+
require file("workspace")
|
|
250
|
+
require file("workspace/*")
|
|
251
|
+
require env("LAMPSON_*")
|
|
252
|
+
let l be plugins.channel(name)
|
|
253
|
+
when l == nothing
|
|
254
|
+
give {"ok": false, "n": 0, "error": "channel off"}
|
|
255
|
+
let cursor be nothing
|
|
256
|
+
try
|
|
257
|
+
set cursor to json_decode(read_file(cursor_file(name)))["cursor"]
|
|
258
|
+
recover err
|
|
259
|
+
set cursor to nothing
|
|
260
|
+
let p be parse_output(plugins.invoke(l, "poll", {"cursor": cursor}))
|
|
261
|
+
when not p["ok"]
|
|
262
|
+
log(name, "POLL-ERROR " + p["error"])
|
|
263
|
+
give {"ok": false, "n": 0, "error": p["error"]}
|
|
264
|
+
let d be p["data"]
|
|
265
|
+
let msgs be when type_of(d) == "map" and contains(d, "messages") and type_of(d["messages"]) == "list" then d["messages"] otherwise []
|
|
266
|
+
-- el cursor se guarda ANTES de encolar: un mensaje nunca se procesa dos veces (si se cae acá, se pierde uno)
|
|
267
|
+
when type_of(d) == "map" and contains(d, "cursor") and d["cursor"] != nothing
|
|
268
|
+
write_file(cursor_file(name), json_encode({"cursor": d["cursor"]}))
|
|
269
|
+
give {"ok": true, "n": route_messages(name, l, msgs), "error": ""}
|
|
270
|
+
|
|
271
|
+
-- el bucle de un canal en modo poll (corre en un agente ChannelPoller del proceso del workspace)
|
|
272
|
+
export task poll_loop(name)
|
|
273
|
+
require exec
|
|
274
|
+
require time
|
|
275
|
+
require random
|
|
276
|
+
require file(".lampson")
|
|
277
|
+
require file(".lampson/*")
|
|
278
|
+
require file.read("plugins")
|
|
279
|
+
require file.read("plugins/*")
|
|
280
|
+
require file.read("agents")
|
|
281
|
+
require file.read("agents/*")
|
|
282
|
+
require file("workspace")
|
|
283
|
+
require file("workspace/*")
|
|
284
|
+
require env("LAMPSON_*")
|
|
285
|
+
log(name, "POLL start (no public URL: Lampson asks the platform for new messages)")
|
|
286
|
+
let going be true
|
|
287
|
+
while going
|
|
288
|
+
let l be plugins.channel(name)
|
|
289
|
+
when l == nothing or not l["channel"]["poll"] or mode_of(name) != "poll"
|
|
290
|
+
set going to false
|
|
291
|
+
otherwise
|
|
292
|
+
let r be poll_once(name)
|
|
293
|
+
when not r["ok"]
|
|
294
|
+
sleep(10)
|
|
295
|
+
otherwise when r["n"] == 0
|
|
296
|
+
sleep(1)
|
|
297
|
+
state_set("chpoll:" + name, false)
|
|
298
|
+
log(name, "POLL stop")
|
|
299
|
+
give true
|
|
300
|
+
|
|
301
|
+
-- MIGA runtime (igual que agents.Child): un agente solo llama tasks TOP-LEVEL del entry; web.syn define
|
|
302
|
+
-- `task lampson_channel_poller(name) give channels.poll_loop(name)`
|
|
303
|
+
agent ChannelPoller
|
|
304
|
+
require exec
|
|
305
|
+
require time
|
|
306
|
+
require random
|
|
307
|
+
require net
|
|
308
|
+
require file(".lampson")
|
|
309
|
+
require file(".lampson/*")
|
|
310
|
+
require file.read("plugins")
|
|
311
|
+
require file.read("plugins/*")
|
|
312
|
+
require file.read("agents")
|
|
313
|
+
require file.read("agents/*")
|
|
314
|
+
require file("workspace")
|
|
315
|
+
require file("workspace/*")
|
|
316
|
+
require file.read("skills")
|
|
317
|
+
require file.read("skills/*")
|
|
318
|
+
require file("memory")
|
|
319
|
+
require file("memory/*")
|
|
320
|
+
require env("LAMPSON_*")
|
|
321
|
+
require env("OS")
|
|
322
|
+
require secret("LAMPSON_*")
|
|
323
|
+
lampson_channel_poller(channel_name)
|
|
324
|
+
|
|
325
|
+
-- el tick (cron de web.syn): un poller vivo por cada canal encendido en modo poll
|
|
326
|
+
export task poll_tick()
|
|
327
|
+
require exec
|
|
328
|
+
require time
|
|
329
|
+
require file(".lampson")
|
|
330
|
+
require file(".lampson/*")
|
|
331
|
+
require file.read("plugins")
|
|
332
|
+
require file.read("plugins/*")
|
|
333
|
+
require file("workspace")
|
|
334
|
+
require file("workspace/*")
|
|
335
|
+
require env("LAMPSON_*")
|
|
336
|
+
each l in plugins.channels()
|
|
337
|
+
when l["channel"]["poll"] and mode_of(l["name"]) == "poll" and state_get("chpoll:" + l["name"], false) != true
|
|
338
|
+
state_set("chpoll:" + l["name"], true)
|
|
339
|
+
spawn ChannelPoller with channel_name = l["name"]
|
|
340
|
+
give true
|
package/lib/loop.syn
CHANGED
|
@@ -25,6 +25,8 @@ use "./permission.syn" as permission
|
|
|
25
25
|
use "./mcp.syn" as mcp
|
|
26
26
|
use "./plugins.syn" as plugins
|
|
27
27
|
use "./tools/todo.syn" as todo
|
|
28
|
+
use "./scope.syn" as scope
|
|
29
|
+
use "./worktree.syn" as worktree
|
|
28
30
|
|
|
29
31
|
let MAX_ERRORS_PER_TURN be 8
|
|
30
32
|
-- capturas de computer_use (lib/computer.syn): el driver las deja en el blackboard "computer:screenshot" y acá van al
|
|
@@ -275,6 +277,19 @@ task strip_raw_tool_calls(t)
|
|
|
275
277
|
task tool_result_msg(tc, content)
|
|
276
278
|
give {"role": "tool", "tool_call_id": tc["id"], "name": tc["name"], "content": content}
|
|
277
279
|
|
|
280
|
+
-- "auto" | "ask" | "" para una tool, según opts["approve"] (nombre exacto o prefijo con * al final)
|
|
281
|
+
export task approve_for(opts, name)
|
|
282
|
+
when not contains(opts, "approve")
|
|
283
|
+
give ""
|
|
284
|
+
let ap be opts["approve"]
|
|
285
|
+
when contains(ap, name)
|
|
286
|
+
give ap[name]
|
|
287
|
+
each k in keys(ap)
|
|
288
|
+
when ends_with(k, "*")
|
|
289
|
+
when starts_with(name, slice(k, 0, length(k) - 1))
|
|
290
|
+
give ap[k]
|
|
291
|
+
give ""
|
|
292
|
+
|
|
278
293
|
-- Ejecuta UNA tool call: permisos → allow-list → call_tool → resultado como texto.
|
|
279
294
|
task execute(tc, opts, on_event)
|
|
280
295
|
let tag be opts["tag"]
|
|
@@ -286,18 +301,52 @@ task execute(tc, opts, on_event)
|
|
|
286
301
|
give "ERROR: unknown tool '" + name + "'. Available: " + join(keys(registry), ", ")
|
|
287
302
|
when contains(args, "_parse_error")
|
|
288
303
|
give "ERROR: could not parse tool arguments as JSON: " + text(args["_parse_error"])
|
|
289
|
-
|
|
304
|
+
-- alcance por ruta (lib/scope.syn): solo agentes del proyecto que limitan una tool de archivos
|
|
305
|
+
when contains(opts, "scopes")
|
|
306
|
+
when contains(opts["scopes"], name)
|
|
307
|
+
let why be scope.violation(name, args, opts["scopes"][name])
|
|
308
|
+
when why != ""
|
|
309
|
+
emit(on_event, "tool_denied", {"call": tc, "reason": "out of scope (" + why + ")"}, tag)
|
|
310
|
+
give "DENIED by scope: " + why + ". Do not retry other paths outside it; work with what is in scope or tell the user."
|
|
311
|
+
-- permiso por tool (runtime: "approve" en el allow-list de un agente del proyecto): "auto" = esta tool se evalúa
|
|
312
|
+
-- como en yolo (lo prohibido en todos los modos sigue prohibido); "ask" = esta tool pide aprobación siempre
|
|
313
|
+
let forced be approve_for(opts, name)
|
|
314
|
+
let verdict be permission.evaluate(name, args, when forced == "auto" then "yolo" otherwise opts["permission_mode"])
|
|
315
|
+
when forced == "ask" and verdict["decision"] == "allow"
|
|
316
|
+
set verdict to {"decision": "ask", "reason": "this agent asks before every " + name + " call"}
|
|
317
|
+
-- equipos (lib/worktree.syn): el agente trabaja en SU copia; tocar la historia compartida (merge a main, push…)
|
|
318
|
+
-- lo aprueba siempre la persona, en cualquier modo (salvo lo prohibido, que sigue prohibido)
|
|
319
|
+
let wroot be when contains(opts, "root") and opts["root"] != nothing then text(opts["root"]) otherwise ""
|
|
320
|
+
when wroot != "" and name == "bash" and verdict["decision"] != "deny"
|
|
321
|
+
let shared be worktree.touches_shared(when contains(args, "command") then args["command"] otherwise "")
|
|
322
|
+
when shared != ""
|
|
323
|
+
set verdict to {"decision": "ask", "reason": "touches the shared history (" + shared + ") — the person approves · agent " + (when contains(opts, "agent_id") then text(opts["agent_id"]) otherwise "?")}
|
|
290
324
|
when verdict["decision"] == "deny"
|
|
291
325
|
emit(on_event, "tool_denied", {"call": tc, "reason": verdict["reason"]}, tag)
|
|
292
326
|
give "DENIED by policy (" + verdict["reason"] + "). Do not retry this exact action; explain or propose an alternative."
|
|
293
327
|
when verdict["decision"] == "ask"
|
|
294
328
|
let approved be false
|
|
329
|
+
let expired be false
|
|
330
|
+
let waited_min be 0
|
|
295
331
|
when opts["ask_fn"] != nothing
|
|
296
|
-
|
|
332
|
+
let ans be call(opts["ask_fn"], {"name": name, "args": args, "why": verdict["reason"]})
|
|
333
|
+
-- la cola de aprobaciones (lib/mailbox.syn) dice además si nadie respondió; la terminal y el chat dan un bool
|
|
334
|
+
when type_of(ans) == "map"
|
|
335
|
+
set approved to ans["approved"] == true
|
|
336
|
+
set expired to ans["expired"] == true
|
|
337
|
+
set waited_min to ans["minutes"]
|
|
338
|
+
otherwise
|
|
339
|
+
set approved to ans == true
|
|
340
|
+
when not approved and expired
|
|
341
|
+
emit(on_event, "tool_denied", {"call": tc, "reason": "no answer to the approval request (" + verdict["reason"] + ")"}, tag)
|
|
342
|
+
give "NOT RUN: nobody answered the approval request in " + text(waited_min) + " min (" + verdict["reason"] + "). The action did not happen. Don't retry it right away: tell whoever is waiting on it (your lead with `send`, or the person with `objective` ask), keep working on what doesn't depend on it, and try again later."
|
|
297
343
|
when not approved
|
|
298
344
|
emit(on_event, "tool_denied", {"call": tc, "reason": "user declined (" + verdict["reason"] + ")"}, tag)
|
|
299
345
|
give "DENIED by the user (" + verdict["reason"] + "). Do not retry this exact action; ask the user or propose an alternative."
|
|
300
346
|
emit(on_event, "busy", {"label": permission.describe_call(name, args), "kind": "tool", "name": name}, tag)
|
|
347
|
+
-- su copia: las tools de archivos y bash operan ahí; los paths que ve el modelo son relativos a ella
|
|
348
|
+
when wroot != "" and (name == "bash" or contains(worktree.FILE_TOOLS, name))
|
|
349
|
+
set args to worktree.rooted(name, args, wroot)
|
|
301
350
|
let out be ""
|
|
302
351
|
try
|
|
303
352
|
-- tools MCP: no son tasks Synsema (args libres); el registry las marca con "mcp"
|
|
@@ -306,11 +355,19 @@ task execute(tc, opts, on_event)
|
|
|
306
355
|
-- tools de plugins: un proceso hijo por llamada (lib/plugins.syn)
|
|
307
356
|
otherwise when registry[name] == "plugin"
|
|
308
357
|
set out to text(plugins.call(name, args))
|
|
358
|
+
-- equipos: send/board saben quién llama por opts.agent_id (el modelo no lo elige: se pisa lo que mande)
|
|
359
|
+
otherwise when name == "send" or name == "board" or name == "objective"
|
|
360
|
+
let targs be args
|
|
361
|
+
set targs["_agent"] to when contains(opts, "agent_id") then opts["agent_id"] otherwise ""
|
|
362
|
+
set targs["_scope"] to when contains(opts, "scope") then opts["scope"] otherwise ""
|
|
363
|
+
set out to text(call_tool(registry[name], targs))
|
|
309
364
|
otherwise
|
|
310
365
|
set out to text(call_tool(registry[name], args))
|
|
311
366
|
recover err
|
|
312
367
|
set out to "ERROR: " + err
|
|
313
368
|
emit(on_event, "idle", nothing, tag)
|
|
369
|
+
when wroot != ""
|
|
370
|
+
set out to replace(out, wroot + "/", "")
|
|
314
371
|
give out
|
|
315
372
|
|
|
316
373
|
export task run_turn(cfg, messages, opts, on_event)
|
|
@@ -403,7 +460,19 @@ export task run_turn(cfg, messages, opts, on_event)
|
|
|
403
460
|
set msgs to append(msgs, assistant)
|
|
404
461
|
when clean_text != ""
|
|
405
462
|
emit(on_event, "assistant", clean_text, tag)
|
|
406
|
-
|
|
463
|
+
-- buzón de conversación (opts.inbox_final): si mientras el modelo escribía la respuesta llegaron mensajes
|
|
464
|
+
-- nuevos, esa respuesta ya está vieja (visto 2026-10-16: «soy Lucía» llegó a mitad de turno y salió «decime
|
|
465
|
+
-- tu nombre»). Se descarta el borrador, entran los mensajes y el modelo responde de nuevo — un paso más.
|
|
466
|
+
let late be nothing
|
|
467
|
+
when length(r["tool_calls"]) == 0 and not last_step and contains(opts, "inbox_final") and opts["inbox_final"] == true and opts["inbox_fn"] != nothing
|
|
468
|
+
set late to call(opts["inbox_fn"], {"step": steps, "tag": tag})
|
|
469
|
+
when late != nothing and length(late["messages"]) > 0
|
|
470
|
+
set msgs to slice(msgs, 0, length(msgs) - 1)
|
|
471
|
+
emit(on_event, "inbox", "llegaron mensajes mientras respondía: rehago la respuesta", tag)
|
|
472
|
+
each m in late["messages"]
|
|
473
|
+
emit(on_event, "inbox", m, tag)
|
|
474
|
+
set msgs to append(msgs, {"role": "user", "content": m})
|
|
475
|
+
otherwise when length(r["tool_calls"]) == 0
|
|
407
476
|
set final_text to clean_text
|
|
408
477
|
give {"messages": msgs, "text": final_text, "steps": steps, "usage": usage, "stopped": when over_budget then "budget" otherwise (when last_step then "max_steps" otherwise "done")}
|
|
409
478
|
each tc in r["tool_calls"]
|
|
@@ -498,5 +567,10 @@ export task with_steps(opts, n)
|
|
|
498
567
|
export task with_inbox(opts, inbox_fn, tag)
|
|
499
568
|
give with_key(with_key(opts, "inbox_fn", inbox_fn), "tag", tag)
|
|
500
569
|
|
|
570
|
+
-- buzón de una conversación (lib/mailbox.syn): además de antes de cada llamada, se consulta al terminar; lo que
|
|
571
|
+
-- llegó mientras el modelo respondía rehace la respuesta en lugar de mandar una vieja
|
|
572
|
+
export task with_final_inbox(opts)
|
|
573
|
+
give with_key(opts, "inbox_final", true)
|
|
574
|
+
|
|
501
575
|
export task with_ask(opts, ask_fn)
|
|
502
576
|
give with_key(opts, "ask_fn", ask_fn)
|