signalwire-agents 0.1.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.
Files changed (245) hide show
  1. package/CLAUDE.md +112 -0
  2. package/README.md +417 -0
  3. package/dist/AgentBase.d.ts +481 -0
  4. package/dist/AgentBase.js +1155 -0
  5. package/dist/AgentBase.js.map +1 -0
  6. package/dist/AgentServer.d.ts +60 -0
  7. package/dist/AgentServer.js +128 -0
  8. package/dist/AgentServer.js.map +1 -0
  9. package/dist/AuthHandler.d.ts +56 -0
  10. package/dist/AuthHandler.js +119 -0
  11. package/dist/AuthHandler.js.map +1 -0
  12. package/dist/ConfigLoader.d.ts +68 -0
  13. package/dist/ConfigLoader.js +150 -0
  14. package/dist/ConfigLoader.js.map +1 -0
  15. package/dist/ContextBuilder.d.ts +431 -0
  16. package/dist/ContextBuilder.js +751 -0
  17. package/dist/ContextBuilder.js.map +1 -0
  18. package/dist/DataMap.d.ts +179 -0
  19. package/dist/DataMap.js +321 -0
  20. package/dist/DataMap.js.map +1 -0
  21. package/dist/Logger.d.ts +91 -0
  22. package/dist/Logger.js +191 -0
  23. package/dist/Logger.js.map +1 -0
  24. package/dist/PomBuilder.d.ts +144 -0
  25. package/dist/PomBuilder.js +249 -0
  26. package/dist/PomBuilder.js.map +1 -0
  27. package/dist/PromptManager.d.ts +85 -0
  28. package/dist/PromptManager.js +108 -0
  29. package/dist/PromptManager.js.map +1 -0
  30. package/dist/SchemaUtils.d.ts +42 -0
  31. package/dist/SchemaUtils.js +148 -0
  32. package/dist/SchemaUtils.js.map +1 -0
  33. package/dist/ServerlessAdapter.d.ts +100 -0
  34. package/dist/ServerlessAdapter.js +166 -0
  35. package/dist/ServerlessAdapter.js.map +1 -0
  36. package/dist/SessionManager.d.ts +86 -0
  37. package/dist/SessionManager.js +155 -0
  38. package/dist/SessionManager.js.map +1 -0
  39. package/dist/SslConfig.d.ts +77 -0
  40. package/dist/SslConfig.js +100 -0
  41. package/dist/SslConfig.js.map +1 -0
  42. package/dist/SwaigFunction.d.ts +88 -0
  43. package/dist/SwaigFunction.js +125 -0
  44. package/dist/SwaigFunction.js.map +1 -0
  45. package/dist/SwaigFunctionResult.d.ts +442 -0
  46. package/dist/SwaigFunctionResult.js +735 -0
  47. package/dist/SwaigFunctionResult.js.map +1 -0
  48. package/dist/SwmlBuilder.d.ts +37 -0
  49. package/dist/SwmlBuilder.js +55 -0
  50. package/dist/SwmlBuilder.js.map +1 -0
  51. package/dist/cli/agent-loader.d.ts +22 -0
  52. package/dist/cli/agent-loader.js +106 -0
  53. package/dist/cli/agent-loader.js.map +1 -0
  54. package/dist/cli/mock-data.d.ts +37 -0
  55. package/dist/cli/mock-data.js +79 -0
  56. package/dist/cli/mock-data.js.map +1 -0
  57. package/dist/cli/swaig-test.d.ts +32 -0
  58. package/dist/cli/swaig-test.js +405 -0
  59. package/dist/cli/swaig-test.js.map +1 -0
  60. package/dist/index.d.ts +36 -0
  61. package/dist/index.js +44 -0
  62. package/dist/index.js.map +1 -0
  63. package/dist/prefabs/ConciergeAgent.d.ts +64 -0
  64. package/dist/prefabs/ConciergeAgent.js +188 -0
  65. package/dist/prefabs/ConciergeAgent.js.map +1 -0
  66. package/dist/prefabs/FAQBotAgent.d.ts +68 -0
  67. package/dist/prefabs/FAQBotAgent.js +206 -0
  68. package/dist/prefabs/FAQBotAgent.js.map +1 -0
  69. package/dist/prefabs/InfoGathererAgent.d.ts +73 -0
  70. package/dist/prefabs/InfoGathererAgent.js +202 -0
  71. package/dist/prefabs/InfoGathererAgent.js.map +1 -0
  72. package/dist/prefabs/ReceptionistAgent.d.ts +65 -0
  73. package/dist/prefabs/ReceptionistAgent.js +205 -0
  74. package/dist/prefabs/ReceptionistAgent.js.map +1 -0
  75. package/dist/prefabs/SurveyAgent.d.ts +84 -0
  76. package/dist/prefabs/SurveyAgent.js +324 -0
  77. package/dist/prefabs/SurveyAgent.js.map +1 -0
  78. package/dist/prefabs/index.d.ts +13 -0
  79. package/dist/prefabs/index.js +14 -0
  80. package/dist/prefabs/index.js.map +1 -0
  81. package/dist/skills/SkillBase.d.ts +134 -0
  82. package/dist/skills/SkillBase.js +105 -0
  83. package/dist/skills/SkillBase.js.map +1 -0
  84. package/dist/skills/SkillManager.d.ts +83 -0
  85. package/dist/skills/SkillManager.js +159 -0
  86. package/dist/skills/SkillManager.js.map +1 -0
  87. package/dist/skills/SkillRegistry.d.ts +105 -0
  88. package/dist/skills/SkillRegistry.js +195 -0
  89. package/dist/skills/SkillRegistry.js.map +1 -0
  90. package/dist/skills/builtin/api_ninjas_trivia.d.ts +35 -0
  91. package/dist/skills/builtin/api_ninjas_trivia.js +161 -0
  92. package/dist/skills/builtin/api_ninjas_trivia.js.map +1 -0
  93. package/dist/skills/builtin/claude_skills.d.ts +34 -0
  94. package/dist/skills/builtin/claude_skills.js +156 -0
  95. package/dist/skills/builtin/claude_skills.js.map +1 -0
  96. package/dist/skills/builtin/custom_skills.d.ts +59 -0
  97. package/dist/skills/builtin/custom_skills.js +231 -0
  98. package/dist/skills/builtin/custom_skills.js.map +1 -0
  99. package/dist/skills/builtin/datasphere.d.ts +34 -0
  100. package/dist/skills/builtin/datasphere.js +163 -0
  101. package/dist/skills/builtin/datasphere.js.map +1 -0
  102. package/dist/skills/builtin/datasphere_serverless.d.ts +54 -0
  103. package/dist/skills/builtin/datasphere_serverless.js +155 -0
  104. package/dist/skills/builtin/datasphere_serverless.js.map +1 -0
  105. package/dist/skills/builtin/datetime.d.ts +31 -0
  106. package/dist/skills/builtin/datetime.js +95 -0
  107. package/dist/skills/builtin/datetime.js.map +1 -0
  108. package/dist/skills/builtin/google_maps.d.ts +33 -0
  109. package/dist/skills/builtin/google_maps.js +233 -0
  110. package/dist/skills/builtin/google_maps.js.map +1 -0
  111. package/dist/skills/builtin/index.d.ts +28 -0
  112. package/dist/skills/builtin/index.js +75 -0
  113. package/dist/skills/builtin/index.js.map +1 -0
  114. package/dist/skills/builtin/info_gatherer.d.ts +52 -0
  115. package/dist/skills/builtin/info_gatherer.js +237 -0
  116. package/dist/skills/builtin/info_gatherer.js.map +1 -0
  117. package/dist/skills/builtin/joke.d.ts +32 -0
  118. package/dist/skills/builtin/joke.js +142 -0
  119. package/dist/skills/builtin/joke.js.map +1 -0
  120. package/dist/skills/builtin/math.d.ts +33 -0
  121. package/dist/skills/builtin/math.js +120 -0
  122. package/dist/skills/builtin/math.js.map +1 -0
  123. package/dist/skills/builtin/mcp_gateway.d.ts +35 -0
  124. package/dist/skills/builtin/mcp_gateway.js +85 -0
  125. package/dist/skills/builtin/mcp_gateway.js.map +1 -0
  126. package/dist/skills/builtin/native_vector_search.d.ts +44 -0
  127. package/dist/skills/builtin/native_vector_search.js +248 -0
  128. package/dist/skills/builtin/native_vector_search.js.map +1 -0
  129. package/dist/skills/builtin/play_background_file.d.ts +34 -0
  130. package/dist/skills/builtin/play_background_file.js +135 -0
  131. package/dist/skills/builtin/play_background_file.js.map +1 -0
  132. package/dist/skills/builtin/spider.d.ts +34 -0
  133. package/dist/skills/builtin/spider.js +164 -0
  134. package/dist/skills/builtin/spider.js.map +1 -0
  135. package/dist/skills/builtin/swml_transfer.d.ts +44 -0
  136. package/dist/skills/builtin/swml_transfer.js +204 -0
  137. package/dist/skills/builtin/swml_transfer.js.map +1 -0
  138. package/dist/skills/builtin/weather_api.d.ts +34 -0
  139. package/dist/skills/builtin/weather_api.js +136 -0
  140. package/dist/skills/builtin/weather_api.js.map +1 -0
  141. package/dist/skills/builtin/web_search.d.ts +34 -0
  142. package/dist/skills/builtin/web_search.js +141 -0
  143. package/dist/skills/builtin/web_search.js.map +1 -0
  144. package/dist/skills/builtin/wikipedia_search.d.ts +34 -0
  145. package/dist/skills/builtin/wikipedia_search.js +166 -0
  146. package/dist/skills/builtin/wikipedia_search.js.map +1 -0
  147. package/dist/skills/index.d.ts +11 -0
  148. package/dist/skills/index.js +10 -0
  149. package/dist/skills/index.js.map +1 -0
  150. package/dist/types.d.ts +85 -0
  151. package/dist/types.js +5 -0
  152. package/dist/types.js.map +1 -0
  153. package/docs/agent-guide.md +869 -0
  154. package/docs/api-reference.md +2419 -0
  155. package/docs/architecture.md +843 -0
  156. package/docs/cli-guide.md +579 -0
  157. package/docs/configuration.md +422 -0
  158. package/docs/contexts-guide.md +787 -0
  159. package/docs/datamap-guide.md +1141 -0
  160. package/docs/prefabs-guide.md +830 -0
  161. package/docs/security.md +436 -0
  162. package/docs/serverless-guide.md +513 -0
  163. package/docs/skills-guide.md +845 -0
  164. package/docs/swaig-reference.md +1627 -0
  165. package/examples/call-flow.ts +60 -0
  166. package/examples/contexts-steps.ts +62 -0
  167. package/examples/datamap-tools.ts +52 -0
  168. package/examples/dynamic-config.ts +51 -0
  169. package/examples/multi-agent.ts +67 -0
  170. package/examples/pom-prompt.ts +68 -0
  171. package/examples/simple-agent.ts +33 -0
  172. package/package.json +35 -0
  173. package/src/AgentBase.ts +1279 -0
  174. package/src/AgentServer.ts +149 -0
  175. package/src/AuthHandler.ts +141 -0
  176. package/src/ConfigLoader.ts +165 -0
  177. package/src/ContextBuilder.ts +796 -0
  178. package/src/DataMap.ts +365 -0
  179. package/src/Logger.ts +197 -0
  180. package/src/PomBuilder.ts +298 -0
  181. package/src/PromptManager.ts +133 -0
  182. package/src/SchemaUtils.ts +170 -0
  183. package/src/ServerlessAdapter.ts +218 -0
  184. package/src/SessionManager.ts +161 -0
  185. package/src/SslConfig.ts +118 -0
  186. package/src/SwaigFunction.ts +168 -0
  187. package/src/SwaigFunctionResult.ts +864 -0
  188. package/src/SwmlBuilder.ts +62 -0
  189. package/src/cli/agent-loader.ts +122 -0
  190. package/src/cli/mock-data.ts +110 -0
  191. package/src/cli/swaig-test.ts +440 -0
  192. package/src/index.ts +99 -0
  193. package/src/prefabs/ConciergeAgent.ts +251 -0
  194. package/src/prefabs/FAQBotAgent.ts +263 -0
  195. package/src/prefabs/InfoGathererAgent.ts +276 -0
  196. package/src/prefabs/ReceptionistAgent.ts +277 -0
  197. package/src/prefabs/SurveyAgent.ts +413 -0
  198. package/src/prefabs/index.ts +23 -0
  199. package/src/skills/SkillBase.ts +184 -0
  200. package/src/skills/SkillManager.ts +177 -0
  201. package/src/skills/SkillRegistry.ts +231 -0
  202. package/src/skills/builtin/api_ninjas_trivia.ts +230 -0
  203. package/src/skills/builtin/claude_skills.ts +239 -0
  204. package/src/skills/builtin/custom_skills.ts +326 -0
  205. package/src/skills/builtin/datasphere.ts +225 -0
  206. package/src/skills/builtin/datasphere_serverless.ts +194 -0
  207. package/src/skills/builtin/datetime.ts +115 -0
  208. package/src/skills/builtin/google_maps.ts +347 -0
  209. package/src/skills/builtin/index.ts +79 -0
  210. package/src/skills/builtin/info_gatherer.ts +331 -0
  211. package/src/skills/builtin/joke.ts +180 -0
  212. package/src/skills/builtin/math.ts +150 -0
  213. package/src/skills/builtin/mcp_gateway.ts +99 -0
  214. package/src/skills/builtin/native_vector_search.ts +327 -0
  215. package/src/skills/builtin/play_background_file.ts +189 -0
  216. package/src/skills/builtin/spider.ts +232 -0
  217. package/src/skills/builtin/swml_transfer.ts +285 -0
  218. package/src/skills/builtin/weather_api.ts +193 -0
  219. package/src/skills/builtin/web_search.ts +212 -0
  220. package/src/skills/builtin/wikipedia_search.ts +248 -0
  221. package/src/skills/index.ts +12 -0
  222. package/src/types.ts +99 -0
  223. package/tests/AgentBase.test.ts +693 -0
  224. package/tests/AgentServer.test.ts +102 -0
  225. package/tests/AuthHandler.test.ts +121 -0
  226. package/tests/ConfigLoader.test.ts +121 -0
  227. package/tests/DataMap.test.ts +286 -0
  228. package/tests/Logger.test.ts +218 -0
  229. package/tests/PomBuilder.test.ts +164 -0
  230. package/tests/ProxyDetection.test.ts +185 -0
  231. package/tests/SchemaUtils.test.ts +106 -0
  232. package/tests/ServerlessAdapter.test.ts +200 -0
  233. package/tests/SessionManager.test.ts +139 -0
  234. package/tests/SslConfig.test.ts +97 -0
  235. package/tests/SwaigFunctionResult.test.ts +298 -0
  236. package/tests/cli/mock-data.test.ts +94 -0
  237. package/tests/cli/swaig-test.test.ts +96 -0
  238. package/tests/prefabs/prefabs.test.ts +854 -0
  239. package/tests/setup.ts +6 -0
  240. package/tests/skills/SkillBase.test.ts +118 -0
  241. package/tests/skills/SkillManager.test.ts +164 -0
  242. package/tests/skills/SkillRegistry.test.ts +124 -0
  243. package/tests/skills/builtin.test.ts +924 -0
  244. package/tsconfig.json +18 -0
  245. package/vitest.config.ts +9 -0
@@ -0,0 +1,155 @@
1
+ /**
2
+ * SessionManager - Stateless HMAC-SHA256 token generation and validation.
3
+ *
4
+ * Tokens encode call_id, function name, expiry and a nonce, signed with a
5
+ * shared secret. No server-side state is stored.
6
+ */
7
+ import { randomBytes, createHmac } from 'node:crypto';
8
+ /** Stateless HMAC-SHA256 token manager for SWAIG function call authentication and per-session metadata storage. */
9
+ export class SessionManager {
10
+ /** Token validity duration in seconds. */
11
+ tokenExpirySecs;
12
+ secretKey;
13
+ sessionMetadata = new Map();
14
+ /**
15
+ * Create a new SessionManager.
16
+ * @param tokenExpirySecs - Token validity duration in seconds (default 3600).
17
+ * @param secretKey - HMAC signing secret; a random key is generated if omitted.
18
+ */
19
+ constructor(tokenExpirySecs = 3600, secretKey) {
20
+ this.tokenExpirySecs = tokenExpirySecs;
21
+ this.secretKey = secretKey ?? randomBytes(32).toString('hex');
22
+ }
23
+ /**
24
+ * Return the given callId or generate a new random session identifier.
25
+ * @param callId - Existing call ID to reuse.
26
+ * @returns The call ID string.
27
+ */
28
+ createSession(callId) {
29
+ if (callId)
30
+ return callId;
31
+ return randomBytes(16).toString('base64url');
32
+ }
33
+ /**
34
+ * Generate a signed, base64url-encoded token binding a function name to a call ID.
35
+ * @param functionName - The SWAIG function name to bind.
36
+ * @param callId - The call ID to bind.
37
+ * @returns A base64url-encoded token string.
38
+ */
39
+ generateToken(functionName, callId) {
40
+ const expiry = Math.floor(Date.now() / 1000) + this.tokenExpirySecs;
41
+ const nonce = randomBytes(4).toString('hex');
42
+ const message = `${callId}:${functionName}:${expiry}:${nonce}`;
43
+ const signature = createHmac('sha256', this.secretKey)
44
+ .update(message)
45
+ .digest('hex')
46
+ .slice(0, 16);
47
+ const token = `${callId}.${functionName}.${expiry}.${nonce}.${signature}`;
48
+ return Buffer.from(token).toString('base64url');
49
+ }
50
+ /**
51
+ * Alias for {@link generateToken}.
52
+ * @param functionName - The SWAIG function name to bind.
53
+ * @param callId - The call ID to bind.
54
+ * @returns A base64url-encoded token string.
55
+ */
56
+ createToolToken(functionName, callId) {
57
+ return this.generateToken(functionName, callId);
58
+ }
59
+ /**
60
+ * Validate a token against the expected call ID and function name.
61
+ * @param callId - The expected call ID.
62
+ * @param functionName - The expected function name.
63
+ * @param token - The base64url-encoded token to validate.
64
+ * @returns True if the token is valid and not expired.
65
+ */
66
+ validateToken(callId, functionName, token) {
67
+ try {
68
+ const decoded = Buffer.from(token, 'base64url').toString();
69
+ const parts = decoded.split('.');
70
+ if (parts.length !== 5)
71
+ return false;
72
+ const [tokenCallId, tokenFunction, tokenExpiry, tokenNonce, tokenSignature] = parts;
73
+ const effectiveCallId = callId || tokenCallId;
74
+ if (tokenFunction !== functionName)
75
+ return false;
76
+ const expiry = parseInt(tokenExpiry, 10);
77
+ if (expiry < Date.now() / 1000)
78
+ return false;
79
+ const message = `${tokenCallId}:${tokenFunction}:${tokenExpiry}:${tokenNonce}`;
80
+ const expectedSig = createHmac('sha256', this.secretKey)
81
+ .update(message)
82
+ .digest('hex')
83
+ .slice(0, 16);
84
+ if (tokenSignature !== expectedSig)
85
+ return false;
86
+ if (tokenCallId !== effectiveCallId)
87
+ return false;
88
+ return true;
89
+ }
90
+ catch {
91
+ return false;
92
+ }
93
+ }
94
+ /**
95
+ * Alias for {@link validateToken} with reordered parameters.
96
+ * @param functionName - The expected function name.
97
+ * @param token - The base64url-encoded token to validate.
98
+ * @param callId - The expected call ID.
99
+ * @returns True if the token is valid and not expired.
100
+ */
101
+ validateToolToken(functionName, token, callId) {
102
+ return this.validateToken(callId, functionName, token);
103
+ }
104
+ /**
105
+ * Decode token components for debugging without validating the signature.
106
+ * @param token - The base64url-encoded token to decode.
107
+ * @returns The decoded token fields and expiration status, or null if malformed.
108
+ */
109
+ debugToken(token) {
110
+ try {
111
+ const decoded = Buffer.from(token, 'base64url').toString();
112
+ const parts = decoded.split('.');
113
+ if (parts.length !== 5)
114
+ return null;
115
+ const [callId, functionName, expiryStr, nonce, signature] = parts;
116
+ const expiry = parseInt(expiryStr, 10);
117
+ return {
118
+ callId,
119
+ functionName,
120
+ expiry,
121
+ nonce,
122
+ signature,
123
+ expired: expiry < Date.now() / 1000,
124
+ };
125
+ }
126
+ catch {
127
+ return null;
128
+ }
129
+ }
130
+ /**
131
+ * Retrieve metadata associated with a session.
132
+ * @param sessionId - The session identifier.
133
+ * @returns The metadata record, or undefined if no metadata exists.
134
+ */
135
+ getSessionMetadata(sessionId) {
136
+ return this.sessionMetadata.get(sessionId);
137
+ }
138
+ /**
139
+ * Merge metadata into a session, creating the entry if it does not exist.
140
+ * @param sessionId - The session identifier.
141
+ * @param metadata - Key-value pairs to merge into the session metadata.
142
+ */
143
+ setSessionMetadata(sessionId, metadata) {
144
+ this.sessionMetadata.set(sessionId, { ...this.sessionMetadata.get(sessionId), ...metadata });
145
+ }
146
+ /**
147
+ * Delete all metadata for a session.
148
+ * @param sessionId - The session identifier.
149
+ * @returns True if the session existed and was deleted.
150
+ */
151
+ deleteSessionMetadata(sessionId) {
152
+ return this.sessionMetadata.delete(sessionId);
153
+ }
154
+ }
155
+ //# sourceMappingURL=SessionManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SessionManager.js","sourceRoot":"","sources":["../src/SessionManager.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEtD,mHAAmH;AACnH,MAAM,OAAO,cAAc;IACzB,0CAA0C;IAC1C,eAAe,CAAS;IAChB,SAAS,CAAS;IAClB,eAAe,GAAyC,IAAI,G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}
@@ -0,0 +1,77 @@
1
+ /**
2
+ * SslConfig - SSL/TLS configuration for HTTPS serving.
3
+ *
4
+ * Env vars:
5
+ * SWML_SSL_ENABLED: true|false
6
+ * SWML_SSL_CERT_PATH: path to PEM cert
7
+ * SWML_SSL_KEY_PATH: path to PEM key
8
+ * SWML_SSL_DOMAIN: domain for HSTS
9
+ */
10
+ /** Configuration options for SSL/TLS setup. */
11
+ export interface SslOptions {
12
+ /** Whether SSL is enabled. */
13
+ enabled?: boolean;
14
+ /** Filesystem path to the PEM-encoded certificate. */
15
+ certPath?: string;
16
+ /** Filesystem path to the PEM-encoded private key. */
17
+ keyPath?: string;
18
+ /** Domain name used for HSTS headers. */
19
+ domain?: string;
20
+ /** Whether to send HSTS headers; defaults to true. */
21
+ hsts?: boolean;
22
+ /** HSTS max-age in seconds; defaults to 31536000 (1 year). */
23
+ hstsMaxAge?: number;
24
+ }
25
+ /** Manages SSL/TLS configuration sourced from explicit options or environment variables. */
26
+ export declare class SslConfig {
27
+ /** Whether SSL is enabled. */
28
+ enabled: boolean;
29
+ /** Filesystem path to the PEM certificate, or null if unset. */
30
+ certPath: string | null;
31
+ /** Filesystem path to the PEM private key, or null if unset. */
32
+ keyPath: string | null;
33
+ /** Domain name for HSTS, or null if unset. */
34
+ domain: string | null;
35
+ /** Whether HSTS headers should be emitted. */
36
+ hsts: boolean;
37
+ /** HSTS max-age value in seconds. */
38
+ hstsMaxAge: number;
39
+ /**
40
+ * Create an SslConfig, falling back to environment variables for any unset options.
41
+ * @param opts - Optional SSL configuration overrides.
42
+ */
43
+ constructor(opts?: SslOptions);
44
+ /**
45
+ * Check whether SSL is fully configured: enabled with both cert and key files present on disk.
46
+ * @returns True if SSL is enabled and both certificate and key files exist.
47
+ */
48
+ isConfigured(): boolean;
49
+ /**
50
+ * Read the PEM certificate file from disk.
51
+ * @returns The certificate contents, or null if the file is missing.
52
+ */
53
+ getCert(): string | null;
54
+ /**
55
+ * Read the PEM private key file from disk.
56
+ * @returns The key contents, or null if the file is missing.
57
+ */
58
+ getKey(): string | null;
59
+ /**
60
+ * Build the Strict-Transport-Security header value.
61
+ * @returns The HSTS header string, or null if HSTS is disabled or SSL is off.
62
+ */
63
+ getHstsHeader(): string | null;
64
+ /**
65
+ * Create the options object needed by Node.js `https.createServer()`.
66
+ * @returns An object with `cert` and `key` strings, or null if either file is missing.
67
+ */
68
+ getServerOptions(): {
69
+ cert: string;
70
+ key: string;
71
+ } | null;
72
+ /**
73
+ * Return a Hono middleware that appends the HSTS header to every response.
74
+ * @returns A Hono-compatible middleware function.
75
+ */
76
+ hstsMiddleware(): (c: any, next: () => Promise<void>) => Promise<void>;
77
+ }
@@ -0,0 +1,100 @@
1
+ /**
2
+ * SslConfig - SSL/TLS configuration for HTTPS serving.
3
+ *
4
+ * Env vars:
5
+ * SWML_SSL_ENABLED: true|false
6
+ * SWML_SSL_CERT_PATH: path to PEM cert
7
+ * SWML_SSL_KEY_PATH: path to PEM key
8
+ * SWML_SSL_DOMAIN: domain for HSTS
9
+ */
10
+ import { readFileSync, existsSync } from 'node:fs';
11
+ /** Manages SSL/TLS configuration sourced from explicit options or environment variables. */
12
+ export class SslConfig {
13
+ /** Whether SSL is enabled. */
14
+ enabled;
15
+ /** Filesystem path to the PEM certificate, or null if unset. */
16
+ certPath;
17
+ /** Filesystem path to the PEM private key, or null if unset. */
18
+ keyPath;
19
+ /** Domain name for HSTS, or null if unset. */
20
+ domain;
21
+ /** Whether HSTS headers should be emitted. */
22
+ hsts;
23
+ /** HSTS max-age value in seconds. */
24
+ hstsMaxAge;
25
+ /**
26
+ * Create an SslConfig, falling back to environment variables for any unset options.
27
+ * @param opts - Optional SSL configuration overrides.
28
+ */
29
+ constructor(opts) {
30
+ this.enabled = opts?.enabled ?? (process.env['SWML_SSL_ENABLED'] === 'true');
31
+ this.certPath = opts?.certPath ?? process.env['SWML_SSL_CERT_PATH'] ?? null;
32
+ this.keyPath = opts?.keyPath ?? process.env['SWML_SSL_KEY_PATH'] ?? null;
33
+ this.domain = opts?.domain ?? process.env['SWML_SSL_DOMAIN'] ?? null;
34
+ this.hsts = opts?.hsts ?? true;
35
+ this.hstsMaxAge = opts?.hstsMaxAge ?? 31536000; // 1 year
36
+ }
37
+ /**
38
+ * Check whether SSL is fully configured: enabled with both cert and key files present on disk.
39
+ * @returns True if SSL is enabled and both certificate and key files exist.
40
+ */
41
+ isConfigured() {
42
+ if (!this.enabled)
43
+ return false;
44
+ if (!this.certPath || !this.keyPath)
45
+ return false;
46
+ return existsSync(this.certPath) && existsSync(this.keyPath);
47
+ }
48
+ /**
49
+ * Read the PEM certificate file from disk.
50
+ * @returns The certificate contents, or null if the file is missing.
51
+ */
52
+ getCert() {
53
+ if (!this.certPath || !existsSync(this.certPath))
54
+ return null;
55
+ return readFileSync(this.certPath, 'utf-8');
56
+ }
57
+ /**
58
+ * Read the PEM private key file from disk.
59
+ * @returns The key contents, or null if the file is missing.
60
+ */
61
+ getKey() {
62
+ if (!this.keyPath || !existsSync(this.keyPath))
63
+ return null;
64
+ return readFileSync(this.keyPath, 'utf-8');
65
+ }
66
+ /**
67
+ * Build the Strict-Transport-Security header value.
68
+ * @returns The HSTS header string, or null if HSTS is disabled or SSL is off.
69
+ */
70
+ getHstsHeader() {
71
+ if (!this.hsts || !this.enabled)
72
+ return null;
73
+ return `max-age=${this.hstsMaxAge}; includeSubDomains`;
74
+ }
75
+ /**
76
+ * Create the options object needed by Node.js `https.createServer()`.
77
+ * @returns An object with `cert` and `key` strings, or null if either file is missing.
78
+ */
79
+ getServerOptions() {
80
+ const cert = this.getCert();
81
+ const key = this.getKey();
82
+ if (!cert || !key)
83
+ return null;
84
+ return { cert, key };
85
+ }
86
+ /**
87
+ * Return a Hono middleware that appends the HSTS header to every response.
88
+ * @returns A Hono-compatible middleware function.
89
+ */
90
+ hstsMiddleware() {
91
+ return async (c, next) => {
92
+ await next();
93
+ const hsts = this.getHstsHeader();
94
+ if (hsts) {
95
+ c.res.headers.set('Strict-Transport-Security', hsts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AAE,CAAC;gBACT,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,2BAA2B,EAAE,IAAI,CAAC,CAAC;YACvD,CAAC;QACH,CAAC,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,88 @@
1
+ /**
2
+ * SwaigFunction - Wraps a handler function with metadata for SWAIG registration.
3
+ */
4
+ import { SwaigFunctionResult } from './SwaigFunctionResult.js';
5
+ /**
6
+ * Handler function for a SWAIG tool invocation.
7
+ * @param args - Parsed arguments extracted by the AI from user speech.
8
+ * @param rawData - The full raw request payload from SignalWire.
9
+ * @returns A SwaigFunctionResult, a plain object with a response key, a string, or a Promise of any of these.
10
+ */
11
+ export type SwaigHandler = (args: Record<string, unknown>, rawData: Record<string, unknown>) => SwaigFunctionResult | Record<string, unknown> | string | Promise<SwaigFunctionResult | Record<string, unknown> | string>;
12
+ /** Configuration options for creating a SwaigFunction. */
13
+ export interface SwaigFunctionOptions {
14
+ /** Unique name used to register and invoke this tool. */
15
+ name: string;
16
+ /** The handler function called when the tool is invoked. */
17
+ handler: SwaigHandler;
18
+ /** Human-readable description of what the tool does, shown to the AI. */
19
+ description: string;
20
+ /** JSON Schema properties describing the tool's parameters. */
21
+ parameters?: Record<string, unknown>;
22
+ /** Whether this tool requires session token authentication. */
23
+ secure?: boolean;
24
+ /** Language-keyed filler phrases spoken while the tool executes. */
25
+ fillers?: Record<string, string[]>;
26
+ /** Audio file URL to play while waiting for the tool to complete. */
27
+ waitFile?: string;
28
+ /** Number of times to loop the wait file. */
29
+ waitFileLoops?: number;
30
+ /** External webhook URL; makes this an externally-hosted tool. */
31
+ webhookUrl?: string;
32
+ /** List of required parameter names. */
33
+ required?: string[];
34
+ /** Additional fields to include in the SWAIG definition output. */
35
+ extraFields?: Record<string, unknown>;
36
+ }
37
+ /**
38
+ * Wraps a tool handler function with metadata for SWAIG registration.
39
+ *
40
+ * Manages the tool's name, description, parameter schema, and handler,
41
+ * and serializes the definition into the SWAIG wire format.
42
+ */
43
+ export declare class SwaigFunction {
44
+ /** Unique name used to register and invoke this tool. */
45
+ name: string;
46
+ /** The handler function called when the tool is invoked. */
47
+ handler: SwaigHandler;
48
+ /** Human-readable description shown to the AI. */
49
+ description: string;
50
+ /** JSON Schema properties describing the tool's parameters. */
51
+ parameters: Record<string, unknown>;
52
+ /** Whether this tool requires session token authentication. */
53
+ secure: boolean;
54
+ /** Language-keyed filler phrases spoken while the tool executes. */
55
+ fillers?: Record<string, string[]>;
56
+ /** Audio file URL to play while waiting for the tool to complete. */
57
+ waitFile?: string;
58
+ /** Number of times to loop the wait file. */
59
+ waitFileLoops?: number;
60
+ /** External webhook URL; set when the tool is externally hosted. */
61
+ webhookUrl?: string;
62
+ /** List of required parameter names. */
63
+ required: string[];
64
+ /** Additional fields included in the SWAIG definition output. */
65
+ extraFields: Record<string, unknown>;
66
+ /** Whether this tool is externally hosted (has a webhookUrl). */
67
+ isExternal: boolean;
68
+ /**
69
+ * @param opts - Configuration options for the SWAIG function.
70
+ */
71
+ constructor(opts: SwaigFunctionOptions);
72
+ private ensureParameterStructure;
73
+ /**
74
+ * Invoke the handler with the given arguments and return a serialized result.
75
+ * @param args - Parsed arguments from the AI.
76
+ * @param rawData - The full raw request payload.
77
+ * @returns A serialized result dictionary suitable for the SWAIG response.
78
+ */
79
+ execute(args: Record<string, unknown>, rawData?: Record<string, unknown>): Promise<Record<string, unknown>>;
80
+ /**
81
+ * Serialize this function to the SWAIG wire format for inclusion in SWML.
82
+ * @param baseUrl - The base URL of the agent server.
83
+ * @param token - Optional session token for secure functions.
84
+ * @param callId - Optional call ID for secure function URLs.
85
+ * @returns A SWAIG function definition object.
86
+ */
87
+ toSwaig(baseUrl: string, token?: string, callId?: string): Record<string, unknown>;
88
+ }
@@ -0,0 +1,125 @@
1
+ /**
2
+ * SwaigFunction - Wraps a handler function with metadata for SWAIG registration.
3
+ */
4
+ import { SwaigFunctionResult } from './SwaigFunctionResult.js';
5
+ import { getLogger } from './Logger.js';
6
+ const log = getLogger('SwaigFunction');
7
+ /**
8
+ * Wraps a tool handler function with metadata for SWAIG registration.
9
+ *
10
+ * Manages the tool's name, description, parameter schema, and handler,
11
+ * and serializes the definition into the SWAIG wire format.
12
+ */
13
+ export class SwaigFunction {
14
+ /** Unique name used to register and invoke this tool. */
15
+ name;
16
+ /** The handler function called when the tool is invoked. */
17
+ handler;
18
+ /** Human-readable description shown to the AI. */
19
+ description;
20
+ /** JSON Schema properties describing the tool's parameters. */
21
+ parameters;
22
+ /** Whether this tool requires session token authentication. */
23
+ secure;
24
+ /** Language-keyed filler phrases spoken while the tool executes. */
25
+ fillers;
26
+ /** Audio file URL to play while waiting for the tool to complete. */
27
+ waitFile;
28
+ /** Number of times to loop the wait file. */
29
+ waitFileLoops;
30
+ /** External webhook URL; set when the tool is externally hosted. */
31
+ webhookUrl;
32
+ /** List of required parameter names. */
33
+ required;
34
+ /** Additional fields included in the SWAIG definition output. */
35
+ extraFields;
36
+ /** Whether this tool is externally hosted (has a webhookUrl). */
37
+ isExternal;
38
+ /**
39
+ * @param opts - Configuration options for the SWAIG function.
40
+ */
41
+ constructor(opts) {
42
+ this.name = opts.name;
43
+ this.handler = opts.handler;
44
+ this.description = opts.description;
45
+ this.parameters = opts.parameters ?? {};
46
+ this.secure = opts.secure ?? false;
47
+ this.fillers = opts.fillers;
48
+ this.waitFile = opts.waitFile;
49
+ this.waitFileLoops = opts.waitFileLoops;
50
+ this.webhookUrl = opts.webhookUrl;
51
+ this.required = opts.required ?? [];
52
+ this.extraFields = opts.extraFields ?? {};
53
+ this.isExternal = opts.webhookUrl !== undefined;
54
+ }
55
+ ensureParameterStructure() {
56
+ if (!this.parameters || Object.keys(this.parameters).length === 0) {
57
+ return { type: 'object', properties: {} };
58
+ }
59
+ if ('type' in this.parameters && 'properties' in this.parameters) {
60
+ return this.parameters;
61
+ }
62
+ const result = {
63
+ type: 'object',
64
+ properties: this.parameters,
65
+ };
66
+ if (this.required.length)
67
+ result['required'] = this.required;
68
+ return result;
69
+ }
70
+ /**
71
+ * Invoke the handler with the given arguments and return a serialized result.
72
+ * @param args - Parsed arguments from the AI.
73
+ * @param rawData - The full raw request payload.
74
+ * @returns A serialized result dictionary suitable for the SWAIG response.
75
+ */
76
+ async execute(args, rawData) {
77
+ try {
78
+ const result = await this.handler(args, rawData ?? {});
79
+ if (result instanceof SwaigFunctionResult) {
80
+ return result.toDict();
81
+ }
82
+ if (typeof result === 'object' && result !== null && 'response' in result) {
83
+ return result;
84
+ }
85
+ if (typeof result === 'object' && result !== null) {
86
+ return new SwaigFunctionResult('Function completed successfully').toDict();
87
+ }
88
+ return new SwaigFunctionResult(String(result)).toDict();
89
+ }
90
+ catch (err) {
91
+ log.error(`Error executing SWAIG function ${this.name}: ${err}`);
92
+ return new SwaigFunctionResult("Sorry, I couldn't complete that action. Please try again or contact support if the issue persists.").toDict();
93
+ }
94
+ }
95
+ /**
96
+ * Serialize this function to the SWAIG wire format for inclusion in SWML.
97
+ * @param baseUrl - The base URL of the agent server.
98
+ * @param token - Optional session token for secure functions.
99
+ * @param callId - Optional call ID for secure function URLs.
100
+ * @returns A SWAIG function definition object.
101
+ */
102
+ toSwaig(baseUrl, token, callId) {
103
+ let url = `${baseUrl}/swaig`;
104
+ if (token && callId) {
105
+ url = `${url}?token=${token}&call_id=${callId}`;
106
+ }
107
+ const def = {
108
+ function: this.name,
109
+ description: this.description,
110
+ parameters: this.ensureParameterStructure(),
111
+ };
112
+ if (url)
113
+ def['web_hook_url'] = url;
114
+ if (this.fillers && Object.keys(this.fillers).length > 0) {
115
+ def['fillers'] = this.fillers;
116
+ }
117
+ if (this.waitFile)
118
+ def['wait_file'] = this.waitFile;
119
+ if (this.waitFileLoops !== undefined)
120
+ def['wait_file_loops'] = this.waitFileLoops;
121
+ Object.assign(def, this.extraFields);
122
+ return def;
123
+ }
124
+ }
125
+ //# sourceMappingURL=SwaigFunction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SwaigFunction.js","sourceRoot":"","sources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