@fluidframework/ai-collab 2.22.1 → 2.23.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 (47) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +70 -4
  3. package/api-report/ai-collab.alpha.api.md +170 -2
  4. package/dist/aiCollab.d.ts +0 -1
  5. package/dist/aiCollab.d.ts.map +1 -1
  6. package/dist/aiCollab.js +1 -2
  7. package/dist/aiCollab.js.map +1 -1
  8. package/dist/aiCollabApi.d.ts +50 -3
  9. package/dist/aiCollabApi.d.ts.map +1 -1
  10. package/dist/aiCollabApi.js.map +1 -1
  11. package/dist/alpha.d.ts +17 -0
  12. package/dist/explicit-strategy/debugEvents.d.ts +248 -0
  13. package/dist/explicit-strategy/debugEvents.d.ts.map +1 -0
  14. package/dist/explicit-strategy/debugEvents.js +36 -0
  15. package/dist/explicit-strategy/debugEvents.js.map +1 -0
  16. package/dist/explicit-strategy/index.d.ts +4 -4
  17. package/dist/explicit-strategy/index.d.ts.map +1 -1
  18. package/dist/explicit-strategy/index.js +176 -54
  19. package/dist/explicit-strategy/index.js.map +1 -1
  20. package/dist/index.d.ts +2 -1
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js.map +1 -1
  23. package/lib/aiCollab.d.ts +0 -1
  24. package/lib/aiCollab.d.ts.map +1 -1
  25. package/lib/aiCollab.js +1 -2
  26. package/lib/aiCollab.js.map +1 -1
  27. package/lib/aiCollabApi.d.ts +50 -3
  28. package/lib/aiCollabApi.d.ts.map +1 -1
  29. package/lib/aiCollabApi.js.map +1 -1
  30. package/lib/alpha.d.ts +17 -0
  31. package/lib/explicit-strategy/debugEvents.d.ts +248 -0
  32. package/lib/explicit-strategy/debugEvents.d.ts.map +1 -0
  33. package/lib/explicit-strategy/debugEvents.js +32 -0
  34. package/lib/explicit-strategy/debugEvents.js.map +1 -0
  35. package/lib/explicit-strategy/index.d.ts +4 -4
  36. package/lib/explicit-strategy/index.d.ts.map +1 -1
  37. package/lib/explicit-strategy/index.js +174 -52
  38. package/lib/explicit-strategy/index.js.map +1 -1
  39. package/lib/index.d.ts +2 -1
  40. package/lib/index.d.ts.map +1 -1
  41. package/lib/index.js.map +1 -1
  42. package/package.json +13 -11
  43. package/src/aiCollab.ts +1 -2
  44. package/src/aiCollabApi.ts +54 -3
  45. package/src/explicit-strategy/debugEvents.ts +297 -0
  46. package/src/explicit-strategy/index.ts +269 -59
  47. package/src/index.ts +20 -0
@@ -0,0 +1,248 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import type { DebugEvent, EventFlowDebugEvent } from "../aiCollabApi.js";
6
+ /**
7
+ * This file contains the types for the debug events that are emitted by the explicit strategy
8
+ * as well as a helper functions to help create a consistent method for producing a base {@link DebugEvent}.
9
+ */
10
+ /**
11
+ * The possible values for the `eventFlowName` field in an {@link EventFlowDebugEvent}.
12
+ * @alpha
13
+ */
14
+ export declare const EventFlowDebugNames: {
15
+ readonly CORE_EVENT_LOOP: "CORE_EVENT_LOOP";
16
+ readonly GENERATE_PLANNING_PROMPT: "GENERATE_PLANNING_PROMPT";
17
+ readonly GENERATE_AND_APPLY_TREE_EDIT: "GENERATE_AND_APPLY_TREE_EDIT";
18
+ readonly FINAL_REVIEW: "FINAL_REVIEW";
19
+ };
20
+ /**
21
+ * The type for possible values for the `eventFlowName` field in an {@link EventFlowDebugEvent}.
22
+ * @alpha
23
+ */
24
+ export type EventFlowDebugName = (typeof EventFlowDebugNames)[keyof typeof EventFlowDebugNames];
25
+ /**
26
+ * An edit generated by an LLM that can be applied to a given SharedTree.
27
+ * @privateremarks TODO: We need a better solution here because don't want to expose the internal TreeEdit type here, but we need to be able to type it.
28
+ * @alpha
29
+ */
30
+ export type LlmTreeEdit = Record<string, unknown>;
31
+ /**
32
+ * An {@link EventFlowDebugEvent} for signaling the start of the ai-collab's core event loop.
33
+ * Which makes various calls to the LLM to eventually apply edits to the users SharedTree which
34
+ * accomplish the user's provided goal. There will be exactly 1 of these events per ai-collab function execution.
35
+ * @alpha
36
+ */
37
+ export interface CoreEventLoopStarted extends EventFlowDebugEvent {
38
+ eventName: "CORE_EVENT_LOOP_STARTED";
39
+ eventFlowName: typeof EventFlowDebugNames.CORE_EVENT_LOOP;
40
+ eventFlowStatus: "STARTED";
41
+ }
42
+ /**
43
+ * An {@link EventFlowDebugEvent} for signaling the end of the ai-collab's core event loop.
44
+ * There could be various reasons for the event loop to end, early exits and failures
45
+ * which should be captured in the status and failureReason fields. There will be exactly 1
46
+ * of these events per ai-collab function execution.
47
+ * @alpha
48
+ */
49
+ export interface CoreEventLoopCompleted extends EventFlowDebugEvent {
50
+ eventName: "CORE_EVENT_LOOP_COMPLETED";
51
+ eventFlowName: typeof EventFlowDebugNames.CORE_EVENT_LOOP;
52
+ eventFlowStatus: "COMPLETED";
53
+ status: "success" | "failure";
54
+ failureReason?: string;
55
+ errorMessage?: string;
56
+ }
57
+ /**
58
+ * An {@link EventFlowDebugEvent} marking the initiation of the flow for prompting an LLM to generate a plan for accomplishing the user's goal.
59
+ * There will be exactly 1 of these events per ai-collab function execution.
60
+ * @alpha
61
+ */
62
+ export interface PlanningPromptStarted extends EventFlowDebugEvent {
63
+ eventName: "GENERATE_PLANNING_PROMPT_STARTED";
64
+ eventFlowName: typeof EventFlowDebugNames.GENERATE_PLANNING_PROMPT;
65
+ eventFlowStatus: "STARTED";
66
+ }
67
+ /**
68
+ * An {@link EventFlowDebugEvent} marking the completion of the flow for prompting an LLM to generate a plan for accomplishing the user's goal.
69
+ * There will be exactly 1 of these events per ai-collab function execution.
70
+ * @alpha
71
+ */
72
+ export interface PlanningPromptCompleted<TIsLlmResponseValid = boolean, TPlan = TIsLlmResponseValid extends true ? string : undefined> extends EventFlowDebugEvent {
73
+ eventName: "GENERATE_PLANNING_PROMPT_COMPLETED";
74
+ eventFlowName: typeof EventFlowDebugNames.GENERATE_PLANNING_PROMPT;
75
+ eventFlowStatus: "COMPLETED";
76
+ /**
77
+ * Whether the response produced by the LLM is an expected response.
78
+ * In the event that the LLM fails to respond in an expected way, despite the API call to the LLM itself being successful, then this fields value will be `false`.
79
+ *
80
+ * For now, this case is limited to the LLM returning undefined as a response when it should have returned something. But in the future this could expand
81
+ * to things such as invalid json.
82
+ */
83
+ isLlmResponseValid: TIsLlmResponseValid;
84
+ /**
85
+ * The plan generated by the LLM. If the `wasLlmResponseValid` field is `false` then this will be undefined, otherwise it will be a string.
86
+ */
87
+ llmGeneratedPlan: TPlan;
88
+ }
89
+ /**
90
+ * An {@link EventFlowDebugEvent} marking the initiation of the flow for prompting an LLM to
91
+ * generate an {@link LlmTreeEdit} to the users Shared Tree based on the users initial ask.
92
+ * It is expected that the LLM will generate multiple of these events when it must generate multiple tree edits to satisfy the user request
93
+ * @alpha
94
+ */
95
+ export interface GenerateTreeEditStarted extends EventFlowDebugEvent {
96
+ eventName: "GENERATE_TREE_EDIT_STARTED";
97
+ eventFlowName: typeof EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT;
98
+ eventFlowStatus: "STARTED";
99
+ llmPrompt: string;
100
+ }
101
+ /**
102
+ * An {@link EventFlowDebugEvent} marking the completion of the flow for prompting an LLM to
103
+ * generate an {@link LlmTreeEdit} to the users Shared Tree based on the users initial ask.
104
+ * It is expected that the LLM will generate multiple of these events when it must generate multiple tree edits to satisfy the user request
105
+ * @alpha
106
+ */
107
+ export interface GenerateTreeEditCompleted<TIsLlmResponseValid = boolean, TEdit = TIsLlmResponseValid extends true ? LlmTreeEdit | null : undefined> extends EventFlowDebugEvent {
108
+ eventName: "GENERATE_TREE_EDIT_COMPLETED";
109
+ eventFlowName: typeof EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT;
110
+ eventFlowStatus: "COMPLETED";
111
+ /**
112
+ * Whether the response produced by the LLM is an expected response.
113
+ * In the event that the LLM fails to respond in an expected way, despite the API call to the LLM itself being successful, then this fields value will be `false`.
114
+ *
115
+ * For now, this case is limited to the LLM returning undefined as a response when it should have returned something. But in the future this could expand
116
+ * to things such as invalid json.
117
+ */
118
+ isLlmResponseValid: TIsLlmResponseValid;
119
+ /**
120
+ * If the `isLlmResponseValid` field value is `true` then this be one of two following values returned by the LLM:
121
+ * 1. An edit that can be applied to a SharedTree to further accomplish the user's goal.
122
+ * 2. `null` if the LLM decides no more edits are necessary.
123
+ *
124
+ * If the `isLlmResponseValid` field is `false` then this field will be `undefined`.
125
+ */
126
+ llmGeneratedEdit: TEdit;
127
+ }
128
+ /**
129
+ * An {@link EventFlowDebugEvent} marking the successful application of an edit generated by the LLM to a SharedTree.
130
+ * @alpha
131
+ */
132
+ export interface ApplyEditSuccess extends EventFlowDebugEvent {
133
+ eventName: "APPLIED_EDIT_SUCCESS";
134
+ eventFlowName: typeof EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT;
135
+ eventFlowStatus: "IN_PROGRESS";
136
+ /**
137
+ * A unique id that will be shared across all debug events that are part of the same event flow.
138
+ */
139
+ eventFlowTraceId: string;
140
+ /**
141
+ * The {@link LlmTreeEdit} generated by the LLM.
142
+ */
143
+ edit: LlmTreeEdit;
144
+ }
145
+ /**
146
+ * An {@link EventFlowDebugEvent} marking the failure of applying an edit generated by the LLM to a SharedTree.
147
+ * @alpha
148
+ */
149
+ export interface ApplyEditFailure extends EventFlowDebugEvent {
150
+ eventName: "APPLIED_EDIT_FAILURE";
151
+ eventFlowName: typeof EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT;
152
+ eventFlowStatus: "IN_PROGRESS";
153
+ /**
154
+ * A unique id that will be shared across all debug events that are part of the same event flow.
155
+ */
156
+ eventFlowTraceId: string;
157
+ /**
158
+ * The {@link LlmTreeEdit} generated by the LLM.
159
+ */
160
+ edit: LlmTreeEdit;
161
+ /**
162
+ * The error message that was thrown when attempting to apply the edit.
163
+ */
164
+ errorMessage: string;
165
+ /**
166
+ * The total number of errors that have occurred up until this point, not including this error.
167
+ */
168
+ sequentialErrorCount: number;
169
+ }
170
+ /**
171
+ * An {@link EventFlowDebugEvent} marking the initiation of the flow for prompting an LLM to complete a final review of its edits
172
+ * and determine whether the user's goal was accomplished.
173
+ * @alpha
174
+ */
175
+ export interface FinalReviewStarted extends EventFlowDebugEvent {
176
+ eventName: "FINAL_REVIEW_STARTED";
177
+ eventFlowName: typeof EventFlowDebugNames.FINAL_REVIEW;
178
+ eventFlowStatus: "STARTED";
179
+ /**
180
+ * The prompt sent to the LLM to complete its final review of the edits its made.
181
+ */
182
+ llmPrompt: string;
183
+ }
184
+ /**
185
+ * An {@link EventFlowDebugEvent} marking the end of the flow for prompting an LLM to complete a final review of its edits
186
+ * and determine whether the user's goal was accomplished.
187
+ * @alpha
188
+ */
189
+ export interface FinalReviewCompleted<TIsLlmResponseValid = boolean, TReviewResponse = TIsLlmResponseValid extends true ? "yes" | "no" : undefined> extends EventFlowDebugEvent {
190
+ eventName: "FINAL_REVIEW_COMPLETED";
191
+ eventFlowName: typeof EventFlowDebugNames.FINAL_REVIEW;
192
+ eventFlowStatus: "COMPLETED";
193
+ /**
194
+ * Whether the response produced by the LLM is an expected response.
195
+ * In the event that the LLM fails to respond in an expected way, despite the API call to the LLM itself being successful, then this fields value will be `false`.
196
+ *
197
+ * For now, this case is limited to the LLM returning undefined as a response when it should have returned something. But in the future this could expand
198
+ * to things such as invalid json.
199
+ */
200
+ isLlmResponseValid: TIsLlmResponseValid;
201
+ /**
202
+ * Whether the Llm believes the user's ask was accomplished, based on the Edits is has already generated and applied.
203
+ * If the `status` field is `false` then this field value will be `undefined`, otherwise it will be either "yes" or "no"
204
+ */
205
+ didLlmAccomplishGoal: TReviewResponse;
206
+ }
207
+ /**
208
+ * An {@link DebugEvent} for an API call directly to a LLM.
209
+ * @alpha
210
+ */
211
+ export interface LlmApiCallDebugEvent extends DebugEvent {
212
+ eventName: "LLM_API_CALL";
213
+ /**
214
+ * The event flow name that made this LLM API call.
215
+ */
216
+ triggeringEventFlowName: EventFlowDebugName;
217
+ /**
218
+ * The unique id that will be shared across all debug events that are part of the same event flow.
219
+ * @remarks This can be used to correlate all debug events that are part of the same event flow.
220
+ */
221
+ eventFlowTraceId: string;
222
+ /**
223
+ * The LLM model name that was used for the API call.
224
+ */
225
+ modelName: string;
226
+ /**
227
+ * The request parameters sent in the API call to the LLM. (Not the HTTP request parameters such as headers, etc.)
228
+ */
229
+ requestParams: unknown;
230
+ /**
231
+ * The raw response from the LLM.
232
+ */
233
+ response: unknown;
234
+ /**
235
+ * The total number of tokens used in the API call to the LLM.
236
+ */
237
+ tokenUsage?: {
238
+ promptTokens: number;
239
+ completionTokens: number;
240
+ };
241
+ }
242
+ /**
243
+ * Creates a base {@link DebugEvent}.
244
+ */
245
+ export declare function generateDebugEvent<Tevent extends string>(eventName: Tevent, traceId: string): DebugEvent & {
246
+ eventName: Tevent;
247
+ };
248
+ //# sourceMappingURL=debugEvents.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debugEvents.d.ts","sourceRoot":"","sources":["../../src/explicit-strategy/debugEvents.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,KAAK,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAEzE;;;GAGG;AAEH;;;GAGG;AACH,eAAO,MAAM,mBAAmB;;;;;CAKtB,CAAC;AAEX;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAC7B,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,OAAO,mBAAmB,CAAC,CAAC;AAEhE;;;;GAIG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAElD;;;;;GAKG;AACH,MAAM,WAAW,oBAAqB,SAAQ,mBAAmB;IAChE,SAAS,EAAE,yBAAyB,CAAC;IACrC,aAAa,EAAE,OAAO,mBAAmB,CAAC,eAAe,CAAC;IAC1D,eAAe,EAAE,SAAS,CAAC;CAC3B;AAED;;;;;;GAMG;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AC;IACpC,aAAa,EAAE,OAAO,mBAAmB,CAAC,YAAY,CAAC;IACvD,eAAe,EAAE,WAAW,CAAC;IAC7B;;;;;;OAMG;IACH,kBAAkB,EAAE,mBAAmB,CAAC;IACxC;;;OAGG;IACH,oBAAoB,EAAE,eAAe,CAAC;CACtC;AAID;;;GAGG;AACH,MAAM,WAAW,oBAAqB,SAAQ,UAAU;IACvD,SAAS,EAAE,cAAc,CAAC;IAC1B;;OAEG;IACH,uBAAuB,EAAE,kBAAkB,CAAC;IAC5C;;;OAGG;IACH,gBAAgB,EAAE,MAAM,CAAC;IACzB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAClB;;OAEG;IACH,aAAa,EAAE,OAAO,CAAC;IACvB;;OAEG;IACH,QAAQ,EAAE,OAAO,CAAC;IAClB;;OAEG;IACH,UAAU,CAAC,EAAE;QACZ,YAAY,EAAE,MAAM,CAAC;QACrB,gBAAgB,EAAE,MAAM,CAAC;KACzB,CAAC;CACF;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,SAAS,MAAM,EACvD,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,GACb,UAAU,GAAG;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,CAQpC"}
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.generateDebugEvent = exports.EventFlowDebugNames = void 0;
8
+ const uuid_1 = require("uuid");
9
+ /**
10
+ * This file contains the types for the debug events that are emitted by the explicit strategy
11
+ * as well as a helper functions to help create a consistent method for producing a base {@link DebugEvent}.
12
+ */
13
+ /**
14
+ * The possible values for the `eventFlowName` field in an {@link EventFlowDebugEvent}.
15
+ * @alpha
16
+ */
17
+ exports.EventFlowDebugNames = {
18
+ CORE_EVENT_LOOP: "CORE_EVENT_LOOP",
19
+ GENERATE_PLANNING_PROMPT: "GENERATE_PLANNING_PROMPT",
20
+ GENERATE_AND_APPLY_TREE_EDIT: "GENERATE_AND_APPLY_TREE_EDIT",
21
+ FINAL_REVIEW: "FINAL_REVIEW",
22
+ };
23
+ /**
24
+ * Creates a base {@link DebugEvent}.
25
+ */
26
+ function generateDebugEvent(eventName, traceId) {
27
+ const event = {
28
+ eventName,
29
+ id: (0, uuid_1.v4)(),
30
+ traceId,
31
+ timestamp: new Date().toISOString(),
32
+ };
33
+ return event;
34
+ }
35
+ exports.generateDebugEvent = generateDebugEvent;
36
+ //# sourceMappingURL=debugEvents.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debugEvents.js","sourceRoot":"","sources":["../../src/explicit-strategy/debugEvents.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,+BAAoC;AAIpC;;;GAGG;AAEH;;;GAGG;AACU,QAAA,mBAAmB,GAAG;IAClC,eAAe,EAAE,iBAAiB;IAClC,wBAAwB,EAAE,0BAA0B;IACpD,4BAA4B,EAAE,8BAA8B;IAC5D,YAAY,EAAE,cAAc;CACnB,CAAC;AAmQX;;GAEG;AACH,SAAgB,kBAAkB,CACjC,SAAiB,EACjB,OAAe;IAEf,MAAM,KAAK,GAAG;QACb,SAAS;QACT,EAAE,EAAE,IAAA,SAAM,GAAE;QACZ,OAAO;QACP,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;KACnC,CAAC;IACF,OAAO,KAAK,CAAC;AACd,CAAC;AAXD,gDAWC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { v4 as uuidv4 } from \"uuid\";\n\nimport type { DebugEvent, EventFlowDebugEvent } from \"../aiCollabApi.js\";\n\n/**\n * This file contains the types for the debug events that are emitted by the explicit strategy\n * as well as a helper functions to help create a consistent method for producing a base {@link DebugEvent}.\n */\n\n/**\n * The possible values for the `eventFlowName` field in an {@link EventFlowDebugEvent}.\n * @alpha\n */\nexport const EventFlowDebugNames = {\n\tCORE_EVENT_LOOP: \"CORE_EVENT_LOOP\",\n\tGENERATE_PLANNING_PROMPT: \"GENERATE_PLANNING_PROMPT\",\n\tGENERATE_AND_APPLY_TREE_EDIT: \"GENERATE_AND_APPLY_TREE_EDIT\",\n\tFINAL_REVIEW: \"FINAL_REVIEW\",\n} as const;\n\n/**\n * The type for possible values for the `eventFlowName` field in an {@link EventFlowDebugEvent}.\n * @alpha\n */\nexport type EventFlowDebugName =\n\t(typeof EventFlowDebugNames)[keyof typeof EventFlowDebugNames];\n\n/**\n * An edit generated by an LLM that can be applied to a given SharedTree.\n * @privateremarks TODO: We need a better solution here because don't want to expose the internal TreeEdit type here, but we need to be able to type it.\n * @alpha\n */\nexport type LlmTreeEdit = Record<string, unknown>;\n\n/**\n * An {@link EventFlowDebugEvent} for signaling the start of the ai-collab's core event loop.\n * Which makes various calls to the LLM to eventually apply edits to the users SharedTree which\n * accomplish the user's provided goal. There will be exactly 1 of these events per ai-collab function execution.\n * @alpha\n */\nexport interface CoreEventLoopStarted extends EventFlowDebugEvent {\n\teventName: \"CORE_EVENT_LOOP_STARTED\";\n\teventFlowName: typeof EventFlowDebugNames.CORE_EVENT_LOOP;\n\teventFlowStatus: \"STARTED\";\n}\n\n/**\n * An {@link EventFlowDebugEvent} for signaling the end of the ai-collab's core event loop.\n * There could be various reasons for the event loop to end, early exits and failures\n * which should be captured in the status and failureReason fields. There will be exactly 1\n * of these events per ai-collab function execution.\n * @alpha\n */\nexport interface CoreEventLoopCompleted extends EventFlowDebugEvent {\n\teventName: \"CORE_EVENT_LOOP_COMPLETED\";\n\teventFlowName: typeof EventFlowDebugNames.CORE_EVENT_LOOP;\n\teventFlowStatus: \"COMPLETED\";\n\tstatus: \"success\" | \"failure\";\n\tfailureReason?: string;\n\terrorMessage?: string;\n}\n\n// #region Planning Prompt Debug events -------------------------------\n\n/**\n * An {@link EventFlowDebugEvent} marking the initiation of the flow for prompting an LLM to generate a plan for accomplishing the user's goal.\n * There will be exactly 1 of these events per ai-collab function execution.\n * @alpha\n */\nexport interface PlanningPromptStarted extends EventFlowDebugEvent {\n\teventName: \"GENERATE_PLANNING_PROMPT_STARTED\";\n\teventFlowName: typeof EventFlowDebugNames.GENERATE_PLANNING_PROMPT;\n\teventFlowStatus: \"STARTED\";\n}\n\n/**\n * An {@link EventFlowDebugEvent} marking the completion of the flow for prompting an LLM to generate a plan for accomplishing the user's goal.\n * There will be exactly 1 of these events per ai-collab function execution.\n * @alpha\n */\nexport interface PlanningPromptCompleted<\n\tTIsLlmResponseValid = boolean,\n\tTPlan = TIsLlmResponseValid extends true ? string : undefined,\n> extends EventFlowDebugEvent {\n\teventName: \"GENERATE_PLANNING_PROMPT_COMPLETED\";\n\teventFlowName: typeof EventFlowDebugNames.GENERATE_PLANNING_PROMPT;\n\teventFlowStatus: \"COMPLETED\";\n\t/**\n\t * Whether the response produced by the LLM is an expected response.\n\t * In the event that the LLM fails to respond in an expected way, despite the API call to the LLM itself being successful, then this fields value will be `false`.\n\t *\n\t * For now, this case is limited to the LLM returning undefined as a response when it should have returned something. But in the future this could expand\n\t * to things such as invalid json.\n\t */\n\tisLlmResponseValid: TIsLlmResponseValid;\n\t/**\n\t * The plan generated by the LLM. If the `wasLlmResponseValid` field is `false` then this will be undefined, otherwise it will be a string.\n\t */\n\tllmGeneratedPlan: TPlan;\n}\n\n// #endregion Planning Prompt Debug events ----------------------------------------------------------\n\n// #region Generate And Apply Tree Edit Debug events ------------------\n\n/**\n * An {@link EventFlowDebugEvent} marking the initiation of the flow for prompting an LLM to\n * generate an {@link LlmTreeEdit} to the users Shared Tree based on the users initial ask.\n * It is expected that the LLM will generate multiple of these events when it must generate multiple tree edits to satisfy the user request\n * @alpha\n */\nexport interface GenerateTreeEditStarted extends EventFlowDebugEvent {\n\teventName: \"GENERATE_TREE_EDIT_STARTED\";\n\teventFlowName: typeof EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT;\n\teventFlowStatus: \"STARTED\";\n\tllmPrompt: string;\n}\n\n/**\n * An {@link EventFlowDebugEvent} marking the completion of the flow for prompting an LLM to\n * generate an {@link LlmTreeEdit} to the users Shared Tree based on the users initial ask.\n * It is expected that the LLM will generate multiple of these events when it must generate multiple tree edits to satisfy the user request\n * @alpha\n */\nexport interface GenerateTreeEditCompleted<\n\tTIsLlmResponseValid = boolean,\n\tTEdit = TIsLlmResponseValid extends true ? LlmTreeEdit | null : undefined,\n> extends EventFlowDebugEvent {\n\teventName: \"GENERATE_TREE_EDIT_COMPLETED\";\n\teventFlowName: typeof EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT;\n\teventFlowStatus: \"COMPLETED\";\n\t/**\n\t * Whether the response produced by the LLM is an expected response.\n\t * In the event that the LLM fails to respond in an expected way, despite the API call to the LLM itself being successful, then this fields value will be `false`.\n\t *\n\t * For now, this case is limited to the LLM returning undefined as a response when it should have returned something. But in the future this could expand\n\t * to things such as invalid json.\n\t */\n\tisLlmResponseValid: TIsLlmResponseValid;\n\t/**\n\t * If the `isLlmResponseValid` field value is `true` then this be one of two following values returned by the LLM:\n\t * 1. An edit that can be applied to a SharedTree to further accomplish the user's goal.\n\t * 2. `null` if the LLM decides no more edits are necessary.\n\t *\n\t * If the `isLlmResponseValid` field is `false` then this field will be `undefined`.\n\t */\n\tllmGeneratedEdit: TEdit;\n}\n\n/**\n * An {@link EventFlowDebugEvent} marking the successful application of an edit generated by the LLM to a SharedTree.\n * @alpha\n */\nexport interface ApplyEditSuccess extends EventFlowDebugEvent {\n\teventName: \"APPLIED_EDIT_SUCCESS\";\n\teventFlowName: typeof EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT;\n\teventFlowStatus: \"IN_PROGRESS\";\n\t/**\n\t * A unique id that will be shared across all debug events that are part of the same event flow.\n\t */\n\teventFlowTraceId: string;\n\t/**\n\t * The {@link LlmTreeEdit} generated by the LLM.\n\t */\n\tedit: LlmTreeEdit;\n}\n\n/**\n * An {@link EventFlowDebugEvent} marking the failure of applying an edit generated by the LLM to a SharedTree.\n * @alpha\n */\nexport interface ApplyEditFailure extends EventFlowDebugEvent {\n\teventName: \"APPLIED_EDIT_FAILURE\";\n\teventFlowName: typeof EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT;\n\teventFlowStatus: \"IN_PROGRESS\";\n\t/**\n\t * A unique id that will be shared across all debug events that are part of the same event flow.\n\t */\n\teventFlowTraceId: string;\n\t/**\n\t * The {@link LlmTreeEdit} generated by the LLM.\n\t */\n\tedit: LlmTreeEdit;\n\t/**\n\t * The error message that was thrown when attempting to apply the edit.\n\t */\n\terrorMessage: string;\n\t/**\n\t * The total number of errors that have occurred up until this point, not including this error.\n\t */\n\tsequentialErrorCount: number;\n}\n\n// #endregion Generate And Apply Tree Edit Debug events ----------------------\n\n// #region Generate Final Review Debug events -------------------------\n\n/**\n * An {@link EventFlowDebugEvent} marking the initiation of the flow for prompting an LLM to complete a final review of its edits\n * and determine whether the user's goal was accomplished.\n * @alpha\n */\nexport interface FinalReviewStarted extends EventFlowDebugEvent {\n\teventName: \"FINAL_REVIEW_STARTED\";\n\teventFlowName: typeof EventFlowDebugNames.FINAL_REVIEW;\n\teventFlowStatus: \"STARTED\";\n\t/**\n\t * The prompt sent to the LLM to complete its final review of the edits its made.\n\t */\n\tllmPrompt: string;\n}\n\n/**\n * An {@link EventFlowDebugEvent} marking the end of the flow for prompting an LLM to complete a final review of its edits\n * and determine whether the user's goal was accomplished.\n * @alpha\n */\nexport interface FinalReviewCompleted<\n\tTIsLlmResponseValid = boolean,\n\tTReviewResponse = TIsLlmResponseValid extends true ? \"yes\" | \"no\" : undefined,\n> extends EventFlowDebugEvent {\n\teventName: \"FINAL_REVIEW_COMPLETED\";\n\teventFlowName: typeof EventFlowDebugNames.FINAL_REVIEW;\n\teventFlowStatus: \"COMPLETED\";\n\t/**\n\t * Whether the response produced by the LLM is an expected response.\n\t * In the event that the LLM fails to respond in an expected way, despite the API call to the LLM itself being successful, then this fields value will be `false`.\n\t *\n\t * For now, this case is limited to the LLM returning undefined as a response when it should have returned something. But in the future this could expand\n\t * to things such as invalid json.\n\t */\n\tisLlmResponseValid: TIsLlmResponseValid;\n\t/**\n\t * Whether the Llm believes the user's ask was accomplished, based on the Edits is has already generated and applied.\n\t * If the `status` field is `false` then this field value will be `undefined`, otherwise it will be either \"yes\" or \"no\"\n\t */\n\tdidLlmAccomplishGoal: TReviewResponse;\n}\n\n// #endregion Generate Final Review Debug events ----------------------\n\n/**\n * An {@link DebugEvent} for an API call directly to a LLM.\n * @alpha\n */\nexport interface LlmApiCallDebugEvent extends DebugEvent {\n\teventName: \"LLM_API_CALL\";\n\t/**\n\t * The event flow name that made this LLM API call.\n\t */\n\ttriggeringEventFlowName: EventFlowDebugName;\n\t/**\n\t * The unique id that will be shared across all debug events that are part of the same event flow.\n\t * @remarks This can be used to correlate all debug events that are part of the same event flow.\n\t */\n\teventFlowTraceId: string;\n\t/**\n\t * The LLM model name that was used for the API call.\n\t */\n\tmodelName: string;\n\t/**\n\t * The request parameters sent in the API call to the LLM. (Not the HTTP request parameters such as headers, etc.)\n\t */\n\trequestParams: unknown;\n\t/**\n\t * The raw response from the LLM.\n\t */\n\tresponse: unknown;\n\t/**\n\t * The total number of tokens used in the API call to the LLM.\n\t */\n\ttokenUsage?: {\n\t\tpromptTokens: number;\n\t\tcompletionTokens: number;\n\t};\n}\n\n/**\n * Creates a base {@link DebugEvent}.\n */\nexport function generateDebugEvent<Tevent extends string>(\n\teventName: Tevent,\n\ttraceId: string,\n): DebugEvent & { eventName: Tevent } {\n\tconst event = {\n\t\teventName,\n\t\tid: uuidv4(),\n\t\ttraceId,\n\t\ttimestamp: new Date().toISOString(),\n\t};\n\treturn event;\n}\n"]}
@@ -3,7 +3,8 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
  import { type TreeNode } from "@fluidframework/tree/internal";
6
- import type { OpenAiClientOptions, TokenLimits, TokenUsage } from "../aiCollabApi.js";
6
+ import type { DebugEventLogHandler, OpenAiClientOptions, TokenLimits, TokenUsage } from "../aiCollabApi.js";
7
+ export type { ApplyEditFailure, ApplyEditSuccess, CoreEventLoopCompleted, CoreEventLoopStarted, FinalReviewCompleted, FinalReviewStarted, GenerateTreeEditCompleted, GenerateTreeEditStarted, LlmApiCallDebugEvent, PlanningPromptCompleted, PlanningPromptStarted, LlmTreeEdit, EventFlowDebugName, EventFlowDebugNames, } from "./debugEvents.js";
7
8
  /**
8
9
  * {@link generateTreeEdits} options.
9
10
  *
@@ -24,7 +25,7 @@ export interface GenerateTreeEditsOptions {
24
25
  };
25
26
  finalReviewStep?: boolean;
26
27
  validator?: (newContent: TreeNode) => void;
27
- dumpDebugLog?: boolean;
28
+ debugEventLogHandler?: DebugEventLogHandler;
28
29
  planningStep?: boolean;
29
30
  }
30
31
  interface GenerateTreeEditsSuccessResponse {
@@ -33,7 +34,7 @@ interface GenerateTreeEditsSuccessResponse {
33
34
  }
34
35
  interface GenerateTreeEditsErrorResponse {
35
36
  status: "failure" | "partial-failure";
36
- errorMessage: "tokenLimitExceeded" | "tooManyErrors" | "tooManyModelCalls" | "aborted";
37
+ errorMessage: "tokenLimitExceeded" | "tooManyErrors" | "tooManyModelCalls" | "aborted" | "unexpectedError";
37
38
  tokensUsed: TokenUsage;
38
39
  }
39
40
  /**
@@ -47,5 +48,4 @@ interface GenerateTreeEditsErrorResponse {
47
48
  * @internal
48
49
  */
49
50
  export declare function generateTreeEdits(options: GenerateTreeEditsOptions): Promise<GenerateTreeEditsSuccessResponse | GenerateTreeEditsErrorResponse>;
50
- export {};
51
51
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/explicit-strategy/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAIN,KAAK,QAAQ,EACb,MAAM,+BAA+B,CAAC;AASvC,OAAO,KAAK,EAAE,mBAAmB,EAAE,WAAW,EAAE,UAAU,EAAE,M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