mistri 0.6.1 → 0.8.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/CHANGELOG.md +355 -2
- data/CONTRIBUTING.md +17 -3
- data/NOTICE +11 -0
- data/README.md +145 -226
- data/UPGRADING.md +810 -0
- data/assets/vendors/README.md +12 -0
- data/assets/vendors/claude-mark.svg +3 -0
- data/assets/vendors/gemini-mark.svg +10 -0
- data/assets/vendors/openai-mark-dark.svg +1 -0
- data/assets/vendors/openai-mark.svg +1 -0
- data/docs/README.md +2 -1
- data/docs/context-and-workspaces.md +84 -13
- data/docs/logging.md +90 -0
- data/docs/mcp.md +762 -21
- data/docs/reliability.md +210 -24
- data/docs/sessions.md +415 -36
- data/docs/sub-agents.md +85 -7
- data/docs/tool-contracts.md +528 -13
- data/examples/approval.rb +7 -7
- data/examples/browser.rb +1 -1
- data/examples/page_editor.rb +2 -2
- data/examples/quickstart.rb +1 -1
- data/lib/generators/mistri/install/templates/migration.rb.tt +4 -2
- data/lib/generators/mistri/mcp/mcp_generator.rb +2 -2
- data/lib/generators/mistri/mcp/templates/model.rb.tt +21 -8
- data/lib/mistri/agent.rb +839 -141
- data/lib/mistri/call_order.rb +137 -0
- data/lib/mistri/child.rb +69 -13
- data/lib/mistri/compaction.rb +200 -35
- data/lib/mistri/compactor/text.rb +91 -0
- data/lib/mistri/compactor.rb +474 -117
- data/lib/mistri/console.rb +44 -11
- data/lib/mistri/content.rb +70 -3
- data/lib/mistri/definition.rb +2 -2
- data/lib/mistri/edit.rb +13 -7
- data/lib/mistri/errors.rb +37 -0
- data/lib/mistri/event.rb +20 -9
- data/lib/mistri/locks/rails_cache.rb +16 -2
- data/lib/mistri/locks.rb +12 -3
- data/lib/mistri/mcp/client.rb +247 -61
- data/lib/mistri/mcp/conversation.rb +161 -0
- data/lib/mistri/mcp/egress.rb +5 -4
- data/lib/mistri/mcp/mirror.rb +90 -0
- data/lib/mistri/mcp/negotiation.rb +222 -0
- data/lib/mistri/mcp/oauth/client_identity.rb +53 -0
- data/lib/mistri/mcp/oauth/tolerance.rb +61 -0
- data/lib/mistri/mcp/oauth.rb +303 -82
- data/lib/mistri/mcp/results.rb +97 -0
- data/lib/mistri/mcp/sdk_client.rb +47 -0
- data/lib/mistri/mcp/wires.rb +325 -52
- data/lib/mistri/mcp.rb +304 -55
- data/lib/mistri/memory.rb +1 -1
- data/lib/mistri/message.rb +45 -8
- data/lib/mistri/models.rb +146 -24
- data/lib/mistri/partial_json.rb +5 -5
- data/lib/mistri/providers/anthropic/assembler.rb +40 -3
- data/lib/mistri/providers/anthropic/serializer.rb +154 -22
- data/lib/mistri/providers/anthropic.rb +77 -12
- data/lib/mistri/providers/fake.rb +4 -1
- data/lib/mistri/providers/gemini/assembler.rb +63 -3
- data/lib/mistri/providers/gemini/serializer.rb +27 -16
- data/lib/mistri/providers/gemini.rb +56 -11
- data/lib/mistri/providers/openai/assembler.rb +36 -5
- data/lib/mistri/providers/openai/serializer.rb +76 -22
- data/lib/mistri/providers/openai.rb +91 -14
- data/lib/mistri/reminder.rb +35 -3
- data/lib/mistri/result.rb +6 -4
- data/lib/mistri/result_limit.rb +164 -0
- data/lib/mistri/sandbox/quickjs.rb +345 -0
- data/lib/mistri/sandbox.rb +18 -0
- data/lib/mistri/schema.rb +26 -2
- data/lib/mistri/scratchpad.rb +107 -0
- data/lib/mistri/session/log.rb +137 -0
- data/lib/mistri/session.rb +405 -75
- data/lib/mistri/sinks/logger.rb +407 -0
- data/lib/mistri/skills.rb +10 -4
- data/lib/mistri/spawner.rb +43 -12
- data/lib/mistri/sse.rb +3 -2
- data/lib/mistri/stores/active_record.rb +30 -6
- data/lib/mistri/stores/jsonl.rb +26 -7
- data/lib/mistri/stores/memory.rb +6 -2
- data/lib/mistri/sub_agent/execution.rb +11 -3
- data/lib/mistri/sub_agent/runtime.rb +11 -1
- data/lib/mistri/sub_agent.rb +13 -4
- data/lib/mistri/tool.rb +71 -7
- data/lib/mistri/tool_arguments.rb +10 -11
- data/lib/mistri/tool_context.rb +15 -3
- data/lib/mistri/tool_executor.rb +49 -26
- data/lib/mistri/tool_result.rb +8 -2
- data/lib/mistri/tool_search/index.rb +193 -0
- data/lib/mistri/tool_search.rb +200 -0
- data/lib/mistri/tools/delete_file.rb +22 -0
- data/lib/mistri/tools/edit_file.rb +24 -14
- data/lib/mistri/tools/find_in_file.rb +1 -0
- data/lib/mistri/tools/grep.rb +212 -0
- data/lib/mistri/tools/list_files.rb +38 -5
- data/lib/mistri/tools/read_file.rb +83 -21
- data/lib/mistri/tools/read_memory.rb +2 -1
- data/lib/mistri/tools/run_code.rb +167 -0
- data/lib/mistri/tools/write_file.rb +66 -9
- data/lib/mistri/tools.rb +72 -1
- data/lib/mistri/transport/streamable_http.rb +140 -0
- data/lib/mistri/transport.rb +53 -63
- data/lib/mistri/usage.rb +33 -4
- data/lib/mistri/version.rb +1 -1
- data/lib/mistri/workspace/active_record.rb +22 -10
- data/lib/mistri/workspace/directory.rb +5 -1
- data/lib/mistri/workspace/memory.rb +4 -0
- data/lib/mistri/workspace/path_space.rb +56 -0
- data/lib/mistri/workspace/single.rb +6 -1
- data/lib/mistri.rb +28 -4
- metadata +30 -2
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mistri
|
|
4
|
+
module ToolSearch
|
|
5
|
+
# The text index behind tool_search: one document for each tool, built
|
|
6
|
+
# from its name, its description and its parameters, and ranked with
|
|
7
|
+
# Okapi BM25.
|
|
8
|
+
module Index
|
|
9
|
+
# Okapi BM25 over the description and the parameters.
|
|
10
|
+
K1 = 1.2
|
|
11
|
+
B = 0.75
|
|
12
|
+
# A query word in the name outweighs any number of mentions in the text,
|
|
13
|
+
# whose share is at most K1 + 1.
|
|
14
|
+
NAME_WEIGHT = 3
|
|
15
|
+
STOP_WORDS = %w[
|
|
16
|
+
a an and are as at be by for from in is it of on or that the this to with
|
|
17
|
+
].freeze
|
|
18
|
+
# Han, kana and Hangul letters, and the long vowel mark that sits inside
|
|
19
|
+
# kana words. Runs of these often have no spaces between words.
|
|
20
|
+
UNSPACED = "\\p{Han}\\p{Hiragana}\\p{Katakana}\\p{Hangul}\\u30FC"
|
|
21
|
+
WORD = /[#{UNSPACED}]+|[[:alnum:]&&[^#{UNSPACED}]]+/
|
|
22
|
+
UNSPACED_RUN = /\A[#{UNSPACED}]/
|
|
23
|
+
UNSPACED_PAIR = /[#{UNSPACED}]{2,}/
|
|
24
|
+
private_constant :K1, :B, :NAME_WEIGHT, :STOP_WORDS, :UNSPACED, :WORD, :UNSPACED_RUN,
|
|
25
|
+
:UNSPACED_PAIR
|
|
26
|
+
|
|
27
|
+
Document = Data.define(:name, :size, :alike, :name_words, :local_words, :counts, :length)
|
|
28
|
+
private_constant :Document
|
|
29
|
+
|
|
30
|
+
module_function
|
|
31
|
+
|
|
32
|
+
# The matches for a query, best first.
|
|
33
|
+
def search(query, documents) = documents.values_at(*positions(query, documents))
|
|
34
|
+
|
|
35
|
+
# Where the matches for a query stand among the documents, best first.
|
|
36
|
+
def positions(query, documents)
|
|
37
|
+
terms = words(query).uniq
|
|
38
|
+
weights = weights(terms, documents)
|
|
39
|
+
average = documents.sum(&:length).fdiv(documents.length).nonzero? || 1.0
|
|
40
|
+
text = query.strip
|
|
41
|
+
wanted = identifier(text)
|
|
42
|
+
matches = documents.each_with_index.filter_map do |document, index|
|
|
43
|
+
name = name_rank(document, text, wanted)
|
|
44
|
+
covered, score = score(document, terms, weights, average)
|
|
45
|
+
[name, -covered, -score, index] if covered.positive? || name < 3
|
|
46
|
+
end
|
|
47
|
+
matches.sort.map(&:last)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# How a query names a tool: 0 its name, 1 its name without the prefix,
|
|
51
|
+
# 2 a name that reads the same, 3 not at all.
|
|
52
|
+
def name_rank(document, query, wanted)
|
|
53
|
+
name = document.name
|
|
54
|
+
return 3 if query.empty?
|
|
55
|
+
return 0 if name == query
|
|
56
|
+
return 1 if name.end_with?("__#{query}")
|
|
57
|
+
|
|
58
|
+
alike = wanted && document.alike
|
|
59
|
+
alike && (alike == wanted || alike.end_with?("__#{wanted}")) ? 2 : 3
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# One document for each tool. A tool that keeps its search document has
|
|
63
|
+
# it built once. Any other object that acts as a tool has one built for
|
|
64
|
+
# each agent.
|
|
65
|
+
def documents(tools)
|
|
66
|
+
tools.map do |tool|
|
|
67
|
+
next document(tool) unless tool.respond_to?(:search_document)
|
|
68
|
+
|
|
69
|
+
tool.search_document { document(tool) }
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def document(tool)
|
|
74
|
+
spec = tool.spec.transform_keys(&:to_sym)
|
|
75
|
+
parts = [spec[:description].to_s]
|
|
76
|
+
schema_text(spec[:input_schema], parts)
|
|
77
|
+
text = plain(parts.join(" "))
|
|
78
|
+
terms = words(text)
|
|
79
|
+
Document.new(name: -tool.name, size: JSON.generate(spec).bytesize,
|
|
80
|
+
alike: identifier(tool.name).freeze,
|
|
81
|
+
name_words: fixed(words(tool.name) + characters(plain(tool.name))),
|
|
82
|
+
local_words: fixed(words(tool.name.split("__").last)),
|
|
83
|
+
counts: (terms + characters(text)).map(&:-@).tally.freeze,
|
|
84
|
+
length: terms.length)
|
|
85
|
+
end
|
|
86
|
+
private_class_method :document
|
|
87
|
+
|
|
88
|
+
# A document is shared by every agent that holds its tool, so its parts
|
|
89
|
+
# are frozen.
|
|
90
|
+
def fixed(words) = words.uniq.map(&:-@).freeze
|
|
91
|
+
private_class_method :fixed
|
|
92
|
+
|
|
93
|
+
# The single characters of the runs that are matched by pairs, so that a
|
|
94
|
+
# query of one character finds them. They are extra entries: a text's
|
|
95
|
+
# length is counted without them.
|
|
96
|
+
def characters(text) = text.scan(UNSPACED_PAIR).flat_map(&:chars)
|
|
97
|
+
private_class_method :characters
|
|
98
|
+
|
|
99
|
+
# Text in one width and one composed form, so that half-width kana and a
|
|
100
|
+
# letter written as two marks match their usual form.
|
|
101
|
+
def plain(text)
|
|
102
|
+
text = text.to_s
|
|
103
|
+
text.ascii_only? ? text : text.unicode_normalize(:nfkc)
|
|
104
|
+
end
|
|
105
|
+
private_class_method :plain
|
|
106
|
+
|
|
107
|
+
# Descriptions and property names, through nested objects, arrays,
|
|
108
|
+
# alternatives and shared definitions.
|
|
109
|
+
def schema_text(schema, parts)
|
|
110
|
+
return unless schema.is_a?(Hash)
|
|
111
|
+
|
|
112
|
+
description = keyword(schema, "description")
|
|
113
|
+
parts << description if description.is_a?(String)
|
|
114
|
+
named(schema, "properties").each do |name, property|
|
|
115
|
+
parts << name.to_s
|
|
116
|
+
schema_text(property, parts)
|
|
117
|
+
end
|
|
118
|
+
schema_text(keyword(schema, "items"), parts)
|
|
119
|
+
%w[anyOf oneOf allOf].each do |alternatives|
|
|
120
|
+
Array(keyword(schema, alternatives)).each { |member| schema_text(member, parts) }
|
|
121
|
+
end
|
|
122
|
+
%w[$defs definitions].each do |shared|
|
|
123
|
+
named(schema, shared).each_value { |member| schema_text(member, parts) }
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
private_class_method :schema_text
|
|
127
|
+
|
|
128
|
+
def keyword(schema, name) = schema[name] || schema[name.to_sym]
|
|
129
|
+
private_class_method :keyword
|
|
130
|
+
|
|
131
|
+
def named(schema, name)
|
|
132
|
+
members = keyword(schema, name)
|
|
133
|
+
members.is_a?(Hash) ? members : {}
|
|
134
|
+
end
|
|
135
|
+
private_class_method :named
|
|
136
|
+
|
|
137
|
+
# How rare each query word is across the tools.
|
|
138
|
+
def weights(terms, documents)
|
|
139
|
+
terms.to_h do |term|
|
|
140
|
+
holders = documents.count do |document|
|
|
141
|
+
document.name_words.include?(term) || document.counts.key?(term)
|
|
142
|
+
end
|
|
143
|
+
[term, Math.log(1 + ((documents.length - holders + 0.5) / (holders + 0.5)))]
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
private_class_method :weights
|
|
147
|
+
|
|
148
|
+
def score(document, terms, weights, average)
|
|
149
|
+
norm = K1 * (1 - B + (B * document.length / average))
|
|
150
|
+
covered = 0
|
|
151
|
+
score = terms.sum do |term|
|
|
152
|
+
times = document.counts.fetch(term, 0)
|
|
153
|
+
weight = (document.name_words.include?(term) ? NAME_WEIGHT : 0) +
|
|
154
|
+
(times * (K1 + 1) / (times + norm))
|
|
155
|
+
covered += 1 if weight.positive?
|
|
156
|
+
weights.fetch(term) * weight
|
|
157
|
+
end
|
|
158
|
+
whole_name = document.local_words.any? && (document.local_words - terms).empty?
|
|
159
|
+
[covered, whole_name ? score + weights.sum { |_term, weight| weight } : score]
|
|
160
|
+
end
|
|
161
|
+
private_class_method :score
|
|
162
|
+
|
|
163
|
+
def words(text)
|
|
164
|
+
plain(text).gsub(/([a-z0-9])([A-Z])/, '\1 \2').gsub(/([A-Z]+)([A-Z][a-z])/, '\1 \2')
|
|
165
|
+
.downcase.scan(WORD)
|
|
166
|
+
.flat_map { |word| word.match?(UNSPACED_RUN) ? pairs(word) : word }
|
|
167
|
+
.reject { |word| STOP_WORDS.include?(word) }.map { |word| singular(word) }
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# A run with no spaces has no words to split at, so it is matched by its
|
|
171
|
+
# pairs of characters, and a run of one character is itself.
|
|
172
|
+
def pairs(run)
|
|
173
|
+
run.length < 2 ? [run] : run.each_char.each_cons(2).map(&:join)
|
|
174
|
+
end
|
|
175
|
+
private_class_method :pairs
|
|
176
|
+
|
|
177
|
+
def singular(word)
|
|
178
|
+
return "#{word[0...-3]}y" if word.length > 4 && word.end_with?("ies")
|
|
179
|
+
return word[0...-2] if word.length > 4 && word.match?(/(?:ch|sh|ss|x|z)es\z/)
|
|
180
|
+
return word[0...-1] if word.length > 3 && word.end_with?("s") && !word.end_with?("ss")
|
|
181
|
+
|
|
182
|
+
word
|
|
183
|
+
end
|
|
184
|
+
private_class_method :singular
|
|
185
|
+
|
|
186
|
+
# A name as it reads whatever its case and punctuation, or nil for text
|
|
187
|
+
# that could not be a name at all.
|
|
188
|
+
def identifier(text)
|
|
189
|
+
text.downcase.gsub(/[-. ]/, "_") if text.match?(/\A[\w\-. ]*[A-Za-z0-9][\w\-. ]*\z/)
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
end
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "tool_search/index"
|
|
4
|
+
|
|
5
|
+
module Mistri
|
|
6
|
+
# Finds the tools an agent has not shown the model yet. A deferred tool
|
|
7
|
+
# costs no tokens until a search loads it: the model asks in words, the
|
|
8
|
+
# best matches are loaded, and it can call them from its next step.
|
|
9
|
+
module ToolSearch
|
|
10
|
+
NAME = "tool_search"
|
|
11
|
+
DESCRIPTION = "Finds tools you have not been shown and loads the best matches. You can " \
|
|
12
|
+
"call a loaded tool from your next step. Search here before you say you " \
|
|
13
|
+
"cannot do something."
|
|
14
|
+
LIMIT = 5
|
|
15
|
+
MAX_LIMIT = 20
|
|
16
|
+
# The definitions of one load, in bytes. Of 303 measured MCP definitions,
|
|
17
|
+
# nine in ten were under 2,200.
|
|
18
|
+
LOAD_BYTES = 20_000
|
|
19
|
+
# The names in one listing, in bytes. The 303 measured MCP names average
|
|
20
|
+
# 25 bytes, so about 800 names fit.
|
|
21
|
+
LISTING_BYTES = 20_000
|
|
22
|
+
QUERY_LIMIT = 500
|
|
23
|
+
# The list names every deferred tool, the loaded ones too.
|
|
24
|
+
LOADED = "Once a #{NAME} result states that a tool is loaded, call it directly and do not " \
|
|
25
|
+
"load it again.".freeze
|
|
26
|
+
SELECT = /\Aselect:/i
|
|
27
|
+
private_constant :DESCRIPTION, :LOADED, :SELECT, :Index
|
|
28
|
+
|
|
29
|
+
module_function
|
|
30
|
+
|
|
31
|
+
# The tool an agent adds when it holds deferred tools. loaded returns
|
|
32
|
+
# the names of the tools already loaded. A search does not load those
|
|
33
|
+
# again: its result states that they are loaded and names them in loads,
|
|
34
|
+
# so they stay loaded while that result is in the history. A search
|
|
35
|
+
# appends the names it loads to the list, so two searches in one turn do
|
|
36
|
+
# not load the same tool.
|
|
37
|
+
def tool(deferred, loaded: -> { [] })
|
|
38
|
+
documents = nil
|
|
39
|
+
turn = Mutex.new
|
|
40
|
+
Tool.define(NAME, DESCRIPTION, parallel: true, schema: lambda {
|
|
41
|
+
string :query, "Words that describe what you need to do, or select: followed by " \
|
|
42
|
+
"the exact names of the tools to load, separated by commas",
|
|
43
|
+
required: true
|
|
44
|
+
integer :limit, "How many tools to load at most (default #{LIMIT}, at most #{MAX_LIMIT})"
|
|
45
|
+
}) do |args|
|
|
46
|
+
query = args["query"].to_s[0, QUERY_LIMIT]
|
|
47
|
+
documents ||= Index.documents(deferred)
|
|
48
|
+
chosen, missing, requested = named_tools(query, documents)
|
|
49
|
+
unless chosen || Index.words(query).any?
|
|
50
|
+
next ToolResult.new(content: "The query has no words to search for. Describe what " \
|
|
51
|
+
"you need to do in a few words.", error: true)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
limit = args.fetch("limit") { [LIMIT, requested.to_i].max }.to_i.clamp(1, MAX_LIMIT)
|
|
55
|
+
matches = chosen || Index.search(query, documents)
|
|
56
|
+
names = loaded.call
|
|
57
|
+
fresh, held, left = turn.synchronize do
|
|
58
|
+
unloaded(matches, names, limit).tap do |picked, _|
|
|
59
|
+
names.concat(picked.map(&:name))
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
answer(fresh, held, missing.to_a, left, named: !chosen.nil?, full: fresh.length == limit)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# The best matches, best first. A query that is a tool's name finds that
|
|
67
|
+
# tool first: the name itself, then the name without its prefix, then a
|
|
68
|
+
# name that reads the same with other punctuation. After those, a tool
|
|
69
|
+
# that matches more of the query's words comes before one that matches
|
|
70
|
+
# fewer, whatever their scores. Equal matches keep the order the tools
|
|
71
|
+
# were given in.
|
|
72
|
+
def rank(query, tools, limit)
|
|
73
|
+
return [] if limit <= 0
|
|
74
|
+
|
|
75
|
+
tools.values_at(*Index.positions(query, Index.documents(tools)).first(limit))
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# The tools a query names, the names in it that match no tool, and the
|
|
79
|
+
# number of names. Nil when the query is a word search. A query that
|
|
80
|
+
# starts with select: is a list of names. A query that matches one tool's
|
|
81
|
+
# name selects that tool. So does a list whose every part is a full name
|
|
82
|
+
# or contains an underscore, when at least one part matches a tool: read
|
|
83
|
+
# file is two words, never the tools x__read and y__file.
|
|
84
|
+
def named_tools(query, documents)
|
|
85
|
+
text = query.strip
|
|
86
|
+
if text.match?(SELECT)
|
|
87
|
+
parts = text.sub(SELECT, "").split(",")
|
|
88
|
+
return [*pick(parts, documents) { 2 }, parts.length]
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
whole = pick([text], documents) { 2 }
|
|
92
|
+
return [*whole, 1] if whole.first.any?
|
|
93
|
+
|
|
94
|
+
parts = text.split(/[\s,]+/)
|
|
95
|
+
found, missing = pick(parts, documents) { |part| part.include?("_") ? 2 : 0 }
|
|
96
|
+
return unless parts.length > 1 && found.any? && missing.all? { |part| part.include?("_") }
|
|
97
|
+
|
|
98
|
+
[found, missing, parts.length]
|
|
99
|
+
end
|
|
100
|
+
private_class_method :named_tools
|
|
101
|
+
|
|
102
|
+
# For each part, the tools at its closest matching rank, up to the rank
|
|
103
|
+
# the block allows for that part.
|
|
104
|
+
def pick(parts, documents)
|
|
105
|
+
found = []
|
|
106
|
+
missing = []
|
|
107
|
+
parts.map(&:strip).reject(&:empty?).each do |part|
|
|
108
|
+
wanted = Index.identifier(part)
|
|
109
|
+
ranks = documents.group_by { |document| Index.name_rank(document, part, wanted) }
|
|
110
|
+
closest = ranks.keys.min
|
|
111
|
+
closest && closest <= yield(part) ? found.concat(ranks.fetch(closest)) : missing << part
|
|
112
|
+
end
|
|
113
|
+
[found.uniq, missing]
|
|
114
|
+
end
|
|
115
|
+
private_class_method :pick
|
|
116
|
+
|
|
117
|
+
# The matches to load, the loaded matches ranked before them, and the
|
|
118
|
+
# number of matches left unloaded. A load holds up to limit tools and up
|
|
119
|
+
# to LOAD_BYTES of definitions, and always its first tool.
|
|
120
|
+
def unloaded(matches, loaded, limit)
|
|
121
|
+
have = loaded.to_set
|
|
122
|
+
fresh = []
|
|
123
|
+
held = []
|
|
124
|
+
left = 0
|
|
125
|
+
room = LOAD_BYTES
|
|
126
|
+
matches.each do |document|
|
|
127
|
+
open = left.zero? && fresh.length < limit
|
|
128
|
+
if have.include?(document.name)
|
|
129
|
+
held << document if open
|
|
130
|
+
elsif open && (fresh.empty? || document.size <= room)
|
|
131
|
+
fresh << document
|
|
132
|
+
room -= document.size
|
|
133
|
+
else
|
|
134
|
+
left += 1
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
[fresh, held, left]
|
|
138
|
+
end
|
|
139
|
+
private_class_method :unloaded
|
|
140
|
+
|
|
141
|
+
# The listing the model reads: every deferred name, or the number of
|
|
142
|
+
# tools under each prefix once the names exceed LISTING_BYTES.
|
|
143
|
+
def listing(names)
|
|
144
|
+
return "No tools load on demand now. This list replaces any earlier one." if names.empty?
|
|
145
|
+
|
|
146
|
+
if names.sum(&:bytesize) > LISTING_BYTES
|
|
147
|
+
return "These tools load on demand. There are too many to list by name: " \
|
|
148
|
+
"#{inventory(names)}. Load one with #{NAME} before you call it: search with " \
|
|
149
|
+
"words that describe what you need. #{LOADED} This list replaces any earlier one."
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
"These tools load on demand. Load one with #{NAME} before you call it: search with " \
|
|
153
|
+
"words that describe what you need, or use select: followed by the names. #{LOADED} " \
|
|
154
|
+
"This list replaces any earlier one.\n#{names.join("\n")}"
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# The number of tools under each prefix, up to LISTING_BYTES of prefixes.
|
|
158
|
+
# Tools without a prefix and tools under the remaining prefixes are
|
|
159
|
+
# counted together.
|
|
160
|
+
def inventory(names)
|
|
161
|
+
groups = names.select { |name| name.include?("__") }
|
|
162
|
+
.group_by { |name| name.split("__", 2).first }
|
|
163
|
+
room = LISTING_BYTES
|
|
164
|
+
listed = groups.map { |prefix, group| "#{prefix} (#{count(group.length, "tool")})" }
|
|
165
|
+
.take_while { |entry| (room -= entry.bytesize + 2) >= 0 }
|
|
166
|
+
others = names.length - groups.values.first(listed.length).sum(&:length)
|
|
167
|
+
listed << count(others, listed.empty? ? "tool" : "other") if others.positive?
|
|
168
|
+
listed.join(", ")
|
|
169
|
+
end
|
|
170
|
+
private_class_method :inventory
|
|
171
|
+
|
|
172
|
+
def count(number, noun) = "#{number} #{noun}#{"s" unless number == 1}"
|
|
173
|
+
private_class_method :count
|
|
174
|
+
|
|
175
|
+
# named: the query named tools, so the result gives the number left
|
|
176
|
+
# unloaded. full: the load reached its count, so a word search adds no
|
|
177
|
+
# line about further matches.
|
|
178
|
+
def answer(fresh, held, missing, left, named:, full:)
|
|
179
|
+
lines = []
|
|
180
|
+
lines << "Loaded: #{names(fresh)}. You can call them from your next step." if fresh.any?
|
|
181
|
+
lines << "Already loaded, call them directly: #{names(held)}." if held.any?
|
|
182
|
+
if left.positive? && named
|
|
183
|
+
lines << "#{count(left, "more tool")} with these names. Search again to load them."
|
|
184
|
+
elsif left.positive? && !full
|
|
185
|
+
lines << "More tools match than one load holds. Search again to load them."
|
|
186
|
+
end
|
|
187
|
+
lines << "No tool is named #{missing.join(", ")}." if missing.any?
|
|
188
|
+
return "No tool matches. Try other words." if lines.empty?
|
|
189
|
+
|
|
190
|
+
ToolResult.new(content: lines.join("\n"), loads: (fresh + held).map(&:name))
|
|
191
|
+
end
|
|
192
|
+
private_class_method :answer
|
|
193
|
+
|
|
194
|
+
def names(documents)
|
|
195
|
+
listed = documents.first(MAX_LIMIT).map(&:name).join(", ")
|
|
196
|
+
documents.length > MAX_LIMIT ? "#{listed} and #{documents.length - MAX_LIMIT} more" : listed
|
|
197
|
+
end
|
|
198
|
+
private_class_method :names
|
|
199
|
+
end
|
|
200
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mistri
|
|
4
|
+
module Tools
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
def delete_file(workspace)
|
|
8
|
+
tool = Tool.define("delete_file", "Delete a document.",
|
|
9
|
+
save_result: false,
|
|
10
|
+
schema: -> { string :path, "Document path", required: true }) do |args|
|
|
11
|
+
refusal = unusable_path(args["path"]) || read_only(args["path"])
|
|
12
|
+
next refusal if refusal
|
|
13
|
+
|
|
14
|
+
with_document(workspace, args) do
|
|
15
|
+
workspace.delete(args["path"])
|
|
16
|
+
"Deleted #{args["path"]}"
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
bind(workspace, tool)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -14,19 +14,25 @@ module Mistri
|
|
|
14
14
|
# model's retry is one shot.
|
|
15
15
|
def edit_file(workspace)
|
|
16
16
|
atomic = atomic_workspace?(workspace)
|
|
17
|
-
Tool.define("edit_file",
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
17
|
+
tool = Tool.define("edit_file",
|
|
18
|
+
"Replace an exact snippet of a document. Copy old_string verbatim from " \
|
|
19
|
+
"read_file output including whitespace, without line-number prefixes. " \
|
|
20
|
+
"It must match exactly one place; add surrounding lines to make it " \
|
|
21
|
+
"unique, or set replace_all to change every occurrence. Several " \
|
|
22
|
+
"edits in one turn apply in the order you write them, and if one " \
|
|
23
|
+
"fails the ones after it are not run.",
|
|
24
|
+
eager_input_streaming: true, save_result: false,
|
|
25
|
+
argument_normalizer: Tools.method(:tolerate),
|
|
26
|
+
schema: lambda {
|
|
27
|
+
string :path, "Document path", required: true
|
|
28
|
+
string :old_string, "Exact text to replace (whitespace matters)",
|
|
29
|
+
required: true
|
|
30
|
+
string :new_string, "Replacement text", required: true
|
|
31
|
+
boolean :replace_all, "Replace every occurrence instead of exactly one"
|
|
32
|
+
}) do |args|
|
|
33
|
+
refusal = unusable_path(args["path"]) || read_only(args["path"])
|
|
34
|
+
next refusal if refusal
|
|
35
|
+
|
|
30
36
|
result = if atomic
|
|
31
37
|
replace_atomically(workspace, args)
|
|
32
38
|
else
|
|
@@ -34,10 +40,12 @@ module Mistri
|
|
|
34
40
|
end
|
|
35
41
|
next result if result.is_a?(ToolResult)
|
|
36
42
|
|
|
37
|
-
"Replaced #{result.count} occurrence(s) in #{args["path"]}"
|
|
43
|
+
"Replaced #{result.count} occurrence(s) in #{args["path"]}, " \
|
|
44
|
+
"version #{version(result.content)}"
|
|
38
45
|
rescue EditError, WorkspaceConflictError => e
|
|
39
46
|
ToolResult.new(content: "edit_file failed: #{e.message}", error: true)
|
|
40
47
|
end
|
|
48
|
+
bind(workspace, tool)
|
|
41
49
|
end
|
|
42
50
|
|
|
43
51
|
def replace_legacy(workspace, args)
|
|
@@ -80,6 +88,8 @@ module Mistri
|
|
|
80
88
|
end
|
|
81
89
|
|
|
82
90
|
def replacement(content, args)
|
|
91
|
+
raise EditError, "#{args["path"]} is not text" unless content.valid_encoding?
|
|
92
|
+
|
|
83
93
|
Edit.replace(content, args["old_string"], args["new_string"],
|
|
84
94
|
replace_all: args["replace_all"] == true)
|
|
85
95
|
end
|
|
@@ -8,6 +8,7 @@ module Mistri
|
|
|
8
8
|
Tool.define("find_in_file",
|
|
9
9
|
"Find text in a document. Returns line-numbered matches with context, " \
|
|
10
10
|
"so you can locate a region without reading the whole document.",
|
|
11
|
+
save_result: false, parallel: true,
|
|
11
12
|
schema: lambda {
|
|
12
13
|
string :path, "Document path", required: true
|
|
13
14
|
string :query, "Text to find (plain substring)", required: true
|