@effect-uai/core 0.8.0 → 0.10.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.
- package/dist/{AiError-CAX_48RU.d.mts → AiError-Bf1pq4BB.d.mts} +9 -4
- package/dist/{AiError-CAX_48RU.d.mts.map → AiError-Bf1pq4BB.d.mts.map} +1 -1
- package/dist/{Audio-DPlTsNdz.d.mts → Audio-CR7CSljJ.d.mts} +1 -1
- package/dist/{Audio-DPlTsNdz.d.mts.map → Audio-CR7CSljJ.d.mts.map} +1 -1
- package/dist/{Image-HNmMpMTh.d.mts → Image-BZmKfIdq.d.mts} +1 -1
- package/dist/{Image-HNmMpMTh.d.mts.map → Image-BZmKfIdq.d.mts.map} +1 -1
- package/dist/{Items-7ljaVxH_.d.mts → Items-BnIduEV2.d.mts} +7 -7
- package/dist/{Items-7ljaVxH_.d.mts.map → Items-BnIduEV2.d.mts.map} +1 -1
- package/dist/Loop-D8nWpowc.d.mts +173 -0
- package/dist/Loop-D8nWpowc.d.mts.map +1 -0
- package/dist/{SandboxError-BvKypkJm.d.mts → SandboxError-CIrVDOMV.d.mts} +2 -2
- package/dist/{SandboxError-BvKypkJm.d.mts.map → SandboxError-CIrVDOMV.d.mts.map} +1 -1
- package/dist/{StructuredFormat-BbN4dosH.d.mts → StructuredFormat-DHaUJBdd.d.mts} +2 -2
- package/dist/{StructuredFormat-BbN4dosH.d.mts.map → StructuredFormat-DHaUJBdd.d.mts.map} +1 -1
- package/dist/Tool-2lC_XMth.d.mts +284 -0
- package/dist/Tool-2lC_XMth.d.mts.map +1 -0
- package/dist/ToolEvent-cWZ2BnbS.d.mts +151 -0
- package/dist/ToolEvent-cWZ2BnbS.d.mts.map +1 -0
- package/dist/ToolResult-CL4hK_9z.d.mts +157 -0
- package/dist/ToolResult-CL4hK_9z.d.mts.map +1 -0
- package/dist/Toolkit-Dr9l_hNy.d.mts +196 -0
- package/dist/Toolkit-Dr9l_hNy.d.mts.map +1 -0
- package/dist/browser/Browser.d.mts +190 -0
- package/dist/browser/Browser.d.mts.map +1 -0
- package/dist/browser/Browser.mjs +34 -0
- package/dist/browser/Browser.mjs.map +1 -0
- package/dist/browser/BrowserError.d.mts +141 -0
- package/dist/browser/BrowserError.d.mts.map +1 -0
- package/dist/browser/BrowserError.mjs +89 -0
- package/dist/browser/BrowserError.mjs.map +1 -0
- package/dist/browser/BrowserTool.d.mts +58 -0
- package/dist/browser/BrowserTool.d.mts.map +1 -0
- package/dist/browser/BrowserTool.mjs +86 -0
- package/dist/browser/BrowserTool.mjs.map +1 -0
- package/dist/{dist-DV5ISja1.mjs → dist-B04dlFHg.mjs} +3 -3
- package/dist/{dist-DV5ISja1.mjs.map → dist-B04dlFHg.mjs.map} +1 -1
- package/dist/dist-DWgRbDN3.mjs +140 -0
- package/dist/dist-DWgRbDN3.mjs.map +1 -0
- package/dist/domain/AiError.d.mts +2 -2
- package/dist/domain/AiError.mjs +22 -3
- package/dist/domain/AiError.mjs.map +1 -1
- package/dist/domain/Audio.d.mts +1 -1
- package/dist/domain/Image.d.mts +1 -1
- package/dist/domain/Items.d.mts +1 -1
- package/dist/domain/Music.d.mts +1 -1
- package/dist/domain/Turn.d.mts +4 -4
- package/dist/domain/Turn.test.mjs +1 -1
- package/dist/embedding-model/Embedding.d.mts +1 -1
- package/dist/embedding-model/EmbeddingModel.d.mts +1 -1
- package/dist/embedding-model/EmbeddingModel.test.mjs +1 -1
- package/dist/embedding-model/EmbeddingModel.test.mjs.map +1 -1
- package/dist/index.d.mts +19 -13
- package/dist/index.mjs +15 -9
- package/dist/language-model/LanguageModel.d.mts +10 -5
- package/dist/language-model/LanguageModel.d.mts.map +1 -1
- package/dist/language-model/LanguageModel.mjs.map +1 -1
- package/dist/language-model/LanguageModel.test.mjs +1 -1
- package/dist/loop/Loop.d.mts +2 -141
- package/dist/loop/Loop.mjs +16 -20
- package/dist/loop/Loop.mjs.map +1 -1
- package/dist/loop/Loop.test.mjs +18 -1
- package/dist/loop/Loop.test.mjs.map +1 -1
- package/dist/{magic-string.es-BgIV5Mu3.mjs → magic-string.es-Dk-MMA0F.mjs} +1 -1
- package/dist/{magic-string.es-BgIV5Mu3.mjs.map → magic-string.es-Dk-MMA0F.mjs.map} +1 -1
- package/dist/music-generator/MusicGenerator.d.mts +2 -2
- package/dist/music-generator/MusicGenerator.test.mjs +1 -1
- package/dist/observability/Metrics.d.mts +116 -28
- package/dist/observability/Metrics.d.mts.map +1 -1
- package/dist/observability/Metrics.mjs +250 -38
- package/dist/observability/Metrics.mjs.map +1 -1
- package/dist/observability/Metrics.test.d.mts +1 -0
- package/dist/observability/Metrics.test.mjs +187 -0
- package/dist/observability/Metrics.test.mjs.map +1 -0
- package/dist/observability/Telemetry.d.mts +37 -0
- package/dist/observability/Telemetry.d.mts.map +1 -0
- package/dist/observability/Telemetry.mjs +57 -0
- package/dist/observability/Telemetry.mjs.map +1 -0
- package/dist/observability/Telemetry.test.d.mts +1 -0
- package/dist/observability/Telemetry.test.mjs +92 -0
- package/dist/observability/Telemetry.test.mjs.map +1 -0
- package/dist/retry/Retry.d.mts +2 -2
- package/dist/retry/Retry.test.mjs +1 -1
- package/dist/sandbox/Sandbox.d.mts +1 -1
- package/dist/sandbox/SandboxError.d.mts +1 -1
- package/dist/speech-synthesizer/SpeechSynthesizer.d.mts +2 -2
- package/dist/speech-synthesizer/SpeechSynthesizer.test.mjs +1 -1
- package/dist/streaming/JSONL.d.mts +1 -1
- package/dist/streaming/JSONL.test.mjs +1 -1
- package/dist/streaming/SSE.test.mjs +1 -1
- package/dist/structured-format/StructuredFormat.d.mts +1 -1
- package/dist/structured-format/StructuredFormat.mjs.map +1 -1
- package/dist/structured-format/StructuredFormat.test.mjs +1 -1
- package/dist/testing/MockMusicGenerator.d.mts +1 -1
- package/dist/testing/MockProvider.d.mts +1 -1
- package/dist/testing/MockSpeechSynthesizer.d.mts +1 -1
- package/dist/tool/Approval.d.mts +19 -7
- package/dist/tool/Approval.d.mts.map +1 -1
- package/dist/tool/Approval.mjs +18 -11
- package/dist/tool/Approval.mjs.map +1 -1
- package/dist/tool/Approval.test.mjs +80 -40
- package/dist/tool/Approval.test.mjs.map +1 -1
- package/dist/tool/HistoryCheck.d.mts +2 -2
- package/dist/tool/Tool.d.mts +2 -2
- package/dist/tool/Tool.mjs +190 -36
- package/dist/tool/Tool.mjs.map +1 -1
- package/dist/tool/Tool.test.mjs +41 -3
- package/dist/tool/Tool.test.mjs.map +1 -1
- package/dist/tool/ToolEvent.d.mts +2 -151
- package/dist/tool/ToolEvent.mjs +5 -0
- package/dist/tool/ToolEvent.mjs.map +1 -1
- package/dist/tool/ToolResult.d.mts +2 -138
- package/dist/tool/ToolResult.mjs +32 -8
- package/dist/tool/ToolResult.mjs.map +1 -1
- package/dist/tool/Toolkit.d.mts +2 -73
- package/dist/tool/Toolkit.mjs +153 -35
- package/dist/tool/Toolkit.mjs.map +1 -1
- package/dist/tool/Toolkit.test.mjs +271 -11
- package/dist/tool/Toolkit.test.mjs.map +1 -1
- package/dist/transcriber/Transcriber.d.mts +2 -2
- package/dist/transcriber/Transcriber.test.mjs +1 -1
- package/dist/web-read/WebRead.d.mts +44 -0
- package/dist/web-read/WebRead.d.mts.map +1 -0
- package/dist/web-read/WebRead.mjs +18 -0
- package/dist/web-read/WebRead.mjs.map +1 -0
- package/dist/web-read/WebReadTool.d.mts +57 -0
- package/dist/web-read/WebReadTool.d.mts.map +1 -0
- package/dist/web-read/WebReadTool.mjs +59 -0
- package/dist/web-read/WebReadTool.mjs.map +1 -0
- package/dist/web-search/WebSearch.d.mts +1 -1
- package/dist/web-search/WebSearchTool.d.mts +12 -6
- package/dist/web-search/WebSearchTool.d.mts.map +1 -1
- package/dist/web-search/WebSearchTool.mjs +10 -5
- package/dist/web-search/WebSearchTool.mjs.map +1 -1
- package/package.json +21 -1
- package/src/browser/Browser.ts +268 -0
- package/src/browser/BrowserError.ts +145 -0
- package/src/browser/BrowserTool.ts +161 -0
- package/src/domain/AiError.ts +27 -1
- package/src/embedding-model/EmbeddingModel.test.ts +4 -0
- package/src/index.ts +6 -0
- package/src/language-model/LanguageModel.ts +7 -2
- package/src/loop/Loop.test.ts +22 -0
- package/src/loop/Loop.ts +27 -4
- package/src/observability/Metrics.test.ts +266 -0
- package/src/observability/Metrics.ts +505 -65
- package/src/observability/Telemetry.test.ts +123 -0
- package/src/observability/Telemetry.ts +96 -0
- package/src/structured-format/StructuredFormat.ts +4 -0
- package/src/tool/Approval.test.ts +77 -26
- package/src/tool/Approval.ts +72 -27
- package/src/tool/Tool.test.ts +55 -2
- package/src/tool/Tool.ts +335 -70
- package/src/tool/ToolEvent.ts +5 -0
- package/src/tool/ToolResult.ts +36 -9
- package/src/tool/Toolkit.test.ts +339 -9
- package/src/tool/Toolkit.ts +470 -120
- package/src/web-read/WebRead.ts +70 -0
- package/src/web-read/WebReadTool.ts +96 -0
- package/src/web-search/WebSearchTool.ts +11 -5
- package/dist/Tool-BKiieFXk.d.mts +0 -102
- package/dist/Tool-BKiieFXk.d.mts.map +0 -1
- package/dist/loop/Loop.d.mts.map +0 -1
- package/dist/tool/ToolEvent.d.mts.map +0 -1
- package/dist/tool/ToolResult.d.mts.map +0 -1
- package/dist/tool/Toolkit.d.mts.map +0 -1
package/dist/tool/Toolkit.d.mts
CHANGED
|
@@ -1,73 +1,2 @@
|
|
|
1
|
-
import { o as
|
|
2
|
-
|
|
3
|
-
import { a as Tool, i as StreamingTool, r as AnyTool } from "../Tool-BKiieFXk.mjs";
|
|
4
|
-
import { Step } from "../loop/Loop.mjs";
|
|
5
|
-
import { ToolResult } from "./ToolResult.mjs";
|
|
6
|
-
import { ToolEvent } from "./ToolEvent.mjs";
|
|
7
|
-
import { Effect, Stream } from "effect";
|
|
8
|
-
|
|
9
|
-
//#region src/tool/Toolkit.d.ts
|
|
10
|
-
declare namespace Toolkit_d_exports {
|
|
11
|
-
export { ExecuteOptions, ToolKindR, appendToolResults, collectResults, continueWithResults, run };
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Union of every tool's `R` requirements in a mixed plain + streaming array.
|
|
15
|
-
* Used by `run` to surface the services tools need at the recipe level, so
|
|
16
|
-
* the loop's stream type carries them through to `Effect.provide`.
|
|
17
|
-
*/
|
|
18
|
-
type ToolKindR<Tools extends ReadonlyArray<AnyTool<any>>> = Tools[number] extends StreamingTool<any, any, any, any, infer R> ? R : Tools[number] extends Tool<any, any, any, infer R> ? R : never;
|
|
19
|
-
type ExecuteOptions = {
|
|
20
|
-
readonly concurrency?: number | "unbounded";
|
|
21
|
-
};
|
|
22
|
-
/** Execute every provided call. Approval/rejection policy belongs upstream. */
|
|
23
|
-
declare const run: <Tools extends ReadonlyArray<AnyTool<any>>>(tools: Tools, calls: ReadonlyArray<ToolCall>, options?: ExecuteOptions) => Stream.Stream<ToolEvent, never, ToolKindR<Tools>>;
|
|
24
|
-
/**
|
|
25
|
-
* Append a completed turn plus tool results to a state's history, converting
|
|
26
|
-
* the results to wire-format `ToolCallOutput`s. Curried so it slots directly
|
|
27
|
-
* into `continueWithResults`:
|
|
28
|
-
*
|
|
29
|
-
* Toolkit.run(tools, calls).pipe(
|
|
30
|
-
* Toolkit.continueWithResults(Toolkit.appendToolResults(state, turn)),
|
|
31
|
-
* )
|
|
32
|
-
*
|
|
33
|
-
* Equivalent to `appendToHistory(state, turn, results.map(toToolCallOutput))`
|
|
34
|
-
* — the helper just hides the wire-conversion step.
|
|
35
|
-
*/
|
|
36
|
-
declare const appendToolResults: <S extends {
|
|
37
|
-
readonly history: ReadonlyArray<HistoryItem>;
|
|
38
|
-
}>(state: S, turn: Turn) => (results: ReadonlyArray<ToolResult>) => S;
|
|
39
|
-
/**
|
|
40
|
-
* Drain a `Stream<ToolEvent>` and return the accumulated `ToolResult`s
|
|
41
|
-
* from every `Output` event. One-shot — the type is `Effect<results>`,
|
|
42
|
-
* not a one-element stream — so it composes naturally with `Effect.map`
|
|
43
|
-
* to build state.
|
|
44
|
-
*
|
|
45
|
-
* Counterpart to `continueWithResults`: `continueWithResults` bundles
|
|
46
|
-
* drain + emit Next into one bridge; this one is the right arm of a
|
|
47
|
-
* broadcast/fork-and-merge for callers that want to vary the left arm
|
|
48
|
-
* (e.g., filter/tap ToolEvents before forwarding).
|
|
49
|
-
*/
|
|
50
|
-
declare const collectResults: <E, R>(stream: Stream.Stream<ToolEvent, E, R>) => Effect.Effect<ReadonlyArray<ToolResult>, E, R>;
|
|
51
|
-
/**
|
|
52
|
-
* Bridge from a `Stream<ToolEvent>` to the loop's emit shape. Forwards
|
|
53
|
-
* every ToolEvent downstream as a `Loop.value` and at end-of-stream emits
|
|
54
|
-
* one `Loop.next(build(results))` carrying the accumulated `ToolResult`s
|
|
55
|
-
* from terminal `Output` events.
|
|
56
|
-
*
|
|
57
|
-
* Conceptually a broadcast fork (one arm passes events through, the other
|
|
58
|
-
* drains them into a state). Implemented as a single-pass Ref tap + concat
|
|
59
|
-
* for zero buffering — the broadcast version is observationally equivalent
|
|
60
|
-
* but holds events in a PubSub until both arms drain. The public primitives
|
|
61
|
-
* (`Loop.value`, `collectResults`, `Loop.next`) compose the same pattern
|
|
62
|
-
* when you need to vary an arm.
|
|
63
|
-
*
|
|
64
|
-
* Dual: data-first `continueWithResults(stream, build)` and data-last
|
|
65
|
-
* `stream.pipe(continueWithResults(build))` both work.
|
|
66
|
-
*/
|
|
67
|
-
declare const continueWithResults: {
|
|
68
|
-
<S>(build: (results: ReadonlyArray<ToolResult>) => S): <R>(stream: Stream.Stream<ToolEvent, never, R>) => Stream.Stream<Step<ToolEvent, S>, never, R>;
|
|
69
|
-
<S, R>(stream: Stream.Stream<ToolEvent, never, R>, build: (results: ReadonlyArray<ToolResult>) => S): Stream.Stream<Step<ToolEvent, S>, never, R>;
|
|
70
|
-
};
|
|
71
|
-
//#endregion
|
|
72
|
-
export { ExecuteOptions, ToolKindR, appendToolResults, collectResults, continueWithResults, run, Toolkit_d_exports as t };
|
|
73
|
-
//# sourceMappingURL=Toolkit.d.mts.map
|
|
1
|
+
import { S as wrap, _ as fromArray, a as ToolMap, b as namespace, c as ToolkitR, d as appendToolResults, f as collectResults, g as descriptors, h as describeFailures, i as NoDuplicateNames, m as continueWithResults, n as ExecuteOptions, o as Toolkit, p as compose, r as Middleware, s as ToolkitE, t as DuplicateToolName, u as UniqueTools, v as make, x as run, y as makeNamespaced } from "../Toolkit-Dr9l_hNy.mjs";
|
|
2
|
+
export { DuplicateToolName, ExecuteOptions, Middleware, NoDuplicateNames, ToolMap, Toolkit, ToolkitE, ToolkitR, UniqueTools, appendToolResults, collectResults, compose, continueWithResults, describeFailures, descriptors, fromArray, make, makeNamespaced, namespace, run, wrap };
|
package/dist/tool/Toolkit.mjs
CHANGED
|
@@ -1,63 +1,181 @@
|
|
|
1
1
|
import { n as __exportAll } from "../chunk-uyGKjUfl.mjs";
|
|
2
|
+
import { ToolResult, failed, nonLocalTool, toToolCallOutput, validationError } from "./ToolResult.mjs";
|
|
3
|
+
import { ToolFailed, decodeCallInput, toDescriptors } from "./Tool.mjs";
|
|
2
4
|
import { appendToHistory } from "../domain/Turn.mjs";
|
|
3
|
-
import { isStreamingTool } from "./Tool.mjs";
|
|
4
5
|
import { next, value } from "../loop/Loop.mjs";
|
|
5
|
-
import { ToolResult, executionError, failed, toToolCallOutput } from "./ToolResult.mjs";
|
|
6
6
|
import { ToolEvent, isOutput } from "./ToolEvent.mjs";
|
|
7
|
-
import { Array, Effect, Function,
|
|
7
|
+
import { Array, Cause, Data, Effect, Fiber, Function, Match, Option, Queue, Record, Ref, Stream } from "effect";
|
|
8
8
|
//#region src/tool/Toolkit.ts
|
|
9
9
|
var Toolkit_exports = /* @__PURE__ */ __exportAll({
|
|
10
|
+
DuplicateToolName: () => DuplicateToolName,
|
|
10
11
|
appendToolResults: () => appendToolResults,
|
|
11
12
|
collectResults: () => collectResults,
|
|
13
|
+
compose: () => compose,
|
|
12
14
|
continueWithResults: () => continueWithResults,
|
|
13
|
-
|
|
15
|
+
describeFailures: () => describeFailures,
|
|
16
|
+
descriptors: () => descriptors,
|
|
17
|
+
fromArray: () => fromArray,
|
|
18
|
+
make: () => make,
|
|
19
|
+
makeNamespaced: () => makeNamespaced,
|
|
20
|
+
namespace: () => namespace,
|
|
21
|
+
run: () => run,
|
|
22
|
+
wrap: () => wrap
|
|
14
23
|
});
|
|
15
|
-
|
|
16
|
-
|
|
24
|
+
const indexByName = (tools) => Object.fromEntries(tools.map((tool) => [tool.name, tool]));
|
|
25
|
+
/**
|
|
26
|
+
* Index literal tools by their `name`. Duplicate names are a compile error
|
|
27
|
+
* (`UniqueTools`). Names are not otherwise checked: a malformed name is a
|
|
28
|
+
* developer typo in a literal that the provider rejects clearly on first use,
|
|
29
|
+
* and there is no single cross-provider name rule to enforce here anyway.
|
|
30
|
+
*/
|
|
31
|
+
const buildStatic = (tools) => indexByName(tools);
|
|
32
|
+
const make = (...tools) => buildStatic(tools);
|
|
33
|
+
/**
|
|
34
|
+
* Build a toolkit from a dynamically-sourced array (MCP server, plugin). Names
|
|
35
|
+
* are not validated (MCP legitimately uses dots / >64 chars — sanitize at the
|
|
36
|
+
* provider adapter); same-named tools resolve last-wins. Cross-source clashes
|
|
37
|
+
* are caught by `compose`.
|
|
38
|
+
*/
|
|
39
|
+
const fromArray = (tools) => indexByName(tools);
|
|
40
|
+
/** Render the provider-facing descriptors for a toolkit on demand. */
|
|
41
|
+
const descriptors = (toolkit) => toDescriptors(Object.values(toolkit));
|
|
42
|
+
/**
|
|
43
|
+
* Two composed toolkits resolved to the same final name. Carries the colliding
|
|
44
|
+
* `sources` (the positions of the clashing toolkits) so the error says *which*
|
|
45
|
+
* inputs collided. Raised at runtime by `compose` for dynamic sources; static
|
|
46
|
+
* collisions are caught earlier at compile time.
|
|
47
|
+
*/
|
|
48
|
+
var DuplicateToolName = class extends Data.TaggedError("DuplicateToolName") {};
|
|
49
|
+
/**
|
|
50
|
+
* Prefix every tool name with `<namespace>__`, returning a new toolkit. Use it
|
|
51
|
+
* to keep generic names (`search`) from two sources distinct before `compose`.
|
|
52
|
+
* If two namespaces collide at the `__` separator, the duplicate final name
|
|
53
|
+
* surfaces from `compose` as a `DuplicateToolName`.
|
|
54
|
+
*/
|
|
55
|
+
const namespace = (prefix, toolkit) => {
|
|
56
|
+
return indexByName(Array.map(Record.values(toolkit), (tool) => ({
|
|
57
|
+
...tool,
|
|
58
|
+
name: `${prefix}__${tool.name}`
|
|
59
|
+
})));
|
|
60
|
+
};
|
|
61
|
+
/** `namespace(prefix, make(...tools))` in one step, for static namespaced tools. */
|
|
62
|
+
const makeNamespaced = (prefix, ...tools) => namespace(prefix, buildStatic(tools));
|
|
63
|
+
/**
|
|
64
|
+
* Combine independently-built toolkits into one. This is where names from
|
|
65
|
+
* separate sources can collide, so it is the one effectful constructor: a
|
|
66
|
+
* duplicate final name fails with `DuplicateToolName` (naming the colliding
|
|
67
|
+
* input positions) instead of silently overwriting or 400-ing later at the
|
|
68
|
+
* provider. Statically-known collisions are additionally a compile error.
|
|
69
|
+
*/
|
|
70
|
+
const compose = (...toolkits) => Effect.gen(function* () {
|
|
71
|
+
const kits = toolkits;
|
|
72
|
+
const labelled = Array.flatMap(kits, (kit, i) => Array.map(Record.keys(kit), (name) => ({
|
|
73
|
+
name,
|
|
74
|
+
source: `toolkit-${i}`
|
|
75
|
+
})));
|
|
76
|
+
const collision = Array.findFirst(Record.toEntries(Array.groupBy(labelled, (entry) => entry.name)), ([, group]) => group.length > 1);
|
|
77
|
+
if (Option.isSome(collision)) {
|
|
78
|
+
const [name, group] = collision.value;
|
|
79
|
+
return yield* new DuplicateToolName({
|
|
80
|
+
name,
|
|
81
|
+
sources: Array.map(group, (e) => e.source)
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return Array.reduce(kits, {}, (acc, kit) => ({
|
|
85
|
+
...acc,
|
|
86
|
+
...kit
|
|
87
|
+
}));
|
|
88
|
+
});
|
|
89
|
+
/**
|
|
90
|
+
* Wrap every local tool's `run` with a middleware, preserving each tool's
|
|
91
|
+
* `Input`/`Output` and unioning the middleware's requirement `R2` into the
|
|
92
|
+
* toolkit's `ToolkitR`. Non-local kinds pass through unchanged. Stack with
|
|
93
|
+
* `pipe` - inner-first, so the last `wrap` applied is the outermost and runs
|
|
94
|
+
* first:
|
|
95
|
+
*
|
|
96
|
+
* const observed = pipe(toolkit, Toolkit.wrap(logging), Toolkit.wrap(authz))
|
|
97
|
+
* Toolkit.run(observed, calls) // authz runs, then logging, then the tool
|
|
98
|
+
*
|
|
99
|
+
* A middleware that fails with `string` / `ToolFailed` is absorbed by the
|
|
100
|
+
* executor into a `ToolResult.Failure`; any other error propagates typed. Use
|
|
101
|
+
* `describeFailures` for the total "everything to the model" case, or the
|
|
102
|
+
* `Approval` flow for a distinct "denied" verdict.
|
|
103
|
+
*/
|
|
104
|
+
const wrap = Function.dual(2, (toolkit, middleware) => Record.map(toolkit, (tool) => tool._tag === "LocalTool" ? {
|
|
105
|
+
...tool,
|
|
106
|
+
run: middleware(tool.run, tool.name)
|
|
107
|
+
} : tool));
|
|
108
|
+
/**
|
|
109
|
+
* Map every local tool's error to a `string`, so `Toolkit.run` absorbs those
|
|
110
|
+
* failures into `ToolResult.Failure` the model reads and adapts to. Pairs
|
|
111
|
+
* with the domain `describe` helpers (`AiError.describe`,
|
|
112
|
+
* `BrowserError.describe`). Non-local kinds pass through.
|
|
113
|
+
*
|
|
114
|
+
* const kit = yield* Toolkit.compose(
|
|
115
|
+
* Toolkit.describeFailures(searchKit, AiError.describe),
|
|
116
|
+
* browserToolkit(session), // failures stay typed
|
|
117
|
+
* )
|
|
118
|
+
*/
|
|
119
|
+
const describeFailures = Function.dual(2, (toolkit, describe) => Record.map(toolkit, (tool) => tool._tag === "LocalTool" ? {
|
|
120
|
+
...tool,
|
|
121
|
+
run: (input, emit) => tool.run(input, emit).pipe(Effect.mapError(describe))
|
|
122
|
+
} : tool));
|
|
123
|
+
/**
|
|
124
|
+
* Execute every provided call. Approval/rejection policy belongs upstream.
|
|
125
|
+
*
|
|
126
|
+
* String and `ToolFailed` failures are absorbed into `ToolResult.Failure`
|
|
127
|
+
* (the model reads and adapts to them); every other tool error propagates
|
|
128
|
+
* typed on the stream, so `Exclude<ToolkitE<T>, string | ToolFailed>` is
|
|
129
|
+
* what a caller catches or lets end the run. If one call in a concurrent
|
|
130
|
+
* batch fails the stream, siblings may go unanswered: a loop that catches
|
|
131
|
+
* and continues must reconcile history first (`HistoryCheck.cancelAllPending`).
|
|
132
|
+
*/
|
|
133
|
+
const run = (toolkit, calls, options) => Stream.fromIterable(calls).pipe(Stream.flatMap((call) => runOne(toolkit, call), { concurrency: options?.concurrency ?? "unbounded" }));
|
|
17
134
|
const okResult = (call, tool, value) => ToolResult.Ok({
|
|
18
135
|
call_id: call.call_id,
|
|
19
136
|
tool,
|
|
20
137
|
value
|
|
21
138
|
});
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return runPlain(tool, call);
|
|
139
|
+
const outputEvent = (result) => Stream.succeed(ToolEvent.Output({ result }));
|
|
140
|
+
const runOne = (toolkit, call) => {
|
|
141
|
+
const tool = Object.hasOwn(toolkit, call.name) ? toolkit[call.name] : void 0;
|
|
142
|
+
return Match.value(tool).pipe(Match.when(Match.undefined, () => outputEvent(failed(call, "unknown_tool", `No tool registered with name "${call.name}"`))), Match.tag("LocalTool", (local) => runTool(local, call)), Match.orElse(() => outputEvent(nonLocalTool(call))));
|
|
27
143
|
};
|
|
28
|
-
const
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
}).pipe(Effect.catchCause(() => Effect.succeed(executionError(call, "Tool execution failed"))), Effect.map((result) => ToolEvent.Output({ result }))));
|
|
39
|
-
const runStreaming = (tool, call) => Stream.unwrap(Effect.gen(function* () {
|
|
40
|
-
const parsed = yield* parseJsonUnknown(call.arguments).pipe(Effect.mapError(() => "json_parse_error"));
|
|
41
|
-
const validated = yield* Effect.tryPromise({
|
|
42
|
-
try: () => Promise.resolve(tool.inputSchema["~standard"].validate(parsed)),
|
|
43
|
-
catch: () => "validation_threw"
|
|
44
|
-
});
|
|
45
|
-
if (validated.issues !== void 0) return Stream.succeed(ToolEvent.Output({ result: executionError(call, "Tool input failed schema validation") }));
|
|
46
|
-
const ref = yield* Ref.make([]);
|
|
47
|
-
const progress = tool.run(validated.value).pipe(Stream.tap((event) => Ref.update(ref, Array.append(event))), Stream.map((data) => ToolEvent.Progress({
|
|
144
|
+
const describeIssues = (issues) => issues.map((issue) => {
|
|
145
|
+
const path = (issue.path ?? []).map((seg) => typeof seg === "object" && seg !== null ? String(seg.key) : String(seg)).join(".");
|
|
146
|
+
return path === "" ? issue.message : `${path}: ${issue.message}`;
|
|
147
|
+
}).join("; ").slice(0, 500);
|
|
148
|
+
const runTool = (tool, call) => Stream.unwrap(decodeCallInput(tool, call).pipe(Effect.map(Match.type().pipe(Match.tag("parseError", () => outputEvent(validationError(call, "arguments are not valid JSON"))), Match.tag("invalid", ({ issues }) => outputEvent(validationError(call, describeIssues(issues)))), Match.tag("ok", ({ input }) => runValidated(tool, call, input)), Match.exhaustive))));
|
|
149
|
+
const runValidated = (tool, call, input) => Stream.unwrap(Effect.gen(function* () {
|
|
150
|
+
const queue = yield* Queue.make({ capacity: tool.emitBufferSize });
|
|
151
|
+
const emit = (event) => Effect.asVoid(Queue.offer(queue, event));
|
|
152
|
+
const fiber = yield* tool.run(input, emit).pipe(Effect.ensuring(Queue.end(queue)), Effect.forkScoped);
|
|
153
|
+
const progress = Stream.fromQueue(queue).pipe(Stream.map((data) => ToolEvent.Progress({
|
|
48
154
|
call_id: call.call_id,
|
|
49
155
|
tool: tool.name,
|
|
50
156
|
data
|
|
51
157
|
})));
|
|
52
|
-
const output = Stream.fromEffect(
|
|
158
|
+
const output = Stream.fromEffect(Fiber.join(fiber).pipe(Effect.matchCauseEffect({
|
|
159
|
+
onSuccess: (value) => Effect.succeed(ToolEvent.Output({ result: okResult(call, tool.name, value) })),
|
|
160
|
+
onFailure: (cause) => routeCause(call, cause)
|
|
161
|
+
})));
|
|
53
162
|
return progress.pipe(Stream.concat(output));
|
|
54
|
-
}))
|
|
163
|
+
}));
|
|
164
|
+
const routeCause = (call, cause) => {
|
|
165
|
+
const only = cause.reasons.length === 1 ? cause.reasons[0] : void 0;
|
|
166
|
+
if (only !== void 0 && Cause.isFailReason(only)) {
|
|
167
|
+
const error = only.error;
|
|
168
|
+
if (typeof error === "string") return Effect.succeed(ToolEvent.Output({ result: failed(call, "tool_failed", error) }));
|
|
169
|
+
if (error instanceof ToolFailed) return Effect.succeed(ToolEvent.Output({ result: failed(call, error.kind ?? "tool_failed", error.message) }));
|
|
170
|
+
}
|
|
171
|
+
return Effect.failCause(cause);
|
|
172
|
+
};
|
|
55
173
|
/**
|
|
56
174
|
* Append a completed turn plus tool results to a state's history, converting
|
|
57
175
|
* the results to wire-format `ToolCallOutput`s. Curried so it slots directly
|
|
58
176
|
* into `continueWithResults`:
|
|
59
177
|
*
|
|
60
|
-
* Toolkit.run(
|
|
178
|
+
* Toolkit.run(toolkit, calls).pipe(
|
|
61
179
|
* Toolkit.continueWithResults(Toolkit.appendToolResults(state, turn)),
|
|
62
180
|
* )
|
|
63
181
|
*
|
|
@@ -100,6 +218,6 @@ const continueWithResults = Function.dual(2, (stream, build) => Stream.unwrap(Ef
|
|
|
100
218
|
return tapped.pipe(Stream.concat(continuation));
|
|
101
219
|
})));
|
|
102
220
|
//#endregion
|
|
103
|
-
export { appendToolResults, collectResults, continueWithResults, run, Toolkit_exports as t };
|
|
221
|
+
export { DuplicateToolName, appendToolResults, collectResults, compose, continueWithResults, describeFailures, descriptors, fromArray, make, makeNamespaced, namespace, run, Toolkit_exports as t, wrap };
|
|
104
222
|
|
|
105
223
|
//# sourceMappingURL=Toolkit.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Toolkit.mjs","names":["Arr","Loop.value","Loop.next"],"sources":["../../src/tool/Toolkit.ts"],"sourcesContent":["import { Array as Arr, Effect, Function, Ref, Schema, Stream } from \"effect\"\nimport * as Loop from \"../loop/Loop.js\"\nimport type { ToolCall, HistoryItem } from \"../domain/Items.js\"\nimport { appendToHistory, type Turn } from \"../domain/Turn.js\"\nimport {\n type AnyTool,\n type AnyPlainTool,\n type AnyStreamingTool,\n isStreamingTool,\n type StreamingTool,\n type Tool,\n} from \"./Tool.js\"\nimport { ToolResult, executionError, failed, toToolCallOutput } from \"./ToolResult.js\"\nimport { ToolEvent } from \"./ToolEvent.js\"\nimport { isOutput } from \"./ToolEvent.js\"\n\n/**\n * Union of every tool's `R` requirements in a mixed plain + streaming array.\n * Used by `run` to surface the services tools need at the recipe level, so\n * the loop's stream type carries them through to `Effect.provide`.\n */\nexport type ToolKindR<Tools extends ReadonlyArray<AnyTool<any>>> =\n Tools[number] extends StreamingTool<any, any, any, any, infer R>\n ? R\n : Tools[number] extends Tool<any, any, any, infer R>\n ? R\n : never\n\n// ---------------------------------------------------------------------------\n// Tool executor. Streams `ToolEvent`s in real time and dispatches streaming\n// and plain tools uniformly. Policy stays outside this module: callers pass\n// only the calls they have already decided should run.\n// ---------------------------------------------------------------------------\n\nexport type ExecuteOptions = {\n readonly concurrency?: number | \"unbounded\"\n}\n\n/** Execute every provided call. Approval/rejection policy belongs upstream. */\nexport const run = <Tools extends ReadonlyArray<AnyTool<any>>>(\n tools: Tools,\n calls: ReadonlyArray<ToolCall>,\n options?: ExecuteOptions,\n): Stream.Stream<ToolEvent, never, ToolKindR<Tools>> =>\n Stream.fromIterable(calls).pipe(\n Stream.flatMap((call) => runOne(tools, call), {\n concurrency: options?.concurrency ?? \"unbounded\",\n }),\n )\n\nconst okResult = (call: ToolCall, tool: string, value: unknown): ToolResult =>\n ToolResult.Ok({\n call_id: call.call_id,\n tool,\n value,\n })\n\nconst runOne = <R>(\n tools: ReadonlyArray<AnyTool<R>>,\n call: ToolCall,\n): Stream.Stream<ToolEvent, never, R> => {\n const tool = tools.find((t) => t.name === call.name)\n if (tool === undefined) {\n // Graceful: emit a synthetic Failure so OTHER calls in this turn\n // still execute. LLMs hallucinate tool names; MCP tools come and go.\n return Stream.succeed(\n ToolEvent.Output({\n result: failed(call, \"unknown_tool\", `No tool registered with name \"${call.name}\"`),\n }),\n )\n }\n if (isStreamingTool(tool)) return runStreaming(tool, call)\n return runPlain(tool, call)\n}\n\nconst parseJsonUnknown = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown))\n\nconst runPlain = <R>(tool: AnyPlainTool<R>, call: ToolCall): Stream.Stream<ToolEvent, never, R> =>\n Stream.fromEffect(\n Effect.gen(function* () {\n const parsed = yield* parseJsonUnknown(call.arguments).pipe(\n Effect.mapError(() => \"json_parse_error\" as const),\n )\n const validated = yield* Effect.tryPromise({\n try: () => Promise.resolve(tool.inputSchema[\"~standard\"].validate(parsed)),\n catch: () => \"validation_threw\" as const,\n })\n if (validated.issues !== undefined) {\n return executionError(call, \"Tool input failed schema validation\")\n }\n const output = yield* tool.run(validated.value)\n return okResult(call, tool.name, output)\n }).pipe(\n Effect.catchCause(() => Effect.succeed(executionError(call, \"Tool execution failed\"))),\n Effect.map((result) => ToolEvent.Output({ result })),\n ),\n )\n\nconst runStreaming = <R>(\n tool: AnyStreamingTool<R>,\n call: ToolCall,\n): Stream.Stream<ToolEvent, never, R> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const parsed = yield* parseJsonUnknown(call.arguments).pipe(\n Effect.mapError(() => \"json_parse_error\" as const),\n )\n const validated = yield* Effect.tryPromise({\n try: () => Promise.resolve(tool.inputSchema[\"~standard\"].validate(parsed)),\n catch: () => \"validation_threw\" as const,\n })\n if (validated.issues !== undefined) {\n return Stream.succeed<ToolEvent>(\n ToolEvent.Output({\n result: executionError(call, \"Tool input failed schema validation\"),\n }),\n )\n }\n\n // Real-time: tap each event into a Ref as it flows; emit one\n // Progress per event; then concat one synthetic Output element\n // built from the accumulated Ref via `finalize`.\n const ref = yield* Ref.make<Array<unknown>>([])\n const progress = tool.run(validated.value).pipe(\n Stream.tap((event) => Ref.update(ref, Arr.append(event))),\n Stream.map((data) =>\n ToolEvent.Progress({\n call_id: call.call_id,\n tool: tool.name,\n data,\n }),\n ),\n )\n const output = Stream.fromEffect(\n Ref.get(ref).pipe(\n Effect.map((events) =>\n ToolEvent.Output({\n result: okResult(call, tool.name, tool.finalize(events)),\n }),\n ),\n ),\n )\n return progress.pipe(Stream.concat(output))\n }),\n ).pipe(\n Stream.catchCause(() =>\n Stream.succeed(\n ToolEvent.Output({\n result: executionError(call, \"Tool execution failed\"),\n }),\n ),\n ),\n )\n\n// ---------------------------------------------------------------------------\n// `continueWithResults` - bridge from a `Stream<ToolEvent>` to the loop's\n// emit shape. Drains the stream to the consumer in real-time, taps every\n// `Output` into an internal Ref, and at end-of-stream emits\n// `Loop.next(build(results))`. Recipe never sees the Ref.\n//\n// Dual: data-first `continueWithResults(stream, build)` and data-last\n// `stream.pipe(continueWithResults(build))` both work.\n// ---------------------------------------------------------------------------\n\n/**\n * Append a completed turn plus tool results to a state's history, converting\n * the results to wire-format `ToolCallOutput`s. Curried so it slots directly\n * into `continueWithResults`:\n *\n * Toolkit.run(tools, calls).pipe(\n * Toolkit.continueWithResults(Toolkit.appendToolResults(state, turn)),\n * )\n *\n * Equivalent to `appendToHistory(state, turn, results.map(toToolCallOutput))`\n * — the helper just hides the wire-conversion step.\n */\nexport const appendToolResults =\n <S extends { readonly history: ReadonlyArray<HistoryItem> }>(state: S, turn: Turn) =>\n (results: ReadonlyArray<ToolResult>): S =>\n appendToHistory(state, turn, results.map(toToolCallOutput))\n\n/**\n * Drain a `Stream<ToolEvent>` and return the accumulated `ToolResult`s\n * from every `Output` event. One-shot — the type is `Effect<results>`,\n * not a one-element stream — so it composes naturally with `Effect.map`\n * to build state.\n *\n * Counterpart to `continueWithResults`: `continueWithResults` bundles\n * drain + emit Next into one bridge; this one is the right arm of a\n * broadcast/fork-and-merge for callers that want to vary the left arm\n * (e.g., filter/tap ToolEvents before forwarding).\n */\nexport const collectResults = <E, R>(\n stream: Stream.Stream<ToolEvent, E, R>,\n): Effect.Effect<ReadonlyArray<ToolResult>, E, R> =>\n stream.pipe(\n Stream.filter(isOutput),\n Stream.map((e) => e.result),\n Stream.runCollect,\n )\n\n/**\n * Bridge from a `Stream<ToolEvent>` to the loop's emit shape. Forwards\n * every ToolEvent downstream as a `Loop.value` and at end-of-stream emits\n * one `Loop.next(build(results))` carrying the accumulated `ToolResult`s\n * from terminal `Output` events.\n *\n * Conceptually a broadcast fork (one arm passes events through, the other\n * drains them into a state). Implemented as a single-pass Ref tap + concat\n * for zero buffering — the broadcast version is observationally equivalent\n * but holds events in a PubSub until both arms drain. The public primitives\n * (`Loop.value`, `collectResults`, `Loop.next`) compose the same pattern\n * when you need to vary an arm.\n *\n * Dual: data-first `continueWithResults(stream, build)` and data-last\n * `stream.pipe(continueWithResults(build))` both work.\n */\nexport const continueWithResults: {\n <S>(\n build: (results: ReadonlyArray<ToolResult>) => S,\n ): <R>(\n stream: Stream.Stream<ToolEvent, never, R>,\n ) => Stream.Stream<Loop.Step<ToolEvent, S>, never, R>\n <S, R>(\n stream: Stream.Stream<ToolEvent, never, R>,\n build: (results: ReadonlyArray<ToolResult>) => S,\n ): Stream.Stream<Loop.Step<ToolEvent, S>, never, R>\n} = Function.dual(\n 2,\n <S, R>(\n stream: Stream.Stream<ToolEvent, never, R>,\n build: (results: ReadonlyArray<ToolResult>) => S,\n ): Stream.Stream<Loop.Step<ToolEvent, S>, never, R> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const ref = yield* Ref.make<ReadonlyArray<ToolResult>>([])\n const tapped = stream.pipe(\n Stream.tap((e) =>\n isOutput(e) ? Ref.update(ref, (acc) => Arr.append(acc, e.result)) : Effect.void,\n ),\n Stream.map(Loop.value),\n )\n const continuation = Stream.unwrap(\n Ref.get(ref).pipe(Effect.map((acc) => Loop.next(build(acc)))),\n )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;;;;;;;;;;;;;;AAkBH,MAAa,sBAUT,SAAS,KACX,IAEE,QACA,UAEA,OAAO,OACL,OAAO,IAAI,aAAa;CACtB,MAAM,MAAM,OAAO,IAAI,KAAgC,EAAE,CAAC;CAC1D,MAAM,SAAS,OAAO,KACpB,OAAO,KAAK,MACV,SAAS,EAAE,GAAG,IAAI,OAAO,MAAM,QAAQA,MAAI,OAAO,KAAK,EAAE,OAAO,CAAC,GAAG,OAAO,KAC5E,EACD,OAAO,IAAIC,MAAW,CACvB;CACD,MAAM,eAAe,OAAO,OAC1B,IAAI,IAAI,IAAI,CAAC,KAAK,OAAO,KAAK,QAAQC,KAAU,MAAM,IAAI,CAAC,CAAC,CAAC,CAC9D;AACD,QAAO,OAAO,KAAK,OAAO,OAAO,aAAa,CAAC;EAC/C,CACH,CACJ"}
|
|
1
|
+
{"version":3,"file":"Toolkit.mjs","names":["Arr","Loop.value","Loop.next"],"sources":["../../src/tool/Toolkit.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\"\nimport {\n Array as Arr,\n Cause,\n Data,\n Effect,\n Fiber,\n Function,\n Match,\n Option,\n Queue,\n Record,\n Ref,\n Stream,\n} from \"effect\"\nimport * as Loop from \"../loop/Loop.js\"\nimport type { HistoryItem, ToolCall } from \"../domain/Items.js\"\nimport { appendToHistory, type Turn } from \"../domain/Turn.js\"\nimport {\n type AnyLocalTool,\n type AnyTool,\n type DecodeResult,\n decodeCallInput,\n type Emit,\n type InteractionTool,\n type ProviderTool,\n type SignalTool,\n type Tool,\n type ToolDescriptor,\n type ToolE,\n ToolFailed,\n type ToolR,\n toDescriptors,\n} from \"./Tool.js\"\nimport {\n ToolResult,\n failed,\n nonLocalTool,\n toToolCallOutput,\n validationError,\n} from \"./ToolResult.js\"\nimport { ToolEvent } from \"./ToolEvent.js\"\nimport { isOutput } from \"./ToolEvent.js\"\n\n// ---------------------------------------------------------------------------\n// Toolkit: a name-indexed record of tools — \"what the model sees\". The record\n// gives O(1) dispatch and within-toolkit name-uniqueness for free; descriptors\n// are derived, not stored. Build a static one with `make` (compile-time\n// duplicate-name check), a dynamic one with `fromArray` (trusted single source,\n// last-wins), and combine independent ones with `compose` (the application\n// boundary where cross-source names can clash).\n// ---------------------------------------------------------------------------\n\nexport type ToolMap = Record<string, AnyTool>\n\nexport type Toolkit<Tools extends ToolMap = ToolMap> = Tools\n\nconst indexByName = (tools: ReadonlyArray<AnyTool>): ToolMap =>\n Object.fromEntries(tools.map((tool) => [tool.name, tool]))\n\n/**\n * Index literal tools by their `name`. Duplicate names are a compile error\n * (`UniqueTools`). Names are not otherwise checked: a malformed name is a\n * developer typo in a literal that the provider rejects clearly on first use,\n * and there is no single cross-provider name rule to enforce here anyway.\n */\nconst buildStatic = <Tools extends ReadonlyArray<AnyTool>>(\n tools: Tools,\n): Toolkit<{ [T in Tools[number] as T[\"name\"]]: T }> =>\n indexByName(tools) as Toolkit<{ [T in Tools[number] as T[\"name\"]]: T }>\n\nexport const make = <const Tools extends ReadonlyArray<AnyTool>>(\n ...tools: UniqueTools<Tools>\n): Toolkit<{ [T in Tools[number] as T[\"name\"]]: T }> => buildStatic(tools as unknown as Tools)\n\n/**\n * Build a toolkit from a dynamically-sourced array (MCP server, plugin). Names\n * are not validated (MCP legitimately uses dots / >64 chars — sanitize at the\n * provider adapter); same-named tools resolve last-wins. Cross-source clashes\n * are caught by `compose`.\n */\nexport const fromArray = (tools: ReadonlyArray<AnyTool>): Toolkit => indexByName(tools)\n\n/** Render the provider-facing descriptors for a toolkit on demand. */\nexport const descriptors = (toolkit: Toolkit): ReadonlyArray<ToolDescriptor> =>\n toDescriptors(Object.values(toolkit))\n\n/**\n * Union of every tool's `R` requirements in a toolkit. Surfaced by `run` so the\n * loop's stream type carries the services tools need through to `Effect.provide`.\n *\n * The `ToolMap extends T` guard yields `never` for the wide `Toolkit` type (a\n * toolkit passed through an untyped boundary, where the requirements aren't\n * statically known) instead of `any`, which would otherwise poison the\n * resulting stream's `R`. Concrete toolkits from `make` keep their precise R.\n */\nexport type ToolkitR<T extends Toolkit> = ToolMap extends T ? never : ToolR<T[keyof T]>\n\n/**\n * Union of every tool's `E` error type in a toolkit. `Toolkit.run` reports\n * `Exclude<ToolkitE<T>, string | ToolFailed>` on its stream: the sentinel\n * failures are absorbed into results, everything else propagates typed. The\n * same `ToolMap extends T` guard as `ToolkitR` keeps a wide toolkit at\n * `never` rather than `any`.\n */\nexport type ToolkitE<T extends Toolkit> = ToolMap extends T ? never : ToolE<T[keyof T]>\n\n// ---------------------------------------------------------------------------\n// Uniqueness & composition. `make` rejects duplicate literal names at compile\n// time; `compose` rejects cross-source collisions — at compile time when the\n// names are statically known, and at runtime (`DuplicateToolName`) for dynamic\n// sources whose keys are only `string`. No silent last-wins overwrite, no late\n// provider 400.\n// ---------------------------------------------------------------------------\n\n// --- compile-time duplicate detection --------------------------------------\n\ntype DuplicateName<\n Tools extends ReadonlyArray<AnyTool>,\n Seen extends string = never,\n> = Tools extends readonly [\n infer Head extends AnyTool,\n ...infer Tail extends ReadonlyArray<AnyTool>,\n]\n ? Head[\"name\"] extends Seen\n ? Head[\"name\"]\n : DuplicateName<Tail, Seen | Head[\"name\"]>\n : never\n\ntype DuplicateNameError<Name extends string> = {\n readonly __error: `Duplicate tool name: ${Name}`\n}\n\nexport type NoDuplicateNames<Tools extends ReadonlyArray<AnyTool>> = [\n DuplicateName<Tools>,\n] extends [never]\n ? unknown\n : DuplicateNameError<DuplicateName<Tools>>\n\n/** `Tools` constrained so a repeated literal name is a compile error in `make`. */\nexport type UniqueTools<Tools extends ReadonlyArray<AnyTool>> = Tools & NoDuplicateNames<Tools>\n\n// Cross-toolkit literal-key collision, skipping wide (`string`-keyed, dynamic)\n// toolkits so the compile-time check never false-positives on `fromArray`.\ntype ComposeDuplicateKey<\n Kits extends ReadonlyArray<Toolkit>,\n Seen extends string = never,\n> = Kits extends readonly [infer Head extends Toolkit, ...infer Tail extends ReadonlyArray<Toolkit>]\n ? string extends keyof Head\n ? ComposeDuplicateKey<Tail, Seen>\n : Extract<keyof Head & string, Seen> extends never\n ? ComposeDuplicateKey<Tail, Seen | (keyof Head & string)>\n : Extract<keyof Head & string, Seen>\n : never\n\ntype NoDuplicateComposedKeys<Kits extends ReadonlyArray<Toolkit>> = [\n ComposeDuplicateKey<Kits>,\n] extends [never]\n ? unknown\n : DuplicateNameError<ComposeDuplicateKey<Kits>>\n\ntype UnionToIntersection<U> = (U extends unknown ? (x: U) => void : never) extends (\n x: infer I,\n) => void\n ? I\n : never\n\ntype Composed<Kits extends ReadonlyArray<Toolkit>> =\n UnionToIntersection<Kits[number]> extends infer M ? (M extends ToolMap ? M : ToolMap) : ToolMap\n\n// --- uniqueness error ------------------------------------------------------\n\n/**\n * Two composed toolkits resolved to the same final name. Carries the colliding\n * `sources` (the positions of the clashing toolkits) so the error says *which*\n * inputs collided. Raised at runtime by `compose` for dynamic sources; static\n * collisions are caught earlier at compile time.\n */\nexport class DuplicateToolName extends Data.TaggedError(\"DuplicateToolName\")<{\n readonly name: string\n readonly sources: ReadonlyArray<string>\n}> {}\n\n// --- namespacing -----------------------------------------------------------\n\ntype PrefixName<Prefix extends string, Name extends string> = `${Prefix}__${Name}`\n\ntype PrefixTool<Prefix extends string, T extends AnyTool> =\n T extends Tool<infer N, infer I, infer Ev, infer O, infer R>\n ? Tool<PrefixName<Prefix, N>, I, Ev, O, R>\n : T extends ProviderTool<infer N, infer I, infer P, infer C>\n ? ProviderTool<PrefixName<Prefix, N>, I, P, C>\n : T extends SignalTool<infer N, infer I>\n ? SignalTool<PrefixName<Prefix, N>, I>\n : T extends InteractionTool<infer N, infer I>\n ? InteractionTool<PrefixName<Prefix, N>, I>\n : T\n\ntype Namespaced<Prefix extends string, T extends ToolMap> = {\n readonly [K in keyof T & string as PrefixName<Prefix, K>]: T[K] extends AnyTool\n ? PrefixTool<Prefix, T[K]>\n : never\n}\n\n/**\n * Prefix every tool name with `<namespace>__`, returning a new toolkit. Use it\n * to keep generic names (`search`) from two sources distinct before `compose`.\n * If two namespaces collide at the `__` separator, the duplicate final name\n * surfaces from `compose` as a `DuplicateToolName`.\n */\nexport const namespace = <const Prefix extends string, const T extends ToolMap>(\n prefix: Prefix,\n toolkit: Toolkit<T>,\n): Toolkit<Namespaced<Prefix, T>> => {\n const renamed = Arr.map(Record.values(toolkit), (tool) => ({\n ...tool,\n name: `${prefix}__${tool.name}`,\n }))\n return indexByName(renamed) as Toolkit<Namespaced<Prefix, T>>\n}\n\n/** `namespace(prefix, make(...tools))` in one step, for static namespaced tools. */\nexport const makeNamespaced = <\n const Prefix extends string,\n const Tools extends ReadonlyArray<AnyTool>,\n>(\n prefix: Prefix,\n ...tools: UniqueTools<Tools>\n): Toolkit<Namespaced<Prefix, { [T in Tools[number] as T[\"name\"]]: T }>> =>\n namespace(prefix, buildStatic(tools as unknown as Tools))\n\n// --- composition (the application boundary) --------------------------------\n\n/**\n * Combine independently-built toolkits into one. This is where names from\n * separate sources can collide, so it is the one effectful constructor: a\n * duplicate final name fails with `DuplicateToolName` (naming the colliding\n * input positions) instead of silently overwriting or 400-ing later at the\n * provider. Statically-known collisions are additionally a compile error.\n */\nexport const compose = <const Kits extends ReadonlyArray<Toolkit>>(\n ...toolkits: Kits & NoDuplicateComposedKeys<Kits>\n): Effect.Effect<Toolkit<Composed<Kits>>, DuplicateToolName> =>\n Effect.gen(function* () {\n const kits = toolkits as unknown as ReadonlyArray<Toolkit>\n const labelled = Arr.flatMap(kits, (kit, i) =>\n Arr.map(Record.keys(kit), (name) => ({ name, source: `toolkit-${i}` })),\n )\n const collision = Arr.findFirst(\n Record.toEntries(Arr.groupBy(labelled, (entry) => entry.name)),\n ([, group]) => group.length > 1,\n )\n if (Option.isSome(collision)) {\n const [name, group] = collision.value\n return yield* new DuplicateToolName({ name, sources: Arr.map(group, (e) => e.source) })\n }\n return Arr.reduce(kits, {} as ToolMap, (acc, kit) => ({ ...acc, ...kit })) as Toolkit<\n Composed<Kits>\n >\n })\n\n// ---------------------------------------------------------------------------\n// Middleware: a `Toolkit -> Toolkit` transform applied up front, then the\n// wrapped toolkit is run. A middleware transforms one local tool's `run` given\n// its name (logging, retry, auth, metrics); `wrap` lifts it over the whole\n// toolkit, leaving provider/signal/interaction kinds untouched. The one reason\n// this is a combinator rather than native `Record.map`: it tracks the\n// middleware's added requirement `R2`, unioning it into `ToolkitR` instead of\n// widening every tool's type.\n// ---------------------------------------------------------------------------\n\ntype Handler<Input, Event, Output, E, R> = (\n input: Input,\n emit: Emit<Event>,\n) => Effect.Effect<Output, E, R>\n\n/**\n * A `run` transform, tracking an added requirement `R2` and optional added\n * error `E2`. The tool's own `E` is preserved; `describeFailures` is the\n * combinator that *replaces* it (mapping every error to `string`).\n */\nexport type Middleware<R2 = never, E2 = never> = <Input, Event, Output, E, R>(\n run: Handler<Input, Event, Output, E, R>,\n name: string,\n) => Handler<Input, Event, Output, E | E2, R | R2>\n\ntype WrapTool<T extends AnyTool, R2, E2> =\n T extends Tool<infer N, infer I, infer Ev, infer O, infer E, infer R>\n ? Tool<N, I, Ev, O, E | E2, R | R2>\n : T\n\ntype WrapToolkit<T extends ToolMap, R2, E2> = {\n readonly [K in keyof T]: T[K] extends AnyTool ? WrapTool<T[K], R2, E2> : T[K]\n}\n\n/**\n * Wrap every local tool's `run` with a middleware, preserving each tool's\n * `Input`/`Output` and unioning the middleware's requirement `R2` into the\n * toolkit's `ToolkitR`. Non-local kinds pass through unchanged. Stack with\n * `pipe` - inner-first, so the last `wrap` applied is the outermost and runs\n * first:\n *\n * const observed = pipe(toolkit, Toolkit.wrap(logging), Toolkit.wrap(authz))\n * Toolkit.run(observed, calls) // authz runs, then logging, then the tool\n *\n * A middleware that fails with `string` / `ToolFailed` is absorbed by the\n * executor into a `ToolResult.Failure`; any other error propagates typed. Use\n * `describeFailures` for the total \"everything to the model\" case, or the\n * `Approval` flow for a distinct \"denied\" verdict.\n */\nexport const wrap: {\n <R2, E2>(\n middleware: Middleware<R2, E2>,\n ): <T extends Toolkit>(toolkit: T) => WrapToolkit<T, R2, E2>\n <T extends Toolkit, R2, E2>(toolkit: T, middleware: Middleware<R2, E2>): WrapToolkit<T, R2, E2>\n} = Function.dual(\n 2,\n <T extends Toolkit, R2, E2>(toolkit: T, middleware: Middleware<R2, E2>): WrapToolkit<T, R2, E2> =>\n Record.map(toolkit, (tool) =>\n tool._tag === \"LocalTool\" ? { ...tool, run: middleware(tool.run, tool.name) } : tool,\n ) as WrapToolkit<T, R2, E2>,\n)\n\n// ---------------------------------------------------------------------------\n// describeFailures: map every local tool's error to a model-visible string,\n// opting its failures into recovery. Group tools by recovery policy, wrap\n// each group, then compose.\n// ---------------------------------------------------------------------------\n\ntype DescribeTool<T extends AnyTool> =\n T extends Tool<infer N, infer I, infer Ev, infer O, any, infer R>\n ? Tool<N, I, Ev, O, string, R>\n : T\n\ntype DescribeToolkit<T extends ToolMap> = {\n readonly [K in keyof T]: T[K] extends AnyTool ? DescribeTool<T[K]> : T[K]\n}\n\n/**\n * Map every local tool's error to a `string`, so `Toolkit.run` absorbs those\n * failures into `ToolResult.Failure` the model reads and adapts to. Pairs\n * with the domain `describe` helpers (`AiError.describe`,\n * `BrowserError.describe`). Non-local kinds pass through.\n *\n * const kit = yield* Toolkit.compose(\n * Toolkit.describeFailures(searchKit, AiError.describe),\n * browserToolkit(session), // failures stay typed\n * )\n */\nexport const describeFailures: {\n <E>(describe: (e: E) => string): <T extends Toolkit>(toolkit: T) => DescribeToolkit<T>\n <T extends Toolkit>(toolkit: T, describe: (e: ToolkitE<T>) => string): DescribeToolkit<T>\n} = Function.dual(\n 2,\n <T extends Toolkit>(toolkit: T, describe: (e: any) => string): DescribeToolkit<T> =>\n Record.map(toolkit, (tool) =>\n tool._tag === \"LocalTool\"\n ? {\n ...tool,\n run: (input: unknown, emit: Emit<unknown>) =>\n (tool as AnyLocalTool).run(input, emit).pipe(Effect.mapError(describe)),\n }\n : tool,\n ) as DescribeToolkit<T>,\n)\n\n// ---------------------------------------------------------------------------\n// Tool executor. Streams `ToolEvent`s in real time. Policy stays outside this\n// module: callers pass only the calls they have already decided should run.\n// ---------------------------------------------------------------------------\n\nexport type ExecuteOptions = {\n readonly concurrency?: number | \"unbounded\"\n}\n\n/**\n * Execute every provided call. Approval/rejection policy belongs upstream.\n *\n * String and `ToolFailed` failures are absorbed into `ToolResult.Failure`\n * (the model reads and adapts to them); every other tool error propagates\n * typed on the stream, so `Exclude<ToolkitE<T>, string | ToolFailed>` is\n * what a caller catches or lets end the run. If one call in a concurrent\n * batch fails the stream, siblings may go unanswered: a loop that catches\n * and continues must reconcile history first (`HistoryCheck.cancelAllPending`).\n */\nexport const run = <T extends Toolkit>(\n toolkit: T,\n calls: ReadonlyArray<ToolCall>,\n options?: ExecuteOptions,\n): Stream.Stream<ToolEvent, Exclude<ToolkitE<T>, string | ToolFailed>, ToolkitR<T>> =>\n Stream.fromIterable(calls).pipe(\n Stream.flatMap((call) => runOne(toolkit, call), {\n concurrency: options?.concurrency ?? \"unbounded\",\n }),\n ) as Stream.Stream<ToolEvent, Exclude<ToolkitE<T>, string | ToolFailed>, ToolkitR<T>>\n\nconst okResult = (call: ToolCall, tool: string, value: unknown): ToolResult =>\n ToolResult.Ok({ call_id: call.call_id, tool, value })\n\nconst outputEvent = (result: ToolResult): Stream.Stream<ToolEvent, never, any> =>\n Stream.succeed(ToolEvent.Output({ result }))\n\nconst runOne = (toolkit: ToolMap, call: ToolCall): Stream.Stream<ToolEvent, unknown, any> => {\n // Own-property lookup: a hallucinated call named `constructor` / `toString`\n // must not resolve to a prototype method.\n const tool = Object.hasOwn(toolkit, call.name) ? toolkit[call.name] : undefined\n return Match.value(tool).pipe(\n // No such tool. Graceful: emit a synthetic Failure so OTHER calls in this\n // turn still execute. LLMs hallucinate tool names; MCP tools come and go.\n Match.when(Match.undefined, () =>\n outputEvent(failed(call, \"unknown_tool\", `No tool registered with name \"${call.name}\"`)),\n ),\n Match.tag(\"LocalTool\", (local) => runTool(local, call)),\n // Provider/signal/interaction tools are model-visible but not locally\n // executable. The loop is expected to intercept these (decode + act) before\n // execution; reaching here means it didn't, so report it rather than crash.\n Match.orElse(() => outputEvent(nonLocalTool(call))),\n )\n}\n\nconst describeIssues = (issues: ReadonlyArray<StandardSchemaV1.Issue>): string =>\n issues\n .map((issue) => {\n const path = (issue.path ?? [])\n .map((seg) => (typeof seg === \"object\" && seg !== null ? String(seg.key) : String(seg)))\n .join(\".\")\n return path === \"\" ? issue.message : `${path}: ${issue.message}`\n })\n .join(\"; \")\n .slice(0, 500)\n\n// A decode failure is the model's contract violation: report it as a result\n// (never a stream failure) so the model can correct its next call.\nconst runTool = (tool: AnyLocalTool, call: ToolCall): Stream.Stream<ToolEvent, unknown, any> =>\n Stream.unwrap(\n decodeCallInput(tool, call).pipe(\n Effect.map(\n Match.type<DecodeResult<unknown>>().pipe(\n Match.tag(\"parseError\", () =>\n outputEvent(validationError(call, \"arguments are not valid JSON\")),\n ),\n Match.tag(\"invalid\", ({ issues }) =>\n outputEvent(validationError(call, describeIssues(issues))),\n ),\n Match.tag(\"ok\", ({ input }) => runValidated(tool, call, input)),\n Match.exhaustive,\n ),\n ),\n ),\n )\n\nconst runValidated = (\n tool: AnyLocalTool,\n call: ToolCall,\n input: unknown,\n): Stream.Stream<ToolEvent, unknown, any> =>\n Stream.unwrap(\n Effect.gen(function* () {\n // Per-call queue: `emit` offers events as `run` produces them; the\n // progress stream drains them in real time; `Queue.end` (in the\n // `ensuring`) flushes pending events then signals a clean end, which\n // `Stream.fromQueue` treats as completion. `Queue.shutdown` would clear\n // queued items and interrupt pending takes — wrong for graceful teardown.\n const queue = yield* Queue.make<unknown, Cause.Done>({ capacity: tool.emitBufferSize })\n const emit = (event: unknown) => Effect.asVoid(Queue.offer(queue, event))\n const fiber = yield* tool\n .run(input, emit)\n .pipe(Effect.ensuring(Queue.end(queue)), Effect.forkScoped)\n\n const progress = Stream.fromQueue(queue).pipe(\n Stream.map((data) => ToolEvent.Progress({ call_id: call.call_id, tool: tool.name, data })),\n )\n const output = Stream.fromEffect(\n Fiber.join(fiber).pipe(\n Effect.matchCauseEffect({\n onSuccess: (value: unknown) =>\n Effect.succeed(ToolEvent.Output({ result: okResult(call, tool.name, value) })),\n onFailure: (cause: Cause.Cause<unknown>) => routeCause(call, cause),\n }),\n ),\n )\n return progress.pipe(Stream.concat(output))\n }),\n )\n\n// A clean single string / ToolFailed failure is absorbed as a model-visible\n// result; interruption, defects, and typed non-sentinel errors propagate.\nconst routeCause = (\n call: ToolCall,\n cause: Cause.Cause<unknown>,\n): Effect.Effect<ToolEvent, unknown> => {\n const only = cause.reasons.length === 1 ? cause.reasons[0] : undefined\n if (only !== undefined && Cause.isFailReason(only)) {\n const error = only.error\n if (typeof error === \"string\") {\n return Effect.succeed(ToolEvent.Output({ result: failed(call, \"tool_failed\", error) }))\n }\n if (error instanceof ToolFailed) {\n return Effect.succeed(\n ToolEvent.Output({ result: failed(call, error.kind ?? \"tool_failed\", error.message) }),\n )\n }\n }\n return Effect.failCause(cause)\n}\n\n// ---------------------------------------------------------------------------\n// `continueWithResults` - bridge from a `Stream<ToolEvent>` to the loop's\n// emit shape. Drains the stream to the consumer in real-time, taps every\n// `Output` into an internal Ref, and at end-of-stream emits\n// `Loop.next(build(results))`. Recipe never sees the Ref.\n//\n// Dual: data-first `continueWithResults(stream, build)` and data-last\n// `stream.pipe(continueWithResults(build))` both work.\n// ---------------------------------------------------------------------------\n\n/**\n * Append a completed turn plus tool results to a state's history, converting\n * the results to wire-format `ToolCallOutput`s. Curried so it slots directly\n * into `continueWithResults`:\n *\n * Toolkit.run(toolkit, calls).pipe(\n * Toolkit.continueWithResults(Toolkit.appendToolResults(state, turn)),\n * )\n *\n * Equivalent to `appendToHistory(state, turn, results.map(toToolCallOutput))`\n * — the helper just hides the wire-conversion step.\n */\nexport const appendToolResults =\n <S extends { readonly history: ReadonlyArray<HistoryItem> }>(state: S, turn: Turn) =>\n (results: ReadonlyArray<ToolResult>): S =>\n appendToHistory(state, turn, results.map(toToolCallOutput))\n\n/**\n * Drain a `Stream<ToolEvent>` and return the accumulated `ToolResult`s\n * from every `Output` event. One-shot — the type is `Effect<results>`,\n * not a one-element stream — so it composes naturally with `Effect.map`\n * to build state.\n *\n * Counterpart to `continueWithResults`: `continueWithResults` bundles\n * drain + emit Next into one bridge; this one is the right arm of a\n * broadcast/fork-and-merge for callers that want to vary the left arm\n * (e.g., filter/tap ToolEvents before forwarding).\n */\nexport const collectResults = <E, R>(\n stream: Stream.Stream<ToolEvent, E, R>,\n): Effect.Effect<ReadonlyArray<ToolResult>, E, R> =>\n stream.pipe(\n Stream.filter(isOutput),\n Stream.map((e) => e.result),\n Stream.runCollect,\n )\n\n/**\n * Bridge from a `Stream<ToolEvent>` to the loop's emit shape. Forwards\n * every ToolEvent downstream as a `Loop.value` and at end-of-stream emits\n * one `Loop.next(build(results))` carrying the accumulated `ToolResult`s\n * from terminal `Output` events.\n *\n * Conceptually a broadcast fork (one arm passes events through, the other\n * drains them into a state). Implemented as a single-pass Ref tap + concat\n * for zero buffering — the broadcast version is observationally equivalent\n * but holds events in a PubSub until both arms drain. The public primitives\n * (`Loop.value`, `collectResults`, `Loop.next`) compose the same pattern\n * when you need to vary an arm.\n *\n * Dual: data-first `continueWithResults(stream, build)` and data-last\n * `stream.pipe(continueWithResults(build))` both work.\n */\nexport const continueWithResults: {\n <S>(\n build: (results: ReadonlyArray<ToolResult>) => S,\n ): <E, R>(stream: Stream.Stream<ToolEvent, E, R>) => Stream.Stream<Loop.Step<ToolEvent, S>, E, R>\n <S, E, R>(\n stream: Stream.Stream<ToolEvent, E, R>,\n build: (results: ReadonlyArray<ToolResult>) => S,\n ): Stream.Stream<Loop.Step<ToolEvent, S>, E, R>\n} = Function.dual(\n 2,\n <S, E, R>(\n stream: Stream.Stream<ToolEvent, E, R>,\n build: (results: ReadonlyArray<ToolResult>) => S,\n ): Stream.Stream<Loop.Step<ToolEvent, S>, E, R> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const ref = yield* Ref.make<ReadonlyArray<ToolResult>>([])\n const tapped = stream.pipe(\n Stream.tap((e) =>\n isOutput(e) ? Ref.update(ref, (acc) => Arr.append(acc, e.result)) : Effect.void,\n ),\n Stream.map(Loop.value),\n )\n const continuation = Stream.unwrap(\n Ref.get(ref).pipe(Effect.map((acc) => Loop.next(build(acc)))),\n )\n return tapped.pipe(Stream.concat(continuation))\n }),\n ),\n)\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAyDA,MAAM,eAAe,UACnB,OAAO,YAAY,MAAM,KAAK,SAAS,CAAC,KAAK,MAAM,KAAK,CAAC,CAAC;;;;;;;AAQ5D,MAAM,eACJ,UAEA,YAAY,MAAM;AAEpB,MAAa,QACX,GAAG,UACmD,YAAY,MAA0B;;;;;;;AAQ9F,MAAa,aAAa,UAA2C,YAAY,MAAM;;AAGvF,MAAa,eAAe,YAC1B,cAAc,OAAO,OAAO,QAAQ,CAAC;;;;;;;AA6FvC,IAAa,oBAAb,cAAuC,KAAK,YAAY,oBAAoB,CAGzE;;;;;;;AA6BH,MAAa,aACX,QACA,YACmC;AAKnC,QAAO,YAJSA,MAAI,IAAI,OAAO,OAAO,QAAQ,GAAG,UAAU;EACzD,GAAG;EACH,MAAM,GAAG,OAAO,IAAI,KAAK;EAC1B,EACyB,CAAC;;;AAI7B,MAAa,kBAIX,QACA,GAAG,UAEH,UAAU,QAAQ,YAAY,MAA0B,CAAC;;;;;;;;AAW3D,MAAa,WACX,GAAG,aAEH,OAAO,IAAI,aAAa;CACtB,MAAM,OAAO;CACb,MAAM,WAAWA,MAAI,QAAQ,OAAO,KAAK,MACvCA,MAAI,IAAI,OAAO,KAAK,IAAI,GAAG,UAAU;EAAE;EAAM,QAAQ,WAAW;EAAK,EAAE,CACxE;CACD,MAAM,YAAYA,MAAI,UACpB,OAAO,UAAUA,MAAI,QAAQ,WAAW,UAAU,MAAM,KAAK,CAAC,GAC7D,GAAG,WAAW,MAAM,SAAS,EAC/B;AACD,KAAI,OAAO,OAAO,UAAU,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