@swirls/looms 0.12.1 → 0.12.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @swirls/looms
2
2
 
3
+ ## 0.12.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 04d546e: `onEffect` and `onAdmit` accept an effect definition that requires host services. Wrapping `callLlmEffect` in middleware, as in `onEffect(callLlmEffect, handler)`, was a type error because the definition's service requirements did not match the parameter's default. The functions now infer those requirements from the definition.
8
+
9
+ ## 0.12.2
10
+
11
+ ### Patch Changes
12
+
13
+ - 19c5f76: Keep agent tool results next to their tool calls when a message arrives mid-call. A non-interrupt steer sent while a tool call was open was stored between the call and its result, and the next LLM turn failed with "Tool result is missing for tool call". Results are now inserted directly after the call, so the steer follows them.
14
+
3
15
  ## 0.12.1
4
16
 
5
17
  ### Patch Changes
@@ -23,6 +23,252 @@ export declare const callLlmEffect: NoInfer<import("@swirls/looms/core").LocalEf
23
23
  readonly alreadyCompacted?: boolean | undefined;
24
24
  readonly consumedSteerMessageIds?: readonly string[] | undefined;
25
25
  }, AgentDefinitionsTag | LlmTag, (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
26
+ readonly type: "runtime.child.close.requested";
27
+ readonly payload: {
28
+ readonly policy: "terminate" | "wait";
29
+ };
30
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
31
+ readonly type: "runtime.child.settled";
32
+ readonly payload: {
33
+ readonly runId: string;
34
+ readonly status: "cancelled" | "completed" | "failed";
35
+ readonly output: Schema.Json;
36
+ readonly error: string | null;
37
+ };
38
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
39
+ readonly type: "runtime.effect.ambiguous";
40
+ readonly payload: {
41
+ readonly effectId: string;
42
+ readonly attempt: number;
43
+ readonly error: string;
44
+ };
45
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
46
+ readonly type: "runtime.effect.attempt.started";
47
+ readonly payload: {
48
+ readonly effectId: string;
49
+ readonly attempt: number;
50
+ };
51
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
52
+ readonly type: "runtime.effect.cancel.requested";
53
+ readonly payload: {
54
+ readonly effectId: string;
55
+ readonly attempt: number;
56
+ readonly deadlineAt: number | null;
57
+ };
58
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
59
+ readonly type: "runtime.effect.cancelled";
60
+ readonly payload: {
61
+ readonly effectId: string;
62
+ readonly attempt: number;
63
+ };
64
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
65
+ readonly type: "runtime.effect.completed";
66
+ readonly payload: {
67
+ readonly effectId: string;
68
+ readonly attempt: number;
69
+ };
70
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
71
+ readonly type: "runtime.effect.dispatch.deferred";
72
+ readonly payload: {
73
+ readonly effectId: string;
74
+ readonly attempt: number;
75
+ readonly retryAt: number;
76
+ readonly error: string;
77
+ readonly try: number;
78
+ };
79
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
80
+ readonly type: "runtime.effect.dispatched";
81
+ readonly payload: {
82
+ readonly effectId: string;
83
+ readonly attempt: number;
84
+ readonly deadlineAt: number | null;
85
+ };
86
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
87
+ readonly type: "runtime.effect.failed";
88
+ readonly payload: {
89
+ readonly effectId: string;
90
+ readonly error: string;
91
+ readonly nonRetryable?: boolean | undefined;
92
+ readonly retryAfterMs?: number | undefined;
93
+ };
94
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
95
+ readonly type: "runtime.effect.heartbeat";
96
+ readonly payload: {
97
+ readonly effectId: string;
98
+ readonly attempt: number;
99
+ readonly heartbeatAt: number;
100
+ readonly deadlineAt: number | null;
101
+ readonly details?: Schema.Json | undefined;
102
+ };
103
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
104
+ readonly type: "runtime.effect.queued";
105
+ readonly payload: {
106
+ readonly effectId: string;
107
+ readonly attempt: number;
108
+ readonly deadlineAt: number | null;
109
+ };
110
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
111
+ readonly type: "runtime.effect.retry.scheduled";
112
+ readonly payload: {
113
+ readonly effectId: string;
114
+ readonly attempt: number;
115
+ readonly nextAttemptAt: number;
116
+ readonly error: string;
117
+ };
118
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
119
+ readonly type: "runtime.effect.timed_out";
120
+ readonly payload: {
121
+ readonly effectId: string;
122
+ readonly attempt: number;
123
+ readonly timeout: string;
124
+ };
125
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
126
+ readonly type: "runtime.effect.worker.started";
127
+ readonly payload: {
128
+ readonly effectId: string;
129
+ readonly attempt: number;
130
+ readonly deadlineAt: number | null;
131
+ };
132
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
133
+ readonly type: "runtime.fork.elided";
134
+ readonly payload: {
135
+ readonly type: string;
136
+ };
137
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
138
+ readonly type: "runtime.run.cancelled";
139
+ readonly payload: {
140
+ readonly reason?: string | undefined;
141
+ };
142
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
143
+ readonly type: "runtime.run.completed";
144
+ readonly payload: {
145
+ readonly output: Schema.Json;
146
+ readonly error: string | null;
147
+ };
148
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
149
+ readonly type: "runtime.run.forked";
150
+ readonly payload: {
151
+ readonly sourceRunId: string;
152
+ readonly sourceSeq: number;
153
+ };
154
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
155
+ readonly type: "runtime.run.launched";
156
+ readonly payload: {
157
+ readonly runId: string;
158
+ readonly kind: string;
159
+ readonly definitionName: string;
160
+ readonly definitionVersion: string;
161
+ readonly effectId: string;
162
+ readonly close: "terminate" | "wait";
163
+ };
164
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
165
+ readonly type: "runtime.run.started";
166
+ readonly payload: {
167
+ readonly rootThreadId?: string | undefined;
168
+ readonly kind: string;
169
+ readonly definitionName: string;
170
+ readonly definitionVersion: string;
171
+ readonly input: Schema.Json;
172
+ readonly requestedThreadId?: string | null | undefined;
173
+ readonly launchedBy?: {
174
+ readonly runId: string;
175
+ readonly threadId: string;
176
+ readonly effectId: string;
177
+ readonly causationId: string;
178
+ readonly causationSeq: number;
179
+ } | undefined;
180
+ readonly attributes?: {
181
+ readonly [x: string]: string;
182
+ } | undefined;
183
+ readonly trace?: {
184
+ readonly traceparent: string;
185
+ readonly tracestate?: string | undefined;
186
+ } | undefined;
187
+ };
188
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
189
+ readonly type: "runtime.signal.received";
190
+ readonly payload: {
191
+ readonly signalType: string;
192
+ };
193
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
194
+ readonly type: "runtime.snapshot.taken";
195
+ readonly payload: {
196
+ readonly seq: number;
197
+ readonly stateHash: string;
198
+ readonly state?: Schema.Json | undefined;
199
+ };
200
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
201
+ readonly type: "runtime.thread.cancel.requested";
202
+ readonly payload: {
203
+ readonly threadId: string;
204
+ };
205
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
206
+ readonly type: "runtime.thread.cancelled";
207
+ readonly payload: {
208
+ readonly threadId: string;
209
+ readonly reason?: string | undefined;
210
+ readonly resumeSeq?: number | undefined;
211
+ };
212
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
213
+ readonly type: "runtime.thread.completed";
214
+ readonly payload: {
215
+ readonly threadId: string;
216
+ readonly output: Schema.Json;
217
+ };
218
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
219
+ readonly type: "runtime.thread.failed";
220
+ readonly payload: {
221
+ readonly threadId: string;
222
+ readonly error: string;
223
+ };
224
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
225
+ readonly type: "runtime.thread.started";
226
+ readonly payload: {
227
+ readonly threadId: string;
228
+ readonly kind: string;
229
+ readonly definitionName: string;
230
+ readonly definitionVersion: string;
231
+ readonly input: Schema.Json;
232
+ readonly parentThreadId: string | null;
233
+ };
234
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
235
+ readonly type: "runtime.timer.fired";
236
+ readonly payload: {
237
+ readonly timerId: string;
238
+ readonly waitId: string;
239
+ };
240
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
241
+ readonly type: "runtime.timer.set";
242
+ readonly payload: {
243
+ readonly timerId: string;
244
+ readonly waitId: string;
245
+ readonly wakeAt: number;
246
+ };
247
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
248
+ readonly type: "runtime.wait.registered";
249
+ readonly payload: {
250
+ readonly waitId: string;
251
+ readonly threadId?: string | undefined;
252
+ readonly on: {
253
+ readonly type: string | readonly string[];
254
+ readonly match?: Schema.Json | undefined;
255
+ } | {
256
+ readonly timerAt: number;
257
+ };
258
+ readonly tag?: Schema.Json | undefined;
259
+ };
260
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
261
+ readonly type: "runtime.wait.satisfied";
262
+ readonly payload: {
263
+ readonly waitId: string;
264
+ readonly tag?: Schema.Json | undefined;
265
+ readonly event: {
266
+ readonly id: string;
267
+ readonly type: string;
268
+ readonly payload: Schema.Json;
269
+ };
270
+ };
271
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
26
272
  readonly type: "agent.context.compacted";
27
273
  readonly payload: {
28
274
  readonly turn: number;
@@ -270,7 +516,19 @@ export declare const callLlmEffect: NoInfer<import("@swirls/looms/core").LocalEf
270
516
  readonly input: number;
271
517
  readonly output: number;
272
518
  };
273
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
519
+ })>>;
520
+ export declare const executeToolEffect: NoInfer<import("@swirls/looms/core").LocalEffectDefinition<{
521
+ readonly turn: number;
522
+ readonly definitionName?: string | undefined;
523
+ readonly definitionVersion: string;
524
+ readonly toolCall: {
525
+ readonly id: string;
526
+ readonly name: string;
527
+ readonly arguments: Schema.Json;
528
+ };
529
+ readonly input?: Schema.Json | undefined;
530
+ readonly depth?: number | undefined;
531
+ }, AgentDefinitionsTag, (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
274
532
  readonly type: "runtime.child.close.requested";
275
533
  readonly payload: {
276
534
  readonly policy: "terminate" | "wait";
@@ -509,26 +767,14 @@ export declare const callLlmEffect: NoInfer<import("@swirls/looms/core").LocalEf
509
767
  readonly type: "runtime.wait.satisfied";
510
768
  readonly payload: {
511
769
  readonly waitId: string;
512
- readonly tag?: Schema.Json | undefined;
513
- readonly event: {
514
- readonly id: string;
515
- readonly type: string;
516
- readonly payload: Schema.Json;
517
- };
518
- };
519
- })>>;
520
- export declare const executeToolEffect: NoInfer<import("@swirls/looms/core").LocalEffectDefinition<{
521
- readonly turn: number;
522
- readonly definitionName?: string | undefined;
523
- readonly definitionVersion: string;
524
- readonly toolCall: {
525
- readonly id: string;
526
- readonly name: string;
527
- readonly arguments: Schema.Json;
770
+ readonly tag?: Schema.Json | undefined;
771
+ readonly event: {
772
+ readonly id: string;
773
+ readonly type: string;
774
+ readonly payload: Schema.Json;
775
+ };
528
776
  };
529
- readonly input?: Schema.Json | undefined;
530
- readonly depth?: number | undefined;
531
- }, AgentDefinitionsTag, (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
777
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
532
778
  readonly type: "agent.context.compacted";
533
779
  readonly payload: {
534
780
  readonly turn: number;
@@ -776,251 +1022,5 @@ export declare const executeToolEffect: NoInfer<import("@swirls/looms/core").Loc
776
1022
  readonly input: number;
777
1023
  readonly output: number;
778
1024
  };
779
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
780
- readonly type: "runtime.child.close.requested";
781
- readonly payload: {
782
- readonly policy: "terminate" | "wait";
783
- };
784
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
785
- readonly type: "runtime.child.settled";
786
- readonly payload: {
787
- readonly runId: string;
788
- readonly status: "cancelled" | "completed" | "failed";
789
- readonly output: Schema.Json;
790
- readonly error: string | null;
791
- };
792
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
793
- readonly type: "runtime.effect.ambiguous";
794
- readonly payload: {
795
- readonly effectId: string;
796
- readonly attempt: number;
797
- readonly error: string;
798
- };
799
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
800
- readonly type: "runtime.effect.attempt.started";
801
- readonly payload: {
802
- readonly effectId: string;
803
- readonly attempt: number;
804
- };
805
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
806
- readonly type: "runtime.effect.cancel.requested";
807
- readonly payload: {
808
- readonly effectId: string;
809
- readonly attempt: number;
810
- readonly deadlineAt: number | null;
811
- };
812
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
813
- readonly type: "runtime.effect.cancelled";
814
- readonly payload: {
815
- readonly effectId: string;
816
- readonly attempt: number;
817
- };
818
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
819
- readonly type: "runtime.effect.completed";
820
- readonly payload: {
821
- readonly effectId: string;
822
- readonly attempt: number;
823
- };
824
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
825
- readonly type: "runtime.effect.dispatch.deferred";
826
- readonly payload: {
827
- readonly effectId: string;
828
- readonly attempt: number;
829
- readonly retryAt: number;
830
- readonly error: string;
831
- readonly try: number;
832
- };
833
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
834
- readonly type: "runtime.effect.dispatched";
835
- readonly payload: {
836
- readonly effectId: string;
837
- readonly attempt: number;
838
- readonly deadlineAt: number | null;
839
- };
840
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
841
- readonly type: "runtime.effect.failed";
842
- readonly payload: {
843
- readonly effectId: string;
844
- readonly error: string;
845
- readonly nonRetryable?: boolean | undefined;
846
- readonly retryAfterMs?: number | undefined;
847
- };
848
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
849
- readonly type: "runtime.effect.heartbeat";
850
- readonly payload: {
851
- readonly effectId: string;
852
- readonly attempt: number;
853
- readonly heartbeatAt: number;
854
- readonly deadlineAt: number | null;
855
- readonly details?: Schema.Json | undefined;
856
- };
857
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
858
- readonly type: "runtime.effect.queued";
859
- readonly payload: {
860
- readonly effectId: string;
861
- readonly attempt: number;
862
- readonly deadlineAt: number | null;
863
- };
864
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
865
- readonly type: "runtime.effect.retry.scheduled";
866
- readonly payload: {
867
- readonly effectId: string;
868
- readonly attempt: number;
869
- readonly nextAttemptAt: number;
870
- readonly error: string;
871
- };
872
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
873
- readonly type: "runtime.effect.timed_out";
874
- readonly payload: {
875
- readonly effectId: string;
876
- readonly attempt: number;
877
- readonly timeout: string;
878
- };
879
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
880
- readonly type: "runtime.effect.worker.started";
881
- readonly payload: {
882
- readonly effectId: string;
883
- readonly attempt: number;
884
- readonly deadlineAt: number | null;
885
- };
886
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
887
- readonly type: "runtime.fork.elided";
888
- readonly payload: {
889
- readonly type: string;
890
- };
891
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
892
- readonly type: "runtime.run.cancelled";
893
- readonly payload: {
894
- readonly reason?: string | undefined;
895
- };
896
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
897
- readonly type: "runtime.run.completed";
898
- readonly payload: {
899
- readonly output: Schema.Json;
900
- readonly error: string | null;
901
- };
902
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
903
- readonly type: "runtime.run.forked";
904
- readonly payload: {
905
- readonly sourceRunId: string;
906
- readonly sourceSeq: number;
907
- };
908
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
909
- readonly type: "runtime.run.launched";
910
- readonly payload: {
911
- readonly runId: string;
912
- readonly kind: string;
913
- readonly definitionName: string;
914
- readonly definitionVersion: string;
915
- readonly effectId: string;
916
- readonly close: "terminate" | "wait";
917
- };
918
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
919
- readonly type: "runtime.run.started";
920
- readonly payload: {
921
- readonly rootThreadId?: string | undefined;
922
- readonly kind: string;
923
- readonly definitionName: string;
924
- readonly definitionVersion: string;
925
- readonly input: Schema.Json;
926
- readonly requestedThreadId?: string | null | undefined;
927
- readonly launchedBy?: {
928
- readonly runId: string;
929
- readonly threadId: string;
930
- readonly effectId: string;
931
- readonly causationId: string;
932
- readonly causationSeq: number;
933
- } | undefined;
934
- readonly attributes?: {
935
- readonly [x: string]: string;
936
- } | undefined;
937
- readonly trace?: {
938
- readonly traceparent: string;
939
- readonly tracestate?: string | undefined;
940
- } | undefined;
941
- };
942
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
943
- readonly type: "runtime.signal.received";
944
- readonly payload: {
945
- readonly signalType: string;
946
- };
947
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
948
- readonly type: "runtime.snapshot.taken";
949
- readonly payload: {
950
- readonly seq: number;
951
- readonly stateHash: string;
952
- readonly state?: Schema.Json | undefined;
953
- };
954
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
955
- readonly type: "runtime.thread.cancel.requested";
956
- readonly payload: {
957
- readonly threadId: string;
958
- };
959
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
960
- readonly type: "runtime.thread.cancelled";
961
- readonly payload: {
962
- readonly threadId: string;
963
- readonly reason?: string | undefined;
964
- readonly resumeSeq?: number | undefined;
965
- };
966
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
967
- readonly type: "runtime.thread.completed";
968
- readonly payload: {
969
- readonly threadId: string;
970
- readonly output: Schema.Json;
971
- };
972
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
973
- readonly type: "runtime.thread.failed";
974
- readonly payload: {
975
- readonly threadId: string;
976
- readonly error: string;
977
- };
978
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
979
- readonly type: "runtime.thread.started";
980
- readonly payload: {
981
- readonly threadId: string;
982
- readonly kind: string;
983
- readonly definitionName: string;
984
- readonly definitionVersion: string;
985
- readonly input: Schema.Json;
986
- readonly parentThreadId: string | null;
987
- };
988
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
989
- readonly type: "runtime.timer.fired";
990
- readonly payload: {
991
- readonly timerId: string;
992
- readonly waitId: string;
993
- };
994
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
995
- readonly type: "runtime.timer.set";
996
- readonly payload: {
997
- readonly timerId: string;
998
- readonly waitId: string;
999
- readonly wakeAt: number;
1000
- };
1001
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
1002
- readonly type: "runtime.wait.registered";
1003
- readonly payload: {
1004
- readonly waitId: string;
1005
- readonly threadId?: string | undefined;
1006
- readonly on: {
1007
- readonly type: string | readonly string[];
1008
- readonly match?: Schema.Json | undefined;
1009
- } | {
1010
- readonly timerAt: number;
1011
- };
1012
- readonly tag?: Schema.Json | undefined;
1013
- };
1014
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
1015
- readonly type: "runtime.wait.satisfied";
1016
- readonly payload: {
1017
- readonly waitId: string;
1018
- readonly tag?: Schema.Json | undefined;
1019
- readonly event: {
1020
- readonly id: string;
1021
- readonly type: string;
1022
- readonly payload: Schema.Json;
1023
- };
1024
- };
1025
1025
  })>>;
1026
1026
  export declare function readToolCall(value: JsonValue): ToolCall | null;
@@ -1,6 +1,6 @@
1
1
  import { Predicate } from 'effect';
2
2
  import { agentModule } from './scope.js';
3
- import { closeOpenToolCalls } from './transcript.js';
3
+ import { closeOpenToolCalls, insertToolResult } from './transcript.js';
4
4
  import { ConversationSchema, TokenUsageSchema, TurnActivitySchema, } from './types.js';
5
5
  export const conversation = agentModule.projection({
6
6
  name: 'conversation',
@@ -54,9 +54,7 @@ export const conversation = agentModule.projection({
54
54
  name: name ?? undefined,
55
55
  toolCalls: undefined,
56
56
  };
57
- return {
58
- lines: [...state.lines, toolLine],
59
- };
57
+ return { lines: insertToolResult(state.lines, toolLine) };
60
58
  }
61
59
  case 'agent.turn.stopped':
62
60
  return { lines: closeOpenToolCalls(state.lines) };
@@ -3,7 +3,7 @@ import { cancel, createWaitId, DEFAULT_DEFINITION_VERSION, emit, invoke, isJsonO
3
3
  import { STRUCTURED_OUTPUT_TOOL_NAME } from './definitions.js';
4
4
  import { callLlmEffect, executeToolEffect } from './effects.js';
5
5
  import { agentModule } from './scope.js';
6
- import { closeOpenToolCalls } from './transcript.js';
6
+ import { closeOpenToolCalls, insertToolResult } from './transcript.js';
7
7
  function inputToLine(input) {
8
8
  if (input === null) {
9
9
  return null;
@@ -167,15 +167,12 @@ export const agentThread = agentModule.thread({
167
167
  structuredOutput =
168
168
  isJsonObject(result) && 'output' in result ? (result.output ?? null) : result;
169
169
  }
170
- const lines = [
171
- ...state.lines,
172
- {
173
- role: 'tool',
174
- content,
175
- toolCallId,
176
- name: name ?? undefined,
177
- },
178
- ];
170
+ const lines = insertToolResult(state.lines, {
171
+ role: 'tool',
172
+ content,
173
+ toolCallId,
174
+ name: name ?? undefined,
175
+ });
179
176
  return {
180
177
  ...state,
181
178
  lines,
@@ -206,7 +203,8 @@ export const agentThread = agentModule.thread({
206
203
  };
207
204
  }
208
205
  // Queue for the next turn. If the agent is idle (no open tool calls), that turn
209
- // starts now — otherwise the message rides along when tools finish.
206
+ // starts now. Otherwise the message waits: tool results are inserted ahead of it
207
+ // (`insertToolResult`), and the turn starts once the last one lands.
210
208
  return {
211
209
  ...state,
212
210
  lines: [...state.lines, message],
@@ -5,3 +5,10 @@ export declare const STOPPED_TOOL_RESULT = "Stopped before the tool returned a r
5
5
  * whose tool calls are left unanswered, so a stopped turn closes them before the next turn.
6
6
  */
7
7
  export declare function closeOpenToolCalls(lines: readonly Message[]): Message[];
8
+ /**
9
+ * Add a tool result directly after the assistant line that made the call, behind any results
10
+ * already there. A user message can arrive while the call is open (a non-interrupt steer); model
11
+ * APIs reject a transcript where that message sits between a tool call and its result, so the
12
+ * result goes ahead of it. A result with no open call (unknown or already answered) is appended.
13
+ */
14
+ export declare function insertToolResult(lines: readonly Message[], result: Message): Message[];
@@ -17,3 +17,24 @@ export function closeOpenToolCalls(lines) {
17
17
  : []);
18
18
  return closing.length === 0 ? [...lines] : [...lines, ...closing];
19
19
  }
20
+ /**
21
+ * Add a tool result directly after the assistant line that made the call, behind any results
22
+ * already there. A user message can arrive while the call is open (a non-interrupt steer); model
23
+ * APIs reject a transcript where that message sits between a tool call and its result, so the
24
+ * result goes ahead of it. A result with no open call (unknown or already answered) is appended.
25
+ */
26
+ export function insertToolResult(lines, result) {
27
+ const callId = result.toolCallId;
28
+ const answered = lines.some((line) => line.role === 'tool' && line.toolCallId === callId);
29
+ const callIndex = answered
30
+ ? -1
31
+ : lines.findLastIndex((line) => line.role === 'assistant' && line.toolCalls?.some((call) => call.id === callId));
32
+ if (callIndex === -1) {
33
+ return [...lines, result];
34
+ }
35
+ let index = callIndex + 1;
36
+ while (lines[index]?.role === 'tool') {
37
+ index += 1;
38
+ }
39
+ return [...lines.slice(0, index), result, ...lines.slice(index)];
40
+ }