tensaku 0.0.1 → 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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +216 -9
- data/README.md +40 -166
- data/app/assets/tensaku.js +352 -65
- data/exe/tensaku +5 -0
- data/lib/generators/tensaku/install_generator.rb +112 -51
- data/lib/tensaku/agents.rb +143 -0
- data/lib/tensaku/build.rb +32 -0
- data/lib/tensaku/cli.rb +155 -0
- data/lib/tensaku/doorbell.rb +91 -0
- data/lib/tensaku/endpoints.rb +128 -37
- data/lib/tensaku/file_write.rb +30 -0
- data/lib/tensaku/guard.rb +73 -17
- data/lib/tensaku/i18n.rb +19 -1
- data/lib/tensaku/mcp.rb +114 -96
- data/lib/tensaku/middleware.rb +48 -8
- data/lib/tensaku/notes/markdown.rb +83 -0
- data/lib/tensaku/notes/target.rb +59 -0
- data/lib/tensaku/notes.rb +257 -127
- data/lib/tensaku/presence.rb +60 -0
- data/lib/tensaku/project.rb +98 -0
- data/lib/tensaku/railtie.rb +16 -1
- data/lib/tensaku/repo.rb +57 -0
- data/lib/tensaku/session/tools.rb +164 -0
- data/lib/tensaku/session.rb +267 -0
- data/lib/tensaku/source/html_text.rb +9 -3
- data/lib/tensaku/source/locale.rb +25 -13
- data/lib/tensaku/source/markdown.rb +36 -8
- data/lib/tensaku/source/template.rb +26 -4
- data/lib/tensaku/store.rb +196 -0
- data/lib/tensaku/version.rb +1 -1
- data/lib/tensaku.rb +75 -16
- data/skills/tensaku/SKILL.md +58 -16
- metadata +52 -13
data/lib/tensaku/mcp.rb
CHANGED
|
@@ -1,135 +1,153 @@
|
|
|
1
1
|
require "json"
|
|
2
2
|
|
|
3
3
|
module Tensaku
|
|
4
|
-
#
|
|
5
|
-
#
|
|
6
|
-
#
|
|
4
|
+
# The MCP server an agent starts with `tensaku mcp`: newline-delimited JSON-RPC over stdin and stdout,
|
|
5
|
+
# in front of one Session. The dev server doesn't have to be running and no port is configured.
|
|
6
|
+
#
|
|
7
|
+
# Claude Code channels: the server declares the experimental claude/channel capability, which lets it
|
|
8
|
+
# push a message into a Claude Code session started with
|
|
9
|
+
# `claude --dangerously-load-development-channels server:tensaku`; the message starts a turn as if the
|
|
10
|
+
# person had typed it. A session without the flag drops these messages without an error, so the
|
|
11
|
+
# server first sends a hello with a code and the session only treats the channel as working once
|
|
12
|
+
# Claude calls channel_ready with that code (see Session).
|
|
7
13
|
class Mcp
|
|
8
14
|
PROTOCOL_VERSIONS = %w[2025-11-25 2025-06-18 2025-03-26 2024-11-05].freeze
|
|
9
|
-
MAX_WATCH = 300
|
|
10
15
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
description: "List Tensaku notes left on pages of this Rails app. Each note has a comment and targets with the exact file and line range that rendered the element. Defaults to notes that need work (open and question).",
|
|
15
|
-
inputSchema: {
|
|
16
|
-
type: "object",
|
|
17
|
-
properties: {
|
|
18
|
-
status: { type: "string", enum: %w[open question resolved active all], description: "active (default) means open or question." },
|
|
19
|
-
url: { type: "string", description: "Only notes left on this path, for example /about." }
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
name: "get_note",
|
|
25
|
-
description: "Get one Tensaku note by id.",
|
|
26
|
-
inputSchema: { type: "object", properties: { id: { type: "string" } }, required: [ "id" ] }
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
name: "resolve_note",
|
|
30
|
-
description: "Mark a note resolved after making the change. The reply is shown on the page next to a green pin, so say briefly what changed and where (for example: changed the heading in app/views/pages/_hero.html.erb:4).",
|
|
31
|
-
inputSchema: { type: "object", properties: { id: { type: "string" }, reply: { type: "string" } }, required: %w[id reply] }
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
name: "ask_question",
|
|
35
|
-
description: "Ask the person who wrote a note for clarification instead of guessing. The question shows on the page; the note's status becomes question until they answer by editing the note.",
|
|
36
|
-
inputSchema: { type: "object", properties: { id: { type: "string" }, question: { type: "string" } }, required: %w[id question] }
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
name: "watch_notes",
|
|
40
|
-
description: "Wait for new or changed open notes, so you can work through notes as they are added. Returns as soon as there are any, or empty after the timeout. Pass the checked_at value of the previous call as since so nothing is missed.",
|
|
41
|
-
inputSchema: {
|
|
42
|
-
type: "object",
|
|
43
|
-
properties: {
|
|
44
|
-
since: { type: "string", description: "ISO 8601 time. Defaults to now." },
|
|
45
|
-
timeout_seconds: { type: "integer", minimum: 1, maximum: MAX_WATCH, description: "Defaults to 60." }
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
name: "current_selection",
|
|
51
|
-
description: "What the person last selected in the Tensaku overlay (elements with file and line), for questions like 'what is this?'.",
|
|
52
|
-
inputSchema: { type: "object", properties: {} }
|
|
53
|
-
}
|
|
54
|
-
].freeze
|
|
16
|
+
CHANNEL_INSTRUCTIONS = " This server may also be a Claude Code channel. Its events arrive as <channel source=\"tensaku\"> messages. The first one asks you to call channel_ready with a code: do that, then tell the person in one line that Tensaku notes now arrive in this chat. Each later event holds notes the person saved on the page, already yours: work through them like notes from watch_notes. There's no need to call watch_notes while the channel works."
|
|
17
|
+
|
|
18
|
+
INSTRUCTIONS = "Tensaku notes are comments a person left on pages of this Rails app, each with the file and line that rendered the element. Work through open notes one at a time: start_note, make the change, then resolve_note with a one-line reply before the next note, so the page shows progress as it happens. Ask a question when a note is unclear, and decline_note when the change shouldn't be made. When the person asks you to watch or listen for notes, call watch_notes in a loop, and tell them the session name its first answer gives you (such as amber-fox), which the page shows next to your name. A note belongs to the session the person assigned it to, was handed it or started it: call start_note before you change anything for a note, and leave notes another session owns. A note with a screenshot says so; get_screenshot returns it. Once you have called watch_notes, notes sent while you work come with the answer of whichever Tensaku tool you call next, already yours: work through them, and call watch_notes again only when you have nothing else to do."
|
|
55
19
|
|
|
56
|
-
|
|
57
|
-
|
|
20
|
+
# Claude Code registers its channel listener a moment after initialization, and drops what comes
|
|
21
|
+
# before it (seen in its debug log), so the hello waits, and is sent once more until answered.
|
|
22
|
+
HELLO_AFTER = [ 1.5, 6 ].freeze
|
|
58
23
|
|
|
59
|
-
|
|
60
|
-
responses = Array.wrap(message).filter_map { respond(it) }
|
|
61
|
-
return [ 202, {}, [ "" ] ] if responses.empty?
|
|
24
|
+
attr_reader :session
|
|
62
25
|
|
|
63
|
-
|
|
64
|
-
|
|
26
|
+
# apps are the root directories of the Rails apps to serve.
|
|
27
|
+
def initialize(apps, output: $stdout)
|
|
28
|
+
@output = output
|
|
29
|
+
@session = Session.new(apps, push: method(:push))
|
|
30
|
+
@lock = Mutex.new
|
|
31
|
+
@calls = {}
|
|
32
|
+
@claude = false
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Reads requests until stdin closes. Tool calls run in threads of their own, so a waiting
|
|
36
|
+
# watch_notes doesn't hold up other requests and can be cancelled.
|
|
37
|
+
def run(input = $stdin)
|
|
38
|
+
heartbeat = session.heartbeat
|
|
39
|
+
input.each_line do |line|
|
|
40
|
+
next if line.strip.empty?
|
|
41
|
+
|
|
42
|
+
handle(line)
|
|
43
|
+
end
|
|
44
|
+
ensure
|
|
45
|
+
heartbeat&.kill
|
|
46
|
+
close
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# One line of JSON-RPC: a request, a notification or a batch.
|
|
50
|
+
def handle(line)
|
|
51
|
+
message = JSON.parse(line)
|
|
52
|
+
if message.is_a?(Hash) && message["method"] == "tools/call" && message.key?("id")
|
|
53
|
+
id = message["id"]
|
|
54
|
+
@lock.synchronize do
|
|
55
|
+
@calls[id] = Thread.new do
|
|
56
|
+
reply = respond(message)
|
|
57
|
+
@lock.synchronize { @calls.delete(id) }
|
|
58
|
+
write(reply)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
elsif message.is_a?(Array)
|
|
62
|
+
replies = message.filter_map { respond(it) }
|
|
63
|
+
write(replies) if replies.any?
|
|
64
|
+
elsif (reply = respond(message))
|
|
65
|
+
write(reply)
|
|
66
|
+
end
|
|
65
67
|
rescue JSON::ParserError
|
|
66
|
-
|
|
68
|
+
write(error_payload(nil, -32700, "Parse error"))
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Stops the hello and the tool calls still running, and the session's channel.
|
|
72
|
+
def close
|
|
73
|
+
@greeter&.kill
|
|
74
|
+
@greeter = nil
|
|
75
|
+
@lock.synchronize { @calls.values }.each(&:kill)
|
|
76
|
+
session.close
|
|
67
77
|
end
|
|
68
78
|
|
|
69
79
|
private
|
|
70
80
|
|
|
71
81
|
def respond(message)
|
|
82
|
+
return error_payload(nil, -32600, "Invalid Request") unless message.is_a?(Hash)
|
|
83
|
+
|
|
72
84
|
id = message["id"]
|
|
73
|
-
|
|
85
|
+
if id.nil? # a notification
|
|
86
|
+
cancel(message.dig("params", "requestId")) if message["method"] == "notifications/cancelled"
|
|
87
|
+
greet if message["method"] == "notifications/initialized"
|
|
88
|
+
return nil
|
|
89
|
+
end
|
|
74
90
|
|
|
75
91
|
result = case message["method"]
|
|
76
|
-
when "initialize" then initialize_result(message
|
|
92
|
+
when "initialize" then initialize_result(message["params"].to_h)
|
|
77
93
|
when "ping" then {}
|
|
78
|
-
when "tools/list" then { tools: TOOLS }
|
|
79
|
-
when "tools/call" then
|
|
94
|
+
when "tools/list" then { tools: Session::TOOLS }
|
|
95
|
+
when "tools/call" then session.call(message.dig("params", "name"), message.dig("params", "arguments") || {})
|
|
80
96
|
else return error_payload(id, -32601, "Method not found: #{message["method"]}")
|
|
81
97
|
end
|
|
82
98
|
{ jsonrpc: "2.0", id:, result: }
|
|
83
99
|
end
|
|
84
100
|
|
|
85
|
-
def initialize_result(
|
|
101
|
+
def initialize_result(params)
|
|
102
|
+
client = params["clientInfo"].to_h
|
|
103
|
+
session.name = (client["title"].presence || client["name"].presence || session.name).to_s.truncate(80)
|
|
104
|
+
# Channels are Claude Code's; other clients get neither the capability nor the hello.
|
|
105
|
+
@claude = [ client["name"], client["title"] ].any? { it.to_s.match?(/claude/i) }
|
|
106
|
+
session.seen!
|
|
107
|
+
requested = params["protocolVersion"]
|
|
108
|
+
capabilities = { tools: { listChanged: false } }
|
|
109
|
+
capabilities[:experimental] = { "claude/channel" => {} } if @claude
|
|
86
110
|
{
|
|
87
111
|
protocolVersion: PROTOCOL_VERSIONS.include?(requested) ? requested : PROTOCOL_VERSIONS.first,
|
|
88
|
-
capabilities
|
|
112
|
+
capabilities:,
|
|
89
113
|
serverInfo: { name: "tensaku", version: Tensaku::VERSION },
|
|
90
|
-
instructions:
|
|
114
|
+
instructions: INSTRUCTIONS + (@claude ? CHANNEL_INSTRUCTIONS : "")
|
|
91
115
|
}
|
|
92
116
|
end
|
|
93
117
|
|
|
94
|
-
def
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
when "watch_notes"
|
|
111
|
-
since = arguments["since"].present? ? Time.iso8601(arguments["since"]) : Time.now.utc
|
|
112
|
-
timeout = arguments["timeout_seconds"].to_i.clamp(1, MAX_WATCH)
|
|
113
|
-
timeout = 60 if arguments["timeout_seconds"].nil?
|
|
114
|
-
notes = Notes.watch(since:, timeout:)
|
|
115
|
-
text = notes.empty? ? "No new notes in #{timeout}s. Call watch_notes again to keep waiting." : Notes.to_markdown(notes)
|
|
116
|
-
tool_result(text, notes:, checked_at: Time.now.utc.iso8601)
|
|
117
|
-
when "current_selection"
|
|
118
|
-
selection = Endpoints.selection
|
|
119
|
-
tool_result(selection ? JSON.pretty_generate(selection) : "Nothing selected in the Tensaku overlay yet.", selection:)
|
|
120
|
-
else
|
|
121
|
-
tool_result("Unknown tool #{name}", error: true)
|
|
122
|
-
end
|
|
123
|
-
rescue Notes::NotFound, Error, ArgumentError => error
|
|
124
|
-
tool_result(error.message, error: true)
|
|
118
|
+
def greet
|
|
119
|
+
return unless @claude
|
|
120
|
+
|
|
121
|
+
@greeter ||= Thread.new { HELLO_AFTER.each { sleep it; hello } }
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# The first channel message. A session that loaded the channel gets it as a turn and calls
|
|
125
|
+
# channel_ready; any other drops it.
|
|
126
|
+
def hello
|
|
127
|
+
return if session.channel?
|
|
128
|
+
|
|
129
|
+
push("Tensaku is connected as #{session.label}. To have the person's notes pushed into this chat as they save them, call the channel_ready tool with code #{session.channel_code}, then tell the person in one line that their Tensaku notes now arrive here.", kind: "hello")
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def push(content, **meta)
|
|
133
|
+
write({ jsonrpc: "2.0", method: "notifications/claude/channel", params: { content:, meta: meta.transform_values(&:to_s) } })
|
|
125
134
|
end
|
|
126
135
|
|
|
127
|
-
def
|
|
128
|
-
|
|
136
|
+
def cancel(id)
|
|
137
|
+
@lock.synchronize { @calls.delete(id) }&.kill
|
|
129
138
|
end
|
|
130
139
|
|
|
131
140
|
def error_payload(id, code, message)
|
|
132
141
|
{ jsonrpc: "2.0", id:, error: { code:, message: } }
|
|
133
142
|
end
|
|
143
|
+
|
|
144
|
+
def write(payload)
|
|
145
|
+
@lock.synchronize do
|
|
146
|
+
@output.write(JSON.generate(payload) + "\n")
|
|
147
|
+
@output.flush
|
|
148
|
+
end
|
|
149
|
+
rescue IOError, Errno::EPIPE
|
|
150
|
+
nil
|
|
151
|
+
end
|
|
134
152
|
end
|
|
135
153
|
end
|
data/lib/tensaku/middleware.rb
CHANGED
|
@@ -2,10 +2,19 @@ module Tensaku
|
|
|
2
2
|
# Development only. Serves the overlay and the endpoints under Tensaku.config.mount_path, and adds
|
|
3
3
|
# the overlay to HTML pages: a meta tag with the request method and the boot token in <head>, and the
|
|
4
4
|
# script before </body>.
|
|
5
|
+
#
|
|
6
|
+
# The overlay goes only into HTML pages requested from this machine with a local Host (see
|
|
7
|
+
# Guard.local_request?), with a 2xx status or 422 (a form shown again with its errors). A page is a
|
|
8
|
+
# whole document, or a fragment the browser opens as a page or frame (a mailer preview part).
|
|
9
|
+
# Fragments fetched by scripts (htmx, Stimulus) and pages requested through another host name get
|
|
10
|
+
# no token. Translation markers are stripped from every other text response.
|
|
5
11
|
class Middleware
|
|
6
12
|
HTML = %r{\Atext/html}
|
|
7
13
|
STRIP_TYPES = %r{\A(?:text/|application/(?:json|xml|javascript|ld\+json))}
|
|
8
14
|
SCRIPT_OR_STYLE = %r{(<(script|style)\b[^>]*>)(.*?)(</\2>)}im
|
|
15
|
+
DOCUMENT = %r{<html[\s>]|</head>|</body>}i
|
|
16
|
+
NONCE = "action_dispatch.content_security_policy_nonce"
|
|
17
|
+
PAGE_DESTINATIONS = %w[document iframe frame].freeze
|
|
9
18
|
|
|
10
19
|
def initialize(app)
|
|
11
20
|
@app = app
|
|
@@ -15,12 +24,16 @@ module Tensaku
|
|
|
15
24
|
return Endpoints.new.call(env) if env["PATH_INFO"].to_s.start_with?("#{Tensaku.config.mount_path}/")
|
|
16
25
|
|
|
17
26
|
status, headers, body = @app.call(env)
|
|
18
|
-
|
|
19
|
-
return [ status, headers, body ] if status == 204 || status == 304 || headers["content-disposition"].to_s.start_with?("attachment")
|
|
27
|
+
return [ status, headers, body ] if passthrough?(status, headers, body, env)
|
|
20
28
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
29
|
+
type = headers["content-type"].to_s
|
|
30
|
+
if type.match?(HTML)
|
|
31
|
+
overlay = ((200..299).cover?(status) || status == 422) && Guard.local_request?(Rack::Request.new(env))
|
|
32
|
+
rewrite(status, headers, body) do |html|
|
|
33
|
+
page = html.match?(DOCUMENT) || PAGE_DESTINATIONS.include?(env["HTTP_SEC_FETCH_DEST"])
|
|
34
|
+
overlay && page ? inject(html, env) : strip_document(html, fragment: !page)
|
|
35
|
+
end
|
|
36
|
+
elsif type.match?(STRIP_TYPES) && !type.include?("event-stream")
|
|
24
37
|
rewrite(status, headers, body) { I18nMarkers.strip(it) }
|
|
25
38
|
else
|
|
26
39
|
[ status, headers, body ]
|
|
@@ -29,24 +42,51 @@ module Tensaku
|
|
|
29
42
|
|
|
30
43
|
private
|
|
31
44
|
|
|
45
|
+
# Responses left as they are: empty ones, downloads, files served from disk, streams and
|
|
46
|
+
# compressed bodies.
|
|
47
|
+
def passthrough?(status, headers, body, env)
|
|
48
|
+
status == 204 || status == 304 ||
|
|
49
|
+
headers["content-disposition"].to_s.start_with?("attachment") ||
|
|
50
|
+
headers["content-encoding"] ||
|
|
51
|
+
headers["x-accel-buffering"] == "no" ||
|
|
52
|
+
body.respond_to?(:to_path) ||
|
|
53
|
+
live?(env)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def live?(env)
|
|
57
|
+
controller = env["action_controller.instance"]
|
|
58
|
+
defined?(ActionController::Live) && controller.is_a?(ActionController::Live)
|
|
59
|
+
end
|
|
60
|
+
|
|
32
61
|
def rewrite(status, headers, body)
|
|
33
62
|
html = +""
|
|
34
63
|
body.each { html << it }
|
|
35
64
|
body.close if body.respond_to?(:close)
|
|
36
|
-
html
|
|
65
|
+
html.force_encoding(Encoding::UTF_8)
|
|
66
|
+
return [ status, headers, [ html ] ] unless html.valid_encoding?
|
|
67
|
+
|
|
68
|
+
html = yield(html)
|
|
37
69
|
headers = headers.dup
|
|
38
70
|
headers.delete("content-length")
|
|
39
71
|
headers.delete("etag")
|
|
40
72
|
[ status, headers, [ html ] ]
|
|
41
73
|
end
|
|
42
74
|
|
|
75
|
+
# An HTML fragment (htmx, a Stimulus fetch) keeps its markers for the overlay on the page that
|
|
76
|
+
# inserts it. A whole document that gets no overlay loses them.
|
|
77
|
+
def strip_document(html, fragment:)
|
|
78
|
+
fragment ? html : I18nMarkers.strip(html)
|
|
79
|
+
end
|
|
80
|
+
|
|
43
81
|
def inject(html, env)
|
|
44
82
|
# Markers inside scripts and styles would end up in code; the overlay can't reach those.
|
|
45
83
|
html = html.gsub(SCRIPT_OR_STYLE) { "#{$1}#{I18nMarkers.strip($3)}#{$4}" } if html.include?(I18nMarkers::FENCE)
|
|
46
84
|
return html if html.include?("data-tensaku-request=")
|
|
47
85
|
|
|
48
|
-
|
|
49
|
-
|
|
86
|
+
root = "#{env["SCRIPT_NAME"]}#{Tensaku.config.mount_path}"
|
|
87
|
+
nonce = env[NONCE].presence
|
|
88
|
+
meta = %(<meta name="tensaku" data-tensaku-request="#{ERB::Util.h(env["REQUEST_METHOD"])}" data-tensaku-token="#{Tensaku.token}" data-tensaku-root="#{ERB::Util.h(root)}">)
|
|
89
|
+
script = %(<script src="#{ERB::Util.h(root)}/tensaku.js?v=#{Endpoints.asset_digest}"#{%( nonce="#{ERB::Util.h(nonce)}") if nonce} data-turbo-eval="false" data-tensaku-ui defer></script>)
|
|
50
90
|
|
|
51
91
|
html = insert_before(html, "</head>", meta) || "#{meta}#{html}"
|
|
52
92
|
insert_before(html, "</body>", script) || "#{html}#{script}"
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
module Tensaku
|
|
2
|
+
module Notes
|
|
3
|
+
# Notes as an agent reads them: the MCP tools' answers, `tensaku notes` and the page's copy button.
|
|
4
|
+
module Markdown
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
# app names the app in each heading, for an agent that sees notes from several apps. With
|
|
8
|
+
# latest: true each thread is cut down to what is new (see latest_thread). session, the agent
|
|
9
|
+
# session the list is for, adds whether each note is its own.
|
|
10
|
+
def list(notes, app: nil, latest: false, session: nil)
|
|
11
|
+
notes = Array(notes)
|
|
12
|
+
return "No notes." if notes.empty?
|
|
13
|
+
|
|
14
|
+
base = Project.base_url
|
|
15
|
+
notes.map { note(it, base:, app:, latest:, session:) }.join("\n\n")
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# One line per note, for a chat where the detailed form is too long.
|
|
19
|
+
def short(notes)
|
|
20
|
+
notes = Array(notes)
|
|
21
|
+
return "No notes." if notes.empty?
|
|
22
|
+
|
|
23
|
+
notes.map do |note|
|
|
24
|
+
where = Array(note["targets"]).filter_map { [ it["file"], it["range"]&.split("-")&.first ].compact.join(":").presence }.uniq.first(3)
|
|
25
|
+
"- `#{note["id"]}` (#{note["status"]}) #{note["comment"].squish.truncate(160)}#{" at #{where.map { "`#{it}`" }.join(", ")}" if where.any?}"
|
|
26
|
+
end.join("\n")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def note(note, base: Project.base_url, app: nil, latest: false, session: nil)
|
|
30
|
+
page = base && note["url"].to_s.start_with?("/") ? "#{base.chomp("/")}#{note["url"]}" : note["url"]
|
|
31
|
+
lines = [ "## Tensaku note #{note["id"]} (#{note["status"]}) on #{page}#{" in #{app}" if app}", "" ]
|
|
32
|
+
if session && ACTIVE.include?(note["status"])
|
|
33
|
+
lines << if note["session"] == session then "*Yours.*"
|
|
34
|
+
elsif note["stopped"] then "*Its session stopped while working on it, so it is nobody's now. Part of the change may already be made: check the file before you redo it. Call start_note to take it.*"
|
|
35
|
+
else "*Nobody's yet: call start_note before you work on it, so no other session takes it.*"
|
|
36
|
+
end
|
|
37
|
+
lines << ""
|
|
38
|
+
end
|
|
39
|
+
lines << note["comment"]
|
|
40
|
+
targets = Array(note["targets"])
|
|
41
|
+
if targets.any?
|
|
42
|
+
lines << ""
|
|
43
|
+
targets.each { lines << "- #{target(it)}" }
|
|
44
|
+
end
|
|
45
|
+
if (rect = note["rect"])
|
|
46
|
+
lines << "" << "Area: #{rect["width"]}x#{rect["height"]} at (#{rect["x"]}, #{rect["y"]}) in a #{note["viewport"] || "unknown"} viewport"
|
|
47
|
+
end
|
|
48
|
+
if (shot = note["screenshot"])
|
|
49
|
+
lines << "" << "Screenshot: #{shot["width"]}x#{shot["height"]} PNG of what the note points at, as the page looked (get_screenshot #{note["id"]}, or `tensaku notes screenshot #{note["id"]}`)"
|
|
50
|
+
end
|
|
51
|
+
thread = Array(note["thread"])
|
|
52
|
+
if thread.any?
|
|
53
|
+
lines << ""
|
|
54
|
+
skipped, thread = latest ? latest_thread(thread) : [ 0, thread ]
|
|
55
|
+
lines << "> *(#{skipped} earlier #{skipped == 1 ? "message" : "messages"}; get_note #{note["id"]} has the whole thread)*" if skipped.positive?
|
|
56
|
+
thread.each { lines << "> **#{it["from"] == "you" ? "You" : (it["agent"] || "Agent")}:** #{it["text"].to_s.gsub("\n", "\n> ")}" }
|
|
57
|
+
end
|
|
58
|
+
lines.join("\n")
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# A long back-and-forth repeated on every watch_notes call costs the agent its context, so a list
|
|
62
|
+
# keeps the agent's latest message and everything after it, and says how many came before.
|
|
63
|
+
def latest_thread(thread)
|
|
64
|
+
last_agent = thread.rindex { it["from"] == "agent" }
|
|
65
|
+
kept = last_agent ? thread[last_agent..] : thread.last(2)
|
|
66
|
+
[ thread.size - kept.size, kept ]
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def target(target)
|
|
70
|
+
location = [ target["file"], target["range"] ].compact.join(":")
|
|
71
|
+
parts = [ target["kind"] ]
|
|
72
|
+
parts << "`#{target["tag"]}`" if target["tag"]
|
|
73
|
+
parts << "`#{target["key"]}` (#{target["locale"]})" if target["key"]
|
|
74
|
+
parts << "at `#{location}`" unless location.empty?
|
|
75
|
+
parts << "\"#{target["text"].to_s.squish.truncate(120)}\"" if target["text"].present?
|
|
76
|
+
parts << "classes `#{Array(target["classes"]).join(" ")}`" if target["classes"].present?
|
|
77
|
+
context = target["context"].to_h.compact_blank
|
|
78
|
+
parts << "(#{context.map { |key, value| "#{key}: #{Array(value).join(" < ")}" }.join(", ")})" if context.any?
|
|
79
|
+
parts.join(" ")
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
module Tensaku
|
|
2
|
+
module Notes
|
|
3
|
+
# What a note points at, as the overlay sent it, with the file and line filled in from the source:
|
|
4
|
+
# an element (a Herb stamp), a translation key, or a Markdown range.
|
|
5
|
+
module Target
|
|
6
|
+
KINDS = %w[element text translation markdown].freeze
|
|
7
|
+
|
|
8
|
+
module_function
|
|
9
|
+
|
|
10
|
+
# Fills in file positions from what the overlay saw: a Herb stamp, a translation key or a Markdown
|
|
11
|
+
# range. A target whose source can't be read keeps what the page said about it.
|
|
12
|
+
def from(raw)
|
|
13
|
+
raise Error, "A note target must be a JSON object" unless raw.is_a?(Hash)
|
|
14
|
+
|
|
15
|
+
raw = raw.stringify_keys
|
|
16
|
+
kind = KINDS.include?(raw["kind"]) ? raw["kind"] : "element"
|
|
17
|
+
target = { "kind" => kind, "text" => raw["text"]&.to_s&.truncate(500), "tag" => raw["tag"]&.to_s }
|
|
18
|
+
|
|
19
|
+
if raw["stamp"].present?
|
|
20
|
+
target["stamp"] = raw["stamp"].to_s
|
|
21
|
+
path, line, column = Source::Template.parse_stamp(raw["stamp"])
|
|
22
|
+
target["file"] = path
|
|
23
|
+
target["range"] = "#{line}:#{column}"
|
|
24
|
+
begin
|
|
25
|
+
target.merge!(Source::Template.new(path).element_range(line, column).stringify_keys)
|
|
26
|
+
rescue Error
|
|
27
|
+
nil
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
if kind == "translation"
|
|
32
|
+
target["key"] = raw["key"].to_s
|
|
33
|
+
target["locale"] = raw["locale"].to_s
|
|
34
|
+
if (entry = Source::Locale.find(target["locale"], target["key"]))
|
|
35
|
+
target["file"] = entry[:file]
|
|
36
|
+
target["range"] = entry[:line].to_s
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
if kind == "markdown" && raw["md"].present?
|
|
41
|
+
target["md"] = raw["md"].to_s
|
|
42
|
+
target["file"], target["range"] = raw["md"].to_s.split(/:(?=\d+:\d+-)/, 2)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
point = raw["point"]
|
|
46
|
+
target["point"] = point.map { it.to_f.clamp(0.0, 1.0).round(3) } if point.is_a?(Array) && point.size == 2 && point.all?(Numeric)
|
|
47
|
+
target["classes"] = Array(raw["classes"]).map(&:to_s).grep(/\A[^\s]{1,80}\z/).first(12)
|
|
48
|
+
target["context"] = raw["context"].to_h.slice("partials", "controller", "frame").transform_values { Array(it).map(&:to_s).first(10) }.compact_blank
|
|
49
|
+
target.compact_blank
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def rect(raw)
|
|
53
|
+
return nil unless raw.is_a?(Hash)
|
|
54
|
+
|
|
55
|
+
raw.to_h.stringify_keys.slice("x", "y", "width", "height").transform_values { it.to_f.round }
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|