praxo-core 0.1.0 → 0.2.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.
data/lib/praxo/models.rb CHANGED
@@ -1,154 +1,154 @@
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
- module_function
34
-
35
- def content_block(**args)
36
- ContentBlock.new(
37
- type: args[:type] || 'text',
38
- text: args[:text],
39
- image_url: args[:image_url],
40
- data: args[:data]
41
- )
42
- end
43
-
44
- def tool_call(**args)
45
- ToolCall.new(
46
- id: args[:id] || '',
47
- name: args[:name] || '',
48
- arguments: args[:arguments] || {}
49
- )
50
- end
51
-
52
- def message(**args)
53
- Message.new(
54
- role: args[:role] || USER,
55
- content: args[:content],
56
- content_blocks: args[:content_blocks] || [],
57
- tool_calls: args[:tool_calls] || [],
58
- tool_id: args[:tool_id]
59
- )
60
- end
61
-
62
- def message_text(role, content)
63
- message(role: role, content: content)
64
- end
65
-
66
- def message_assistant(content, tool_calls = [])
67
- message(role: ASSISTANT, content: content, tool_calls: tool_calls)
68
- end
69
-
70
- def message_tool(content, tool_id)
71
- message(role: TOOL, content: content, tool_id: tool_id)
72
- end
73
-
74
- def tool_spec(**args)
75
- ToolSpec.new(
76
- name: args[:name] || '',
77
- description: args[:description] || '',
78
- parameters: args[:parameters],
79
- risk: args[:risk]
80
- )
81
- end
82
-
83
- def usage(input_tokens: 0, output_tokens: 0, cached_tokens: 0)
84
- Usage.new(input_tokens: input_tokens, output_tokens: output_tokens,
85
- cached_tokens: cached_tokens)
86
- end
87
-
88
- def request(**args)
89
- Request.new(
90
- messages: args[:messages] || [],
91
- tools: args[:tools] || [],
92
- tool_choice: args[:tool_choice],
93
- response_format: args[:response_format],
94
- temperature: args[:temperature] || 0
95
- )
96
- end
97
-
98
- def response(message:, usage: nil, finish_reason: nil)
99
- Response.new(message: message, usage: usage || Praxo::Models.usage,
100
- finish_reason: finish_reason)
101
- end
102
-
103
- def session(id:, messages: [])
104
- Session.new(id: id, messages: messages)
105
- end
106
-
107
- def run_result(run_id:, output:, steps:, usage:)
108
- RunResult.new(run_id: run_id, output: output, steps: steps, usage: usage)
109
- end
110
-
111
- def memory_record(**args)
112
- MemoryRecord.new(
113
- id: args[:id] || '',
114
- frame: args[:frame] || '',
115
- content: args[:content] || '',
116
- created_at: args[:created_at]
117
- )
118
- end
119
-
120
- def event(type:, session_id:, run_id: nil, data: nil)
121
- Event.new(type: type, session_id: session_id, run_id: run_id, data: data)
122
- end
123
-
124
- def action(kind:, session_id:, tool_name: nil, arguments: nil)
125
- Action.new(kind: kind, session_id: session_id, tool_name: tool_name,
126
- arguments: arguments)
127
- end
128
-
129
- def decision(allowed:, reason: '')
130
- Decision.new(allowed: allowed, reason: reason)
131
- end
132
-
133
- def message_wire(message)
134
- out = { 'role' => message.role }
135
- out['content'] = message.content unless message.content.nil?
136
- unless message.content_blocks.nil? || message.content_blocks.empty?
137
- out['contentBlocks'] = message.content_blocks.map do |block|
138
- wire = { 'type' => block.type }
139
- wire['text'] = block.text unless block.text.nil?
140
- wire['image_url'] = block.image_url unless block.image_url.nil?
141
- wire['data'] = block.data unless block.data.nil?
142
- wire
143
- end
144
- end
145
- unless message.tool_calls.nil? || message.tool_calls.empty?
146
- out['toolCalls'] = message.tool_calls.map do |call|
147
- { 'id' => call.id, 'name' => call.name, 'arguments' => call.arguments }
148
- end
149
- end
150
- out['toolId'] = message.tool_id unless message.tool_id.nil?
151
- out
152
- end
153
- end
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
+
33
+ module_function
34
+
35
+ def content_block(**args)
36
+ ContentBlock.new(
37
+ type: args[:type] || 'text',
38
+ text: args[:text],
39
+ image_url: args[:image_url],
40
+ data: args[:data]
41
+ )
42
+ end
43
+
44
+ def tool_call(**args)
45
+ ToolCall.new(
46
+ id: args[:id] || '',
47
+ name: args[:name] || '',
48
+ arguments: args[:arguments] || {}
49
+ )
50
+ end
51
+
52
+ def message(**args)
53
+ Message.new(
54
+ role: args[:role] || USER,
55
+ content: args[:content],
56
+ content_blocks: args[:content_blocks] || [],
57
+ tool_calls: args[:tool_calls] || [],
58
+ tool_id: args[:tool_id]
59
+ )
60
+ end
61
+
62
+ def message_text(role, content)
63
+ message(role: role, content: content)
64
+ end
65
+
66
+ def message_assistant(content, tool_calls = [])
67
+ message(role: ASSISTANT, content: content, tool_calls: tool_calls)
68
+ end
69
+
70
+ def message_tool(content, tool_id)
71
+ message(role: TOOL, content: content, tool_id: tool_id)
72
+ end
73
+
74
+ def tool_spec(**args)
75
+ ToolSpec.new(
76
+ name: args[:name] || '',
77
+ description: args[:description] || '',
78
+ parameters: args[:parameters],
79
+ risk: args[:risk]
80
+ )
81
+ end
82
+
83
+ def usage(input_tokens: 0, output_tokens: 0, cached_tokens: 0)
84
+ Usage.new(input_tokens: input_tokens, output_tokens: output_tokens,
85
+ cached_tokens: cached_tokens)
86
+ end
87
+
88
+ def request(**args)
89
+ Request.new(
90
+ messages: args[:messages] || [],
91
+ tools: args[:tools] || [],
92
+ tool_choice: args[:tool_choice],
93
+ response_format: args[:response_format],
94
+ temperature: args[:temperature] || 0
95
+ )
96
+ end
97
+
98
+ def response(message:, usage: nil, finish_reason: nil)
99
+ Response.new(message: message, usage: usage || Praxo::Models.usage,
100
+ finish_reason: finish_reason)
101
+ end
102
+
103
+ def session(id:, messages: [])
104
+ Session.new(id: id, messages: messages)
105
+ end
106
+
107
+ def run_result(run_id:, output:, steps:, usage:)
108
+ RunResult.new(run_id: run_id, output: output, steps: steps, usage: usage)
109
+ end
110
+
111
+ def memory_record(**args)
112
+ MemoryRecord.new(
113
+ id: args[:id] || '',
114
+ frame: args[:frame] || '',
115
+ content: args[:content] || '',
116
+ created_at: args[:created_at]
117
+ )
118
+ end
119
+
120
+ def event(type:, session_id:, run_id: nil, data: nil)
121
+ Event.new(type: type, session_id: session_id, run_id: run_id, data: data)
122
+ end
123
+
124
+ def action(kind:, session_id:, tool_name: nil, arguments: nil)
125
+ Action.new(kind: kind, session_id: session_id, tool_name: tool_name,
126
+ arguments: arguments)
127
+ end
128
+
129
+ def decision(allowed:, reason: '')
130
+ Decision.new(allowed: allowed, reason: reason)
131
+ end
132
+
133
+ def message_wire(message)
134
+ out = { 'role' => message.role }
135
+ out['content'] = message.content unless message.content.nil?
136
+ unless message.content_blocks.nil? || message.content_blocks.empty?
137
+ out['contentBlocks'] = message.content_blocks.map do |block|
138
+ wire = { 'type' => block.type }
139
+ wire['text'] = block.text unless block.text.nil?
140
+ wire['image_url'] = block.image_url unless block.image_url.nil?
141
+ wire['data'] = block.data unless block.data.nil?
142
+ wire
143
+ end
144
+ end
145
+ unless message.tool_calls.nil? || message.tool_calls.empty?
146
+ out['toolCalls'] = message.tool_calls.map do |call|
147
+ { 'id' => call.id, 'name' => call.name, 'arguments' => call.arguments }
148
+ end
149
+ end
150
+ out['toolId'] = message.tool_id unless message.tool_id.nil?
151
+ out
152
+ end
153
+ end
154
+ 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