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/runtime.syn
ADDED
|
@@ -0,0 +1,630 @@
|
|
|
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
|
+
use "./teams.syn" as teams
|
|
46
|
+
use "./worktree.syn" as worktree
|
|
47
|
+
|
|
48
|
+
export let PROJECT_DIR be "workspace/.lampson/agents"
|
|
49
|
+
export let GLOBAL_DIR be "agents"
|
|
50
|
+
export let THREADS_DIR be ".lampson/threads"
|
|
51
|
+
export let SUFFIX be ".agent.json"
|
|
52
|
+
let ID_RE be "[a-z0-9][a-z0-9-]{0,39}"
|
|
53
|
+
let MODES be ["", "ask", "strict", "yolo"]
|
|
54
|
+
let BASES be ["lampson", "none"]
|
|
55
|
+
|
|
56
|
+
-- ---------- agentes ----------
|
|
57
|
+
|
|
58
|
+
task builtin(id)
|
|
59
|
+
let p be agents.PROFILES[id]
|
|
60
|
+
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"}
|
|
61
|
+
|
|
62
|
+
task field(raw, key, fallback)
|
|
63
|
+
when contains(raw, key)
|
|
64
|
+
when raw[key] != nothing
|
|
65
|
+
give raw[key]
|
|
66
|
+
give fallback
|
|
67
|
+
|
|
68
|
+
-- {ok, spec, error}: valida un .agent.json ya decodificado y completa los defaults
|
|
69
|
+
export task validate(raw, source)
|
|
70
|
+
when type_of(raw) != "map"
|
|
71
|
+
give {"ok": false, "spec": nothing, "error": source + ": the agent file must be a JSON object"}
|
|
72
|
+
let id be text(field(raw, "id", ""))
|
|
73
|
+
when not matches(id, ID_RE)
|
|
74
|
+
give {"ok": false, "spec": nothing, "error": source + ": id must be lowercase letters, digits and - (max 40), got '" + id + "'"}
|
|
75
|
+
let base be lower(text(field(raw, "base", "none")))
|
|
76
|
+
when not contains(BASES, base)
|
|
77
|
+
give {"ok": false, "spec": nothing, "error": source + ": base must be lampson or none"}
|
|
78
|
+
let perm be lower(text(field(raw, "permission", "")))
|
|
79
|
+
when not contains(MODES, perm)
|
|
80
|
+
give {"ok": false, "spec": nothing, "error": source + ": permission must be ask, strict or yolo"}
|
|
81
|
+
let tl be field(raw, "tools", [])
|
|
82
|
+
when type_of(tl) != "list"
|
|
83
|
+
give {"ok": false, "spec": nothing, "error": source + ": tools must be a list of tool names"}
|
|
84
|
+
-- cada tool: "read" (sin límite, permisos del modo del agente) o un objeto con
|
|
85
|
+
-- "paths": ["dir/*", …] alcance por ruta (lib/scope.syn; solo tools de archivos)
|
|
86
|
+
-- "approve": "auto" esta tool corre sin pedir aprobación (como en yolo, SOLO ella; lo prohibido en todos los
|
|
87
|
+
-- modos sigue prohibido) · "ask": esta tool pide aprobación SIEMPRE, aunque el agente sea yolo
|
|
88
|
+
let names be []
|
|
89
|
+
let scopes be {}
|
|
90
|
+
let approvals_by_tool be {}
|
|
91
|
+
each t in tl
|
|
92
|
+
when type_of(t) == "map"
|
|
93
|
+
let tn be text(field(t, "name", ""))
|
|
94
|
+
let ps be field(t, "paths", nothing)
|
|
95
|
+
let ap be lower(text(field(t, "approve", "")))
|
|
96
|
+
when tn == ""
|
|
97
|
+
give {"ok": false, "spec": nothing, "error": source + ": a tool object needs a name"}
|
|
98
|
+
when ps == nothing and ap == ""
|
|
99
|
+
give {"ok": false, "spec": nothing, "error": source + ": tool '" + tn + "': an object needs \"paths\" and/or \"approve\" (or write just \"" + tn + "\")"}
|
|
100
|
+
when ap != "" and ap != "auto" and ap != "ask"
|
|
101
|
+
give {"ok": false, "spec": nothing, "error": source + ": tool '" + tn + "': approve must be auto or ask"}
|
|
102
|
+
when ps != nothing
|
|
103
|
+
when type_of(ps) != "list" or length(ps) == 0
|
|
104
|
+
give {"ok": false, "spec": nothing, "error": source + ": tool '" + tn + "': paths must be a non-empty list"}
|
|
105
|
+
when not contains(scope.SCOPABLE, tn)
|
|
106
|
+
give {"ok": false, "spec": nothing, "error": source + ": only " + join(scope.SCOPABLE, ", ") + " can be limited by path ('" + tn + "' cannot)"}
|
|
107
|
+
each pp in ps
|
|
108
|
+
let n be scope.norm(pp)
|
|
109
|
+
when not n["ok"]
|
|
110
|
+
give {"ok": false, "spec": nothing, "error": source + ": " + n["error"]}
|
|
111
|
+
set scopes[tn] to apply((pp) => text(pp), ps)
|
|
112
|
+
when ap != ""
|
|
113
|
+
set approvals_by_tool[tn] to ap
|
|
114
|
+
set names to append(names, tn)
|
|
115
|
+
otherwise
|
|
116
|
+
set names to append(names, text(t))
|
|
117
|
+
when length(keys(scopes)) > 0
|
|
118
|
+
let leaks be where(names, (n) => contains(scope.ESCAPES, n))
|
|
119
|
+
when length(leaks) > 0
|
|
120
|
+
give {"ok": false, "spec": nothing, "error": source + ": " + join(leaks, ", ") + " can reach any file, so it cannot be combined with path-limited tools"}
|
|
121
|
+
let system be text(field(raw, "system", ""))
|
|
122
|
+
when base == "none" and trim(system) == ""
|
|
123
|
+
give {"ok": false, "spec": nothing, "error": source + ": an agent with base=none needs a system prompt"}
|
|
124
|
+
let steps be floor(number(field(raw, "steps", 0)))
|
|
125
|
+
let budget be number(field(raw, "budget_tokens", 0))
|
|
126
|
+
when steps < 0 or budget < 0
|
|
127
|
+
give {"ok": false, "spec": nothing, "error": source + ": steps and budget_tokens must be >= 0"}
|
|
128
|
+
-- espera de silencio del buzón (lib/mailbox.syn): false = sin espera; {quiet_s, max_s}; sin campo = la de la puerta
|
|
129
|
+
let buf be field(raw, "buffer", nothing)
|
|
130
|
+
when buf != nothing and buf != false
|
|
131
|
+
when type_of(buf) != "map"
|
|
132
|
+
give {"ok": false, "spec": nothing, "error": source + ": buffer must be false or {\"quiet_s\": 3, \"max_s\": 15}"}
|
|
133
|
+
let qs be number(field(buf, "quiet_s", 3))
|
|
134
|
+
let ms be number(field(buf, "max_s", 15))
|
|
135
|
+
when qs < 0 or ms < qs
|
|
136
|
+
give {"ok": false, "spec": nothing, "error": source + ": buffer needs 0 <= quiet_s <= max_s"}
|
|
137
|
+
set buf to {"quiet_s": qs, "max_s": ms}
|
|
138
|
+
let m be field(raw, "model", {})
|
|
139
|
+
when type_of(m) != "map"
|
|
140
|
+
set m to {}
|
|
141
|
+
let model be {"provider": lower(text(field(m, "provider", ""))), "model": text(field(m, "model", ""))}
|
|
142
|
+
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}}
|
|
143
|
+
|
|
144
|
+
-- {specs, errors}: los .agent.json del proyecto (un archivo roto no tumba a los demás: va a errors)
|
|
145
|
+
export task load_project()
|
|
146
|
+
require file.read("workspace")
|
|
147
|
+
require file.read("workspace/*")
|
|
148
|
+
require file.read("agents")
|
|
149
|
+
require file.read("agents/*")
|
|
150
|
+
give load_dir(PROJECT_DIR)
|
|
151
|
+
|
|
152
|
+
-- {specs, errors}: los globales de la instalación
|
|
153
|
+
export task load_global()
|
|
154
|
+
require file.read("agents")
|
|
155
|
+
require file.read("agents/*")
|
|
156
|
+
give load_dir(GLOBAL_DIR)
|
|
157
|
+
|
|
158
|
+
task load_dir(dir)
|
|
159
|
+
require file.read("workspace")
|
|
160
|
+
require file.read("workspace/*")
|
|
161
|
+
require file.read("agents")
|
|
162
|
+
require file.read("agents/*")
|
|
163
|
+
let specs be []
|
|
164
|
+
let errors be []
|
|
165
|
+
try
|
|
166
|
+
each e in list_dir(dir)
|
|
167
|
+
when not e["is_dir"] and ends_with(e["name"], SUFFIX)
|
|
168
|
+
let src be dir + "/" + e["name"]
|
|
169
|
+
try
|
|
170
|
+
let v be validate(json_decode(read_file(src)), src)
|
|
171
|
+
when v["ok"]
|
|
172
|
+
when v["spec"]["id"] + SUFFIX != e["name"]
|
|
173
|
+
set errors to append(errors, src + ": the file name must be <id>" + SUFFIX + " (id is '" + v["spec"]["id"] + "')")
|
|
174
|
+
otherwise
|
|
175
|
+
set specs to append(specs, v["spec"])
|
|
176
|
+
otherwise
|
|
177
|
+
set errors to append(errors, v["error"])
|
|
178
|
+
recover err
|
|
179
|
+
set errors to append(errors, src + ": " + text(err))
|
|
180
|
+
recover err
|
|
181
|
+
set specs to specs
|
|
182
|
+
give {"specs": specs, "errors": errors}
|
|
183
|
+
|
|
184
|
+
-- todos los agentes por id: integrados, después los globales y después los del proyecto (pisa el más cercano)
|
|
185
|
+
export task all()
|
|
186
|
+
require file.read("workspace")
|
|
187
|
+
require file.read("workspace/*")
|
|
188
|
+
require file.read("agents")
|
|
189
|
+
require file.read("agents/*")
|
|
190
|
+
let out be {}
|
|
191
|
+
each id in keys(agents.PROFILES)
|
|
192
|
+
set out[id] to builtin(id)
|
|
193
|
+
each s in load_global()["specs"]
|
|
194
|
+
set s["source"] to "global:" + s["source"]
|
|
195
|
+
set out[s["id"]] to s
|
|
196
|
+
each s in load_project()["specs"]
|
|
197
|
+
set out[s["id"]] to s
|
|
198
|
+
give out
|
|
199
|
+
|
|
200
|
+
export task get(id)
|
|
201
|
+
require file.read("workspace")
|
|
202
|
+
require file.read("workspace/*")
|
|
203
|
+
require file.read("agents")
|
|
204
|
+
require file.read("agents/*")
|
|
205
|
+
let a be all()
|
|
206
|
+
when contains(a, id)
|
|
207
|
+
give a[id]
|
|
208
|
+
give nothing
|
|
209
|
+
|
|
210
|
+
-- los que se eligen desde afuera (selector, --agent, API): worker es interno de delegate
|
|
211
|
+
export task names()
|
|
212
|
+
require file.read("workspace")
|
|
213
|
+
require file.read("workspace/*")
|
|
214
|
+
require file.read("agents")
|
|
215
|
+
require file.read("agents/*")
|
|
216
|
+
give sort(where(keys(all()), (n) => n != "worker"))
|
|
217
|
+
|
|
218
|
+
-- el agente que atiende cuando el sobre no dice `to`
|
|
219
|
+
export task default_agent()
|
|
220
|
+
require env("LAMPSON_*")
|
|
221
|
+
give lower(env("LAMPSON_AGENT", "build"))
|
|
222
|
+
|
|
223
|
+
-- ---------- tools de un agente del proyecto ----------
|
|
224
|
+
|
|
225
|
+
-- ¿`name` entra en el allow-list? exacto, o prefijo con * al final ("plugin_pedidos_*", "mcp_*")
|
|
226
|
+
export task allowed(allow_list, name)
|
|
227
|
+
each a in allow_list
|
|
228
|
+
when a == name
|
|
229
|
+
give true
|
|
230
|
+
when ends_with(a, "*")
|
|
231
|
+
when starts_with(name, slice(a, 0, length(a) - 1))
|
|
232
|
+
give true
|
|
233
|
+
give false
|
|
234
|
+
|
|
235
|
+
task builtin_tool_names(spec)
|
|
236
|
+
require exec
|
|
237
|
+
require time
|
|
238
|
+
require env("LAMPSON_*")
|
|
239
|
+
require env("OS")
|
|
240
|
+
require file.read(".lampson")
|
|
241
|
+
require file.read(".lampson/*")
|
|
242
|
+
give agents.tool_names({"tools": where(spec["tools"], (n) => not ends_with(n, "*"))})
|
|
243
|
+
|
|
244
|
+
-- ¿tiene send y board? los miembros de un equipo siempre; un integrado nunca (el de programación no coordina equipos)
|
|
245
|
+
task team_tools(spec)
|
|
246
|
+
require file.read("workspace")
|
|
247
|
+
require file.read("workspace/*")
|
|
248
|
+
when spec["builtin"]
|
|
249
|
+
give false
|
|
250
|
+
give contains(spec["tools"], "send") or contains(spec["tools"], "board") or length(teams.teams_of(spec["id"])) > 0
|
|
251
|
+
|
|
252
|
+
export task registry_of(spec)
|
|
253
|
+
require exec
|
|
254
|
+
require time
|
|
255
|
+
require file(".lampson")
|
|
256
|
+
require file(".lampson/*")
|
|
257
|
+
require file("workspace")
|
|
258
|
+
require file("workspace/*")
|
|
259
|
+
require file.read("plugins")
|
|
260
|
+
require file.read("plugins/*")
|
|
261
|
+
require file.read("agents")
|
|
262
|
+
require file.read("agents/*")
|
|
263
|
+
require env("LAMPSON_*")
|
|
264
|
+
require env("OS")
|
|
265
|
+
when spec["builtin"]
|
|
266
|
+
give agents.registry_for(spec["id"])
|
|
267
|
+
let names be builtin_tool_names(spec)
|
|
268
|
+
let reg be tools.registry_subset(names)
|
|
269
|
+
when contains(names, "delegate")
|
|
270
|
+
set reg["delegate"] to agents.delegate
|
|
271
|
+
each m in mcp.names(false)
|
|
272
|
+
when allowed(spec["tools"], m)
|
|
273
|
+
set reg[m] to "mcp"
|
|
274
|
+
each m in plugins.names(false)
|
|
275
|
+
when allowed(spec["tools"], m)
|
|
276
|
+
set reg[m] to "plugin"
|
|
277
|
+
-- equipos (lib/teams.syn): un miembro coordina con send y board; fuera de un equipo, solo si los lista
|
|
278
|
+
when team_tools(spec)
|
|
279
|
+
set reg["send"] to teams.send_tool
|
|
280
|
+
set reg["board"] to teams.board_tool
|
|
281
|
+
give reg
|
|
282
|
+
|
|
283
|
+
export task catalog_of(spec)
|
|
284
|
+
require exec
|
|
285
|
+
require time
|
|
286
|
+
require file(".lampson")
|
|
287
|
+
require file(".lampson/*")
|
|
288
|
+
require file("workspace")
|
|
289
|
+
require file("workspace/*")
|
|
290
|
+
require file.read("plugins")
|
|
291
|
+
require file.read("plugins/*")
|
|
292
|
+
require file.read("agents")
|
|
293
|
+
require file.read("agents/*")
|
|
294
|
+
require env("LAMPSON_*")
|
|
295
|
+
require env("OS")
|
|
296
|
+
when spec["builtin"]
|
|
297
|
+
give agents.catalog_for(spec["id"])
|
|
298
|
+
let names be builtin_tool_names(spec)
|
|
299
|
+
let cat be tools.catalog_subset(names)
|
|
300
|
+
when contains(names, "delegate")
|
|
301
|
+
set cat to append(cat, agents.DELEGATE_SPEC)
|
|
302
|
+
let extra be where(mcp.catalog(false) + plugins.catalog(false), (s) => allowed(spec["tools"], s["name"]))
|
|
303
|
+
when team_tools(spec)
|
|
304
|
+
set extra to extra + [teams.SEND_SPEC, teams.BOARD_SPEC]
|
|
305
|
+
give cat + extra
|
|
306
|
+
|
|
307
|
+
-- ---------- boot: cfg + system + opts de un agente ----------
|
|
308
|
+
|
|
309
|
+
task pick(a, b)
|
|
310
|
+
when a != nothing and a != ""
|
|
311
|
+
give a
|
|
312
|
+
give b
|
|
313
|
+
|
|
314
|
+
task spec_or_raise(id)
|
|
315
|
+
require file.read("workspace")
|
|
316
|
+
require file.read("workspace/*")
|
|
317
|
+
require file.read("agents")
|
|
318
|
+
require file.read("agents/*")
|
|
319
|
+
let spec be get(id)
|
|
320
|
+
when spec == nothing
|
|
321
|
+
raise("no agent named '" + id + "' · available: " + join(names(), ", "))
|
|
322
|
+
give spec
|
|
323
|
+
|
|
324
|
+
-- opts del loop para un agente: tools, pasos, presupuesto, buzón y modo ("" = el del agente, o LAMPSON_PERMISSION).
|
|
325
|
+
-- Barato de rearmar: la terminal lo hace en cada turno (catálogo fresco si un server MCP o un plugin cambió).
|
|
326
|
+
export task opts_for(id, mode, ask_fn)
|
|
327
|
+
require exec
|
|
328
|
+
require time
|
|
329
|
+
require file(".lampson")
|
|
330
|
+
require file(".lampson/*")
|
|
331
|
+
require file("workspace")
|
|
332
|
+
require file("workspace/*")
|
|
333
|
+
require file.read("skills")
|
|
334
|
+
require file.read("skills/*")
|
|
335
|
+
require file.read("plugins")
|
|
336
|
+
require file.read("plugins/*")
|
|
337
|
+
require file.read("agents")
|
|
338
|
+
require file.read("agents/*")
|
|
339
|
+
require file.read("memory")
|
|
340
|
+
require file.read("memory/*")
|
|
341
|
+
require env("LAMPSON_*")
|
|
342
|
+
require env("OS")
|
|
343
|
+
require secret("LAMPSON_*")
|
|
344
|
+
let spec be spec_or_raise(id)
|
|
345
|
+
let opts be loop.default_opts(registry_of(spec), catalog_of(spec), ask_fn)
|
|
346
|
+
set opts["agent_id"] to spec["id"]
|
|
347
|
+
-- pasos: los integrados como siempre (LAMPSON_MAX_STEPS o el del perfil); uno del proyecto, el suyo si lo fija
|
|
348
|
+
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"])
|
|
349
|
+
when spec["budget_tokens"] > 0
|
|
350
|
+
set opts["budget_tokens"] to spec["budget_tokens"]
|
|
351
|
+
when length(keys(spec["scopes"])) > 0
|
|
352
|
+
set opts["scopes"] to spec["scopes"]
|
|
353
|
+
when length(keys(spec["approve"])) > 0
|
|
354
|
+
set opts["approve"] to spec["approve"]
|
|
355
|
+
-- buzón de subagentes: los integrados siempre (igual que antes); uno del proyecto solo si puede delegar
|
|
356
|
+
when spec["builtin"] or contains(spec["tools"], "delegate")
|
|
357
|
+
set opts to loop.with_inbox(opts, agents.parent_inbox, "")
|
|
358
|
+
let m be lower(pick(mode, spec["permission"]))
|
|
359
|
+
when m != ""
|
|
360
|
+
set opts to loop.with_mode(opts, m)
|
|
361
|
+
give opts
|
|
362
|
+
|
|
363
|
+
-- el mensaje system de un agente. base=lampson: el prompt de código (prompt.build) + el suyo; none: solo el suyo.
|
|
364
|
+
-- La terminal lo arma UNA vez por sesión (el prompt cache depende de que no cambie entre turnos).
|
|
365
|
+
export task system_for(id, cfg)
|
|
366
|
+
require exec
|
|
367
|
+
require time
|
|
368
|
+
require file(".lampson")
|
|
369
|
+
require file(".lampson/*")
|
|
370
|
+
require file("workspace")
|
|
371
|
+
require file("workspace/*")
|
|
372
|
+
require file.read("skills")
|
|
373
|
+
require file.read("skills/*")
|
|
374
|
+
require file.read("plugins")
|
|
375
|
+
require file.read("plugins/*")
|
|
376
|
+
require file.read("agents")
|
|
377
|
+
require file.read("agents/*")
|
|
378
|
+
require file.read("memory")
|
|
379
|
+
require file.read("memory/*")
|
|
380
|
+
require env("LAMPSON_*")
|
|
381
|
+
require env("OS")
|
|
382
|
+
require secret("LAMPSON_*")
|
|
383
|
+
let spec be spec_or_raise(id)
|
|
384
|
+
-- los plugins traen su proceso: las instrucciones de los que tienen tools en el catálogo de este agente
|
|
385
|
+
let extra be plugins.instructions(apply(catalog_of(spec), (s) => s["name"]))
|
|
386
|
+
-- su equipo: rol, a quién reporta, a quién le puede escribir, cómo se usa el tablero (lib/teams.syn)
|
|
387
|
+
when not spec["builtin"]
|
|
388
|
+
set extra to extra + teams.instructions(spec["id"])
|
|
389
|
+
when spec["base"] == "lampson"
|
|
390
|
+
give {"role": "system", "content": prompt.build(agents.env_info(cfg), spec["system"]) + extra}
|
|
391
|
+
give {"role": "system", "content": spec["system"] + extra + "\n\n# Environment\n- Date: " + format_time(now(), "%Y-%m-%d")}
|
|
392
|
+
|
|
393
|
+
-- over: {mode, provider, model} — lo que pide la puerta pisa lo del agente; vacío = lo del agente / lo configurado
|
|
394
|
+
export task boot(id, over, ask_fn)
|
|
395
|
+
require exec
|
|
396
|
+
require time
|
|
397
|
+
require file(".lampson")
|
|
398
|
+
require file(".lampson/*")
|
|
399
|
+
require file("workspace")
|
|
400
|
+
require file("workspace/*")
|
|
401
|
+
require file.read("skills")
|
|
402
|
+
require file.read("skills/*")
|
|
403
|
+
require file.read("plugins")
|
|
404
|
+
require file.read("plugins/*")
|
|
405
|
+
require file.read("agents")
|
|
406
|
+
require file.read("agents/*")
|
|
407
|
+
require file.read("memory")
|
|
408
|
+
require file.read("memory/*")
|
|
409
|
+
require env("LAMPSON_*")
|
|
410
|
+
require env("OS")
|
|
411
|
+
require secret("LAMPSON_*")
|
|
412
|
+
let spec be spec_or_raise(id)
|
|
413
|
+
let cfg be provider.config_for(pick(field(over, "provider", ""), spec["model"]["provider"]), pick(field(over, "model", ""), spec["model"]["model"]))
|
|
414
|
+
give {"cfg": cfg, "system": system_for(id, cfg), "opts": opts_for(id, field(over, "mode", ""), ask_fn), "agent": spec["id"], "spec": spec}
|
|
415
|
+
|
|
416
|
+
-- ---------- sesiones (agente, thread) ----------
|
|
417
|
+
|
|
418
|
+
export task thread_key(agent, thread)
|
|
419
|
+
give slice(decode(sha256(agent + "\n" + thread), "hex"), 0, 32)
|
|
420
|
+
|
|
421
|
+
-- id de sesión nuevo que no exista: timestamp + 4 dígitos
|
|
422
|
+
export task fresh_session()
|
|
423
|
+
require time
|
|
424
|
+
require random
|
|
425
|
+
require file.read(".lampson")
|
|
426
|
+
require file.read(".lampson/*")
|
|
427
|
+
let base be session.new_id()
|
|
428
|
+
let i be 0
|
|
429
|
+
while i < 20
|
|
430
|
+
set i to i + 1
|
|
431
|
+
let id be base + "-" + slice(text(10000 + floor(random() * 10000)), 1, 5)
|
|
432
|
+
when not session.exists(id)
|
|
433
|
+
give id
|
|
434
|
+
raise("could not allocate a session id")
|
|
435
|
+
|
|
436
|
+
-- la sesión de (agente, thread) si ya existe; "" si no
|
|
437
|
+
export task thread_session(agent, thread)
|
|
438
|
+
require file.read(".lampson")
|
|
439
|
+
require file.read(".lampson/*")
|
|
440
|
+
when thread == ""
|
|
441
|
+
give ""
|
|
442
|
+
try
|
|
443
|
+
let idx be json_decode(read_file(THREADS_DIR + "/" + thread_key(agent, thread) + ".json"))
|
|
444
|
+
when session.exists(idx["session"])
|
|
445
|
+
give idx["session"]
|
|
446
|
+
recover err
|
|
447
|
+
give ""
|
|
448
|
+
give ""
|
|
449
|
+
|
|
450
|
+
task remember_thread(agent, thread, sid)
|
|
451
|
+
require time
|
|
452
|
+
require file(".lampson")
|
|
453
|
+
require file(".lampson/*")
|
|
454
|
+
when thread != ""
|
|
455
|
+
write_file(THREADS_DIR + "/" + thread_key(agent, thread) + ".json", json_encode({"agent": agent, "thread": thread, "session": sid, "updated": format_time(now())}))
|
|
456
|
+
|
|
457
|
+
-- ---------- deliver ----------
|
|
458
|
+
|
|
459
|
+
-- abre la conversación sin correr el turno: {ctx, session, thread, messages, mode}. La usan las puertas que a
|
|
460
|
+
-- veces hacen otra cosa con la sesión (el `!comando` de la web); el resto llama a deliver.
|
|
461
|
+
export task open(sobre, hooks)
|
|
462
|
+
require db(".lampson/teams.db")
|
|
463
|
+
require exec
|
|
464
|
+
require time
|
|
465
|
+
require random
|
|
466
|
+
require file(".lampson")
|
|
467
|
+
require file(".lampson/*")
|
|
468
|
+
require file("workspace")
|
|
469
|
+
require file("workspace/*")
|
|
470
|
+
require file.read("skills")
|
|
471
|
+
require file.read("skills/*")
|
|
472
|
+
require file.read("plugins")
|
|
473
|
+
require file.read("plugins/*")
|
|
474
|
+
require file.read("agents")
|
|
475
|
+
require file.read("agents/*")
|
|
476
|
+
require file.read("memory")
|
|
477
|
+
require file.read("memory/*")
|
|
478
|
+
require env("LAMPSON_*")
|
|
479
|
+
require env("OS")
|
|
480
|
+
require secret("LAMPSON_*")
|
|
481
|
+
let id be lower(text(pick(field(sobre, "to", ""), default_agent())))
|
|
482
|
+
let ctx be boot(id, hooks, field(hooks, "ask_fn", nothing))
|
|
483
|
+
let thread be text(field(sobre, "thread", ""))
|
|
484
|
+
let sid be text(field(sobre, "session", ""))
|
|
485
|
+
when sid != "" and not session.exists(sid)
|
|
486
|
+
set sid to ""
|
|
487
|
+
when sid == ""
|
|
488
|
+
set sid to thread_session(ctx["agent"], thread)
|
|
489
|
+
let messages be [ctx["system"]]
|
|
490
|
+
let note be ""
|
|
491
|
+
when sid != ""
|
|
492
|
+
let doc be session.load(sid)
|
|
493
|
+
let prev be doc["messages"]
|
|
494
|
+
set messages to [ctx["system"]] + slice(prev, 1, length(prev))
|
|
495
|
+
when thread == "" and contains(doc["meta"], "thread")
|
|
496
|
+
set thread to text(doc["meta"]["thread"])
|
|
497
|
+
-- sesión guardada por otra corrida de lampson: sus procesos ya no viven — decírselo al modelo (una vez)
|
|
498
|
+
when session.from_other_run(doc)
|
|
499
|
+
set note to proc.resume_note(sid)
|
|
500
|
+
when note != ""
|
|
501
|
+
set messages to append(messages, {"role": "user", "content": note})
|
|
502
|
+
otherwise
|
|
503
|
+
set sid to fresh_session()
|
|
504
|
+
-- equipos y objetivos (lib/teams.syn): la conversación dice en qué trabaja; send/board/objective lo reciben como
|
|
505
|
+
-- _scope (lib/loop.syn). En un objetivo el agente tiene su tablero y la tool objective aunque no esté en un equipo.
|
|
506
|
+
when starts_with(thread, "team:") or starts_with(thread, "objective:")
|
|
507
|
+
let copts be ctx["opts"]
|
|
508
|
+
set copts["scope"] to thread
|
|
509
|
+
-- en un equipo con git, cada agente en su copia (lib/worktree.syn); "worktrees": false en el equipo lo apaga
|
|
510
|
+
when starts_with(thread, "team:")
|
|
511
|
+
let tm be teams.get(slice(thread, 5, length(thread)))
|
|
512
|
+
when tm != nothing and tm["worktrees"]
|
|
513
|
+
let me be teams.member(tm, ctx["agent"])
|
|
514
|
+
let wt be worktree.ensure(tm["id"], ctx["agent"], {"name": (when me != nothing and me["role"] != ctx["agent"] then me["role"] + " · " + ctx["agent"] otherwise ctx["agent"]) + " (Lampson)", "email": ctx["agent"] + "@lampson.local"})
|
|
515
|
+
when wt["ok"]
|
|
516
|
+
set copts["root"] to wt["root"]
|
|
517
|
+
when tm != nothing and teams.team_objective(tm["id"], ctx["agent"]) != nothing
|
|
518
|
+
let treg be copts["registry"]
|
|
519
|
+
set treg["objective"] to teams.objective_tool
|
|
520
|
+
set copts["registry"] to treg
|
|
521
|
+
set copts["catalog"] to where(copts["catalog"], (s) => s["name"] != "objective") + [teams.OBJECTIVE_SPEC]
|
|
522
|
+
when starts_with(thread, "objective:")
|
|
523
|
+
let reg be copts["registry"]
|
|
524
|
+
set reg["board"] to teams.board_tool
|
|
525
|
+
set reg["objective"] to teams.objective_tool
|
|
526
|
+
set copts["registry"] to reg
|
|
527
|
+
set copts["catalog"] to where(copts["catalog"], (s) => s["name"] != "board" and s["name"] != "objective") + [teams.BOARD_SPEC, teams.OBJECTIVE_SPEC]
|
|
528
|
+
set ctx["opts"] to copts
|
|
529
|
+
-- sesión actual (blackboard): la tool todo guarda su lista por sesión (lib/tools/todo.syn)
|
|
530
|
+
share {"id": sid} as "lampson:session"
|
|
531
|
+
give {"ctx": ctx, "session": sid, "thread": thread, "messages": messages, "mode": ctx["opts"]["permission_mode"], "note": note}
|
|
532
|
+
|
|
533
|
+
-- guarda la sesión con su agente y thread (y el índice del thread)
|
|
534
|
+
export task save(o, messages)
|
|
535
|
+
require time
|
|
536
|
+
require file(".lampson")
|
|
537
|
+
require file(".lampson/*")
|
|
538
|
+
require env("LAMPSON_*")
|
|
539
|
+
session.save(o["session"], messages, {"title": session.title_of(messages), "agent": o["ctx"]["agent"], "thread": o["thread"]})
|
|
540
|
+
remember_thread(o["ctx"]["agent"], o["thread"], o["session"])
|
|
541
|
+
|
|
542
|
+
-- corre UN turno sobre mensajes ya armados (el del usuario ya agregado): traza, run_turn, guarda.
|
|
543
|
+
-- o: {ctx: {cfg, opts, agent}, session, thread} — lo que devuelve open(), o lo que arma la terminal.
|
|
544
|
+
-- label: lo que se anota como turno del usuario en la traza ("" = el último mensaje de usuario).
|
|
545
|
+
export task turn(o, messages, label, hooks)
|
|
546
|
+
require db(".lampson/teams.db")
|
|
547
|
+
require net
|
|
548
|
+
require exec
|
|
549
|
+
require time
|
|
550
|
+
require random
|
|
551
|
+
require file(".lampson")
|
|
552
|
+
require file(".lampson/*")
|
|
553
|
+
require file("workspace")
|
|
554
|
+
require file("workspace/*")
|
|
555
|
+
require file.read("skills")
|
|
556
|
+
require file.read("skills/*")
|
|
557
|
+
require file.read("plugins")
|
|
558
|
+
require file.read("plugins/*")
|
|
559
|
+
require file.read("agents")
|
|
560
|
+
require file.read("agents/*")
|
|
561
|
+
require file.read("memory")
|
|
562
|
+
require file.read("memory/*")
|
|
563
|
+
require env("LAMPSON_*")
|
|
564
|
+
require env("OS")
|
|
565
|
+
require secret("LAMPSON_*")
|
|
566
|
+
let sid be o["session"]
|
|
567
|
+
let user_cb be field(hooks, "on_event", nothing)
|
|
568
|
+
task on_event(kind, data, tag)
|
|
569
|
+
trace.event(sid, kind, data, tag)
|
|
570
|
+
when user_cb != nothing
|
|
571
|
+
call(user_cb, {"kind": kind, "data": data, "tag": tag})
|
|
572
|
+
let said be label
|
|
573
|
+
when said == ""
|
|
574
|
+
each m in messages
|
|
575
|
+
when m["role"] == "user"
|
|
576
|
+
set said to text(m["content"])
|
|
577
|
+
let cfg be o["ctx"]["cfg"]
|
|
578
|
+
trace.user(sid, said, o["ctx"]["agent"], o["ctx"]["opts"]["permission_mode"], cfg["model"])
|
|
579
|
+
let result be loop.run_turn(cfg, messages, o["ctx"]["opts"], on_event)
|
|
580
|
+
trace.turn_end(sid, result)
|
|
581
|
+
-- presupuesto de equipos: lo que gastó este turno (los cacheados pesan 10%, como en el loop)
|
|
582
|
+
let cost be result["usage"]["input"] - result["usage"]["cached"] + floor(result["usage"]["cached"] / 10) + result["usage"]["output"]
|
|
583
|
+
let th be text(field(o, "thread", ""))
|
|
584
|
+
when starts_with(th, "team:")
|
|
585
|
+
teams.record_spend(o["ctx"]["agent"], cost)
|
|
586
|
+
teams.team_turn_end(slice(th, 5, length(th)), o["ctx"]["agent"], cost)
|
|
587
|
+
when starts_with(th, "objective:")
|
|
588
|
+
teams.obj_turn_end(floor(number(slice(th, 10, length(th)))), cost)
|
|
589
|
+
save(o, result["messages"])
|
|
590
|
+
give result
|
|
591
|
+
|
|
592
|
+
export task deliver(sobre, hooks)
|
|
593
|
+
require db(".lampson/teams.db")
|
|
594
|
+
require net
|
|
595
|
+
require exec
|
|
596
|
+
require time
|
|
597
|
+
require random
|
|
598
|
+
require file(".lampson")
|
|
599
|
+
require file(".lampson/*")
|
|
600
|
+
require file("workspace")
|
|
601
|
+
require file("workspace/*")
|
|
602
|
+
require file.read("skills")
|
|
603
|
+
require file.read("skills/*")
|
|
604
|
+
require file.read("plugins")
|
|
605
|
+
require file.read("plugins/*")
|
|
606
|
+
require file.read("agents")
|
|
607
|
+
require file.read("agents/*")
|
|
608
|
+
require file.read("memory")
|
|
609
|
+
require file.read("memory/*")
|
|
610
|
+
require env("LAMPSON_*")
|
|
611
|
+
require env("OS")
|
|
612
|
+
require secret("LAMPSON_*")
|
|
613
|
+
let o be open(sobre, hooks)
|
|
614
|
+
let sid be o["session"]
|
|
615
|
+
let cfg be o["ctx"]["cfg"]
|
|
616
|
+
let start_cb be field(hooks, "on_start", nothing)
|
|
617
|
+
when start_cb != nothing
|
|
618
|
+
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"]}})
|
|
619
|
+
when o["note"] != ""
|
|
620
|
+
trace.event(sid, "inbox", o["note"], "")
|
|
621
|
+
let cb be field(hooks, "on_event", nothing)
|
|
622
|
+
when cb != nothing
|
|
623
|
+
call(cb, {"kind": "inbox", "data": o["note"], "tag": ""})
|
|
624
|
+
let msg be text(field(sobre, "text", ""))
|
|
625
|
+
let umsg be {"role": "user", "content": msg}
|
|
626
|
+
let images be field(sobre, "images", [])
|
|
627
|
+
when length(images) > 0
|
|
628
|
+
set umsg to {"role": "user", "content": msg, "images": images}
|
|
629
|
+
let result be turn(o, append(o["messages"], umsg), msg, hooks)
|
|
630
|
+
give {"agent": o["ctx"]["agent"], "thread": o["thread"], "session": sid, "text": result["text"], "steps": result["steps"], "usage": result["usage"], "stopped": result["stopped"]}
|