Skip to content

Commit

Permalink
Remove magic boolean value (e.g. messages: true and context: true)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkpiano committed Jun 29, 2024
1 parent 094bb8a commit 13ec4ca
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 26 deletions.
12 changes: 6 additions & 6 deletions examples/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ const machine = setup({
invoke: [
{
src: 'summarizer',
input: {
context: true,
input: (x) => ({
context: x.context,
prompt:
'Summarize the patient visit in a single sentence. The summary should be in English.',
},
}),
onDone: {
actions: assign({
englishSummary: ({ event }) => event.output.text,
Expand All @@ -45,11 +45,11 @@ const machine = setup({
},
{
src: 'summarizer',
input: {
context: true,
input: (x) => ({
context: x.context,
prompt:
'Summarize the patient visit in a single sentence. The summary should be in Spanish.',
},
}),
onDone: {
actions: assign({
spanishSummary: ({ event }) => event.output.text,
Expand Down
2 changes: 1 addition & 1 deletion examples/wiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function main() {
console.log(response1.text);

const response2 = await agent.generateText({
messages: true,
messages: (x) => x.select((ctx) => ctx.messages),
prompt: 'What about the first one?',
});

Expand Down
25 changes: 7 additions & 18 deletions src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ async function getMessages(
options: AgentStreamTextOptions
): Promise<CoreMessage[]> {
let messages: CoreMessage[] = [];
if (options.messages === true) {
messages = agent.select((s) => s.messages);
} else if (typeof options.messages === 'function') {
if (typeof options.messages === 'function') {
messages = await options.messages(agent);
} else if (options.messages) {
messages = options.messages;
Expand Down Expand Up @@ -164,15 +162,10 @@ export function fromTextStream<T extends Agent<any>>(
): ObservableActorLogic<
{ textDelta: string },
Omit<AgentStreamTextOptions, 'context'> & {
context?: AgentStreamTextOptions['context'] | boolean;
context?: AgentStreamTextOptions['context'];
}
> {
return fromObservable(({ input, self }) => {
const context =
input.context === true
? (self._parent?.getSnapshot() as AnyMachineSnapshot).context
: input.context;

return fromObservable(({ input }) => {
const observers = new Set<Observer<{ textDelta: string }>>();

// TODO: check if messages was provided instead
Expand All @@ -181,7 +174,7 @@ export function fromTextStream<T extends Agent<any>>(
const result = await agentStreamText(agent, {
...defaultOptions,
...input,
context,
context: input.context,
});

for await (const part of result.fullStream) {
Expand Down Expand Up @@ -214,18 +207,14 @@ export function fromText<T extends Agent<any>>(
): PromiseActorLogic<
GenerateTextResult<Record<string, CoreTool<any, any>>>,
Omit<AgentGenerateTextOptions, 'context'> & {
context?: AgentGenerateTextOptions['context'] | boolean;
context?: AgentGenerateTextOptions['context'];
}
> {
return fromPromise(async ({ input, self }) => {
const context =
input.context === true
? (self._parent?.getSnapshot() as AnyMachineSnapshot).context
: input.context;
return fromPromise(async ({ input }) => {
return await agentGenerateText(agent, {
...input,
...defaultOptions,
context,
context: input.context,
});
});
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export interface CommonTextOptions {
prompt: FromAgent<string>;
model?: LanguageModel;
context?: Record<string, any>;
messages?: FromAgent<CoreMessage[]> | true;
messages?: FromAgent<CoreMessage[]>;
template?: PromptTemplate<any>;
}

Expand Down

0 comments on commit 13ec4ca

Please sign in to comment.