Skip to content

Commit 43a5e4e

Browse files
authoredSep 5, 2024
feat: create*Event methods now return strings (copilot-extensions#59)
BREAKING CHANGE: All `create*Event` methods now return a string instead of an object with a `.toString()` method. Before: ```js esponse.write(createTextEvent("Hello, world!").toString()); ``` Now: ```js esponse.write(createTextEvent("Hello, world!")); ```
1 parent 9533a1f commit 43a5e4e

6 files changed

+64
-361
lines changed
 

‎README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ export default handler(request, response) {
4646
const textEvent = createTextEvent("Hello, world!");
4747
const doneEvent = createDoneEvent();
4848

49-
response.write(ackEvent.toString());
50-
response.write(textEvent.toString());
51-
response.end(doneEvent.toString());
49+
response.write(ackEvent);
50+
response.write(textEvent);
51+
response.end(doneEvent);
5252
}
5353
```
5454

@@ -131,7 +131,7 @@ const payloadIsVerified = await verifyRequestPayload(
131131

132132
### Response
133133

134-
All `create*Event()` methods return an object with a `.toString()` method, which is called automatically when a string is expected. Unfortunately that's not the case for `response.write()`, you need to call `.toString()` explicitly.
134+
All `create*Event()` methods return a string that can directly be written to the response stream.
135135

136136
#### `createAckEvent()`
137137

@@ -141,7 +141,7 @@ The `ack` event should only be sent once.
141141
```js
142142
import { createAckEvent } from "@copilot-extensions/preview-sdk";
143143

144-
response.write(createAckEvent().toString());
144+
response.write(createAckEvent());
145145
```
146146

147147
#### `createTextEvent(message)`
@@ -151,8 +151,8 @@ Send a text message to the chat UI. Multiple messages can be sent. The `message`
151151
```js
152152
import { createTextEvent } from "@copilot-extensions/preview-sdk";
153153

154-
response.write(createTextEvent("Hello, world!").toString());
155-
response.write(createTextEvent("Hello, again!").toString());
154+
response.write(createTextEvent("Hello, world!"));
155+
response.write(createTextEvent("Hello, again!"));
156156
```
157157

158158
#### `createConfirmationEvent({ id, title, message, metadata })`
@@ -171,7 +171,7 @@ response.write(
171171
id: "123",
172172
title: "Are you sure?",
173173
message: "This will do something.",
174-
}).toString(),
174+
}),
175175
);
176176
```
177177

@@ -209,7 +209,7 @@ response.write(
209209
display_icon: "issue-opened",
210210
display_url: "https://github.com/monalisa/hello-world/issues/123",
211211
},
212-
]).toString()
212+
])
213213
);
214214
```
215215
@@ -231,7 +231,7 @@ The `done` event should only be sent once, at the end of the response. No furthe
231231
```js
232232
import { createDoneEvent } from "@copilot-extensions/preview-sdk";
233233

234-
response.write(createDoneEvent().toString());
234+
response.write(createDoneEvent());
235235
```
236236
237237
### Parsing

‎index.d.ts

+6-77
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ interface VerifyRequestByKeyIdInterface {
3333
// response types
3434

3535
export interface CreateAckEventInterface {
36-
(): ResponseEvent<"ack">;
36+
(): string;
3737
}
38-
3938
export interface CreateTextEventInterface {
40-
(message: string): ResponseEvent<"text">;
39+
(message: string): string;
4140
}
4241

4342
export type CreateConfirmationEventOptions = {
@@ -50,88 +49,18 @@ export type CreateConfirmationEventOptions = {
5049
export interface CreateConfirmationEventInterface {
5150
(
5251
options: CreateConfirmationEventOptions,
53-
): ResponseEvent<"copilot_confirmation">;
52+
): string;
5453
}
5554
export interface CreateReferencesEventInterface {
56-
(references: CopilotReference[]): ResponseEvent<"copilot_references">;
55+
(references: CopilotReference[]): string;
5756
}
5857
export interface CreateErrorsEventInterface {
59-
(errors: CopilotError[]): ResponseEvent<"copilot_errors">;
58+
(errors: CopilotError[]): string;
6059
}
6160
export interface CreateDoneEventInterface {
62-
(): ResponseEvent<"done">;
61+
(): string;
6362
}
6463

65-
type ResponseEventType =
66-
| "ack"
67-
| "done"
68-
| "text"
69-
| "copilot_references"
70-
| "copilot_confirmation"
71-
| "copilot_errors";
72-
type EventsWithoutEventKey = "ack" | "done" | "text";
73-
type ResponseEvent<T extends ResponseEventType = "text"> =
74-
T extends EventsWithoutEventKey
75-
? {
76-
data: T extends "ack"
77-
? CopilotAckResponseEventData
78-
: T extends "done"
79-
? CopilotDoneResponseEventData
80-
: T extends "text"
81-
? CopilotTextResponseEventData
82-
: never;
83-
toString: () => string;
84-
}
85-
: {
86-
event: T;
87-
data: T extends "copilot_references"
88-
? CopilotReferenceResponseEventData
89-
: T extends "copilot_confirmation"
90-
? CopilotConfirmationResponseEventData
91-
: T extends "copilot_errors"
92-
? CopilotErrorsResponseEventData
93-
: never;
94-
toString: () => string;
95-
};
96-
97-
type CopilotAckResponseEventData = {
98-
choices: [
99-
{
100-
delta: InteropMessage<"assistant">;
101-
},
102-
];
103-
};
104-
105-
type CopilotDoneResponseEventData = {
106-
choices: [
107-
{
108-
finish_reason: "stop";
109-
delta: {
110-
content: null;
111-
};
112-
},
113-
];
114-
};
115-
116-
type CopilotTextResponseEventData = {
117-
choices: [
118-
{
119-
delta: InteropMessage<"assistant">;
120-
},
121-
];
122-
};
123-
type CopilotConfirmationResponseEventData = {
124-
type: "action";
125-
title: string;
126-
message: string;
127-
confirmation?: {
128-
id: string;
129-
[key: string]: any;
130-
};
131-
};
132-
type CopilotErrorsResponseEventData = CopilotError[];
133-
type CopilotReferenceResponseEventData = CopilotReference[];
134-
13564
type CopilotError = {
13665
type: "reference" | "function" | "agent";
13766
code: string;

‎index.test-d.ts

+7-101
Original file line numberDiff line numberDiff line change
@@ -87,36 +87,12 @@ export async function fetchVerificationKeysTest() {
8787

8888
export function createAckEventTest() {
8989
const event = createAckEvent();
90-
expectType<() => string>(event.toString);
91-
expectType<string>(event.toString());
92-
93-
expectType<{
94-
choices: [
95-
{
96-
delta: InteropMessage<"assistant">;
97-
},
98-
];
99-
}>(event.data);
100-
101-
// @ts-expect-error - .event is required
102-
event.event;
90+
expectType<string>(event);
10391
}
10492

10593
export function createTextEventTest() {
10694
const event = createTextEvent("test");
107-
expectType<() => string>(event.toString);
108-
expectType<string>(event.toString());
109-
110-
expectType<{
111-
choices: [
112-
{
113-
delta: InteropMessage<"assistant">;
114-
},
115-
];
116-
}>(event.data);
117-
118-
// @ts-expect-error - .event is required
119-
event.event;
95+
expectType<string>(event);
12096
}
12197

12298
export function createConfirmationEventTest() {
@@ -125,31 +101,7 @@ export function createConfirmationEventTest() {
125101
title: "test",
126102
message: "test",
127103
});
128-
129-
// optional metadata
130-
createConfirmationEvent({
131-
id: "test",
132-
title: "test",
133-
message: "test",
134-
metadata: {
135-
someOtherId: "test",
136-
},
137-
});
138-
139-
expectType<() => string>(event.toString);
140-
expectType<string>(event.toString());
141-
142-
expectType<{
143-
type: "action";
144-
title: string;
145-
message: string;
146-
confirmation?: {
147-
id: string;
148-
[key: string]: any;
149-
};
150-
}>(event.data);
151-
152-
expectType<"copilot_confirmation">(event.event);
104+
expectType<string>(event);
153105
}
154106

155107
export function createReferencesEventTest() {
@@ -172,26 +124,7 @@ export function createReferencesEventTest() {
172124
},
173125
},
174126
]);
175-
expectType<() => string>(event.toString);
176-
expectType<string>(event.toString());
177-
178-
expectType<
179-
{
180-
type: string;
181-
id: string;
182-
data?: {
183-
[key: string]: unknown;
184-
};
185-
is_implicit?: boolean;
186-
metadata?: {
187-
display_name: string;
188-
display_icon?: string;
189-
display_url?: string;
190-
};
191-
}[]
192-
>(event.data);
193-
194-
expectType<"copilot_references">(event.event);
127+
expectType<string>(event);
195128
}
196129

197130
export function createErrorsEventTest() {
@@ -215,39 +148,12 @@ export function createErrorsEventTest() {
215148
identifier: "agent-identifier",
216149
},
217150
]);
218-
expectType<() => string>(event.toString);
219-
expectType<string>(event.toString());
220-
221-
expectType<
222-
{
223-
type: "reference" | "function" | "agent";
224-
code: string;
225-
message: string;
226-
identifier: string;
227-
}[]
228-
>(event.data);
229-
230-
expectType<"copilot_errors">(event.event);
151+
expectType<string>(event);
231152
}
232153

233154
export function createDoneEventTest() {
234155
const event = createDoneEvent();
235-
expectType<() => string>(event.toString);
236-
expectType<string>(event.toString());
237-
238-
expectType<{
239-
choices: [
240-
{
241-
finish_reason: "stop";
242-
delta: {
243-
content: null;
244-
};
245-
},
246-
];
247-
}>(event.data);
248-
249-
// @ts-expect-error - .event is required
250-
event.event;
156+
expectType<string>(event);
251157
}
252158

253159
export function parseRequestBodyTest(body: string) {
@@ -312,7 +218,7 @@ export async function promptTest() {
312218
await prompt("What is the capital of France?", {
313219
token: "secret",
314220
request: {
315-
fetch: () => {},
221+
fetch: () => { },
316222
},
317223
});
318224

0 commit comments

Comments
 (0)
Failed to load comments.