-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_agent.rb
More file actions
81 lines (70 loc) · 2.89 KB
/
Copy pathlambda_agent.rb
File metadata and controls
81 lines (70 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true
# Example: AWS Lambda-deployed agent.
#
# This file doubles as:
# * A local script you can run directly (`ruby lambda_agent.rb`) — it
# boots a WEBrick server the same way every other example does.
# * A Lambda deployment entrypoint — bundle the SDK into a Lambda zip,
# point the function handler at `lambda_agent.handler`, and AWS will
# invoke the bottom of this file.
#
# No code changes are required between the two modes: the SDK auto-detects
# Lambda via `AWS_LAMBDA_FUNCTION_NAME` / `LAMBDA_TASK_ROOT`, and
# `SignalWire::Serverless::LambdaHandler` adapts API Gateway / Function
# URL events to the agent's Rack app.
#
# Webhook URLs are derived automatically in this priority order:
# 1. `SWML_PROXY_URL_BASE` (any custom domain / proxy)
# 2. `AWS_LAMBDA_FUNCTION_URL` (Function URLs, the usual case)
# 3. `https://{AWS_LAMBDA_FUNCTION_NAME}.lambda-url.{AWS_REGION}.on.aws`
#
# In each case the agent's `route:` is appended, so deploying at a
# non-root route (e.g. `/my-agent`) just works.
require 'signalwire'
AGENT = SignalWire::AgentBase.new(
name: 'lambda-agent',
route: '/'
)
AGENT.add_language('English', 'en-US', 'elevenlabs.rachel')
AGENT.prompt_add_section(
'Role',
'You are a helpful AI assistant running in a serverless environment.'
)
AGENT.prompt_add_section('Instructions', nil, bullets: [
'Greet users warmly and offer help.',
'Use the greet_user function when asked to greet someone.',
'Use the get_time function when asked about the current time.'
])
AGENT.define_tool(
name: 'greet_user',
description: 'Greet a user by name',
parameters: {
'name' => { 'type' => 'string', 'description' => 'Name to greet' }
}
) do |args, _raw_data|
name = args['name'] || 'friend'
SignalWire::Swaig::FunctionResult.new("Hello #{name}! I'm running in serverless mode!")
end
AGENT.define_tool(
name: 'get_time',
description: 'Get the current time',
parameters: {}
) do |_args, _raw_data|
SignalWire::Swaig::FunctionResult.new("Current time: #{Time.now.iso8601}")
end
# ---------------------------------------------------------------------------
# Lambda entrypoint. AWS invokes `handler(event:, context:)` at the top level
# of this file. `LambdaHandler.for(agent)` builds a long-lived adapter that
# translates API Gateway / Function URL events to Rack and back.
# ---------------------------------------------------------------------------
HANDLER = SignalWire::Serverless::LambdaHandler.for(AGENT)
def handler(event:, context: nil)
HANDLER.call(event, context)
end
# ---------------------------------------------------------------------------
# Local development fallback — only runs when this file is the main script.
# ---------------------------------------------------------------------------
if __FILE__ == $PROGRAM_NAME
puts "Starting lambda-style agent on port #{AGENT.port}..."
AGENT.run
end