react-native-nobodywho 1.1.0 → 2.0.1
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/Nobodywho.podspec +2 -2
- package/android/build.gradle +2 -2
- package/generated/ts/nobodywho.ts +105 -99
- package/package.json +1 -1
- package/src/chat.ts +3 -3
- package/src/message.ts +49 -35
- package/src/wrapper.ts +3 -4
package/Nobodywho.podspec
CHANGED
|
@@ -10,7 +10,7 @@ framework_name = "NobodywhoFramework.xcframework"
|
|
|
10
10
|
framework_dir = File.join(__dir__, framework_name)
|
|
11
11
|
zip_name = "#{framework_name}.zip"
|
|
12
12
|
zip_path = File.join(__dir__, zip_name)
|
|
13
|
-
url = "https://github.com/nobodywho-ooo/nobodywho/releases/download/nobodywho-react-native
|
|
13
|
+
url = "https://github.com/nobodywho-ooo/nobodywho/releases/download/nobodywho-react-native-v#{version}/#{zip_name}"
|
|
14
14
|
|
|
15
15
|
# Always download a fresh xcframework to ensure it matches the package version.
|
|
16
16
|
FileUtils.rm_rf(framework_dir) if File.exist?(framework_dir)
|
|
@@ -20,7 +20,7 @@ puts "[NobodyWho] Downloading xcframework from #{url}"
|
|
|
20
20
|
system("curl", "-L", "-f", "-o", zip_path, url) or
|
|
21
21
|
raise "Failed to download NobodyWho xcframework.\n" \
|
|
22
22
|
"URL: #{url}\n" \
|
|
23
|
-
"Check that the release exists: https://github.com/nobodywho-ooo/nobodywho/releases/tag/nobodywho-react-native
|
|
23
|
+
"Check that the release exists: https://github.com/nobodywho-ooo/nobodywho/releases/tag/nobodywho-react-native-v#{version}\n" \
|
|
24
24
|
"For local development, manually place the xcframework at: #{framework_dir}"
|
|
25
25
|
|
|
26
26
|
puts "[NobodyWho] Extracting xcframework..."
|
package/android/build.gradle
CHANGED
|
@@ -82,7 +82,7 @@ task resolveNativeLibraries {
|
|
|
82
82
|
abiDir.mkdirs()
|
|
83
83
|
|
|
84
84
|
def fileName = "libnobodywho-uniffi-${triple}-release.so"
|
|
85
|
-
def downloadUrl = "https://github.com/nobodywho-ooo/nobodywho/releases/download/nobodywho-react-native
|
|
85
|
+
def downloadUrl = "https://github.com/nobodywho-ooo/nobodywho/releases/download/nobodywho-react-native-v${nobodywhoVersion}/${fileName}"
|
|
86
86
|
|
|
87
87
|
logger.lifecycle("[NobodyWho] ${abi}: Downloading from ${downloadUrl}")
|
|
88
88
|
|
|
@@ -110,7 +110,7 @@ task resolveNativeLibraries {
|
|
|
110
110
|
throw new GradleException(
|
|
111
111
|
"Failed to download NobodyWho library for ${abi}: HTTP ${code}\n" +
|
|
112
112
|
"URL: ${downloadUrl}\n" +
|
|
113
|
-
"Check that the release exists: https://github.com/nobodywho-ooo/nobodywho/releases/tag/nobodywho-react-native
|
|
113
|
+
"Check that the release exists: https://github.com/nobodywho-ooo/nobodywho/releases/tag/nobodywho-react-native-v${nobodywhoVersion}"
|
|
114
114
|
)
|
|
115
115
|
}
|
|
116
116
|
}
|
|
@@ -619,38 +619,39 @@ const FfiConverterString = uniffiCreateFfiConverterString(stringConverter);
|
|
|
619
619
|
|
|
620
620
|
// Enum: Message
|
|
621
621
|
export enum Message_Tags {
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
622
|
+
User = "User",
|
|
623
|
+
Assistant = "Assistant",
|
|
624
|
+
System = "System",
|
|
625
|
+
Tool = "Tool"
|
|
625
626
|
}
|
|
626
627
|
export const Message = (() => {
|
|
627
628
|
|
|
628
629
|
|
|
629
|
-
type
|
|
630
|
-
tag: Message_Tags.
|
|
631
|
-
inner: Readonly<{
|
|
630
|
+
type User__interface = {
|
|
631
|
+
tag: Message_Tags.User;
|
|
632
|
+
inner: Readonly<{content: string; assets: Array<Asset>}>
|
|
632
633
|
};
|
|
633
634
|
|
|
634
635
|
|
|
635
|
-
class
|
|
636
|
+
class User_ extends UniffiEnum implements User__interface {
|
|
636
637
|
/**
|
|
637
638
|
* @private
|
|
638
639
|
* This field is private and should not be used, use `tag` instead.
|
|
639
640
|
*/
|
|
640
641
|
readonly [uniffiTypeNameSymbol] = "Message";
|
|
641
|
-
readonly tag = Message_Tags.
|
|
642
|
-
readonly inner: Readonly<{
|
|
643
|
-
constructor(inner: {
|
|
644
|
-
super("Message", "
|
|
642
|
+
readonly tag = Message_Tags.User;
|
|
643
|
+
readonly inner: Readonly<{content: string; assets: Array<Asset>}>;
|
|
644
|
+
constructor(inner: { content: string, assets: Array<Asset> }) {
|
|
645
|
+
super("Message", "User");
|
|
645
646
|
this.inner = Object.freeze(inner);
|
|
646
647
|
}
|
|
647
648
|
|
|
648
|
-
static new(inner: {
|
|
649
|
-
return new
|
|
649
|
+
static new(inner: { content: string, assets: Array<Asset> }): User_ {
|
|
650
|
+
return new User_(inner);
|
|
650
651
|
}
|
|
651
652
|
|
|
652
|
-
static instanceOf(obj: any): obj is
|
|
653
|
-
return obj.tag === Message_Tags.
|
|
653
|
+
static instanceOf(obj: any): obj is User_ {
|
|
654
|
+
return obj.tag === Message_Tags.User;
|
|
654
655
|
}
|
|
655
656
|
|
|
656
657
|
|
|
@@ -659,31 +660,31 @@ export const Message = (() => {
|
|
|
659
660
|
}
|
|
660
661
|
|
|
661
662
|
|
|
662
|
-
type
|
|
663
|
-
tag: Message_Tags.
|
|
664
|
-
inner: Readonly<{
|
|
663
|
+
type Assistant__interface = {
|
|
664
|
+
tag: Message_Tags.Assistant;
|
|
665
|
+
inner: Readonly<{content: string; toolCalls: Array<ToolCall> | undefined}>
|
|
665
666
|
};
|
|
666
667
|
|
|
667
668
|
|
|
668
|
-
class
|
|
669
|
+
class Assistant_ extends UniffiEnum implements Assistant__interface {
|
|
669
670
|
/**
|
|
670
671
|
* @private
|
|
671
672
|
* This field is private and should not be used, use `tag` instead.
|
|
672
673
|
*/
|
|
673
674
|
readonly [uniffiTypeNameSymbol] = "Message";
|
|
674
|
-
readonly tag = Message_Tags.
|
|
675
|
-
readonly inner: Readonly<{
|
|
676
|
-
constructor(inner: {
|
|
677
|
-
super("Message", "
|
|
675
|
+
readonly tag = Message_Tags.Assistant;
|
|
676
|
+
readonly inner: Readonly<{content: string; toolCalls: Array<ToolCall> | undefined}>;
|
|
677
|
+
constructor(inner: { content: string, toolCalls: Array<ToolCall> | undefined }) {
|
|
678
|
+
super("Message", "Assistant");
|
|
678
679
|
this.inner = Object.freeze(inner);
|
|
679
680
|
}
|
|
680
681
|
|
|
681
|
-
static new(inner: {
|
|
682
|
-
return new
|
|
682
|
+
static new(inner: { content: string, toolCalls: Array<ToolCall> | undefined }): Assistant_ {
|
|
683
|
+
return new Assistant_(inner);
|
|
683
684
|
}
|
|
684
685
|
|
|
685
|
-
static instanceOf(obj: any): obj is
|
|
686
|
-
return obj.tag === Message_Tags.
|
|
686
|
+
static instanceOf(obj: any): obj is Assistant_ {
|
|
687
|
+
return obj.tag === Message_Tags.Assistant;
|
|
687
688
|
}
|
|
688
689
|
|
|
689
690
|
|
|
@@ -692,31 +693,64 @@ export const Message = (() => {
|
|
|
692
693
|
}
|
|
693
694
|
|
|
694
695
|
|
|
695
|
-
type
|
|
696
|
-
tag: Message_Tags.
|
|
697
|
-
inner: Readonly<{
|
|
696
|
+
type System__interface = {
|
|
697
|
+
tag: Message_Tags.System;
|
|
698
|
+
inner: Readonly<{content: string}>
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
class System_ extends UniffiEnum implements System__interface {
|
|
703
|
+
/**
|
|
704
|
+
* @private
|
|
705
|
+
* This field is private and should not be used, use `tag` instead.
|
|
706
|
+
*/
|
|
707
|
+
readonly [uniffiTypeNameSymbol] = "Message";
|
|
708
|
+
readonly tag = Message_Tags.System;
|
|
709
|
+
readonly inner: Readonly<{content: string}>;
|
|
710
|
+
constructor(inner: { content: string }) {
|
|
711
|
+
super("Message", "System");
|
|
712
|
+
this.inner = Object.freeze(inner);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
static new(inner: { content: string }): System_ {
|
|
716
|
+
return new System_(inner);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
static instanceOf(obj: any): obj is System_ {
|
|
720
|
+
return obj.tag === Message_Tags.System;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
type Tool__interface = {
|
|
730
|
+
tag: Message_Tags.Tool;
|
|
731
|
+
inner: Readonly<{name: string; content: string}>
|
|
698
732
|
};
|
|
699
733
|
|
|
700
734
|
|
|
701
|
-
class
|
|
735
|
+
class Tool_ extends UniffiEnum implements Tool__interface {
|
|
702
736
|
/**
|
|
703
737
|
* @private
|
|
704
738
|
* This field is private and should not be used, use `tag` instead.
|
|
705
739
|
*/
|
|
706
740
|
readonly [uniffiTypeNameSymbol] = "Message";
|
|
707
|
-
readonly tag = Message_Tags.
|
|
708
|
-
readonly inner: Readonly<{
|
|
709
|
-
constructor(inner: {
|
|
710
|
-
super("Message", "
|
|
741
|
+
readonly tag = Message_Tags.Tool;
|
|
742
|
+
readonly inner: Readonly<{name: string; content: string}>;
|
|
743
|
+
constructor(inner: { name: string, content: string }) {
|
|
744
|
+
super("Message", "Tool");
|
|
711
745
|
this.inner = Object.freeze(inner);
|
|
712
746
|
}
|
|
713
747
|
|
|
714
|
-
static new(inner: {
|
|
715
|
-
return new
|
|
748
|
+
static new(inner: { name: string, content: string }): Tool_ {
|
|
749
|
+
return new Tool_(inner);
|
|
716
750
|
}
|
|
717
751
|
|
|
718
|
-
static instanceOf(obj: any): obj is
|
|
719
|
-
return obj.tag === Message_Tags.
|
|
752
|
+
static instanceOf(obj: any): obj is Tool_ {
|
|
753
|
+
return obj.tag === Message_Tags.Tool;
|
|
720
754
|
}
|
|
721
755
|
|
|
722
756
|
|
|
@@ -730,9 +764,10 @@ export const Message = (() => {
|
|
|
730
764
|
|
|
731
765
|
return Object.freeze({
|
|
732
766
|
instanceOf,
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
767
|
+
User: User_,
|
|
768
|
+
Assistant: Assistant_,
|
|
769
|
+
System: System_,
|
|
770
|
+
Tool: Tool_
|
|
736
771
|
});
|
|
737
772
|
|
|
738
773
|
})();
|
|
@@ -750,34 +785,38 @@ const FfiConverterTypeMessage = (() => {
|
|
|
750
785
|
class FFIConverter extends AbstractFfiConverterByteArray<TypeName> {
|
|
751
786
|
read(from: RustBuffer): TypeName {
|
|
752
787
|
switch (ordinalConverter.read(from)) {
|
|
753
|
-
case 1: return new Message.
|
|
754
|
-
case 2: return new Message.
|
|
755
|
-
case 3: return new Message.
|
|
788
|
+
case 1: return new Message.User({content: FfiConverterString.read(from), assets: FfiConverterArrayTypeAsset.read(from) });
|
|
789
|
+
case 2: return new Message.Assistant({content: FfiConverterString.read(from), toolCalls: FfiConverterOptionalArrayTypeToolCall.read(from) });
|
|
790
|
+
case 3: return new Message.System({content: FfiConverterString.read(from) });
|
|
791
|
+
case 4: return new Message.Tool({name: FfiConverterString.read(from), content: FfiConverterString.read(from) });
|
|
756
792
|
default: throw new UniffiInternalError.UnexpectedEnumCase();
|
|
757
793
|
}
|
|
758
794
|
}
|
|
759
795
|
write(value: TypeName, into: RustBuffer): void {
|
|
760
796
|
switch (value.tag) {
|
|
761
|
-
case Message_Tags.
|
|
797
|
+
case Message_Tags.User: {
|
|
762
798
|
ordinalConverter.write(1, into);
|
|
763
799
|
const inner = value.inner;
|
|
764
|
-
FfiConverterTypeRole.write(inner.role, into);
|
|
765
800
|
FfiConverterString.write(inner.content, into);
|
|
766
801
|
FfiConverterArrayTypeAsset.write(inner.assets, into);
|
|
767
802
|
return;
|
|
768
803
|
}
|
|
769
|
-
case Message_Tags.
|
|
804
|
+
case Message_Tags.Assistant: {
|
|
770
805
|
ordinalConverter.write(2, into);
|
|
771
806
|
const inner = value.inner;
|
|
772
|
-
FfiConverterTypeRole.write(inner.role, into);
|
|
773
807
|
FfiConverterString.write(inner.content, into);
|
|
774
|
-
|
|
808
|
+
FfiConverterOptionalArrayTypeToolCall.write(inner.toolCalls, into);
|
|
775
809
|
return;
|
|
776
810
|
}
|
|
777
|
-
case Message_Tags.
|
|
811
|
+
case Message_Tags.System: {
|
|
778
812
|
ordinalConverter.write(3, into);
|
|
779
813
|
const inner = value.inner;
|
|
780
|
-
|
|
814
|
+
FfiConverterString.write(inner.content, into);
|
|
815
|
+
return;
|
|
816
|
+
}
|
|
817
|
+
case Message_Tags.Tool: {
|
|
818
|
+
ordinalConverter.write(4, into);
|
|
819
|
+
const inner = value.inner;
|
|
781
820
|
FfiConverterString.write(inner.name, into);
|
|
782
821
|
FfiConverterString.write(inner.content, into);
|
|
783
822
|
return;
|
|
@@ -789,26 +828,29 @@ const FfiConverterTypeMessage = (() => {
|
|
|
789
828
|
}
|
|
790
829
|
allocationSize(value: TypeName): number {
|
|
791
830
|
switch (value.tag) {
|
|
792
|
-
case Message_Tags.
|
|
831
|
+
case Message_Tags.User: {
|
|
793
832
|
const inner = value.inner;
|
|
794
833
|
let size = ordinalConverter.allocationSize(1);
|
|
795
|
-
size += FfiConverterTypeRole.allocationSize(inner.role);
|
|
796
834
|
size += FfiConverterString.allocationSize(inner.content);
|
|
797
835
|
size += FfiConverterArrayTypeAsset.allocationSize(inner.assets);
|
|
798
836
|
return size;
|
|
799
837
|
}
|
|
800
|
-
case Message_Tags.
|
|
838
|
+
case Message_Tags.Assistant: {
|
|
801
839
|
const inner = value.inner;
|
|
802
840
|
let size = ordinalConverter.allocationSize(2);
|
|
803
|
-
size += FfiConverterTypeRole.allocationSize(inner.role);
|
|
804
841
|
size += FfiConverterString.allocationSize(inner.content);
|
|
805
|
-
size +=
|
|
842
|
+
size += FfiConverterOptionalArrayTypeToolCall.allocationSize(inner.toolCalls);
|
|
806
843
|
return size;
|
|
807
844
|
}
|
|
808
|
-
case Message_Tags.
|
|
845
|
+
case Message_Tags.System: {
|
|
809
846
|
const inner = value.inner;
|
|
810
847
|
let size = ordinalConverter.allocationSize(3);
|
|
811
|
-
size +=
|
|
848
|
+
size += FfiConverterString.allocationSize(inner.content);
|
|
849
|
+
return size;
|
|
850
|
+
}
|
|
851
|
+
case Message_Tags.Tool: {
|
|
852
|
+
const inner = value.inner;
|
|
853
|
+
let size = ordinalConverter.allocationSize(4);
|
|
812
854
|
size += FfiConverterString.allocationSize(inner.name);
|
|
813
855
|
size += FfiConverterString.allocationSize(inner.content);
|
|
814
856
|
return size;
|
|
@@ -1094,45 +1136,6 @@ const FfiConverterTypePromptPart = (() => {
|
|
|
1094
1136
|
|
|
1095
1137
|
|
|
1096
1138
|
|
|
1097
|
-
export enum Role {
|
|
1098
|
-
User,
|
|
1099
|
-
Assistant,
|
|
1100
|
-
System,
|
|
1101
|
-
Tool
|
|
1102
|
-
}
|
|
1103
|
-
|
|
1104
|
-
const FfiConverterTypeRole = (() => {
|
|
1105
|
-
const ordinalConverter = FfiConverterInt32;
|
|
1106
|
-
type TypeName = Role;
|
|
1107
|
-
class FFIConverter extends AbstractFfiConverterByteArray<TypeName> {
|
|
1108
|
-
read(from: RustBuffer): TypeName {
|
|
1109
|
-
switch (ordinalConverter.read(from)) {
|
|
1110
|
-
case 1: return Role.User;
|
|
1111
|
-
case 2: return Role.Assistant;
|
|
1112
|
-
case 3: return Role.System;
|
|
1113
|
-
case 4: return Role.Tool;
|
|
1114
|
-
default: throw new UniffiInternalError.UnexpectedEnumCase();
|
|
1115
|
-
}
|
|
1116
|
-
}
|
|
1117
|
-
write(value: TypeName, into: RustBuffer): void {
|
|
1118
|
-
switch (value) {
|
|
1119
|
-
case Role.User: return ordinalConverter.write(1, into);
|
|
1120
|
-
case Role.Assistant: return ordinalConverter.write(2, into);
|
|
1121
|
-
case Role.System: return ordinalConverter.write(3, into);
|
|
1122
|
-
case Role.Tool: return ordinalConverter.write(4, into);
|
|
1123
|
-
}
|
|
1124
|
-
}
|
|
1125
|
-
allocationSize(value: TypeName): number {
|
|
1126
|
-
return ordinalConverter.allocationSize(0);
|
|
1127
|
-
}
|
|
1128
|
-
}
|
|
1129
|
-
return new FFIConverter();
|
|
1130
|
-
})();
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
1139
|
// FfiConverter for Map<string, boolean>
|
|
1137
1140
|
const FfiConverterMapStringBool = new FfiConverterMap(FfiConverterString, FfiConverterBool);
|
|
1138
1141
|
|
|
@@ -2999,6 +3002,10 @@ const FfiConverterOptionalMapStringBool = new FfiConverterOptional(FfiConverterM
|
|
|
2999
3002
|
const FfiConverterOptionalTypeSamplerConfig = new FfiConverterOptional(FfiConverterTypeSamplerConfig);
|
|
3000
3003
|
|
|
3001
3004
|
|
|
3005
|
+
// FfiConverter for Array<ToolCall> | undefined
|
|
3006
|
+
const FfiConverterOptionalArrayTypeToolCall = new FfiConverterOptional(FfiConverterArrayTypeToolCall);
|
|
3007
|
+
|
|
3008
|
+
|
|
3002
3009
|
// FfiConverter for Array<Message>
|
|
3003
3010
|
const FfiConverterArrayTypeMessage = new FfiConverterArray(FfiConverterTypeMessage);
|
|
3004
3011
|
|
|
@@ -3210,7 +3217,6 @@ export default Object.freeze({
|
|
|
3210
3217
|
FfiConverterTypeNobodyWhoError,
|
|
3211
3218
|
FfiConverterTypePendingToolCall,
|
|
3212
3219
|
FfiConverterTypePromptPart,
|
|
3213
|
-
FfiConverterTypeRole,
|
|
3214
3220
|
FfiConverterTypeRustChat,
|
|
3215
3221
|
FfiConverterTypeRustCrossEncoder,
|
|
3216
3222
|
FfiConverterTypeRustEncoder,
|
package/package.json
CHANGED
package/src/chat.ts
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
SamplerConfig,
|
|
4
4
|
} from "../generated/ts/nobodywho";
|
|
5
5
|
import { Model } from "./model";
|
|
6
|
-
import { type
|
|
6
|
+
import { type Message, fromInternal, toInternal } from "./message";
|
|
7
7
|
import { TokenStream } from "./streaming";
|
|
8
8
|
import type { Prompt } from "./prompt";
|
|
9
9
|
import type { Tool } from "./tool";
|
|
@@ -110,13 +110,13 @@ export class Chat {
|
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
/** Get the current chat history as a list of messages. */
|
|
113
|
-
async getChatHistory(): Promise<
|
|
113
|
+
async getChatHistory(): Promise<Message[]> {
|
|
114
114
|
const internal = await this._inner.getChatHistory();
|
|
115
115
|
return internal.map(fromInternal);
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
/** Set the chat history from a list of messages. */
|
|
119
|
-
async setChatHistory(messages:
|
|
119
|
+
async setChatHistory(messages: Message[]): Promise<void> {
|
|
120
120
|
return this._inner.setChatHistory(messages.map(toInternal));
|
|
121
121
|
}
|
|
122
122
|
|
package/src/message.ts
CHANGED
|
@@ -1,75 +1,89 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Message as InternalMessage,
|
|
3
3
|
Message_Tags,
|
|
4
|
-
Role,
|
|
5
4
|
type Asset,
|
|
6
5
|
type ToolCall,
|
|
7
6
|
} from "../generated/ts/nobodywho";
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
|
-
* A chat message. The
|
|
11
|
-
* `toolCalls` or `name` determines the message type:
|
|
9
|
+
* A chat message. The variant determines the message type:
|
|
12
10
|
*
|
|
13
|
-
* - **User
|
|
14
|
-
* - **
|
|
15
|
-
* - **
|
|
11
|
+
* - **User message:** `{ role: "user", content, assets? }`
|
|
12
|
+
* - **Assistant message:** `{ role: "assistant", content }`
|
|
13
|
+
* - **Assistant tool call:** `{ role: "assistant", content, toolCalls }`
|
|
14
|
+
* - **System message:** `{ role: "system", content }`
|
|
15
|
+
* - **Tool response:** `{ role: "tool", name, content }`
|
|
16
16
|
*
|
|
17
17
|
* @example
|
|
18
18
|
* ```typescript
|
|
19
19
|
* const history = await chat.getChatHistory();
|
|
20
20
|
* for (const msg of history) {
|
|
21
|
-
* if ("
|
|
22
|
-
* console.log("Tool calls:", msg.toolCalls);
|
|
23
|
-
* } else if ("name" in msg) {
|
|
21
|
+
* if (msg.role === "tool") {
|
|
24
22
|
* console.log("Tool response:", msg.name, msg.content);
|
|
23
|
+
* } else if (msg.role === "assistant" && "toolCalls" in msg) {
|
|
24
|
+
* console.log("Tool calls:", msg.toolCalls);
|
|
25
25
|
* } else {
|
|
26
26
|
* console.log(msg.role, msg.content);
|
|
27
27
|
* }
|
|
28
28
|
* }
|
|
29
29
|
* ```
|
|
30
30
|
*/
|
|
31
|
-
export type
|
|
32
|
-
| { role:
|
|
33
|
-
| { role:
|
|
34
|
-
| { role:
|
|
31
|
+
export type Message =
|
|
32
|
+
| { role: "user"; content: string; assets?: Asset[] }
|
|
33
|
+
| { role: "assistant"; content: string }
|
|
34
|
+
| { role: "assistant"; content: string; toolCalls: ToolCall[] }
|
|
35
|
+
| { role: "system"; content: string }
|
|
36
|
+
| { role: "tool"; name: string; content: string };
|
|
35
37
|
|
|
36
|
-
/** @internal Convert internal Message to
|
|
37
|
-
export function fromInternal(msg: InternalMessage):
|
|
38
|
-
if (msg.tag === Message_Tags.
|
|
39
|
-
const {
|
|
38
|
+
/** @internal Convert internal Message to Message */
|
|
39
|
+
export function fromInternal(msg: InternalMessage): Message {
|
|
40
|
+
if (msg.tag === Message_Tags.User) {
|
|
41
|
+
const { content, assets } = msg.inner;
|
|
40
42
|
return {
|
|
41
|
-
role:
|
|
43
|
+
role: "user",
|
|
42
44
|
content,
|
|
43
45
|
...(assets.length > 0 ? { assets } : {}),
|
|
44
46
|
};
|
|
45
|
-
} else if (msg.tag === Message_Tags.
|
|
46
|
-
const {
|
|
47
|
-
|
|
47
|
+
} else if (msg.tag === Message_Tags.Assistant) {
|
|
48
|
+
const { content, toolCalls } = msg.inner;
|
|
49
|
+
if (toolCalls != null && toolCalls.length > 0) {
|
|
50
|
+
return { role: "assistant", content, toolCalls };
|
|
51
|
+
}
|
|
52
|
+
return { role: "assistant", content };
|
|
53
|
+
} else if (msg.tag === Message_Tags.System) {
|
|
54
|
+
const { content } = msg.inner;
|
|
55
|
+
return { role: "system", content };
|
|
48
56
|
} else {
|
|
49
|
-
const {
|
|
50
|
-
return { role:
|
|
57
|
+
const { name, content } = msg.inner;
|
|
58
|
+
return { role: "tool", name, content };
|
|
51
59
|
}
|
|
52
60
|
}
|
|
53
61
|
|
|
54
|
-
/** @internal Convert
|
|
55
|
-
export function toInternal(msg:
|
|
56
|
-
if ("
|
|
57
|
-
return new InternalMessage.
|
|
58
|
-
|
|
62
|
+
/** @internal Convert Message to internal Message */
|
|
63
|
+
export function toInternal(msg: Message): InternalMessage {
|
|
64
|
+
if (msg.role === "user") {
|
|
65
|
+
return new InternalMessage.User({
|
|
66
|
+
content: msg.content,
|
|
67
|
+
assets: msg.assets ?? [],
|
|
68
|
+
});
|
|
69
|
+
} else if (msg.role === "assistant" && "toolCalls" in msg) {
|
|
70
|
+
return new InternalMessage.Assistant({
|
|
59
71
|
content: msg.content,
|
|
60
72
|
toolCalls: msg.toolCalls,
|
|
61
73
|
});
|
|
62
|
-
} else if ("
|
|
63
|
-
return new InternalMessage.
|
|
64
|
-
|
|
65
|
-
|
|
74
|
+
} else if (msg.role === "assistant") {
|
|
75
|
+
return new InternalMessage.Assistant({
|
|
76
|
+
content: msg.content,
|
|
77
|
+
toolCalls: null,
|
|
78
|
+
});
|
|
79
|
+
} else if (msg.role === "system") {
|
|
80
|
+
return new InternalMessage.System({
|
|
66
81
|
content: msg.content,
|
|
67
82
|
});
|
|
68
83
|
} else {
|
|
69
|
-
return new InternalMessage.
|
|
70
|
-
|
|
84
|
+
return new InternalMessage.Tool({
|
|
85
|
+
name: msg.name,
|
|
71
86
|
content: msg.content,
|
|
72
|
-
assets: msg.assets ?? [],
|
|
73
87
|
});
|
|
74
88
|
}
|
|
75
89
|
}
|
package/src/wrapper.ts
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* This is the public entry point. It re-exports the public API:
|
|
5
5
|
* - Wrapper classes: Model, Chat, Encoder, CrossEncoder, Tool, TokenStream, Prompt
|
|
6
|
-
* - Direct re-exports: SamplerBuilder, SamplerConfig
|
|
7
|
-
* - Types:
|
|
6
|
+
* - Direct re-exports: SamplerBuilder, SamplerConfig
|
|
7
|
+
* - Types: Message, Asset, ToolCall
|
|
8
8
|
* - Utilities: SamplerPresets, cosineSimilarity
|
|
9
9
|
*
|
|
10
10
|
* This file is NOT generated — it is safe to edit.
|
|
@@ -20,13 +20,12 @@ export { Tool } from "./tool";
|
|
|
20
20
|
export { TokenStream } from "./streaming";
|
|
21
21
|
|
|
22
22
|
// Message types
|
|
23
|
-
export type {
|
|
23
|
+
export type { Message } from "./message";
|
|
24
24
|
|
|
25
25
|
// Re-export types that don't need wrapping.
|
|
26
26
|
export {
|
|
27
27
|
SamplerBuilder,
|
|
28
28
|
SamplerConfig,
|
|
29
|
-
Role,
|
|
30
29
|
cosineSimilarity,
|
|
31
30
|
} from "./index";
|
|
32
31
|
|