agentlaunch-templates 0.4.8 → 0.4.10
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/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +2 -0
- package/dist/registry.js.map +1 -1
- package/dist/templates/connect.d.ts +12 -0
- package/dist/templates/connect.d.ts.map +1 -0
- package/dist/templates/connect.js +177 -0
- package/dist/templates/connect.js.map +1 -0
- package/dist/templates/proxy.d.ts +12 -0
- package/dist/templates/proxy.d.ts.map +1 -0
- package/dist/templates/proxy.js +176 -0
- package/dist/templates/proxy.js.map +1 -0
- package/package.json +1 -1
package/dist/registry.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAiBH,MAAM,WAAW,gBAAgB;IAC/B,sEAAsE;IACtE,IAAI,EAAE,MAAM,CAAC;IACb,4EAA4E;IAC5E,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,mEAAmE;IACnE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,mEAAmE;IACnE,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,aAAa;IAC5B,sEAAsE;IACtE,IAAI,EAAE,MAAM,CAAC;IACb,6CAA6C;IAC7C,WAAW,EAAE,MAAM,CAAC;IACpB,8EAA8E;IAC9E,QAAQ,EAAE,MAAM,CAAC;IACjB,gEAAgE;IAChE,SAAS,EAAE,gBAAgB,EAAE,CAAC;IAC9B,8DAA8D;IAC9D,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,wEAAwE;IACxE,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,4DAA4D;IAC5D,IAAI,EAAE,MAAM,CAAC;CACd;AAuBD;;;GAGG;AACH,wBAAgB,aAAa,IAAI,aAAa,EAAE,CAE/C;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS,CAEnE;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAE7D"}
|
package/dist/registry.js
CHANGED
|
@@ -15,6 +15,7 @@ import { template as dataAnalyzerTemplate } from "./templates/data-analyzer.js";
|
|
|
15
15
|
import { template as researchTemplate } from "./templates/research.js";
|
|
16
16
|
import { template as gifterTemplate } from "./templates/gifter.js";
|
|
17
17
|
import { template as consumerCommerceTemplate } from "./templates/consumer-commerce.js";
|
|
18
|
+
import { template as connectTemplate } from "./templates/connect.js";
|
|
18
19
|
// ---------------------------------------------------------------------------
|
|
19
20
|
// Internal registry
|
|
20
21
|
// ---------------------------------------------------------------------------
|
|
@@ -28,6 +29,7 @@ const TEMPLATES = [
|
|
|
28
29
|
researchTemplate,
|
|
29
30
|
gifterTemplate,
|
|
30
31
|
consumerCommerceTemplate,
|
|
32
|
+
connectTemplate,
|
|
31
33
|
];
|
|
32
34
|
// ---------------------------------------------------------------------------
|
|
33
35
|
// Public API
|
package/dist/registry.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAC5E,OAAO,EAAE,QAAQ,IAAI,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAChF,OAAO,EAAE,QAAQ,IAAI,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,EAAE,QAAQ,IAAI,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAChF,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAC5E,OAAO,EAAE,QAAQ,IAAI,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAChF,OAAO,EAAE,QAAQ,IAAI,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AACvE,OAAO,EAAE,QAAQ,IAAI,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,EAAE,QAAQ,IAAI,wBAAwB,EAAE,MAAM,kCAAkC,CAAC;
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAC5E,OAAO,EAAE,QAAQ,IAAI,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAChF,OAAO,EAAE,QAAQ,IAAI,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,EAAE,QAAQ,IAAI,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAChF,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAC5E,OAAO,EAAE,QAAQ,IAAI,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAChF,OAAO,EAAE,QAAQ,IAAI,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AACvE,OAAO,EAAE,QAAQ,IAAI,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,EAAE,QAAQ,IAAI,wBAAwB,EAAE,MAAM,kCAAkC,CAAC;AACxF,OAAO,EAAE,QAAQ,IAAI,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAkCrE,8EAA8E;AAC9E,oBAAoB;AACpB,8EAA8E;AAE9E,MAAM,SAAS,GAAoB;IACjC,kBAAkB,EAAG,6CAA6C;IAClE,oBAAoB;IACpB,cAAc;IACd,oBAAoB;IACpB,kBAAkB;IAClB,oBAAoB;IACpB,gBAAgB;IAChB,cAAc;IACd,wBAAwB;IACxB,eAAe;CAChB,CAAC;AAEF,8EAA8E;AAC9E,aAAa;AACb,8EAA8E;AAE9E;;;GAGG;AACH,MAAM,UAAU,aAAa;IAC3B,OAAO,CAAC,GAAG,SAAS,CAAC,CAAC;AACxB,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY;IACtC,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;AAChD,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,YAAoB;IACnD,OAAO,YAAY,CAAC;AACtB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* connect.ts — Connect agent that forwards incoming chat messages to an external HTTP endpoint
|
|
3
|
+
*
|
|
4
|
+
* This template implements the connect pattern: the agent receives a message via
|
|
5
|
+
* Chat Protocol v0.3.0, POSTs it (along with the sender address) to a
|
|
6
|
+
* configurable EXTERNAL_ENDPOINT, and replies with whatever the endpoint
|
|
7
|
+
* returns. This is useful for connecting your existing agent or service to
|
|
8
|
+
* Agentverse without rebuilding it from scratch.
|
|
9
|
+
*/
|
|
10
|
+
import type { AgentTemplate } from "../registry.js";
|
|
11
|
+
export declare const template: AgentTemplate;
|
|
12
|
+
//# sourceMappingURL=connect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../../src/templates/connect.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAEpD,eAAO,MAAM,QAAQ,EAAE,aAwKtB,CAAC"}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* connect.ts — Connect agent that forwards incoming chat messages to an external HTTP endpoint
|
|
3
|
+
*
|
|
4
|
+
* This template implements the connect pattern: the agent receives a message via
|
|
5
|
+
* Chat Protocol v0.3.0, POSTs it (along with the sender address) to a
|
|
6
|
+
* configurable EXTERNAL_ENDPOINT, and replies with whatever the endpoint
|
|
7
|
+
* returns. This is useful for connecting your existing agent or service to
|
|
8
|
+
* Agentverse without rebuilding it from scratch.
|
|
9
|
+
*/
|
|
10
|
+
export const template = {
|
|
11
|
+
name: "connect",
|
|
12
|
+
description: "Connect your agent or service to Agentverse via an external HTTP endpoint",
|
|
13
|
+
category: "Infrastructure",
|
|
14
|
+
variables: [
|
|
15
|
+
{
|
|
16
|
+
name: "AGENT_NAME",
|
|
17
|
+
required: true,
|
|
18
|
+
description: "Display name of the connect agent",
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
name: "SEED_PHRASE",
|
|
22
|
+
required: true,
|
|
23
|
+
description: "Seed phrase used to derive the agent's deterministic address",
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: "EXTERNAL_ENDPOINT",
|
|
27
|
+
required: true,
|
|
28
|
+
description: "Full URL of the HTTP endpoint to forward messages to (e.g. https://api.example.com/chat)",
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
dependencies: ["requests"],
|
|
32
|
+
secrets: ["SEED_PHRASE", "EXTERNAL_ENDPOINT"],
|
|
33
|
+
code: `#!/usr/bin/env python3
|
|
34
|
+
"""
|
|
35
|
+
{{AGENT_NAME}} — AgentLaunch Connect Agent
|
|
36
|
+
Generated by: agentlaunch scaffold {{AGENT_NAME}} --type connect
|
|
37
|
+
|
|
38
|
+
Connects your existing service to Agentverse by forwarding every incoming
|
|
39
|
+
Chat Protocol message to an external HTTP endpoint and relaying the response
|
|
40
|
+
back to the sender.
|
|
41
|
+
|
|
42
|
+
Configuration (set as Agentverse secrets):
|
|
43
|
+
SEED_PHRASE — deterministic agent seed
|
|
44
|
+
EXTERNAL_ENDPOINT — target URL to forward messages to
|
|
45
|
+
|
|
46
|
+
The POST body sent to EXTERNAL_ENDPOINT:
|
|
47
|
+
{
|
|
48
|
+
"sender": "<agentverse address of the caller>",
|
|
49
|
+
"message": "<text content of the chat message>"
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
Expected response from EXTERNAL_ENDPOINT (JSON):
|
|
53
|
+
{ "reply": "<text to send back>" }
|
|
54
|
+
— or any JSON; the full response body is returned if "reply" is absent.
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
from uagents import Agent, Context, Protocol
|
|
58
|
+
from uagents_core.contrib.protocols.chat import (
|
|
59
|
+
ChatAcknowledgement,
|
|
60
|
+
ChatMessage,
|
|
61
|
+
EndSessionContent,
|
|
62
|
+
TextContent,
|
|
63
|
+
)
|
|
64
|
+
try:
|
|
65
|
+
from uagents_core.contrib.protocols.chat import create_protocol as _create_chat
|
|
66
|
+
except ImportError:
|
|
67
|
+
_create_chat = None
|
|
68
|
+
try:
|
|
69
|
+
from uagents_core.contrib.protocols.chat import chat_protocol_spec as _chat_spec
|
|
70
|
+
except ImportError:
|
|
71
|
+
_chat_spec = None
|
|
72
|
+
|
|
73
|
+
import json
|
|
74
|
+
import os
|
|
75
|
+
from datetime import datetime
|
|
76
|
+
from uuid import uuid4
|
|
77
|
+
|
|
78
|
+
import requests as http
|
|
79
|
+
|
|
80
|
+
# ==============================================================================
|
|
81
|
+
# CONFIG
|
|
82
|
+
# ==============================================================================
|
|
83
|
+
|
|
84
|
+
AGENT_NAME = "{{AGENT_NAME}}"
|
|
85
|
+
SEED_PHRASE = os.environ.get("SEED_PHRASE", "{{SEED_PHRASE}}")
|
|
86
|
+
EXTERNAL_ENDPOINT = os.environ.get("EXTERNAL_ENDPOINT", "{{EXTERNAL_ENDPOINT}}")
|
|
87
|
+
|
|
88
|
+
REQUEST_TIMEOUT = 30 # seconds
|
|
89
|
+
|
|
90
|
+
# ==============================================================================
|
|
91
|
+
# HELPERS
|
|
92
|
+
# ==============================================================================
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
async def reply(ctx: Context, sender: str, text: str, end: bool = True) -> None:
|
|
96
|
+
"""Send a Chat Protocol reply to *sender*."""
|
|
97
|
+
content = [TextContent(type="text", text=text)]
|
|
98
|
+
if end:
|
|
99
|
+
content.append(EndSessionContent(type="end-session"))
|
|
100
|
+
try:
|
|
101
|
+
await ctx.send(
|
|
102
|
+
sender,
|
|
103
|
+
ChatMessage(
|
|
104
|
+
timestamp=datetime.utcnow(), msg_id=uuid4(), content=content
|
|
105
|
+
),
|
|
106
|
+
)
|
|
107
|
+
except Exception as exc:
|
|
108
|
+
ctx.logger.error(f"[REPLY_ERROR] {exc}")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def forward(sender: str, message: str) -> str:
|
|
112
|
+
"""POST the message to EXTERNAL_ENDPOINT and return the reply text."""
|
|
113
|
+
try:
|
|
114
|
+
resp = http.post(
|
|
115
|
+
EXTERNAL_ENDPOINT,
|
|
116
|
+
json={"sender": sender, "message": message},
|
|
117
|
+
timeout=REQUEST_TIMEOUT,
|
|
118
|
+
)
|
|
119
|
+
resp.raise_for_status()
|
|
120
|
+
data = resp.json()
|
|
121
|
+
if isinstance(data, dict) and "reply" in data:
|
|
122
|
+
return str(data["reply"])
|
|
123
|
+
return json.dumps(data)
|
|
124
|
+
except http.exceptions.Timeout:
|
|
125
|
+
return "The upstream service timed out. Please try again."
|
|
126
|
+
except http.exceptions.HTTPError as exc:
|
|
127
|
+
return f"Upstream error: {exc.response.status_code}"
|
|
128
|
+
except Exception as exc:
|
|
129
|
+
return f"Error contacting upstream service: {exc}"
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
# ==============================================================================
|
|
133
|
+
# AGENT
|
|
134
|
+
# ==============================================================================
|
|
135
|
+
|
|
136
|
+
agent = Agent(name=AGENT_NAME, seed=SEED_PHRASE)
|
|
137
|
+
chat_proto = _create_chat() if _create_chat else Protocol(spec=_chat_spec)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
@chat_proto.on_message(ChatMessage)
|
|
141
|
+
async def handle_chat(ctx: Context, sender: str, msg: ChatMessage) -> None:
|
|
142
|
+
# Acknowledge immediately
|
|
143
|
+
try:
|
|
144
|
+
await ctx.send(
|
|
145
|
+
sender,
|
|
146
|
+
ChatAcknowledgement(
|
|
147
|
+
timestamp=datetime.utcnow(), acknowledged_msg_id=msg.msg_id
|
|
148
|
+
),
|
|
149
|
+
)
|
|
150
|
+
except Exception as exc:
|
|
151
|
+
ctx.logger.error(f"[ACK_ERROR] {exc}")
|
|
152
|
+
|
|
153
|
+
text = " ".join(
|
|
154
|
+
item.text for item in msg.content if isinstance(item, TextContent)
|
|
155
|
+
).strip()
|
|
156
|
+
|
|
157
|
+
if not text:
|
|
158
|
+
await reply(ctx, sender, "Empty message — nothing to forward.")
|
|
159
|
+
return
|
|
160
|
+
|
|
161
|
+
ctx.logger.info(f"[CONNECT] sender={sender[:20]} len={len(text)} endpoint={EXTERNAL_ENDPOINT}")
|
|
162
|
+
response_text = forward(sender, text)
|
|
163
|
+
await reply(ctx, sender, response_text)
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
@chat_proto.on_message(ChatAcknowledgement)
|
|
167
|
+
async def handle_ack(ctx: Context, sender: str, msg: ChatAcknowledgement) -> None:
|
|
168
|
+
ctx.logger.debug(f"[ACK] from={sender[:20]} msg_id={msg.acknowledged_msg_id}")
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
agent.include(chat_proto, publish_manifest=True)
|
|
172
|
+
|
|
173
|
+
if __name__ == "__main__":
|
|
174
|
+
agent.run()
|
|
175
|
+
`,
|
|
176
|
+
};
|
|
177
|
+
//# sourceMappingURL=connect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect.js","sourceRoot":"","sources":["../../src/templates/connect.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,MAAM,CAAC,MAAM,QAAQ,GAAkB;IACrC,IAAI,EAAE,SAAS;IACf,WAAW,EACT,2EAA2E;IAC7E,QAAQ,EAAE,gBAAgB;IAC1B,SAAS,EAAE;QACT;YACE,IAAI,EAAE,YAAY;YAClB,QAAQ,EAAE,IAAI;YACd,WAAW,EAAE,mCAAmC;SACjD;QACD;YACE,IAAI,EAAE,aAAa;YACnB,QAAQ,EAAE,IAAI;YACd,WAAW,EAAE,8DAA8D;SAC5E;QACD;YACE,IAAI,EAAE,mBAAmB;YACzB,QAAQ,EAAE,IAAI;YACd,WAAW,EACT,0FAA0F;SAC7F;KACF;IACD,YAAY,EAAE,CAAC,UAAU,CAAC;IAC1B,OAAO,EAAE,CAAC,aAAa,EAAE,mBAAmB,CAAC;IAC7C,IAAI,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8IP;CACA,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* proxy.ts — Proxy agent that forwards incoming chat messages to an external HTTP endpoint
|
|
3
|
+
*
|
|
4
|
+
* This template implements the proxy pattern: the agent receives a message via
|
|
5
|
+
* Chat Protocol v0.3.0, POSTs it (along with the sender address) to a
|
|
6
|
+
* configurable EXTERNAL_ENDPOINT, and replies with whatever the endpoint
|
|
7
|
+
* returns. This is useful for bridging Agentverse agents to existing
|
|
8
|
+
* REST services without rebuilding them as agents.
|
|
9
|
+
*/
|
|
10
|
+
import type { AgentTemplate } from "../registry.js";
|
|
11
|
+
export declare const template: AgentTemplate;
|
|
12
|
+
//# sourceMappingURL=proxy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../../src/templates/proxy.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAEpD,eAAO,MAAM,QAAQ,EAAE,aAuKtB,CAAC"}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* proxy.ts — Proxy agent that forwards incoming chat messages to an external HTTP endpoint
|
|
3
|
+
*
|
|
4
|
+
* This template implements the proxy pattern: the agent receives a message via
|
|
5
|
+
* Chat Protocol v0.3.0, POSTs it (along with the sender address) to a
|
|
6
|
+
* configurable EXTERNAL_ENDPOINT, and replies with whatever the endpoint
|
|
7
|
+
* returns. This is useful for bridging Agentverse agents to existing
|
|
8
|
+
* REST services without rebuilding them as agents.
|
|
9
|
+
*/
|
|
10
|
+
export const template = {
|
|
11
|
+
name: "proxy",
|
|
12
|
+
description: "Proxy agent that forwards messages to an external HTTP endpoint",
|
|
13
|
+
category: "Infrastructure",
|
|
14
|
+
variables: [
|
|
15
|
+
{
|
|
16
|
+
name: "AGENT_NAME",
|
|
17
|
+
required: true,
|
|
18
|
+
description: "Display name of the proxy agent",
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
name: "SEED_PHRASE",
|
|
22
|
+
required: true,
|
|
23
|
+
description: "Seed phrase used to derive the agent's deterministic address",
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: "EXTERNAL_ENDPOINT",
|
|
27
|
+
required: true,
|
|
28
|
+
description: "Full URL of the HTTP endpoint to forward messages to (e.g. https://api.example.com/chat)",
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
dependencies: ["requests"],
|
|
32
|
+
secrets: ["SEED_PHRASE", "EXTERNAL_ENDPOINT"],
|
|
33
|
+
code: `#!/usr/bin/env python3
|
|
34
|
+
"""
|
|
35
|
+
{{AGENT_NAME}} — AgentLaunch Proxy Agent
|
|
36
|
+
Generated by: agentlaunch scaffold {{AGENT_NAME}} --type proxy
|
|
37
|
+
|
|
38
|
+
Forwards every incoming Chat Protocol message to an external HTTP endpoint
|
|
39
|
+
and relays the response back to the sender.
|
|
40
|
+
|
|
41
|
+
Configuration (set as Agentverse secrets):
|
|
42
|
+
SEED_PHRASE — deterministic agent seed
|
|
43
|
+
EXTERNAL_ENDPOINT — target URL to forward messages to
|
|
44
|
+
|
|
45
|
+
The POST body sent to EXTERNAL_ENDPOINT:
|
|
46
|
+
{
|
|
47
|
+
"sender": "<agentverse address of the caller>",
|
|
48
|
+
"message": "<text content of the chat message>"
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
Expected response from EXTERNAL_ENDPOINT (JSON):
|
|
52
|
+
{ "reply": "<text to send back>" }
|
|
53
|
+
— or any JSON; the full response body is returned if "reply" is absent.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
from uagents import Agent, Context, Protocol
|
|
57
|
+
from uagents_core.contrib.protocols.chat import (
|
|
58
|
+
ChatAcknowledgement,
|
|
59
|
+
ChatMessage,
|
|
60
|
+
EndSessionContent,
|
|
61
|
+
TextContent,
|
|
62
|
+
)
|
|
63
|
+
try:
|
|
64
|
+
from uagents_core.contrib.protocols.chat import create_protocol as _create_chat
|
|
65
|
+
except ImportError:
|
|
66
|
+
_create_chat = None
|
|
67
|
+
try:
|
|
68
|
+
from uagents_core.contrib.protocols.chat import chat_protocol_spec as _chat_spec
|
|
69
|
+
except ImportError:
|
|
70
|
+
_chat_spec = None
|
|
71
|
+
|
|
72
|
+
import json
|
|
73
|
+
import os
|
|
74
|
+
from datetime import datetime
|
|
75
|
+
from uuid import uuid4
|
|
76
|
+
|
|
77
|
+
import requests as http
|
|
78
|
+
|
|
79
|
+
# ==============================================================================
|
|
80
|
+
# CONFIG
|
|
81
|
+
# ==============================================================================
|
|
82
|
+
|
|
83
|
+
AGENT_NAME = "{{AGENT_NAME}}"
|
|
84
|
+
SEED_PHRASE = os.environ.get("SEED_PHRASE", "{{SEED_PHRASE}}")
|
|
85
|
+
EXTERNAL_ENDPOINT = os.environ.get("EXTERNAL_ENDPOINT", "{{EXTERNAL_ENDPOINT}}")
|
|
86
|
+
|
|
87
|
+
REQUEST_TIMEOUT = 30 # seconds
|
|
88
|
+
|
|
89
|
+
# ==============================================================================
|
|
90
|
+
# HELPERS
|
|
91
|
+
# ==============================================================================
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
async def reply(ctx: Context, sender: str, text: str, end: bool = True) -> None:
|
|
95
|
+
"""Send a Chat Protocol reply to *sender*."""
|
|
96
|
+
content = [TextContent(type="text", text=text)]
|
|
97
|
+
if end:
|
|
98
|
+
content.append(EndSessionContent(type="end-session"))
|
|
99
|
+
try:
|
|
100
|
+
await ctx.send(
|
|
101
|
+
sender,
|
|
102
|
+
ChatMessage(
|
|
103
|
+
timestamp=datetime.utcnow(), msg_id=uuid4(), content=content
|
|
104
|
+
),
|
|
105
|
+
)
|
|
106
|
+
except Exception as exc:
|
|
107
|
+
ctx.logger.error(f"[REPLY_ERROR] {exc}")
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def forward(sender: str, message: str) -> str:
|
|
111
|
+
"""POST the message to EXTERNAL_ENDPOINT and return the reply text."""
|
|
112
|
+
try:
|
|
113
|
+
resp = http.post(
|
|
114
|
+
EXTERNAL_ENDPOINT,
|
|
115
|
+
json={"sender": sender, "message": message},
|
|
116
|
+
timeout=REQUEST_TIMEOUT,
|
|
117
|
+
)
|
|
118
|
+
resp.raise_for_status()
|
|
119
|
+
data = resp.json()
|
|
120
|
+
if isinstance(data, dict) and "reply" in data:
|
|
121
|
+
return str(data["reply"])
|
|
122
|
+
return json.dumps(data)
|
|
123
|
+
except http.exceptions.Timeout:
|
|
124
|
+
return "The upstream service timed out. Please try again."
|
|
125
|
+
except http.exceptions.HTTPError as exc:
|
|
126
|
+
return f"Upstream error: {exc.response.status_code}"
|
|
127
|
+
except Exception as exc:
|
|
128
|
+
return f"Error contacting upstream service: {exc}"
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
# ==============================================================================
|
|
132
|
+
# AGENT
|
|
133
|
+
# ==============================================================================
|
|
134
|
+
|
|
135
|
+
agent = Agent(name=AGENT_NAME, seed=SEED_PHRASE)
|
|
136
|
+
chat_proto = _create_chat() if _create_chat else Protocol(spec=_chat_spec)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
@chat_proto.on_message(ChatMessage)
|
|
140
|
+
async def handle_chat(ctx: Context, sender: str, msg: ChatMessage) -> None:
|
|
141
|
+
# Acknowledge immediately
|
|
142
|
+
try:
|
|
143
|
+
await ctx.send(
|
|
144
|
+
sender,
|
|
145
|
+
ChatAcknowledgement(
|
|
146
|
+
timestamp=datetime.utcnow(), acknowledged_msg_id=msg.msg_id
|
|
147
|
+
),
|
|
148
|
+
)
|
|
149
|
+
except Exception as exc:
|
|
150
|
+
ctx.logger.error(f"[ACK_ERROR] {exc}")
|
|
151
|
+
|
|
152
|
+
text = " ".join(
|
|
153
|
+
item.text for item in msg.content if isinstance(item, TextContent)
|
|
154
|
+
).strip()
|
|
155
|
+
|
|
156
|
+
if not text:
|
|
157
|
+
await reply(ctx, sender, "Empty message — nothing to forward.")
|
|
158
|
+
return
|
|
159
|
+
|
|
160
|
+
ctx.logger.info(f"[PROXY] sender={sender[:20]} len={len(text)} endpoint={EXTERNAL_ENDPOINT}")
|
|
161
|
+
response_text = forward(sender, text)
|
|
162
|
+
await reply(ctx, sender, response_text)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
@chat_proto.on_message(ChatAcknowledgement)
|
|
166
|
+
async def handle_ack(ctx: Context, sender: str, msg: ChatAcknowledgement) -> None:
|
|
167
|
+
ctx.logger.debug(f"[ACK] from={sender[:20]} msg_id={msg.acknowledged_msg_id}")
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
agent.include(chat_proto, publish_manifest=True)
|
|
171
|
+
|
|
172
|
+
if __name__ == "__main__":
|
|
173
|
+
agent.run()
|
|
174
|
+
`,
|
|
175
|
+
};
|
|
176
|
+
//# sourceMappingURL=proxy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../../src/templates/proxy.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,MAAM,CAAC,MAAM,QAAQ,GAAkB;IACrC,IAAI,EAAE,OAAO;IACb,WAAW,EACT,iEAAiE;IACnE,QAAQ,EAAE,gBAAgB;IAC1B,SAAS,EAAE;QACT;YACE,IAAI,EAAE,YAAY;YAClB,QAAQ,EAAE,IAAI;YACd,WAAW,EAAE,iCAAiC;SAC/C;QACD;YACE,IAAI,EAAE,aAAa;YACnB,QAAQ,EAAE,IAAI;YACd,WAAW,EAAE,8DAA8D;SAC5E;QACD;YACE,IAAI,EAAE,mBAAmB;YACzB,QAAQ,EAAE,IAAI;YACd,WAAW,EACT,0FAA0F;SAC7F;KACF;IACD,YAAY,EAAE,CAAC,UAAU,CAAC;IAC1B,OAAO,EAAE,CAAC,aAAa,EAAE,mBAAmB,CAAC;IAC7C,IAAI,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6IP;CACA,CAAC"}
|