gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/plugins/fetch/pending_fetch.hl

4.3 KB

  1. \* A request in flight. Handed back by fetch()/fetchStart() — never `new`ed.
  2. The request is already on the wire when you hold one: it has a thread of
  3. its own, holding one costs a socket, not a fiber. With stream = true the
  4. events below arrive AT this object. *\
  5. \* A decoded piece of the body, token by token (stream mode). *\
  6. emit chunk(
  7. String chunk \\ the decoded bytes as they arrived
  8. )
  9. \* The stream's final frame — body is EMPTY, the chunks were the body. *\
  10. emit done(
  11. Number status \\ the HTTP status
  12. Boolean ok \\ true for 2xx
  13. Hybrid headers \\ response headers
  14. String error \\ '' or the failure text
  15. Boolean aborted \\ true when abort() cut it
  16. )
  17. Hybrid #handle
  18. Number #srcId = 0
  19. // Has the answer arrived? Never waits — a poll for code that wants to do
  20. // something else in the meantime.
  21. done() {
  22. return #handle.done()
  23. }
  24. // THE WAIT. Blocks the calling fiber until the answer is in, then hands back a
  25. // FetchResponse. A failed fetch (refused connection, timeout, TLS verification,
  26. // too many redirects) surfaces here as a located error routed to `on Error`, so
  27. // a caller that wants to survive one writes an `on Error(e)` handler.
  28. response() {
  29. // Not in yet: let the sibling fibers (a `concurrent` group, the loop's other
  30. // handlers) run instead of blocking the thread on this socket. On the process
  31. // stack there is no one to yield to and result() is the wait.
  32. // The yield names the fetch's bell (an eventfd rung when it is done): a
  33. // `concurrent` group whose arms ALL wait sleeps on those, not on a timer.
  34. let bell = #handle.doneFd()
  35. while (!#handle.done()) {
  36. if (!__native("fiber.yield", bell)) { break }
  37. }
  38. let r = #handle.result()
  39. // An `on Error` handler that ABSORBED the failure leaves the call yielding
  40. // null (the plugin-error contract every `hl:` module follows). Hydrating null
  41. // would be a second, misleading error, so a caught failure answers null —
  42. // exactly what `hl:fs`'s file() does.
  43. if (r == null) {
  44. return null
  45. }
  46. return r > new FetchResponse()
  47. }
  48. // STREAM MODE (the LLM-token flow). `fetch(url, { stream = true })` calls this
  49. // before handing the instance out: the ticket's own event source is registered
  50. // AT this instance and the fan-out below re-emits every frame at itself, so app
  51. // code holds nothing but the object and its events — `on p.chunk`, `on p.done`.
  52. arm() {
  53. #srcId = __native("eventloop.register", #handle.events(), "__fetchEvent")
  54. }
  55. // THE FAN-OUT: a native frame in, a DECLARED event out. The frames are Hybrids
  56. // ({ tag, url, chunk } or { tag, url, status, ok, body, error, headers, aborted });
  57. // `chunk` and `done` above declare TYPED parameters, so each field is handed over
  58. // BY NAME — `emit chunk(ev)` would bind a Hybrid to `String chunk` and fail every
  59. // frame (mission 257: it did).
  60. //
  61. // NAMED, NOT POSITIONAL, and that distinction cost the creator a night (2026-08-30).
  62. // 257 fixed the Hybrid-into-String bind by emitting POSITIONALLY — `emit
  63. // chunk(ev.chunk)` — which type-checks and is still wrong: every handler in this
  64. // language receives a PAYLOAD OBJECT and destructures it. His own working code is
  65. // `on stream.chunk({ chunk })`, exactly as `on server.line({ line, stream })` reads
  66. // for hl:proc and as his `emit ready(port = …)` writes it. Positional emission
  67. // hands `{ chunk }` a String, and the app dies with "Cannot destructure non-object
  68. // value" — a DIFFERENT error from the one 257 fixed, on the same line of his app.
  69. // `name = value` is the form that satisfies the typed declaration AND keeps
  70. // destructuring working. Do not "simplify" it back.
  71. on __fetchEvent(ev) {
  72. if (ev.chunk != null) {
  73. emit chunk(chunk = ev.chunk)
  74. return null
  75. }
  76. // the final frame — retire the source so the loop can end by itself
  77. __native("eventloop.unregister", #srcId)
  78. emit done(status = ev.status, ok = ev.ok, headers = ev.headers, error = ev.error, aborted = ev.aborted)
  79. return null
  80. }
  81. \* THE STOP BUTTON: abort this fetch — the socket is shut down, the peer
  82. sees the disconnect and stops generating, and `done` arrives with
  83. aborted = true. (fetchAbort(tag) cuts a whole tag at once.) *\
  84. abort() {
  85. return #handle.abort()
  86. }
  87. // Same wait as response(), with the native failure kept as an ordinary Hybrid.
  88. tryResponse() {
  89. bell = #handle.doneFd()
  90. while (!#handle.done()) {
  91. if (!__native("fiber.yield", bell)) { break }
  92. }
  93. r = #handle.tryResult()
  94. if (r.status == 0) { return r }
  95. return r > new FetchResponse()
  96. }

Branches

Latest commits

  • 10f8a8c8Merge branch 'code-order' into mainJukebox
  • 204fc828Merge branch 'day72' into mainJukebox
  • 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre