Skip to content

Commit a9b807b

Browse files
committed
cleanup AiChat
1 parent 8e7edcf commit a9b807b

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

packages/ai/ai/src/AiChat.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
import * as Channel from "effect/Channel"
55
import * as Context from "effect/Context"
66
import * as Effect from "effect/Effect"
7+
import * as Layer from "effect/Layer"
78
import type { ParseError } from "effect/ParseResult"
89
import * as Ref from "effect/Ref"
910
import * as Schema from "effect/Schema"
1011
import * as Stream from "effect/Stream"
11-
import type { NoExcessProperties } from "effect/Types"
1212
import type { AiError } from "./AiError.js"
1313
import * as AiInput from "./AiInput.js"
1414
import * as AiLanguageModel from "./AiLanguageModel.js"
1515
import * as AiResponse from "./AiResponse.js"
16+
import type * as AiTool from "./AiTool.js"
1617

1718
/**
1819
* @since 1.0.0
@@ -56,9 +57,9 @@ export declare namespace AiChat {
5657
*
5758
* Both input and output messages will be added to the chat history.
5859
*/
59-
readonly generateText: <
60-
Options extends NoExcessProperties<Omit<AiLanguageModel.GenerateTextOptions<any>, "system">, Options>
61-
>(options: Options) => Effect.Effect<
60+
readonly generateText: <Tools extends AiTool.Any, Options>(
61+
options: Options & Omit<AiLanguageModel.GenerateTextOptions<Tools>, "system">
62+
) => Effect.Effect<
6263
AiLanguageModel.ExtractSuccess<Options>,
6364
AiLanguageModel.ExtractError<Options>,
6465
AiLanguageModel.ExtractContext<Options>
@@ -73,9 +74,9 @@ export declare namespace AiChat {
7374
*
7475
* Both input and output messages will be added to the chat history.
7576
*/
76-
readonly streamText: <
77-
Options extends NoExcessProperties<Omit<AiLanguageModel.GenerateTextOptions<any>, "system">, Options>
78-
>(options: Options) => Stream.Stream<
77+
readonly streamText: <Tools extends AiTool.Any, Options>(
78+
options: Options & Omit<AiLanguageModel.GenerateTextOptions<Tools>, "system">
79+
) => Stream.Stream<
7980
AiLanguageModel.ExtractSuccess<Options>,
8081
AiLanguageModel.ExtractError<Options>,
8182
AiLanguageModel.ExtractContext<Options>
@@ -103,7 +104,7 @@ export declare namespace AiChat {
103104

104105
/**
105106
* @since 1.0.0
106-
* @category constructors
107+
* @category Constructors
107108
*/
108109
export const fromPrompt = Effect.fnUntraced(function*(options: {
109110
readonly prompt: AiInput.Raw
@@ -139,7 +140,7 @@ export const fromPrompt = Effect.fnUntraced(function*(options: {
139140
)
140141
}),
141142
semaphore.withPermits(1),
142-
Effect.withSpan("AiChat.send", { captureStackTrace: false })
143+
Effect.withSpan("AiChat.generateText", { captureStackTrace: false })
143144
)
144145
},
145146
streamText(options) {
@@ -167,7 +168,7 @@ export const fromPrompt = Effect.fnUntraced(function*(options: {
167168
semaphore.release(1)
168169
)
169170
))
170-
}).pipe(Stream.withSpan("AiChat.stream", {
171+
}).pipe(Stream.withSpan("AiChat.streamText", {
171172
captureStackTrace: false
172173
})) as any
173174
},
@@ -190,7 +191,7 @@ export const fromPrompt = Effect.fnUntraced(function*(options: {
190191
)
191192
}),
192193
semaphore.withPermits(1),
193-
Effect.withSpan("AiChat.structured", {
194+
Effect.withSpan("AiChat.generateObject", {
194195
attributes: {
195196
toolCallId: "toolCallId" in options
196197
? options.toolCallId
@@ -207,15 +208,15 @@ export const fromPrompt = Effect.fnUntraced(function*(options: {
207208

208209
/**
209210
* @since 1.0.0
210-
* @category constructors
211+
* @category Constructors
211212
*/
212213
export const empty: Effect.Effect<AiChat.Service, never, AiLanguageModel.AiLanguageModel> = fromPrompt({ prompt: [] })
213214

214215
const decodeUnknown = Schema.decodeUnknown(AiInput.AiInput)
215216

216217
/**
217218
* @since 1.0.0
218-
* @category constructors
219+
* @category Constructors
219220
*/
220221
export const fromExport = (data: unknown): Effect.Effect<AiChat.Service, ParseError, AiLanguageModel.AiLanguageModel> =>
221222
Effect.flatMap(decodeUnknown(data), (prompt) => fromPrompt({ prompt }))
@@ -224,7 +225,7 @@ const decodeJson = Schema.decode(AiInput.FromJson)
224225

225226
/**
226227
* @since 1.0.0
227-
* @category constructors
228+
* @category Constructors
228229
*/
229230
export const fromJson = (data: string): Effect.Effect<AiChat.Service, ParseError, AiLanguageModel.AiLanguageModel> =>
230231
Effect.flatMap(decodeJson(data), (prompt) => fromPrompt({ prompt }))

packages/ai/ai/src/AiInput.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ export class ToolCallPart extends Schema.TaggedClass<ToolCallPart>(
359359
readonly [PartTypeId]: PartTypeId = PartTypeId
360360

361361
constructor(props: any, options?: Schema.MakeOptions) {
362-
console.log(props)
363362
super(props, options)
364363
}
365364
}

packages/ai/ai/src/AiLanguageModel.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,10 @@ export declare namespace AiLanguageModel {
233233
* If a `toolkit` is specified, the large language model will additionally
234234
* be able to perform tool calls to augment its response.
235235
*/
236-
readonly streamText: <
237-
Options extends NoExcessProperties<GenerateTextOptions<any>, Options>
238-
>(options: Options) => Stream.Stream<
239-
AiResponse.AiResponse,
236+
readonly streamText: <Tools extends AiTool.Any, Options>(
237+
options: Options & GenerateTextOptions<Tools>
238+
) => Stream.Stream<
239+
ExtractSuccess<Options>,
240240
ExtractError<Options>,
241241
ExtractContext<Options> | Config
242242
>
@@ -547,9 +547,15 @@ const resolveParts = <Tools extends AiTool.Any>(options: {
547547
* be able to perform tool calls to augment its response.
548548
*
549549
* @since 1.0.0
550-
* @category functions
550+
* @category Functions
551551
*/
552-
export const generateText = Effect.serviceFunctionEffect(AiLanguageModel, (_) => _.generateText)
552+
export const generateText: <Tools extends AiTool.Any, Options>(
553+
options: Options & GenerateTextOptions<Tools>
554+
) => Effect.Effect<
555+
ExtractSuccess<Options>,
556+
ExtractError<Options>,
557+
AiLanguageModel | ExtractContext<Options>
558+
> = Effect.serviceFunctionEffect(AiLanguageModel, (_) => _.generateText)
553559

554560
/**
555561
* Generate a structured object for the specified prompt and schema using a
@@ -560,9 +566,15 @@ export const generateText = Effect.serviceFunctionEffect(AiLanguageModel, (_) =>
560566
* output of the model.
561567
*
562568
* @since 1.0.0
563-
* @category functions
569+
* @category Functions
564570
*/
565-
export const generateObject = Effect.serviceFunctionEffect(AiLanguageModel, (_) => _.generateObject)
571+
export const generateObject: <A, I, R>(
572+
options: GenerateObjectOptions<A, I, R> | GenerateObjectWithToolCallIdOptions<A, I, R>
573+
) => Effect.Effect<
574+
AiResponse.WithStructuredOutput<A>,
575+
AiError,
576+
AiLanguageModel | R
577+
> = Effect.serviceFunctionEffect(AiLanguageModel, (_) => _.generateObject)
566578

567579
/**
568580
* Generate text using a large language model for the specified `prompt`,
@@ -572,7 +584,12 @@ export const generateObject = Effect.serviceFunctionEffect(AiLanguageModel, (_)
572584
* be able to perform tool calls to augment its response.
573585
*
574586
* @since 1.0.0
575-
* @category functions
587+
* @category Functions
576588
*/
577-
export const streamText = <Options extends NoExcessProperties<GenerateTextOptions<any>, Options>>(options: Options) =>
578-
Stream.unwrap(AiLanguageModel.pipe(Effect.map((_) => _.streamText(options))))
589+
export const streamText = <Tools extends AiTool.Any, Options>(
590+
options: Options & GenerateTextOptions<Tools>
591+
): Stream.Stream<
592+
ExtractSuccess<Options>,
593+
ExtractError<Options>,
594+
AiLanguageModel | ExtractContext<Options>
595+
> => Stream.unwrap(AiLanguageModel.pipe(Effect.map((_) => _.streamText(options))))

0 commit comments

Comments
 (0)