@yourgpt/llm-sdk 0.1.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +65 -2
- package/dist/index.d.ts +65 -2
- package/dist/index.js +68 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +68 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -3430,8 +3430,28 @@ var Runtime = class {
|
|
|
3430
3430
|
}
|
|
3431
3431
|
/**
|
|
3432
3432
|
* Handle HTTP request (for use with any framework)
|
|
3433
|
+
*
|
|
3434
|
+
* @param request - The HTTP request
|
|
3435
|
+
* @param options - Optional configuration including onFinish callback for persistence
|
|
3436
|
+
*
|
|
3437
|
+
* @example
|
|
3438
|
+
* ```typescript
|
|
3439
|
+
* // Basic usage
|
|
3440
|
+
* return runtime.handleRequest(request);
|
|
3441
|
+
*
|
|
3442
|
+
* // With server-side persistence
|
|
3443
|
+
* return runtime.handleRequest(request, {
|
|
3444
|
+
* onFinish: async ({ messages, threadId }) => {
|
|
3445
|
+
* await db.thread.upsert({
|
|
3446
|
+
* where: { id: threadId },
|
|
3447
|
+
* update: { messages, updatedAt: new Date() },
|
|
3448
|
+
* create: { id: threadId, messages },
|
|
3449
|
+
* });
|
|
3450
|
+
* },
|
|
3451
|
+
* });
|
|
3452
|
+
* ```
|
|
3433
3453
|
*/
|
|
3434
|
-
async handleRequest(request) {
|
|
3454
|
+
async handleRequest(request, options) {
|
|
3435
3455
|
try {
|
|
3436
3456
|
const body = await request.json();
|
|
3437
3457
|
if (this.config.debug) {
|
|
@@ -3445,11 +3465,17 @@ var Runtime = class {
|
|
|
3445
3465
|
body,
|
|
3446
3466
|
signal,
|
|
3447
3467
|
useAgentLoop || false,
|
|
3448
|
-
request
|
|
3468
|
+
request,
|
|
3469
|
+
options
|
|
3449
3470
|
);
|
|
3450
3471
|
}
|
|
3451
3472
|
const generator = useAgentLoop ? this.processChatWithLoop(body, signal, void 0, void 0, request) : this.processChat(body, signal);
|
|
3452
|
-
|
|
3473
|
+
const wrappedGenerator = this.wrapGeneratorWithOnFinish(
|
|
3474
|
+
generator,
|
|
3475
|
+
body.threadId,
|
|
3476
|
+
options
|
|
3477
|
+
);
|
|
3478
|
+
return createSSEResponse(wrappedGenerator);
|
|
3453
3479
|
} catch (error) {
|
|
3454
3480
|
console.error("[Copilot SDK] Error:", error);
|
|
3455
3481
|
return new Response(
|
|
@@ -3463,10 +3489,34 @@ var Runtime = class {
|
|
|
3463
3489
|
);
|
|
3464
3490
|
}
|
|
3465
3491
|
}
|
|
3492
|
+
/**
|
|
3493
|
+
* Wrap a generator to intercept the done event and call onFinish
|
|
3494
|
+
*/
|
|
3495
|
+
async *wrapGeneratorWithOnFinish(generator, threadId, options) {
|
|
3496
|
+
let doneMessages;
|
|
3497
|
+
for await (const event of generator) {
|
|
3498
|
+
if (event.type === "done" && event.messages) {
|
|
3499
|
+
doneMessages = event.messages;
|
|
3500
|
+
}
|
|
3501
|
+
yield event;
|
|
3502
|
+
}
|
|
3503
|
+
if (options?.onFinish && doneMessages) {
|
|
3504
|
+
try {
|
|
3505
|
+
const result = {
|
|
3506
|
+
messages: doneMessages,
|
|
3507
|
+
threadId
|
|
3508
|
+
// TODO: Add usage tracking when available from adapter
|
|
3509
|
+
};
|
|
3510
|
+
await options.onFinish(result);
|
|
3511
|
+
} catch (error) {
|
|
3512
|
+
console.error("[Copilot SDK] onFinish callback error:", error);
|
|
3513
|
+
}
|
|
3514
|
+
}
|
|
3515
|
+
}
|
|
3466
3516
|
/**
|
|
3467
3517
|
* Handle non-streaming request - returns JSON instead of SSE
|
|
3468
3518
|
*/
|
|
3469
|
-
async handleNonStreamingRequest(body, signal, useAgentLoop, httpRequest) {
|
|
3519
|
+
async handleNonStreamingRequest(body, signal, useAgentLoop, httpRequest, options) {
|
|
3470
3520
|
try {
|
|
3471
3521
|
const generator = useAgentLoop ? this.processChatWithLoop(
|
|
3472
3522
|
body,
|
|
@@ -3518,6 +3568,20 @@ var Runtime = class {
|
|
|
3518
3568
|
break;
|
|
3519
3569
|
}
|
|
3520
3570
|
}
|
|
3571
|
+
if (options?.onFinish && messages && !error) {
|
|
3572
|
+
try {
|
|
3573
|
+
const result = {
|
|
3574
|
+
messages,
|
|
3575
|
+
threadId: body.threadId
|
|
3576
|
+
};
|
|
3577
|
+
await options.onFinish(result);
|
|
3578
|
+
} catch (callbackError) {
|
|
3579
|
+
console.error(
|
|
3580
|
+
"[Copilot SDK] onFinish callback error:",
|
|
3581
|
+
callbackError
|
|
3582
|
+
);
|
|
3583
|
+
}
|
|
3584
|
+
}
|
|
3521
3585
|
const response = {
|
|
3522
3586
|
success: !error,
|
|
3523
3587
|
content,
|