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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7d670e8b7b865aefb2e7e835287b4ed462ef4d8c5a85ac0d4d675e850cd0396b
|
|
4
|
+
data.tar.gz: '0926ad65b8330fcbe70384777a40cc85197190b1758f58df2ac8133ae29071ce'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0e402ec5e711bfb60eef9a7a595b8edcb099ed7c8796db1a64d6e341dee363276c5fa3b0784ff94179c3e9e25e9cddb5d37c3a9d5fcdab4471b0520abac6570a
|
|
7
|
+
data.tar.gz: '0853a682529eb0b0405dbbd3f00bc2f7b877c3a51b123c899c6224574057f4b627b2dd0553374c408b5c8b67abd1796159b2ea25b0eac764557f732b7205fdb2'
|
data/CHANGELOG.md
CHANGED
|
@@ -4,7 +4,28 @@ Every change a person using this gem would notice. The format is
|
|
|
4
4
|
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/); version numbers and tags are the
|
|
5
5
|
maintainer's call.
|
|
6
6
|
|
|
7
|
-
## [
|
|
7
|
+
## [0.11.31] — a voice app, and @agent says what an agent is (2026-11-12)
|
|
8
|
+
|
|
9
|
+
- **A voice app, as in TypeScript and Python:** `app.rb` makes `Pinecall.voice_app(…)`: lines with a
|
|
10
|
+
greeting, a team (`takes`) the call passes between, a line that chooses who answers, rules, `silence`,
|
|
11
|
+
`on` for what the app hears, and a page of its own.
|
|
12
|
+
- **Flows:** `Pinecall.flow("…") { |input, step| … }`, its runs kept by the gateway.
|
|
13
|
+
- **Tasks:** a tool asks the caller with `ask(Pinecall::GetEmail)`; `Pinecall.task(…)` makes one.
|
|
14
|
+
- **The caller's screen:** `screen`, `Pinecall.part`, `scenes`, `tool shows:`, `call.show`.
|
|
15
|
+
- **A class declares its `timezone`.**
|
|
16
|
+
|
|
17
|
+
- **The wire reads `stage.changed`**: the agent's settings name another vendor, model or voice for a stage of a
|
|
18
|
+
live call. The model and the voice run from its next turn; the ears wait for the next call (`applied` false).
|
|
19
|
+
|
|
20
|
+
## [0.11.30] — an app is written whole in its class (2026-11-09)
|
|
21
|
+
|
|
22
|
+
- **A class declares its monitors:** `monitors`, each a number of its calls and the line it fires across.
|
|
23
|
+
|
|
24
|
+
- **A tool may be `quiet`:** a move on the caller's screen that runs as the model calls it and asks for no answer, as in TypeScript.
|
|
25
|
+
|
|
26
|
+
- **A class may declare several knowledge bases:** `bases`, in place of `docs`.
|
|
27
|
+
|
|
28
|
+
- **A tool says more:** `tool` says `delayed:` (with `delayed_after:`) and `failed:`, as a Studio tool does.
|
|
8
29
|
|
|
9
30
|
## [0.11.29] — an agent may bear silence, leave a voicemail, and name its own fallbacks (2026-11-09)
|
|
10
31
|
|
|
@@ -13,8 +13,8 @@ module Pinecall
|
|
|
13
13
|
AS_WRITTEN = %i[phone whatsapp web channel_rules].freeze
|
|
14
14
|
|
|
15
15
|
# The settings a class may declare. Must match the TypeScript package's `ENVIRONMENT`.
|
|
16
|
-
ENVIRONMENT = %i[language voice llm realtime stt judge greeting hangup turn says hears knowledge docs memory record judges
|
|
17
|
-
personas widget idle voicemail stt_fallbacks tts_fallbacks].freeze
|
|
16
|
+
ENVIRONMENT = %i[language timezone voice llm realtime stt judge greeting hangup turn says hears knowledge docs memory record judges
|
|
17
|
+
personas widget idle voicemail stt_fallbacks tts_fallbacks bases monitors].freeze
|
|
18
18
|
|
|
19
19
|
# The ones written as the wire's own fields, keyword by keyword: `docs base: "…"`, `judges on: [...]`.
|
|
20
20
|
AS_KEYWORDS = %i[turn knowledge docs memory judges personas widget idle].freeze
|
|
@@ -142,8 +142,8 @@ module Pinecall
|
|
|
142
142
|
environment[:hangup] = Config.hangup_of(ending)
|
|
143
143
|
end
|
|
144
144
|
|
|
145
|
-
# `language "es"`, `says [{ word: "GSA", spoken: "ge ese a" }]`, `record false`.
|
|
146
|
-
%i[language says record].each do |field|
|
|
145
|
+
# `language "es"`, `says [{ word: "GSA", spoken: "ge ese a" }]`, `record false`, `bases [{ base: "faq" }, { base: "prices" }]`.
|
|
146
|
+
%i[language timezone says record bases monitors].each do |field|
|
|
147
147
|
define_method(field) do |value = NOTHING|
|
|
148
148
|
return environment[field] if value.equal?(NOTHING)
|
|
149
149
|
|
|
@@ -217,6 +217,36 @@ module Pinecall
|
|
|
217
217
|
end
|
|
218
218
|
end
|
|
219
219
|
|
|
220
|
+
# What a teammate passes it, as a model reads it: `takes "a question about a bill"`.
|
|
221
|
+
def takes(what = NOTHING)
|
|
222
|
+
return declared_takes if what.equal?(NOTHING)
|
|
223
|
+
|
|
224
|
+
@takes = what.to_s.strip.empty? ? nil : what.to_s.strip
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def declared_takes = @takes || (superclass.respond_to?(:declared_takes) ? superclass.declared_takes : nil)
|
|
228
|
+
|
|
229
|
+
# The tasks its tools may ask the caller for: `tasks Pinecall::GetEmail, Pinecall::GetPhone`.
|
|
230
|
+
def tasks(*asked)
|
|
231
|
+
return declared_tasks if asked.empty?
|
|
232
|
+
|
|
233
|
+
wrong = asked.flatten.reject { |one| one.is_a?(Task) }
|
|
234
|
+
raise DeclarationRefused, "#{self}: tasks is a list of tasks, like tasks Pinecall::GetEmail, Pinecall::GetPhone" unless wrong.empty?
|
|
235
|
+
|
|
236
|
+
@tasks = asked.flatten
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def declared_tasks = @tasks || (superclass.respond_to?(:declared_tasks) ? superclass.declared_tasks : [])
|
|
240
|
+
|
|
241
|
+
# The scenes of the caller's screen, each with what it shows: `scenes agenda: "the open times"`.
|
|
242
|
+
def scenes(**declared)
|
|
243
|
+
return declared_scenes if declared.empty?
|
|
244
|
+
|
|
245
|
+
@scenes = declared.to_h { |name, what| Screen.scene!(self, name, what) }
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def declared_scenes = @scenes || (superclass.respond_to?(:declared_scenes) ? superclass.declared_scenes : {})
|
|
249
|
+
|
|
220
250
|
# What the class declares of its environment, including inherited values.
|
|
221
251
|
def environment
|
|
222
252
|
@environment ||= superclass.respond_to?(:environment) ? superclass.environment.dup : {}
|
data/lib/pinecall/agent/spec.rb
CHANGED
|
@@ -37,6 +37,10 @@ module Pinecall
|
|
|
37
37
|
}
|
|
38
38
|
spec[:confirm] = options[:confirm] if options[:confirm]
|
|
39
39
|
spec[:announce] = options[:announce] if options[:announce]
|
|
40
|
+
spec[:delayed] = options[:delayed] if options[:delayed]
|
|
41
|
+
spec[:delayed_after_s] = options[:delayed_after].to_f if options[:delayed_after]
|
|
42
|
+
spec[:failed] = options[:failed] if options[:failed]
|
|
43
|
+
spec[:quiet] = true if options[:quiet]
|
|
40
44
|
spec[:pii] = pii!(options, spec) if options[:pii]
|
|
41
45
|
spec[:timeout_s] = options[:timeout].to_f if options[:timeout]
|
|
42
46
|
Wire::Validate.call!("ToolSpec", spec, where: "tool #{name}")
|
data/lib/pinecall/agent/tools.rb
CHANGED
|
@@ -18,12 +18,19 @@ module Pinecall
|
|
|
18
18
|
# @param stage [Symbol, Array<Symbol>] shorthand for a `when` on the stage field
|
|
19
19
|
# @param confirm [String] read-back spoken before running; marks the tool irreversible
|
|
20
20
|
# @param announce [String] what the agent says as the tool starts, when its turn said nothing
|
|
21
|
+
# @param delayed [String] what the agent says when the wait has grown long
|
|
22
|
+
# @param delayed_after [Numeric] seconds before it says it, 5 unless set
|
|
23
|
+
# @param failed [String] what the agent says when the tool failed, before the model reads why
|
|
24
|
+
# @param quiet [Boolean] a move on the caller's screen: runs as the model calls it, asks for no answer
|
|
21
25
|
# @param preview [Integer] rows of a list result the model sees
|
|
22
26
|
# @param pii [Array<Symbol>] parameters with personal data, masked in the log
|
|
23
27
|
# @param timeout [Numeric] seconds the platform waits for the method
|
|
24
28
|
# @param params [Hash{Symbol=>Object}] parameter types, e.g. `{ day: String, how_many: Integer }`
|
|
29
|
+
# @param shows [Symbol] the scene of the caller's screen it puts up once it returns (`scenes`)
|
|
25
30
|
def tool(**options)
|
|
26
|
-
unknown = options.keys - %i[
|
|
31
|
+
unknown = options.keys - %i[
|
|
32
|
+
when stage confirm announce delayed delayed_after failed quiet preview pii timeout params doc shows
|
|
33
|
+
]
|
|
27
34
|
raise DeclarationRefused, "tool takes no #{unknown.join(", ")}" unless unknown.empty?
|
|
28
35
|
|
|
29
36
|
@pending_tool = options
|
data/lib/pinecall/agent.rb
CHANGED
|
@@ -79,6 +79,12 @@ module Pinecall
|
|
|
79
79
|
# Make the model speak now, guided by instructions the caller does not hear.
|
|
80
80
|
def reply(instructions, **options) = call.reply(instructions, **options)
|
|
81
81
|
|
|
82
|
+
# Ask the caller for one of the class's `tasks` from inside a tool; blocks the tool until given.
|
|
83
|
+
def ask(task) = call.ask(task)
|
|
84
|
+
|
|
85
|
+
# What the caller's page shows now: parts (`Agenda.(slots:)`), words, an array of them.
|
|
86
|
+
def screen = nil
|
|
87
|
+
|
|
82
88
|
# Append a named entry to the call's log.
|
|
83
89
|
def log(name, data = nil)
|
|
84
90
|
entry = LogEntry.new(seq: next_seq, name: name.to_s, data:, at: Time.now.to_f)
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pinecall
|
|
4
|
+
# The asking of one call: a tool that stops to ask has its call answered with the question, and
|
|
5
|
+
# goes on, on its own thread, once the tool that gives the answer is called. One question at a time.
|
|
6
|
+
class Asking
|
|
7
|
+
NOT_IN_A_TOOL = "ask is for inside a tool: the tool's call is what the question is answered on"
|
|
8
|
+
|
|
9
|
+
# What a tool stopped to ask for, for a screen that draws it as a form.
|
|
10
|
+
Asked = Data.define(:task, :asks, :fields, :refused)
|
|
11
|
+
|
|
12
|
+
# One tool running on its thread: its name, and where it says it finished, raised or asked.
|
|
13
|
+
Frame = Data.define(:tool, :said)
|
|
14
|
+
|
|
15
|
+
# The task one tool waits for, and where its answer is handed to it.
|
|
16
|
+
Waiting = Data.define(:task, :frame, :answer)
|
|
17
|
+
|
|
18
|
+
attr_reader :tasks
|
|
19
|
+
|
|
20
|
+
# The tasks a call may ask for: its class's, and with a team every member's, once each.
|
|
21
|
+
def self.tasks_on(klass, team = nil) = (team.nil? ? [klass] : team.keys).flat_map(&:declared_tasks).uniq
|
|
22
|
+
|
|
23
|
+
# The tools a set of tasks is given with; a name an agent's own tool has is refused.
|
|
24
|
+
def self.giving_tools(tasks, taken)
|
|
25
|
+
clash = tasks.find { |asked| taken.include?(Tasks.given_with(asked)) }
|
|
26
|
+
unless clash.nil?
|
|
27
|
+
raise DeclarationRefused, "the task #{clash.name} is given with #{Tasks.given_with(clash)}, and an agent declares a tool of that name"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
tasks.map { |asked| Tasks.giving_tool(asked) }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def initialize(tasks)
|
|
34
|
+
@tasks = tasks
|
|
35
|
+
@waiting = nil
|
|
36
|
+
@refused = nil
|
|
37
|
+
@lock = Mutex.new
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# The one tool open while something is asked for; nil when nothing is.
|
|
41
|
+
def open = (waiting = @waiting) && Tasks.giving_tool(waiting.task)
|
|
42
|
+
|
|
43
|
+
# What is asked for now, or nil.
|
|
44
|
+
def asking
|
|
45
|
+
waiting = @waiting
|
|
46
|
+
return nil if waiting.nil?
|
|
47
|
+
|
|
48
|
+
Asked.new(task: waiting.task.name, asks: waiting.task.asks, fields: waiting.task.fields, refused: @refused)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Whether a tool of that name gives one of the call's tasks.
|
|
52
|
+
def gives?(tool) = @tasks.any? { |asked| Tasks.given_with(asked) == tool.to_s }
|
|
53
|
+
|
|
54
|
+
# Run the tool on a thread of its own: what it returned, or the question it stopped at.
|
|
55
|
+
def run(name, &tool)
|
|
56
|
+
frame = Frame.new(tool: name.to_s, said: Thread::Queue.new)
|
|
57
|
+
Thread.new do
|
|
58
|
+
Fiber[:pinecall_frame] = frame
|
|
59
|
+
frame.said.push([:returned, tool.call])
|
|
60
|
+
rescue StandardError => e
|
|
61
|
+
frame.said.push([:raised, e])
|
|
62
|
+
end
|
|
63
|
+
outcome(frame)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Ask for a task from inside a tool: blocks the tool's thread until the answer is given.
|
|
67
|
+
def ask(asked)
|
|
68
|
+
frame = Fiber[:pinecall_frame]
|
|
69
|
+
raise Error, NOT_IN_A_TOOL if frame.nil?
|
|
70
|
+
raise Error, "#{asked.name} is not a task of this agent: name it in tasks" unless @tasks.include?(asked)
|
|
71
|
+
|
|
72
|
+
answer = Thread::Queue.new
|
|
73
|
+
@lock.synchronize do
|
|
74
|
+
raise Error, "#{@waiting.task.name} is being asked for: one question at a time" unless @waiting.nil?
|
|
75
|
+
|
|
76
|
+
@waiting = Waiting.new(task: asked, frame:, answer:)
|
|
77
|
+
end
|
|
78
|
+
frame.said.push([:asked, Tasks.asking_for(asked)])
|
|
79
|
+
kind, value = answer.pop
|
|
80
|
+
raise value if kind == :dropped
|
|
81
|
+
|
|
82
|
+
value
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# The model gave the answer: the waiting tool goes on, and this call says how it ended.
|
|
86
|
+
def given(tool, arguments)
|
|
87
|
+
waiting = @waiting
|
|
88
|
+
raise ToolFailed, "#{tool}: nothing is being asked for with it now" if waiting.nil? || Tasks.given_with(waiting.task) != tool.to_s
|
|
89
|
+
|
|
90
|
+
given = arguments.to_h { |key, value| [key.to_s, value] }
|
|
91
|
+
missing = waiting.task.fields.keys.reject { |field| given[field].is_a?(String) }
|
|
92
|
+
raise ToolFailed, "#{tool}: #{missing.join(", ")} must be given, in words" unless missing.empty?
|
|
93
|
+
|
|
94
|
+
value = checked(waiting.task, given.slice(*waiting.task.fields.keys))
|
|
95
|
+
@lock.synchronize { @waiting = nil }
|
|
96
|
+
@refused = nil
|
|
97
|
+
waiting.answer.push([:given, value])
|
|
98
|
+
ended = outcome(waiting.frame)
|
|
99
|
+
ended.is_a?(String) ? ended : { finished: waiting.frame.tool, result: ended }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# The caller's page gave the answer: what the model is to be told, or nil.
|
|
103
|
+
def given_by_page(task, values)
|
|
104
|
+
waiting = @waiting
|
|
105
|
+
return nil if waiting.nil? || waiting.task.name != task.to_s || !values.is_a?(Hash)
|
|
106
|
+
|
|
107
|
+
ended = given(Tasks.given_with(waiting.task), values)
|
|
108
|
+
what = waiting.task.asks
|
|
109
|
+
return "The caller gave #{what} on their screen. #{ended}" if ended.is_a?(String)
|
|
110
|
+
|
|
111
|
+
"The caller gave #{what} on their screen, and #{waiting.frame.tool} is done with this result: #{JSON.generate(ended[:result])}. Tell them how it went."
|
|
112
|
+
rescue ToolFailed => e
|
|
113
|
+
@refused = e.message
|
|
114
|
+
nil
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# The call ended with a question open: the tool that asked is told.
|
|
118
|
+
def abandon
|
|
119
|
+
waiting = @lock.synchronize { @waiting.tap { @waiting = nil } }
|
|
120
|
+
return if waiting.nil?
|
|
121
|
+
|
|
122
|
+
waiting.answer.push([:dropped, Error.new("the call ended before the caller gave #{waiting.task.asks}")])
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
private
|
|
126
|
+
|
|
127
|
+
# What the tool's thread said next: its result, its question, or what it raised.
|
|
128
|
+
def outcome(frame)
|
|
129
|
+
kind, value = frame.said.pop
|
|
130
|
+
raise value if kind == :raised
|
|
131
|
+
|
|
132
|
+
value
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def checked(task, given)
|
|
136
|
+
task.check.call(given)
|
|
137
|
+
rescue StandardError => e
|
|
138
|
+
raise ToolFailed, e.message
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
data/lib/pinecall/blocks.rb
CHANGED
|
@@ -74,17 +74,18 @@ module Pinecall
|
|
|
74
74
|
|
|
75
75
|
module_function
|
|
76
76
|
|
|
77
|
-
def render(agent, resumed: false, remembered: [], line: nil)
|
|
77
|
+
def render(agent, resumed: false, remembered: [], line: nil, rules: [])
|
|
78
78
|
reading = reading_for(agent, resumed:, remembered:, line:)
|
|
79
79
|
blocks = FRAMEWORK.map do |spec|
|
|
80
|
-
Block.new(name: spec[:name], region: spec[:region], text: text_of(agent, spec[:name], reading))
|
|
80
|
+
Block.new(name: spec[:name], region: spec[:region], text: text_of(agent, spec[:name], reading, rules))
|
|
81
81
|
end
|
|
82
82
|
Blocks.new(blocks:, history: history(agent))
|
|
83
83
|
end
|
|
84
84
|
|
|
85
|
-
# The class docstring, the framework's rules
|
|
86
|
-
def identity(agent)
|
|
87
|
-
|
|
85
|
+
# The class docstring, the framework's and the app's rules, the protocols, the channel's. Reads no state.
|
|
86
|
+
def identity(agent, rules = [])
|
|
87
|
+
app_rules = rules.map { |rule| "- #{rule}" }.join("\n")
|
|
88
|
+
[agent.doc, tagged("rules", Rules::RULES), tagged("app_rules", app_rules), tagged("protocols", Rules::PROTOCOLS), channel(agent)]
|
|
88
89
|
.compact.reject { |part| part.strip.empty? }.join("\n\n")
|
|
89
90
|
end
|
|
90
91
|
|
|
@@ -113,9 +114,12 @@ module Pinecall
|
|
|
113
114
|
# Marker for a collapsed stretch of the call.
|
|
114
115
|
def collapsed(seq) = "<!-- collapsed: #{JSON.generate({ seq: })} -->"
|
|
115
116
|
|
|
117
|
+
# The view, and what the model reads of its caller's screen.
|
|
116
118
|
def view(agent, reading)
|
|
117
119
|
view = agent.class.view
|
|
118
|
-
view.nil? ? "" : view.render(reading)
|
|
120
|
+
shown = view.nil? ? "" : view.render(reading)
|
|
121
|
+
scene = agent.call? ? agent.call.scene : nil
|
|
122
|
+
[shown, tagged("screen", Screen.scene_line(agent.class.declared_scenes, scene))].reject(&:empty?).join("\n\n")
|
|
119
123
|
end
|
|
120
124
|
|
|
121
125
|
# The view's scope: state, call context, and remembered facts (queryable, never printed).
|
|
@@ -135,9 +139,9 @@ module Pinecall
|
|
|
135
139
|
sections.map { |header, text| "#{header}\n#{text}".rstrip }.join("\n\n")
|
|
136
140
|
end
|
|
137
141
|
|
|
138
|
-
def text_of(agent, called, reading)
|
|
142
|
+
def text_of(agent, called, reading, rules = [])
|
|
139
143
|
case called
|
|
140
|
-
when "identity" then identity(agent)
|
|
144
|
+
when "identity" then identity(agent, rules)
|
|
141
145
|
# Filled by the gateway from the agent's settings (`pinecall agent knowledge edit`).
|
|
142
146
|
when "knowledge" then ""
|
|
143
147
|
when "tools" then tools(agent)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pinecall
|
|
4
|
+
# What a tool call does on a served call, and one agent of a team taking the call from another.
|
|
5
|
+
module Bridge
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
# A task given, the screen moved, the call passed, or one of the agent's own tools run.
|
|
9
|
+
def dispatched(serving, name, arguments, call_id)
|
|
10
|
+
return serving.asking.given(name, arguments) if serving.asking.gives?(name)
|
|
11
|
+
return Screened.scene_chosen(serving.world, serving.screened, serving.klass, arguments[:scene]) if name == Screen::SHOW_SCREEN
|
|
12
|
+
|
|
13
|
+
declared = serving.klass.tool_named(name)
|
|
14
|
+
mate = declared.nil? && !serving.team.nil? ? Handoff.mate_named(name, serving.team) : nil
|
|
15
|
+
return serving.world.handoff(mate, arguments) unless mate.nil?
|
|
16
|
+
raise ToolFailed, "#{name}: this agent declares no such tool" if declared.nil?
|
|
17
|
+
|
|
18
|
+
serving.asking.run(name) { showing(serving, declared, arguments, call_id) }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Run a tool; one that shows a scene keeps it through the turn, and puts it up once it returns.
|
|
22
|
+
def showing(serving, declared, arguments, call_id)
|
|
23
|
+
scene = declared.options[:shows]
|
|
24
|
+
serving.screened.kept = true unless scene.nil?
|
|
25
|
+
result = Agent::Tools.run(serving.agent, declared, arguments, call_id)
|
|
26
|
+
serving.world.scene = scene.to_s unless scene.nil?
|
|
27
|
+
result
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# The call is served by an instance of `klass` from now on; the call itself stays.
|
|
31
|
+
def become(serving, klass, call, instance = nil)
|
|
32
|
+
instance ||= klass.new.seal
|
|
33
|
+
instance.reads_last_from(serving.last) unless serving.last.nil?
|
|
34
|
+
instance.serving(serving.world)
|
|
35
|
+
serving.agent = instance
|
|
36
|
+
serving.klass = klass
|
|
37
|
+
serving.transfers = Handoff.transfers_of(klass, serving.team) unless serving.team.nil?
|
|
38
|
+
forward_log(serving, call)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# One agent of the team takes the call from another; the model is told whose it is now.
|
|
42
|
+
def passed(serving, call, to, data)
|
|
43
|
+
slug = serving.team&.[](to)
|
|
44
|
+
raise ToolFailed, "#{to} is not of this call's team" if slug.nil?
|
|
45
|
+
raise ToolFailed, "this call was passed #{Handoff::MOST_PASSES} times already: answer the caller yourself" if serving.passes >= Handoff::MOST_PASSES
|
|
46
|
+
|
|
47
|
+
serving.stops.values_at(:state, :entries, :log).compact.each(&:call)
|
|
48
|
+
serving.stops.clear
|
|
49
|
+
serving.agent.serving(nil)
|
|
50
|
+
handed = as_object(data)
|
|
51
|
+
call.log(Handoff::PASSED, { from: serving.team.fetch(serving.klass, ""), to: slug, **handed })
|
|
52
|
+
serving.world.handed = handed
|
|
53
|
+
serving.world.scene = nil
|
|
54
|
+
become(serving, to, call)
|
|
55
|
+
serving.passes += 1
|
|
56
|
+
hooked(serving, call, :on_call, serving.world)
|
|
57
|
+
call.set_state(state_of(serving))
|
|
58
|
+
sync(serving, call)
|
|
59
|
+
listen(serving, call)
|
|
60
|
+
"The call is #{slug}'s now: go on as #{slug}, without saying it was passed."
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The line chooses while the caller hears its greeting; a choice it could not make is said on the log.
|
|
64
|
+
def routed(serving, call, answers)
|
|
65
|
+
first = Handoff.answered_by(answers, serving.world, serving.klass, serving.team)
|
|
66
|
+
if first.agent != serving.klass || !first.why.nil?
|
|
67
|
+
call.log(Handoff::ROUTED, { to: serving.team.fetch(first.agent, ""), why: first.why }.compact)
|
|
68
|
+
end
|
|
69
|
+
return if first.agent == serving.klass
|
|
70
|
+
|
|
71
|
+
serving.stops.delete(:log)&.call
|
|
72
|
+
become(serving, first.agent, call)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# The call's state as the gateway is told it: the agent's own, and its screen once drawn.
|
|
76
|
+
def state_of(serving) = Screened.state_with(serving.agent.snapshot, serving.screened)
|
|
77
|
+
|
|
78
|
+
# The page answered a task: the waiting tool goes on, the model is told, the form drawn again.
|
|
79
|
+
def given_by_page(serving, call, data)
|
|
80
|
+
told = serving.asking.given_by_page(data[:task], data[:given])
|
|
81
|
+
serving.world.reply(told) unless told.nil?
|
|
82
|
+
sync(serving, call)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pinecall
|
|
4
|
+
module Bridge
|
|
5
|
+
# A call's screen as the bridge keeps it: drawn from the agent, sent in the state when it moved,
|
|
6
|
+
# and moved by the conversation's turns.
|
|
7
|
+
module Screened
|
|
8
|
+
# The screen a call's page was last sent, and whether this turn kept its scene.
|
|
9
|
+
class Kept
|
|
10
|
+
attr_accessor :screen, :key, :by_user, :kept
|
|
11
|
+
|
|
12
|
+
def initialize
|
|
13
|
+
@screen = nil
|
|
14
|
+
@key = "[]"
|
|
15
|
+
@by_user = false
|
|
16
|
+
@kept = false
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
# Whether one of the classes draws a screen: its field is then public.
|
|
23
|
+
def screens?(classes) = classes.any? { |klass| Screen.draws?(klass) }
|
|
24
|
+
|
|
25
|
+
# A class with a screen declares its field public, and with tasks the page's event.
|
|
26
|
+
def declared(options, tasks:)
|
|
27
|
+
fields = [*options[:state_fields], { name: Screen::SCREEN, visibility: "public" }]
|
|
28
|
+
events = tasks ? [*options[:events], { name: Screen::TASK_GIVE, from: ["participant"] }] : options[:events]
|
|
29
|
+
options.merge(state_fields: fields, events:).compact
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Draw the screen with what is shown over it; true when it is not the one already sent.
|
|
33
|
+
def redrawn(drawn, shown, kept)
|
|
34
|
+
screen = Screen.screen_of([drawn, shown])
|
|
35
|
+
key = JSON.generate(screen)
|
|
36
|
+
return false if key == kept.key
|
|
37
|
+
|
|
38
|
+
kept.screen = screen
|
|
39
|
+
kept.key = key
|
|
40
|
+
true
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# The call's state as the gateway is told it: the agent's own, and its screen once drawn.
|
|
44
|
+
def state_with(snapshot, kept) = kept.screen.nil? ? snapshot : snapshot.merge(Screen::SCREEN.to_sym => kept.screen)
|
|
45
|
+
|
|
46
|
+
# A state the gateway kept, without the screen: it is drawn again, never restored.
|
|
47
|
+
def unscreened(state) = state.reject { |name, _| name.to_s == Screen::SCREEN }
|
|
48
|
+
|
|
49
|
+
# The tool the classes of a call move their screen with, declared once for all of them.
|
|
50
|
+
def scene_tools(classes, taken)
|
|
51
|
+
scenes = classes.each_with_object({}) { |klass, all| all.merge!(Screen.scenes_of(klass)) }
|
|
52
|
+
return [] if scenes.empty?
|
|
53
|
+
if taken.include?(Screen::SHOW_SCREEN)
|
|
54
|
+
raise DeclarationRefused, "#{Screen::SHOW_SCREEN} is the tool a screen is moved with, and an agent declares a tool so named"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
[Screen.showing_tool(scenes)]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# The tool the class serving the call moves its screen with, when it declares scenes.
|
|
61
|
+
def scene_tool_of(klass)
|
|
62
|
+
scenes = klass.declared_scenes
|
|
63
|
+
scenes.empty? ? [] : [Screen.showing_tool(scenes)]
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# The model chose a scene, or none: it is the call's now, and kept through this turn.
|
|
67
|
+
def scene_chosen(world, kept, klass, scene)
|
|
68
|
+
scene = scene.to_s
|
|
69
|
+
scenes = klass.declared_scenes
|
|
70
|
+
return "There is no scene #{scene}: #{[*scenes.keys, Screen::NO_SCENE].join(", ")}." unless scene == Screen::NO_SCENE || scenes.key?(scene)
|
|
71
|
+
|
|
72
|
+
world.scene = scene == Screen::NO_SCENE ? nil : scene
|
|
73
|
+
kept.kept = true
|
|
74
|
+
"shown"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# A turn went by; one the caller opened that kept no scene takes it away once the agent waits.
|
|
78
|
+
# True when the scene moved.
|
|
79
|
+
def turned(type, world, kept)
|
|
80
|
+
if type == "turn.user"
|
|
81
|
+
kept.by_user = true
|
|
82
|
+
kept.kept = false
|
|
83
|
+
return false
|
|
84
|
+
end
|
|
85
|
+
return false unless type == "agent.waiting" && kept.by_user
|
|
86
|
+
|
|
87
|
+
kept.by_user = false
|
|
88
|
+
return false if kept.kept || world.scene.nil?
|
|
89
|
+
|
|
90
|
+
world.scene = nil
|
|
91
|
+
true
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|