praxo-core 0.2.0 → 0.4.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.
- checksums.yaml +4 -4
- data/LICENSE +21 -21
- data/README.md +93 -74
- data/api/0.1.0.txt +110 -110
- data/api/0.2.0.txt +112 -112
- data/api/0.3.0.txt +122 -0
- data/api/0.4.0.txt +122 -0
- data/lib/praxo/agent.rb +352 -221
- data/lib/praxo/errors.rb +18 -18
- data/lib/praxo/json.rb +47 -47
- data/lib/praxo/jsonschema.rb +122 -122
- data/lib/praxo/middleware.rb +138 -133
- data/lib/praxo/models.rb +186 -154
- data/lib/praxo/plugin.rb +147 -147
- data/lib/praxo/runtime.rb +224 -224
- data/lib/praxo/sse.rb +37 -0
- data/lib/praxo/toolcall.rb +96 -96
- data/lib/praxo.rb +10 -9
- metadata +4 -1
data/lib/praxo/models.rb
CHANGED
|
@@ -1,154 +1,186 @@
|
|
|
1
|
-
module Praxo
|
|
2
|
-
module Models
|
|
3
|
-
SYSTEM = 'system'
|
|
4
|
-
USER = 'user'
|
|
5
|
-
ASSISTANT = 'assistant'
|
|
6
|
-
TOOL = 'tool'
|
|
7
|
-
|
|
8
|
-
STREAM_KINDS = %w[values updates messages tasks debug checkpoint].freeze
|
|
9
|
-
|
|
10
|
-
ContentBlock = Struct.new(:type, :text, :image_url, :data, keyword_init: true)
|
|
11
|
-
ToolCall = Struct.new(:id, :name, :arguments, keyword_init: true)
|
|
12
|
-
ToolSpec = Struct.new(:name, :description, :parameters, :risk, keyword_init: true)
|
|
13
|
-
Message = Struct.new(:role, :content, :content_blocks, :tool_calls, :tool_id, keyword_init: true)
|
|
14
|
-
Usage = Struct.new(:input_tokens, :output_tokens, :cached_tokens, keyword_init: true) do
|
|
15
|
-
def plus(other)
|
|
16
|
-
Usage.new(
|
|
17
|
-
input_tokens: input_tokens.to_i + other.input_tokens.to_i,
|
|
18
|
-
output_tokens: output_tokens.to_i + other.output_tokens.to_i,
|
|
19
|
-
cached_tokens: cached_tokens.to_i + other.cached_tokens.to_i
|
|
20
|
-
)
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
Request = Struct.new(:messages, :tools, :tool_choice, :response_format, :temperature, keyword_init: true)
|
|
24
|
-
Response = Struct.new(:message, :usage, :finish_reason, keyword_init: true)
|
|
25
|
-
Session = Struct.new(:id, :messages, keyword_init: true)
|
|
26
|
-
RunResult = Struct.new(:run_id, :output, :steps, :usage, keyword_init: true)
|
|
27
|
-
MemoryRecord = Struct.new(:id, :frame, :content, :created_at, keyword_init: true)
|
|
28
|
-
PathGrant = Struct.new(:root, :access, keyword_init: true)
|
|
29
|
-
Action = Struct.new(:kind, :session_id, :tool_name, :arguments, keyword_init: true)
|
|
30
|
-
Decision = Struct.new(:allowed, :reason, keyword_init: true)
|
|
31
|
-
Event = Struct.new(:type, :session_id, :run_id, :data, keyword_init: true)
|
|
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
|
-
end
|
|
65
|
-
|
|
66
|
-
def
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
def
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
def
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
)
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
def
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
def
|
|
130
|
-
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
def
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
end
|
|
1
|
+
module Praxo
|
|
2
|
+
module Models
|
|
3
|
+
SYSTEM = 'system'
|
|
4
|
+
USER = 'user'
|
|
5
|
+
ASSISTANT = 'assistant'
|
|
6
|
+
TOOL = 'tool'
|
|
7
|
+
|
|
8
|
+
STREAM_KINDS = %w[values updates messages tasks debug checkpoint].freeze
|
|
9
|
+
|
|
10
|
+
ContentBlock = Struct.new(:type, :text, :image_url, :data, keyword_init: true)
|
|
11
|
+
ToolCall = Struct.new(:id, :name, :arguments, keyword_init: true)
|
|
12
|
+
ToolSpec = Struct.new(:name, :description, :parameters, :risk, keyword_init: true)
|
|
13
|
+
Message = Struct.new(:role, :content, :content_blocks, :tool_calls, :tool_id, keyword_init: true)
|
|
14
|
+
Usage = Struct.new(:input_tokens, :output_tokens, :cached_tokens, keyword_init: true) do
|
|
15
|
+
def plus(other)
|
|
16
|
+
Usage.new(
|
|
17
|
+
input_tokens: input_tokens.to_i + other.input_tokens.to_i,
|
|
18
|
+
output_tokens: output_tokens.to_i + other.output_tokens.to_i,
|
|
19
|
+
cached_tokens: cached_tokens.to_i + other.cached_tokens.to_i
|
|
20
|
+
)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
Request = Struct.new(:messages, :tools, :tool_choice, :response_format, :temperature, keyword_init: true)
|
|
24
|
+
Response = Struct.new(:message, :usage, :finish_reason, keyword_init: true)
|
|
25
|
+
Session = Struct.new(:id, :messages, keyword_init: true)
|
|
26
|
+
RunResult = Struct.new(:run_id, :output, :steps, :usage, keyword_init: true)
|
|
27
|
+
MemoryRecord = Struct.new(:id, :frame, :content, :created_at, keyword_init: true)
|
|
28
|
+
PathGrant = Struct.new(:root, :access, keyword_init: true)
|
|
29
|
+
Action = Struct.new(:kind, :session_id, :tool_name, :arguments, keyword_init: true)
|
|
30
|
+
Decision = Struct.new(:allowed, :reason, keyword_init: true)
|
|
31
|
+
Event = Struct.new(:type, :session_id, :run_id, :data, keyword_init: true)
|
|
32
|
+
Interrupt = Struct.new(:run_id, :session_id, :call, keyword_init: true)
|
|
33
|
+
InterruptDecision = Struct.new(
|
|
34
|
+
:action, :arguments, :response, :reason, keyword_init: true
|
|
35
|
+
)
|
|
36
|
+
StreamPart = Struct.new(:run_id, :session_id, :kind, :step, :data, keyword_init: true)
|
|
37
|
+
|
|
38
|
+
module_function
|
|
39
|
+
|
|
40
|
+
def content_block(**args)
|
|
41
|
+
ContentBlock.new(
|
|
42
|
+
type: args[:type] || 'text',
|
|
43
|
+
text: args[:text],
|
|
44
|
+
image_url: args[:image_url],
|
|
45
|
+
data: args[:data]
|
|
46
|
+
)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def interrupt(**args)
|
|
50
|
+
Interrupt.new(
|
|
51
|
+
run_id: args[:run_id] || '',
|
|
52
|
+
session_id: args[:session_id] || '',
|
|
53
|
+
call: args[:call]
|
|
54
|
+
)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def interrupt_decision(**args)
|
|
58
|
+
InterruptDecision.new(
|
|
59
|
+
action: args[:action] || '',
|
|
60
|
+
arguments: args[:arguments],
|
|
61
|
+
response: args[:response] || '',
|
|
62
|
+
reason: args[:reason] || ''
|
|
63
|
+
)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def tool_call(**args)
|
|
67
|
+
ToolCall.new(
|
|
68
|
+
id: args[:id] || '',
|
|
69
|
+
name: args[:name] || '',
|
|
70
|
+
arguments: args[:arguments] || {}
|
|
71
|
+
)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def message(**args)
|
|
75
|
+
Message.new(
|
|
76
|
+
role: args[:role] || USER,
|
|
77
|
+
content: args[:content],
|
|
78
|
+
content_blocks: args[:content_blocks] || [],
|
|
79
|
+
tool_calls: args[:tool_calls] || [],
|
|
80
|
+
tool_id: args[:tool_id]
|
|
81
|
+
)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def message_text(role, content)
|
|
85
|
+
message(role: role, content: content)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def message_assistant(content, tool_calls = [])
|
|
89
|
+
message(role: ASSISTANT, content: content, tool_calls: tool_calls)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def message_tool(content, tool_id)
|
|
93
|
+
message(role: TOOL, content: content, tool_id: tool_id)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def tool_spec(**args)
|
|
97
|
+
ToolSpec.new(
|
|
98
|
+
name: args[:name] || '',
|
|
99
|
+
description: args[:description] || '',
|
|
100
|
+
parameters: args[:parameters],
|
|
101
|
+
risk: args[:risk]
|
|
102
|
+
)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def usage(input_tokens: 0, output_tokens: 0, cached_tokens: 0)
|
|
106
|
+
Usage.new(input_tokens: input_tokens, output_tokens: output_tokens,
|
|
107
|
+
cached_tokens: cached_tokens)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def request(**args)
|
|
111
|
+
Request.new(
|
|
112
|
+
messages: args[:messages] || [],
|
|
113
|
+
tools: args[:tools] || [],
|
|
114
|
+
tool_choice: args[:tool_choice],
|
|
115
|
+
response_format: args[:response_format],
|
|
116
|
+
temperature: args[:temperature] || 0
|
|
117
|
+
)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def response(message:, usage: nil, finish_reason: nil)
|
|
121
|
+
Response.new(message: message, usage: usage || Praxo::Models.usage,
|
|
122
|
+
finish_reason: finish_reason)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def session(id:, messages: [])
|
|
126
|
+
Session.new(id: id, messages: messages)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def run_result(run_id:, output:, steps:, usage:)
|
|
130
|
+
RunResult.new(run_id: run_id, output: output, steps: steps, usage: usage)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def memory_record(**args)
|
|
134
|
+
MemoryRecord.new(
|
|
135
|
+
id: args[:id] || '',
|
|
136
|
+
frame: args[:frame] || '',
|
|
137
|
+
content: args[:content] || '',
|
|
138
|
+
created_at: args[:created_at]
|
|
139
|
+
)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def event(type:, session_id:, run_id: nil, data: nil)
|
|
143
|
+
Event.new(type: type, session_id: session_id, run_id: run_id, data: data)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def stream_part(run_id:, session_id:, kind:, step: 0, data: nil)
|
|
147
|
+
StreamPart.new(
|
|
148
|
+
run_id: run_id,
|
|
149
|
+
session_id: session_id,
|
|
150
|
+
kind: kind,
|
|
151
|
+
step: step,
|
|
152
|
+
data: data
|
|
153
|
+
)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def action(kind:, session_id:, tool_name: nil, arguments: nil)
|
|
157
|
+
Action.new(kind: kind, session_id: session_id, tool_name: tool_name,
|
|
158
|
+
arguments: arguments)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def decision(allowed:, reason: '')
|
|
162
|
+
Decision.new(allowed: allowed, reason: reason)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def message_wire(message)
|
|
166
|
+
out = { 'role' => message.role }
|
|
167
|
+
out['content'] = message.content unless message.content.nil?
|
|
168
|
+
unless message.content_blocks.nil? || message.content_blocks.empty?
|
|
169
|
+
out['contentBlocks'] = message.content_blocks.map do |block|
|
|
170
|
+
wire = { 'type' => block.type }
|
|
171
|
+
wire['text'] = block.text unless block.text.nil?
|
|
172
|
+
wire['image_url'] = block.image_url unless block.image_url.nil?
|
|
173
|
+
wire['data'] = block.data unless block.data.nil?
|
|
174
|
+
wire
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
unless message.tool_calls.nil? || message.tool_calls.empty?
|
|
178
|
+
out['toolCalls'] = message.tool_calls.map do |call|
|
|
179
|
+
{ 'id' => call.id, 'name' => call.name, 'arguments' => call.arguments }
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
out['toolId'] = message.tool_id unless message.tool_id.nil?
|
|
183
|
+
out
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
end
|
data/lib/praxo/plugin.rb
CHANGED
|
@@ -1,147 +1,147 @@
|
|
|
1
|
-
require_relative 'errors'
|
|
2
|
-
require_relative 'json'
|
|
3
|
-
|
|
4
|
-
module Praxo
|
|
5
|
-
module Plugin
|
|
6
|
-
PluginManifest = Struct.new(
|
|
7
|
-
:schema_version, :id, :version, :kind, :entrypoint, :protocol,
|
|
8
|
-
:capabilities, :permissions, :limits, :isolation, :health, keyword_init: true
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
KINDS = %w[
|
|
12
|
-
tool.provider.v1 model.provider.v1 policy.provider.v1
|
|
13
|
-
memory.provider.v1 sandbox.provider.v1
|
|
14
|
-
].freeze
|
|
15
|
-
TOP_KEYS = %w[
|
|
16
|
-
schemaVersion id version kind entrypoint protocol capabilities permissions
|
|
17
|
-
limits isolation health signature sbom
|
|
18
|
-
].freeze
|
|
19
|
-
ENTRYPOINT_KEYS = %w[type command module image args dir].freeze
|
|
20
|
-
PERMISSION_KEYS = %w[filesystem network env].freeze
|
|
21
|
-
LIMIT_KEYS = %w[memoryMB cpuMillis timeoutMs maxInflight].freeze
|
|
22
|
-
HEALTH_KEYS = %w[method intervalMs].freeze
|
|
23
|
-
SIGNATURE_KEYS = %w[algorithm keyId].freeze
|
|
24
|
-
|
|
25
|
-
module_function
|
|
26
|
-
|
|
27
|
-
def parse_manifest(raw)
|
|
28
|
-
Praxo.error('PLUGIN_INVALID', 'plugin: manifest is empty') if raw.nil? || raw.strip.empty?
|
|
29
|
-
begin
|
|
30
|
-
root = JSON.parse(raw)
|
|
31
|
-
rescue JSON::ParserError => e
|
|
32
|
-
Praxo.error('PLUGIN_INVALID', "plugin: manifest: #{e.message}")
|
|
33
|
-
end
|
|
34
|
-
Praxo.error('PLUGIN_INVALID', 'plugin: manifest must be an object') unless root.is_a?(Hash)
|
|
35
|
-
check_keys(root, TOP_KEYS, 'manifest')
|
|
36
|
-
schema_version = as_int(root['schemaVersion'], 'schemaVersion')
|
|
37
|
-
if schema_version != 1
|
|
38
|
-
Praxo.error('PLUGIN_INVALID',
|
|
39
|
-
"plugin: manifest: schemaVersion = #{schema_version}, want 1")
|
|
40
|
-
end
|
|
41
|
-
id = as_string(root['id'], 'id')
|
|
42
|
-
unless id.match?(/\A(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z/)
|
|
43
|
-
Praxo.error('PLUGIN_INVALID', "plugin: manifest: id #{id} is not reverse-DNS")
|
|
44
|
-
end
|
|
45
|
-
version = as_string(root['version'], 'version')
|
|
46
|
-
unless version.match?(/\A(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?\z/)
|
|
47
|
-
Praxo.error('PLUGIN_INVALID', "plugin: manifest: version #{version} is not semver")
|
|
48
|
-
end
|
|
49
|
-
kind = as_string(root['kind'], 'kind')
|
|
50
|
-
unless KINDS.include?(kind)
|
|
51
|
-
Praxo.error('PLUGIN_INVALID', "plugin: manifest: kind #{kind} is not a known contract")
|
|
52
|
-
end
|
|
53
|
-
protocol = as_string(root['protocol'], 'protocol')
|
|
54
|
-
unless protocol == 'jsonrpc-2'
|
|
55
|
-
Praxo.error('PLUGIN_INVALID', "plugin: manifest: protocol = #{protocol}, want jsonrpc-2")
|
|
56
|
-
end
|
|
57
|
-
entrypoint = as_object(root['entrypoint'], 'entrypoint')
|
|
58
|
-
check_keys(entrypoint, ENTRYPOINT_KEYS, 'entrypoint')
|
|
59
|
-
entry_type = as_string(entrypoint['type'], 'entrypoint.type')
|
|
60
|
-
required = case entry_type
|
|
61
|
-
when 'process' then 'command'
|
|
62
|
-
when 'wasm' then 'module'
|
|
63
|
-
when 'container' then 'image'
|
|
64
|
-
else
|
|
65
|
-
Praxo.error('PLUGIN_INVALID',
|
|
66
|
-
"plugin: manifest: unknown entrypoint type #{entry_type}")
|
|
67
|
-
end
|
|
68
|
-
unless text_present(entrypoint[required])
|
|
69
|
-
Praxo.error('PLUGIN_INVALID',
|
|
70
|
-
"plugin: manifest: #{entry_type} entrypoint requires #{required}")
|
|
71
|
-
end
|
|
72
|
-
isolation = as_string(root['isolation'], 'isolation')
|
|
73
|
-
unless isolation == entry_type
|
|
74
|
-
Praxo.error('PLUGIN_INVALID',
|
|
75
|
-
"plugin: manifest: isolation #{isolation} does not match entrypoint #{entry_type}")
|
|
76
|
-
end
|
|
77
|
-
permissions = as_object(root['permissions'], 'permissions')
|
|
78
|
-
check_keys(permissions, PERMISSION_KEYS, 'permissions')
|
|
79
|
-
limits = as_object(root['limits'], 'limits')
|
|
80
|
-
check_keys(limits, LIMIT_KEYS, 'limits')
|
|
81
|
-
limits.each do |key, value|
|
|
82
|
-
Praxo.error('PLUGIN_INVALID', 'plugin: manifest: limits must not be negative') if as_int(value, "limits.#{key}").negative?
|
|
83
|
-
end
|
|
84
|
-
health = as_object(root['health'], 'health')
|
|
85
|
-
check_keys(health, HEALTH_KEYS, 'health')
|
|
86
|
-
if root.key?('signature')
|
|
87
|
-
signature = as_object(root['signature'], 'signature')
|
|
88
|
-
check_keys(signature, SIGNATURE_KEYS, 'signature')
|
|
89
|
-
unless text_present(signature['algorithm']) && text_present(signature['keyId'])
|
|
90
|
-
Praxo.error('PLUGIN_INVALID',
|
|
91
|
-
'plugin: manifest: signature requires algorithm and keyId')
|
|
92
|
-
end
|
|
93
|
-
end
|
|
94
|
-
capabilities = []
|
|
95
|
-
if root.key?('capabilities')
|
|
96
|
-
unless root['capabilities'].is_a?(Array)
|
|
97
|
-
Praxo.error('PLUGIN_INVALID', 'plugin: manifest: capabilities must be an array')
|
|
98
|
-
end
|
|
99
|
-
root['capabilities'].each do |item|
|
|
100
|
-
capability = as_string(item, 'capabilities')
|
|
101
|
-
Praxo.error('PLUGIN_INVALID', 'plugin: manifest: capability must not be empty') if capability.empty?
|
|
102
|
-
Praxo.error('PLUGIN_INVALID', 'plugin: manifest: duplicate capability') if capabilities.include?(capability)
|
|
103
|
-
capabilities << capability
|
|
104
|
-
end
|
|
105
|
-
end
|
|
106
|
-
PluginManifest.new(
|
|
107
|
-
schema_version: schema_version, id: id, version: version, kind: kind,
|
|
108
|
-
entrypoint: entrypoint, protocol: protocol, capabilities: capabilities,
|
|
109
|
-
permissions: permissions, limits: limits, isolation: isolation, health: health
|
|
110
|
-
)
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
def check_keys(value, allowed, where)
|
|
114
|
-
value.each_key do |key|
|
|
115
|
-
unless allowed.include?(key)
|
|
116
|
-
Praxo.error('PLUGIN_INVALID', "plugin: manifest: unknown field #{key} in #{where}")
|
|
117
|
-
end
|
|
118
|
-
end
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
def as_object(value, where)
|
|
122
|
-
return {} if value.nil?
|
|
123
|
-
unless value.is_a?(Hash)
|
|
124
|
-
Praxo.error('PLUGIN_INVALID', "plugin: manifest: #{where} must be an object")
|
|
125
|
-
end
|
|
126
|
-
value
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
def as_string(value, where)
|
|
130
|
-
unless value.is_a?(String)
|
|
131
|
-
Praxo.error('PLUGIN_INVALID', "plugin: manifest: #{where} must be a string")
|
|
132
|
-
end
|
|
133
|
-
value
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
def as_int(value, where)
|
|
137
|
-
unless value.is_a?(Numeric) && value.to_i == value
|
|
138
|
-
Praxo.error('PLUGIN_INVALID', "plugin: manifest: #{where} must be an integer")
|
|
139
|
-
end
|
|
140
|
-
value.to_i
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
def text_present(value)
|
|
144
|
-
value.is_a?(String) && !value.empty?
|
|
145
|
-
end
|
|
146
|
-
end
|
|
147
|
-
end
|
|
1
|
+
require_relative 'errors'
|
|
2
|
+
require_relative 'json'
|
|
3
|
+
|
|
4
|
+
module Praxo
|
|
5
|
+
module Plugin
|
|
6
|
+
PluginManifest = Struct.new(
|
|
7
|
+
:schema_version, :id, :version, :kind, :entrypoint, :protocol,
|
|
8
|
+
:capabilities, :permissions, :limits, :isolation, :health, keyword_init: true
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
KINDS = %w[
|
|
12
|
+
tool.provider.v1 model.provider.v1 policy.provider.v1
|
|
13
|
+
memory.provider.v1 sandbox.provider.v1
|
|
14
|
+
].freeze
|
|
15
|
+
TOP_KEYS = %w[
|
|
16
|
+
schemaVersion id version kind entrypoint protocol capabilities permissions
|
|
17
|
+
limits isolation health signature sbom
|
|
18
|
+
].freeze
|
|
19
|
+
ENTRYPOINT_KEYS = %w[type command module image args dir].freeze
|
|
20
|
+
PERMISSION_KEYS = %w[filesystem network env].freeze
|
|
21
|
+
LIMIT_KEYS = %w[memoryMB cpuMillis timeoutMs maxInflight].freeze
|
|
22
|
+
HEALTH_KEYS = %w[method intervalMs].freeze
|
|
23
|
+
SIGNATURE_KEYS = %w[algorithm keyId].freeze
|
|
24
|
+
|
|
25
|
+
module_function
|
|
26
|
+
|
|
27
|
+
def parse_manifest(raw)
|
|
28
|
+
Praxo.error('PLUGIN_INVALID', 'plugin: manifest is empty') if raw.nil? || raw.strip.empty?
|
|
29
|
+
begin
|
|
30
|
+
root = JSON.parse(raw)
|
|
31
|
+
rescue JSON::ParserError => e
|
|
32
|
+
Praxo.error('PLUGIN_INVALID', "plugin: manifest: #{e.message}")
|
|
33
|
+
end
|
|
34
|
+
Praxo.error('PLUGIN_INVALID', 'plugin: manifest must be an object') unless root.is_a?(Hash)
|
|
35
|
+
check_keys(root, TOP_KEYS, 'manifest')
|
|
36
|
+
schema_version = as_int(root['schemaVersion'], 'schemaVersion')
|
|
37
|
+
if schema_version != 1
|
|
38
|
+
Praxo.error('PLUGIN_INVALID',
|
|
39
|
+
"plugin: manifest: schemaVersion = #{schema_version}, want 1")
|
|
40
|
+
end
|
|
41
|
+
id = as_string(root['id'], 'id')
|
|
42
|
+
unless id.match?(/\A(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z/)
|
|
43
|
+
Praxo.error('PLUGIN_INVALID', "plugin: manifest: id #{id} is not reverse-DNS")
|
|
44
|
+
end
|
|
45
|
+
version = as_string(root['version'], 'version')
|
|
46
|
+
unless version.match?(/\A(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?\z/)
|
|
47
|
+
Praxo.error('PLUGIN_INVALID', "plugin: manifest: version #{version} is not semver")
|
|
48
|
+
end
|
|
49
|
+
kind = as_string(root['kind'], 'kind')
|
|
50
|
+
unless KINDS.include?(kind)
|
|
51
|
+
Praxo.error('PLUGIN_INVALID', "plugin: manifest: kind #{kind} is not a known contract")
|
|
52
|
+
end
|
|
53
|
+
protocol = as_string(root['protocol'], 'protocol')
|
|
54
|
+
unless protocol == 'jsonrpc-2'
|
|
55
|
+
Praxo.error('PLUGIN_INVALID', "plugin: manifest: protocol = #{protocol}, want jsonrpc-2")
|
|
56
|
+
end
|
|
57
|
+
entrypoint = as_object(root['entrypoint'], 'entrypoint')
|
|
58
|
+
check_keys(entrypoint, ENTRYPOINT_KEYS, 'entrypoint')
|
|
59
|
+
entry_type = as_string(entrypoint['type'], 'entrypoint.type')
|
|
60
|
+
required = case entry_type
|
|
61
|
+
when 'process' then 'command'
|
|
62
|
+
when 'wasm' then 'module'
|
|
63
|
+
when 'container' then 'image'
|
|
64
|
+
else
|
|
65
|
+
Praxo.error('PLUGIN_INVALID',
|
|
66
|
+
"plugin: manifest: unknown entrypoint type #{entry_type}")
|
|
67
|
+
end
|
|
68
|
+
unless text_present(entrypoint[required])
|
|
69
|
+
Praxo.error('PLUGIN_INVALID',
|
|
70
|
+
"plugin: manifest: #{entry_type} entrypoint requires #{required}")
|
|
71
|
+
end
|
|
72
|
+
isolation = as_string(root['isolation'], 'isolation')
|
|
73
|
+
unless isolation == entry_type
|
|
74
|
+
Praxo.error('PLUGIN_INVALID',
|
|
75
|
+
"plugin: manifest: isolation #{isolation} does not match entrypoint #{entry_type}")
|
|
76
|
+
end
|
|
77
|
+
permissions = as_object(root['permissions'], 'permissions')
|
|
78
|
+
check_keys(permissions, PERMISSION_KEYS, 'permissions')
|
|
79
|
+
limits = as_object(root['limits'], 'limits')
|
|
80
|
+
check_keys(limits, LIMIT_KEYS, 'limits')
|
|
81
|
+
limits.each do |key, value|
|
|
82
|
+
Praxo.error('PLUGIN_INVALID', 'plugin: manifest: limits must not be negative') if as_int(value, "limits.#{key}").negative?
|
|
83
|
+
end
|
|
84
|
+
health = as_object(root['health'], 'health')
|
|
85
|
+
check_keys(health, HEALTH_KEYS, 'health')
|
|
86
|
+
if root.key?('signature')
|
|
87
|
+
signature = as_object(root['signature'], 'signature')
|
|
88
|
+
check_keys(signature, SIGNATURE_KEYS, 'signature')
|
|
89
|
+
unless text_present(signature['algorithm']) && text_present(signature['keyId'])
|
|
90
|
+
Praxo.error('PLUGIN_INVALID',
|
|
91
|
+
'plugin: manifest: signature requires algorithm and keyId')
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
capabilities = []
|
|
95
|
+
if root.key?('capabilities')
|
|
96
|
+
unless root['capabilities'].is_a?(Array)
|
|
97
|
+
Praxo.error('PLUGIN_INVALID', 'plugin: manifest: capabilities must be an array')
|
|
98
|
+
end
|
|
99
|
+
root['capabilities'].each do |item|
|
|
100
|
+
capability = as_string(item, 'capabilities')
|
|
101
|
+
Praxo.error('PLUGIN_INVALID', 'plugin: manifest: capability must not be empty') if capability.empty?
|
|
102
|
+
Praxo.error('PLUGIN_INVALID', 'plugin: manifest: duplicate capability') if capabilities.include?(capability)
|
|
103
|
+
capabilities << capability
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
PluginManifest.new(
|
|
107
|
+
schema_version: schema_version, id: id, version: version, kind: kind,
|
|
108
|
+
entrypoint: entrypoint, protocol: protocol, capabilities: capabilities,
|
|
109
|
+
permissions: permissions, limits: limits, isolation: isolation, health: health
|
|
110
|
+
)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def check_keys(value, allowed, where)
|
|
114
|
+
value.each_key do |key|
|
|
115
|
+
unless allowed.include?(key)
|
|
116
|
+
Praxo.error('PLUGIN_INVALID', "plugin: manifest: unknown field #{key} in #{where}")
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def as_object(value, where)
|
|
122
|
+
return {} if value.nil?
|
|
123
|
+
unless value.is_a?(Hash)
|
|
124
|
+
Praxo.error('PLUGIN_INVALID', "plugin: manifest: #{where} must be an object")
|
|
125
|
+
end
|
|
126
|
+
value
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def as_string(value, where)
|
|
130
|
+
unless value.is_a?(String)
|
|
131
|
+
Praxo.error('PLUGIN_INVALID', "plugin: manifest: #{where} must be a string")
|
|
132
|
+
end
|
|
133
|
+
value
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def as_int(value, where)
|
|
137
|
+
unless value.is_a?(Numeric) && value.to_i == value
|
|
138
|
+
Praxo.error('PLUGIN_INVALID', "plugin: manifest: #{where} must be an integer")
|
|
139
|
+
end
|
|
140
|
+
value.to_i
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def text_present(value)
|
|
144
|
+
value.is_a?(String) && !value.empty?
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|