@theokit/agents 7.0.0 → 7.1.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.
@@ -1814,528 +1814,8 @@ function withGuardrails(handle, guardrails) {
1814
1814
  }
1815
1815
  __name(withGuardrails, "withGuardrails");
1816
1816
 
1817
- // ../presenter/dist/index.js
1818
- var __defProp = Object.defineProperty;
1819
- var __name2 = /* @__PURE__ */ __name((target, value) => __defProp(target, "name", {
1820
- value,
1821
- configurable: true
1822
- }), "__name");
1823
- var UnknownPresenterError = class extends Error {
1824
- static {
1825
- __name(this, "UnknownPresenterError");
1826
- }
1827
- static {
1828
- __name2(this, "UnknownPresenterError");
1829
- }
1830
- name = "UnknownPresenterError";
1831
- constructor(surface, known) {
1832
- super(`No presenter registered for surface "${surface}". Known: ${known.join(", ") || "(none)"}.`);
1833
- }
1834
- };
1835
- var PresenterRegistry = class {
1836
- static {
1837
- __name(this, "PresenterRegistry");
1838
- }
1839
- static {
1840
- __name2(this, "PresenterRegistry");
1841
- }
1842
- #presenters = /* @__PURE__ */ new Map();
1843
- /** Register (or replace) the presenter for its surface. Returns `this` for fluent wiring. */
1844
- register(presenter) {
1845
- this.#presenters.set(presenter.surface, presenter);
1846
- return this;
1847
- }
1848
- /** Whether a presenter is registered for `surface`. */
1849
- has(surface) {
1850
- return this.#presenters.has(surface);
1851
- }
1852
- /** The registered surface keys. */
1853
- surfaces() {
1854
- return [
1855
- ...this.#presenters.keys()
1856
- ];
1857
- }
1858
- /** Resolve the presenter for `surface`, or throw {@link UnknownPresenterError} (never returns undefined). */
1859
- resolve(surface) {
1860
- const p = this.#presenters.get(surface);
1861
- if (p === void 0) throw new UnknownPresenterError(surface, this.surfaces());
1862
- return p;
1863
- }
1864
- };
1865
- function asString2(value, fallback) {
1866
- return typeof value === "string" ? value : fallback;
1867
- }
1868
- __name(asString2, "asString");
1869
- __name2(asString2, "asString");
1870
- function serializeToolResult(value, fallback) {
1871
- if (typeof value === "string") return value;
1872
- if (value === void 0 || value === null) return fallback;
1873
- try {
1874
- return JSON.stringify(value);
1875
- } catch {
1876
- return typeof value === "bigint" ? value.toString() : fallback;
1877
- }
1878
- }
1879
- __name(serializeToolResult, "serializeToolResult");
1880
- __name2(serializeToolResult, "serializeToolResult");
1881
- function fromAssistant(msg) {
1882
- const events = [];
1883
- const content = msg.message?.content;
1884
- if (!Array.isArray(content)) return events;
1885
- for (const block of content) {
1886
- const b = block;
1887
- if (b.type === "text" && b.text) events.push({
1888
- type: "text",
1889
- text: b.text
1890
- });
1891
- if (b.type === "tool_use") {
1892
- events.push({
1893
- type: "tool-call",
1894
- callId: b.id ?? `tc-${Date.now()}`,
1895
- name: b.name ?? "unknown",
1896
- input: b.input ?? {}
1897
- });
1898
- }
1899
- }
1900
- return events;
1901
- }
1902
- __name(fromAssistant, "fromAssistant");
1903
- __name2(fromAssistant, "fromAssistant");
1904
- function fromToolUse(msg) {
1905
- const status = msg.status;
1906
- const callId = asString2(msg.call_id, `tc-${Date.now()}`);
1907
- const name = asString2(msg.name, "unknown");
1908
- if (status === "completed") {
1909
- return [
1910
- {
1911
- type: "tool-result",
1912
- callId,
1913
- name,
1914
- result: serializeToolResult(msg.result, ""),
1915
- isError: false
1916
- }
1917
- ];
1918
- }
1919
- if (status === "error") {
1920
- return [
1921
- {
1922
- type: "tool-result",
1923
- callId,
1924
- name,
1925
- result: serializeToolResult(msg.result, "Tool failed"),
1926
- isError: true
1927
- }
1928
- ];
1929
- }
1930
- if (status === "running") {
1931
- return [
1932
- {
1933
- type: "tool-call",
1934
- callId,
1935
- name,
1936
- input: msg.args ?? msg.input ?? msg.arguments ?? {}
1937
- }
1938
- ];
1939
- }
1940
- return [];
1941
- }
1942
- __name(fromToolUse, "fromToolUse");
1943
- __name2(fromToolUse, "fromToolUse");
1944
- function fromStatus(msg) {
1945
- const s = msg.status;
1946
- if (s === "FINISHED" || s === "CANCELLED") return [
1947
- {
1948
- type: "finish",
1949
- reason: s.toLowerCase()
1950
- }
1951
- ];
1952
- if (s === "ERROR" || s === "EXPIRED") {
1953
- return [
1954
- {
1955
- type: "error",
1956
- message: asString2(msg.message, "Agent error"),
1957
- code: "AGENT_ERROR"
1958
- }
1959
- ];
1960
- }
1961
- return [];
1962
- }
1963
- __name(fromStatus, "fromStatus");
1964
- __name2(fromStatus, "fromStatus");
1965
- function fromSdkMessage(msg) {
1966
- switch (msg.type) {
1967
- case "assistant":
1968
- return fromAssistant(msg);
1969
- case "tool_call":
1970
- return fromToolUse(msg);
1971
- case "thinking":
1972
- return [
1973
- {
1974
- type: "reasoning",
1975
- text: asString2(msg.text, "")
1976
- }
1977
- ];
1978
- case "status":
1979
- return fromStatus(msg);
1980
- // `system` / run-lifecycle is framework framing, not pure output — intentionally skipped (ADR-4).
1981
- default:
1982
- return [];
1983
- }
1984
- }
1985
- __name(fromSdkMessage, "fromSdkMessage");
1986
- __name2(fromSdkMessage, "fromSdkMessage");
1987
- function fromInteractionUpdate(update) {
1988
- switch (update.type) {
1989
- case "text-delta":
1990
- return update.text ? [
1991
- {
1992
- type: "text",
1993
- text: update.text
1994
- }
1995
- ] : [];
1996
- case "thinking-delta":
1997
- return update.text ? [
1998
- {
1999
- type: "reasoning",
2000
- text: update.text
2001
- }
2002
- ] : [];
2003
- case "tool-call-started":
2004
- return [
2005
- {
2006
- type: "tool-call",
2007
- callId: update.callId,
2008
- name: update.toolCall.name,
2009
- input: update.toolCall.args ?? {}
2010
- }
2011
- ];
2012
- case "partial-tool-call":
2013
- return [
2014
- {
2015
- type: "partial-tool-call",
2016
- callId: update.callId,
2017
- name: update.toolCall.name,
2018
- input: update.toolCall.args ?? {}
2019
- }
2020
- ];
2021
- case "tool-call-completed":
2022
- return [
2023
- {
2024
- type: "tool-result",
2025
- callId: update.callId,
2026
- name: update.toolCall.name,
2027
- result: serializeToolResult(update.toolCall.result, ""),
2028
- isError: false
2029
- }
2030
- ];
2031
- default:
2032
- return [];
2033
- }
2034
- }
2035
- __name(fromInteractionUpdate, "fromInteractionUpdate");
2036
- __name2(fromInteractionUpdate, "fromInteractionUpdate");
2037
- var UIMessageStreamPresenter = class {
2038
- static {
2039
- __name(this, "UIMessageStreamPresenter");
2040
- }
2041
- static {
2042
- __name2(this, "UIMessageStreamPresenter");
2043
- }
2044
- surface = "ui-message-stream";
2045
- #textId;
2046
- #openBlock = null;
2047
- #reasoningId = null;
2048
- #seen = /* @__PURE__ */ new Set();
2049
- constructor(options) {
2050
- this.#textId = options.textId;
2051
- }
2052
- /** Emit the opening `start` chunk (exactly once, before any event). */
2053
- start() {
2054
- return [
2055
- {
2056
- type: "start"
2057
- }
2058
- ];
2059
- }
2060
- present(event) {
2061
- switch (event.type) {
2062
- case "text":
2063
- return this.#emitTextDelta(event.text);
2064
- case "reasoning":
2065
- return this.#emitReasoningDelta(event.text);
2066
- case "tool-call":
2067
- return [
2068
- ...this.#closeOpenBlock(),
2069
- ...this.#emitToolCall(event.callId, event.name, event.input)
2070
- ];
2071
- case "tool-result":
2072
- return [
2073
- ...this.#closeOpenBlock(),
2074
- ...this.#emitToolResult(event.callId, event.name, event.result, event.isError ?? false)
2075
- ];
2076
- case "error":
2077
- return [
2078
- {
2079
- type: "error",
2080
- errorText: event.message
2081
- }
2082
- ];
2083
- // `partial-tool-call` streams incremental args — the current web path emits no chunk for it
2084
- // (args are shown on the committed `tool-call`); `finish` / `status` are handled by finish()/host.
2085
- default:
2086
- return [];
2087
- }
2088
- }
2089
- /**
2090
- * Close any open text/reasoning block and emit the terminal `finish` chunk. When `metadata` is given
2091
- * (a clean run's turn totals from the framework `done`), it rides `messageMetadata`; otherwise the
2092
- * finish is bare (error/abort turns), byte-identical to the original translator.
2093
- */
2094
- finish(metadata) {
2095
- const out = this.#closeOpenBlock();
2096
- out.push(metadata ? {
2097
- type: "finish",
2098
- messageMetadata: metadata
2099
- } : {
2100
- type: "finish"
2101
- });
2102
- return out;
2103
- }
2104
- /**
2105
- * Close any open text/reasoning block WITHOUT finishing the stream. The host calls this before
2106
- * interleaving a framework chunk (HITL approval / checkpoint) that must not appear inside an open text
2107
- * block — mirroring the original translator's `closeOpenBlock` before those chunks. Idempotent.
2108
- */
2109
- closeBlock() {
2110
- return this.#closeOpenBlock();
2111
- }
2112
- /**
2113
- * Whether a `callId` has already been introduced (a `tool-call` or a synthesized `tool-input`). The host
2114
- * uses this so a framework `approval_required` reuses the EC-1 synthesize-input-first rule exactly once.
2115
- */
2116
- hasSeen(callId) {
2117
- return this.#seen.has(callId);
2118
- }
2119
- /** Mark a `callId` as introduced (the host synthesized its `tool-input` for a framework chunk). */
2120
- markSeen(callId) {
2121
- this.#seen.add(callId);
2122
- }
2123
- // --- internal open-block state machine (verbatim from the original translator) ---
2124
- #closeOpenBlock() {
2125
- const out = [];
2126
- if (this.#openBlock === "text") {
2127
- out.push({
2128
- type: "text-end",
2129
- id: this.#textId
2130
- });
2131
- } else if (this.#openBlock === "reasoning" && this.#reasoningId) {
2132
- out.push({
2133
- type: "reasoning-end",
2134
- id: this.#reasoningId
2135
- });
2136
- }
2137
- this.#openBlock = null;
2138
- this.#reasoningId = null;
2139
- return out;
2140
- }
2141
- #emitTextDelta(content) {
2142
- const out = [];
2143
- if (this.#openBlock !== "text") {
2144
- out.push(...this.#closeOpenBlock(), {
2145
- type: "text-start",
2146
- id: this.#textId
2147
- });
2148
- this.#openBlock = "text";
2149
- }
2150
- out.push({
2151
- type: "text-delta",
2152
- id: this.#textId,
2153
- delta: content
2154
- });
2155
- return out;
2156
- }
2157
- #emitReasoningDelta(content) {
2158
- const out = [];
2159
- let reasoningId = this.#openBlock === "reasoning" ? this.#reasoningId : null;
2160
- if (reasoningId === null) {
2161
- out.push(...this.#closeOpenBlock());
2162
- reasoningId = crypto.randomUUID();
2163
- this.#reasoningId = reasoningId;
2164
- out.push({
2165
- type: "reasoning-start",
2166
- id: reasoningId
2167
- });
2168
- this.#openBlock = "reasoning";
2169
- }
2170
- out.push({
2171
- type: "reasoning-delta",
2172
- id: reasoningId,
2173
- delta: content
2174
- });
2175
- return out;
2176
- }
2177
- #emitToolCall(callId, name, input) {
2178
- this.#seen.add(callId);
2179
- return [
2180
- {
2181
- type: "tool-input-available",
2182
- toolCallId: callId,
2183
- toolName: name,
2184
- input,
2185
- dynamic: true
2186
- }
2187
- ];
2188
- }
2189
- #emitToolResult(callId, name, result, isError2) {
2190
- const out = [];
2191
- if (!this.#seen.has(callId)) {
2192
- this.#seen.add(callId);
2193
- out.push({
2194
- type: "tool-input-available",
2195
- toolCallId: callId,
2196
- toolName: name,
2197
- input: {},
2198
- dynamic: true
2199
- });
2200
- }
2201
- const output = typeof result === "string" ? result : "";
2202
- if (isError2) {
2203
- out.push({
2204
- type: "tool-output-error",
2205
- toolCallId: callId,
2206
- errorText: output
2207
- });
2208
- } else {
2209
- out.push({
2210
- type: "tool-output-available",
2211
- toolCallId: callId,
2212
- output
2213
- });
2214
- }
2215
- return out;
2216
- }
2217
- };
2218
- var ANSI = {
2219
- text: "",
2220
- reasoning: "\x1B[2m",
2221
- tool: "\x1B[36m",
2222
- "tool-result": "\x1B[2m",
2223
- "tool-error": "\x1B[31m",
2224
- error: "\x1B[31m",
2225
- status: "\x1B[35m",
2226
- finish: "\x1B[2m"
2227
- };
2228
- var RESET = "\x1B[0m";
2229
- function preview(value, max) {
2230
- const raw = typeof value === "string" ? value : safeJson(value);
2231
- const flat = raw.replace(/\s+/g, " ").trim();
2232
- return flat.length > max ? `${flat.slice(0, max - 1)}\u2026` : flat;
2233
- }
2234
- __name(preview, "preview");
2235
- __name2(preview, "preview");
2236
- function safeJson(value) {
2237
- if (value === void 0 || value === null) return "";
2238
- try {
2239
- return JSON.stringify(value);
2240
- } catch {
2241
- return typeof value === "bigint" ? value.toString() : "";
2242
- }
2243
- }
2244
- __name(safeJson, "safeJson");
2245
- __name2(safeJson, "safeJson");
2246
- function suffix(open, value, close) {
2247
- return value === void 0 ? "" : open + value + close;
2248
- }
2249
- __name(suffix, "suffix");
2250
- __name2(suffix, "suffix");
2251
- var TerminalPresenter = class {
2252
- static {
2253
- __name(this, "TerminalPresenter");
2254
- }
2255
- static {
2256
- __name2(this, "TerminalPresenter");
2257
- }
2258
- surface = "terminal";
2259
- #ansi;
2260
- #max;
2261
- constructor(options = {}) {
2262
- this.#ansi = options.ansi ?? false;
2263
- this.#max = options.maxPreview ?? 88;
2264
- }
2265
- present(event) {
2266
- switch (event.type) {
2267
- case "text":
2268
- return event.text.length > 0 ? [
2269
- this.#row("text", event.text)
2270
- ] : [];
2271
- case "reasoning":
2272
- return event.text.length > 0 ? [
2273
- this.#row("reasoning", `\xB7 ${event.text}`)
2274
- ] : [];
2275
- case "tool-call":
2276
- return [
2277
- this.#row("tool", `\u23FA ${event.name}(${preview(event.input, this.#max)})`)
2278
- ];
2279
- case "tool-result":
2280
- return [
2281
- this.#toolResult(event.isError === true, preview(event.result, this.#max))
2282
- ];
2283
- case "error":
2284
- return [
2285
- this.#row("error", `\u2716 ${event.message}${suffix(" (", event.code, ")")}`)
2286
- ];
2287
- case "status":
2288
- return [
2289
- this.#row("status", `\u25CF ${event.status}${suffix(" \u2014 ", event.detail, "")}`)
2290
- ];
2291
- case "finish":
2292
- return [
2293
- this.#row("finish", this.#finishText(event.reason, event.usage?.totalTokens))
2294
- ];
2295
- // `partial-tool-call` streams incremental args — the committed `tool-call` renders them.
2296
- default:
2297
- return [];
2298
- }
2299
- }
2300
- #toolResult(isError2, body) {
2301
- return this.#row(isError2 ? "tool-error" : "tool-result", ` \u23BF ${body}`);
2302
- }
2303
- #finishText(reason, tokens) {
2304
- const r = suffix(" ", reason, "");
2305
- const t = tokens === void 0 ? "" : ` \xB7 ${tokens} tokens`;
2306
- return `<<${r}${t} >>`;
2307
- }
2308
- #row(kind, text) {
2309
- return {
2310
- kind,
2311
- text: this.#ansi && ANSI[kind] !== "" ? `${ANSI[kind]}${text}${RESET}` : text
2312
- };
2313
- }
2314
- };
2315
- var JsonPresenter = class {
2316
- static {
2317
- __name(this, "JsonPresenter");
2318
- }
2319
- static {
2320
- __name2(this, "JsonPresenter");
2321
- }
2322
- surface = "json";
2323
- #ns;
2324
- constructor(options = {}) {
2325
- this.#ns = options.namespace ?? "agent.";
2326
- }
2327
- present(event) {
2328
- const { type, ...payload } = event;
2329
- return [
2330
- {
2331
- type: `${this.#ns}${type}`,
2332
- ...payload
2333
- }
2334
- ];
2335
- }
2336
- };
2337
-
2338
1817
  // src/bridge/present-ui-message-stream.ts
1818
+ import { UIMessageStreamPresenter } from "@theokit/presenter";
2339
1819
  function toAgentOutputEvent(e) {
2340
1820
  switch (e.type) {
2341
1821
  case "text_delta":
@@ -2872,10 +2352,10 @@ var DelegationError = class extends Error {
2872
2352
  };
2873
2353
 
2874
2354
  // src/loop/run-reflective-loop.ts
2875
- function asString3(value, fallback) {
2355
+ function asString2(value, fallback) {
2876
2356
  return typeof value === "string" ? value : fallback;
2877
2357
  }
2878
- __name(asString3, "asString");
2358
+ __name(asString2, "asString");
2879
2359
  function asNumber(value, fallback) {
2880
2360
  return typeof value === "number" ? value : fallback;
2881
2361
  }
@@ -2951,15 +2431,15 @@ function deriveFinishReason(signals) {
2951
2431
  }
2952
2432
  __name(deriveFinishReason, "deriveFinishReason");
2953
2433
  function pushToolResult(event, r, callInputs) {
2954
- const id = asString3(event.callId, "");
2434
+ const id = asString2(event.callId, "");
2955
2435
  const call = callInputs.get(id);
2956
2436
  r.toolCalls.push({
2957
2437
  id,
2958
- name: call?.name ?? asString3(event.toolName, "unknown"),
2438
+ name: call?.name ?? asString2(event.toolName, "unknown"),
2959
2439
  // Prefer the correlated tool_call input (real SDK shape); fall back to an input on the
2960
2440
  // result event itself (some streams/tests carry it there), else {}.
2961
2441
  input: call?.input ?? event.input ?? {},
2962
- output: asString3(event.output, "")
2442
+ output: asString2(event.output, "")
2963
2443
  });
2964
2444
  }
2965
2445
  __name(pushToolResult, "pushToolResult");
@@ -2978,8 +2458,8 @@ function accumulateEvent(event, r, signals, callInputs) {
2978
2458
  if (event.type === "text_delta" && typeof event.content === "string") {
2979
2459
  r.responseText += event.content;
2980
2460
  } else if (event.type === "tool_call") {
2981
- callInputs.set(asString3(event.callId, ""), {
2982
- name: asString3(event.toolName, "unknown"),
2461
+ callInputs.set(asString2(event.callId, ""), {
2462
+ name: asString2(event.toolName, "unknown"),
2983
2463
  input: event.input ?? {}
2984
2464
  });
2985
2465
  } else if (event.type === "tool_result") {
@@ -2987,11 +2467,11 @@ function accumulateEvent(event, r, signals, callInputs) {
2987
2467
  pushToolResult(event, r, callInputs);
2988
2468
  } else if (event.type === "done") {
2989
2469
  signals.sawDone = true;
2990
- signals.doneFinishReason = asString3(event.finishReason, "");
2470
+ signals.doneFinishReason = asString2(event.finishReason, "");
2991
2471
  applyDone(event, r);
2992
2472
  } else if (event.type === "error") {
2993
2473
  signals.sawError = true;
2994
- r.errorMessage = asString3(event.message, "Unknown agent error");
2474
+ r.errorMessage = asString2(event.message, "Unknown agent error");
2995
2475
  }
2996
2476
  }
2997
2477
  __name(accumulateEvent, "accumulateEvent");
@@ -3893,4 +3373,4 @@ export {
3893
3373
  generateAgentManifest,
3894
3374
  agentsPlugin
3895
3375
  };
3896
- //# sourceMappingURL=chunk-3YPKTOJ6.js.map
3376
+ //# sourceMappingURL=chunk-LJLNFCLS.js.map