@yourgpt/llm-sdk 1.5.2 → 1.5.4
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 +167 -13
- package/dist/index.d.ts +167 -13
- package/dist/index.js +189 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +189 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.d.mts
CHANGED
|
@@ -413,6 +413,29 @@ interface HandleRequestOptions {
|
|
|
413
413
|
*/
|
|
414
414
|
onFinish?: (result: HandleRequestResult) => Promise<void> | void;
|
|
415
415
|
}
|
|
416
|
+
/**
|
|
417
|
+
* Options for runtime.generate()
|
|
418
|
+
*
|
|
419
|
+
* @example
|
|
420
|
+
* ```typescript
|
|
421
|
+
* const result = await runtime.generate(body, {
|
|
422
|
+
* onFinish: async ({ messages }) => {
|
|
423
|
+
* await db.saveMessages(messages);
|
|
424
|
+
* },
|
|
425
|
+
* });
|
|
426
|
+
* ```
|
|
427
|
+
*/
|
|
428
|
+
interface GenerateOptions {
|
|
429
|
+
/** AbortSignal for cancellation */
|
|
430
|
+
signal?: AbortSignal;
|
|
431
|
+
/** HTTP request for extracting headers (auth context) */
|
|
432
|
+
httpRequest?: Request;
|
|
433
|
+
/**
|
|
434
|
+
* Called after generation completes successfully.
|
|
435
|
+
* Use for server-side persistence.
|
|
436
|
+
*/
|
|
437
|
+
onFinish?: (result: HandleRequestResult) => Promise<void> | void;
|
|
438
|
+
}
|
|
416
439
|
|
|
417
440
|
/**
|
|
418
441
|
* StreamResult - Industry-standard streaming result object
|
|
@@ -677,6 +700,118 @@ declare class StreamResult {
|
|
|
677
700
|
*/
|
|
678
701
|
declare function createStreamResult(generator: AsyncGenerator<StreamEvent>): StreamResult;
|
|
679
702
|
|
|
703
|
+
/**
|
|
704
|
+
* GenerateResult - Result from non-streaming generation
|
|
705
|
+
*
|
|
706
|
+
* Similar to Vercel AI SDK's generateText() result.
|
|
707
|
+
* Provides both raw access and formatted response methods.
|
|
708
|
+
*
|
|
709
|
+
* @example
|
|
710
|
+
* ```typescript
|
|
711
|
+
* const result = await runtime.generate(body);
|
|
712
|
+
*
|
|
713
|
+
* // Raw access
|
|
714
|
+
* console.log(result.text);
|
|
715
|
+
* console.log(result.toolCalls);
|
|
716
|
+
*
|
|
717
|
+
* // CopilotChat format
|
|
718
|
+
* res.json(result.toResponse());
|
|
719
|
+
* ```
|
|
720
|
+
*/
|
|
721
|
+
|
|
722
|
+
interface GenerateResultData {
|
|
723
|
+
text: string;
|
|
724
|
+
messages: DoneEventMessage[];
|
|
725
|
+
toolCalls: Array<{
|
|
726
|
+
id: string;
|
|
727
|
+
name: string;
|
|
728
|
+
args: Record<string, unknown>;
|
|
729
|
+
}>;
|
|
730
|
+
toolResults: Array<{
|
|
731
|
+
id: string;
|
|
732
|
+
result: unknown;
|
|
733
|
+
}>;
|
|
734
|
+
requiresAction: boolean;
|
|
735
|
+
error?: {
|
|
736
|
+
message: string;
|
|
737
|
+
code?: string;
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* Response format compatible with CopilotChat
|
|
742
|
+
*/
|
|
743
|
+
interface CopilotChatResponse {
|
|
744
|
+
success: boolean;
|
|
745
|
+
content: string;
|
|
746
|
+
messages?: DoneEventMessage[];
|
|
747
|
+
toolCalls?: Array<{
|
|
748
|
+
id: string;
|
|
749
|
+
name: string;
|
|
750
|
+
args: Record<string, unknown>;
|
|
751
|
+
}>;
|
|
752
|
+
toolResults?: Array<{
|
|
753
|
+
id: string;
|
|
754
|
+
result: unknown;
|
|
755
|
+
}>;
|
|
756
|
+
requiresAction?: boolean;
|
|
757
|
+
error?: {
|
|
758
|
+
message: string;
|
|
759
|
+
code?: string;
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* GenerateResult - Non-streaming generation result
|
|
764
|
+
*/
|
|
765
|
+
declare class GenerateResult {
|
|
766
|
+
private data;
|
|
767
|
+
constructor(data: GenerateResultData);
|
|
768
|
+
/**
|
|
769
|
+
* Generated text content
|
|
770
|
+
*/
|
|
771
|
+
get text(): string;
|
|
772
|
+
/**
|
|
773
|
+
* All messages from the conversation
|
|
774
|
+
*/
|
|
775
|
+
get messages(): DoneEventMessage[];
|
|
776
|
+
/**
|
|
777
|
+
* Tool calls made during generation
|
|
778
|
+
*/
|
|
779
|
+
get toolCalls(): GenerateResultData["toolCalls"];
|
|
780
|
+
/**
|
|
781
|
+
* Results from tool executions
|
|
782
|
+
*/
|
|
783
|
+
get toolResults(): GenerateResultData["toolResults"];
|
|
784
|
+
/**
|
|
785
|
+
* Whether client action is required (e.g., tool approval)
|
|
786
|
+
*/
|
|
787
|
+
get requiresAction(): boolean;
|
|
788
|
+
/**
|
|
789
|
+
* Error if generation failed
|
|
790
|
+
*/
|
|
791
|
+
get error(): GenerateResultData["error"];
|
|
792
|
+
/**
|
|
793
|
+
* Whether generation was successful
|
|
794
|
+
*/
|
|
795
|
+
get success(): boolean;
|
|
796
|
+
/**
|
|
797
|
+
* Convert to CopilotChat-compatible JSON response
|
|
798
|
+
*
|
|
799
|
+
* @example
|
|
800
|
+
* ```typescript
|
|
801
|
+
* // Express
|
|
802
|
+
* res.json(result.toResponse());
|
|
803
|
+
*
|
|
804
|
+
* // Next.js
|
|
805
|
+
* return Response.json(result.toResponse());
|
|
806
|
+
* ```
|
|
807
|
+
*/
|
|
808
|
+
toResponse(): CopilotChatResponse;
|
|
809
|
+
/**
|
|
810
|
+
* Convert to raw object (without methods)
|
|
811
|
+
*/
|
|
812
|
+
toJSON(): GenerateResultData;
|
|
813
|
+
}
|
|
814
|
+
|
|
680
815
|
/**
|
|
681
816
|
* Copilot SDK Runtime
|
|
682
817
|
*
|
|
@@ -860,23 +995,42 @@ declare class Runtime {
|
|
|
860
995
|
* console.log('Response:', text);
|
|
861
996
|
* res.json({ response: text });
|
|
862
997
|
*
|
|
863
|
-
* //
|
|
864
|
-
* const
|
|
865
|
-
*
|
|
866
|
-
*
|
|
998
|
+
* // Usage is included in result - strip before sending to client
|
|
999
|
+
* const { usage, ...clientResult } = await runtime.chat(body);
|
|
1000
|
+
* await billing.record(usage);
|
|
1001
|
+
* res.json(clientResult);
|
|
867
1002
|
* ```
|
|
868
1003
|
*/
|
|
869
1004
|
chat(request: ChatRequest, options?: {
|
|
870
1005
|
signal?: AbortSignal;
|
|
871
|
-
onFinish?: (result: {
|
|
872
|
-
messages: DoneEventMessage[];
|
|
873
|
-
usage?: {
|
|
874
|
-
promptTokens: number;
|
|
875
|
-
completionTokens: number;
|
|
876
|
-
totalTokens: number;
|
|
877
|
-
};
|
|
878
|
-
}) => Promise<void> | void;
|
|
879
1006
|
}): Promise<CollectedResult>;
|
|
1007
|
+
/**
|
|
1008
|
+
* Generate a complete response (non-streaming)
|
|
1009
|
+
*
|
|
1010
|
+
* Like Vercel AI SDK's generateText() - clean, non-streaming API.
|
|
1011
|
+
* Returns GenerateResult with .toResponse() for CopilotChat format.
|
|
1012
|
+
*
|
|
1013
|
+
* @example
|
|
1014
|
+
* ```typescript
|
|
1015
|
+
* // Simple usage
|
|
1016
|
+
* const result = await runtime.generate(body);
|
|
1017
|
+
* console.log(result.text);
|
|
1018
|
+
*
|
|
1019
|
+
* // CopilotChat format response (Express)
|
|
1020
|
+
* res.json(result.toResponse());
|
|
1021
|
+
*
|
|
1022
|
+
* // CopilotChat format response (Next.js)
|
|
1023
|
+
* return Response.json(result.toResponse());
|
|
1024
|
+
*
|
|
1025
|
+
* // With persistence callback
|
|
1026
|
+
* const result = await runtime.generate(body, {
|
|
1027
|
+
* onFinish: async ({ messages }) => {
|
|
1028
|
+
* await db.saveMessages(messages);
|
|
1029
|
+
* },
|
|
1030
|
+
* });
|
|
1031
|
+
* ```
|
|
1032
|
+
*/
|
|
1033
|
+
generate(request: ChatRequest, options?: GenerateOptions): Promise<GenerateResult>;
|
|
880
1034
|
/**
|
|
881
1035
|
* Create Express-compatible handler middleware
|
|
882
1036
|
*
|
|
@@ -1179,4 +1333,4 @@ declare function generateThreadId(): string;
|
|
|
1179
1333
|
*/
|
|
1180
1334
|
declare function generateToolCallId(): string;
|
|
1181
1335
|
|
|
1182
|
-
export { AIProvider, ActionDefinition, type ActionRequest, AgentLoopConfig, type AgentLoopOptions, type ChatRequest, type CollectedResult, DEFAULT_MAX_ITERATIONS, DoneEventMessage, GenerateTextParams, GenerateTextResult, LLMAdapter, Message, type RequestContext, Runtime, type RuntimeConfig, StreamEvent, StreamResult, type StreamResultOptions, StreamTextParams, StreamTextResult, TokenUsageRaw, Tool, ToolCallInfo, ToolContext, ToolDefinition, ToolResponse, createEventStream, createExpressHandler, createExpressMiddleware, createHonoApp, createNextHandler, createNodeHandler, createRuntime, createSSEHeaders, createSSEResponse, createStreamResult, createTextStreamHeaders, createTextStreamResponse, formatSSEData, formatToolsForAnthropic, formatToolsForGoogle, formatToolsForOpenAI, generateMessageId, generateText, generateThreadId, generateToolCallId, pipeSSEToResponse, pipeTextToResponse, runAgentLoop, streamText, tool };
|
|
1336
|
+
export { AIProvider, ActionDefinition, type ActionRequest, AgentLoopConfig, type AgentLoopOptions, type ChatRequest, type CollectedResult, type CopilotChatResponse, DEFAULT_MAX_ITERATIONS, DoneEventMessage, GenerateResult, type GenerateResultData, GenerateTextParams, GenerateTextResult, LLMAdapter, Message, type RequestContext, Runtime, type RuntimeConfig, StreamEvent, StreamResult, type StreamResultOptions, StreamTextParams, StreamTextResult, TokenUsageRaw, Tool, ToolCallInfo, ToolContext, ToolDefinition, ToolResponse, createEventStream, createExpressHandler, createExpressMiddleware, createHonoApp, createNextHandler, createNodeHandler, createRuntime, createSSEHeaders, createSSEResponse, createStreamResult, createTextStreamHeaders, createTextStreamResponse, formatSSEData, formatToolsForAnthropic, formatToolsForGoogle, formatToolsForOpenAI, generateMessageId, generateText, generateThreadId, generateToolCallId, pipeSSEToResponse, pipeTextToResponse, runAgentLoop, streamText, tool };
|
package/dist/index.d.ts
CHANGED
|
@@ -413,6 +413,29 @@ interface HandleRequestOptions {
|
|
|
413
413
|
*/
|
|
414
414
|
onFinish?: (result: HandleRequestResult) => Promise<void> | void;
|
|
415
415
|
}
|
|
416
|
+
/**
|
|
417
|
+
* Options for runtime.generate()
|
|
418
|
+
*
|
|
419
|
+
* @example
|
|
420
|
+
* ```typescript
|
|
421
|
+
* const result = await runtime.generate(body, {
|
|
422
|
+
* onFinish: async ({ messages }) => {
|
|
423
|
+
* await db.saveMessages(messages);
|
|
424
|
+
* },
|
|
425
|
+
* });
|
|
426
|
+
* ```
|
|
427
|
+
*/
|
|
428
|
+
interface GenerateOptions {
|
|
429
|
+
/** AbortSignal for cancellation */
|
|
430
|
+
signal?: AbortSignal;
|
|
431
|
+
/** HTTP request for extracting headers (auth context) */
|
|
432
|
+
httpRequest?: Request;
|
|
433
|
+
/**
|
|
434
|
+
* Called after generation completes successfully.
|
|
435
|
+
* Use for server-side persistence.
|
|
436
|
+
*/
|
|
437
|
+
onFinish?: (result: HandleRequestResult) => Promise<void> | void;
|
|
438
|
+
}
|
|
416
439
|
|
|
417
440
|
/**
|
|
418
441
|
* StreamResult - Industry-standard streaming result object
|
|
@@ -677,6 +700,118 @@ declare class StreamResult {
|
|
|
677
700
|
*/
|
|
678
701
|
declare function createStreamResult(generator: AsyncGenerator<StreamEvent>): StreamResult;
|
|
679
702
|
|
|
703
|
+
/**
|
|
704
|
+
* GenerateResult - Result from non-streaming generation
|
|
705
|
+
*
|
|
706
|
+
* Similar to Vercel AI SDK's generateText() result.
|
|
707
|
+
* Provides both raw access and formatted response methods.
|
|
708
|
+
*
|
|
709
|
+
* @example
|
|
710
|
+
* ```typescript
|
|
711
|
+
* const result = await runtime.generate(body);
|
|
712
|
+
*
|
|
713
|
+
* // Raw access
|
|
714
|
+
* console.log(result.text);
|
|
715
|
+
* console.log(result.toolCalls);
|
|
716
|
+
*
|
|
717
|
+
* // CopilotChat format
|
|
718
|
+
* res.json(result.toResponse());
|
|
719
|
+
* ```
|
|
720
|
+
*/
|
|
721
|
+
|
|
722
|
+
interface GenerateResultData {
|
|
723
|
+
text: string;
|
|
724
|
+
messages: DoneEventMessage[];
|
|
725
|
+
toolCalls: Array<{
|
|
726
|
+
id: string;
|
|
727
|
+
name: string;
|
|
728
|
+
args: Record<string, unknown>;
|
|
729
|
+
}>;
|
|
730
|
+
toolResults: Array<{
|
|
731
|
+
id: string;
|
|
732
|
+
result: unknown;
|
|
733
|
+
}>;
|
|
734
|
+
requiresAction: boolean;
|
|
735
|
+
error?: {
|
|
736
|
+
message: string;
|
|
737
|
+
code?: string;
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* Response format compatible with CopilotChat
|
|
742
|
+
*/
|
|
743
|
+
interface CopilotChatResponse {
|
|
744
|
+
success: boolean;
|
|
745
|
+
content: string;
|
|
746
|
+
messages?: DoneEventMessage[];
|
|
747
|
+
toolCalls?: Array<{
|
|
748
|
+
id: string;
|
|
749
|
+
name: string;
|
|
750
|
+
args: Record<string, unknown>;
|
|
751
|
+
}>;
|
|
752
|
+
toolResults?: Array<{
|
|
753
|
+
id: string;
|
|
754
|
+
result: unknown;
|
|
755
|
+
}>;
|
|
756
|
+
requiresAction?: boolean;
|
|
757
|
+
error?: {
|
|
758
|
+
message: string;
|
|
759
|
+
code?: string;
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* GenerateResult - Non-streaming generation result
|
|
764
|
+
*/
|
|
765
|
+
declare class GenerateResult {
|
|
766
|
+
private data;
|
|
767
|
+
constructor(data: GenerateResultData);
|
|
768
|
+
/**
|
|
769
|
+
* Generated text content
|
|
770
|
+
*/
|
|
771
|
+
get text(): string;
|
|
772
|
+
/**
|
|
773
|
+
* All messages from the conversation
|
|
774
|
+
*/
|
|
775
|
+
get messages(): DoneEventMessage[];
|
|
776
|
+
/**
|
|
777
|
+
* Tool calls made during generation
|
|
778
|
+
*/
|
|
779
|
+
get toolCalls(): GenerateResultData["toolCalls"];
|
|
780
|
+
/**
|
|
781
|
+
* Results from tool executions
|
|
782
|
+
*/
|
|
783
|
+
get toolResults(): GenerateResultData["toolResults"];
|
|
784
|
+
/**
|
|
785
|
+
* Whether client action is required (e.g., tool approval)
|
|
786
|
+
*/
|
|
787
|
+
get requiresAction(): boolean;
|
|
788
|
+
/**
|
|
789
|
+
* Error if generation failed
|
|
790
|
+
*/
|
|
791
|
+
get error(): GenerateResultData["error"];
|
|
792
|
+
/**
|
|
793
|
+
* Whether generation was successful
|
|
794
|
+
*/
|
|
795
|
+
get success(): boolean;
|
|
796
|
+
/**
|
|
797
|
+
* Convert to CopilotChat-compatible JSON response
|
|
798
|
+
*
|
|
799
|
+
* @example
|
|
800
|
+
* ```typescript
|
|
801
|
+
* // Express
|
|
802
|
+
* res.json(result.toResponse());
|
|
803
|
+
*
|
|
804
|
+
* // Next.js
|
|
805
|
+
* return Response.json(result.toResponse());
|
|
806
|
+
* ```
|
|
807
|
+
*/
|
|
808
|
+
toResponse(): CopilotChatResponse;
|
|
809
|
+
/**
|
|
810
|
+
* Convert to raw object (without methods)
|
|
811
|
+
*/
|
|
812
|
+
toJSON(): GenerateResultData;
|
|
813
|
+
}
|
|
814
|
+
|
|
680
815
|
/**
|
|
681
816
|
* Copilot SDK Runtime
|
|
682
817
|
*
|
|
@@ -860,23 +995,42 @@ declare class Runtime {
|
|
|
860
995
|
* console.log('Response:', text);
|
|
861
996
|
* res.json({ response: text });
|
|
862
997
|
*
|
|
863
|
-
* //
|
|
864
|
-
* const
|
|
865
|
-
*
|
|
866
|
-
*
|
|
998
|
+
* // Usage is included in result - strip before sending to client
|
|
999
|
+
* const { usage, ...clientResult } = await runtime.chat(body);
|
|
1000
|
+
* await billing.record(usage);
|
|
1001
|
+
* res.json(clientResult);
|
|
867
1002
|
* ```
|
|
868
1003
|
*/
|
|
869
1004
|
chat(request: ChatRequest, options?: {
|
|
870
1005
|
signal?: AbortSignal;
|
|
871
|
-
onFinish?: (result: {
|
|
872
|
-
messages: DoneEventMessage[];
|
|
873
|
-
usage?: {
|
|
874
|
-
promptTokens: number;
|
|
875
|
-
completionTokens: number;
|
|
876
|
-
totalTokens: number;
|
|
877
|
-
};
|
|
878
|
-
}) => Promise<void> | void;
|
|
879
1006
|
}): Promise<CollectedResult>;
|
|
1007
|
+
/**
|
|
1008
|
+
* Generate a complete response (non-streaming)
|
|
1009
|
+
*
|
|
1010
|
+
* Like Vercel AI SDK's generateText() - clean, non-streaming API.
|
|
1011
|
+
* Returns GenerateResult with .toResponse() for CopilotChat format.
|
|
1012
|
+
*
|
|
1013
|
+
* @example
|
|
1014
|
+
* ```typescript
|
|
1015
|
+
* // Simple usage
|
|
1016
|
+
* const result = await runtime.generate(body);
|
|
1017
|
+
* console.log(result.text);
|
|
1018
|
+
*
|
|
1019
|
+
* // CopilotChat format response (Express)
|
|
1020
|
+
* res.json(result.toResponse());
|
|
1021
|
+
*
|
|
1022
|
+
* // CopilotChat format response (Next.js)
|
|
1023
|
+
* return Response.json(result.toResponse());
|
|
1024
|
+
*
|
|
1025
|
+
* // With persistence callback
|
|
1026
|
+
* const result = await runtime.generate(body, {
|
|
1027
|
+
* onFinish: async ({ messages }) => {
|
|
1028
|
+
* await db.saveMessages(messages);
|
|
1029
|
+
* },
|
|
1030
|
+
* });
|
|
1031
|
+
* ```
|
|
1032
|
+
*/
|
|
1033
|
+
generate(request: ChatRequest, options?: GenerateOptions): Promise<GenerateResult>;
|
|
880
1034
|
/**
|
|
881
1035
|
* Create Express-compatible handler middleware
|
|
882
1036
|
*
|
|
@@ -1179,4 +1333,4 @@ declare function generateThreadId(): string;
|
|
|
1179
1333
|
*/
|
|
1180
1334
|
declare function generateToolCallId(): string;
|
|
1181
1335
|
|
|
1182
|
-
export { AIProvider, ActionDefinition, type ActionRequest, AgentLoopConfig, type AgentLoopOptions, type ChatRequest, type CollectedResult, DEFAULT_MAX_ITERATIONS, DoneEventMessage, GenerateTextParams, GenerateTextResult, LLMAdapter, Message, type RequestContext, Runtime, type RuntimeConfig, StreamEvent, StreamResult, type StreamResultOptions, StreamTextParams, StreamTextResult, TokenUsageRaw, Tool, ToolCallInfo, ToolContext, ToolDefinition, ToolResponse, createEventStream, createExpressHandler, createExpressMiddleware, createHonoApp, createNextHandler, createNodeHandler, createRuntime, createSSEHeaders, createSSEResponse, createStreamResult, createTextStreamHeaders, createTextStreamResponse, formatSSEData, formatToolsForAnthropic, formatToolsForGoogle, formatToolsForOpenAI, generateMessageId, generateText, generateThreadId, generateToolCallId, pipeSSEToResponse, pipeTextToResponse, runAgentLoop, streamText, tool };
|
|
1336
|
+
export { AIProvider, ActionDefinition, type ActionRequest, AgentLoopConfig, type AgentLoopOptions, type ChatRequest, type CollectedResult, type CopilotChatResponse, DEFAULT_MAX_ITERATIONS, DoneEventMessage, GenerateResult, type GenerateResultData, GenerateTextParams, GenerateTextResult, LLMAdapter, Message, type RequestContext, Runtime, type RuntimeConfig, StreamEvent, StreamResult, type StreamResultOptions, StreamTextParams, StreamTextResult, TokenUsageRaw, Tool, ToolCallInfo, ToolContext, ToolDefinition, ToolResponse, createEventStream, createExpressHandler, createExpressMiddleware, createHonoApp, createNextHandler, createNodeHandler, createRuntime, createSSEHeaders, createSSEResponse, createStreamResult, createTextStreamHeaders, createTextStreamResponse, formatSSEData, formatToolsForAnthropic, formatToolsForGoogle, formatToolsForOpenAI, generateMessageId, generateText, generateThreadId, generateToolCallId, pipeSSEToResponse, pipeTextToResponse, runAgentLoop, streamText, tool };
|
package/dist/index.js
CHANGED
|
@@ -1909,6 +1909,84 @@ function createStreamResult(generator) {
|
|
|
1909
1909
|
return new StreamResult(generator);
|
|
1910
1910
|
}
|
|
1911
1911
|
|
|
1912
|
+
// src/server/generate-result.ts
|
|
1913
|
+
var GenerateResult = class {
|
|
1914
|
+
constructor(data) {
|
|
1915
|
+
this.data = data;
|
|
1916
|
+
}
|
|
1917
|
+
/**
|
|
1918
|
+
* Generated text content
|
|
1919
|
+
*/
|
|
1920
|
+
get text() {
|
|
1921
|
+
return this.data.text;
|
|
1922
|
+
}
|
|
1923
|
+
/**
|
|
1924
|
+
* All messages from the conversation
|
|
1925
|
+
*/
|
|
1926
|
+
get messages() {
|
|
1927
|
+
return this.data.messages;
|
|
1928
|
+
}
|
|
1929
|
+
/**
|
|
1930
|
+
* Tool calls made during generation
|
|
1931
|
+
*/
|
|
1932
|
+
get toolCalls() {
|
|
1933
|
+
return this.data.toolCalls;
|
|
1934
|
+
}
|
|
1935
|
+
/**
|
|
1936
|
+
* Results from tool executions
|
|
1937
|
+
*/
|
|
1938
|
+
get toolResults() {
|
|
1939
|
+
return this.data.toolResults;
|
|
1940
|
+
}
|
|
1941
|
+
/**
|
|
1942
|
+
* Whether client action is required (e.g., tool approval)
|
|
1943
|
+
*/
|
|
1944
|
+
get requiresAction() {
|
|
1945
|
+
return this.data.requiresAction;
|
|
1946
|
+
}
|
|
1947
|
+
/**
|
|
1948
|
+
* Error if generation failed
|
|
1949
|
+
*/
|
|
1950
|
+
get error() {
|
|
1951
|
+
return this.data.error;
|
|
1952
|
+
}
|
|
1953
|
+
/**
|
|
1954
|
+
* Whether generation was successful
|
|
1955
|
+
*/
|
|
1956
|
+
get success() {
|
|
1957
|
+
return !this.data.error;
|
|
1958
|
+
}
|
|
1959
|
+
/**
|
|
1960
|
+
* Convert to CopilotChat-compatible JSON response
|
|
1961
|
+
*
|
|
1962
|
+
* @example
|
|
1963
|
+
* ```typescript
|
|
1964
|
+
* // Express
|
|
1965
|
+
* res.json(result.toResponse());
|
|
1966
|
+
*
|
|
1967
|
+
* // Next.js
|
|
1968
|
+
* return Response.json(result.toResponse());
|
|
1969
|
+
* ```
|
|
1970
|
+
*/
|
|
1971
|
+
toResponse() {
|
|
1972
|
+
return {
|
|
1973
|
+
success: this.success,
|
|
1974
|
+
content: this.data.text,
|
|
1975
|
+
messages: this.data.messages.length > 0 ? this.data.messages : void 0,
|
|
1976
|
+
toolCalls: this.data.toolCalls.length > 0 ? this.data.toolCalls : void 0,
|
|
1977
|
+
toolResults: this.data.toolResults.length > 0 ? this.data.toolResults : void 0,
|
|
1978
|
+
requiresAction: this.data.requiresAction || void 0,
|
|
1979
|
+
error: this.data.error
|
|
1980
|
+
};
|
|
1981
|
+
}
|
|
1982
|
+
/**
|
|
1983
|
+
* Convert to raw object (without methods)
|
|
1984
|
+
*/
|
|
1985
|
+
toJSON() {
|
|
1986
|
+
return { ...this.data };
|
|
1987
|
+
}
|
|
1988
|
+
};
|
|
1989
|
+
|
|
1912
1990
|
// src/server/runtime.ts
|
|
1913
1991
|
function buildToolResultForAI(tool2, result, args) {
|
|
1914
1992
|
const typedResult = result;
|
|
@@ -3071,14 +3149,119 @@ var Runtime = class {
|
|
|
3071
3149
|
* console.log('Response:', text);
|
|
3072
3150
|
* res.json({ response: text });
|
|
3073
3151
|
*
|
|
3074
|
-
* //
|
|
3075
|
-
* const
|
|
3076
|
-
*
|
|
3077
|
-
*
|
|
3152
|
+
* // Usage is included in result - strip before sending to client
|
|
3153
|
+
* const { usage, ...clientResult } = await runtime.chat(body);
|
|
3154
|
+
* await billing.record(usage);
|
|
3155
|
+
* res.json(clientResult);
|
|
3078
3156
|
* ```
|
|
3079
3157
|
*/
|
|
3080
3158
|
async chat(request, options) {
|
|
3081
|
-
return this.stream(request, options).collect(
|
|
3159
|
+
return this.stream(request, { signal: options?.signal }).collect({
|
|
3160
|
+
includeUsage: true
|
|
3161
|
+
});
|
|
3162
|
+
}
|
|
3163
|
+
/**
|
|
3164
|
+
* Generate a complete response (non-streaming)
|
|
3165
|
+
*
|
|
3166
|
+
* Like Vercel AI SDK's generateText() - clean, non-streaming API.
|
|
3167
|
+
* Returns GenerateResult with .toResponse() for CopilotChat format.
|
|
3168
|
+
*
|
|
3169
|
+
* @example
|
|
3170
|
+
* ```typescript
|
|
3171
|
+
* // Simple usage
|
|
3172
|
+
* const result = await runtime.generate(body);
|
|
3173
|
+
* console.log(result.text);
|
|
3174
|
+
*
|
|
3175
|
+
* // CopilotChat format response (Express)
|
|
3176
|
+
* res.json(result.toResponse());
|
|
3177
|
+
*
|
|
3178
|
+
* // CopilotChat format response (Next.js)
|
|
3179
|
+
* return Response.json(result.toResponse());
|
|
3180
|
+
*
|
|
3181
|
+
* // With persistence callback
|
|
3182
|
+
* const result = await runtime.generate(body, {
|
|
3183
|
+
* onFinish: async ({ messages }) => {
|
|
3184
|
+
* await db.saveMessages(messages);
|
|
3185
|
+
* },
|
|
3186
|
+
* });
|
|
3187
|
+
* ```
|
|
3188
|
+
*/
|
|
3189
|
+
async generate(request, options) {
|
|
3190
|
+
const generator = this.processChatWithLoop(
|
|
3191
|
+
{ ...request, streaming: false },
|
|
3192
|
+
options?.signal,
|
|
3193
|
+
void 0,
|
|
3194
|
+
void 0,
|
|
3195
|
+
options?.httpRequest
|
|
3196
|
+
);
|
|
3197
|
+
let text = "";
|
|
3198
|
+
const toolCalls = [];
|
|
3199
|
+
const toolResults = [];
|
|
3200
|
+
let messages = [];
|
|
3201
|
+
let requiresAction = false;
|
|
3202
|
+
let error;
|
|
3203
|
+
try {
|
|
3204
|
+
for await (const event of generator) {
|
|
3205
|
+
switch (event.type) {
|
|
3206
|
+
case "message:delta":
|
|
3207
|
+
text += event.content;
|
|
3208
|
+
break;
|
|
3209
|
+
case "action:start":
|
|
3210
|
+
toolCalls.push({ id: event.id, name: event.name, args: {} });
|
|
3211
|
+
break;
|
|
3212
|
+
case "action:args": {
|
|
3213
|
+
const tc = toolCalls.find((t) => t.id === event.id);
|
|
3214
|
+
if (tc) {
|
|
3215
|
+
try {
|
|
3216
|
+
tc.args = JSON.parse(event.args || "{}");
|
|
3217
|
+
} catch {
|
|
3218
|
+
tc.args = {};
|
|
3219
|
+
}
|
|
3220
|
+
}
|
|
3221
|
+
break;
|
|
3222
|
+
}
|
|
3223
|
+
case "action:end":
|
|
3224
|
+
toolResults.push({
|
|
3225
|
+
id: event.id,
|
|
3226
|
+
result: event.result || event.error
|
|
3227
|
+
});
|
|
3228
|
+
break;
|
|
3229
|
+
case "done":
|
|
3230
|
+
messages = event.messages || [];
|
|
3231
|
+
requiresAction = event.requiresAction || false;
|
|
3232
|
+
break;
|
|
3233
|
+
case "error":
|
|
3234
|
+
error = { message: event.message, code: event.code };
|
|
3235
|
+
break;
|
|
3236
|
+
}
|
|
3237
|
+
}
|
|
3238
|
+
} catch (err) {
|
|
3239
|
+
error = {
|
|
3240
|
+
message: err instanceof Error ? err.message : "Unknown error",
|
|
3241
|
+
code: "GENERATION_ERROR"
|
|
3242
|
+
};
|
|
3243
|
+
}
|
|
3244
|
+
if (options?.onFinish && messages.length > 0 && !error) {
|
|
3245
|
+
try {
|
|
3246
|
+
await options.onFinish({
|
|
3247
|
+
messages,
|
|
3248
|
+
threadId: request.threadId
|
|
3249
|
+
});
|
|
3250
|
+
} catch (callbackError) {
|
|
3251
|
+
console.error(
|
|
3252
|
+
"[Copilot SDK] generate() onFinish callback error:",
|
|
3253
|
+
callbackError
|
|
3254
|
+
);
|
|
3255
|
+
}
|
|
3256
|
+
}
|
|
3257
|
+
return new GenerateResult({
|
|
3258
|
+
text,
|
|
3259
|
+
messages,
|
|
3260
|
+
toolCalls,
|
|
3261
|
+
toolResults,
|
|
3262
|
+
requiresAction,
|
|
3263
|
+
error
|
|
3264
|
+
});
|
|
3082
3265
|
}
|
|
3083
3266
|
/**
|
|
3084
3267
|
* Create Express-compatible handler middleware
|
|
@@ -3785,6 +3968,7 @@ async function executeToolCalls(toolCalls, tools, executeServerTool, waitForClie
|
|
|
3785
3968
|
|
|
3786
3969
|
exports.DEFAULT_CAPABILITIES = DEFAULT_CAPABILITIES;
|
|
3787
3970
|
exports.DEFAULT_MAX_ITERATIONS = DEFAULT_MAX_ITERATIONS;
|
|
3971
|
+
exports.GenerateResult = GenerateResult;
|
|
3788
3972
|
exports.Runtime = Runtime;
|
|
3789
3973
|
exports.StreamResult = StreamResult;
|
|
3790
3974
|
exports.createEventStream = createEventStream;
|