pinecall 0.11.29 → 0.11.31
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +22 -1
- data/lib/pinecall/agent/config.rb +34 -4
- data/lib/pinecall/agent/spec.rb +4 -0
- data/lib/pinecall/agent/tools.rb +8 -1
- data/lib/pinecall/agent.rb +6 -0
- data/lib/pinecall/asking.rb +141 -0
- data/lib/pinecall/blocks.rb +12 -8
- data/lib/pinecall/bridge/passing.rb +85 -0
- data/lib/pinecall/bridge/screened.rb +95 -0
- data/lib/pinecall/bridge.rb +88 -38
- data/lib/pinecall/call_world.rb +49 -0
- data/lib/pinecall/client/app_work.rb +61 -0
- data/lib/pinecall/flow_loop.rb +101 -0
- data/lib/pinecall/flows.rb +182 -0
- data/lib/pinecall/handoff.rb +77 -0
- data/lib/pinecall/screen.rb +106 -0
- data/lib/pinecall/serve/app.rb +108 -0
- data/lib/pinecall/serve/loading.rb +4 -3
- data/lib/pinecall/serve/paged.rb +110 -0
- data/lib/pinecall/serve.rb +29 -3
- data/lib/pinecall/tasks.rb +96 -0
- data/lib/pinecall/version.rb +1 -1
- data/lib/pinecall/voice_app.rb +205 -0
- data/lib/pinecall/wire/registry.rb +2 -1
- data/lib/pinecall/wire/shapes_config.rb +8 -0
- data/lib/pinecall/wire/shapes_doors.rb +7 -0
- data/lib/pinecall/wire/shapes_events.rb +7 -0
- data/lib/pinecall.rb +10 -0
- data/sig/pinecall.rbs +90 -3
- metadata +13 -1
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pinecall
|
|
4
|
+
# A team of agents one call passes between: the tools that offer the pass, and who answers first.
|
|
5
|
+
# A team is a Hash of each agent class to the slug it is served as.
|
|
6
|
+
module Handoff
|
|
7
|
+
# How many times one call is passed before the next pass is refused.
|
|
8
|
+
MOST_PASSES = 3
|
|
9
|
+
# The custom entry a pass leaves on its call.
|
|
10
|
+
PASSED = "handoff"
|
|
11
|
+
# The custom entry a call leaves when its line's `answers` chose who serves it, or could not.
|
|
12
|
+
ROUTED = "routed"
|
|
13
|
+
# How long a line's `answers` has to choose: the caller is hearing the greeting meanwhile.
|
|
14
|
+
ANSWERED_WITHIN_S = 1.0
|
|
15
|
+
TRANSFER = "transfer_to_"
|
|
16
|
+
|
|
17
|
+
# Who serves a call whose line chooses, and why it is not the one chosen, when it is not.
|
|
18
|
+
Answered = Data.define(:agent, :why)
|
|
19
|
+
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
# What `answers` chose when it is of the team and came in time, else `bound`. Never raises.
|
|
23
|
+
def answered_by(answers, line, bound, team)
|
|
24
|
+
chosen = Timeout.timeout(ANSWERED_WITHIN_S) { answers.call(line) }
|
|
25
|
+
return Answered.new(agent: bound, why: nil) if chosen.nil? || chosen == bound
|
|
26
|
+
return Answered.new(agent: bound, why: "answers chose #{chosen}, which is not of the line's team") unless team.key?(chosen)
|
|
27
|
+
|
|
28
|
+
Answered.new(agent: chosen, why: nil)
|
|
29
|
+
rescue Timeout::Error
|
|
30
|
+
Answered.new(agent: bound, why: "answers took more than #{ANSWERED_WITHIN_S} s")
|
|
31
|
+
rescue StandardError => e
|
|
32
|
+
Answered.new(agent: bound, why: "answers raised: #{e.message}")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# The tool a teammate is offered by: named by its class, which the model reads.
|
|
36
|
+
def transfer_name(mate)
|
|
37
|
+
"#{TRANSFER}#{mate.name.split("::").last.gsub(/([a-z0-9])([A-Z])/, '\1_\2').downcase}"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# The tools that offer a call of `klass` to each teammate that says what it takes.
|
|
41
|
+
def transfers_of(klass, team)
|
|
42
|
+
team.keys.filter_map do |mate|
|
|
43
|
+
takes = mate.declared_takes
|
|
44
|
+
next if mate == klass || takes.nil?
|
|
45
|
+
|
|
46
|
+
{
|
|
47
|
+
name: transfer_name(mate),
|
|
48
|
+
description: "Pass the call to #{mate.name.split("::").last}, who takes #{takes}. Call it as soon as the caller brings " \
|
|
49
|
+
"that up: do not ask them first, and do not answer it yourself. The caller is not told about the pass.",
|
|
50
|
+
parameters: { type: "object", properties: { reason: { type: "string", description: "Why, in a few words." } },
|
|
51
|
+
required: [] },
|
|
52
|
+
side_effect: "read"
|
|
53
|
+
}
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# The teammate a transfer tool names.
|
|
58
|
+
def mate_named(tool, team) = team.keys.find { |mate| transfer_name(mate) == tool.to_s }
|
|
59
|
+
|
|
60
|
+
# Every tool a call of the team may open; a name two members declare is refused.
|
|
61
|
+
def team_tools(team)
|
|
62
|
+
owners = {}
|
|
63
|
+
team.each_with_object([]) do |(member, slug), declared|
|
|
64
|
+
(member.declared_tools.values.map(&:spec) + transfers_of(member, team)).each do |spec|
|
|
65
|
+
owner = owners[spec[:name]]
|
|
66
|
+
next if owner == slug || (!owner.nil? && spec[:name].start_with?(TRANSFER))
|
|
67
|
+
unless owner.nil?
|
|
68
|
+
raise DeclarationRefused, "#{owner} and #{slug} both declare a tool named #{spec[:name]}: the agents of one team give each tool a name of its own"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
owners[spec[:name]] = slug
|
|
72
|
+
declared << spec
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pinecall
|
|
4
|
+
# A screen: what an agent puts on its caller's page, as named parts the page draws its own way.
|
|
5
|
+
module Screen
|
|
6
|
+
# The field of a call's state its screen travels in, public to the caller's own page.
|
|
7
|
+
SCREEN = "screen"
|
|
8
|
+
# The event a page answers a task with: `{ task: "phone", given: { number: "…" } }`.
|
|
9
|
+
TASK_GIVE = "task.give"
|
|
10
|
+
# The tool the model moves the caller's screen with, offered to a class that declares scenes.
|
|
11
|
+
SHOW_SCREEN = "show_screen"
|
|
12
|
+
# The scene that takes what a tool put up away: the screen with nothing chosen.
|
|
13
|
+
NO_SCENE = "none"
|
|
14
|
+
|
|
15
|
+
A_PART = /\A[a-z][a-z0-9-]*\z/
|
|
16
|
+
A_SCENE = /\A[a-z][a-z0-9_-]*\z/
|
|
17
|
+
|
|
18
|
+
# One part with what it was given and what it holds, as `screen` returns it.
|
|
19
|
+
Shown = Data.define(:part, :props, :children)
|
|
20
|
+
|
|
21
|
+
# A part of the agent's own: `Agenda = Pinecall.part("agenda")`, drawn as `Agenda.(slots:)`.
|
|
22
|
+
Part = Data.define(:name) do
|
|
23
|
+
# This part with these props, holding these children.
|
|
24
|
+
def call(*children, **props) = Shown.new(part: name, props:, children:)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
module_function
|
|
28
|
+
|
|
29
|
+
# Declare a part; the name is what the page maps to a component of its own.
|
|
30
|
+
def part(name)
|
|
31
|
+
name = name.to_s
|
|
32
|
+
unless A_PART.match?(name)
|
|
33
|
+
raise DeclarationRefused, "a part's name is lowercase letters, digits and dashes, and \"#{name}\" is not: it is what the page maps to its component"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
Part.new(name:)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# What `screen` returned, as the parts a page reads: parts, words, and arrays of them.
|
|
40
|
+
def screen_of(drawn)
|
|
41
|
+
case drawn
|
|
42
|
+
when nil, true, false then []
|
|
43
|
+
when String then drawn.strip.empty? ? [] : [drawn.strip]
|
|
44
|
+
when Numeric then [drawn.to_s]
|
|
45
|
+
when Shown then [{ part: drawn.part, props: Wire::Codec.plain(drawn.props) || {}, children: screen_of(drawn.children) }]
|
|
46
|
+
when Array then drawn.flat_map { |one| screen_of(one) }
|
|
47
|
+
else raise DeclarationRefused, "a screen is parts, words and arrays of them, and #{drawn.class} is none"
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Whether the class draws a screen of its own.
|
|
52
|
+
def draws?(klass) = klass.instance_method(:screen).owner != Agent
|
|
53
|
+
|
|
54
|
+
# The scenes a class declares, each with what it shows; checked against its tools' `shows`.
|
|
55
|
+
def scenes_of(klass)
|
|
56
|
+
scenes = klass.declared_scenes
|
|
57
|
+
klass.declared_tools.each_value do |declared|
|
|
58
|
+
shows = declared.options[:shows]
|
|
59
|
+
next if shows.nil? || scenes.key?(shows.to_s)
|
|
60
|
+
|
|
61
|
+
raise DeclarationRefused, "#{klass}.#{declared.name} shows #{shows}, which is none of its scenes"
|
|
62
|
+
end
|
|
63
|
+
scenes
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Check one declared scene's name and words, as `scenes` is written.
|
|
67
|
+
def scene!(klass, name, what)
|
|
68
|
+
name = name.to_s
|
|
69
|
+
unless A_SCENE.match?(name) && name != NO_SCENE
|
|
70
|
+
raise DeclarationRefused, "#{klass}: a scene is named in lowercase, and not \"#{NO_SCENE}\": \"#{name}\" is not one"
|
|
71
|
+
end
|
|
72
|
+
raise DeclarationRefused, "#{klass}: the scene #{name} says what it shows, in words" if what.to_s.strip.empty?
|
|
73
|
+
|
|
74
|
+
[name, what.to_s]
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# The tool the model moves the screen with: one of the scenes, or `none`.
|
|
78
|
+
def showing_tool(scenes)
|
|
79
|
+
listed = scenes.map { |name, what| "#{name}: #{what}" }.join("; ")
|
|
80
|
+
{
|
|
81
|
+
name: SHOW_SCREEN,
|
|
82
|
+
description: "Put a scene on the caller's screen, or keep the one there, in the same turn as your words. #{listed}; #{NO_SCENE}: nothing chosen.",
|
|
83
|
+
parameters: { type: "object", properties: { scene: { type: "string", enum: [*scenes.keys, NO_SCENE] } }, required: ["scene"] },
|
|
84
|
+
side_effect: "read",
|
|
85
|
+
quiet: true
|
|
86
|
+
}
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# What the model reads of the screen in this turn; empty for a class with no scenes.
|
|
90
|
+
def scene_line(scenes, scene)
|
|
91
|
+
return "" if scenes.empty?
|
|
92
|
+
return "The caller's screen shows no scene. Put one up with #{SHOW_SCREEN} when it helps them." if scene.nil?
|
|
93
|
+
|
|
94
|
+
"The caller's screen shows #{scene} (#{scenes.fetch(scene, scene)}). It goes away when you answer about something else; " \
|
|
95
|
+
"call #{SHOW_SCREEN} to keep it or to show another."
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
class << self
|
|
100
|
+
# Declare a part of the caller's screen: `Agenda = Pinecall.part("agenda")`.
|
|
101
|
+
def part(name) = Screen.part(name)
|
|
102
|
+
|
|
103
|
+
# A task of your own: `Pinecall.task("plate", asks: "…", fields: { plate: "…" }) { |given| … }`.
|
|
104
|
+
def task(name, **spec, &check) = Tasks.task(name, **spec, &check)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pinecall
|
|
4
|
+
module Serve
|
|
5
|
+
# The project's voice app in its serve entry: loaded after its agents, described, its agents held.
|
|
6
|
+
module App
|
|
7
|
+
# A reload starts this process while the one before it drains, still on the port.
|
|
8
|
+
PORT_TRIES = 90
|
|
9
|
+
PORT_TRY_S = 0.5
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
# The voice app `app.rb` makes: the last `Pinecall.voice_app(…)` its loading made.
|
|
14
|
+
def load_app(file)
|
|
15
|
+
path = File.expand_path(file)
|
|
16
|
+
raise CannotServe, "no voice app at #{path}" unless File.exist?(path)
|
|
17
|
+
|
|
18
|
+
before = VoiceApp.made.dup
|
|
19
|
+
begin
|
|
20
|
+
load path
|
|
21
|
+
rescue ScriptError, StandardError => e
|
|
22
|
+
raise if e.is_a?(DeclarationRefused)
|
|
23
|
+
|
|
24
|
+
raise CannotServe, "#{path} did not load: #{e.message}"
|
|
25
|
+
end
|
|
26
|
+
made = VoiceApp.made - before
|
|
27
|
+
raise CannotServe, "#{path} makes one voice app: APP = Pinecall.voice_app(\"…\")" unless made.length == 1
|
|
28
|
+
|
|
29
|
+
made.first
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Every agent served with its slug, and the app, loaded after them so it names their classes.
|
|
33
|
+
def loaded(flags)
|
|
34
|
+
classes = flags.served.map { |served| [served, Loading.load_served(served)] }
|
|
35
|
+
return [classes, nil] if flags.app.nil?
|
|
36
|
+
|
|
37
|
+
app = load_app(flags.app)
|
|
38
|
+
app.agents.each do |klass|
|
|
39
|
+
next if classes.any? { |_, one| one == klass }
|
|
40
|
+
|
|
41
|
+
slugs = classes.map { |served, _| served.slug }.join(", ")
|
|
42
|
+
raise CannotServe, "#{app.name} answers a line with #{klass}, and no agent served here is that class (#{slugs}): it is the Agent of its agents/<name>/agent.rb"
|
|
43
|
+
end
|
|
44
|
+
[classes, app]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# `serve app`: the app's name and page, one line of JSON, for a CLI that never loads it.
|
|
48
|
+
def described(flags, out)
|
|
49
|
+
classes, app = loaded(flags)
|
|
50
|
+
raise CannotServe, "serve app takes --app, and one --slug for each --file" if app.nil?
|
|
51
|
+
|
|
52
|
+
page = nil
|
|
53
|
+
unless app.paged.nil?
|
|
54
|
+
served, = classes.find { |_, klass| klass == app.paged.answers }
|
|
55
|
+
raise CannotServe, "#{app.name}'s page reaches #{app.paged.answers}, which no agent is" if served.nil?
|
|
56
|
+
|
|
57
|
+
page = { dir: app.paged.dir, agent: served.slug }
|
|
58
|
+
end
|
|
59
|
+
out.puts(JSON.generate({ name: app.name, page: }))
|
|
60
|
+
0
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# One agent held with what its app adds: its line's greeting, its team, the app's rules.
|
|
64
|
+
def held(client, klass, slug, app, slugs, console:, err:)
|
|
65
|
+
return Pinecall.mount(klass, client:, slug:, takes_unclaimed: !console) if app.nil?
|
|
66
|
+
|
|
67
|
+
mates = app.team_of(klass)
|
|
68
|
+
mounted = Pinecall.mount(klass, client:, slug:, takes_unclaimed: !console, declares: app.declares(klass),
|
|
69
|
+
team: mates.empty? ? nil : mates.to_h { |mate| [mate, slugs.fetch(mate)] },
|
|
70
|
+
rules: app.agents.include?(klass) ? app.rules : [], answers: app.choosing(klass))
|
|
71
|
+
pointed(mounted, app.numbers_of(klass), err)
|
|
72
|
+
mounted
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# A number is the org's row: the app says who answers it, and the gateway says whether it is so.
|
|
76
|
+
def pointed(mounted, numbers, err)
|
|
77
|
+
told = []
|
|
78
|
+
mounted.agent.on("agent.registered") do |registered, _|
|
|
79
|
+
answered = (registered[:routes] || []).map { |route| route[:number] }
|
|
80
|
+
(numbers - answered - told).each do |number|
|
|
81
|
+
told << number
|
|
82
|
+
err.puts("#{mounted.slug} #{number} is not pointed at this agent yet: `pinecall numbers` says where it rings")
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# The app's page served here, unless a runner hosts the process: the platform serves that one.
|
|
88
|
+
def page_of(app, file, client, slugs, env, err)
|
|
89
|
+
paged = app.paged
|
|
90
|
+
agent = paged && slugs[paged.answers]
|
|
91
|
+
return nil if agent.nil? || !env["PINECALL_HOST"].to_s.empty?
|
|
92
|
+
|
|
93
|
+
port = Integer(env["PINECALL_PAGE_PORT"] || Paged::PORT)
|
|
94
|
+
page = Paged.new(folder: File.join(File.dirname(File.expand_path(file)), paged.dir), agent:, url: client.url,
|
|
95
|
+
api_key: client.api_key, env: client.env, port:)
|
|
96
|
+
PORT_TRIES.times do
|
|
97
|
+
started = page.start
|
|
98
|
+
err.puts("#{app.name} its page is at http://localhost:#{port}")
|
|
99
|
+
return started
|
|
100
|
+
rescue Errno::EADDRINUSE
|
|
101
|
+
sleep(PORT_TRY_S)
|
|
102
|
+
end
|
|
103
|
+
err.puts("#{app.name} its page is not served: port #{port} is taken (PINECALL_PAGE_PORT moves it)")
|
|
104
|
+
nil
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -11,7 +11,7 @@ module Pinecall
|
|
|
11
11
|
Served = Data.define(:file, :slug)
|
|
12
12
|
|
|
13
13
|
# The flags of one verb, as the CLI passes them.
|
|
14
|
-
Flags = Data.define(:served, :console, :events, :prod, :state, :channel, :medium, :show_machine)
|
|
14
|
+
Flags = Data.define(:served, :console, :events, :prod, :state, :channel, :medium, :show_machine, :app)
|
|
15
15
|
|
|
16
16
|
module Loading
|
|
17
17
|
module_function
|
|
@@ -19,7 +19,7 @@ module Pinecall
|
|
|
19
19
|
# `--file` and `--slug` paired by place; the rest by name. A flag nobody declared is refused.
|
|
20
20
|
def parse(argv)
|
|
21
21
|
found = { files: [], slugs: [], console: false, events: false, prod: false, state: {}, channel: "phone", medium: nil,
|
|
22
|
-
show_machine: false }
|
|
22
|
+
show_machine: false, app: nil }
|
|
23
23
|
words = argv.dup
|
|
24
24
|
until words.empty?
|
|
25
25
|
word = words.shift
|
|
@@ -31,6 +31,7 @@ module Pinecall
|
|
|
31
31
|
when "--console", "--events", "--prod" then found[word.delete_prefix("--").to_sym] = true
|
|
32
32
|
when "--show-machine" then found[:show_machine] = true
|
|
33
33
|
when "--medium" then found[:medium] = value_of(word, words)
|
|
34
|
+
when "--app" then found[:app] = value_of(word, words)
|
|
34
35
|
else raise CannotServe, "serve has no flag #{word}"
|
|
35
36
|
end
|
|
36
37
|
end
|
|
@@ -95,7 +96,7 @@ module Pinecall
|
|
|
95
96
|
|
|
96
97
|
Flags.new(served: files.zip(slugs).map { |file, slug| Served.new(file:, slug:) },
|
|
97
98
|
console: found[:console], events: found[:events], prod: found[:prod], state: found[:state],
|
|
98
|
-
channel: found[:channel], medium: found[:medium], show_machine: found[:show_machine])
|
|
99
|
+
channel: found[:channel], medium: found[:medium], show_machine: found[:show_machine], app: found[:app])
|
|
99
100
|
end
|
|
100
101
|
end
|
|
101
102
|
end
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pinecall
|
|
4
|
+
module Serve
|
|
5
|
+
# The app's page on this machine: its folder, its config, and the visitor's ticket passed on.
|
|
6
|
+
class Paged
|
|
7
|
+
# The port the page is served on unless `PINECALL_PAGE_PORT` names another.
|
|
8
|
+
PORT = 4747
|
|
9
|
+
INDEX = "index.html"
|
|
10
|
+
NOT_BUILT = "the page is not built yet: build it into the folder app.page names, and reload"
|
|
11
|
+
TYPES = { ".html" => "text/html; charset=utf-8", ".js" => "text/javascript", ".mjs" => "text/javascript",
|
|
12
|
+
".css" => "text/css", ".json" => "application/json", ".svg" => "image/svg+xml", ".png" => "image/png",
|
|
13
|
+
".jpg" => "image/jpeg", ".webp" => "image/webp", ".ico" => "image/x-icon", ".woff2" => "font/woff2" }.freeze
|
|
14
|
+
|
|
15
|
+
# One answer: its status, its body and what the body is.
|
|
16
|
+
Said = Data.define(:status, :body, :type)
|
|
17
|
+
|
|
18
|
+
def initialize(folder:, agent:, url:, api_key:, env:, port:)
|
|
19
|
+
@folder = File.expand_path(folder)
|
|
20
|
+
@agent = agent
|
|
21
|
+
@url = url
|
|
22
|
+
@api_key = api_key
|
|
23
|
+
@env = env
|
|
24
|
+
@port = port
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Serve on a thread of its own until `stop`; raises when the port is taken.
|
|
28
|
+
def start
|
|
29
|
+
@server = TCPServer.new("127.0.0.1", @port)
|
|
30
|
+
@thread = Thread.new { loop { Thread.new(@server.accept) { |client| answer(client) } } }
|
|
31
|
+
self
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def stop
|
|
35
|
+
@thread&.kill
|
|
36
|
+
@server&.close
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# A GET: the page's config, or a file of the built folder.
|
|
40
|
+
def got(path)
|
|
41
|
+
# No captcha here: the key is the developer's own, and nobody else reaches the port.
|
|
42
|
+
return Said.new(200, JSON.generate({ agent: @agent, turnstile: nil }), "application/json") if path == "/pinecall/config"
|
|
43
|
+
|
|
44
|
+
file_of(path)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# A POST: a visitor's ticket or code, asked of the gateway with the key.
|
|
48
|
+
def posted(path, body)
|
|
49
|
+
asked = begin
|
|
50
|
+
JSON.parse(body.to_s.empty? ? "{}" : body, symbolize_names: true)
|
|
51
|
+
rescue JSON::ParserError
|
|
52
|
+
{}
|
|
53
|
+
end
|
|
54
|
+
case path
|
|
55
|
+
when "/pinecall/token"
|
|
56
|
+
passed("/v1/tokens", { agent: @agent, scope: asked[:scope] == "chat" ? "chat" : "talk", resume: asked[:resume] }.compact)
|
|
57
|
+
when "/pinecall/code" then passed("/v1/codes", { agent: @agent })
|
|
58
|
+
else Said.new(405, JSON.generate({ detail: "a page is read" }), "application/json")
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# A path that names no file is the page itself, so its own router reads it.
|
|
63
|
+
def file_of(path)
|
|
64
|
+
asked = File.expand_path(File.join(@folder, URI.decode_www_form_component(path)))
|
|
65
|
+
inside = asked == @folder || asked.start_with?("#{@folder}/")
|
|
66
|
+
found = inside && File.file?(asked) ? asked : File.join(@folder, INDEX)
|
|
67
|
+
if !inside || (found != asked && !File.extname(path).empty?) || !File.file?(found)
|
|
68
|
+
gone = File.file?(File.join(@folder, INDEX)) ? "the page has no file #{path}" : NOT_BUILT
|
|
69
|
+
return Said.new(404, JSON.generate({ detail: gone }), "application/json")
|
|
70
|
+
end
|
|
71
|
+
Said.new(200, File.binread(found), TYPES.fetch(File.extname(found), "application/octet-stream"))
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
def passed(door, body)
|
|
77
|
+
request = Net::HTTP::Post.new(URI.parse("#{@url.chomp("/")}#{door}"))
|
|
78
|
+
request["content-type"] = "application/json"
|
|
79
|
+
request["authorization"] = "Bearer #{@api_key}"
|
|
80
|
+
request[Client::Rest::ENV_HEADER] = @env unless @env.nil?
|
|
81
|
+
request.body = JSON.generate(body)
|
|
82
|
+
where = request.uri
|
|
83
|
+
http = Net::HTTP.new(where.host, where.port)
|
|
84
|
+
http.use_ssl = where.scheme == "https"
|
|
85
|
+
answered = http.request(request)
|
|
86
|
+
Said.new(answered.code.to_i, answered.body.to_s, "application/json")
|
|
87
|
+
rescue SystemCallError, IOError, Timeout::Error => e
|
|
88
|
+
Said.new(502, JSON.generate({ detail: e.message }), "application/json")
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def answer(client)
|
|
92
|
+
method, target = client.gets.to_s.split
|
|
93
|
+
headers = {}
|
|
94
|
+
while (line = client.gets) && line != "\r\n"
|
|
95
|
+
name, value = line.split(":", 2)
|
|
96
|
+
headers[name.downcase] = value.to_s.strip
|
|
97
|
+
end
|
|
98
|
+
path = URI.parse(target.to_s).path
|
|
99
|
+
said = method == "POST" ? posted(path, client.read(headers["content-length"].to_i)) : got(path)
|
|
100
|
+
client.write("HTTP/1.1 #{said.status} OK\r\ncontent-type: #{said.type}\r\ncache-control: no-store\r\n" \
|
|
101
|
+
"content-length: #{said.body.bytesize}\r\nconnection: close\r\n\r\n")
|
|
102
|
+
client.write(said.body)
|
|
103
|
+
rescue StandardError
|
|
104
|
+
nil
|
|
105
|
+
ensure
|
|
106
|
+
client.close
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
data/lib/pinecall/serve.rb
CHANGED
|
@@ -6,6 +6,8 @@ require_relative "serve/declared_hold"
|
|
|
6
6
|
require_relative "serve/held"
|
|
7
7
|
require_relative "serve/masking"
|
|
8
8
|
require_relative "serve/viewing"
|
|
9
|
+
require_relative "serve/paged"
|
|
10
|
+
require_relative "serve/app"
|
|
9
11
|
|
|
10
12
|
module Pinecall
|
|
11
13
|
# The entry the one `pinecall` CLI starts a Ruby agent with: `start` holds the agents and serves
|
|
@@ -16,8 +18,9 @@ module Pinecall
|
|
|
16
18
|
# SIGINT, SIGTERM or the end of its stdin, draining first; a second signal leaves now.
|
|
17
19
|
module Serve
|
|
18
20
|
USAGE = <<~USAGE
|
|
19
|
-
usage: serve start --file <agent.rb> --slug <slug> [--file … --slug …] [--console] [--events] [--prod]
|
|
21
|
+
usage: serve start --file <agent.rb> --slug <slug> [--file … --slug …] [--app <app.rb>] [--console] [--events] [--prod]
|
|
20
22
|
serve prompt --file <agent.rb> --slug <slug> [--state field=json]… [--channel name] [--show-machine]
|
|
23
|
+
serve app --app <app.rb> --file <agent.rb> --slug <slug> [--file … --slug …]
|
|
21
24
|
USAGE
|
|
22
25
|
|
|
23
26
|
NO_DOOR = "serve start reads PINECALL_URL and PINECALL_KEY from its environment, and one was not set"
|
|
@@ -32,6 +35,7 @@ module Pinecall
|
|
|
32
35
|
case verb
|
|
33
36
|
when "start" then start(Loading.parse(rest), out:, err:, env:, input:, signals:)
|
|
34
37
|
when "prompt" then prompt(Loading.parse(rest), out:)
|
|
38
|
+
when "app" then App.described(Loading.parse(rest), out)
|
|
35
39
|
else err.puts(USAGE) || 2
|
|
36
40
|
end
|
|
37
41
|
# A class refused as it loads is one sentence too, never a backtrace (R F19).
|
|
@@ -43,12 +47,16 @@ module Pinecall
|
|
|
43
47
|
# Hold every agent named until asked to leave; 0 once they drained.
|
|
44
48
|
def start(flags, out:, err:, env:, input:, signals:)
|
|
45
49
|
client = client_from(env, prod: flags.prod)
|
|
46
|
-
classes =
|
|
50
|
+
classes, app = App.loaded(flags)
|
|
51
|
+
slugs = classes.to_h { |served, klass| [klass, served.slug] }
|
|
47
52
|
mounted = classes.map do |served, klass|
|
|
48
|
-
held =
|
|
53
|
+
held = App.held(client, klass, served.slug, app, slugs, console: flags.console, err:)
|
|
49
54
|
held.agent.on_dev { |verb, data| Viewing.answer(klass, served.slug, verb, data) }
|
|
50
55
|
held
|
|
51
56
|
end
|
|
57
|
+
working = app && FlowLoop.new(app, Client::AppWork::Door.new(url: client.url, api_key: client.api_key, env: client.env, app: app.name),
|
|
58
|
+
slug_of: ->(klass) { slugs[klass] }, report: ->(line) { err.puts("#{app.name} #{line}") })
|
|
59
|
+
heard(client, working) unless working.nil?
|
|
52
60
|
said(client, out:, err:, events: flags.events, maskings: mounted.to_h { |one| [one.slug, Masking.new(one.options)] })
|
|
53
61
|
held = Held.new(client, mounted, err:)
|
|
54
62
|
asked = signals || trapped
|
|
@@ -59,8 +67,26 @@ module Pinecall
|
|
|
59
67
|
DeclaredDocs.push(client, klass, served.file, err:)
|
|
60
68
|
DeclaredHold.upload(client, klass, served.file, err:)
|
|
61
69
|
end
|
|
70
|
+
page = app && App.page_of(app, flags.app, client, slugs, env, err)
|
|
71
|
+
# The gateway is asked for work only by an app that has flows: the rest keep the socket alone.
|
|
72
|
+
working&.start if app&.flows&.any?
|
|
62
73
|
Thread.new { ended(input, asked) }
|
|
63
74
|
leave(held, asked)
|
|
75
|
+
ensure
|
|
76
|
+
working&.stop
|
|
77
|
+
page&.stop
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# What an app's agents log and what happens to their calls reaches its handlers and its flows.
|
|
81
|
+
def heard(client, working)
|
|
82
|
+
app = working.app
|
|
83
|
+
client.on_entries do |entry|
|
|
84
|
+
Thread.new do
|
|
85
|
+
app.heard(entry.type, entry.call, entry.agent, entry.data, ->(name, error) { warn("#{app.name} the handler of #{name} raised: #{error.message}") })
|
|
86
|
+
working.entry_heard(entry)
|
|
87
|
+
working.pass_heard(entry)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
64
90
|
end
|
|
65
91
|
|
|
66
92
|
# Load the class, open it in the state the pairs name, and print its prompt. No gateway, no key.
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pinecall
|
|
4
|
+
# One thing a tool needs from the caller before it can finish, asked for in the middle of the tool.
|
|
5
|
+
# `check` takes what the model gave (a Hash of strings) and answers what the tool is handed.
|
|
6
|
+
Task = Data.define(:name, :asks, :fields, :check, :how)
|
|
7
|
+
|
|
8
|
+
# Tasks: making one, the tool it is given with, and what the model is told when a tool asks.
|
|
9
|
+
module Tasks
|
|
10
|
+
A_NAME = /\A[a-z][a-z0-9_]*\z/
|
|
11
|
+
GIVE = "give_"
|
|
12
|
+
AN_EMAIL = /\A[^\s@]+@[^\s@]+\.[^\s@]{2,}\z/
|
|
13
|
+
DIGITS = (7..15)
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
# A task of your own: `Pinecall.task("plate", asks: "the plate", fields: { plate: "…" }) { |given| … }`.
|
|
18
|
+
def task(name, asks:, fields:, how: nil, &check)
|
|
19
|
+
name = name.to_s
|
|
20
|
+
unless A_NAME.match?(name)
|
|
21
|
+
raise DeclarationRefused, "a task's name is lowercase letters, digits and underscores: it names the tool the model gives it with"
|
|
22
|
+
end
|
|
23
|
+
raise DeclarationRefused, "the task #{name} says what it asks for: asks: \"the caller's email address\"" if asks.to_s.strip.empty?
|
|
24
|
+
raise DeclarationRefused, "the task #{name} names what the model gives: fields: { email: \"the address\" }" if fields.empty?
|
|
25
|
+
raise DeclarationRefused, "the task #{name} has a check: a block of what the model gave" if check.nil?
|
|
26
|
+
|
|
27
|
+
Task.new(name:, asks:, fields: fields.transform_keys(&:to_s).freeze, check:, how:)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# The name of the tool a task is given with.
|
|
31
|
+
def given_with(asked) = "#{GIVE}#{asked.name}"
|
|
32
|
+
|
|
33
|
+
# The tool a task is given with, as it is declared.
|
|
34
|
+
def giving_tool(asked)
|
|
35
|
+
{
|
|
36
|
+
name: given_with(asked),
|
|
37
|
+
description: "Give #{asked.asks}, once the caller gave it to you. Never guess it and never make one up.",
|
|
38
|
+
parameters: {
|
|
39
|
+
type: "object",
|
|
40
|
+
properties: asked.fields.to_h { |name, what| [name.to_sym, { type: "string", description: what }] },
|
|
41
|
+
required: asked.fields.keys
|
|
42
|
+
},
|
|
43
|
+
side_effect: "read"
|
|
44
|
+
}
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# What the model is told when a tool stops to ask.
|
|
48
|
+
def asking_for(asked)
|
|
49
|
+
how = asked.how.nil? ? "" : " #{asked.how}"
|
|
50
|
+
"This is not done yet: it needs #{asked.asks}. Ask the caller for it now.#{how} Then call #{given_with(asked)}."
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def email(given)
|
|
54
|
+
address = given["email"].to_s.strip.downcase
|
|
55
|
+
raise ArgumentError, "\"#{given["email"]}\" is not an email address. Ask them to say it again, slowly." unless AN_EMAIL.match?(address)
|
|
56
|
+
|
|
57
|
+
address
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def phone(given)
|
|
61
|
+
number = given["number"].to_s
|
|
62
|
+
digits = number.gsub(/\D/, "")
|
|
63
|
+
raise ArgumentError, "\"#{number}\" is not a phone number. Ask for it again, digit by digit." unless DIGITS.cover?(digits.length)
|
|
64
|
+
|
|
65
|
+
number.strip.start_with?("+") ? "+#{digits}" : digits
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def full_name(given)
|
|
69
|
+
first, last = given.values_at("first", "last").map { |part| part.to_s.strip }
|
|
70
|
+
raise ArgumentError, "Both names are needed. Ask for the one that is missing." if first.empty? || last.empty?
|
|
71
|
+
|
|
72
|
+
{ first:, last: }
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# The caller's email address, lowercased.
|
|
77
|
+
GetEmail = Tasks.task("email", asks: "the caller's email address",
|
|
78
|
+
how: "Spell it back to them letter by letter and wait for a yes.",
|
|
79
|
+
fields: { email: "The address as it is typed, like ana@example.com: no spaces, no words like 'at' or 'dot'." }) do |given|
|
|
80
|
+
Tasks.email(given)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# The caller's phone number, digits only, with its `+` when they gave a country code.
|
|
84
|
+
GetPhone = Tasks.task("phone", asks: "a phone number to reach the caller at",
|
|
85
|
+
how: "Read it back in groups of digits and wait for a yes.",
|
|
86
|
+
fields: { number: "The number in digits, with its country code when they said one: +14155550142." }) do |given|
|
|
87
|
+
Tasks.phone(given)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# The caller's first and last name.
|
|
91
|
+
GetName = Tasks.task("name", asks: "the caller's first and last name",
|
|
92
|
+
how: "Ask how the last name is spelled when it is not a common one.",
|
|
93
|
+
fields: { first: "Their first name.", last: "Their last name." }) do |given|
|
|
94
|
+
Tasks.full_name(given)
|
|
95
|
+
end
|
|
96
|
+
end
|
data/lib/pinecall/version.rb
CHANGED