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/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 {
|
package/lib/workspaces.syn
CHANGED
|
@@ -27,8 +27,8 @@ export let HUB_FILE be "hub.syn"
|
|
|
27
27
|
export let HUB_TEMPLATE be "hub.tpl.syn"
|
|
28
28
|
-- rango alto para no chocar con lo que usan las apps (3000, 5173, 8000, 8080-8090…): solo el hub queda en 8080
|
|
29
29
|
let FIRST_PORT be 47101
|
|
30
|
-
let LINKS be ["lib", "public", "skills", "plugins", "memory"]
|
|
31
|
-
let COPIES be ["web.syn", "chat.syn"]
|
|
30
|
+
let LINKS be ["lib", "public", "skills", "plugins", "memory", "agents"]
|
|
31
|
+
let COPIES be ["web.syn", "chat.syn", "run.syn"]
|
|
32
32
|
|
|
33
33
|
task is_win()
|
|
34
34
|
give env("OS", "") == "Windows_NT"
|
|
@@ -215,6 +215,7 @@ export task prepare(w)
|
|
|
215
215
|
require file(".lampson/*")
|
|
216
216
|
require file.read("web.syn")
|
|
217
217
|
require file.read("chat.syn")
|
|
218
|
+
require file.read("run.syn")
|
|
218
219
|
let d be dir_of(w["slug"])
|
|
219
220
|
let h be home()
|
|
220
221
|
let sep be when is_win() then "\\" otherwise "/"
|
|
@@ -235,7 +236,13 @@ export task prepare(w)
|
|
|
235
236
|
ensure_link(d + "/" + n, h + sep + n)
|
|
236
237
|
ensure_link(d + "/.lampson/global", h + sep + ".lampson")
|
|
237
238
|
each f in COPIES
|
|
238
|
-
|
|
239
|
+
-- run.syn (lampson run, 0.2.11): un hub viejo en memoria no tiene el permiso de leerlo → se saltea y el
|
|
240
|
+
-- workspace enciende igual; la próxima vez que el hub arranque con el template nuevo se copia
|
|
241
|
+
try
|
|
242
|
+
write_file(d + "/" + f, read_file(f))
|
|
243
|
+
recover err
|
|
244
|
+
when f != "run.syn"
|
|
245
|
+
raise(err)
|
|
239
246
|
give d
|
|
240
247
|
|
|
241
248
|
-- estado guardado ANTES de los workspaces (en <home>/.lampson, separado por slug en cada registro): sesiones y trazas
|
|
@@ -295,6 +302,7 @@ export task ensure(path)
|
|
|
295
302
|
require file(".lampson/*")
|
|
296
303
|
require file.read("web.syn")
|
|
297
304
|
require file.read("chat.syn")
|
|
305
|
+
require file.read("run.syn")
|
|
298
306
|
require file.read("hub.tpl.syn")
|
|
299
307
|
require file("hub.syn")
|
|
300
308
|
migrate_ports()
|
|
@@ -353,6 +361,14 @@ task hub_routes(w)
|
|
|
353
361
|
each m in ["GET", "POST"]
|
|
354
362
|
set lines to append(lines, " route \"" + m + " /w/" + w["slug"] + "/api/*path\" requires auth\n proxy to \"" + base + "\"")
|
|
355
363
|
set lines to append(lines, " route \"GET /w/" + w["slug"] + "/approve/*path\"\n proxy to \"" + base + "\"")
|
|
364
|
+
-- la API /v1 para apps (lib/api.syn) tampoco: la autentica el proceso del workspace con su API key (Bearer lk_…).
|
|
365
|
+
-- POST /v1/messages espera al agente (buzón + turno con tools): sin `timeout` el proxy corta a los 30 s con 504
|
|
366
|
+
-- (visto 2026-10-16 con una ráfaga real); 600 s, como el bash más largo
|
|
367
|
+
each m in ["GET", "POST"]
|
|
368
|
+
set lines to append(lines, " route \"" + m + " /w/" + w["slug"] + "/v1/*path\"\n timeout 600\n proxy to \"" + base + "\"")
|
|
369
|
+
-- los webhooks de los canales (lib/channels.syn) tampoco: la firma de la plataforma la verifica el plugin
|
|
370
|
+
each m in ["GET", "POST"]
|
|
371
|
+
set lines to append(lines, " route \"" + m + " /w/" + w["slug"] + "/ch/*path\"\n proxy to \"" + base + "\"")
|
|
356
372
|
give join(lines, "\n")
|
|
357
373
|
|
|
358
374
|
-- reescribe hub.syn desde la plantilla si cambió (bajo --watch, el hub se reinicia solo)
|
|
@@ -407,6 +423,7 @@ export task start_ws(w)
|
|
|
407
423
|
require file(".lampson/*")
|
|
408
424
|
require file.read("web.syn")
|
|
409
425
|
require file.read("chat.syn")
|
|
426
|
+
require file.read("run.syn")
|
|
410
427
|
require file.read(".env")
|
|
411
428
|
-- encender levanta la pausa manual
|
|
412
429
|
when contains(w, "paused") and w["paused"] == true
|
|
@@ -725,6 +742,17 @@ export task should_live(w, hc)
|
|
|
725
742
|
give true
|
|
726
743
|
when schedules_on_disk(w) > 0
|
|
727
744
|
give true
|
|
745
|
+
-- con API keys (lib/apikeys.syn) el workspace atiende apps (un bot, un webhook): apagarlo por inactividad haría
|
|
746
|
+
-- que el próximo mensaje encuentre un 502 en el hub
|
|
747
|
+
when api_keys_on_disk(w) > 0
|
|
748
|
+
give true
|
|
749
|
+
-- con un canal encendido (lib/channels.syn) también: en modo poll se caería el poller, y en modo webhook la
|
|
750
|
+
-- plataforma recibiría un 502 del hub (que no enciende workspaces por un webhook)
|
|
751
|
+
when channels_on_disk(w) > 0
|
|
752
|
+
give true
|
|
753
|
+
-- con un equipo que hace rondas (lib/teams.syn: round_minutes > 0): las rondas corren en el proceso del workspace
|
|
754
|
+
when teams_on_disk(w) > 0
|
|
755
|
+
give true
|
|
728
756
|
let ih be idle_hours()
|
|
729
757
|
when ih == 0
|
|
730
758
|
give true
|
|
@@ -732,6 +760,52 @@ export task should_live(w, hc)
|
|
|
732
760
|
give now() - last < ih * 3600
|
|
733
761
|
|
|
734
762
|
-- tareas encendidas según el json del workspace (sirve aunque el proceso esté apagado)
|
|
763
|
+
-- canales encendidos en el workspace: plugins encendidos (.lampson/plugins.json del ws) cuyo plugin.json declara
|
|
764
|
+
-- "channel" — globales (plugins/ de la instalación, leídos por el link del ws) o del proyecto (.lampson/plugins/)
|
|
765
|
+
export task channels_on_disk(w)
|
|
766
|
+
let n be 0
|
|
767
|
+
try
|
|
768
|
+
let st be json_decode(read_file(dir_of(w["slug"]) + "/.lampson/plugins.json"))
|
|
769
|
+
let on be when contains(st, "enabled") then st["enabled"] otherwise {}
|
|
770
|
+
each name in keys(on)
|
|
771
|
+
when on[name] == true
|
|
772
|
+
let found be false
|
|
773
|
+
each f in [dir_of(w["slug"]) + "/plugins/" + name + "/plugin.json", dir_of(w["slug"]) + "/workspace/.lampson/plugins/" + name + "/plugin.json"]
|
|
774
|
+
when not found
|
|
775
|
+
try
|
|
776
|
+
when contains(read_file(f), "\"channel\"")
|
|
777
|
+
set found to true
|
|
778
|
+
recover err
|
|
779
|
+
set found to found
|
|
780
|
+
when found
|
|
781
|
+
set n to n + 1
|
|
782
|
+
recover err
|
|
783
|
+
give 0
|
|
784
|
+
give n
|
|
785
|
+
|
|
786
|
+
-- equipos con rondas en el proyecto (leídos por el link workspace/ del ws: el hub no tiene permiso sobre el proyecto)
|
|
787
|
+
export task teams_on_disk(w)
|
|
788
|
+
let n be 0
|
|
789
|
+
try
|
|
790
|
+
let d be dir_of(w["slug"]) + "/workspace/.lampson/teams"
|
|
791
|
+
each e in list_dir(d)
|
|
792
|
+
when ends_with(e["name"], ".team.json")
|
|
793
|
+
try
|
|
794
|
+
let t be json_decode(read_file(d + "/" + e["name"]))
|
|
795
|
+
when contains(t, "round_minutes") and t["round_minutes"] != nothing and number(t["round_minutes"]) > 0
|
|
796
|
+
set n to n + 1
|
|
797
|
+
recover err
|
|
798
|
+
set n to n
|
|
799
|
+
recover err
|
|
800
|
+
give 0
|
|
801
|
+
give n
|
|
802
|
+
|
|
803
|
+
task api_keys_on_disk(w)
|
|
804
|
+
try
|
|
805
|
+
give length(json_decode(read_file(dir_of(w["slug"]) + "/.lampson/api_keys.json"))["keys"])
|
|
806
|
+
recover err
|
|
807
|
+
give 0
|
|
808
|
+
|
|
735
809
|
task schedules_on_disk(w)
|
|
736
810
|
try
|
|
737
811
|
let doc be json_decode(read_file(dir_of(w["slug"]) + "/.lampson/schedules.json"))
|
|
@@ -750,6 +824,7 @@ export task tick()
|
|
|
750
824
|
require file(".lampson/*")
|
|
751
825
|
require file.read("web.syn")
|
|
752
826
|
require file.read("chat.syn")
|
|
827
|
+
require file.read("run.syn")
|
|
753
828
|
when migrate_ports() and hub_alive()
|
|
754
829
|
restart_hub_later()
|
|
755
830
|
keep_edge()
|
package/lib/worktree.syn
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
-- lib/worktree.syn — la copia de trabajo de cada agente de un equipo (git worktree)
|
|
2
|
+
--
|
|
3
|
+
-- En un proyecto con git, cada miembro de un equipo trabaja en SU copia: workspace/.lampson/worktrees/<agente>, en
|
|
4
|
+
-- su rama team/<equipo>/<agente> (sale de la rama actual del proyecto), con SU nombre de autor (config de git por
|
|
5
|
+
-- worktree: extensions.worktreeConfig). Así dos agentes no se pisan archivos ni ramas, y el registro de git dice
|
|
6
|
+
-- quién hizo cada commit. La copia principal (la de la persona, con main) no se toca.
|
|
7
|
+
-- * .lampson/ queda en .git/info/exclude (exclusión local: no cambia ningún archivo del repo)
|
|
8
|
+
-- * lib/loop.syn traduce: las tools de archivos y bash del agente operan en su copia; para él es la raíz
|
|
9
|
+
-- * un merge/push que sale de su copia (toca la historia compartida) pide aprobación a la persona (lib/loop.syn)
|
|
10
|
+
use "./tools/common.syn" as c
|
|
11
|
+
|
|
12
|
+
export let DIR be ".lampson/worktrees"
|
|
13
|
+
let G be "git"
|
|
14
|
+
|
|
15
|
+
task git(args, cwd)
|
|
16
|
+
require exec
|
|
17
|
+
require time
|
|
18
|
+
require file("workspace")
|
|
19
|
+
require file("workspace/*")
|
|
20
|
+
try
|
|
21
|
+
let r be run(G, args, 30, {"cwd": cwd})
|
|
22
|
+
give {"ok": r["exit_code"] == 0, "out": trim(r["stdout"] + "\n" + r["stderr"])}
|
|
23
|
+
recover err
|
|
24
|
+
give {"ok": false, "out": text(err)}
|
|
25
|
+
|
|
26
|
+
-- ¿el proyecto es un repo de git (con al menos un commit)?
|
|
27
|
+
export task is_repo()
|
|
28
|
+
require exec
|
|
29
|
+
require time
|
|
30
|
+
require file("workspace")
|
|
31
|
+
require file("workspace/*")
|
|
32
|
+
give git(["rev-parse", "--verify", "HEAD"], c.ROOT)["ok"]
|
|
33
|
+
|
|
34
|
+
-- la copia de `agent` (la crea si falta). → {ok, root, branch, error}; root es relativa al workspace
|
|
35
|
+
export task ensure(team_id, agent, author)
|
|
36
|
+
require exec
|
|
37
|
+
require time
|
|
38
|
+
require file("workspace")
|
|
39
|
+
require file("workspace/*")
|
|
40
|
+
let rel be DIR + "/" + agent
|
|
41
|
+
let branch be "team/" + team_id + "/" + agent
|
|
42
|
+
when not is_repo()
|
|
43
|
+
give {"ok": false, "root": "", "branch": "", "error": "not a git repository (or no commits yet)"}
|
|
44
|
+
when not file_exists(c.ROOT + "/" + rel + "/.git")
|
|
45
|
+
-- .lampson/ fuera de git sin tocar el .gitignore del repo
|
|
46
|
+
let ex be c.ROOT + "/.git/info/exclude"
|
|
47
|
+
let cur be ""
|
|
48
|
+
try
|
|
49
|
+
set cur to read_file(ex)
|
|
50
|
+
recover err
|
|
51
|
+
set cur to ""
|
|
52
|
+
when not contains(cur, "/.lampson/")
|
|
53
|
+
write_file(ex, cur + (when cur == "" or ends_with(cur, "\n") then "" otherwise "\n") + "/.lampson/\n")
|
|
54
|
+
let exists be git(["rev-parse", "--verify", "refs/heads/" + branch], c.ROOT)["ok"]
|
|
55
|
+
let add be git(when exists then ["worktree", "add", rel, branch] otherwise ["worktree", "add", "-b", branch, rel], c.ROOT)
|
|
56
|
+
when not add["ok"]
|
|
57
|
+
give {"ok": false, "root": "", "branch": branch, "error": "git worktree add failed: " + add["out"]}
|
|
58
|
+
-- autor del agente, solo en su copia
|
|
59
|
+
git(["config", "extensions.worktreeConfig", "true"], c.ROOT)
|
|
60
|
+
git(["config", "--worktree", "user.name", author["name"]], c.ROOT + "/" + rel)
|
|
61
|
+
git(["config", "--worktree", "user.email", author["email"]], c.ROOT + "/" + rel)
|
|
62
|
+
give {"ok": true, "root": rel, "branch": branch, "error": ""}
|
|
63
|
+
|
|
64
|
+
-- la rama principal del proyecto (la de la copia de la persona)
|
|
65
|
+
export task main_branch()
|
|
66
|
+
require exec
|
|
67
|
+
require time
|
|
68
|
+
require file("workspace")
|
|
69
|
+
require file("workspace/*")
|
|
70
|
+
let r be git(["rev-parse", "--abbrev-ref", "HEAD"], c.ROOT)
|
|
71
|
+
give when r["ok"] then r["out"] otherwise "main"
|
|
72
|
+
|
|
73
|
+
-- las tools que operan en la copia del agente, y cómo se traducen sus args
|
|
74
|
+
export let FILE_TOOLS be ["read", "write", "edit", "ls", "find", "grep"]
|
|
75
|
+
|
|
76
|
+
export task rooted(name, args, root)
|
|
77
|
+
let a be args
|
|
78
|
+
when name == "bash"
|
|
79
|
+
set a["command"] to "cd \"" + root + "\" && " + text(args["command"])
|
|
80
|
+
give a
|
|
81
|
+
let p be when contains(args, "path") and args["path"] != nothing then replace(text(args["path"]), "\\", "/") otherwise "."
|
|
82
|
+
while starts_with(p, "./")
|
|
83
|
+
set p to slice(p, 2, length(p))
|
|
84
|
+
when p == "." or p == ""
|
|
85
|
+
set a["path"] to root
|
|
86
|
+
otherwise when starts_with(p, root + "/") or p == root or starts_with(p, ".lampson/spill/")
|
|
87
|
+
set a["path"] to p
|
|
88
|
+
otherwise
|
|
89
|
+
set a["path"] to root + "/" + p
|
|
90
|
+
give a
|
|
91
|
+
|
|
92
|
+
-- ¿este comando toca la historia compartida desde una copia de agente? push siempre; merge/rebase/update-ref/branch -f
|
|
93
|
+
-- cuando sale de su copia (-C, cd o ../): adentro de su copia puede traer main a su rama sin pedir nada
|
|
94
|
+
export task touches_shared(command)
|
|
95
|
+
let cmd be " " + lower(replace(text(command), "\n", " ")) + " "
|
|
96
|
+
when not contains(cmd, "git")
|
|
97
|
+
give ""
|
|
98
|
+
when matches(cmd, ".*\\bgit\\b.*\\bpush\\b.*")
|
|
99
|
+
give "git push"
|
|
100
|
+
let leaves be contains(cmd, " -c ") or contains(cmd, "../") or contains(cmd, "..\\") or matches(cmd, ".*\\bcd\\b.*")
|
|
101
|
+
when leaves and matches(cmd, ".*\\bgit\\b.*\\b(merge|rebase|update-ref|cherry-pick|reset)\\b.*")
|
|
102
|
+
give "git merge/rebase/reset outside your working copy"
|
|
103
|
+
when matches(cmd, ".*\\bgit\\b.*\\bbranch\\b.*\\s-(f|d|D)\\b.*") or matches(cmd, ".*\\bgit\\b.*\\bupdate-ref\\b.*")
|
|
104
|
+
give "git branch -f/-d or update-ref"
|
|
105
|
+
give ""
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lampson",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "A coding agent written in Synsema: terminal + web, tools confined to the mounted project, plugins (your own tools, any language), LSP, MCP, sub-agents.",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "A coding agent written in Synsema: terminal + web, tools confined to the mounted project, plugins (your own tools, any language), LSP, MCP, sub-agents. Also an SDK: import { Lampson } from \"lampson\" (server or local mode).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -23,11 +23,15 @@
|
|
|
23
23
|
},
|
|
24
24
|
"files": [
|
|
25
25
|
"bin/",
|
|
26
|
+
"sdk/index.js",
|
|
27
|
+
"sdk/index.mjs",
|
|
28
|
+
"sdk/index.d.ts",
|
|
26
29
|
"lib/",
|
|
27
30
|
"public/",
|
|
28
31
|
"skills/",
|
|
29
32
|
"plugins/",
|
|
30
33
|
"chat.syn",
|
|
34
|
+
"run.syn",
|
|
31
35
|
"web.syn",
|
|
32
36
|
"hub.tpl.syn",
|
|
33
37
|
"edge.syn",
|
|
@@ -47,5 +51,15 @@
|
|
|
47
51
|
},
|
|
48
52
|
"scripts": {
|
|
49
53
|
"test": "pwsh -NoProfile -File tests/run.ps1"
|
|
54
|
+
},
|
|
55
|
+
"main": "sdk/index.js",
|
|
56
|
+
"types": "sdk/index.d.ts",
|
|
57
|
+
"exports": {
|
|
58
|
+
".": {
|
|
59
|
+
"types": "./sdk/index.d.ts",
|
|
60
|
+
"import": "./sdk/index.mjs",
|
|
61
|
+
"require": "./sdk/index.js"
|
|
62
|
+
},
|
|
63
|
+
"./package.json": "./package.json"
|
|
50
64
|
}
|
|
51
65
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "chatwoot",
|
|
3
|
+
"description": "Channel: answer your Chatwoot inbox (WhatsApp, Instagram, Facebook, email, web chat… everything Chatwoot connects) with a Lampson agent.",
|
|
4
|
+
"kind": "syn",
|
|
5
|
+
"entry": "plugin.syn",
|
|
6
|
+
"caps": "net",
|
|
7
|
+
"timeout": 30,
|
|
8
|
+
"config": ["CHATWOOT_URL", "CHATWOOT_TOKEN", "CHATWOOT_WEBHOOK_SECRET"],
|
|
9
|
+
"channel": {"agent": ""},
|
|
10
|
+
"tools": []
|
|
11
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
-- plugins/chatwoot/plugin.syn — canal Chatwoot (contrato en lib/channels.syn)
|
|
2
|
+
--
|
|
3
|
+
-- Chatwoot ya une WhatsApp, Instagram, Facebook, Telegram, email y el chat web en una bandeja: con este canal, un
|
|
4
|
+
-- agente de Lampson contesta todo eso. Lampson lo corre con techo: stdout, time, env=PLUGIN_*, net.
|
|
5
|
+
--
|
|
6
|
+
-- receive el webhook de Chatwoot (evento message_created). Verifica X-Chatwoot-Signature si hay secreto
|
|
7
|
+
-- (HMAC-SHA256 de "<X-Chatwoot-Timestamp>.<body crudo>", con tolerancia de 5 min contra replays), y solo
|
|
8
|
+
-- pasa los mensajes ENTRANTES y públicos: los salientes (incluidas las respuestas del propio agente, que
|
|
9
|
+
-- Chatwoot también notifica) y las notas privadas se ignoran — si no, el agente se contestaría a sí mismo.
|
|
10
|
+
-- send POST /api/v1/accounts/<account>/conversations/<conversation>/messages con api_access_token.
|
|
11
|
+
--
|
|
12
|
+
-- Config (en el .env de Lampson): LAMPSON_CHATWOOT_URL=https://app.chatwoot.com (o tu instancia),
|
|
13
|
+
-- LAMPSON_CHATWOOT_TOKEN=<access token de un agente o agent bot>, LAMPSON_CHATWOOT_WEBHOOK_SECRET=<el secreto que
|
|
14
|
+
-- Chatwoot muestra al crear el webhook>, y LAMPSON_CHANNEL_CHATWOOT_AGENT=<el agente que atiende>.
|
|
15
|
+
intent: "lampson channel plugin: Chatwoot inbox ↔ Lampson agent"
|
|
16
|
+
|
|
17
|
+
require env("PLUGIN_*")
|
|
18
|
+
require net
|
|
19
|
+
require time
|
|
20
|
+
|
|
21
|
+
let action be env("PLUGIN_TOOL", "")
|
|
22
|
+
let args be json_decode(env("PLUGIN_ARGS", "{}"))
|
|
23
|
+
|
|
24
|
+
task header(h, name)
|
|
25
|
+
each k in keys(h)
|
|
26
|
+
when lower(k) == name
|
|
27
|
+
give text(h[k])
|
|
28
|
+
give ""
|
|
29
|
+
|
|
30
|
+
task field(m, k, fallback)
|
|
31
|
+
when m != nothing and type_of(m) == "map" and contains(m, k) and m[k] != nothing
|
|
32
|
+
give m[k]
|
|
33
|
+
give fallback
|
|
34
|
+
|
|
35
|
+
task out(m)
|
|
36
|
+
print(json_encode(m))
|
|
37
|
+
|
|
38
|
+
task ignore(why)
|
|
39
|
+
out({"messages": [], "ignored": why})
|
|
40
|
+
|
|
41
|
+
task fetch_image(u)
|
|
42
|
+
try
|
|
43
|
+
let r be http_bytes("GET", u, {}, nothing, nothing, 20)
|
|
44
|
+
when not r["ok"] or length(r["bytes"]) == 0 or length(r["bytes"]) > 1500000
|
|
45
|
+
give nothing
|
|
46
|
+
let ct be ""
|
|
47
|
+
each k in keys(field(r, "headers", {}))
|
|
48
|
+
when lower(k) == "content-type"
|
|
49
|
+
set ct to lower(text(r["headers"][k]))
|
|
50
|
+
when not starts_with(ct, "image/")
|
|
51
|
+
set ct to when ends_with(lower(u), ".png") then "image/png" otherwise "image/jpeg"
|
|
52
|
+
give {"media_type": split(ct, ";")[0], "data": decode(r["bytes"], "base64")}
|
|
53
|
+
recover err
|
|
54
|
+
give nothing
|
|
55
|
+
|
|
56
|
+
task receive()
|
|
57
|
+
let body be text(field(args, "body", ""))
|
|
58
|
+
let h be field(args, "headers", {})
|
|
59
|
+
let sec be env("PLUGIN_CHATWOOT_WEBHOOK_SECRET", "")
|
|
60
|
+
when sec != ""
|
|
61
|
+
let ts be header(h, "x-chatwoot-timestamp")
|
|
62
|
+
let sig be header(h, "x-chatwoot-signature")
|
|
63
|
+
let expected be "sha256=" + decode(hmac(ts + "." + body, sec), "hex")
|
|
64
|
+
when ts == "" or sig != expected
|
|
65
|
+
out({"messages": [], "http": {"status": 401, "body": "invalid signature"}})
|
|
66
|
+
give false
|
|
67
|
+
let age be 0
|
|
68
|
+
try
|
|
69
|
+
set age to now() - number(ts)
|
|
70
|
+
recover err
|
|
71
|
+
set age to 99999
|
|
72
|
+
when age > 300 or age < -300
|
|
73
|
+
out({"messages": [], "http": {"status": 401, "body": "stale timestamp"}})
|
|
74
|
+
give false
|
|
75
|
+
let ev be nothing
|
|
76
|
+
try
|
|
77
|
+
set ev to json_decode(body)
|
|
78
|
+
recover err
|
|
79
|
+
ignore("body is not JSON")
|
|
80
|
+
give false
|
|
81
|
+
when text(field(ev, "event", "")) != "message_created"
|
|
82
|
+
ignore("event " + text(field(ev, "event", "")))
|
|
83
|
+
give false
|
|
84
|
+
-- message_type llega como texto ("incoming") o, según la versión, como número (0 = incoming)
|
|
85
|
+
let mt be field(ev, "message_type", "")
|
|
86
|
+
when not (mt == "incoming" or mt == 0)
|
|
87
|
+
ignore("not an incoming message")
|
|
88
|
+
give false
|
|
89
|
+
when field(ev, "private", false) == true
|
|
90
|
+
ignore("private note")
|
|
91
|
+
give false
|
|
92
|
+
let content be text(field(ev, "content", ""))
|
|
93
|
+
-- adjuntos: una IMAGEN (hasta 1,5 MB) viaja como imagen para el modelo; lo demás, como link en el texto
|
|
94
|
+
let images be []
|
|
95
|
+
each a in field(ev, "attachments", [])
|
|
96
|
+
let u be text(field(a, "data_url", ""))
|
|
97
|
+
let img be nothing
|
|
98
|
+
when u != "" and text(field(a, "file_type", "")) == "image"
|
|
99
|
+
set img to fetch_image(u)
|
|
100
|
+
when img != nothing
|
|
101
|
+
set images to append(images, img)
|
|
102
|
+
otherwise when u != ""
|
|
103
|
+
set content to content + "\n[attachment: " + u + "]"
|
|
104
|
+
when trim(content) == "" and length(images) > 0
|
|
105
|
+
set content to "[image]"
|
|
106
|
+
when trim(content) == ""
|
|
107
|
+
ignore("empty message")
|
|
108
|
+
give false
|
|
109
|
+
let conv be field(ev, "conversation", {})
|
|
110
|
+
let conv_id be field(conv, "display_id", field(conv, "id", nothing))
|
|
111
|
+
let account_id be field(field(ev, "account", {}), "id", nothing)
|
|
112
|
+
when conv_id == nothing or account_id == nothing
|
|
113
|
+
ignore("no account or conversation id")
|
|
114
|
+
give false
|
|
115
|
+
let who be field(ev, "sender", field(ev, "contact", {}))
|
|
116
|
+
let created be field(ev, "created_at", nothing)
|
|
117
|
+
let sent_at be when type_of(created) == "number" then created otherwise nothing
|
|
118
|
+
out({"messages": [{"thread": text(account_id) + ":" + text(conv_id), "text": content, "sent_at": sent_at, "from": {"id": text(field(who, "id", "")), "name": text(field(who, "name", "")), "kind": "human"}, "images": images, "meta": {"account_id": account_id, "conversation_id": conv_id, "message_id": field(ev, "id", nothing), "inbox_id": field(field(ev, "inbox", {}), "id", nothing)}}]})
|
|
119
|
+
give true
|
|
120
|
+
|
|
121
|
+
task send()
|
|
122
|
+
let base be env("PLUGIN_CHATWOOT_URL", "")
|
|
123
|
+
let tok be env("PLUGIN_CHATWOOT_TOKEN", "")
|
|
124
|
+
when base == "" or tok == ""
|
|
125
|
+
print("ERROR: set LAMPSON_CHATWOOT_URL and LAMPSON_CHATWOOT_TOKEN in Lampson's .env")
|
|
126
|
+
give false
|
|
127
|
+
while ends_with(base, "/")
|
|
128
|
+
set base to slice(base, 0, length(base) - 1)
|
|
129
|
+
let meta be field(args, "meta", {})
|
|
130
|
+
let url be base + "/api/v1/accounts/" + text(field(meta, "account_id", "")) + "/conversations/" + text(field(meta, "conversation_id", "")) + "/messages"
|
|
131
|
+
let r be http_post(url, json_encode({"content": text(field(args, "text", "")), "message_type": "outgoing", "private": false}), {"api_access_token": tok, "Content-Type": "application/json"}, 20)
|
|
132
|
+
when r["ok"]
|
|
133
|
+
print("sent to conversation " + text(field(meta, "conversation_id", "")))
|
|
134
|
+
give true
|
|
135
|
+
print("ERROR: Chatwoot answered " + text(r["status"]) + ": " + slice(text(field(r, "body", "")), 0, 200))
|
|
136
|
+
give false
|
|
137
|
+
|
|
138
|
+
when action == "receive"
|
|
139
|
+
receive()
|
|
140
|
+
otherwise when action == "send"
|
|
141
|
+
send()
|
|
142
|
+
otherwise
|
|
143
|
+
print("ERROR: unknown action '" + action + "'")
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
You can take orders with the `plugin_orders_*` tools. They work the same whether a customer is chatting with you or a system sends you the order.
|
|
2
|
+
|
|
3
|
+
Rules that always apply:
|
|
4
|
+
- Product, price, stock, delivery, hours and payment facts come from `catalog`. What is not there, you don't know. A product with stock 0 is not available: its notes say when it comes back.
|
|
5
|
+
- Every total comes from `quote` (promos, packs and delivery fee applied) — never add up yourself, never invent discounts or order numbers.
|
|
6
|
+
- An order needs: items with quantities, delivery or pickup, the address for delivery (street, number, apartment, a reference), the customer's name, and a payment method from the catalog.
|
|
7
|
+
- `place_order` saves the order and lowers the stock: call it once per order, only when the data is complete and confirmed. If it answers with an error (no stock, missing data), fix the cause — don't retry the same call.
|
|
8
|
+
- A placed order can be changed (`update_order`: add or remove items, delivery, address, payment, time) or cancelled (`cancel_order`) while its status is "new". Change the existing order instead of placing a second one; confirm the new total (from `quote`) before `update_order`, and the cancellation before `cancel_order`. If the store already started it, the tool says so: tell the customer to contact the store.
|
|
9
|
+
|
|
10
|
+
When a customer is chatting with you (they write from their phone, keep each message short):
|
|
11
|
+
1. Help them choose; if the store is closed now, say when it opens and that you can still take the order for later. Several messages in a row are one message: answer them together.
|
|
12
|
+
2. Ask only for what is missing, one or two things per message. With a phone number, `find_customer` may already have their address: confirm it instead of asking again.
|
|
13
|
+
3. Send a short summary (items, delivery or pickup, address, payment, total from `quote`) and ask for confirmation. Place the order ONLY after a clear yes.
|
|
14
|
+
4. Then give the order number, the total and when it arrives (the catalog's delivery time) or when to pick it up.
|
|
15
|
+
|
|
16
|
+
When a system sends you a complete order (an API call, a script, another agent — nobody to ask): check it with `quote`, place it, and answer with the order number and total; if something is missing or out of stock, answer with exactly what and don't place it.
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "orders",
|
|
3
|
+
"description": "Take orders by chat: catalog, quotes with promos and delivery, orders saved with customer and address (and changed or cancelled while still new), stock kept up to date. Data in .lampson/orders/ of the project.",
|
|
4
|
+
"kind": "syn",
|
|
5
|
+
"entry": "plugin.syn",
|
|
6
|
+
"caps": "file=workspace/.lampson/orders,file=workspace/.lampson/orders/*",
|
|
7
|
+
"timeout": 20,
|
|
8
|
+
"instructions": "INSTRUCTIONS.md",
|
|
9
|
+
"tools": [
|
|
10
|
+
{
|
|
11
|
+
"name": "catalog",
|
|
12
|
+
"description": "The store's catalog: products (sku, name, price, unit, stock, notes, promos), hours, delivery and payment methods. Read it before talking about products, prices, stock, delivery or hours.",
|
|
13
|
+
"parameters": {"type": "object", "properties": {}},
|
|
14
|
+
"readonly": true
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"name": "quote",
|
|
18
|
+
"description": "Prices a cart exactly: lines, promos and packs applied, delivery fee and total, plus any item without enough stock. Use it for every total you tell the customer; never add up yourself.",
|
|
19
|
+
"parameters": {
|
|
20
|
+
"type": "object",
|
|
21
|
+
"properties": {
|
|
22
|
+
"items": {"type": "array", "description": "the cart", "items": {"type": "object", "properties": {"sku": {"type": "string"}, "qty": {"type": "integer", "minimum": 1}}, "required": ["sku", "qty"]}},
|
|
23
|
+
"delivery": {"type": "string", "enum": ["delivery", "pickup"], "description": "delivery to an address, or pickup at the store"}
|
|
24
|
+
},
|
|
25
|
+
"required": ["items"]
|
|
26
|
+
},
|
|
27
|
+
"readonly": true
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"name": "place_order",
|
|
31
|
+
"description": "Places the order once the customer CONFIRMED the summary: saves it with an order number, saves the customer (name, phone, address) and lowers the stock. Prices are computed here again from the catalog.",
|
|
32
|
+
"parameters": {
|
|
33
|
+
"type": "object",
|
|
34
|
+
"properties": {
|
|
35
|
+
"customer": {"type": "object", "properties": {"name": {"type": "string"}, "phone": {"type": "string"}}, "required": ["name"]},
|
|
36
|
+
"items": {"type": "array", "items": {"type": "object", "properties": {"sku": {"type": "string"}, "qty": {"type": "integer", "minimum": 1}}, "required": ["sku", "qty"]}},
|
|
37
|
+
"delivery": {"type": "string", "enum": ["delivery", "pickup"]},
|
|
38
|
+
"address": {"type": "string", "description": "street, number, floor/apartment and any reference — required for delivery"},
|
|
39
|
+
"payment": {"type": "string", "description": "one of the catalog's payment methods"},
|
|
40
|
+
"when": {"type": "string", "description": "when the customer wants it, if they said (e.g. 'today 18 h'); empty = as soon as possible"},
|
|
41
|
+
"notes": {"type": "string", "description": "anything else the store must know"}
|
|
42
|
+
},
|
|
43
|
+
"required": ["customer", "items", "delivery", "payment"]
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"name": "update_order",
|
|
48
|
+
"description": "Changes an order that is still 'new' (the store hasn't started it): the cart, delivery or pickup, address, payment, time or notes. Pass `items` as the WHOLE cart after the change (omit it to keep the cart). Re-prices everything and moves the stock. Only after the customer confirmed the change.",
|
|
49
|
+
"parameters": {
|
|
50
|
+
"type": "object",
|
|
51
|
+
"properties": {
|
|
52
|
+
"order_id": {"type": "string"},
|
|
53
|
+
"items": {"type": "array", "description": "the whole cart after the change", "items": {"type": "object", "properties": {"sku": {"type": "string"}, "qty": {"type": "integer", "minimum": 1}}, "required": ["sku", "qty"]}},
|
|
54
|
+
"delivery": {"type": "string", "enum": ["delivery", "pickup"]},
|
|
55
|
+
"address": {"type": "string"},
|
|
56
|
+
"payment": {"type": "string"},
|
|
57
|
+
"when": {"type": "string"},
|
|
58
|
+
"notes": {"type": "string"}
|
|
59
|
+
},
|
|
60
|
+
"required": ["order_id"]
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
"name": "cancel_order",
|
|
65
|
+
"description": "Cancels an order that is still 'new' and puts its stock back. Only after the customer clearly asked to cancel it.",
|
|
66
|
+
"parameters": {"type": "object", "properties": {"order_id": {"type": "string"}, "reason": {"type": "string"}}, "required": ["order_id"]}
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
"name": "find_customer",
|
|
70
|
+
"description": "Looks up a returning customer by phone (or by name): saved address and last orders.",
|
|
71
|
+
"parameters": {"type": "object", "properties": {"phone": {"type": "string"}, "name": {"type": "string"}}},
|
|
72
|
+
"readonly": true
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"name": "order_status",
|
|
76
|
+
"description": "An order by its number (e.g. P-0003): items, total, delivery and status.",
|
|
77
|
+
"parameters": {"type": "object", "properties": {"order_id": {"type": "string"}}, "required": ["order_id"]},
|
|
78
|
+
"readonly": true
|
|
79
|
+
}
|
|
80
|
+
]
|
|
81
|
+
}
|