@yolk-sdk/agent 0.0.1-canary.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.
Files changed (161) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +93 -0
  3. package/dist/client/index.d.mts +3 -0
  4. package/dist/client/index.mjs +3 -0
  5. package/dist/client/state.d.mts +99 -0
  6. package/dist/client/state.d.mts.map +1 -0
  7. package/dist/client/state.mjs +245 -0
  8. package/dist/client/state.mjs.map +1 -0
  9. package/dist/client/transport.d.mts +67 -0
  10. package/dist/client/transport.d.mts.map +1 -0
  11. package/dist/client/transport.mjs +219 -0
  12. package/dist/client/transport.mjs.map +1 -0
  13. package/dist/index.d.mts +1 -0
  14. package/dist/index.mjs +1 -0
  15. package/dist/loop/accumulator.d.mts +11 -0
  16. package/dist/loop/accumulator.d.mts.map +1 -0
  17. package/dist/loop/accumulator.mjs +40 -0
  18. package/dist/loop/accumulator.mjs.map +1 -0
  19. package/dist/loop/error.d.mts +36 -0
  20. package/dist/loop/error.d.mts.map +1 -0
  21. package/dist/loop/error.mjs +84 -0
  22. package/dist/loop/error.mjs.map +1 -0
  23. package/dist/loop/index.d.mts +9 -0
  24. package/dist/loop/index.mjs +9 -0
  25. package/dist/loop/llm-event.d.mts +44 -0
  26. package/dist/loop/llm-event.d.mts.map +1 -0
  27. package/dist/loop/llm-event.mjs +34 -0
  28. package/dist/loop/llm-event.mjs.map +1 -0
  29. package/dist/loop/run.d.mts +37 -0
  30. package/dist/loop/run.d.mts.map +1 -0
  31. package/dist/loop/run.mjs +624 -0
  32. package/dist/loop/run.mjs.map +1 -0
  33. package/dist/loop/services/context-transformer.d.mts +18 -0
  34. package/dist/loop/services/context-transformer.d.mts.map +1 -0
  35. package/dist/loop/services/context-transformer.mjs +12 -0
  36. package/dist/loop/services/context-transformer.mjs.map +1 -0
  37. package/dist/loop/services/llm-provider.d.mts +20 -0
  38. package/dist/loop/services/llm-provider.d.mts.map +1 -0
  39. package/dist/loop/services/llm-provider.mjs +7 -0
  40. package/dist/loop/services/llm-provider.mjs.map +1 -0
  41. package/dist/loop/services/loop-config.d.mts +17 -0
  42. package/dist/loop/services/loop-config.d.mts.map +1 -0
  43. package/dist/loop/services/loop-config.mjs +15 -0
  44. package/dist/loop/services/loop-config.mjs.map +1 -0
  45. package/dist/loop/services/tool-executor.d.mts +12 -0
  46. package/dist/loop/services/tool-executor.d.mts.map +1 -0
  47. package/dist/loop/services/tool-executor.mjs +7 -0
  48. package/dist/loop/services/tool-executor.mjs.map +1 -0
  49. package/dist/loop/testing/faux-provider.d.mts +31 -0
  50. package/dist/loop/testing/faux-provider.d.mts.map +1 -0
  51. package/dist/loop/testing/faux-provider.mjs +47 -0
  52. package/dist/loop/testing/faux-provider.mjs.map +1 -0
  53. package/dist/loop/testing/index.d.mts +3 -0
  54. package/dist/loop/testing/index.mjs +3 -0
  55. package/dist/loop/testing/test-tool-executor.d.mts +10 -0
  56. package/dist/loop/testing/test-tool-executor.d.mts.map +1 -0
  57. package/dist/loop/testing/test-tool-executor.mjs +21 -0
  58. package/dist/loop/testing/test-tool-executor.mjs.map +1 -0
  59. package/dist/protocol/capability.d.mts +20 -0
  60. package/dist/protocol/capability.d.mts.map +1 -0
  61. package/dist/protocol/capability.mjs +34 -0
  62. package/dist/protocol/capability.mjs.map +1 -0
  63. package/dist/protocol/content.d.mts +31 -0
  64. package/dist/protocol/content.d.mts.map +1 -0
  65. package/dist/protocol/content.mjs +52 -0
  66. package/dist/protocol/content.mjs.map +1 -0
  67. package/dist/protocol/event.d.mts +228 -0
  68. package/dist/protocol/event.d.mts.map +1 -0
  69. package/dist/protocol/event.mjs +217 -0
  70. package/dist/protocol/event.mjs.map +1 -0
  71. package/dist/protocol/index.d.mts +14 -0
  72. package/dist/protocol/index.d.mts.map +1 -0
  73. package/dist/protocol/index.mjs +9 -0
  74. package/dist/protocol/message.d.mts +53 -0
  75. package/dist/protocol/message.d.mts.map +1 -0
  76. package/dist/protocol/message.mjs +49 -0
  77. package/dist/protocol/message.mjs.map +1 -0
  78. package/dist/protocol/reasoning.d.mts +8 -0
  79. package/dist/protocol/reasoning.d.mts.map +1 -0
  80. package/dist/protocol/reasoning.mjs +13 -0
  81. package/dist/protocol/reasoning.mjs.map +1 -0
  82. package/dist/protocol/session.d.mts +39 -0
  83. package/dist/protocol/session.d.mts.map +1 -0
  84. package/dist/protocol/session.mjs +38 -0
  85. package/dist/protocol/session.mjs.map +1 -0
  86. package/dist/protocol/tool.d.mts +101 -0
  87. package/dist/protocol/tool.d.mts.map +1 -0
  88. package/dist/protocol/tool.mjs +102 -0
  89. package/dist/protocol/tool.mjs.map +1 -0
  90. package/dist/protocol/usage.d.mts +26 -0
  91. package/dist/protocol/usage.d.mts.map +1 -0
  92. package/dist/protocol/usage.mjs +40 -0
  93. package/dist/protocol/usage.mjs.map +1 -0
  94. package/dist/runtime/error.d.mts +29 -0
  95. package/dist/runtime/error.d.mts.map +1 -0
  96. package/dist/runtime/error.mjs +46 -0
  97. package/dist/runtime/error.mjs.map +1 -0
  98. package/dist/runtime/index.d.mts +9 -0
  99. package/dist/runtime/index.d.mts.map +1 -0
  100. package/dist/runtime/index.mjs +4 -0
  101. package/dist/runtime/run-runtime.d.mts +47 -0
  102. package/dist/runtime/run-runtime.d.mts.map +1 -0
  103. package/dist/runtime/run-runtime.mjs +112 -0
  104. package/dist/runtime/run-runtime.mjs.map +1 -0
  105. package/dist/runtime/session-event-store.d.mts +75 -0
  106. package/dist/runtime/session-event-store.d.mts.map +1 -0
  107. package/dist/runtime/session-event-store.mjs +124 -0
  108. package/dist/runtime/session-event-store.mjs.map +1 -0
  109. package/dist/tools/index.d.mts +4 -0
  110. package/dist/tools/index.mjs +4 -0
  111. package/dist/tools/question.d.mts +21 -0
  112. package/dist/tools/question.d.mts.map +1 -0
  113. package/dist/tools/question.mjs +41 -0
  114. package/dist/tools/question.mjs.map +1 -0
  115. package/dist/tools/registry.d.mts +61 -0
  116. package/dist/tools/registry.d.mts.map +1 -0
  117. package/dist/tools/registry.mjs +113 -0
  118. package/dist/tools/registry.mjs.map +1 -0
  119. package/dist/tools/task.d.mts +34 -0
  120. package/dist/tools/task.d.mts.map +1 -0
  121. package/dist/tools/task.mjs +81 -0
  122. package/dist/tools/task.mjs.map +1 -0
  123. package/package.json +86 -0
  124. package/src/client/README.md +23 -0
  125. package/src/client/index.ts +43 -0
  126. package/src/client/state.ts +380 -0
  127. package/src/client/transport.ts +517 -0
  128. package/src/index.ts +2 -0
  129. package/src/loop/README.md +23 -0
  130. package/src/loop/accumulator.ts +71 -0
  131. package/src/loop/error.ts +105 -0
  132. package/src/loop/index.ts +35 -0
  133. package/src/loop/llm-event.ts +52 -0
  134. package/src/loop/run.ts +1237 -0
  135. package/src/loop/services/context-transformer.ts +24 -0
  136. package/src/loop/services/llm-provider.ts +20 -0
  137. package/src/loop/services/loop-config.ts +20 -0
  138. package/src/loop/services/tool-executor.ts +11 -0
  139. package/src/loop/testing/faux-provider.ts +94 -0
  140. package/src/loop/testing/index.ts +3 -0
  141. package/src/loop/testing/test-tool-executor.ts +28 -0
  142. package/src/protocol/README.md +24 -0
  143. package/src/protocol/capability.ts +29 -0
  144. package/src/protocol/content.ts +76 -0
  145. package/src/protocol/event.ts +286 -0
  146. package/src/protocol/index.ts +109 -0
  147. package/src/protocol/message.ts +86 -0
  148. package/src/protocol/reasoning.ts +4 -0
  149. package/src/protocol/session.ts +47 -0
  150. package/src/protocol/tool.ts +154 -0
  151. package/src/protocol/usage.ts +48 -0
  152. package/src/runtime/README.md +44 -0
  153. package/src/runtime/error.ts +70 -0
  154. package/src/runtime/index.ts +43 -0
  155. package/src/runtime/run-runtime.ts +307 -0
  156. package/src/runtime/session-event-store.ts +254 -0
  157. package/src/tools/README.md +22 -0
  158. package/src/tools/index.ts +29 -0
  159. package/src/tools/question.ts +58 -0
  160. package/src/tools/registry.ts +228 -0
  161. package/src/tools/task.ts +132 -0
@@ -0,0 +1,112 @@
1
+ import { runtimeErrorToAgentError } from "./error.mjs";
2
+ import { HitlResponseAppended, InputAppended, RunAwaitingInput, RunCompleted, RunFailed, RunStarted, SessionEventStore, replayRuntimeHitlResponses, replayRuntimeSessionEvents } from "./session-event-store.mjs";
3
+ import { Effect, Ref, Stream } from "effect";
4
+ import { run } from "@yolk-sdk/agent/loop";
5
+ //#region src/runtime/run-runtime.ts
6
+ const extractNewMessages = (event) => event._tag === "AgentEnd" ? event.messages : [];
7
+ const runtimeRunConfig = (config, messages) => ({
8
+ messages,
9
+ systemPrompt: config.systemPrompt,
10
+ tools: config.tools,
11
+ hitlResponses: config.hitlResponses,
12
+ reasoningEffort: config.reasoningEffort,
13
+ capabilities: config.capabilities,
14
+ model: config.model
15
+ });
16
+ const runAndCollectMessages = (config, messages, createdMessages) => run(runtimeRunConfig(config, messages)).pipe(Stream.tap((event) => {
17
+ const newMessages = extractNewMessages(event);
18
+ return newMessages.length === 0 ? Effect.void : Ref.update(createdMessages, (messages) => [...messages, ...newMessages]);
19
+ }));
20
+ const makeTranscriptRuntimeStream = (request, config) => Stream.unwrap(Ref.make([]).pipe(Effect.map((createdMessages) => runAndCollectMessages(config, request.messages, createdMessages))));
21
+ const emptyRuntimeSessionEventLog = (sessionId) => ({
22
+ sessionId,
23
+ revision: 0,
24
+ events: []
25
+ });
26
+ const loadAppendLogOrEmpty = (store, sessionId) => store.load(sessionId).pipe(Effect.catchTag("SessionNotFoundError", () => Effect.succeed(emptyRuntimeSessionEventLog(sessionId))));
27
+ const appendRunFailed = (store, request, revision, error) => store.append({
28
+ sessionId: request.sessionId,
29
+ expectedRevision: revision,
30
+ events: [RunFailed.make({
31
+ runId: request.runId,
32
+ error: runtimeErrorToAgentError(error)
33
+ })]
34
+ });
35
+ const makeAppendInputRuntimeStream = (request, config) => Stream.unwrap(Effect.gen(function* () {
36
+ const store = yield* SessionEventStore;
37
+ const initialLog = yield* loadAppendLogOrEmpty(store, request.sessionId);
38
+ const startedLog = yield* store.append({
39
+ sessionId: request.sessionId,
40
+ expectedRevision: request.expectedRevision ?? initialLog.revision,
41
+ events: [InputAppended.make({ message: request.input }), RunStarted.make({ runId: request.runId })]
42
+ });
43
+ return run(runtimeRunConfig(config, [...replayRuntimeSessionEvents(initialLog.events), request.input])).pipe(Stream.tap((event) => event._tag === "AgentEnd" ? store.append({
44
+ sessionId: request.sessionId,
45
+ expectedRevision: startedLog.revision,
46
+ events: [RunCompleted.make({
47
+ runId: request.runId,
48
+ messages: event.messages
49
+ })]
50
+ }).pipe(Effect.asVoid) : event._tag === "AgentAwaitingInput" ? store.append({
51
+ sessionId: request.sessionId,
52
+ expectedRevision: startedLog.revision,
53
+ events: [RunAwaitingInput.make({
54
+ runId: request.runId,
55
+ requests: event.requests,
56
+ messages: event.messages
57
+ })]
58
+ }).pipe(Effect.asVoid) : Effect.void), Stream.catchTags({
59
+ AbortError: (error) => Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(Stream.flatMap(() => Stream.fail(error))),
60
+ ContextTransformError: (error) => Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(Stream.flatMap(() => Stream.fail(error))),
61
+ FauxExhaustedError: (error) => Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(Stream.flatMap(() => Stream.fail(error))),
62
+ LLMError: (error) => Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(Stream.flatMap(() => Stream.fail(error))),
63
+ ToolError: (error) => Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(Stream.flatMap(() => Stream.fail(error)))
64
+ }));
65
+ }));
66
+ const makeAppendHitlResponseRuntimeStream = (request, config) => Stream.unwrap(Effect.gen(function* () {
67
+ const store = yield* SessionEventStore;
68
+ const initialLog = yield* loadAppendLogOrEmpty(store, request.sessionId);
69
+ const startedLog = yield* store.append({
70
+ sessionId: request.sessionId,
71
+ expectedRevision: request.expectedRevision ?? initialLog.revision,
72
+ events: [HitlResponseAppended.make({ response: request.response }), RunStarted.make({ runId: request.runId })]
73
+ });
74
+ const messages = replayRuntimeSessionEvents(initialLog.events);
75
+ const hitlResponses = [...replayRuntimeHitlResponses(initialLog.events), request.response];
76
+ return run(runtimeRunConfig({
77
+ ...config,
78
+ hitlResponses
79
+ }, messages)).pipe(Stream.tap((event) => event._tag === "AgentEnd" ? store.append({
80
+ sessionId: request.sessionId,
81
+ expectedRevision: startedLog.revision,
82
+ events: [RunCompleted.make({
83
+ runId: request.runId,
84
+ messages: event.messages
85
+ })]
86
+ }).pipe(Effect.asVoid) : event._tag === "AgentAwaitingInput" ? store.append({
87
+ sessionId: request.sessionId,
88
+ expectedRevision: startedLog.revision,
89
+ events: [RunAwaitingInput.make({
90
+ runId: request.runId,
91
+ requests: event.requests,
92
+ messages: event.messages
93
+ })]
94
+ }).pipe(Effect.asVoid) : Effect.void), Stream.catchTags({
95
+ AbortError: (error) => Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(Stream.flatMap(() => Stream.fail(error))),
96
+ ContextTransformError: (error) => Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(Stream.flatMap(() => Stream.fail(error))),
97
+ FauxExhaustedError: (error) => Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(Stream.flatMap(() => Stream.fail(error))),
98
+ LLMError: (error) => Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(Stream.flatMap(() => Stream.fail(error))),
99
+ ToolError: (error) => Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(Stream.flatMap(() => Stream.fail(error)))
100
+ }));
101
+ }));
102
+ function runRuntime(request, config) {
103
+ switch (request._tag) {
104
+ case "Transcript": return makeTranscriptRuntimeStream(request, config);
105
+ case "AppendInput": return makeAppendInputRuntimeStream(request, config);
106
+ case "AppendHitlResponse": return makeAppendHitlResponseRuntimeStream(request, config);
107
+ }
108
+ }
109
+ //#endregion
110
+ export { runRuntime };
111
+
112
+ //# sourceMappingURL=run-runtime.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run-runtime.mjs","names":[],"sources":["../../src/runtime/run-runtime.ts"],"sourcesContent":["import { Effect, Ref, Stream } from 'effect'\nimport type {\n AgentEvent,\n AgentMessage,\n AgentModelCapabilities,\n AgentReasoningEffort,\n HitlResponse,\n ToolDef\n} from '@yolk-sdk/agent/protocol'\nimport {\n run,\n type AgentLoopError,\n type ContextTransformer,\n type LLMProvider,\n type LoopConfig,\n type ToolExecutor\n} from '@yolk-sdk/agent/loop'\nimport { runtimeErrorToAgentError } from './error.ts'\nimport {\n HitlResponseAppended,\n InputAppended,\n replayRuntimeHitlResponses,\n replayRuntimeSessionEvents,\n RunAwaitingInput,\n RunCompleted,\n RunFailed,\n RunStarted,\n SessionEventStore,\n type RuntimeSessionEventLog,\n type SessionEventStoreApi,\n type SessionRevision\n} from './session-event-store.ts'\nimport type { RuntimeError } from './error.ts'\n\nexport type RuntimeTranscript = readonly [AgentMessage, ...Array<AgentMessage>]\n\nexport type RuntimeConfig = {\n readonly systemPrompt: string\n readonly tools: ReadonlyArray<ToolDef>\n readonly hitlResponses?: ReadonlyArray<HitlResponse>\n readonly model: string\n readonly reasoningEffort?: AgentReasoningEffort\n readonly capabilities?: AgentModelCapabilities\n}\n\nexport type TranscriptRuntimeRequest = {\n readonly _tag: 'Transcript'\n readonly sessionId: string\n readonly messages: RuntimeTranscript\n}\n\nexport type AppendInputRuntimeRequest = {\n readonly _tag: 'AppendInput'\n readonly sessionId: string\n readonly input: AgentMessage\n readonly runId: string\n readonly expectedRevision?: SessionRevision\n}\n\nexport type AppendHitlResponseRuntimeRequest = {\n readonly _tag: 'AppendHitlResponse'\n readonly sessionId: string\n readonly response: HitlResponse\n readonly runId: string\n readonly expectedRevision?: SessionRevision\n}\n\nexport type RuntimeRequest =\n | TranscriptRuntimeRequest\n | AppendInputRuntimeRequest\n | AppendHitlResponseRuntimeRequest\n\ntype LoopRequirements = ContextTransformer | LLMProvider | LoopConfig | ToolExecutor\ntype AppendRuntimeRequirements = LoopRequirements | SessionEventStore\ntype RuntimeRequirements = LoopRequirements | AppendRuntimeRequirements\ntype RuntimeErrorUnion = RuntimeError | AgentLoopError\n\nconst extractNewMessages = (event: AgentEvent) => (event._tag === 'AgentEnd' ? event.messages : [])\n\nconst runtimeRunConfig = (config: RuntimeConfig, messages: ReadonlyArray<AgentMessage>) => ({\n messages,\n systemPrompt: config.systemPrompt,\n tools: config.tools,\n hitlResponses: config.hitlResponses,\n reasoningEffort: config.reasoningEffort,\n capabilities: config.capabilities,\n model: config.model\n})\n\nconst runAndCollectMessages = (\n config: RuntimeConfig,\n messages: ReadonlyArray<AgentMessage>,\n createdMessages: Ref.Ref<ReadonlyArray<AgentMessage>>\n) =>\n run(runtimeRunConfig(config, messages)).pipe(\n Stream.tap(event => {\n const newMessages = extractNewMessages(event)\n\n return newMessages.length === 0\n ? Effect.void\n : Ref.update(createdMessages, messages => [...messages, ...newMessages])\n })\n )\n\nconst makeTranscriptRuntimeStream = (request: TranscriptRuntimeRequest, config: RuntimeConfig) =>\n Stream.unwrap(\n Ref.make<ReadonlyArray<AgentMessage>>([]).pipe(\n Effect.map(createdMessages =>\n runAndCollectMessages(config, request.messages, createdMessages)\n )\n )\n )\n\nconst emptyRuntimeSessionEventLog = (sessionId: string): RuntimeSessionEventLog => ({\n sessionId,\n revision: 0,\n events: []\n})\n\nconst loadAppendLogOrEmpty = (store: SessionEventStoreApi, sessionId: string) =>\n store\n .load(sessionId)\n .pipe(\n Effect.catchTag('SessionNotFoundError', () =>\n Effect.succeed(emptyRuntimeSessionEventLog(sessionId))\n )\n )\n\nconst appendRunFailed = (\n store: SessionEventStoreApi,\n request: AppendInputRuntimeRequest | AppendHitlResponseRuntimeRequest,\n revision: SessionRevision,\n error: AgentLoopError\n) =>\n store.append({\n sessionId: request.sessionId,\n expectedRevision: revision,\n events: [RunFailed.make({ runId: request.runId, error: runtimeErrorToAgentError(error) })]\n })\n\nconst makeAppendInputRuntimeStream = (request: AppendInputRuntimeRequest, config: RuntimeConfig) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const store = yield* SessionEventStore\n const initialLog = yield* loadAppendLogOrEmpty(store, request.sessionId)\n const startedLog = yield* store.append({\n sessionId: request.sessionId,\n expectedRevision: request.expectedRevision ?? initialLog.revision,\n events: [\n InputAppended.make({ message: request.input }),\n RunStarted.make({ runId: request.runId })\n ]\n })\n const messages = [...replayRuntimeSessionEvents(initialLog.events), request.input]\n return run(runtimeRunConfig(config, messages)).pipe(\n Stream.tap(event =>\n event._tag === 'AgentEnd'\n ? store\n .append({\n sessionId: request.sessionId,\n expectedRevision: startedLog.revision,\n events: [RunCompleted.make({ runId: request.runId, messages: event.messages })]\n })\n .pipe(Effect.asVoid)\n : event._tag === 'AgentAwaitingInput'\n ? store\n .append({\n sessionId: request.sessionId,\n expectedRevision: startedLog.revision,\n events: [\n RunAwaitingInput.make({\n runId: request.runId,\n requests: event.requests,\n messages: event.messages\n })\n ]\n })\n .pipe(Effect.asVoid)\n : Effect.void\n ),\n Stream.catchTags({\n AbortError: error =>\n Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(\n Stream.flatMap(() => Stream.fail(error))\n ),\n ContextTransformError: error =>\n Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(\n Stream.flatMap(() => Stream.fail(error))\n ),\n FauxExhaustedError: error =>\n Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(\n Stream.flatMap(() => Stream.fail(error))\n ),\n LLMError: error =>\n Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(\n Stream.flatMap(() => Stream.fail(error))\n ),\n ToolError: error =>\n Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(\n Stream.flatMap(() => Stream.fail(error))\n )\n })\n )\n })\n )\n\nconst makeAppendHitlResponseRuntimeStream = (\n request: AppendHitlResponseRuntimeRequest,\n config: RuntimeConfig\n) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const store = yield* SessionEventStore\n const initialLog = yield* loadAppendLogOrEmpty(store, request.sessionId)\n const startedLog = yield* store.append({\n sessionId: request.sessionId,\n expectedRevision: request.expectedRevision ?? initialLog.revision,\n events: [\n HitlResponseAppended.make({ response: request.response }),\n RunStarted.make({ runId: request.runId })\n ]\n })\n const messages = replayRuntimeSessionEvents(initialLog.events)\n const priorResponses = replayRuntimeHitlResponses(initialLog.events)\n const hitlResponses = [...priorResponses, request.response]\n\n return run(runtimeRunConfig({ ...config, hitlResponses }, messages)).pipe(\n Stream.tap(event =>\n event._tag === 'AgentEnd'\n ? store\n .append({\n sessionId: request.sessionId,\n expectedRevision: startedLog.revision,\n events: [RunCompleted.make({ runId: request.runId, messages: event.messages })]\n })\n .pipe(Effect.asVoid)\n : event._tag === 'AgentAwaitingInput'\n ? store\n .append({\n sessionId: request.sessionId,\n expectedRevision: startedLog.revision,\n events: [\n RunAwaitingInput.make({\n runId: request.runId,\n requests: event.requests,\n messages: event.messages\n })\n ]\n })\n .pipe(Effect.asVoid)\n : Effect.void\n ),\n Stream.catchTags({\n AbortError: error =>\n Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(\n Stream.flatMap(() => Stream.fail(error))\n ),\n ContextTransformError: error =>\n Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(\n Stream.flatMap(() => Stream.fail(error))\n ),\n FauxExhaustedError: error =>\n Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(\n Stream.flatMap(() => Stream.fail(error))\n ),\n LLMError: error =>\n Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(\n Stream.flatMap(() => Stream.fail(error))\n ),\n ToolError: error =>\n Stream.fromEffect(appendRunFailed(store, request, startedLog.revision, error)).pipe(\n Stream.flatMap(() => Stream.fail(error))\n )\n })\n )\n })\n )\n\nexport function runRuntime(\n request: TranscriptRuntimeRequest,\n config: RuntimeConfig\n): Stream.Stream<AgentEvent, AgentLoopError, LoopRequirements>\nexport function runRuntime(\n request: AppendInputRuntimeRequest,\n config: RuntimeConfig\n): Stream.Stream<AgentEvent, RuntimeErrorUnion, AppendRuntimeRequirements>\nexport function runRuntime(\n request: AppendHitlResponseRuntimeRequest,\n config: RuntimeConfig\n): Stream.Stream<AgentEvent, RuntimeErrorUnion, AppendRuntimeRequirements>\nexport function runRuntime(\n request: RuntimeRequest,\n config: RuntimeConfig\n): Stream.Stream<AgentEvent, RuntimeErrorUnion, RuntimeRequirements>\nexport function runRuntime(\n request: RuntimeRequest,\n config: RuntimeConfig\n): Stream.Stream<AgentEvent, RuntimeErrorUnion, RuntimeRequirements> {\n switch (request._tag) {\n case 'Transcript':\n return makeTranscriptRuntimeStream(request, config)\n case 'AppendInput':\n return makeAppendInputRuntimeStream(request, config)\n case 'AppendHitlResponse':\n return makeAppendHitlResponseRuntimeStream(request, config)\n }\n}\n"],"mappings":";;;;;AA6EA,MAAM,sBAAsB,UAAuB,MAAM,SAAS,aAAa,MAAM,WAAW,CAAC;AAEjG,MAAM,oBAAoB,QAAuB,cAA2C;CAC1F;CACA,cAAc,OAAO;CACrB,OAAO,OAAO;CACd,eAAe,OAAO;CACtB,iBAAiB,OAAO;CACxB,cAAc,OAAO;CACrB,OAAO,OAAO;AAChB;AAEA,MAAM,yBACJ,QACA,UACA,oBAEA,IAAI,iBAAiB,QAAQ,QAAQ,CAAC,EAAE,KACtC,OAAO,KAAI,UAAS;CAClB,MAAM,cAAc,mBAAmB,KAAK;CAE5C,OAAO,YAAY,WAAW,IAC1B,OAAO,OACP,IAAI,OAAO,kBAAiB,aAAY,CAAC,GAAG,UAAU,GAAG,WAAW,CAAC;AAC3E,CAAC,CACH;AAEF,MAAM,+BAA+B,SAAmC,WACtE,OAAO,OACL,IAAI,KAAkC,CAAC,CAAC,EAAE,KACxC,OAAO,KAAI,oBACT,sBAAsB,QAAQ,QAAQ,UAAU,eAAe,CACjE,CACF,CACF;AAEF,MAAM,+BAA+B,eAA+C;CAClF;CACA,UAAU;CACV,QAAQ,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SAAS,MAAM;EACpD,KAAK,eACH,OAAO,6BAA6B,SAAS,MAAM;EACrD,KAAK,sBACH,OAAO,oCAAoC,SAAS,MAAM;CAC9D;AACF"}
@@ -0,0 +1,75 @@
1
+ import { SessionConflictError, SessionLoadError, SessionNotFoundError, SessionSaveError } from "./error.mjs";
2
+ import { Context, Effect, Layer, Option } from "effect";
3
+ import * as Schema from "effect/Schema";
4
+ import { AgentError, AgentMessage, HitlResponse } from "@yolk-sdk/agent/protocol";
5
+
6
+ //#region src/runtime/session-event-store.d.ts
7
+ type SessionRevision = number;
8
+ declare const InputAppended_base: Schema.Class<InputAppended, Schema.TaggedStruct<"InputAppended", {
9
+ readonly message: Schema.Union<readonly [typeof import("@yolk-sdk/agent/protocol").UserMessage, typeof import("@yolk-sdk/agent/protocol").AssistantAgentMessage, typeof import("@yolk-sdk/agent/protocol").ToolResultMessage]>;
10
+ }>, {}>;
11
+ declare class InputAppended extends InputAppended_base {}
12
+ declare const HitlResponseAppended_base: Schema.Class<HitlResponseAppended, Schema.TaggedStruct<"HitlResponseAppended", {
13
+ readonly response: Schema.Union<readonly [typeof import("@yolk-sdk/agent/protocol").ToolApprovalResponse, typeof import("@yolk-sdk/agent/protocol").QuestionResponse]>;
14
+ }>, {}>;
15
+ declare class HitlResponseAppended extends HitlResponseAppended_base {}
16
+ declare const RunStarted_base: Schema.Class<RunStarted, Schema.TaggedStruct<"RunStarted", {
17
+ readonly runId: Schema.String;
18
+ }>, {}>;
19
+ declare class RunStarted extends RunStarted_base {}
20
+ declare const RunCompleted_base: Schema.Class<RunCompleted, Schema.TaggedStruct<"RunCompleted", {
21
+ readonly runId: Schema.String;
22
+ readonly messages: Schema.$Array<Schema.Union<readonly [typeof import("@yolk-sdk/agent/protocol").UserMessage, typeof import("@yolk-sdk/agent/protocol").AssistantAgentMessage, typeof import("@yolk-sdk/agent/protocol").ToolResultMessage]>>;
23
+ }>, {}>;
24
+ declare class RunCompleted extends RunCompleted_base {}
25
+ declare const RunAwaitingInput_base: Schema.Class<RunAwaitingInput, Schema.TaggedStruct<"RunAwaitingInput", {
26
+ readonly runId: Schema.String;
27
+ readonly requests: Schema.NonEmptyArray<Schema.Union<readonly [typeof import("@yolk-sdk/agent/protocol").ToolApprovalRequest, typeof import("@yolk-sdk/agent/protocol").QuestionRequest]>>;
28
+ readonly messages: Schema.$Array<Schema.Union<readonly [typeof import("@yolk-sdk/agent/protocol").UserMessage, typeof import("@yolk-sdk/agent/protocol").AssistantAgentMessage, typeof import("@yolk-sdk/agent/protocol").ToolResultMessage]>>;
29
+ }>, {}>;
30
+ declare class RunAwaitingInput extends RunAwaitingInput_base {}
31
+ declare const RunFailed_base: Schema.Class<RunFailed, Schema.TaggedStruct<"RunFailed", {
32
+ readonly runId: Schema.String;
33
+ readonly error: typeof AgentError;
34
+ }>, {}>;
35
+ declare class RunFailed extends RunFailed_base {}
36
+ declare const RunInterrupted_base: Schema.Class<RunInterrupted, Schema.TaggedStruct<"RunInterrupted", {
37
+ readonly runId: Schema.String;
38
+ }>, {}>;
39
+ declare class RunInterrupted extends RunInterrupted_base {}
40
+ declare const RuntimeSessionEvent: Schema.Union<readonly [typeof InputAppended, typeof HitlResponseAppended, typeof RunStarted, typeof RunCompleted, typeof RunAwaitingInput, typeof RunFailed, typeof RunInterrupted]>;
41
+ type RuntimeSessionEvent = typeof RuntimeSessionEvent.Type;
42
+ type StoredRuntimeSessionEvent = {
43
+ readonly id: string;
44
+ readonly sessionId: string;
45
+ readonly revision: SessionRevision;
46
+ readonly event: RuntimeSessionEvent;
47
+ };
48
+ type RuntimeSessionEventLog = {
49
+ readonly sessionId: string;
50
+ readonly revision: SessionRevision;
51
+ readonly events: ReadonlyArray<StoredRuntimeSessionEvent>;
52
+ };
53
+ type IncompleteRuntimeRun = {
54
+ readonly runId: string;
55
+ readonly startedRevision: SessionRevision;
56
+ };
57
+ type AppendRuntimeSessionEventsInput = {
58
+ readonly sessionId: string;
59
+ readonly expectedRevision?: SessionRevision;
60
+ readonly events: ReadonlyArray<RuntimeSessionEvent>;
61
+ };
62
+ type SessionEventStoreApi = {
63
+ readonly load: (sessionId: string) => Effect.Effect<RuntimeSessionEventLog, SessionNotFoundError | SessionLoadError>;
64
+ readonly append: (input: AppendRuntimeSessionEventsInput) => Effect.Effect<RuntimeSessionEventLog, SessionSaveError | SessionConflictError>;
65
+ };
66
+ declare const SessionEventStore_base: Context.ServiceClass<SessionEventStore, "@yolk-sdk/agent/runtime/SessionEventStore", SessionEventStoreApi>;
67
+ declare class SessionEventStore extends SessionEventStore_base {}
68
+ declare const replayRuntimeSessionEvents: (events: ReadonlyArray<StoredRuntimeSessionEvent>) => ReadonlyArray<AgentMessage>;
69
+ declare const replayRuntimeHitlResponses: (events: ReadonlyArray<StoredRuntimeSessionEvent>) => ReadonlyArray<HitlResponse>;
70
+ declare const latestIncompleteRuntimeRun: (events: ReadonlyArray<StoredRuntimeSessionEvent>) => Option.Option<IncompleteRuntimeRun>;
71
+ declare const appendRuntimeSessionEventsToLog: (current: RuntimeSessionEventLog, input: AppendRuntimeSessionEventsInput) => RuntimeSessionEventLog;
72
+ declare const makeInMemorySessionEventStoreLayer: (initial?: ReadonlyArray<RuntimeSessionEventLog>) => Layer.Layer<SessionEventStore, never, never>;
73
+ //#endregion
74
+ export { AppendRuntimeSessionEventsInput, HitlResponseAppended, IncompleteRuntimeRun, InputAppended, RunAwaitingInput, RunCompleted, RunFailed, RunInterrupted, RunStarted, RuntimeSessionEvent, RuntimeSessionEventLog, SessionEventStore, SessionEventStoreApi, SessionRevision, StoredRuntimeSessionEvent, appendRuntimeSessionEventsToLog, latestIncompleteRuntimeRun, makeInMemorySessionEventStoreLayer, replayRuntimeHitlResponses, replayRuntimeSessionEvents };
75
+ //# sourceMappingURL=session-event-store.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session-event-store.d.mts","names":[],"sources":["../../src/runtime/session-event-store.ts"],"mappings":";;;;;;KAMY,eAAA;AAAA,cAAwB,kBAAA;;;cAEvB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MAAA,EAAQ,aAAA,CAAc,yBAAA,MACrB,aAAA,CAAc,YAAA;AAAA,cAgBJ,0BAAA,GACX,MAAA,EAAQ,aAAA,CAAc,yBAAA,MACrB,aAAA,CAAc,YAAA;AAAA,cA6BJ,0BAAA,GACX,MAAA,EAAQ,aAAA,CAAc,yBAAA,MACrB,MAAA,CAAO,MAAA,CAAO,oBAAA;AAAA,cAwDJ,+BAAA,GACX,OAAA,EAAS,sBAAA,EACT,KAAA,EAAO,+BAAA,KACN,sBAAA;AAAA,cAWU,kCAAA,GACX,OAAA,GAAS,aAAA,CAAc,sBAAA,MAA4B,KAAA,CAAA,KAAA,CAAA,iBAAA"}
@@ -0,0 +1,124 @@
1
+ import { SessionConflictError, SessionNotFoundError } from "./error.mjs";
2
+ import { Context, Effect, Layer, Option, Ref } from "effect";
3
+ import * as Schema from "effect/Schema";
4
+ import { AgentError, AgentMessage, HitlRequest, HitlResponse } from "@yolk-sdk/agent/protocol";
5
+ //#region src/runtime/session-event-store.ts
6
+ var InputAppended = class extends Schema.TaggedClass()("InputAppended", { message: AgentMessage }) {};
7
+ var HitlResponseAppended = class extends Schema.TaggedClass()("HitlResponseAppended", { response: HitlResponse }) {};
8
+ var RunStarted = class extends Schema.TaggedClass()("RunStarted", { runId: Schema.String }) {};
9
+ var RunCompleted = class extends Schema.TaggedClass()("RunCompleted", {
10
+ runId: Schema.String,
11
+ messages: Schema.Array(AgentMessage)
12
+ }) {};
13
+ var RunAwaitingInput = class extends Schema.TaggedClass()("RunAwaitingInput", {
14
+ runId: Schema.String,
15
+ requests: Schema.NonEmptyArray(HitlRequest),
16
+ messages: Schema.Array(AgentMessage)
17
+ }) {};
18
+ var RunFailed = class extends Schema.TaggedClass()("RunFailed", {
19
+ runId: Schema.String,
20
+ error: AgentError
21
+ }) {};
22
+ var RunInterrupted = class extends Schema.TaggedClass()("RunInterrupted", { runId: Schema.String }) {};
23
+ const RuntimeSessionEvent = Schema.Union([
24
+ InputAppended,
25
+ HitlResponseAppended,
26
+ RunStarted,
27
+ RunCompleted,
28
+ RunAwaitingInput,
29
+ RunFailed,
30
+ RunInterrupted
31
+ ]);
32
+ var SessionEventStore = class extends Context.Service()("@yolk-sdk/agent/runtime/SessionEventStore") {};
33
+ const replayRuntimeSessionEvents = (events) => events.flatMap((stored) => {
34
+ switch (stored.event._tag) {
35
+ case "InputAppended": return [stored.event.message];
36
+ case "RunCompleted":
37
+ case "RunAwaitingInput": return stored.event.messages;
38
+ case "HitlResponseAppended":
39
+ case "RunFailed":
40
+ case "RunInterrupted":
41
+ case "RunStarted": return [];
42
+ }
43
+ });
44
+ const replayRuntimeHitlResponses = (events) => events.flatMap((stored) => stored.event._tag === "HitlResponseAppended" ? [stored.event.response] : []);
45
+ const terminalRunId = (event) => {
46
+ switch (event._tag) {
47
+ case "RunCompleted":
48
+ case "RunAwaitingInput":
49
+ case "RunFailed":
50
+ case "RunInterrupted": return Option.some(event.runId);
51
+ case "HitlResponseAppended":
52
+ case "InputAppended":
53
+ case "RunStarted": return Option.none();
54
+ }
55
+ };
56
+ const latestIncompleteRuntimeRun = (events) => {
57
+ const search = events.reduceRight((state, stored) => {
58
+ if (state._tag === "Found") return state;
59
+ const terminal = terminalRunId(stored.event);
60
+ if (Option.isSome(terminal)) return {
61
+ _tag: "Searching",
62
+ terminalRunIds: new Set([...state.terminalRunIds, terminal.value])
63
+ };
64
+ if (stored.event._tag === "RunStarted" && !state.terminalRunIds.has(stored.event.runId)) return {
65
+ _tag: "Found",
66
+ run: {
67
+ runId: stored.event.runId,
68
+ startedRevision: stored.revision
69
+ }
70
+ };
71
+ return state;
72
+ }, {
73
+ _tag: "Searching",
74
+ terminalRunIds: /* @__PURE__ */ new Set()
75
+ });
76
+ return search._tag === "Found" ? Option.some(search.run) : Option.none();
77
+ };
78
+ const emptyLog = (sessionId) => ({
79
+ sessionId,
80
+ revision: 0,
81
+ events: []
82
+ });
83
+ const makeStoredRuntimeSessionEvents = (sessionId, currentRevision, events) => events.map((event, index) => {
84
+ const revision = currentRevision + index + 1;
85
+ return {
86
+ id: `${sessionId}:${revision}`,
87
+ sessionId,
88
+ revision,
89
+ event
90
+ };
91
+ });
92
+ const appendRuntimeSessionEventsToLog = (current, input) => {
93
+ const stored = makeStoredRuntimeSessionEvents(input.sessionId, current.revision, input.events);
94
+ const last = stored.at(-1);
95
+ return {
96
+ sessionId: input.sessionId,
97
+ revision: last?.revision ?? current.revision,
98
+ events: [...current.events, ...stored]
99
+ };
100
+ };
101
+ const makeInMemorySessionEventStoreLayer = (initial = []) => Layer.effect(SessionEventStore, Effect.gen(function* () {
102
+ const logs = yield* Ref.make(new Map(initial.map((log) => [log.sessionId, log])));
103
+ return SessionEventStore.of({
104
+ load: (sessionId) => Effect.gen(function* () {
105
+ const current = yield* Ref.get(logs);
106
+ return yield* Effect.fromNullishOr(current.get(sessionId)).pipe(Effect.mapError(() => new SessionNotFoundError({ sessionId })));
107
+ }),
108
+ append: (input) => Effect.gen(function* () {
109
+ const current = yield* Ref.get(logs);
110
+ const currentLog = current.get(input.sessionId) ?? emptyLog(input.sessionId);
111
+ if (input.expectedRevision !== void 0 && input.expectedRevision !== currentLog.revision) return yield* Effect.fail(new SessionConflictError({
112
+ sessionId: input.sessionId,
113
+ message: `Session revision conflict: expected ${input.expectedRevision}, got ${currentLog.revision}`
114
+ }));
115
+ const nextLog = appendRuntimeSessionEventsToLog(currentLog, input);
116
+ yield* Ref.set(logs, new Map([...current, [input.sessionId, nextLog]]));
117
+ return nextLog;
118
+ })
119
+ });
120
+ }));
121
+ //#endregion
122
+ export { HitlResponseAppended, InputAppended, RunAwaitingInput, RunCompleted, RunFailed, RunInterrupted, RunStarted, RuntimeSessionEvent, SessionEventStore, appendRuntimeSessionEventsToLog, latestIncompleteRuntimeRun, makeInMemorySessionEventStoreLayer, replayRuntimeHitlResponses, replayRuntimeSessionEvents };
123
+
124
+ //# sourceMappingURL=session-event-store.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session-event-store.mjs","names":[],"sources":["../../src/runtime/session-event-store.ts"],"sourcesContent":["import { Context, Effect, Layer, Option, Ref } from 'effect'\nimport * as Schema from 'effect/Schema'\nimport { AgentError, AgentMessage, HitlRequest, HitlResponse } from '@yolk-sdk/agent/protocol'\nimport { SessionConflictError, SessionNotFoundError } from './error.ts'\nimport type { SessionLoadError, SessionSaveError } from './error.ts'\n\nexport type SessionRevision = number\n\nexport class InputAppended extends Schema.TaggedClass<InputAppended>()('InputAppended', {\n message: AgentMessage\n}) {}\n\nexport class HitlResponseAppended extends Schema.TaggedClass<HitlResponseAppended>()(\n 'HitlResponseAppended',\n {\n response: HitlResponse\n }\n) {}\n\nexport class RunStarted extends Schema.TaggedClass<RunStarted>()('RunStarted', {\n runId: Schema.String\n}) {}\n\nexport class RunCompleted extends Schema.TaggedClass<RunCompleted>()('RunCompleted', {\n runId: Schema.String,\n messages: Schema.Array(AgentMessage)\n}) {}\n\nexport class RunAwaitingInput extends Schema.TaggedClass<RunAwaitingInput>()('RunAwaitingInput', {\n runId: Schema.String,\n requests: Schema.NonEmptyArray(HitlRequest),\n messages: Schema.Array(AgentMessage)\n}) {}\n\nexport class RunFailed extends Schema.TaggedClass<RunFailed>()('RunFailed', {\n runId: Schema.String,\n error: AgentError\n}) {}\n\nexport class RunInterrupted extends Schema.TaggedClass<RunInterrupted>()('RunInterrupted', {\n runId: Schema.String\n}) {}\n\nexport const RuntimeSessionEvent = Schema.Union([\n InputAppended,\n HitlResponseAppended,\n RunStarted,\n RunCompleted,\n RunAwaitingInput,\n RunFailed,\n RunInterrupted\n])\nexport type RuntimeSessionEvent = typeof RuntimeSessionEvent.Type\n\nexport type StoredRuntimeSessionEvent = {\n readonly id: string\n readonly sessionId: string\n readonly revision: SessionRevision\n readonly event: RuntimeSessionEvent\n}\n\nexport type RuntimeSessionEventLog = {\n readonly sessionId: string\n readonly revision: SessionRevision\n readonly events: ReadonlyArray<StoredRuntimeSessionEvent>\n}\n\nexport type IncompleteRuntimeRun = {\n readonly runId: string\n readonly startedRevision: SessionRevision\n}\n\nexport type AppendRuntimeSessionEventsInput = {\n readonly sessionId: string\n readonly expectedRevision?: SessionRevision\n readonly events: ReadonlyArray<RuntimeSessionEvent>\n}\n\nexport type SessionEventStoreApi = {\n readonly load: (\n sessionId: string\n ) => Effect.Effect<RuntimeSessionEventLog, SessionNotFoundError | SessionLoadError>\n readonly append: (\n input: AppendRuntimeSessionEventsInput\n ) => Effect.Effect<RuntimeSessionEventLog, SessionSaveError | SessionConflictError>\n}\n\nexport class SessionEventStore extends Context.Service<SessionEventStore, SessionEventStoreApi>()(\n '@yolk-sdk/agent/runtime/SessionEventStore'\n) {}\n\nexport const replayRuntimeSessionEvents = (\n events: ReadonlyArray<StoredRuntimeSessionEvent>\n): ReadonlyArray<AgentMessage> =>\n events.flatMap(stored => {\n switch (stored.event._tag) {\n case 'InputAppended':\n return [stored.event.message]\n case 'RunCompleted':\n case 'RunAwaitingInput':\n return stored.event.messages\n case 'HitlResponseAppended':\n case 'RunFailed':\n case 'RunInterrupted':\n case 'RunStarted':\n return []\n }\n })\n\nexport const replayRuntimeHitlResponses = (\n events: ReadonlyArray<StoredRuntimeSessionEvent>\n): ReadonlyArray<HitlResponse> =>\n events.flatMap(stored =>\n stored.event._tag === 'HitlResponseAppended' ? [stored.event.response] : []\n )\n\ntype IncompleteRunSearch =\n | {\n readonly _tag: 'Found'\n readonly run: IncompleteRuntimeRun\n }\n | {\n readonly _tag: 'Searching'\n readonly terminalRunIds: ReadonlySet<string>\n }\n\nconst terminalRunId = (event: RuntimeSessionEvent): Option.Option<string> => {\n switch (event._tag) {\n case 'RunCompleted':\n case 'RunAwaitingInput':\n case 'RunFailed':\n case 'RunInterrupted':\n return Option.some(event.runId)\n case 'HitlResponseAppended':\n case 'InputAppended':\n case 'RunStarted':\n return Option.none()\n }\n}\n\nexport const latestIncompleteRuntimeRun = (\n events: ReadonlyArray<StoredRuntimeSessionEvent>\n): Option.Option<IncompleteRuntimeRun> => {\n const search = events.reduceRight<IncompleteRunSearch>(\n (state, stored) => {\n if (state._tag === 'Found') {\n return state\n }\n\n const terminal = terminalRunId(stored.event)\n\n if (Option.isSome(terminal)) {\n return {\n _tag: 'Searching',\n terminalRunIds: new Set([...state.terminalRunIds, terminal.value])\n }\n }\n\n if (stored.event._tag === 'RunStarted' && !state.terminalRunIds.has(stored.event.runId)) {\n return {\n _tag: 'Found',\n run: {\n runId: stored.event.runId,\n startedRevision: stored.revision\n }\n }\n }\n\n return state\n },\n { _tag: 'Searching', terminalRunIds: new Set() }\n )\n\n return search._tag === 'Found' ? Option.some(search.run) : Option.none()\n}\n\nconst emptyLog = (sessionId: string): RuntimeSessionEventLog => ({\n sessionId,\n revision: 0,\n events: []\n})\n\nconst makeStoredRuntimeSessionEvents = (\n sessionId: string,\n currentRevision: SessionRevision,\n events: ReadonlyArray<RuntimeSessionEvent>\n): ReadonlyArray<StoredRuntimeSessionEvent> =>\n events.map((event, index) => {\n const revision = currentRevision + index + 1\n\n return {\n id: `${sessionId}:${revision}`,\n sessionId,\n revision,\n event\n }\n })\n\nexport const appendRuntimeSessionEventsToLog = (\n current: RuntimeSessionEventLog,\n input: AppendRuntimeSessionEventsInput\n): RuntimeSessionEventLog => {\n const stored = makeStoredRuntimeSessionEvents(input.sessionId, current.revision, input.events)\n const last = stored.at(-1)\n\n return {\n sessionId: input.sessionId,\n revision: last?.revision ?? current.revision,\n events: [...current.events, ...stored]\n }\n}\n\nexport const makeInMemorySessionEventStoreLayer = (\n initial: ReadonlyArray<RuntimeSessionEventLog> = []\n) =>\n Layer.effect(\n SessionEventStore,\n Effect.gen(function* () {\n const logs = yield* Ref.make(new Map(initial.map(log => [log.sessionId, log])))\n\n return SessionEventStore.of({\n load: sessionId =>\n Effect.gen(function* () {\n const current = yield* Ref.get(logs)\n\n return yield* Effect.fromNullishOr(current.get(sessionId)).pipe(\n Effect.mapError(() => new SessionNotFoundError({ sessionId }))\n )\n }),\n append: input =>\n Effect.gen(function* () {\n const current = yield* Ref.get(logs)\n const currentLog = current.get(input.sessionId) ?? emptyLog(input.sessionId)\n\n if (\n input.expectedRevision !== undefined &&\n input.expectedRevision !== currentLog.revision\n ) {\n return yield* Effect.fail(\n new SessionConflictError({\n sessionId: input.sessionId,\n message: `Session revision conflict: expected ${input.expectedRevision}, got ${currentLog.revision}`\n })\n )\n }\n\n const nextLog = appendRuntimeSessionEventsToLog(currentLog, input)\n yield* Ref.set(logs, new Map([...current, [input.sessionId, nextLog]]))\n\n return nextLog\n })\n })\n })\n )\n"],"mappings":";;;;;AAQA,IAAa,gBAAb,cAAmC,OAAO,YAA2B,EAAE,iBAAiB,EACtF,SAAS,aACX,CAAC,EAAE,CAAC;AAEJ,IAAa,uBAAb,cAA0C,OAAO,YAAkC,EACjF,wBACA,EACE,UAAU,aACZ,CACF,EAAE,CAAC;AAEH,IAAa,aAAb,cAAgC,OAAO,YAAwB,EAAE,cAAc,EAC7E,OAAO,OAAO,OAChB,CAAC,EAAE,CAAC;AAEJ,IAAa,eAAb,cAAkC,OAAO,YAA0B,EAAE,gBAAgB;CACnF,OAAO,OAAO;CACd,UAAU,OAAO,MAAM,YAAY;AACrC,CAAC,EAAE,CAAC;AAEJ,IAAa,mBAAb,cAAsC,OAAO,YAA8B,EAAE,oBAAoB;CAC/F,OAAO,OAAO;CACd,UAAU,OAAO,cAAc,WAAW;CAC1C,UAAU,OAAO,MAAM,YAAY;AACrC,CAAC,EAAE,CAAC;AAEJ,IAAa,YAAb,cAA+B,OAAO,YAAuB,EAAE,aAAa;CAC1E,OAAO,OAAO;CACd,OAAO;AACT,CAAC,EAAE,CAAC;AAEJ,IAAa,iBAAb,cAAoC,OAAO,YAA4B,EAAE,kBAAkB,EACzF,OAAO,OAAO,OAChB,CAAC,EAAE,CAAC;AAEJ,MAAa,sBAAsB,OAAO,MAAM;CAC9C;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;AAoCD,IAAa,oBAAb,cAAuC,QAAQ,QAAiD,EAC9F,2CACF,EAAE,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I,IAAI,MAAM,IAAI,IAAI,CAAC,GAAG,SAAS,CAAC,MAAM,WAAW,OAAO,CAAC,CAAC,CAAC;GAEtE,OAAO;EACT,CAAC;CACL,CAAC;AACH,CAAC,CACH"}
@@ -0,0 +1,4 @@
1
+ import { EmptyToolParams, ResolvedToolSet, SchemaToolExecutionInput, ToolAccess, ToolExecutionInput, ToolMetadata, ToolModule, ToolRegistration, ToolRegistryError, makeTool, makeToolExecutorLayer, resolveTools } from "./registry.mjs";
2
+ import { QuestionExecutionInput, QuestionToolOptions, makeQuestionToolDef, makeQuestionToolModule, makeQuestionToolRegistration, questionToolName } from "./question.mjs";
3
+ import { TaskExecutionInput, TaskSubagentDefinition, TaskToolOptions, TaskToolParams, formatTaskResult, makeTaskToolDef, makeTaskToolModule, makeTaskToolRegistration, taskToolName } from "./task.mjs";
4
+ export { EmptyToolParams, type QuestionExecutionInput, type QuestionToolOptions, type ResolvedToolSet, type SchemaToolExecutionInput, type TaskExecutionInput, type TaskSubagentDefinition, type TaskToolOptions, type TaskToolParams, ToolAccess, type ToolExecutionInput, type ToolMetadata, type ToolModule, type ToolRegistration, ToolRegistryError, formatTaskResult, makeQuestionToolDef, makeQuestionToolModule, makeQuestionToolRegistration, makeTaskToolDef, makeTaskToolModule, makeTaskToolRegistration, makeTool, makeToolExecutorLayer, questionToolName, resolveTools, taskToolName };
@@ -0,0 +1,4 @@
1
+ import { EmptyToolParams, ToolAccess, ToolRegistryError, makeTool, makeToolExecutorLayer, resolveTools } from "./registry.mjs";
2
+ import { makeQuestionToolDef, makeQuestionToolModule, makeQuestionToolRegistration, questionToolName } from "./question.mjs";
3
+ import { formatTaskResult, makeTaskToolDef, makeTaskToolModule, makeTaskToolRegistration, taskToolName } from "./task.mjs";
4
+ export { EmptyToolParams, ToolAccess, ToolRegistryError, formatTaskResult, makeQuestionToolDef, makeQuestionToolModule, makeQuestionToolRegistration, makeTaskToolDef, makeTaskToolModule, makeTaskToolRegistration, makeTool, makeToolExecutorLayer, questionToolName, resolveTools, taskToolName };
@@ -0,0 +1,21 @@
1
+ import { ToolModule, ToolRegistration } from "./registry.mjs";
2
+ import { Effect } from "effect";
3
+ import { QuestionToolParams, ToolCall, ToolResult } from "@yolk-sdk/agent/protocol";
4
+ import { ToolError } from "@yolk-sdk/agent/loop";
5
+
6
+ //#region src/tools/question.d.ts
7
+ declare const questionToolName = "question";
8
+ type QuestionExecutionInput<Context> = {
9
+ readonly call: ToolCall;
10
+ readonly context: Context;
11
+ readonly params: QuestionToolParams;
12
+ };
13
+ type QuestionToolOptions<Context> = {
14
+ readonly execute: (input: QuestionExecutionInput<Context>) => Effect.Effect<ToolResult, ToolError>;
15
+ };
16
+ declare const makeQuestionToolRegistration: <Context>(options: QuestionToolOptions<Context>) => ToolRegistration<Context>;
17
+ declare const makeQuestionToolDef: () => import("@yolk-sdk/agent/protocol").ToolDef;
18
+ declare const makeQuestionToolModule: <Context>(options: QuestionToolOptions<Context>) => ToolModule<Context>;
19
+ //#endregion
20
+ export { QuestionExecutionInput, QuestionToolOptions, makeQuestionToolDef, makeQuestionToolModule, makeQuestionToolRegistration, questionToolName };
21
+ //# sourceMappingURL=question.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"question.d.mts","names":[],"sources":["../../src/tools/question.ts"],"mappings":";;;;;;cAKa,gBAAA;AAAA,KAED,sBAAA;EAAA,SACD,IAAA,EAAM,QAAA;EAAA,SACN,OAAA,EAAS,OAAA;EAAA,SACT,MAAA,EAAQ,kBAAA;AAAA;AAAA,KAGP,mBAAA;EAAA,SACD,OAAA,GAAU,KAAA,EAAO,sBAAA,CAAuB,OAAA,MAAa,MAAA,CAAO,MAAA,CAAO,UAAA,EAAY,SAAA;AAAA;AAAA,cAiB7E,4BAAA,YACX,OAAA,EAAS,mBAAA,CAAoB,OAAA,MAC5B,gBAAA,CAAiB,OAAA;AAAA,cAcP,mBAAA,2CAAmB,OAGxB;AAAA,cAEK,sBAAA,YACX,OAAA,EAAS,mBAAA,CAAoB,OAAA,MAC5B,UAAA,CAAW,OAAA"}
@@ -0,0 +1,41 @@
1
+ import { makeTool } from "./registry.mjs";
2
+ import { Effect } from "effect";
3
+ import { QuestionToolParams, ToolResult } from "@yolk-sdk/agent/protocol";
4
+ import { ToolError } from "@yolk-sdk/agent/loop";
5
+ //#region src/tools/question.ts
6
+ const questionToolName = "question";
7
+ const questionToolError = (message, cause) => new ToolError({
8
+ tool: questionToolName,
9
+ message,
10
+ cause
11
+ });
12
+ const questionToolDescription = [
13
+ "Ask the user one or more structured questions and wait for their response.",
14
+ "Use this when you need explicit user input before continuing.",
15
+ "Each question has a stable id, prompt, optional options, and optional custom-answer support.",
16
+ "Yolk returns user answers as structured tool output."
17
+ ].join("\n\n");
18
+ const makeQuestionToolRegistration = (options) => makeTool({
19
+ name: questionToolName,
20
+ description: questionToolDescription,
21
+ parameters: QuestionToolParams,
22
+ access: "read",
23
+ invalidParamsMessage: (error) => `Invalid question arguments: ${error instanceof Error ? error.message : String(error)}`,
24
+ execute: ({ call, context, params }) => call.name === "question" ? options.execute({
25
+ call,
26
+ context,
27
+ params
28
+ }) : Effect.fail(questionToolError(`Tool is not configured: ${call.name}`, "not_found"))
29
+ });
30
+ const makeQuestionToolDef = () => makeQuestionToolRegistration({ execute: ({ call }) => Effect.succeed(ToolResult.make({
31
+ toolCallId: call.id,
32
+ content: ""
33
+ })) }).def;
34
+ const makeQuestionToolModule = (options) => ({
35
+ id: "question",
36
+ tools: [makeQuestionToolRegistration(options)]
37
+ });
38
+ //#endregion
39
+ export { makeQuestionToolDef, makeQuestionToolModule, makeQuestionToolRegistration, questionToolName };
40
+
41
+ //# sourceMappingURL=question.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"question.mjs","names":[],"sources":["../../src/tools/question.ts"],"sourcesContent":["import { Effect } from 'effect'\nimport { ToolError } from '@yolk-sdk/agent/loop'\nimport { QuestionToolParams, ToolResult, type ToolCall } from '@yolk-sdk/agent/protocol'\nimport { makeTool, type ToolModule, type ToolRegistration } from './registry.ts'\n\nexport const questionToolName = 'question'\n\nexport type QuestionExecutionInput<Context> = {\n readonly call: ToolCall\n readonly context: Context\n readonly params: QuestionToolParams\n}\n\nexport type QuestionToolOptions<Context> = {\n readonly execute: (input: QuestionExecutionInput<Context>) => Effect.Effect<ToolResult, ToolError>\n}\n\nconst questionToolError = (message: string, cause: ToolError['cause']) =>\n new ToolError({\n tool: questionToolName,\n message,\n cause\n })\n\nconst questionToolDescription = [\n 'Ask the user one or more structured questions and wait for their response.',\n 'Use this when you need explicit user input before continuing.',\n 'Each question has a stable id, prompt, optional options, and optional custom-answer support.',\n 'Yolk returns user answers as structured tool output.'\n].join('\\n\\n')\n\nexport const makeQuestionToolRegistration = <Context>(\n options: QuestionToolOptions<Context>\n): ToolRegistration<Context> =>\n makeTool({\n name: questionToolName,\n description: questionToolDescription,\n parameters: QuestionToolParams,\n access: 'read',\n invalidParamsMessage: error =>\n `Invalid question arguments: ${error instanceof Error ? error.message : String(error)}`,\n execute: ({ call, context, params }) =>\n call.name === questionToolName\n ? options.execute({ call, context, params })\n : Effect.fail(questionToolError(`Tool is not configured: ${call.name}`, 'not_found'))\n })\n\nexport const makeQuestionToolDef = () =>\n makeQuestionToolRegistration({\n execute: ({ call }) => Effect.succeed(ToolResult.make({ toolCallId: call.id, content: '' }))\n }).def\n\nexport const makeQuestionToolModule = <Context>(\n options: QuestionToolOptions<Context>\n): ToolModule<Context> => ({\n id: 'question',\n tools: [makeQuestionToolRegistration(options)]\n})\n"],"mappings":";;;;;AAKA,MAAa,mBAAmB;AAYhC,MAAM,qBAAqB,SAAiB,UAC1C,IAAI,UAAU;CACZ,MAAM;CACN;CACA;AACF,CAAC;AAEH,MAAM,0BAA0B;CAC9B;CACA;CACA;CACA;AACF,EAAE,KAAK,MAAM;AAEb,MAAa,gCACX,YAEA,SAAS;CACP,MAAM;CACN,aAAa;CACb,YAAY;CACZ,QAAQ;CACR,uBAAsB,UACpB,+BAA+B,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;CACtF,UAAU,EAAE,MAAM,SAAS,aACzB,KAAK,SAAA,aACD,QAAQ,QAAQ;EAAE;EAAM;EAAS;CAAO,CAAC,IACzC,OAAO,KAAK,kBAAkB,2BAA2B,KAAK,QAAQ,WAAW,CAAC;AAC1F,CAAC;AAEH,MAAa,4BACX,6BAA6B,EAC3B,UAAU,EAAE,WAAW,OAAO,QAAQ,WAAW,KAAK;CAAE,YAAY,KAAK;CAAI,SAAS;AAAG,CAAC,CAAC,EAC7F,CAAC,EAAE;AAEL,MAAa,0BACX,aACyB;CACzB,IAAI;CACJ,OAAO,CAAC,6BAA6B,OAAO,CAAC;AAC/C"}
@@ -0,0 +1,61 @@
1
+ import { Effect, Layer } from "effect";
2
+ import * as Schema from "effect/Schema";
3
+ import { ToolApprovalPolicy, ToolCall, ToolDef, ToolResult } from "@yolk-sdk/agent/protocol";
4
+ import { ToolError, ToolExecutor } from "@yolk-sdk/agent/loop";
5
+
6
+ //#region src/tools/registry.d.ts
7
+ declare const ToolAccess: Schema.Literals<readonly ["read", "write", "destructive"]>;
8
+ type ToolAccess = typeof ToolAccess.Type;
9
+ declare const ToolRegistryError_base: Schema.Class<ToolRegistryError, Schema.TaggedStruct<"ToolRegistryError", {
10
+ readonly message: Schema.String;
11
+ readonly cause: Schema.Literals<readonly ["duplicate_tool"]>;
12
+ }>, import("effect/Cause").YieldableError>;
13
+ declare class ToolRegistryError extends ToolRegistryError_base {}
14
+ type ToolExecutionInput<Context> = {
15
+ readonly call: ToolCall;
16
+ readonly context: Context;
17
+ };
18
+ type SchemaToolExecutionInput<Context, Params> = ToolExecutionInput<Context> & {
19
+ readonly params: Params;
20
+ };
21
+ type ToolRegistration<Context> = {
22
+ readonly def: ToolDef;
23
+ readonly access: ToolAccess;
24
+ readonly approval?: ToolApprovalPolicy;
25
+ readonly isEnabled?: (context: Context) => Effect.Effect<boolean, ToolRegistryError>;
26
+ readonly execute: (input: ToolExecutionInput<Context>) => Effect.Effect<ToolResult, ToolError>;
27
+ };
28
+ type ToolParamsSchema = Schema.Schema<unknown> & {
29
+ readonly DecodingServices: never;
30
+ };
31
+ declare const EmptyToolParams: Schema.$Record<Schema.String, Schema.Never>;
32
+ type MakeToolOptions<Context, ParamsSchema extends ToolParamsSchema> = {
33
+ readonly name: string;
34
+ readonly description: string;
35
+ readonly parameters: ParamsSchema;
36
+ readonly access: ToolAccess;
37
+ readonly approval?: ToolApprovalPolicy;
38
+ readonly isEnabled?: (context: Context) => Effect.Effect<boolean, ToolRegistryError>;
39
+ readonly invalidParamsMessage?: (error: unknown) => string;
40
+ readonly execute: (input: SchemaToolExecutionInput<Context, ParamsSchema['Type']>) => Effect.Effect<ToolResult, ToolError>;
41
+ };
42
+ type ToolModule<Context> = {
43
+ readonly id: string;
44
+ readonly tools: ReadonlyArray<ToolRegistration<Context>>;
45
+ };
46
+ type ToolMetadata = {
47
+ readonly moduleId: string;
48
+ readonly name: string;
49
+ readonly access: ToolAccess;
50
+ };
51
+ type ResolvedToolSet = {
52
+ readonly tools: ReadonlyArray<ToolDef>;
53
+ readonly metadata: ReadonlyArray<ToolMetadata>;
54
+ readonly execute: (call: ToolCall) => Effect.Effect<ToolResult, ToolError>;
55
+ };
56
+ declare const makeTool: <Context, ParamsSchema extends ToolParamsSchema>(options: MakeToolOptions<Context, ParamsSchema>) => ToolRegistration<Context>;
57
+ declare const resolveTools: <Context>(modules: ReadonlyArray<ToolModule<Context>>, context: Context) => Effect.Effect<ResolvedToolSet, ToolRegistryError>;
58
+ declare const makeToolExecutorLayer: (toolSet: ResolvedToolSet) => Layer.Layer<ToolExecutor, never, never>;
59
+ //#endregion
60
+ export { EmptyToolParams, MakeToolOptions, ResolvedToolSet, SchemaToolExecutionInput, ToolAccess, ToolExecutionInput, ToolMetadata, ToolModule, ToolRegistration, ToolRegistryError, makeTool, makeToolExecutorLayer, resolveTools };
61
+ //# sourceMappingURL=registry.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"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