@microsoft/teams.ai 0.2.7

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 (116) hide show
  1. package/README.md +35 -0
  2. package/dist/citation.d.mts +19 -0
  3. package/dist/citation.d.ts +19 -0
  4. package/dist/citation.js +4 -0
  5. package/dist/citation.js.map +1 -0
  6. package/dist/citation.mjs +3 -0
  7. package/dist/citation.mjs.map +1 -0
  8. package/dist/function.d.mts +18 -0
  9. package/dist/function.d.ts +18 -0
  10. package/dist/function.js +4 -0
  11. package/dist/function.js.map +1 -0
  12. package/dist/function.mjs +3 -0
  13. package/dist/function.mjs.map +1 -0
  14. package/dist/index.d.mts +17 -0
  15. package/dist/index.d.ts +17 -0
  16. package/dist/index.js +77 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/index.mjs +12 -0
  19. package/dist/index.mjs.map +1 -0
  20. package/dist/local-memory.d.mts +32 -0
  21. package/dist/local-memory.d.ts +32 -0
  22. package/dist/local-memory.js +78 -0
  23. package/dist/local-memory.js.map +1 -0
  24. package/dist/local-memory.mjs +76 -0
  25. package/dist/local-memory.mjs.map +1 -0
  26. package/dist/memory.d.mts +11 -0
  27. package/dist/memory.d.ts +11 -0
  28. package/dist/memory.js +4 -0
  29. package/dist/memory.js.map +1 -0
  30. package/dist/memory.mjs +3 -0
  31. package/dist/memory.mjs.map +1 -0
  32. package/dist/message.d.mts +76 -0
  33. package/dist/message.d.ts +76 -0
  34. package/dist/message.js +4 -0
  35. package/dist/message.js.map +1 -0
  36. package/dist/message.mjs +3 -0
  37. package/dist/message.mjs.map +1 -0
  38. package/dist/models/audio.d.mts +27 -0
  39. package/dist/models/audio.d.ts +27 -0
  40. package/dist/models/audio.js +4 -0
  41. package/dist/models/audio.js.map +1 -0
  42. package/dist/models/audio.mjs +3 -0
  43. package/dist/models/audio.mjs.map +1 -0
  44. package/dist/models/chat.d.mts +46 -0
  45. package/dist/models/chat.d.ts +46 -0
  46. package/dist/models/chat.js +4 -0
  47. package/dist/models/chat.js.map +1 -0
  48. package/dist/models/chat.mjs +3 -0
  49. package/dist/models/chat.mjs.map +1 -0
  50. package/dist/models/image.d.mts +22 -0
  51. package/dist/models/image.d.ts +22 -0
  52. package/dist/models/image.js +4 -0
  53. package/dist/models/image.js.map +1 -0
  54. package/dist/models/image.mjs +3 -0
  55. package/dist/models/image.mjs.map +1 -0
  56. package/dist/models/index.d.mts +16 -0
  57. package/dist/models/index.d.ts +16 -0
  58. package/dist/models/index.js +28 -0
  59. package/dist/models/index.js.map +1 -0
  60. package/dist/models/index.mjs +5 -0
  61. package/dist/models/index.mjs.map +1 -0
  62. package/dist/prompts/audio.d.mts +54 -0
  63. package/dist/prompts/audio.d.ts +54 -0
  64. package/dist/prompts/audio.js +34 -0
  65. package/dist/prompts/audio.js.map +1 -0
  66. package/dist/prompts/audio.mjs +32 -0
  67. package/dist/prompts/audio.mjs.map +1 -0
  68. package/dist/prompts/chat.d.mts +146 -0
  69. package/dist/prompts/chat.d.ts +146 -0
  70. package/dist/prompts/chat.js +162 -0
  71. package/dist/prompts/chat.js.map +1 -0
  72. package/dist/prompts/chat.mjs +160 -0
  73. package/dist/prompts/chat.mjs.map +1 -0
  74. package/dist/prompts/index.d.mts +18 -0
  75. package/dist/prompts/index.d.ts +18 -0
  76. package/dist/prompts/index.js +21 -0
  77. package/dist/prompts/index.js.map +1 -0
  78. package/dist/prompts/index.mjs +4 -0
  79. package/dist/prompts/index.mjs.map +1 -0
  80. package/dist/prompts/plugin.d.mts +26 -0
  81. package/dist/prompts/plugin.d.ts +26 -0
  82. package/dist/prompts/plugin.js +4 -0
  83. package/dist/prompts/plugin.js.map +1 -0
  84. package/dist/prompts/plugin.mjs +3 -0
  85. package/dist/prompts/plugin.mjs.map +1 -0
  86. package/dist/schema.d.mts +46 -0
  87. package/dist/schema.d.ts +46 -0
  88. package/dist/schema.js +4 -0
  89. package/dist/schema.js.map +1 -0
  90. package/dist/schema.mjs +3 -0
  91. package/dist/schema.mjs.map +1 -0
  92. package/dist/template.d.mts +13 -0
  93. package/dist/template.d.ts +13 -0
  94. package/dist/template.js +4 -0
  95. package/dist/template.js.map +1 -0
  96. package/dist/template.mjs +3 -0
  97. package/dist/template.mjs.map +1 -0
  98. package/dist/templates/index.d.mts +2 -0
  99. package/dist/templates/index.d.ts +2 -0
  100. package/dist/templates/index.js +14 -0
  101. package/dist/templates/index.js.map +1 -0
  102. package/dist/templates/index.mjs +3 -0
  103. package/dist/templates/index.mjs.map +1 -0
  104. package/dist/templates/string.d.mts +9 -0
  105. package/dist/templates/string.d.ts +9 -0
  106. package/dist/templates/string.js +14 -0
  107. package/dist/templates/string.js.map +1 -0
  108. package/dist/templates/string.mjs +12 -0
  109. package/dist/templates/string.mjs.map +1 -0
  110. package/dist/utils/types.d.mts +5 -0
  111. package/dist/utils/types.d.ts +5 -0
  112. package/dist/utils/types.js +4 -0
  113. package/dist/utils/types.js.map +1 -0
  114. package/dist/utils/types.mjs +3 -0
  115. package/dist/utils/types.mjs.map +1 -0
  116. package/package.json +49 -0
@@ -0,0 +1,146 @@
1
+ import { Function, FunctionHandler } from '../function.mjs';
2
+ import { IMemory } from '../memory.mjs';
3
+ import { Message, ContentPart, ModelMessage } from '../message.mjs';
4
+ import { IChatModel, TextChunkHandler } from '../models/chat.mjs';
5
+ import { Schema } from '../schema.mjs';
6
+ import { ITemplate } from '../template.mjs';
7
+ import { IAiPlugin } from './plugin.mjs';
8
+ import '@microsoft/teams.common';
9
+ import '../citation.mjs';
10
+
11
+ type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any>> = {
12
+ /**
13
+ * the name of the prompt
14
+ */
15
+ readonly name?: string;
16
+ /**
17
+ * the description of the prompt
18
+ */
19
+ readonly description?: string;
20
+ /**
21
+ * the model to send messages to
22
+ */
23
+ readonly model: IChatModel<TOptions>;
24
+ /**
25
+ * the defining characteristics/objective
26
+ * of the prompt. This is commonly used to provide a system prompt.
27
+ * If you supply the system prompt as part of the messages,
28
+ * you do not need to supply this option.
29
+ */
30
+ readonly instructions?: string | string[] | ITemplate;
31
+ /**
32
+ * the `role` of the initial message
33
+ */
34
+ readonly role?: 'system' | 'user';
35
+ /**
36
+ * the conversation history
37
+ */
38
+ readonly messages?: Message[] | IMemory;
39
+ };
40
+ type ChatPromptSendOptions<TOptions extends Record<string, any> = Record<string, any>> = {
41
+ /**
42
+ * the conversation history
43
+ */
44
+ readonly messages?: Message[] | IMemory;
45
+ /**
46
+ * the models request options
47
+ */
48
+ readonly request?: TOptions;
49
+ /**
50
+ * the callback to be called for each
51
+ * stream chunk
52
+ */
53
+ readonly onChunk?: TextChunkHandler;
54
+ };
55
+ /**
56
+ * a prompt that can interface with a
57
+ * chat model that provides utility like
58
+ * streaming and function calling
59
+ */
60
+ interface IChatPrompt<TOptions extends Record<string, any> = Record<string, any>, TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = []> {
61
+ /**
62
+ * the prompt name
63
+ */
64
+ readonly name: string;
65
+ /**
66
+ * the prompt description
67
+ */
68
+ readonly description: string;
69
+ /**
70
+ * the chat history
71
+ */
72
+ readonly messages: IMemory;
73
+ /**
74
+ * the registered functions
75
+ */
76
+ readonly functions: Array<Function>;
77
+ /**
78
+ * the chat model
79
+ */
80
+ plugins: TChatPromptPlugins;
81
+ /**
82
+ * add another chat prompt as a
83
+ */
84
+ use(prompt: IChatPrompt): this;
85
+ use(name: string, prompt: IChatPrompt): this;
86
+ /**
87
+ * add a function that can be called
88
+ * by the model
89
+ */
90
+ function(name: string, description: string, handler: FunctionHandler): this;
91
+ function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;
92
+ usePlugin<TPluginName extends TChatPromptPlugins[number]['name']>(name: TPluginName, args: Extract<TChatPromptPlugins[number], {
93
+ name: TPluginName;
94
+ }>['onUsePlugin'] extends ((args: infer U) => void) | undefined ? U : never): this;
95
+ /**
96
+ * call a function
97
+ */
98
+ call<A extends Record<string, any>, R = any>(name: string, args?: A): Promise<R>;
99
+ /**
100
+ * send a message to the model and get a response
101
+ */
102
+ send(input: string | ContentPart[], options?: ChatPromptSendOptions<TOptions>): Promise<Pick<ModelMessage, 'content'> & Omit<ModelMessage, 'content'>>;
103
+ }
104
+ type ChatPromptPlugin<TPluginName extends string, TPluginUseArgs extends {}> = IAiPlugin<TPluginName, TPluginUseArgs, Parameters<IChatPrompt['send']>[0], ReturnType<IChatPrompt['send']>> & {
105
+ /**
106
+ * Optionally passed in to modify the functions array that
107
+ * is passed to the model
108
+ * @param functions
109
+ * @returns Functions
110
+ */
111
+ onBuildFunctions?: (functions: Function[]) => Function[] | Promise<Function[]>;
112
+ };
113
+ /**
114
+ * a prompt that can interface with a
115
+ * chat model that provides utility like
116
+ * streaming and function calling
117
+ */
118
+ declare class ChatPrompt<TOptions extends Record<string, any> = Record<string, any>, TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = []> implements IChatPrompt<TOptions, TChatPromptPlugins> {
119
+ get name(): string;
120
+ protected readonly _name: string;
121
+ get description(): string;
122
+ protected readonly _description: string;
123
+ get messages(): IMemory;
124
+ protected readonly _messages: IMemory;
125
+ get functions(): Function[];
126
+ protected readonly _functions: Record<string, Function>;
127
+ get plugins(): TChatPromptPlugins;
128
+ protected readonly _plugins: TChatPromptPlugins;
129
+ protected readonly _role: 'system' | 'user';
130
+ protected readonly _template: ITemplate;
131
+ protected readonly _model: IChatModel<TOptions>;
132
+ constructor(options: ChatPromptOptions<TOptions>, plugins?: TChatPromptPlugins);
133
+ use(prompt: ChatPrompt): this;
134
+ use(name: string, prompt: ChatPrompt): this;
135
+ function(name: string, description: string, handler: FunctionHandler): this;
136
+ function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;
137
+ usePlugin<K extends TChatPromptPlugins[number]['name']>(name: K, args: Extract<TChatPromptPlugins[number], {
138
+ name: K;
139
+ }>['onUsePlugin'] extends ((args: infer U) => void) | undefined ? U : never): this;
140
+ call<A extends {
141
+ [key: string]: any;
142
+ }, R = any>(name: string, args?: A): Promise<R>;
143
+ send(input: string | ContentPart[], options?: ChatPromptSendOptions<TOptions>): Promise<ModelMessage>;
144
+ }
145
+
146
+ export { ChatPrompt, type ChatPromptOptions, type ChatPromptPlugin, type ChatPromptSendOptions, type IChatPrompt };
@@ -0,0 +1,146 @@
1
+ import { Function, FunctionHandler } from '../function.js';
2
+ import { IMemory } from '../memory.js';
3
+ import { Message, ContentPart, ModelMessage } from '../message.js';
4
+ import { IChatModel, TextChunkHandler } from '../models/chat.js';
5
+ import { Schema } from '../schema.js';
6
+ import { ITemplate } from '../template.js';
7
+ import { IAiPlugin } from './plugin.js';
8
+ import '@microsoft/teams.common';
9
+ import '../citation.js';
10
+
11
+ type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any>> = {
12
+ /**
13
+ * the name of the prompt
14
+ */
15
+ readonly name?: string;
16
+ /**
17
+ * the description of the prompt
18
+ */
19
+ readonly description?: string;
20
+ /**
21
+ * the model to send messages to
22
+ */
23
+ readonly model: IChatModel<TOptions>;
24
+ /**
25
+ * the defining characteristics/objective
26
+ * of the prompt. This is commonly used to provide a system prompt.
27
+ * If you supply the system prompt as part of the messages,
28
+ * you do not need to supply this option.
29
+ */
30
+ readonly instructions?: string | string[] | ITemplate;
31
+ /**
32
+ * the `role` of the initial message
33
+ */
34
+ readonly role?: 'system' | 'user';
35
+ /**
36
+ * the conversation history
37
+ */
38
+ readonly messages?: Message[] | IMemory;
39
+ };
40
+ type ChatPromptSendOptions<TOptions extends Record<string, any> = Record<string, any>> = {
41
+ /**
42
+ * the conversation history
43
+ */
44
+ readonly messages?: Message[] | IMemory;
45
+ /**
46
+ * the models request options
47
+ */
48
+ readonly request?: TOptions;
49
+ /**
50
+ * the callback to be called for each
51
+ * stream chunk
52
+ */
53
+ readonly onChunk?: TextChunkHandler;
54
+ };
55
+ /**
56
+ * a prompt that can interface with a
57
+ * chat model that provides utility like
58
+ * streaming and function calling
59
+ */
60
+ interface IChatPrompt<TOptions extends Record<string, any> = Record<string, any>, TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = []> {
61
+ /**
62
+ * the prompt name
63
+ */
64
+ readonly name: string;
65
+ /**
66
+ * the prompt description
67
+ */
68
+ readonly description: string;
69
+ /**
70
+ * the chat history
71
+ */
72
+ readonly messages: IMemory;
73
+ /**
74
+ * the registered functions
75
+ */
76
+ readonly functions: Array<Function>;
77
+ /**
78
+ * the chat model
79
+ */
80
+ plugins: TChatPromptPlugins;
81
+ /**
82
+ * add another chat prompt as a
83
+ */
84
+ use(prompt: IChatPrompt): this;
85
+ use(name: string, prompt: IChatPrompt): this;
86
+ /**
87
+ * add a function that can be called
88
+ * by the model
89
+ */
90
+ function(name: string, description: string, handler: FunctionHandler): this;
91
+ function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;
92
+ usePlugin<TPluginName extends TChatPromptPlugins[number]['name']>(name: TPluginName, args: Extract<TChatPromptPlugins[number], {
93
+ name: TPluginName;
94
+ }>['onUsePlugin'] extends ((args: infer U) => void) | undefined ? U : never): this;
95
+ /**
96
+ * call a function
97
+ */
98
+ call<A extends Record<string, any>, R = any>(name: string, args?: A): Promise<R>;
99
+ /**
100
+ * send a message to the model and get a response
101
+ */
102
+ send(input: string | ContentPart[], options?: ChatPromptSendOptions<TOptions>): Promise<Pick<ModelMessage, 'content'> & Omit<ModelMessage, 'content'>>;
103
+ }
104
+ type ChatPromptPlugin<TPluginName extends string, TPluginUseArgs extends {}> = IAiPlugin<TPluginName, TPluginUseArgs, Parameters<IChatPrompt['send']>[0], ReturnType<IChatPrompt['send']>> & {
105
+ /**
106
+ * Optionally passed in to modify the functions array that
107
+ * is passed to the model
108
+ * @param functions
109
+ * @returns Functions
110
+ */
111
+ onBuildFunctions?: (functions: Function[]) => Function[] | Promise<Function[]>;
112
+ };
113
+ /**
114
+ * a prompt that can interface with a
115
+ * chat model that provides utility like
116
+ * streaming and function calling
117
+ */
118
+ declare class ChatPrompt<TOptions extends Record<string, any> = Record<string, any>, TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = []> implements IChatPrompt<TOptions, TChatPromptPlugins> {
119
+ get name(): string;
120
+ protected readonly _name: string;
121
+ get description(): string;
122
+ protected readonly _description: string;
123
+ get messages(): IMemory;
124
+ protected readonly _messages: IMemory;
125
+ get functions(): Function[];
126
+ protected readonly _functions: Record<string, Function>;
127
+ get plugins(): TChatPromptPlugins;
128
+ protected readonly _plugins: TChatPromptPlugins;
129
+ protected readonly _role: 'system' | 'user';
130
+ protected readonly _template: ITemplate;
131
+ protected readonly _model: IChatModel<TOptions>;
132
+ constructor(options: ChatPromptOptions<TOptions>, plugins?: TChatPromptPlugins);
133
+ use(prompt: ChatPrompt): this;
134
+ use(name: string, prompt: ChatPrompt): this;
135
+ function(name: string, description: string, handler: FunctionHandler): this;
136
+ function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;
137
+ usePlugin<K extends TChatPromptPlugins[number]['name']>(name: K, args: Extract<TChatPromptPlugins[number], {
138
+ name: K;
139
+ }>['onUsePlugin'] extends ((args: infer U) => void) | undefined ? U : never): this;
140
+ call<A extends {
141
+ [key: string]: any;
142
+ }, R = any>(name: string, args?: A): Promise<R>;
143
+ send(input: string | ContentPart[], options?: ChatPromptSendOptions<TOptions>): Promise<ModelMessage>;
144
+ }
145
+
146
+ export { ChatPrompt, type ChatPromptOptions, type ChatPromptPlugin, type ChatPromptSendOptions, type IChatPrompt };
@@ -0,0 +1,162 @@
1
+ 'use strict';
2
+
3
+ var localMemory = require('../local-memory');
4
+ var templates = require('../templates');
5
+
6
+ class ChatPrompt {
7
+ get name() {
8
+ return this._name;
9
+ }
10
+ _name;
11
+ get description() {
12
+ return this._description;
13
+ }
14
+ _description;
15
+ get messages() {
16
+ return this._messages;
17
+ }
18
+ _messages;
19
+ get functions() {
20
+ return Object.values(this._functions);
21
+ }
22
+ _functions = {};
23
+ get plugins() {
24
+ return this._plugins;
25
+ }
26
+ _plugins;
27
+ _role;
28
+ _template;
29
+ _model;
30
+ constructor(options, plugins) {
31
+ this._name = options.name || "chat";
32
+ this._description = options.description || "an agent you can chat with";
33
+ this._role = options.role || "system";
34
+ this._model = options.model;
35
+ this._template = Array.isArray(options.instructions) ? new templates.StringTemplate(options.instructions.join("\n")) : typeof options.instructions !== "object" ? new templates.StringTemplate(options.instructions) : options.instructions;
36
+ this._messages = typeof options.messages === "object" && !Array.isArray(options.messages) ? options.messages : new localMemory.LocalMemory({ messages: options.messages || [] });
37
+ this._plugins = plugins || [];
38
+ }
39
+ use(...args) {
40
+ const prompt = args.length === 1 ? args[0] : args[1];
41
+ const name = args.length === 1 ? prompt.name : args[0];
42
+ this._functions[name] = {
43
+ name,
44
+ description: prompt.description,
45
+ parameters: {
46
+ type: "object",
47
+ properties: {
48
+ text: {
49
+ type: "string",
50
+ description: "the text to send to the assistant"
51
+ }
52
+ },
53
+ required: ["text"]
54
+ },
55
+ handler: ({ text }) => {
56
+ return prompt.send(text);
57
+ }
58
+ };
59
+ return this;
60
+ }
61
+ function(...args) {
62
+ const name = args[0];
63
+ const description = args[1];
64
+ const parameters = args.length === 3 ? null : args[2];
65
+ const handler = args[args.length - 1];
66
+ this._functions[name] = {
67
+ name,
68
+ description,
69
+ parameters: parameters || {},
70
+ handler
71
+ };
72
+ return this;
73
+ }
74
+ usePlugin(name, args) {
75
+ const plugin = this._plugins.find((p) => p.name === name);
76
+ if (!plugin) {
77
+ throw new Error(`Plugin "${name}" not found`);
78
+ }
79
+ if (plugin.onUsePlugin) {
80
+ plugin.onUsePlugin(args);
81
+ }
82
+ return this;
83
+ }
84
+ async call(name, args) {
85
+ const fn = this._functions[name];
86
+ if (!fn) {
87
+ throw new Error(`function "${name}" not found`);
88
+ }
89
+ return await fn.handler(args || {});
90
+ }
91
+ async send(input, options = {}) {
92
+ for (const plugin of this.plugins) {
93
+ if (plugin.onBeforeSend) {
94
+ input = await plugin.onBeforeSend(input);
95
+ }
96
+ }
97
+ const { onChunk } = options;
98
+ if (typeof input === "string") {
99
+ input = input.trim();
100
+ }
101
+ const messages = !Array.isArray(options.messages) ? options.messages || this._messages : new localMemory.LocalMemory({ messages: options.messages || [] });
102
+ let buffer = "";
103
+ let system = void 0;
104
+ const prompt = await this._template.render();
105
+ if (prompt) {
106
+ system = {
107
+ role: this._role,
108
+ content: prompt
109
+ };
110
+ }
111
+ let functions = Object.values(this._functions);
112
+ const pluginsWithOnBuildFunctions = this._plugins.filter(
113
+ (plugin) => plugin.onBuildFunctions != null
114
+ );
115
+ for (const plugin of pluginsWithOnBuildFunctions) {
116
+ functions = await plugin.onBuildFunctions(functions);
117
+ }
118
+ const fnMap = functions.reduce(
119
+ (acc, fn) => {
120
+ acc[fn.name] = fn;
121
+ return acc;
122
+ },
123
+ {}
124
+ );
125
+ const res = await this._model.send(
126
+ {
127
+ role: "user",
128
+ content: input
129
+ },
130
+ {
131
+ system,
132
+ messages,
133
+ request: options.request,
134
+ functions: fnMap,
135
+ onChunk: async (chunk) => {
136
+ if (!chunk || !onChunk) return;
137
+ buffer += chunk;
138
+ try {
139
+ await onChunk(buffer);
140
+ buffer = "";
141
+ } catch (err) {
142
+ return;
143
+ }
144
+ }
145
+ }
146
+ );
147
+ let output = {
148
+ ...res,
149
+ content: res.content || ""
150
+ };
151
+ for (const plugin of this.plugins) {
152
+ if (plugin.onAfterSend) {
153
+ output = await plugin.onAfterSend(output);
154
+ }
155
+ }
156
+ return output;
157
+ }
158
+ }
159
+
160
+ exports.ChatPrompt = ChatPrompt;
161
+ //# sourceMappingURL=chat.js.map
162
+ //# sourceMappingURL=chat.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/prompts/chat.ts"],"names":["StringTemplate","LocalMemory"],"mappings":";;;;;AA0JO,MAAM,UAIb,CAAA;AAAA,EACE,IAAI,IAAO,GAAA;AACT,IAAA,OAAO,IAAK,CAAA,KAAA;AAAA;AACd,EACmB,KAAA;AAAA,EAEnB,IAAI,WAAc,GAAA;AAChB,IAAA,OAAO,IAAK,CAAA,YAAA;AAAA;AACd,EACmB,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AA;AAAA,mBACF,GAAK,EAAA;AACZ,YAAA;AAAA;AACF;AACF;AACF,KACF;AAEA,IAAA,IAAI,MAAuD,GAAA;AAAA,MACzD,GAAG,GAAA;AAAA,MACH,OAAA,EAAS,IAAI,OAAW,IAAA;AAAA,KAC1B;AACA,IAAW,KAAA,MAAA,MAAA,IAAU,KAAK,OAAS,EAAA;AACjC,MAAA,IAAI,OAAO,WAAa,EAAA;AACtB,QAAS,MAAA,GAAA,MAAM,MAAO,CAAA,WAAA,CAAY,MAAM,CAAA;AAAA;AAC1C;AAGF,IAAO,OAAA,MAAA;AAAA;AAEX","file":"chat.js","sourcesContent":["import { Function, FunctionHandler } from '../function';\nimport { LocalMemory } from '../local-memory';\nimport { IMemory } from '../memory';\nimport { ContentPart, Message, ModelMessage, SystemMessage, UserMessage } from '../message';\nimport { IChatModel, TextChunkHandler } from '../models';\nimport { Schema } from '../schema';\nimport { ITemplate } from '../template';\nimport { StringTemplate } from '../templates';\nimport { WithRequired } from '../utils/types';\nimport { IAiPlugin } from './plugin';\n\nexport type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any>> = {\n /**\n * the name of the prompt\n */\n readonly name?: string;\n\n /**\n * the description of the prompt\n */\n readonly description?: string;\n\n /**\n * the model to send messages to\n */\n readonly model: IChatModel<TOptions>;\n\n /**\n * the defining characteristics/objective\n * of the prompt. This is commonly used to provide a system prompt.\n * If you supply the system prompt as part of the messages,\n * you do not need to supply this option.\n */\n readonly instructions?: string | string[] | ITemplate;\n\n /**\n * the `role` of the initial message\n */\n readonly role?: 'system' | 'user';\n\n /**\n * the conversation history\n */\n readonly messages?: Message[] | IMemory;\n};\n\nexport type ChatPromptSendOptions<TOptions extends Record<string, any> = Record<string, any>> = {\n /**\n * the conversation history\n */\n readonly messages?: Message[] | IMemory;\n\n /**\n * the models request options\n */\n readonly request?: TOptions;\n\n /**\n * the callback to be called for each\n * stream chunk\n */\n readonly onChunk?: TextChunkHandler;\n};\n\n/**\n * a prompt that can interface with a\n * chat model that provides utility like\n * streaming and function calling\n */\nexport interface IChatPrompt<\n TOptions extends Record<string, any> = Record<string, any>,\n TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = [],\n> {\n /**\n * the prompt name\n */\n readonly name: string;\n\n /**\n * the prompt description\n */\n readonly description: string;\n\n /**\n * the chat history\n */\n readonly messages: IMemory;\n\n /**\n * the registered functions\n */\n readonly functions: Array<Function>;\n\n /**\n * the chat model\n */\n plugins: TChatPromptPlugins;\n\n /**\n * add another chat prompt as a\n */\n use(prompt: IChatPrompt): this;\n use(name: string, prompt: IChatPrompt): this;\n\n /**\n * add a function that can be called\n * by the model\n */\n function(name: string, description: string, handler: FunctionHandler): this;\n function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;\n\n usePlugin<TPluginName extends TChatPromptPlugins[number]['name']>(\n name: TPluginName,\n args: Extract<TChatPromptPlugins[number], { name: TPluginName }>['onUsePlugin'] extends\n | ((args: infer U) => void)\n | undefined\n ? U\n : never\n ): this;\n\n /**\n * call a function\n */\n call<A extends Record<string, any>, R = any>(name: string, args?: A): Promise<R>;\n\n /**\n * send a message to the model and get a response\n */\n send(\n input: string | ContentPart[],\n options?: ChatPromptSendOptions<TOptions>\n ): Promise<Pick<ModelMessage, 'content'> & Omit<ModelMessage, 'content'>>;\n}\n\nexport type ChatPromptPlugin<TPluginName extends string, TPluginUseArgs extends {}> = IAiPlugin<\n TPluginName,\n TPluginUseArgs,\n Parameters<IChatPrompt['send']>[0],\n ReturnType<IChatPrompt['send']>\n> & {\n /**\n * Optionally passed in to modify the functions array that\n * is passed to the model\n * @param functions\n * @returns Functions\n */\n onBuildFunctions?: (functions: Function[]) => Function[] | Promise<Function[]>;\n};\n\n/**\n * a prompt that can interface with a\n * chat model that provides utility like\n * streaming and function calling\n */\nexport class ChatPrompt<\n TOptions extends Record<string, any> = Record<string, any>,\n TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = [],\n> implements IChatPrompt<TOptions, TChatPromptPlugins>\n{\n get name() {\n return this._name;\n }\n protected readonly _name: string;\n\n get description() {\n return this._description;\n }\n protected readonly _description: string;\n\n get messages() {\n return this._messages;\n }\n protected readonly _messages: IMemory;\n\n get functions() {\n return Object.values(this._functions);\n }\n protected readonly _functions: Record<string, Function> = {};\n\n get plugins() {\n return this._plugins;\n }\n protected readonly _plugins: TChatPromptPlugins;\n\n protected readonly _role: 'system' | 'user';\n protected readonly _template: ITemplate;\n protected readonly _model: IChatModel<TOptions>;\n\n constructor(options: ChatPromptOptions<TOptions>, plugins?: TChatPromptPlugins) {\n this._name = options.name || 'chat';\n this._description = options.description || 'an agent you can chat with';\n this._role = options.role || 'system';\n this._model = options.model;\n this._template = Array.isArray(options.instructions)\n ? new StringTemplate(options.instructions.join('\\n'))\n : typeof options.instructions !== 'object'\n ? new StringTemplate(options.instructions)\n : options.instructions;\n\n this._messages =\n typeof options.messages === 'object' && !Array.isArray(options.messages)\n ? options.messages\n : new LocalMemory({ messages: options.messages || [] });\n\n this._plugins = plugins || ([] as unknown as TChatPromptPlugins);\n }\n\n use(prompt: ChatPrompt): this;\n use(name: string, prompt: ChatPrompt): this;\n use(...args: any[]) {\n const prompt: ChatPrompt = args.length === 1 ? args[0] : args[1];\n const name: string = args.length === 1 ? prompt.name : args[0];\n this._functions[name] = {\n name,\n description: prompt.description,\n parameters: {\n type: 'object',\n properties: {\n text: {\n type: 'string',\n description: 'the text to send to the assistant',\n },\n },\n required: ['text'],\n },\n handler: ({ text }: { text: string }) => {\n return prompt.send(text);\n },\n };\n\n return this;\n }\n\n function(name: string, description: string, handler: FunctionHandler): this;\n function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;\n function(...args: any[]) {\n const name: string = args[0];\n const description: string = args[1];\n const parameters: Schema | null = args.length === 3 ? null : args[2];\n const handler: FunctionHandler = args[args.length - 1];\n this._functions[name] = {\n name,\n description,\n parameters: parameters || {},\n handler,\n };\n\n return this;\n }\n\n usePlugin<K extends TChatPromptPlugins[number]['name']>(\n name: K,\n args: Extract<TChatPromptPlugins[number], { name: K }>['onUsePlugin'] extends\n | ((args: infer U) => void)\n | undefined\n ? U\n : never\n ): this {\n const plugin = this._plugins.find((p) => p.name === name);\n if (!plugin) {\n throw new Error(`Plugin \"${name}\" not found`);\n }\n\n if (plugin.onUsePlugin) {\n plugin.onUsePlugin(args);\n }\n\n return this;\n }\n\n async call<A extends { [key: string]: any }, R = any>(name: string, args?: A): Promise<R> {\n const fn = this._functions[name];\n\n if (!fn) {\n throw new Error(`function \"${name}\" not found`);\n }\n\n return await fn.handler(args || {});\n }\n\n async send(input: string | ContentPart[], options: ChatPromptSendOptions<TOptions> = {}) {\n for (const plugin of this.plugins) {\n if (plugin.onBeforeSend) {\n input = await plugin.onBeforeSend(input);\n }\n }\n\n const { onChunk } = options;\n\n if (typeof input === 'string') {\n input = input.trim();\n }\n\n const messages = !Array.isArray(options.messages)\n ? options.messages || this._messages\n : new LocalMemory({ messages: options.messages || [] });\n\n let buffer = '';\n let system: SystemMessage | UserMessage | undefined = undefined;\n const prompt = await this._template.render();\n\n if (prompt) {\n system = {\n role: this._role,\n content: prompt,\n };\n }\n\n let functions = Object.values(this._functions);\n const pluginsWithOnBuildFunctions = this._plugins.filter(\n (plugin): plugin is WithRequired<TChatPromptPlugins[number], 'onBuildFunctions'> =>\n plugin.onBuildFunctions != null\n );\n for (const plugin of pluginsWithOnBuildFunctions) {\n functions = await plugin.onBuildFunctions(functions);\n }\n\n const fnMap = functions.reduce(\n (acc, fn) => {\n acc[fn.name] = fn;\n return acc;\n },\n {} as Record<string, Function>\n );\n\n const res = await this._model.send(\n {\n role: 'user',\n content: input,\n },\n {\n system,\n messages,\n request: options.request,\n functions: fnMap,\n onChunk: async (chunk) => {\n if (!chunk || !onChunk) return;\n buffer += chunk;\n\n try {\n await onChunk(buffer);\n buffer = '';\n } catch (err) {\n return;\n }\n },\n }\n );\n\n let output: Awaited<ReturnType<typeof this._model.send>> = {\n ...res,\n content: res.content || '',\n };\n for (const plugin of this.plugins) {\n if (plugin.onAfterSend) {\n output = await plugin.onAfterSend(output);\n }\n }\n\n return output;\n }\n}\n"]}
@@ -0,0 +1,160 @@
1
+ import { LocalMemory } from '../local-memory';
2
+ import { StringTemplate } from '../templates';
3
+
4
+ class ChatPrompt {
5
+ get name() {
6
+ return this._name;
7
+ }
8
+ _name;
9
+ get description() {
10
+ return this._description;
11
+ }
12
+ _description;
13
+ get messages() {
14
+ return this._messages;
15
+ }
16
+ _messages;
17
+ get functions() {
18
+ return Object.values(this._functions);
19
+ }
20
+ _functions = {};
21
+ get plugins() {
22
+ return this._plugins;
23
+ }
24
+ _plugins;
25
+ _role;
26
+ _template;
27
+ _model;
28
+ constructor(options, plugins) {
29
+ this._name = options.name || "chat";
30
+ this._description = options.description || "an agent you can chat with";
31
+ this._role = options.role || "system";
32
+ this._model = options.model;
33
+ this._template = Array.isArray(options.instructions) ? new StringTemplate(options.instructions.join("\n")) : typeof options.instructions !== "object" ? new StringTemplate(options.instructions) : options.instructions;
34
+ this._messages = typeof options.messages === "object" && !Array.isArray(options.messages) ? options.messages : new LocalMemory({ messages: options.messages || [] });
35
+ this._plugins = plugins || [];
36
+ }
37
+ use(...args) {
38
+ const prompt = args.length === 1 ? args[0] : args[1];
39
+ const name = args.length === 1 ? prompt.name : args[0];
40
+ this._functions[name] = {
41
+ name,
42
+ description: prompt.description,
43
+ parameters: {
44
+ type: "object",
45
+ properties: {
46
+ text: {
47
+ type: "string",
48
+ description: "the text to send to the assistant"
49
+ }
50
+ },
51
+ required: ["text"]
52
+ },
53
+ handler: ({ text }) => {
54
+ return prompt.send(text);
55
+ }
56
+ };
57
+ return this;
58
+ }
59
+ function(...args) {
60
+ const name = args[0];
61
+ const description = args[1];
62
+ const parameters = args.length === 3 ? null : args[2];
63
+ const handler = args[args.length - 1];
64
+ this._functions[name] = {
65
+ name,
66
+ description,
67
+ parameters: parameters || {},
68
+ handler
69
+ };
70
+ return this;
71
+ }
72
+ usePlugin(name, args) {
73
+ const plugin = this._plugins.find((p) => p.name === name);
74
+ if (!plugin) {
75
+ throw new Error(`Plugin "${name}" not found`);
76
+ }
77
+ if (plugin.onUsePlugin) {
78
+ plugin.onUsePlugin(args);
79
+ }
80
+ return this;
81
+ }
82
+ async call(name, args) {
83
+ const fn = this._functions[name];
84
+ if (!fn) {
85
+ throw new Error(`function "${name}" not found`);
86
+ }
87
+ return await fn.handler(args || {});
88
+ }
89
+ async send(input, options = {}) {
90
+ for (const plugin of this.plugins) {
91
+ if (plugin.onBeforeSend) {
92
+ input = await plugin.onBeforeSend(input);
93
+ }
94
+ }
95
+ const { onChunk } = options;
96
+ if (typeof input === "string") {
97
+ input = input.trim();
98
+ }
99
+ const messages = !Array.isArray(options.messages) ? options.messages || this._messages : new LocalMemory({ messages: options.messages || [] });
100
+ let buffer = "";
101
+ let system = void 0;
102
+ const prompt = await this._template.render();
103
+ if (prompt) {
104
+ system = {
105
+ role: this._role,
106
+ content: prompt
107
+ };
108
+ }
109
+ let functions = Object.values(this._functions);
110
+ const pluginsWithOnBuildFunctions = this._plugins.filter(
111
+ (plugin) => plugin.onBuildFunctions != null
112
+ );
113
+ for (const plugin of pluginsWithOnBuildFunctions) {
114
+ functions = await plugin.onBuildFunctions(functions);
115
+ }
116
+ const fnMap = functions.reduce(
117
+ (acc, fn) => {
118
+ acc[fn.name] = fn;
119
+ return acc;
120
+ },
121
+ {}
122
+ );
123
+ const res = await this._model.send(
124
+ {
125
+ role: "user",
126
+ content: input
127
+ },
128
+ {
129
+ system,
130
+ messages,
131
+ request: options.request,
132
+ functions: fnMap,
133
+ onChunk: async (chunk) => {
134
+ if (!chunk || !onChunk) return;
135
+ buffer += chunk;
136
+ try {
137
+ await onChunk(buffer);
138
+ buffer = "";
139
+ } catch (err) {
140
+ return;
141
+ }
142
+ }
143
+ }
144
+ );
145
+ let output = {
146
+ ...res,
147
+ content: res.content || ""
148
+ };
149
+ for (const plugin of this.plugins) {
150
+ if (plugin.onAfterSend) {
151
+ output = await plugin.onAfterSend(output);
152
+ }
153
+ }
154
+ return output;
155
+ }
156
+ }
157
+
158
+ export { ChatPrompt };
159
+ //# sourceMappingURL=chat.mjs.map
160
+ //# sourceMappingURL=chat.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/prompts/chat.ts"],"names":[],"mappings":";;;AA0JO,MAAM,UAIb,CAAA;AAAA,EACE,IAAI,IAAO,GAAA;AACT,IAAA,OAAO,IAAK,CAAA,KAAA;AAAA;AACd,EACmB,KAAA;AAAA,EAEnB,IAAI,WAAc,GAAA;AAChB,IAAA,OAAO,IAAK,CAAA,YAAA;AAAA;AACd,EACmB,YAAA;AAAA,EAEnB,IAAI,QAAW,GAAA;AACb,IAAA,OAAO,IAAK,CAAA,SAAA;AAAA;AACd,EACmB,SAAA;AAAA,EAEnB,IAAI,SAAY,GAAA;AACd,IAAO,OAAA,MAAA,CAAO,MAAO,CAAA,IAAA,CAAK,UAAU,CAAA;AAAA;AACtC,EACmB,aAAuC,EAAC;AAAA,EAE3D,IAAI,OAAU,GAAA;AACZ,IAAA,OAAO,IAAK,CAAA,QAAA;AAAA;AACd,EACmB,QAAA;AAAA,EAEA,KAAA;AAAA,EACA,SAAA;AAAA,EACA,MAAA;AAAA,EAEnB,WAAA,CAAY,SAAsC,OAA8B,EAAA;AAC9E,IAAK,IAAA,CAAA,KAAA,GAAQ,QAAQ,IAAQ,IAAA,MAAA;AAC7B,IAAK,IAAA,CAAA,YAAA,GAAe,QAAQ,WAAe,IAAA,4BAAA;AAC3C,IAAK,IAAA,CAAA,KAAA,GAAQ,QAAQ,IAAQ,IAAA,QAAA;AAC7B,IAAA,IAAA,CAAK,SAAS,OAAQ,CAAA,KAAA;AACtB,IAAK,IAAA,CAAA,SAAA,GAAY,MAAM,OAAQ,CAAA,OAAA,CAAQ,YAAY,CAC/C,GAAA,IAAI,cAAe,CAAA,OAAA,CAAQ,YAAa,CAAA,IAAA,CAAK,IAAI,CAAC,CAAA,GAClD,OAAO,OAAA,CAAQ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5B;AAAA,QACE,IAAM,EAAA,MAAA;AAAA,QACN,OAAS,EAAA;AAAA,OACX;AAAA,MACA;AAAA,QACE,MAAA;AAAA,QACA,QAAA;AAAA,QACA,SAAS,OAAQ,CAAA,OAAA;AAAA,QACjB,SAAW,EAAA,KAAA;AAAA,QACX,OAAA,EAAS,OAAO,KAAU,KAAA;AACxB,UAAI,IAAA,CAAC,KAAS,IAAA,CAAC,OAAS,EAAA;AACxB,UAAU,MAAA,IAAA,KAAA;AAEV,UAAI,IAAA;AACF,YAAA,MAAM,QAAQ,MAAM,CAAA;AACpB,YAAS,MAAA,GAAA,EAAA;AAAA,mBACF,GAAK,EAAA;AACZ,YAAA;AAAA;AACF;AACF;AACF,KACF;AAEA,IAAA,IAAI,MAAuD,GAAA;AAAA,MACzD,GAAG,GAAA;AAAA,MACH,OAAA,EAAS,IAAI,OAAW,IAAA;AAAA,KAC1B;AACA,IAAW,KAAA,MAAA,MAAA,IAAU,KAAK,OAAS,EAAA;AACjC,MAAA,IAAI,OAAO,WAAa,EAAA;AACtB,QAAS,MAAA,GAAA,MAAM,MAAO,CAAA,WAAA,CAAY,MAAM,CAAA;AAAA;AAC1C;AAGF,IAAO,OAAA,MAAA;AAAA;AAEX","file":"chat.mjs","sourcesContent":["import { Function, FunctionHandler } from '../function';\nimport { LocalMemory } from '../local-memory';\nimport { IMemory } from '../memory';\nimport { ContentPart, Message, ModelMessage, SystemMessage, UserMessage } from '../message';\nimport { IChatModel, TextChunkHandler } from '../models';\nimport { Schema } from '../schema';\nimport { ITemplate } from '../template';\nimport { StringTemplate } from '../templates';\nimport { WithRequired } from '../utils/types';\nimport { IAiPlugin } from './plugin';\n\nexport type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any>> = {\n /**\n * the name of the prompt\n */\n readonly name?: string;\n\n /**\n * the description of the prompt\n */\n readonly description?: string;\n\n /**\n * the model to send messages to\n */\n readonly model: IChatModel<TOptions>;\n\n /**\n * the defining characteristics/objective\n * of the prompt. This is commonly used to provide a system prompt.\n * If you supply the system prompt as part of the messages,\n * you do not need to supply this option.\n */\n readonly instructions?: string | string[] | ITemplate;\n\n /**\n * the `role` of the initial message\n */\n readonly role?: 'system' | 'user';\n\n /**\n * the conversation history\n */\n readonly messages?: Message[] | IMemory;\n};\n\nexport type ChatPromptSendOptions<TOptions extends Record<string, any> = Record<string, any>> = {\n /**\n * the conversation history\n */\n readonly messages?: Message[] | IMemory;\n\n /**\n * the models request options\n */\n readonly request?: TOptions;\n\n /**\n * the callback to be called for each\n * stream chunk\n */\n readonly onChunk?: TextChunkHandler;\n};\n\n/**\n * a prompt that can interface with a\n * chat model that provides utility like\n * streaming and function calling\n */\nexport interface IChatPrompt<\n TOptions extends Record<string, any> = Record<string, any>,\n TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = [],\n> {\n /**\n * the prompt name\n */\n readonly name: string;\n\n /**\n * the prompt description\n */\n readonly description: string;\n\n /**\n * the chat history\n */\n readonly messages: IMemory;\n\n /**\n * the registered functions\n */\n readonly functions: Array<Function>;\n\n /**\n * the chat model\n */\n plugins: TChatPromptPlugins;\n\n /**\n * add another chat prompt as a\n */\n use(prompt: IChatPrompt): this;\n use(name: string, prompt: IChatPrompt): this;\n\n /**\n * add a function that can be called\n * by the model\n */\n function(name: string, description: string, handler: FunctionHandler): this;\n function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;\n\n usePlugin<TPluginName extends TChatPromptPlugins[number]['name']>(\n name: TPluginName,\n args: Extract<TChatPromptPlugins[number], { name: TPluginName }>['onUsePlugin'] extends\n | ((args: infer U) => void)\n | undefined\n ? U\n : never\n ): this;\n\n /**\n * call a function\n */\n call<A extends Record<string, any>, R = any>(name: string, args?: A): Promise<R>;\n\n /**\n * send a message to the model and get a response\n */\n send(\n input: string | ContentPart[],\n options?: ChatPromptSendOptions<TOptions>\n ): Promise<Pick<ModelMessage, 'content'> & Omit<ModelMessage, 'content'>>;\n}\n\nexport type ChatPromptPlugin<TPluginName extends string, TPluginUseArgs extends {}> = IAiPlugin<\n TPluginName,\n TPluginUseArgs,\n Parameters<IChatPrompt['send']>[0],\n ReturnType<IChatPrompt['send']>\n> & {\n /**\n * Optionally passed in to modify the functions array that\n * is passed to the model\n * @param functions\n * @returns Functions\n */\n onBuildFunctions?: (functions: Function[]) => Function[] | Promise<Function[]>;\n};\n\n/**\n * a prompt that can interface with a\n * chat model that provides utility like\n * streaming and function calling\n */\nexport class ChatPrompt<\n TOptions extends Record<string, any> = Record<string, any>,\n TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = [],\n> implements IChatPrompt<TOptions, TChatPromptPlugins>\n{\n get name() {\n return this._name;\n }\n protected readonly _name: string;\n\n get description() {\n return this._description;\n }\n protected readonly _description: string;\n\n get messages() {\n return this._messages;\n }\n protected readonly _messages: IMemory;\n\n get functions() {\n return Object.values(this._functions);\n }\n protected readonly _functions: Record<string, Function> = {};\n\n get plugins() {\n return this._plugins;\n }\n protected readonly _plugins: TChatPromptPlugins;\n\n protected readonly _role: 'system' | 'user';\n protected readonly _template: ITemplate;\n protected readonly _model: IChatModel<TOptions>;\n\n constructor(options: ChatPromptOptions<TOptions>, plugins?: TChatPromptPlugins) {\n this._name = options.name || 'chat';\n this._description = options.description || 'an agent you can chat with';\n this._role = options.role || 'system';\n this._model = options.model;\n this._template = Array.isArray(options.instructions)\n ? new StringTemplate(options.instructions.join('\\n'))\n : typeof options.instructions !== 'object'\n ? new StringTemplate(options.instructions)\n : options.instructions;\n\n this._messages =\n typeof options.messages === 'object' && !Array.isArray(options.messages)\n ? options.messages\n : new LocalMemory({ messages: options.messages || [] });\n\n this._plugins = plugins || ([] as unknown as TChatPromptPlugins);\n }\n\n use(prompt: ChatPrompt): this;\n use(name: string, prompt: ChatPrompt): this;\n use(...args: any[]) {\n const prompt: ChatPrompt = args.length === 1 ? args[0] : args[1];\n const name: string = args.length === 1 ? prompt.name : args[0];\n this._functions[name] = {\n name,\n description: prompt.description,\n parameters: {\n type: 'object',\n properties: {\n text: {\n type: 'string',\n description: 'the text to send to the assistant',\n },\n },\n required: ['text'],\n },\n handler: ({ text }: { text: string }) => {\n return prompt.send(text);\n },\n };\n\n return this;\n }\n\n function(name: string, description: string, handler: FunctionHandler): this;\n function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;\n function(...args: any[]) {\n const name: string = args[0];\n const description: string = args[1];\n const parameters: Schema | null = args.length === 3 ? null : args[2];\n const handler: FunctionHandler = args[args.length - 1];\n this._functions[name] = {\n name,\n description,\n parameters: parameters || {},\n handler,\n };\n\n return this;\n }\n\n usePlugin<K extends TChatPromptPlugins[number]['name']>(\n name: K,\n args: Extract<TChatPromptPlugins[number], { name: K }>['onUsePlugin'] extends\n | ((args: infer U) => void)\n | undefined\n ? U\n : never\n ): this {\n const plugin = this._plugins.find((p) => p.name === name);\n if (!plugin) {\n throw new Error(`Plugin \"${name}\" not found`);\n }\n\n if (plugin.onUsePlugin) {\n plugin.onUsePlugin(args);\n }\n\n return this;\n }\n\n async call<A extends { [key: string]: any }, R = any>(name: string, args?: A): Promise<R> {\n const fn = this._functions[name];\n\n if (!fn) {\n throw new Error(`function \"${name}\" not found`);\n }\n\n return await fn.handler(args || {});\n }\n\n async send(input: string | ContentPart[], options: ChatPromptSendOptions<TOptions> = {}) {\n for (const plugin of this.plugins) {\n if (plugin.onBeforeSend) {\n input = await plugin.onBeforeSend(input);\n }\n }\n\n const { onChunk } = options;\n\n if (typeof input === 'string') {\n input = input.trim();\n }\n\n const messages = !Array.isArray(options.messages)\n ? options.messages || this._messages\n : new LocalMemory({ messages: options.messages || [] });\n\n let buffer = '';\n let system: SystemMessage | UserMessage | undefined = undefined;\n const prompt = await this._template.render();\n\n if (prompt) {\n system = {\n role: this._role,\n content: prompt,\n };\n }\n\n let functions = Object.values(this._functions);\n const pluginsWithOnBuildFunctions = this._plugins.filter(\n (plugin): plugin is WithRequired<TChatPromptPlugins[number], 'onBuildFunctions'> =>\n plugin.onBuildFunctions != null\n );\n for (const plugin of pluginsWithOnBuildFunctions) {\n functions = await plugin.onBuildFunctions(functions);\n }\n\n const fnMap = functions.reduce(\n (acc, fn) => {\n acc[fn.name] = fn;\n return acc;\n },\n {} as Record<string, Function>\n );\n\n const res = await this._model.send(\n {\n role: 'user',\n content: input,\n },\n {\n system,\n messages,\n request: options.request,\n functions: fnMap,\n onChunk: async (chunk) => {\n if (!chunk || !onChunk) return;\n buffer += chunk;\n\n try {\n await onChunk(buffer);\n buffer = '';\n } catch (err) {\n return;\n }\n },\n }\n );\n\n let output: Awaited<ReturnType<typeof this._model.send>> = {\n ...res,\n content: res.content || '',\n };\n for (const plugin of this.plugins) {\n if (plugin.onAfterSend) {\n output = await plugin.onAfterSend(output);\n }\n }\n\n return output;\n }\n}\n"]}