ReMe/reme_studio/mobile/ios/ReMeStudio/StudioAssets.swift
jinliyl 524b1b6908
feat(studio): unify the workbench and proactive runtime (#764)
* fix(proactive): simplify round files and restore failed summaries

* fix(proactive): validate document-relative Markdown links

* feat(proactive_agent): implement v2 daily record workflow with single-file decision

- Add ProactiveAgentStepV2 class to support single daily note decision and reminder
- Introduce RoundOutputs for v2 round file tracking, and keep RoundArtifacts for v1 summary backup
- Refactor AgentOutbox to support both v1 standalone reminder files and v2 daily reminder records
- Add proactive_record utilities to parse and validate v2 daily record frontmatter and metadata
- Enhance proactive_agent to prepare, read, and manage artifacts for both v1 and v2 flows
- Add configuration profile proactive_v2 to enable new workflow with backward compatibility
- Update proactive.yaml and reactive proactive_agent_step configuration comment for easy switching
- Provide extensive schema validation for v2 reminders ensuring priority and message consistency
- Document v2 workflow and usage in English and Chinese runtime docs
- Add unit tests for v2 step verifying single file output and proper decision handling

* refactor(agent/runtime): replace ReminderRef with string keys and simplify reminder handling

- Remove ReminderRef model and switch to using workspace-relative string keys for reminders
- Simplify AgentOutbox to read, write, and manage reminders keyed by strings
- Change file locking and version checking to protect concurrent edits on daily record files
- Replace legacy and v2 reminder file handling with unified ReminderFiles storage
- Adjust ChatNotifications to operate with the new key scheme and update pending/read sets
- Refactor RoundOutputs to RoundRecord for safer round record tracking and rollback
- Remove unused ProactiveRunResult and related references from imports and exports
- Update InboxWakeRequest, AgentReminder, and related models for consistent ID and path validation
- Adjust proactive agent runtime and local runtime to check reminder versions before commit
- Correct language and docs to describe reminders as daily records instead of separate files
- Clean up roles of inbox and outbox for reminder ownership, delivery, and acknowledgments

* fix(proactive): serialize round commits and align desktop reminders

* refactor(proactive): unify task triggers in Proactive Runtime

* fix(proactive): bound reminder payloads and simplify receipts

* refactor(studio): share workbench and simplify proactive delivery

* fix(studio): update and centralize build dependencies

* fix(proactive): guard draft repair and verify Studio deployments

* chore(docs): update source-map-js

* feat(release): support publishing selected artifacts from a tag

* chore(studio): remove the public playground

* refactor: simplify reminders and release workflows

* fix: preserve proactive records and align release provenance

* test(proactive): remove brittle prompt and preset assertions

* fix(proactive): register reminders after input acknowledgement

* feat(notebook): list projects across todos and ideas

* feat(studio): connect mobile apps to configurable ReMe services

* fix(proactive): reserve registered reminder paths

* fix(studio): request Android local network access

* fix(proactive): reserve reminder paths referenced by receipts
2026-11-23 17:19:21 +08:00

120 lines
5 KiB
Swift

import Foundation
import Network
import UniformTypeIdentifiers
/// Serves the bundled Vite build from a stable origin for WebKit storage and recording.
final class StudioAssets {
static let host = "127.0.0.1"
static let port: UInt16 = 47813
static let origin = "http://\(host):\(port)"
private let root: URL
private let listener: NWListener
private var connections: [ObjectIdentifier: NWConnection] = [:]
private let headerEnd = Data([13, 10, 13, 10])
init(root: URL) throws {
self.root = root
let parameters = NWParameters.tcp
parameters.requiredLocalEndpoint = .hostPort(
host: .ipv4(.loopback), port: NWEndpoint.Port(rawValue: Self.port)!
)
listener = try NWListener(using: parameters)
}
func start(onStateChange: @escaping (NWListener.State) -> Void) {
listener.stateUpdateHandler = onStateChange
listener.newConnectionHandler = { [weak self] connection in
guard let self, self.connections.count < 32 else {
connection.cancel()
return
}
self.connections[ObjectIdentifier(connection)] = connection
connection.start(queue: .main)
self.readRequest(from: connection, received: Data())
DispatchQueue.main.asyncAfter(deadline: .now() + 10) { [weak self, weak connection] in
if let connection { self?.close(connection) }
}
}
listener.start(queue: .main)
}
deinit {
listener.cancel()
for connection in connections.values { connection.cancel() }
}
private func readRequest(from connection: NWConnection, received: Data) {
connection.receive(minimumIncompleteLength: 1, maximumLength: 8192) { [weak self] data, _, complete, error in
guard let self else { return }
let request = received + (data ?? Data())
if error != nil || request.count > 16384 {
self.close(connection)
} else if let end = request.range(of: self.headerEnd) {
self.serve(request[..<end.lowerBound], on: connection)
} else if complete {
self.close(connection)
} else {
self.readRequest(from: connection, received: request)
}
}
}
private func serve(_ request: Data, on connection: NWConnection) {
guard let header = String(data: request, encoding: .utf8),
let requestLine = header.components(separatedBy: "\r\n").first else {
return respond("400 Bad Request", on: connection)
}
let fields = requestLine.split(separator: " ")
guard fields.count == 3, fields[2] == "HTTP/1.1" || fields[2] == "HTTP/1.0" else {
return respond("400 Bad Request", on: connection)
}
guard fields[0] == "GET" else { return respond("405 Method Not Allowed", on: connection) }
let path = fields[1].prefix { $0 != "?" }
let file: URL
if path == "/" || path == "/index.html" {
file = root.appendingPathComponent("index.html")
} else if path.hasPrefix("/assets/") {
let name = String(path.dropFirst("/assets/".count))
let fileCharacters = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._")
guard !name.isEmpty, !name.hasPrefix("."), name.unicodeScalars.allSatisfy(fileCharacters.contains) else {
return respond("404 Not Found", on: connection)
}
file = root.appendingPathComponent("assets", isDirectory: true).appendingPathComponent(name)
} else {
return respond("404 Not Found", on: connection)
}
guard let body = try? Data(contentsOf: file, options: .mappedIfSafe) else {
return respond("404 Not Found", on: connection)
}
let type = switch file.pathExtension {
case "html": "text/html; charset=utf-8"
case "css": "text/css"
case "js": "text/javascript"
default: UTType(filenameExtension: file.pathExtension)?.preferredMIMEType ?? "application/octet-stream"
}
respond("200 OK", on: connection, type: type, body: body)
}
private func respond(
_ status: String, on connection: NWConnection, type: String = "text/plain", body: Data = Data()
) {
let headers = [
"HTTP/1.1 \(status)",
"Content-Type: \(type)",
"Content-Length: \(body.count)",
"Connection: close",
"Cache-Control: no-cache",
"Content-Security-Policy: frame-ancestors 'none'; object-src 'none'; base-uri 'none'",
"X-Content-Type-Options: nosniff",
"", "",
].joined(separator: "\r\n")
connection.send(content: Data(headers.utf8) + body, completion: .contentProcessed { [weak self] _ in
self?.close(connection)
})
}
private func close(_ connection: NWConnection) {
if connections.removeValue(forKey: ObjectIdentifier(connection)) != nil { connection.cancel() }
}
}