@microsoft/teams.ai 2.0.0-preview.5 → 2.0.0-preview.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.
package/dist/index.d.mts CHANGED
@@ -11,8 +11,9 @@ export { StringTemplate } from './templates/string.mjs';
11
11
  export { ChatSendOptions, IChatModel, TextChunkHandler } from './models/chat.mjs';
12
12
  export { AudioToTextParams, IAudioModel, TextToAudioParams } from './models/audio.mjs';
13
13
  export { IImageModel, TextToImageParams } from './models/image.mjs';
14
- export { ChatPrompt, ChatPromptOptions, ChatPromptPlugin, ChatPromptSendOptions, IChatPrompt } from './prompts/chat.mjs';
15
14
  export { AudioPrompt, AudioPromptOptions, IAudioPrompt } from './prompts/audio.mjs';
15
+ export { ChatPrompt } from './prompts/chat.mjs';
16
+ export { ChatPromptOptions, ChatPromptPlugin, ChatPromptSendOptions, IChatPrompt } from './prompts/chat-types.mjs';
16
17
  import '@microsoft/teams.common';
17
18
  import './utils/types.mjs';
18
19
  import './prompts/plugin.mjs';
package/dist/index.d.ts CHANGED
@@ -11,8 +11,9 @@ export { StringTemplate } from './templates/string.js';
11
11
  export { ChatSendOptions, IChatModel, TextChunkHandler } from './models/chat.js';
12
12
  export { AudioToTextParams, IAudioModel, TextToAudioParams } from './models/audio.js';
13
13
  export { IImageModel, TextToImageParams } from './models/image.js';
14
- export { ChatPrompt, ChatPromptOptions, ChatPromptPlugin, ChatPromptSendOptions, IChatPrompt } from './prompts/chat.js';
15
14
  export { AudioPrompt, AudioPromptOptions, IAudioPrompt } from './prompts/audio.js';
15
+ export { ChatPrompt } from './prompts/chat.js';
16
+ export { ChatPromptOptions, ChatPromptPlugin, ChatPromptSendOptions, IChatPrompt } from './prompts/chat-types.js';
16
17
  import '@microsoft/teams.common';
17
18
  import './utils/types.js';
18
19
  import './prompts/plugin.js';
@@ -29,6 +29,11 @@ type ChatSendOptions<TOptions = Record<string, any>> = {
29
29
  * stream chunk
30
30
  */
31
31
  readonly onChunk?: TextChunkHandler;
32
+ /**
33
+ * enable/disable automatic function calling
34
+ * @default true
35
+ */
36
+ readonly autoFunctionCalling?: boolean;
32
37
  };
33
38
  /**
34
39
  * a conversational model for sending and receiving
@@ -29,6 +29,11 @@ type ChatSendOptions<TOptions = Record<string, any>> = {
29
29
  * stream chunk
30
30
  */
31
31
  readonly onChunk?: TextChunkHandler;
32
+ /**
33
+ * enable/disable automatic function calling
34
+ * @default true
35
+ */
36
+ readonly autoFunctionCalling?: boolean;
32
37
  };
33
38
  /**
34
39
  * a conversational model for sending and receiving
@@ -0,0 +1,131 @@
1
+ import { ILogger } from '@microsoft/teams.common';
2
+ import { Function, FunctionHandler } from '../function.mjs';
3
+ import { IMemory } from '../memory.mjs';
4
+ import { ContentPart, Message, ModelMessage } from '../message.mjs';
5
+ import { TextChunkHandler, IChatModel } from '../models/chat.mjs';
6
+ import { Schema } from '../schema.mjs';
7
+ import { ITemplate } from '../template.mjs';
8
+ import { PromiseOrValue } from '../utils/types.mjs';
9
+ import { IAiPlugin } from './plugin.mjs';
10
+ import '../citation.mjs';
11
+
12
+ type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any>> = {
13
+ /**
14
+ * the name of the prompt
15
+ */
16
+ readonly name?: string;
17
+ /**
18
+ * the description of the prompt
19
+ */
20
+ readonly description?: string;
21
+ /**
22
+ * the model to send messages to
23
+ */
24
+ readonly model: IChatModel<TOptions>;
25
+ /**
26
+ * the defining characteristics/objective
27
+ * of the prompt. This is commonly used to provide a system prompt.
28
+ * If you supply the system prompt as part of the messages,
29
+ * you do not need to supply this option.
30
+ */
31
+ readonly instructions?: string | string[] | ITemplate;
32
+ /**
33
+ * the `role` of the initial message
34
+ */
35
+ readonly role?: 'system' | 'user';
36
+ /**
37
+ * the conversation history
38
+ */
39
+ readonly messages?: Message[] | IMemory;
40
+ /**
41
+ * Logger instance to use for logging
42
+ * If not provided, a ConsoleLogger will be used
43
+ */
44
+ logger?: ILogger;
45
+ };
46
+ type ChatPromptSendOptions<TOptions extends Record<string, any> = Record<string, any>> = {
47
+ /**
48
+ * the conversation history
49
+ */
50
+ readonly messages?: Message[] | IMemory;
51
+ /**
52
+ * the models request options
53
+ */
54
+ readonly request?: TOptions;
55
+ /**
56
+ * the callback to be called for each
57
+ * stream chunk
58
+ */
59
+ readonly onChunk?: TextChunkHandler;
60
+ /**
61
+ * enable/disable automatic function calling
62
+ * @default true
63
+ */
64
+ readonly autoFunctionCalling?: boolean;
65
+ };
66
+ /**
67
+ * a prompt that can interface with a
68
+ * chat model that provides utility like
69
+ * streaming and function calling
70
+ */
71
+ interface IChatPrompt<TOptions extends Record<string, any> = Record<string, any>, TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = []> {
72
+ /**
73
+ * the prompt name
74
+ */
75
+ readonly name: string;
76
+ /**
77
+ * the prompt description
78
+ */
79
+ readonly description: string;
80
+ /**
81
+ * the chat history
82
+ */
83
+ readonly messages: IMemory;
84
+ /**
85
+ * the registered functions
86
+ */
87
+ readonly functions: Array<Function>;
88
+ /**
89
+ * the chat model
90
+ */
91
+ plugins: TChatPromptPlugins;
92
+ /**
93
+ * add another chat prompt as a
94
+ */
95
+ use(prompt: IChatPrompt): this;
96
+ use(name: string, prompt: IChatPrompt): this;
97
+ /**
98
+ * add a function that can be called
99
+ * by the model
100
+ */
101
+ function(name: string, description: string, handler: FunctionHandler): this;
102
+ function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;
103
+ usePlugin<TPluginName extends TChatPromptPlugins[number]['name']>(name: TPluginName, args: Extract<TChatPromptPlugins[number], {
104
+ name: TPluginName;
105
+ }>['onUsePlugin'] extends ((args: infer U) => void) | undefined ? U : never): this;
106
+ /**
107
+ * call a function
108
+ */
109
+ call<A extends Record<string, any>, R = any>(name: string, args?: A): Promise<R>;
110
+ /**
111
+ * send a message to the model and get a response
112
+ */
113
+ send(input: string | ContentPart[], options?: ChatPromptSendOptions<TOptions>): Promise<ModelMessage>;
114
+ }
115
+ type ChatPromptPlugin<TPluginName extends string, TPluginUseArgs extends {}> = IAiPlugin<TPluginName, TPluginUseArgs, Parameters<IChatPrompt['send']>[0], ReturnType<IChatPrompt['send']>> & {
116
+ /**
117
+ * Optionally passed in to modify the functions array that
118
+ * is passed to the model
119
+ * @param functions
120
+ * @returns Functions
121
+ */
122
+ onBuildFunctions?: (functions: Function[]) => PromiseOrValue<Function[]>;
123
+ /**
124
+ * Optionally passed in to modify the system prompt before it is sent to the model.
125
+ * @param systemPrompt The system prompt string (or undefined)
126
+ * @returns The modified system prompt string (or undefined)
127
+ */
128
+ onBuildPrompt?: (systemPrompt: string | undefined) => PromiseOrValue<string | undefined>;
129
+ };
130
+
131
+ export type { ChatPromptOptions, ChatPromptPlugin, ChatPromptSendOptions, IChatPrompt };
@@ -0,0 +1,131 @@
1
+ import { ILogger } from '@microsoft/teams.common';
2
+ import { Function, FunctionHandler } from '../function.js';
3
+ import { IMemory } from '../memory.js';
4
+ import { ContentPart, Message, ModelMessage } from '../message.js';
5
+ import { TextChunkHandler, IChatModel } from '../models/chat.js';
6
+ import { Schema } from '../schema.js';
7
+ import { ITemplate } from '../template.js';
8
+ import { PromiseOrValue } from '../utils/types.js';
9
+ import { IAiPlugin } from './plugin.js';
10
+ import '../citation.js';
11
+
12
+ type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any>> = {
13
+ /**
14
+ * the name of the prompt
15
+ */
16
+ readonly name?: string;
17
+ /**
18
+ * the description of the prompt
19
+ */
20
+ readonly description?: string;
21
+ /**
22
+ * the model to send messages to
23
+ */
24
+ readonly model: IChatModel<TOptions>;
25
+ /**
26
+ * the defining characteristics/objective
27
+ * of the prompt. This is commonly used to provide a system prompt.
28
+ * If you supply the system prompt as part of the messages,
29
+ * you do not need to supply this option.
30
+ */
31
+ readonly instructions?: string | string[] | ITemplate;
32
+ /**
33
+ * the `role` of the initial message
34
+ */
35
+ readonly role?: 'system' | 'user';
36
+ /**
37
+ * the conversation history
38
+ */
39
+ readonly messages?: Message[] | IMemory;
40
+ /**
41
+ * Logger instance to use for logging
42
+ * If not provided, a ConsoleLogger will be used
43
+ */
44
+ logger?: ILogger;
45
+ };
46
+ type ChatPromptSendOptions<TOptions extends Record<string, any> = Record<string, any>> = {
47
+ /**
48
+ * the conversation history
49
+ */
50
+ readonly messages?: Message[] | IMemory;
51
+ /**
52
+ * the models request options
53
+ */
54
+ readonly request?: TOptions;
55
+ /**
56
+ * the callback to be called for each
57
+ * stream chunk
58
+ */
59
+ readonly onChunk?: TextChunkHandler;
60
+ /**
61
+ * enable/disable automatic function calling
62
+ * @default true
63
+ */
64
+ readonly autoFunctionCalling?: boolean;
65
+ };
66
+ /**
67
+ * a prompt that can interface with a
68
+ * chat model that provides utility like
69
+ * streaming and function calling
70
+ */
71
+ interface IChatPrompt<TOptions extends Record<string, any> = Record<string, any>, TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = []> {
72
+ /**
73
+ * the prompt name
74
+ */
75
+ readonly name: string;
76
+ /**
77
+ * the prompt description
78
+ */
79
+ readonly description: string;
80
+ /**
81
+ * the chat history
82
+ */
83
+ readonly messages: IMemory;
84
+ /**
85
+ * the registered functions
86
+ */
87
+ readonly functions: Array<Function>;
88
+ /**
89
+ * the chat model
90
+ */
91
+ plugins: TChatPromptPlugins;
92
+ /**
93
+ * add another chat prompt as a
94
+ */
95
+ use(prompt: IChatPrompt): this;
96
+ use(name: string, prompt: IChatPrompt): this;
97
+ /**
98
+ * add a function that can be called
99
+ * by the model
100
+ */
101
+ function(name: string, description: string, handler: FunctionHandler): this;
102
+ function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;
103
+ usePlugin<TPluginName extends TChatPromptPlugins[number]['name']>(name: TPluginName, args: Extract<TChatPromptPlugins[number], {
104
+ name: TPluginName;
105
+ }>['onUsePlugin'] extends ((args: infer U) => void) | undefined ? U : never): this;
106
+ /**
107
+ * call a function
108
+ */
109
+ call<A extends Record<string, any>, R = any>(name: string, args?: A): Promise<R>;
110
+ /**
111
+ * send a message to the model and get a response
112
+ */
113
+ send(input: string | ContentPart[], options?: ChatPromptSendOptions<TOptions>): Promise<ModelMessage>;
114
+ }
115
+ type ChatPromptPlugin<TPluginName extends string, TPluginUseArgs extends {}> = IAiPlugin<TPluginName, TPluginUseArgs, Parameters<IChatPrompt['send']>[0], ReturnType<IChatPrompt['send']>> & {
116
+ /**
117
+ * Optionally passed in to modify the functions array that
118
+ * is passed to the model
119
+ * @param functions
120
+ * @returns Functions
121
+ */
122
+ onBuildFunctions?: (functions: Function[]) => PromiseOrValue<Function[]>;
123
+ /**
124
+ * Optionally passed in to modify the system prompt before it is sent to the model.
125
+ * @param systemPrompt The system prompt string (or undefined)
126
+ * @returns The modified system prompt string (or undefined)
127
+ */
128
+ onBuildPrompt?: (systemPrompt: string | undefined) => PromiseOrValue<string | undefined>;
129
+ };
130
+
131
+ export type { ChatPromptOptions, ChatPromptPlugin, ChatPromptSendOptions, IChatPrompt };
@@ -0,0 +1,4 @@
1
+ 'use strict';
2
+
3
+ //# sourceMappingURL=chat-types.js.map
4
+ //# sourceMappingURL=chat-types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"chat-types.js"}
@@ -0,0 +1,3 @@
1
+
2
+ //# sourceMappingURL=chat-types.mjs.map
3
+ //# sourceMappingURL=chat-types.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"chat-types.mjs"}
@@ -1,127 +1,15 @@
1
+ import { ContentPart, ModelMessage } from '../message.mjs';
1
2
  import { ILogger } from '@microsoft/teams.common';
2
3
  import { Function, FunctionHandler } from '../function.mjs';
3
4
  import { IMemory } from '../memory.mjs';
4
- import { Message, ContentPart, ModelMessage } from '../message.mjs';
5
- import { IChatModel, TextChunkHandler } from '../models/chat.mjs';
5
+ import { IChatModel } from '../models/chat.mjs';
6
6
  import { Schema } from '../schema.mjs';
7
7
  import { ITemplate } from '../template.mjs';
8
- import { PromiseOrValue } from '../utils/types.mjs';
9
- import { IAiPlugin } from './plugin.mjs';
8
+ import { ChatPromptPlugin, IChatPrompt, ChatPromptOptions, ChatPromptSendOptions } from './chat-types.mjs';
10
9
  import '../citation.mjs';
10
+ import '../utils/types.mjs';
11
+ import './plugin.mjs';
11
12
 
12
- type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any>> = {
13
- /**
14
- * the name of the prompt
15
- */
16
- readonly name?: string;
17
- /**
18
- * the description of the prompt
19
- */
20
- readonly description?: string;
21
- /**
22
- * the model to send messages to
23
- */
24
- readonly model: IChatModel<TOptions>;
25
- /**
26
- * the defining characteristics/objective
27
- * of the prompt. This is commonly used to provide a system prompt.
28
- * If you supply the system prompt as part of the messages,
29
- * you do not need to supply this option.
30
- */
31
- readonly instructions?: string | string[] | ITemplate;
32
- /**
33
- * the `role` of the initial message
34
- */
35
- readonly role?: 'system' | 'user';
36
- /**
37
- * the conversation history
38
- */
39
- readonly messages?: Message[] | IMemory;
40
- /**
41
- * Logger instance to use for logging
42
- * If not provided, a ConsoleLogger will be used
43
- */
44
- logger?: ILogger;
45
- };
46
- type ChatPromptSendOptions<TOptions extends Record<string, any> = Record<string, any>> = {
47
- /**
48
- * the conversation history
49
- */
50
- readonly messages?: Message[] | IMemory;
51
- /**
52
- * the models request options
53
- */
54
- readonly request?: TOptions;
55
- /**
56
- * the callback to be called for each
57
- * stream chunk
58
- */
59
- readonly onChunk?: TextChunkHandler;
60
- };
61
- /**
62
- * a prompt that can interface with a
63
- * chat model that provides utility like
64
- * streaming and function calling
65
- */
66
- interface IChatPrompt<TOptions extends Record<string, any> = Record<string, any>, TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = []> {
67
- /**
68
- * the prompt name
69
- */
70
- readonly name: string;
71
- /**
72
- * the prompt description
73
- */
74
- readonly description: string;
75
- /**
76
- * the chat history
77
- */
78
- readonly messages: IMemory;
79
- /**
80
- * the registered functions
81
- */
82
- readonly functions: Array<Function>;
83
- /**
84
- * the chat model
85
- */
86
- plugins: TChatPromptPlugins;
87
- /**
88
- * add another chat prompt as a
89
- */
90
- use(prompt: IChatPrompt): this;
91
- use(name: string, prompt: IChatPrompt): this;
92
- /**
93
- * add a function that can be called
94
- * by the model
95
- */
96
- function(name: string, description: string, handler: FunctionHandler): this;
97
- function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;
98
- usePlugin<TPluginName extends TChatPromptPlugins[number]['name']>(name: TPluginName, args: Extract<TChatPromptPlugins[number], {
99
- name: TPluginName;
100
- }>['onUsePlugin'] extends ((args: infer U) => void) | undefined ? U : never): this;
101
- /**
102
- * call a function
103
- */
104
- call<A extends Record<string, any>, R = any>(name: string, args?: A): Promise<R>;
105
- /**
106
- * send a message to the model and get a response
107
- */
108
- send(input: string | ContentPart[], options?: ChatPromptSendOptions<TOptions>): Promise<Pick<ModelMessage, 'content'> & Omit<ModelMessage, 'content'>>;
109
- }
110
- type ChatPromptPlugin<TPluginName extends string, TPluginUseArgs extends {}> = IAiPlugin<TPluginName, TPluginUseArgs, Parameters<IChatPrompt['send']>[0], ReturnType<IChatPrompt['send']>> & {
111
- /**
112
- * Optionally passed in to modify the functions array that
113
- * is passed to the model
114
- * @param functions
115
- * @returns Functions
116
- */
117
- onBuildFunctions?: (functions: Function[]) => PromiseOrValue<Function[]>;
118
- /**
119
- * Optionally passed in to modify the system prompt before it is sent to the model.
120
- * @param systemPrompt The system prompt string (or undefined)
121
- * @returns The modified system prompt string (or undefined)
122
- */
123
- onBuildPrompt?: (systemPrompt: string | undefined) => PromiseOrValue<string | undefined>;
124
- };
125
13
  /**
126
14
  * a prompt that can interface with a
127
15
  * chat model that provides utility like
@@ -157,4 +45,4 @@ declare class ChatPrompt<TOptions extends Record<string, any> = Record<string, a
157
45
  protected executeFunction<R = any>(name: string, fn: Function, args?: Record<string, any>): Promise<R>;
158
46
  }
159
47
 
160
- export { ChatPrompt, type ChatPromptOptions, type ChatPromptPlugin, type ChatPromptSendOptions, type IChatPrompt };
48
+ export { ChatPrompt };
@@ -1,127 +1,15 @@
1
+ import { ContentPart, ModelMessage } from '../message.js';
1
2
  import { ILogger } from '@microsoft/teams.common';
2
3
  import { Function, FunctionHandler } from '../function.js';
3
4
  import { IMemory } from '../memory.js';
4
- import { Message, ContentPart, ModelMessage } from '../message.js';
5
- import { IChatModel, TextChunkHandler } from '../models/chat.js';
5
+ import { IChatModel } from '../models/chat.js';
6
6
  import { Schema } from '../schema.js';
7
7
  import { ITemplate } from '../template.js';
8
- import { PromiseOrValue } from '../utils/types.js';
9
- import { IAiPlugin } from './plugin.js';
8
+ import { ChatPromptPlugin, IChatPrompt, ChatPromptOptions, ChatPromptSendOptions } from './chat-types.js';
10
9
  import '../citation.js';
10
+ import '../utils/types.js';
11
+ import './plugin.js';
11
12
 
12
- type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any>> = {
13
- /**
14
- * the name of the prompt
15
- */
16
- readonly name?: string;
17
- /**
18
- * the description of the prompt
19
- */
20
- readonly description?: string;
21
- /**
22
- * the model to send messages to
23
- */
24
- readonly model: IChatModel<TOptions>;
25
- /**
26
- * the defining characteristics/objective
27
- * of the prompt. This is commonly used to provide a system prompt.
28
- * If you supply the system prompt as part of the messages,
29
- * you do not need to supply this option.
30
- */
31
- readonly instructions?: string | string[] | ITemplate;
32
- /**
33
- * the `role` of the initial message
34
- */
35
- readonly role?: 'system' | 'user';
36
- /**
37
- * the conversation history
38
- */
39
- readonly messages?: Message[] | IMemory;
40
- /**
41
- * Logger instance to use for logging
42
- * If not provided, a ConsoleLogger will be used
43
- */
44
- logger?: ILogger;
45
- };
46
- type ChatPromptSendOptions<TOptions extends Record<string, any> = Record<string, any>> = {
47
- /**
48
- * the conversation history
49
- */
50
- readonly messages?: Message[] | IMemory;
51
- /**
52
- * the models request options
53
- */
54
- readonly request?: TOptions;
55
- /**
56
- * the callback to be called for each
57
- * stream chunk
58
- */
59
- readonly onChunk?: TextChunkHandler;
60
- };
61
- /**
62
- * a prompt that can interface with a
63
- * chat model that provides utility like
64
- * streaming and function calling
65
- */
66
- interface IChatPrompt<TOptions extends Record<string, any> = Record<string, any>, TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = []> {
67
- /**
68
- * the prompt name
69
- */
70
- readonly name: string;
71
- /**
72
- * the prompt description
73
- */
74
- readonly description: string;
75
- /**
76
- * the chat history
77
- */
78
- readonly messages: IMemory;
79
- /**
80
- * the registered functions
81
- */
82
- readonly functions: Array<Function>;
83
- /**
84
- * the chat model
85
- */
86
- plugins: TChatPromptPlugins;
87
- /**
88
- * add another chat prompt as a
89
- */
90
- use(prompt: IChatPrompt): this;
91
- use(name: string, prompt: IChatPrompt): this;
92
- /**
93
- * add a function that can be called
94
- * by the model
95
- */
96
- function(name: string, description: string, handler: FunctionHandler): this;
97
- function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;
98
- usePlugin<TPluginName extends TChatPromptPlugins[number]['name']>(name: TPluginName, args: Extract<TChatPromptPlugins[number], {
99
- name: TPluginName;
100
- }>['onUsePlugin'] extends ((args: infer U) => void) | undefined ? U : never): this;
101
- /**
102
- * call a function
103
- */
104
- call<A extends Record<string, any>, R = any>(name: string, args?: A): Promise<R>;
105
- /**
106
- * send a message to the model and get a response
107
- */
108
- send(input: string | ContentPart[], options?: ChatPromptSendOptions<TOptions>): Promise<Pick<ModelMessage, 'content'> & Omit<ModelMessage, 'content'>>;
109
- }
110
- type ChatPromptPlugin<TPluginName extends string, TPluginUseArgs extends {}> = IAiPlugin<TPluginName, TPluginUseArgs, Parameters<IChatPrompt['send']>[0], ReturnType<IChatPrompt['send']>> & {
111
- /**
112
- * Optionally passed in to modify the functions array that
113
- * is passed to the model
114
- * @param functions
115
- * @returns Functions
116
- */
117
- onBuildFunctions?: (functions: Function[]) => PromiseOrValue<Function[]>;
118
- /**
119
- * Optionally passed in to modify the system prompt before it is sent to the model.
120
- * @param systemPrompt The system prompt string (or undefined)
121
- * @returns The modified system prompt string (or undefined)
122
- */
123
- onBuildPrompt?: (systemPrompt: string | undefined) => PromiseOrValue<string | undefined>;
124
- };
125
13
  /**
126
14
  * a prompt that can interface with a
127
15
  * chat model that provides utility like
@@ -157,4 +45,4 @@ declare class ChatPrompt<TOptions extends Record<string, any> = Record<string, a
157
45
  protected executeFunction<R = any>(name: string, fn: Function, args?: Record<string, any>): Promise<R>;
158
46
  }
159
47
 
160
- export { ChatPrompt, type ChatPromptOptions, type ChatPromptPlugin, type ChatPromptSendOptions, type IChatPrompt };
48
+ export { ChatPrompt };
@@ -184,7 +184,8 @@ class ChatPrompt {
184
184
  } catch (err) {
185
185
  return;
186
186
  }
187
- }
187
+ },
188
+ autoFunctionCalling: options.autoFunctionCalling
188
189
  }
189
190
  );
190
191
  let output = {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/prompts/chat.ts"],"names":["StringTemplate","LocalMemory","ConsoleLogger"],"mappings":";;;;;;AAwKO,MAAM,UAG0C,CAAA;AAAA,EACrD,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,EACA,IAAA;AAAA,EAEnB,WAAA,CAAY,SAAsC,OAA8B,EAAA;AAC9E,IAAK,IAAA,CAAA,KAAA,GAAQ,QAAQ,IAAQ,IAAA,MAAA;AAC7B,IAAK,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C1C;AAGF,IAAO,OAAA,MAAA;AAAA;AACT,EAEA,MAAgB,eAAA,CACd,IACA,EAAA,EAAA,EACA,IACY,EAAA;AACZ,IAAM,MAAA,aAAA,GAAgB,QAAQ,EAAC;AAG/B,IAAW,KAAA,MAAA,MAAA,IAAU,KAAK,OAAS,EAAA;AACjC,MAAA,IAAI,OAAO,oBAAsB,EAAA;AAC/B,QAAM,MAAA,MAAA,CAAO,oBAAqB,CAAA,IAAA,EAAM,aAAa,CAAA;AAAA;AACvD;AAIF,IAAA,IAAI,MAAS,GAAA,MAAM,EAAG,CAAA,OAAA,CAAQ,aAAa,CAAA;AAG3C,IAAW,KAAA,MAAA,MAAA,IAAU,KAAK,OAAS,EAAA;AACjC,MAAA,IAAI,OAAO,mBAAqB,EAAA;AAC9B,QAAA,MAAA,GAAS,MAAM,MAAA,CAAO,mBAAoB,CAAA,IAAA,EAAM,eAAe,MAAM,CAAA;AAAA;AACvE;AAGF,IAAO,OAAA,MAAA;AAAA;AAEX","file":"chat.js","sourcesContent":["import { ConsoleLogger, ILogger } from '@microsoft/teams.common';\n\nimport { 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 { PromiseOrValue, WithRequired } from '../utils/types';\n\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 /**\n * Logger instance to use for logging\n * If not provided, a ConsoleLogger will be used\n */\n logger?: ILogger;\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 * 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[]) => PromiseOrValue<Function[]>;\n /**\n * Optionally passed in to modify the system prompt before it is sent to the model.\n * @param systemPrompt The system prompt string (or undefined)\n * @returns The modified system prompt string (or undefined)\n */\n onBuildPrompt?: (systemPrompt: string | undefined) => PromiseOrValue<string | undefined>;\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 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 protected readonly _log: ILogger;\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 this._log = options.logger || new ConsoleLogger(`@microsoft/teams.ai/prompts/${this._name}`);\n }\n\n use(prompt: IChatPrompt): this;\n use(name: string, prompt: IChatPrompt): this;\n use(...args: any[]) {\n const prompt: IChatPrompt = 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 this._log.debug(`Plugin \"${name}\" not found`);\n throw new Error(`Plugin \"${name}\" not found`);\n }\n\n if (plugin.onUsePlugin) {\n this._log.debug(`Using plugin \"${name}\" with args:`, args);\n plugin.onUsePlugin(args);\n this._log.debug(`Successfully initialized plugin \"${name}\"`);\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 if (!fn) {\n throw new Error(`function \"${name}\" not found`);\n }\n return this.executeFunction(name, fn, args);\n }\n\n async send(input: string | ContentPart[], options: ChatPromptSendOptions<TOptions> = {}) {\n this._log.debug(`Processing plugins before send (${this.plugins.length} plugins found)`);\n for (const plugin of this.plugins) {\n if (plugin.onBeforeSend) {\n this._log.debug(`Running onBeforeSend for plugin \"${plugin.name}\"`);\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 let prompt = await this._template.render();\n\n for (const plugin of this.plugins) {\n if (plugin.onBuildPrompt) {\n const nextPrompt = await plugin.onBuildPrompt(prompt);\n if (nextPrompt != null && nextPrompt !== prompt) {\n this._log.debug(`Plugin \"${plugin.name}\" modified the system prompt`);\n prompt = nextPrompt;\n }\n }\n }\n\n if (prompt) {\n system = {\n role: this._role,\n content: prompt,\n };\n this._log.debug('System instructions for LLM:', prompt);\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((acc, fn) => {\n acc[fn.name] = {\n ...fn,\n handler: (args: any) => this.executeFunction(fn.name, fn, args),\n };\n return acc;\n }, {} as Record<string, Function>);\n\n if (Object.keys(fnMap).length > 0) {\n this._log.debug(\n 'Available functions for LLM:',\n Object.keys(fnMap).map((name) => {\n const fn = fnMap[name];\n const paramDescriptions =\n 'properties' in fn.parameters && fn.parameters.properties\n ? Object.entries(\n fn.parameters.properties as Record<string, { description?: string }>\n ).reduce(\n (acc, [key, prop]) => ({\n ...acc,\n [key]: prop.description,\n }),\n {} as Record<string, string | undefined>\n )\n : {};\n\n return {\n name,\n description: fn.description,\n parameters: {\n schema: fn.parameters,\n descriptions: paramDescriptions,\n },\n };\n })\n );\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\n // Log function calls if present\n if (output.function_calls && output.function_calls.length > 0) {\n this._log.debug(\n 'LLM requested function calls:',\n output.function_calls.map((call) => ({\n name: call.name,\n id: call.id,\n arguments: call.arguments,\n }))\n );\n }\n\n this._log.debug(`Processing plugins after send (${this.plugins.length} plugins found)`);\n for (const plugin of this.plugins) {\n if (plugin.onAfterSend) {\n this._log.debug(`Running onAfterSend for plugin \"${plugin.name}\"`);\n output = await plugin.onAfterSend(output);\n }\n }\n\n return output;\n }\n\n protected async executeFunction<R = any>(\n name: string,\n fn: Function,\n args?: Record<string, any>\n ): Promise<R> {\n const processedArgs = args || {};\n\n // Execute beforeFunctionCall hooks\n for (const plugin of this.plugins) {\n if (plugin.onBeforeFunctionCall) {\n await plugin.onBeforeFunctionCall(name, processedArgs);\n }\n }\n\n // Call the function\n let result = await fn.handler(processedArgs);\n\n // Execute afterFunctionCall hooks\n for (const plugin of this.plugins) {\n if (plugin.onAfterFunctionCall) {\n result = await plugin.onAfterFunctionCall(name, processedArgs, result);\n }\n }\n\n return result;\n }\n}\n"]}
1
+ {"version":3,"sources":["../../src/prompts/chat.ts"],"names":["StringTemplate","LocalMemory","ConsoleLogger"],"mappings":";;;;;;AAmBO,MAAM,UAG0C,CAAA;AAAA,EACrD,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;A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,+BAAA,EAAkC,IAAK,CAAA,OAAA,CAAQ,MAAM,CAAiB,eAAA,CAAA,CAAA;AACtF,IAAW,KAAA,MAAA,MAAA,IAAU,KAAK,OAAS,EAAA;AACjC,MAAA,IAAI,OAAO,WAAa,EAAA;AACtB,QAAA,IAAA,CAAK,IAAK,CAAA,KAAA,CAAM,CAAmC,gCAAA,EAAA,MAAA,CAAO,IAAI,CAAG,CAAA,CAAA,CAAA;AACjE,QAAS,MAAA,GAAA,MAAM,MAAO,CAAA,WAAA,CAAY,MAAM,CAAA;AAAA;AAC1C;AAGF,IAAO,OAAA,MAAA;AAAA;AACT,EAEA,MAAgB,eAAA,CACd,IACA,EAAA,EAAA,EACA,IACY,EAAA;AACZ,IAAM,MAAA,aAAA,GAAgB,QAAQ,EAAC;AAG/B,IAAW,KAAA,MAAA,MAAA,IAAU,KAAK,OAAS,EAAA;AACjC,MAAA,IAAI,OAAO,oBAAsB,EAAA;AAC/B,QAAM,MAAA,MAAA,CAAO,oBAAqB,CAAA,IAAA,EAAM,aAAa,CAAA;AAAA;AACvD;AAIF,IAAA,IAAI,MAAS,GAAA,MAAM,EAAG,CAAA,OAAA,CAAQ,aAAa,CAAA;AAG3C,IAAW,KAAA,MAAA,MAAA,IAAU,KAAK,OAAS,EAAA;AACjC,MAAA,IAAI,OAAO,mBAAqB,EAAA;AAC9B,QAAA,MAAA,GAAS,MAAM,MAAA,CAAO,mBAAoB,CAAA,IAAA,EAAM,eAAe,MAAM,CAAA;AAAA;AACvE;AAGF,IAAO,OAAA,MAAA;AAAA;AAEX","file":"chat.js","sourcesContent":["import { ConsoleLogger, ILogger } from '@microsoft/teams.common';\n\nimport { Function, FunctionHandler } from '../function';\nimport { LocalMemory } from '../local-memory';\nimport { IMemory } from '../memory';\nimport { ContentPart, SystemMessage, UserMessage } from '../message';\nimport { IChatModel } from '../models';\nimport { Schema } from '../schema';\nimport { ITemplate } from '../template';\nimport { StringTemplate } from '../templates';\nimport { WithRequired } from '../utils/types';\n\nimport { ChatPromptOptions, ChatPromptPlugin, ChatPromptSendOptions, IChatPrompt } from './chat-types';\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 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 protected readonly _log: ILogger;\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 this._log = options.logger || new ConsoleLogger(`@microsoft/teams.ai/prompts/${this._name}`);\n }\n\n use(prompt: IChatPrompt): this;\n use(name: string, prompt: IChatPrompt): this;\n use(...args: any[]) {\n const prompt: IChatPrompt = 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 this._log.debug(`Plugin \"${name}\" not found`);\n throw new Error(`Plugin \"${name}\" not found`);\n }\n\n if (plugin.onUsePlugin) {\n this._log.debug(`Using plugin \"${name}\" with args:`, args);\n plugin.onUsePlugin(args);\n this._log.debug(`Successfully initialized plugin \"${name}\"`);\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 if (!fn) {\n throw new Error(`function \"${name}\" not found`);\n }\n return this.executeFunction(name, fn, args);\n }\n\n async send(input: string | ContentPart[], options: ChatPromptSendOptions<TOptions> = {}) {\n this._log.debug(`Processing plugins before send (${this.plugins.length} plugins found)`);\n for (const plugin of this.plugins) {\n if (plugin.onBeforeSend) {\n this._log.debug(`Running onBeforeSend for plugin \"${plugin.name}\"`);\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 let prompt = await this._template.render();\n\n for (const plugin of this.plugins) {\n if (plugin.onBuildPrompt) {\n const nextPrompt = await plugin.onBuildPrompt(prompt);\n if (nextPrompt != null && nextPrompt !== prompt) {\n this._log.debug(`Plugin \"${plugin.name}\" modified the system prompt`);\n prompt = nextPrompt;\n }\n }\n }\n\n if (prompt) {\n system = {\n role: this._role,\n content: prompt,\n };\n this._log.debug('System instructions for LLM:', prompt);\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((acc, fn) => {\n acc[fn.name] = {\n ...fn,\n handler: (args: any) => this.executeFunction(fn.name, fn, args),\n };\n return acc;\n }, {} as Record<string, Function>);\n\n if (Object.keys(fnMap).length > 0) {\n this._log.debug(\n 'Available functions for LLM:',\n Object.keys(fnMap).map((name) => {\n const fn = fnMap[name];\n const paramDescriptions =\n 'properties' in fn.parameters && fn.parameters.properties\n ? Object.entries(\n fn.parameters.properties as Record<string, { description?: string }>\n ).reduce(\n (acc, [key, prop]) => ({\n ...acc,\n [key]: prop.description,\n }),\n {} as Record<string, string | undefined>\n )\n : {};\n\n return {\n name,\n description: fn.description,\n parameters: {\n schema: fn.parameters,\n descriptions: paramDescriptions,\n },\n };\n })\n );\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 autoFunctionCalling: options.autoFunctionCalling,\n }\n );\n\n let output: Awaited<ReturnType<typeof this._model.send>> = {\n ...res,\n content: res.content || '',\n };\n\n // Log function calls if present\n if (output.function_calls && output.function_calls.length > 0) {\n this._log.debug(\n 'LLM requested function calls:',\n output.function_calls.map((call) => ({\n name: call.name,\n id: call.id,\n arguments: call.arguments,\n }))\n );\n }\n\n this._log.debug(`Processing plugins after send (${this.plugins.length} plugins found)`);\n for (const plugin of this.plugins) {\n if (plugin.onAfterSend) {\n this._log.debug(`Running onAfterSend for plugin \"${plugin.name}\"`);\n output = await plugin.onAfterSend(output);\n }\n }\n\n return output;\n }\n\n protected async executeFunction<R = any>(\n name: string,\n fn: Function,\n args?: Record<string, any>\n ): Promise<R> {\n const processedArgs = args || {};\n\n // Execute beforeFunctionCall hooks\n for (const plugin of this.plugins) {\n if (plugin.onBeforeFunctionCall) {\n await plugin.onBeforeFunctionCall(name, processedArgs);\n }\n }\n\n // Call the function\n let result = await fn.handler(processedArgs);\n\n // Execute afterFunctionCall hooks\n for (const plugin of this.plugins) {\n if (plugin.onAfterFunctionCall) {\n result = await plugin.onAfterFunctionCall(name, processedArgs, result);\n }\n }\n\n return result;\n }\n}\n"]}
@@ -182,7 +182,8 @@ class ChatPrompt {
182
182
  } catch (err) {
183
183
  return;
184
184
  }
185
- }
185
+ },
186
+ autoFunctionCalling: options.autoFunctionCalling
186
187
  }
187
188
  );
188
189
  let output = {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/prompts/chat.ts"],"names":[],"mappings":";;;;AAwKO,MAAM,UAG0C,CAAA;AAAA,EACrD,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,QA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AACtB,QAAA,IAAA,CAAK,IAAK,CAAA,KAAA,CAAM,CAAmC,gCAAA,EAAA,MAAA,CAAO,IAAI,CAAG,CAAA,CAAA,CAAA;AACjE,QAAS,MAAA,GAAA,MAAM,MAAO,CAAA,WAAA,CAAY,MAAM,CAAA;AAAA;AAC1C;AAGF,IAAO,OAAA,MAAA;AAAA;AACT,EAEA,MAAgB,eAAA,CACd,IACA,EAAA,EAAA,EACA,IACY,EAAA;AACZ,IAAM,MAAA,aAAA,GAAgB,QAAQ,EAAC;AAG/B,IAAW,KAAA,MAAA,MAAA,IAAU,KAAK,OAAS,EAAA;AACjC,MAAA,IAAI,OAAO,oBAAsB,EAAA;AAC/B,QAAM,MAAA,MAAA,CAAO,oBAAqB,CAAA,IAAA,EAAM,aAAa,CAAA;AAAA;AACvD;AAIF,IAAA,IAAI,MAAS,GAAA,MAAM,EAAG,CAAA,OAAA,CAAQ,aAAa,CAAA;AAG3C,IAAW,KAAA,MAAA,MAAA,IAAU,KAAK,OAAS,EAAA;AACjC,MAAA,IAAI,OAAO,mBAAqB,EAAA;AAC9B,QAAA,MAAA,GAAS,MAAM,MAAA,CAAO,mBAAoB,CAAA,IAAA,EAAM,eAAe,MAAM,CAAA;AAAA;AACvE;AAGF,IAAO,OAAA,MAAA;AAAA;AAEX","file":"chat.mjs","sourcesContent":["import { ConsoleLogger, ILogger } from '@microsoft/teams.common';\n\nimport { 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 { PromiseOrValue, WithRequired } from '../utils/types';\n\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 /**\n * Logger instance to use for logging\n * If not provided, a ConsoleLogger will be used\n */\n logger?: ILogger;\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 * 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[]) => PromiseOrValue<Function[]>;\n /**\n * Optionally passed in to modify the system prompt before it is sent to the model.\n * @param systemPrompt The system prompt string (or undefined)\n * @returns The modified system prompt string (or undefined)\n */\n onBuildPrompt?: (systemPrompt: string | undefined) => PromiseOrValue<string | undefined>;\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 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 protected readonly _log: ILogger;\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 this._log = options.logger || new ConsoleLogger(`@microsoft/teams.ai/prompts/${this._name}`);\n }\n\n use(prompt: IChatPrompt): this;\n use(name: string, prompt: IChatPrompt): this;\n use(...args: any[]) {\n const prompt: IChatPrompt = 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 this._log.debug(`Plugin \"${name}\" not found`);\n throw new Error(`Plugin \"${name}\" not found`);\n }\n\n if (plugin.onUsePlugin) {\n this._log.debug(`Using plugin \"${name}\" with args:`, args);\n plugin.onUsePlugin(args);\n this._log.debug(`Successfully initialized plugin \"${name}\"`);\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 if (!fn) {\n throw new Error(`function \"${name}\" not found`);\n }\n return this.executeFunction(name, fn, args);\n }\n\n async send(input: string | ContentPart[], options: ChatPromptSendOptions<TOptions> = {}) {\n this._log.debug(`Processing plugins before send (${this.plugins.length} plugins found)`);\n for (const plugin of this.plugins) {\n if (plugin.onBeforeSend) {\n this._log.debug(`Running onBeforeSend for plugin \"${plugin.name}\"`);\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 let prompt = await this._template.render();\n\n for (const plugin of this.plugins) {\n if (plugin.onBuildPrompt) {\n const nextPrompt = await plugin.onBuildPrompt(prompt);\n if (nextPrompt != null && nextPrompt !== prompt) {\n this._log.debug(`Plugin \"${plugin.name}\" modified the system prompt`);\n prompt = nextPrompt;\n }\n }\n }\n\n if (prompt) {\n system = {\n role: this._role,\n content: prompt,\n };\n this._log.debug('System instructions for LLM:', prompt);\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((acc, fn) => {\n acc[fn.name] = {\n ...fn,\n handler: (args: any) => this.executeFunction(fn.name, fn, args),\n };\n return acc;\n }, {} as Record<string, Function>);\n\n if (Object.keys(fnMap).length > 0) {\n this._log.debug(\n 'Available functions for LLM:',\n Object.keys(fnMap).map((name) => {\n const fn = fnMap[name];\n const paramDescriptions =\n 'properties' in fn.parameters && fn.parameters.properties\n ? Object.entries(\n fn.parameters.properties as Record<string, { description?: string }>\n ).reduce(\n (acc, [key, prop]) => ({\n ...acc,\n [key]: prop.description,\n }),\n {} as Record<string, string | undefined>\n )\n : {};\n\n return {\n name,\n description: fn.description,\n parameters: {\n schema: fn.parameters,\n descriptions: paramDescriptions,\n },\n };\n })\n );\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\n // Log function calls if present\n if (output.function_calls && output.function_calls.length > 0) {\n this._log.debug(\n 'LLM requested function calls:',\n output.function_calls.map((call) => ({\n name: call.name,\n id: call.id,\n arguments: call.arguments,\n }))\n );\n }\n\n this._log.debug(`Processing plugins after send (${this.plugins.length} plugins found)`);\n for (const plugin of this.plugins) {\n if (plugin.onAfterSend) {\n this._log.debug(`Running onAfterSend for plugin \"${plugin.name}\"`);\n output = await plugin.onAfterSend(output);\n }\n }\n\n return output;\n }\n\n protected async executeFunction<R = any>(\n name: string,\n fn: Function,\n args?: Record<string, any>\n ): Promise<R> {\n const processedArgs = args || {};\n\n // Execute beforeFunctionCall hooks\n for (const plugin of this.plugins) {\n if (plugin.onBeforeFunctionCall) {\n await plugin.onBeforeFunctionCall(name, processedArgs);\n }\n }\n\n // Call the function\n let result = await fn.handler(processedArgs);\n\n // Execute afterFunctionCall hooks\n for (const plugin of this.plugins) {\n if (plugin.onAfterFunctionCall) {\n result = await plugin.onAfterFunctionCall(name, processedArgs, result);\n }\n }\n\n return result;\n }\n}\n"]}
1
+ {"version":3,"sources":["../../src/prompts/chat.ts"],"names":[],"mappings":";;;;AAmBO,MAAM,UAG0C,CAAA;AAAA,EACrD,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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AAAA,UACX,IAAI,IAAK,CAAA,EAAA;AAAA,UACT,WAAW,IAAK,CAAA;AAAA,SAChB,CAAA;AAAA,OACJ;AAAA;AAGF,IAAA,IAAA,CAAK,KAAK,KAAM,CAAA,CAAA,+BAAA,EAAkC,IAAK,CAAA,OAAA,CAAQ,MAAM,CAAiB,eAAA,CAAA,CAAA;AACtF,IAAW,KAAA,MAAA,MAAA,IAAU,KAAK,OAAS,EAAA;AACjC,MAAA,IAAI,OAAO,WAAa,EAAA;AACtB,QAAA,IAAA,CAAK,IAAK,CAAA,KAAA,CAAM,CAAmC,gCAAA,EAAA,MAAA,CAAO,IAAI,CAAG,CAAA,CAAA,CAAA;AACjE,QAAS,MAAA,GAAA,MAAM,MAAO,CAAA,WAAA,CAAY,MAAM,CAAA;AAAA;AAC1C;AAGF,IAAO,OAAA,MAAA;AAAA;AACT,EAEA,MAAgB,eAAA,CACd,IACA,EAAA,EAAA,EACA,IACY,EAAA;AACZ,IAAM,MAAA,aAAA,GAAgB,QAAQ,EAAC;AAG/B,IAAW,KAAA,MAAA,MAAA,IAAU,KAAK,OAAS,EAAA;AACjC,MAAA,IAAI,OAAO,oBAAsB,EAAA;AAC/B,QAAM,MAAA,MAAA,CAAO,oBAAqB,CAAA,IAAA,EAAM,aAAa,CAAA;AAAA;AACvD;AAIF,IAAA,IAAI,MAAS,GAAA,MAAM,EAAG,CAAA,OAAA,CAAQ,aAAa,CAAA;AAG3C,IAAW,KAAA,MAAA,MAAA,IAAU,KAAK,OAAS,EAAA;AACjC,MAAA,IAAI,OAAO,mBAAqB,EAAA;AAC9B,QAAA,MAAA,GAAS,MAAM,MAAA,CAAO,mBAAoB,CAAA,IAAA,EAAM,eAAe,MAAM,CAAA;AAAA;AACvE;AAGF,IAAO,OAAA,MAAA;AAAA;AAEX","file":"chat.mjs","sourcesContent":["import { ConsoleLogger, ILogger } from '@microsoft/teams.common';\n\nimport { Function, FunctionHandler } from '../function';\nimport { LocalMemory } from '../local-memory';\nimport { IMemory } from '../memory';\nimport { ContentPart, SystemMessage, UserMessage } from '../message';\nimport { IChatModel } from '../models';\nimport { Schema } from '../schema';\nimport { ITemplate } from '../template';\nimport { StringTemplate } from '../templates';\nimport { WithRequired } from '../utils/types';\n\nimport { ChatPromptOptions, ChatPromptPlugin, ChatPromptSendOptions, IChatPrompt } from './chat-types';\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 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 protected readonly _log: ILogger;\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 this._log = options.logger || new ConsoleLogger(`@microsoft/teams.ai/prompts/${this._name}`);\n }\n\n use(prompt: IChatPrompt): this;\n use(name: string, prompt: IChatPrompt): this;\n use(...args: any[]) {\n const prompt: IChatPrompt = 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 this._log.debug(`Plugin \"${name}\" not found`);\n throw new Error(`Plugin \"${name}\" not found`);\n }\n\n if (plugin.onUsePlugin) {\n this._log.debug(`Using plugin \"${name}\" with args:`, args);\n plugin.onUsePlugin(args);\n this._log.debug(`Successfully initialized plugin \"${name}\"`);\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 if (!fn) {\n throw new Error(`function \"${name}\" not found`);\n }\n return this.executeFunction(name, fn, args);\n }\n\n async send(input: string | ContentPart[], options: ChatPromptSendOptions<TOptions> = {}) {\n this._log.debug(`Processing plugins before send (${this.plugins.length} plugins found)`);\n for (const plugin of this.plugins) {\n if (plugin.onBeforeSend) {\n this._log.debug(`Running onBeforeSend for plugin \"${plugin.name}\"`);\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 let prompt = await this._template.render();\n\n for (const plugin of this.plugins) {\n if (plugin.onBuildPrompt) {\n const nextPrompt = await plugin.onBuildPrompt(prompt);\n if (nextPrompt != null && nextPrompt !== prompt) {\n this._log.debug(`Plugin \"${plugin.name}\" modified the system prompt`);\n prompt = nextPrompt;\n }\n }\n }\n\n if (prompt) {\n system = {\n role: this._role,\n content: prompt,\n };\n this._log.debug('System instructions for LLM:', prompt);\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((acc, fn) => {\n acc[fn.name] = {\n ...fn,\n handler: (args: any) => this.executeFunction(fn.name, fn, args),\n };\n return acc;\n }, {} as Record<string, Function>);\n\n if (Object.keys(fnMap).length > 0) {\n this._log.debug(\n 'Available functions for LLM:',\n Object.keys(fnMap).map((name) => {\n const fn = fnMap[name];\n const paramDescriptions =\n 'properties' in fn.parameters && fn.parameters.properties\n ? Object.entries(\n fn.parameters.properties as Record<string, { description?: string }>\n ).reduce(\n (acc, [key, prop]) => ({\n ...acc,\n [key]: prop.description,\n }),\n {} as Record<string, string | undefined>\n )\n : {};\n\n return {\n name,\n description: fn.description,\n parameters: {\n schema: fn.parameters,\n descriptions: paramDescriptions,\n },\n };\n })\n );\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 autoFunctionCalling: options.autoFunctionCalling,\n }\n );\n\n let output: Awaited<ReturnType<typeof this._model.send>> = {\n ...res,\n content: res.content || '',\n };\n\n // Log function calls if present\n if (output.function_calls && output.function_calls.length > 0) {\n this._log.debug(\n 'LLM requested function calls:',\n output.function_calls.map((call) => ({\n name: call.name,\n id: call.id,\n arguments: call.arguments,\n }))\n );\n }\n\n this._log.debug(`Processing plugins after send (${this.plugins.length} plugins found)`);\n for (const plugin of this.plugins) {\n if (plugin.onAfterSend) {\n this._log.debug(`Running onAfterSend for plugin \"${plugin.name}\"`);\n output = await plugin.onAfterSend(output);\n }\n }\n\n return output;\n }\n\n protected async executeFunction<R = any>(\n name: string,\n fn: Function,\n args?: Record<string, any>\n ): Promise<R> {\n const processedArgs = args || {};\n\n // Execute beforeFunctionCall hooks\n for (const plugin of this.plugins) {\n if (plugin.onBeforeFunctionCall) {\n await plugin.onBeforeFunctionCall(name, processedArgs);\n }\n }\n\n // Call the function\n let result = await fn.handler(processedArgs);\n\n // Execute afterFunctionCall hooks\n for (const plugin of this.plugins) {\n if (plugin.onAfterFunctionCall) {\n result = await plugin.onAfterFunctionCall(name, processedArgs, result);\n }\n }\n\n return result;\n }\n}\n"]}
@@ -1,7 +1,8 @@
1
1
  import { IAudioPrompt } from './audio.mjs';
2
2
  export { AudioPrompt, AudioPromptOptions } from './audio.mjs';
3
- import { IChatPrompt } from './chat.mjs';
4
- export { ChatPrompt, ChatPromptOptions, ChatPromptPlugin, ChatPromptSendOptions } from './chat.mjs';
3
+ import { IChatPrompt } from './chat-types.mjs';
4
+ export { ChatPromptOptions, ChatPromptPlugin, ChatPromptSendOptions } from './chat-types.mjs';
5
+ export { ChatPrompt } from './chat.mjs';
5
6
  import '../models/audio.mjs';
6
7
  import '@microsoft/teams.common';
7
8
  import '../function.mjs';
@@ -1,7 +1,8 @@
1
1
  import { IAudioPrompt } from './audio.js';
2
2
  export { AudioPrompt, AudioPromptOptions } from './audio.js';
3
- import { IChatPrompt } from './chat.js';
4
- export { ChatPrompt, ChatPromptOptions, ChatPromptPlugin, ChatPromptSendOptions } from './chat.js';
3
+ import { IChatPrompt } from './chat-types.js';
4
+ export { ChatPromptOptions, ChatPromptPlugin, ChatPromptSendOptions } from './chat-types.js';
5
+ export { ChatPrompt } from './chat.js';
5
6
  import '../models/audio.js';
6
7
  import '@microsoft/teams.common';
7
8
  import '../function.js';
@@ -1,20 +1,27 @@
1
1
  'use strict';
2
2
 
3
- var chat = require('./chat');
4
3
  var audio = require('./audio');
4
+ var chat = require('./chat');
5
+ var chatTypes = require('./chat-types');
5
6
 
6
7
 
7
8
 
9
+ Object.keys(audio).forEach(function (k) {
10
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
11
+ enumerable: true,
12
+ get: function () { return audio[k]; }
13
+ });
14
+ });
8
15
  Object.keys(chat).forEach(function (k) {
9
16
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
10
17
  enumerable: true,
11
18
  get: function () { return chat[k]; }
12
19
  });
13
20
  });
14
- Object.keys(audio).forEach(function (k) {
21
+ Object.keys(chatTypes).forEach(function (k) {
15
22
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
16
23
  enumerable: true,
17
- get: function () { return audio[k]; }
24
+ get: function () { return chatTypes[k]; }
18
25
  });
19
26
  });
20
27
  //# sourceMappingURL=index.js.map
@@ -1,4 +1,5 @@
1
- export * from './chat';
2
1
  export * from './audio';
2
+ export * from './chat';
3
+ export * from './chat-types';
3
4
  //# sourceMappingURL=index.mjs.map
4
5
  //# sourceMappingURL=index.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@microsoft/teams.ai",
3
- "version": "2.0.0-preview.5",
3
+ "version": "2.0.0-preview.7",
4
4
  "license": "MIT",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -34,10 +34,10 @@
34
34
  "test": "npx jest"
35
35
  },
36
36
  "peerDependencies": {
37
- "@microsoft/teams.common": "2.0.0-preview.5"
37
+ "@microsoft/teams.common": "2.0.0-preview.7"
38
38
  },
39
39
  "devDependencies": {
40
- "@microsoft/teams.config": "2.0.0-preview.5",
40
+ "@microsoft/teams.config": "2.0.0-preview.7",
41
41
  "@types/jest": "^29.5.12",
42
42
  "@types/node": "^22.0.2",
43
43
  "jest": "^29.7.0",