lampson 0.2.10 → 0.3.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 +16 -0
- package/bin/lampson.js +2 -2
- package/chat.syn +39 -24
- package/cli.syn +6 -3
- package/hub.tpl.syn +4 -0
- package/lampson.ps1 +48 -6
- package/lampson.sh +69 -24
- package/lib/api.syn +153 -0
- package/lib/apikeys.syn +115 -0
- package/lib/channels.syn +340 -0
- package/lib/loop.syn +27 -1
- package/lib/mailbox.syn +418 -0
- package/lib/mcp.syn +128 -9
- package/lib/mcp_server.syn +235 -0
- package/lib/plugins.syn +62 -7
- package/lib/runtime.syn +572 -0
- package/lib/scope.syn +62 -0
- package/lib/session.syn +11 -1
- package/lib/tools.syn +1 -1
- package/lib/workspaces.syn +29 -3
- package/package.json +16 -2
- package/plugins/chatwoot/plugin.json +11 -0
- package/plugins/chatwoot/plugin.syn +143 -0
- package/plugins/telegram/plugin.json +11 -0
- package/plugins/telegram/plugin.syn +272 -0
- package/public/css/panel.css +2 -0
- package/public/index.html +9 -0
- package/public/js/app.js +1 -1
- package/public/js/engine.js +149 -0
- package/public/js/sessions.js +3 -1
- package/run.syn +394 -0
- package/sdk/index.d.ts +94 -0
- package/sdk/index.js +250 -0
- package/sdk/index.mjs +4 -0
- package/web.syn +315 -47
package/lib/runtime.syn
ADDED
|
@@ -0,0 +1,572 @@
|
|
|
1
|
+
-- lib/runtime.syn — la capa entre las puertas (terminal, web, CLI, API, canales) y el kernel (loop.run_turn)
|
|
2
|
+
--
|
|
3
|
+
-- Norte y contratos: spec/engine.md. En dos líneas: todo lo que entra es un SOBRE dirigido a un agente, y una
|
|
4
|
+
-- sesión es (agente, thread). Las puertas llaman a deliver(); deliver elige el agente, abre la sesión, corre el
|
|
5
|
+
-- turno, traza, guarda y devuelve. Así la orquestación (agentes que se mandan mensajes) es otra puerta más.
|
|
6
|
+
--
|
|
7
|
+
-- AGENTES = datos:
|
|
8
|
+
-- integrados: los perfiles de lib/agents.syn (build, plan, review, explore, worker) — se comportan EXACTO como
|
|
9
|
+
-- antes: registry/catalog/steps/prompt salen de las mismas tasks de agents.syn
|
|
10
|
+
-- globales: agents/<id>.agent.json en la instalación (<LAMPSON_HOME>/agents, enlazada en cada workspace como
|
|
11
|
+
-- los plugins): valen para todos los proyectos
|
|
12
|
+
-- proyecto: workspace/.lampson/agents/<id>.agent.json — {id, description, base, system, tools, permission,
|
|
13
|
+
-- steps, budget_tokens, model:{provider, model}}; pisan a un global o integrado con el mismo id
|
|
14
|
+
--
|
|
15
|
+
-- SESIONES: `session` en el sobre = id de archivo ya conocido (lo que mandan la web y la terminal); `thread` =
|
|
16
|
+
-- identidad externa (un número de WhatsApp, "cto:tarea-17"): se mapea a una sesión con un índice en
|
|
17
|
+
-- .lampson/threads/<hash>.json. Los ids nuevos son timestamp + 4 dígitos (dos clientes en el mismo segundo no
|
|
18
|
+
-- pisan la misma sesión; session.list sigue ordenándolos como números).
|
|
19
|
+
--
|
|
20
|
+
-- NIVELES (de más alto a más bajo; una puerta usa el más alto que le sirve):
|
|
21
|
+
-- deliver(sobre, hooks) open + turn: lo que usan run.syn y POST /api/chat
|
|
22
|
+
-- open(sobre, hooks) / save(o, msgs) abrir la conversación sin correr el turno (el `!comando` de la web)
|
|
23
|
+
-- turn(o, messages, label, hooks) un turno sobre mensajes ya armados: traza, run_turn, guarda (la terminal,
|
|
24
|
+
-- que tiene el historial en memoria entre turnos)
|
|
25
|
+
-- boot / system_for / opts_for armar un agente (cfg, mensaje system, opts del loop)
|
|
26
|
+
--
|
|
27
|
+
-- deliver(sobre, hooks) → {agent, thread, session, text, steps, usage, stopped}
|
|
28
|
+
-- sobre : {to, thread, session, from, text, images, channel, meta} (todos opcionales menos text)
|
|
29
|
+
-- hooks : {on_event, ask_fn, mode, provider, model, on_start}
|
|
30
|
+
-- on_event(kind, data, tag) eventos del loop; el runtime ya trazó cada uno en .lampson/trace/<sid>.log
|
|
31
|
+
-- ask_fn(name, args, why) aprobación humana; nothing = sin humano → lo que pida aprobación se deniega
|
|
32
|
+
-- on_start(info) antes del turno: {session, agent, thread, mode, provider, model, has_key}
|
|
33
|
+
|
|
34
|
+
use "./agents.syn" as agents
|
|
35
|
+
use "./tools.syn" as tools
|
|
36
|
+
use "./loop.syn" as loop
|
|
37
|
+
use "./provider.syn" as provider
|
|
38
|
+
use "./prompt.syn" as prompt
|
|
39
|
+
use "./session.syn" as session
|
|
40
|
+
use "./trace.syn" as trace
|
|
41
|
+
use "./mcp.syn" as mcp
|
|
42
|
+
use "./plugins.syn" as plugins
|
|
43
|
+
use "./tools/proc.syn" as proc
|
|
44
|
+
use "./scope.syn" as scope
|
|
45
|
+
|
|
46
|
+
export let PROJECT_DIR be "workspace/.lampson/agents"
|
|
47
|
+
export let GLOBAL_DIR be "agents"
|
|
48
|
+
export let THREADS_DIR be ".lampson/threads"
|
|
49
|
+
export let SUFFIX be ".agent.json"
|
|
50
|
+
let ID_RE be "[a-z0-9][a-z0-9-]{0,39}"
|
|
51
|
+
let MODES be ["", "ask", "strict", "yolo"]
|
|
52
|
+
let BASES be ["lampson", "none"]
|
|
53
|
+
|
|
54
|
+
-- ---------- agentes ----------
|
|
55
|
+
|
|
56
|
+
task builtin(id)
|
|
57
|
+
let p be agents.PROFILES[id]
|
|
58
|
+
give {"id": id, "description": "", "role": "", "base": "lampson", "system": p["addendum"], "tools": p["tools"], "scopes": {}, "approve": {}, "permission": "", "steps": 0, "budget_tokens": 0, "model": {"provider": "", "model": ""}, "builtin": true, "source": "builtin"}
|
|
59
|
+
|
|
60
|
+
task field(raw, key, fallback)
|
|
61
|
+
when contains(raw, key)
|
|
62
|
+
when raw[key] != nothing
|
|
63
|
+
give raw[key]
|
|
64
|
+
give fallback
|
|
65
|
+
|
|
66
|
+
-- {ok, spec, error}: valida un .agent.json ya decodificado y completa los defaults
|
|
67
|
+
export task validate(raw, source)
|
|
68
|
+
when type_of(raw) != "map"
|
|
69
|
+
give {"ok": false, "spec": nothing, "error": source + ": the agent file must be a JSON object"}
|
|
70
|
+
let id be text(field(raw, "id", ""))
|
|
71
|
+
when not matches(id, ID_RE)
|
|
72
|
+
give {"ok": false, "spec": nothing, "error": source + ": id must be lowercase letters, digits and - (max 40), got '" + id + "'"}
|
|
73
|
+
let base be lower(text(field(raw, "base", "none")))
|
|
74
|
+
when not contains(BASES, base)
|
|
75
|
+
give {"ok": false, "spec": nothing, "error": source + ": base must be lampson or none"}
|
|
76
|
+
let perm be lower(text(field(raw, "permission", "")))
|
|
77
|
+
when not contains(MODES, perm)
|
|
78
|
+
give {"ok": false, "spec": nothing, "error": source + ": permission must be ask, strict or yolo"}
|
|
79
|
+
let tl be field(raw, "tools", [])
|
|
80
|
+
when type_of(tl) != "list"
|
|
81
|
+
give {"ok": false, "spec": nothing, "error": source + ": tools must be a list of tool names"}
|
|
82
|
+
-- cada tool: "read" (sin límite, permisos del modo del agente) o un objeto con
|
|
83
|
+
-- "paths": ["dir/*", …] alcance por ruta (lib/scope.syn; solo tools de archivos)
|
|
84
|
+
-- "approve": "auto" esta tool corre sin pedir aprobación (como en yolo, SOLO ella; lo prohibido en todos los
|
|
85
|
+
-- modos sigue prohibido) · "ask": esta tool pide aprobación SIEMPRE, aunque el agente sea yolo
|
|
86
|
+
let names be []
|
|
87
|
+
let scopes be {}
|
|
88
|
+
let approvals_by_tool be {}
|
|
89
|
+
each t in tl
|
|
90
|
+
when type_of(t) == "map"
|
|
91
|
+
let tn be text(field(t, "name", ""))
|
|
92
|
+
let ps be field(t, "paths", nothing)
|
|
93
|
+
let ap be lower(text(field(t, "approve", "")))
|
|
94
|
+
when tn == ""
|
|
95
|
+
give {"ok": false, "spec": nothing, "error": source + ": a tool object needs a name"}
|
|
96
|
+
when ps == nothing and ap == ""
|
|
97
|
+
give {"ok": false, "spec": nothing, "error": source + ": tool '" + tn + "': an object needs \"paths\" and/or \"approve\" (or write just \"" + tn + "\")"}
|
|
98
|
+
when ap != "" and ap != "auto" and ap != "ask"
|
|
99
|
+
give {"ok": false, "spec": nothing, "error": source + ": tool '" + tn + "': approve must be auto or ask"}
|
|
100
|
+
when ps != nothing
|
|
101
|
+
when type_of(ps) != "list" or length(ps) == 0
|
|
102
|
+
give {"ok": false, "spec": nothing, "error": source + ": tool '" + tn + "': paths must be a non-empty list"}
|
|
103
|
+
when not contains(scope.SCOPABLE, tn)
|
|
104
|
+
give {"ok": false, "spec": nothing, "error": source + ": only " + join(scope.SCOPABLE, ", ") + " can be limited by path ('" + tn + "' cannot)"}
|
|
105
|
+
each pp in ps
|
|
106
|
+
let n be scope.norm(pp)
|
|
107
|
+
when not n["ok"]
|
|
108
|
+
give {"ok": false, "spec": nothing, "error": source + ": " + n["error"]}
|
|
109
|
+
set scopes[tn] to apply((pp) => text(pp), ps)
|
|
110
|
+
when ap != ""
|
|
111
|
+
set approvals_by_tool[tn] to ap
|
|
112
|
+
set names to append(names, tn)
|
|
113
|
+
otherwise
|
|
114
|
+
set names to append(names, text(t))
|
|
115
|
+
when length(keys(scopes)) > 0
|
|
116
|
+
let leaks be where(names, (n) => contains(scope.ESCAPES, n))
|
|
117
|
+
when length(leaks) > 0
|
|
118
|
+
give {"ok": false, "spec": nothing, "error": source + ": " + join(leaks, ", ") + " can reach any file, so it cannot be combined with path-limited tools"}
|
|
119
|
+
let system be text(field(raw, "system", ""))
|
|
120
|
+
when base == "none" and trim(system) == ""
|
|
121
|
+
give {"ok": false, "spec": nothing, "error": source + ": an agent with base=none needs a system prompt"}
|
|
122
|
+
let steps be floor(number(field(raw, "steps", 0)))
|
|
123
|
+
let budget be number(field(raw, "budget_tokens", 0))
|
|
124
|
+
when steps < 0 or budget < 0
|
|
125
|
+
give {"ok": false, "spec": nothing, "error": source + ": steps and budget_tokens must be >= 0"}
|
|
126
|
+
-- espera de silencio del buzón (lib/mailbox.syn): false = sin espera; {quiet_s, max_s}; sin campo = la de la puerta
|
|
127
|
+
let buf be field(raw, "buffer", nothing)
|
|
128
|
+
when buf != nothing and buf != false
|
|
129
|
+
when type_of(buf) != "map"
|
|
130
|
+
give {"ok": false, "spec": nothing, "error": source + ": buffer must be false or {\"quiet_s\": 3, \"max_s\": 15}"}
|
|
131
|
+
let qs be number(field(buf, "quiet_s", 3))
|
|
132
|
+
let ms be number(field(buf, "max_s", 15))
|
|
133
|
+
when qs < 0 or ms < qs
|
|
134
|
+
give {"ok": false, "spec": nothing, "error": source + ": buffer needs 0 <= quiet_s <= max_s"}
|
|
135
|
+
set buf to {"quiet_s": qs, "max_s": ms}
|
|
136
|
+
let m be field(raw, "model", {})
|
|
137
|
+
when type_of(m) != "map"
|
|
138
|
+
set m to {}
|
|
139
|
+
let model be {"provider": lower(text(field(m, "provider", ""))), "model": text(field(m, "model", ""))}
|
|
140
|
+
give {"ok": true, "error": "", "spec": {"id": id, "description": text(field(raw, "description", "")), "role": text(field(raw, "role", "")), "base": base, "system": system, "tools": names, "scopes": scopes, "approve": approvals_by_tool, "buffer": buf, "permission": perm, "steps": steps, "budget_tokens": budget, "model": model, "builtin": false, "source": source}}
|
|
141
|
+
|
|
142
|
+
-- {specs, errors}: los .agent.json del proyecto (un archivo roto no tumba a los demás: va a errors)
|
|
143
|
+
export task load_project()
|
|
144
|
+
require file.read("workspace")
|
|
145
|
+
require file.read("workspace/*")
|
|
146
|
+
require file.read("agents")
|
|
147
|
+
require file.read("agents/*")
|
|
148
|
+
give load_dir(PROJECT_DIR)
|
|
149
|
+
|
|
150
|
+
-- {specs, errors}: los globales de la instalación
|
|
151
|
+
export task load_global()
|
|
152
|
+
require file.read("agents")
|
|
153
|
+
require file.read("agents/*")
|
|
154
|
+
give load_dir(GLOBAL_DIR)
|
|
155
|
+
|
|
156
|
+
task load_dir(dir)
|
|
157
|
+
require file.read("workspace")
|
|
158
|
+
require file.read("workspace/*")
|
|
159
|
+
require file.read("agents")
|
|
160
|
+
require file.read("agents/*")
|
|
161
|
+
let specs be []
|
|
162
|
+
let errors be []
|
|
163
|
+
try
|
|
164
|
+
each e in list_dir(dir)
|
|
165
|
+
when not e["is_dir"] and ends_with(e["name"], SUFFIX)
|
|
166
|
+
let src be dir + "/" + e["name"]
|
|
167
|
+
try
|
|
168
|
+
let v be validate(json_decode(read_file(src)), src)
|
|
169
|
+
when v["ok"]
|
|
170
|
+
when v["spec"]["id"] + SUFFIX != e["name"]
|
|
171
|
+
set errors to append(errors, src + ": the file name must be <id>" + SUFFIX + " (id is '" + v["spec"]["id"] + "')")
|
|
172
|
+
otherwise
|
|
173
|
+
set specs to append(specs, v["spec"])
|
|
174
|
+
otherwise
|
|
175
|
+
set errors to append(errors, v["error"])
|
|
176
|
+
recover err
|
|
177
|
+
set errors to append(errors, src + ": " + text(err))
|
|
178
|
+
recover err
|
|
179
|
+
set specs to specs
|
|
180
|
+
give {"specs": specs, "errors": errors}
|
|
181
|
+
|
|
182
|
+
-- todos los agentes por id: integrados, después los globales y después los del proyecto (pisa el más cercano)
|
|
183
|
+
export task all()
|
|
184
|
+
require file.read("workspace")
|
|
185
|
+
require file.read("workspace/*")
|
|
186
|
+
require file.read("agents")
|
|
187
|
+
require file.read("agents/*")
|
|
188
|
+
let out be {}
|
|
189
|
+
each id in keys(agents.PROFILES)
|
|
190
|
+
set out[id] to builtin(id)
|
|
191
|
+
each s in load_global()["specs"]
|
|
192
|
+
set s["source"] to "global:" + s["source"]
|
|
193
|
+
set out[s["id"]] to s
|
|
194
|
+
each s in load_project()["specs"]
|
|
195
|
+
set out[s["id"]] to s
|
|
196
|
+
give out
|
|
197
|
+
|
|
198
|
+
export task get(id)
|
|
199
|
+
require file.read("workspace")
|
|
200
|
+
require file.read("workspace/*")
|
|
201
|
+
require file.read("agents")
|
|
202
|
+
require file.read("agents/*")
|
|
203
|
+
let a be all()
|
|
204
|
+
when contains(a, id)
|
|
205
|
+
give a[id]
|
|
206
|
+
give nothing
|
|
207
|
+
|
|
208
|
+
-- los que se eligen desde afuera (selector, --agent, API): worker es interno de delegate
|
|
209
|
+
export task names()
|
|
210
|
+
require file.read("workspace")
|
|
211
|
+
require file.read("workspace/*")
|
|
212
|
+
require file.read("agents")
|
|
213
|
+
require file.read("agents/*")
|
|
214
|
+
give sort(where(keys(all()), (n) => n != "worker"))
|
|
215
|
+
|
|
216
|
+
-- el agente que atiende cuando el sobre no dice `to`
|
|
217
|
+
export task default_agent()
|
|
218
|
+
require env("LAMPSON_*")
|
|
219
|
+
give lower(env("LAMPSON_AGENT", "build"))
|
|
220
|
+
|
|
221
|
+
-- ---------- tools de un agente del proyecto ----------
|
|
222
|
+
|
|
223
|
+
-- ¿`name` entra en el allow-list? exacto, o prefijo con * al final ("plugin_pedidos_*", "mcp_*")
|
|
224
|
+
export task allowed(allow_list, name)
|
|
225
|
+
each a in allow_list
|
|
226
|
+
when a == name
|
|
227
|
+
give true
|
|
228
|
+
when ends_with(a, "*")
|
|
229
|
+
when starts_with(name, slice(a, 0, length(a) - 1))
|
|
230
|
+
give true
|
|
231
|
+
give false
|
|
232
|
+
|
|
233
|
+
task builtin_tool_names(spec)
|
|
234
|
+
require exec
|
|
235
|
+
require time
|
|
236
|
+
require env("LAMPSON_*")
|
|
237
|
+
require env("OS")
|
|
238
|
+
require file.read(".lampson")
|
|
239
|
+
require file.read(".lampson/*")
|
|
240
|
+
give agents.tool_names({"tools": where(spec["tools"], (n) => not ends_with(n, "*"))})
|
|
241
|
+
|
|
242
|
+
export task registry_of(spec)
|
|
243
|
+
require exec
|
|
244
|
+
require time
|
|
245
|
+
require file(".lampson")
|
|
246
|
+
require file(".lampson/*")
|
|
247
|
+
require file("workspace")
|
|
248
|
+
require file("workspace/*")
|
|
249
|
+
require file.read("plugins")
|
|
250
|
+
require file.read("plugins/*")
|
|
251
|
+
require file.read("agents")
|
|
252
|
+
require file.read("agents/*")
|
|
253
|
+
require env("LAMPSON_*")
|
|
254
|
+
require env("OS")
|
|
255
|
+
when spec["builtin"]
|
|
256
|
+
give agents.registry_for(spec["id"])
|
|
257
|
+
let names be builtin_tool_names(spec)
|
|
258
|
+
let reg be tools.registry_subset(names)
|
|
259
|
+
when contains(names, "delegate")
|
|
260
|
+
set reg["delegate"] to agents.delegate
|
|
261
|
+
each m in mcp.names(false)
|
|
262
|
+
when allowed(spec["tools"], m)
|
|
263
|
+
set reg[m] to "mcp"
|
|
264
|
+
each m in plugins.names(false)
|
|
265
|
+
when allowed(spec["tools"], m)
|
|
266
|
+
set reg[m] to "plugin"
|
|
267
|
+
give reg
|
|
268
|
+
|
|
269
|
+
export task catalog_of(spec)
|
|
270
|
+
require exec
|
|
271
|
+
require time
|
|
272
|
+
require file(".lampson")
|
|
273
|
+
require file(".lampson/*")
|
|
274
|
+
require file("workspace")
|
|
275
|
+
require file("workspace/*")
|
|
276
|
+
require file.read("plugins")
|
|
277
|
+
require file.read("plugins/*")
|
|
278
|
+
require file.read("agents")
|
|
279
|
+
require file.read("agents/*")
|
|
280
|
+
require env("LAMPSON_*")
|
|
281
|
+
require env("OS")
|
|
282
|
+
when spec["builtin"]
|
|
283
|
+
give agents.catalog_for(spec["id"])
|
|
284
|
+
let names be builtin_tool_names(spec)
|
|
285
|
+
let cat be tools.catalog_subset(names)
|
|
286
|
+
when contains(names, "delegate")
|
|
287
|
+
set cat to append(cat, agents.DELEGATE_SPEC)
|
|
288
|
+
let extra be where(mcp.catalog(false) + plugins.catalog(false), (s) => allowed(spec["tools"], s["name"]))
|
|
289
|
+
give cat + extra
|
|
290
|
+
|
|
291
|
+
-- ---------- boot: cfg + system + opts de un agente ----------
|
|
292
|
+
|
|
293
|
+
task pick(a, b)
|
|
294
|
+
when a != nothing and a != ""
|
|
295
|
+
give a
|
|
296
|
+
give b
|
|
297
|
+
|
|
298
|
+
task spec_or_raise(id)
|
|
299
|
+
require file.read("workspace")
|
|
300
|
+
require file.read("workspace/*")
|
|
301
|
+
require file.read("agents")
|
|
302
|
+
require file.read("agents/*")
|
|
303
|
+
let spec be get(id)
|
|
304
|
+
when spec == nothing
|
|
305
|
+
raise("no agent named '" + id + "' · available: " + join(names(), ", "))
|
|
306
|
+
give spec
|
|
307
|
+
|
|
308
|
+
-- opts del loop para un agente: tools, pasos, presupuesto, buzón y modo ("" = el del agente, o LAMPSON_PERMISSION).
|
|
309
|
+
-- Barato de rearmar: la terminal lo hace en cada turno (catálogo fresco si un server MCP o un plugin cambió).
|
|
310
|
+
export task opts_for(id, mode, ask_fn)
|
|
311
|
+
require exec
|
|
312
|
+
require time
|
|
313
|
+
require file(".lampson")
|
|
314
|
+
require file(".lampson/*")
|
|
315
|
+
require file("workspace")
|
|
316
|
+
require file("workspace/*")
|
|
317
|
+
require file.read("skills")
|
|
318
|
+
require file.read("skills/*")
|
|
319
|
+
require file.read("plugins")
|
|
320
|
+
require file.read("plugins/*")
|
|
321
|
+
require file.read("agents")
|
|
322
|
+
require file.read("agents/*")
|
|
323
|
+
require file.read("memory")
|
|
324
|
+
require file.read("memory/*")
|
|
325
|
+
require env("LAMPSON_*")
|
|
326
|
+
require env("OS")
|
|
327
|
+
require secret("LAMPSON_*")
|
|
328
|
+
let spec be spec_or_raise(id)
|
|
329
|
+
let opts be loop.default_opts(registry_of(spec), catalog_of(spec), ask_fn)
|
|
330
|
+
-- pasos: los integrados como siempre (LAMPSON_MAX_STEPS o el del perfil); uno del proyecto, el suyo si lo fija
|
|
331
|
+
set opts to loop.with_steps(opts, when spec["builtin"] or spec["steps"] == 0 then agents.steps_for(when spec["builtin"] then id otherwise "build") otherwise spec["steps"])
|
|
332
|
+
when spec["budget_tokens"] > 0
|
|
333
|
+
set opts["budget_tokens"] to spec["budget_tokens"]
|
|
334
|
+
when length(keys(spec["scopes"])) > 0
|
|
335
|
+
set opts["scopes"] to spec["scopes"]
|
|
336
|
+
when length(keys(spec["approve"])) > 0
|
|
337
|
+
set opts["approve"] to spec["approve"]
|
|
338
|
+
-- buzón de subagentes: los integrados siempre (igual que antes); uno del proyecto solo si puede delegar
|
|
339
|
+
when spec["builtin"] or contains(spec["tools"], "delegate")
|
|
340
|
+
set opts to loop.with_inbox(opts, agents.parent_inbox, "")
|
|
341
|
+
let m be lower(pick(mode, spec["permission"]))
|
|
342
|
+
when m != ""
|
|
343
|
+
set opts to loop.with_mode(opts, m)
|
|
344
|
+
give opts
|
|
345
|
+
|
|
346
|
+
-- el mensaje system de un agente. base=lampson: el prompt de código (prompt.build) + el suyo; none: solo el suyo.
|
|
347
|
+
-- La terminal lo arma UNA vez por sesión (el prompt cache depende de que no cambie entre turnos).
|
|
348
|
+
export task system_for(id, cfg)
|
|
349
|
+
require exec
|
|
350
|
+
require time
|
|
351
|
+
require file(".lampson")
|
|
352
|
+
require file(".lampson/*")
|
|
353
|
+
require file("workspace")
|
|
354
|
+
require file("workspace/*")
|
|
355
|
+
require file.read("skills")
|
|
356
|
+
require file.read("skills/*")
|
|
357
|
+
require file.read("plugins")
|
|
358
|
+
require file.read("plugins/*")
|
|
359
|
+
require file.read("agents")
|
|
360
|
+
require file.read("agents/*")
|
|
361
|
+
require file.read("memory")
|
|
362
|
+
require file.read("memory/*")
|
|
363
|
+
require env("LAMPSON_*")
|
|
364
|
+
require env("OS")
|
|
365
|
+
require secret("LAMPSON_*")
|
|
366
|
+
let spec be spec_or_raise(id)
|
|
367
|
+
when spec["base"] == "lampson"
|
|
368
|
+
give {"role": "system", "content": prompt.build(agents.env_info(cfg), spec["system"])}
|
|
369
|
+
give {"role": "system", "content": spec["system"] + "\n\n# Environment\n- Date: " + format_time(now(), "%Y-%m-%d")}
|
|
370
|
+
|
|
371
|
+
-- over: {mode, provider, model} — lo que pide la puerta pisa lo del agente; vacío = lo del agente / lo configurado
|
|
372
|
+
export task boot(id, over, ask_fn)
|
|
373
|
+
require exec
|
|
374
|
+
require time
|
|
375
|
+
require file(".lampson")
|
|
376
|
+
require file(".lampson/*")
|
|
377
|
+
require file("workspace")
|
|
378
|
+
require file("workspace/*")
|
|
379
|
+
require file.read("skills")
|
|
380
|
+
require file.read("skills/*")
|
|
381
|
+
require file.read("plugins")
|
|
382
|
+
require file.read("plugins/*")
|
|
383
|
+
require file.read("agents")
|
|
384
|
+
require file.read("agents/*")
|
|
385
|
+
require file.read("memory")
|
|
386
|
+
require file.read("memory/*")
|
|
387
|
+
require env("LAMPSON_*")
|
|
388
|
+
require env("OS")
|
|
389
|
+
require secret("LAMPSON_*")
|
|
390
|
+
let spec be spec_or_raise(id)
|
|
391
|
+
let cfg be provider.config_for(pick(field(over, "provider", ""), spec["model"]["provider"]), pick(field(over, "model", ""), spec["model"]["model"]))
|
|
392
|
+
give {"cfg": cfg, "system": system_for(id, cfg), "opts": opts_for(id, field(over, "mode", ""), ask_fn), "agent": spec["id"], "spec": spec}
|
|
393
|
+
|
|
394
|
+
-- ---------- sesiones (agente, thread) ----------
|
|
395
|
+
|
|
396
|
+
export task thread_key(agent, thread)
|
|
397
|
+
give slice(decode(sha256(agent + "\n" + thread), "hex"), 0, 32)
|
|
398
|
+
|
|
399
|
+
-- id de sesión nuevo que no exista: timestamp + 4 dígitos
|
|
400
|
+
export task fresh_session()
|
|
401
|
+
require time
|
|
402
|
+
require random
|
|
403
|
+
require file.read(".lampson")
|
|
404
|
+
require file.read(".lampson/*")
|
|
405
|
+
let base be session.new_id()
|
|
406
|
+
let i be 0
|
|
407
|
+
while i < 20
|
|
408
|
+
set i to i + 1
|
|
409
|
+
let id be base + "-" + slice(text(10000 + floor(random() * 10000)), 1, 5)
|
|
410
|
+
when not session.exists(id)
|
|
411
|
+
give id
|
|
412
|
+
raise("could not allocate a session id")
|
|
413
|
+
|
|
414
|
+
-- la sesión de (agente, thread) si ya existe; "" si no
|
|
415
|
+
export task thread_session(agent, thread)
|
|
416
|
+
require file.read(".lampson")
|
|
417
|
+
require file.read(".lampson/*")
|
|
418
|
+
when thread == ""
|
|
419
|
+
give ""
|
|
420
|
+
try
|
|
421
|
+
let idx be json_decode(read_file(THREADS_DIR + "/" + thread_key(agent, thread) + ".json"))
|
|
422
|
+
when session.exists(idx["session"])
|
|
423
|
+
give idx["session"]
|
|
424
|
+
recover err
|
|
425
|
+
give ""
|
|
426
|
+
give ""
|
|
427
|
+
|
|
428
|
+
task remember_thread(agent, thread, sid)
|
|
429
|
+
require time
|
|
430
|
+
require file(".lampson")
|
|
431
|
+
require file(".lampson/*")
|
|
432
|
+
when thread != ""
|
|
433
|
+
write_file(THREADS_DIR + "/" + thread_key(agent, thread) + ".json", json_encode({"agent": agent, "thread": thread, "session": sid, "updated": format_time(now())}))
|
|
434
|
+
|
|
435
|
+
-- ---------- deliver ----------
|
|
436
|
+
|
|
437
|
+
-- abre la conversación sin correr el turno: {ctx, session, thread, messages, mode}. La usan las puertas que a
|
|
438
|
+
-- veces hacen otra cosa con la sesión (el `!comando` de la web); el resto llama a deliver.
|
|
439
|
+
export task open(sobre, hooks)
|
|
440
|
+
require exec
|
|
441
|
+
require time
|
|
442
|
+
require random
|
|
443
|
+
require file(".lampson")
|
|
444
|
+
require file(".lampson/*")
|
|
445
|
+
require file("workspace")
|
|
446
|
+
require file("workspace/*")
|
|
447
|
+
require file.read("skills")
|
|
448
|
+
require file.read("skills/*")
|
|
449
|
+
require file.read("plugins")
|
|
450
|
+
require file.read("plugins/*")
|
|
451
|
+
require file.read("agents")
|
|
452
|
+
require file.read("agents/*")
|
|
453
|
+
require file.read("memory")
|
|
454
|
+
require file.read("memory/*")
|
|
455
|
+
require env("LAMPSON_*")
|
|
456
|
+
require env("OS")
|
|
457
|
+
require secret("LAMPSON_*")
|
|
458
|
+
let id be lower(text(pick(field(sobre, "to", ""), default_agent())))
|
|
459
|
+
let ctx be boot(id, hooks, field(hooks, "ask_fn", nothing))
|
|
460
|
+
let thread be text(field(sobre, "thread", ""))
|
|
461
|
+
let sid be text(field(sobre, "session", ""))
|
|
462
|
+
when sid != "" and not session.exists(sid)
|
|
463
|
+
set sid to ""
|
|
464
|
+
when sid == ""
|
|
465
|
+
set sid to thread_session(ctx["agent"], thread)
|
|
466
|
+
let messages be [ctx["system"]]
|
|
467
|
+
let note be ""
|
|
468
|
+
when sid != ""
|
|
469
|
+
let doc be session.load(sid)
|
|
470
|
+
let prev be doc["messages"]
|
|
471
|
+
set messages to [ctx["system"]] + slice(prev, 1, length(prev))
|
|
472
|
+
when thread == "" and contains(doc["meta"], "thread")
|
|
473
|
+
set thread to text(doc["meta"]["thread"])
|
|
474
|
+
-- sesión guardada por otra corrida de lampson: sus procesos ya no viven — decírselo al modelo (una vez)
|
|
475
|
+
when session.from_other_run(doc)
|
|
476
|
+
set note to proc.resume_note(sid)
|
|
477
|
+
when note != ""
|
|
478
|
+
set messages to append(messages, {"role": "user", "content": note})
|
|
479
|
+
otherwise
|
|
480
|
+
set sid to fresh_session()
|
|
481
|
+
-- sesión actual (blackboard): la tool todo guarda su lista por sesión (lib/tools/todo.syn)
|
|
482
|
+
share {"id": sid} as "lampson:session"
|
|
483
|
+
give {"ctx": ctx, "session": sid, "thread": thread, "messages": messages, "mode": ctx["opts"]["permission_mode"], "note": note}
|
|
484
|
+
|
|
485
|
+
-- guarda la sesión con su agente y thread (y el índice del thread)
|
|
486
|
+
export task save(o, messages)
|
|
487
|
+
require time
|
|
488
|
+
require file(".lampson")
|
|
489
|
+
require file(".lampson/*")
|
|
490
|
+
require env("LAMPSON_*")
|
|
491
|
+
session.save(o["session"], messages, {"title": session.title_of(messages), "agent": o["ctx"]["agent"], "thread": o["thread"]})
|
|
492
|
+
remember_thread(o["ctx"]["agent"], o["thread"], o["session"])
|
|
493
|
+
|
|
494
|
+
-- corre UN turno sobre mensajes ya armados (el del usuario ya agregado): traza, run_turn, guarda.
|
|
495
|
+
-- o: {ctx: {cfg, opts, agent}, session, thread} — lo que devuelve open(), o lo que arma la terminal.
|
|
496
|
+
-- label: lo que se anota como turno del usuario en la traza ("" = el último mensaje de usuario).
|
|
497
|
+
export task turn(o, messages, label, hooks)
|
|
498
|
+
require net
|
|
499
|
+
require exec
|
|
500
|
+
require time
|
|
501
|
+
require random
|
|
502
|
+
require file(".lampson")
|
|
503
|
+
require file(".lampson/*")
|
|
504
|
+
require file("workspace")
|
|
505
|
+
require file("workspace/*")
|
|
506
|
+
require file.read("skills")
|
|
507
|
+
require file.read("skills/*")
|
|
508
|
+
require file.read("plugins")
|
|
509
|
+
require file.read("plugins/*")
|
|
510
|
+
require file.read("agents")
|
|
511
|
+
require file.read("agents/*")
|
|
512
|
+
require file.read("memory")
|
|
513
|
+
require file.read("memory/*")
|
|
514
|
+
require env("LAMPSON_*")
|
|
515
|
+
require env("OS")
|
|
516
|
+
require secret("LAMPSON_*")
|
|
517
|
+
let sid be o["session"]
|
|
518
|
+
let user_cb be field(hooks, "on_event", nothing)
|
|
519
|
+
task on_event(kind, data, tag)
|
|
520
|
+
trace.event(sid, kind, data, tag)
|
|
521
|
+
when user_cb != nothing
|
|
522
|
+
call(user_cb, {"kind": kind, "data": data, "tag": tag})
|
|
523
|
+
let said be label
|
|
524
|
+
when said == ""
|
|
525
|
+
each m in messages
|
|
526
|
+
when m["role"] == "user"
|
|
527
|
+
set said to text(m["content"])
|
|
528
|
+
let cfg be o["ctx"]["cfg"]
|
|
529
|
+
trace.user(sid, said, o["ctx"]["agent"], o["ctx"]["opts"]["permission_mode"], cfg["model"])
|
|
530
|
+
let result be loop.run_turn(cfg, messages, o["ctx"]["opts"], on_event)
|
|
531
|
+
trace.turn_end(sid, result)
|
|
532
|
+
save(o, result["messages"])
|
|
533
|
+
give result
|
|
534
|
+
|
|
535
|
+
export task deliver(sobre, hooks)
|
|
536
|
+
require net
|
|
537
|
+
require exec
|
|
538
|
+
require time
|
|
539
|
+
require random
|
|
540
|
+
require file(".lampson")
|
|
541
|
+
require file(".lampson/*")
|
|
542
|
+
require file("workspace")
|
|
543
|
+
require file("workspace/*")
|
|
544
|
+
require file.read("skills")
|
|
545
|
+
require file.read("skills/*")
|
|
546
|
+
require file.read("plugins")
|
|
547
|
+
require file.read("plugins/*")
|
|
548
|
+
require file.read("agents")
|
|
549
|
+
require file.read("agents/*")
|
|
550
|
+
require file.read("memory")
|
|
551
|
+
require file.read("memory/*")
|
|
552
|
+
require env("LAMPSON_*")
|
|
553
|
+
require env("OS")
|
|
554
|
+
require secret("LAMPSON_*")
|
|
555
|
+
let o be open(sobre, hooks)
|
|
556
|
+
let sid be o["session"]
|
|
557
|
+
let cfg be o["ctx"]["cfg"]
|
|
558
|
+
let start_cb be field(hooks, "on_start", nothing)
|
|
559
|
+
when start_cb != nothing
|
|
560
|
+
call(start_cb, {"info": {"session": sid, "agent": o["ctx"]["agent"], "thread": o["thread"], "mode": o["mode"], "provider": cfg["provider"], "model": cfg["model"], "has_key": cfg["has_key"]}})
|
|
561
|
+
when o["note"] != ""
|
|
562
|
+
trace.event(sid, "inbox", o["note"], "")
|
|
563
|
+
let cb be field(hooks, "on_event", nothing)
|
|
564
|
+
when cb != nothing
|
|
565
|
+
call(cb, {"kind": "inbox", "data": o["note"], "tag": ""})
|
|
566
|
+
let msg be text(field(sobre, "text", ""))
|
|
567
|
+
let umsg be {"role": "user", "content": msg}
|
|
568
|
+
let images be field(sobre, "images", [])
|
|
569
|
+
when length(images) > 0
|
|
570
|
+
set umsg to {"role": "user", "content": msg, "images": images}
|
|
571
|
+
let result be turn(o, append(o["messages"], umsg), msg, hooks)
|
|
572
|
+
give {"agent": o["ctx"]["agent"], "thread": o["thread"], "session": sid, "text": result["text"], "steps": result["steps"], "usage": result["usage"], "stopped": result["stopped"]}
|
package/lib/scope.syn
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
-- lib/scope.syn — alcance por ruta de las tools de archivos (opcional, por agente; spec/engine.md)
|
|
2
|
+
--
|
|
3
|
+
-- Un agente del proyecto puede limitar una tool de archivos a ciertas rutas del workspace:
|
|
4
|
+
-- "tools": ["read"] -- todo el workspace (lo de siempre)
|
|
5
|
+
-- "tools": [{"name": "read", "paths": [".lampson/tienda/*"]}] -- solo eso
|
|
6
|
+
-- Patrones, relativos a la raíz del workspace: "dir/*" (o "dir/**") = la carpeta y todo lo de adentro;
|
|
7
|
+
-- "a/b.json" = ese archivo; "*" = todo el workspace. Sin comodines en otro lugar (nada de "*.json").
|
|
8
|
+
--
|
|
9
|
+
-- Lo hace cumplir loop.execute ANTES de correr la tool (opts["scopes"] = {tool: [patrones]}), con el mismo
|
|
10
|
+
-- c.ws() que usan las tools para resolver la ruta: lo que se compara es la ruta real, no el texto del modelo.
|
|
11
|
+
-- Una tool que lee cualquier archivo por otro camino (bash, process, delegate, lsp, computer_use) no puede
|
|
12
|
+
-- convivir con tools limitadas en el mismo agente: lo rechaza runtime.validate.
|
|
13
|
+
|
|
14
|
+
use "./tools/common.syn" as c
|
|
15
|
+
|
|
16
|
+
export let SCOPABLE be ["read", "write", "edit", "ls", "find", "grep"]
|
|
17
|
+
export let ESCAPES be ["bash", "process", "delegate", "lsp", "computer_use"]
|
|
18
|
+
|
|
19
|
+
-- {ok, real, recursive, error}
|
|
20
|
+
export task norm(pattern)
|
|
21
|
+
let p be trim(replace(text(pattern), "\\", "/"))
|
|
22
|
+
let recursive be false
|
|
23
|
+
when p == "*" or p == "**"
|
|
24
|
+
give {"ok": true, "real": c.ROOT, "recursive": true, "error": ""}
|
|
25
|
+
when c.ends_with(p, "/**")
|
|
26
|
+
set p to slice(p, 0, length(p) - 3)
|
|
27
|
+
set recursive to true
|
|
28
|
+
otherwise when c.ends_with(p, "/*")
|
|
29
|
+
set p to slice(p, 0, length(p) - 2)
|
|
30
|
+
set recursive to true
|
|
31
|
+
when p == "" or contains(p, "*")
|
|
32
|
+
give {"ok": false, "real": "", "recursive": false, "error": "path pattern '" + text(pattern) + "': use 'dir/*' (a folder and everything in it), an exact file, or '*' (whole workspace)"}
|
|
33
|
+
try
|
|
34
|
+
give {"ok": true, "real": c.ws(p), "recursive": recursive, "error": ""}
|
|
35
|
+
recover err
|
|
36
|
+
give {"ok": false, "real": "", "recursive": false, "error": "path pattern '" + text(pattern) + "' escapes the workspace"}
|
|
37
|
+
|
|
38
|
+
task covers(n, real)
|
|
39
|
+
when n["recursive"]
|
|
40
|
+
give real == n["real"] or starts_with(real, n["real"] + "/")
|
|
41
|
+
give real == n["real"]
|
|
42
|
+
|
|
43
|
+
-- "" si la llamada entra en el alcance; si no, el motivo (lo ve el modelo)
|
|
44
|
+
export task violation(name, args, patterns)
|
|
45
|
+
let raw be nothing
|
|
46
|
+
when contains(args, "path")
|
|
47
|
+
set raw to args["path"]
|
|
48
|
+
let real be ""
|
|
49
|
+
try
|
|
50
|
+
set real to c.ws(raw)
|
|
51
|
+
recover err
|
|
52
|
+
give "path outside the workspace"
|
|
53
|
+
-- resultados largos guardados por el propio loop (.lampson/spill): ya pasaron por este alcance
|
|
54
|
+
when starts_with(real, ".lampson/spill/")
|
|
55
|
+
give ""
|
|
56
|
+
each p in patterns
|
|
57
|
+
let n be norm(p)
|
|
58
|
+
when n["ok"]
|
|
59
|
+
when covers(n, real)
|
|
60
|
+
give ""
|
|
61
|
+
let shown be when raw == nothing or text(raw) == "" then "." otherwise text(raw)
|
|
62
|
+
give "this agent's " + name + " is limited to " + join(patterns, ", ") + " — '" + shown + "' is outside"
|
package/lib/session.syn
CHANGED
|
@@ -80,7 +80,17 @@ export task list()
|
|
|
80
80
|
set out to out
|
|
81
81
|
recover err
|
|
82
82
|
give []
|
|
83
|
-
give sort_by(out, (s) => 0 -
|
|
83
|
+
give sort_by(out, (s) => 0 - id_key(s["id"]))
|
|
84
|
+
|
|
85
|
+
-- orden de los ids: YYYYMMDD-HHMMSS (terminal, web) y YYYYMMDD-HHMMSS-NNNN (lib/runtime.syn: dos threads en el mismo
|
|
86
|
+
-- segundo). 18 dígitos no entran exactos en un float → int, con el timestamp × 10000 + sufijo para que viejos y
|
|
87
|
+
-- nuevos queden en orden de creación. Un id que no es de dígitos (test-…) va al final.
|
|
88
|
+
export task id_key(id)
|
|
89
|
+
let d be replace(replace(id, "-", ""), "test", "")
|
|
90
|
+
when not matches(d, "[0-9]+") or length(d) < 14
|
|
91
|
+
give 0
|
|
92
|
+
let suffix be slice(d, 14, length(d))
|
|
93
|
+
give int(slice(d, 0, 14)) * 10000 + (when suffix == "" then 0 otherwise int(slice(suffix, 0, 4)))
|
|
84
94
|
|
|
85
95
|
-- Búsqueda de texto en las sesiones de ESTE proyecto (título y contenido de los mensajes user/assistant):
|
|
86
96
|
-- [{id, updated, title, snippet}] — snippet = el primer trozo donde aparece `q`. Vacío = todas (sin snippet).
|
package/lib/tools.syn
CHANGED
|
@@ -130,7 +130,7 @@ task plugin_tool(action, name, manifest, files)
|
|
|
130
130
|
give "no plugins found. A plugin is a folder with a plugin.json manifest: " + plugins.GLOBAL_DIR + "/<name>/ (global) or " + plugins.PROJECT_DIR + "/<name>/ (this project; you can create it with write). See the lampson skill for the manifest format."
|
|
131
131
|
let lines be []
|
|
132
132
|
each l in sm
|
|
133
|
-
set lines to append(lines, l["name"] + " (" + l["scope"] + ", " + l["kind"] + ", " + (when l["enabled"] then "ON" otherwise "off") + "): " + (when l["error"] != nothing then "BROKEN — " + text(l["error"]) otherwise text(length(l["tools"])) + " tools [" + join(l["tools"], ", ") + "]" + (when l["description"] != "" then " — " + l["description"] otherwise "")) + (when l["legacy"] then " [" + plugins.LEGACY_HINT + "]" otherwise ""))
|
|
133
|
+
set lines to append(lines, l["name"] + " (" + l["scope"] + ", " + l["kind"] + ", " + (when l["enabled"] then "ON" otherwise "off") + "): " + (when l["error"] != nothing then "BROKEN — " + text(l["error"]) otherwise (when l["channel"] then "CHANNEL (webhook /ch/" + l["name"] + "; no tools for you)" otherwise text(length(l["tools"])) + " tools [" + join(l["tools"], ", ") + "]") + (when l["description"] != "" then " — " + l["description"] otherwise "")) + (when l["legacy"] then " [" + plugins.LEGACY_HINT + "]" otherwise ""))
|
|
134
134
|
give join(lines, "\n")
|
|
135
135
|
|
|
136
136
|
let PLUGIN_SPEC be {
|