@opencode-ai/ai 0.0.0-bootstrap.0 → 0.0.0-dev-17471
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/README.md +390 -1
- package/dist/cache-policy.d.ts +2 -0
- package/dist/cache-policy.js +121 -0
- package/dist/image-client.d.ts +19 -0
- package/dist/image-client.js +19 -0
- package/dist/image.d.ts +110 -0
- package/dist/image.js +102 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +12 -0
- package/dist/llm.d.ts +230 -0
- package/dist/llm.js +77 -0
- package/dist/protocols/anthropic-messages.d.ts +633 -0
- package/dist/protocols/anthropic-messages.js +860 -0
- package/dist/protocols/bedrock-converse.d.ts +533 -0
- package/dist/protocols/bedrock-converse.js +584 -0
- package/dist/protocols/bedrock-event-stream.d.ts +9 -0
- package/dist/protocols/bedrock-event-stream.js +73 -0
- package/dist/protocols/gemini.d.ts +300 -0
- package/dist/protocols/gemini.js +512 -0
- package/dist/protocols/google-images.d.ts +30 -0
- package/dist/protocols/google-images.js +191 -0
- package/dist/protocols/index.d.ts +10 -0
- package/dist/protocols/index.js +10 -0
- package/dist/protocols/open-responses-channel.d.ts +26 -0
- package/dist/protocols/open-responses-channel.js +120 -0
- package/dist/protocols/open-responses.d.ts +951 -0
- package/dist/protocols/open-responses.js +873 -0
- package/dist/protocols/openai-chat.d.ts +795 -0
- package/dist/protocols/openai-chat.js +718 -0
- package/dist/protocols/openai-compatible-chat.d.ts +107 -0
- package/dist/protocols/openai-compatible-chat.js +20 -0
- package/dist/protocols/openai-compatible-responses.d.ts +96 -0
- package/dist/protocols/openai-compatible-responses.js +17 -0
- package/dist/protocols/openai-images.d.ts +32 -0
- package/dist/protocols/openai-images.js +188 -0
- package/dist/protocols/openai-responses-channel.d.ts +12 -0
- package/dist/protocols/openai-responses-channel.js +142 -0
- package/dist/protocols/openai-responses.d.ts +596 -0
- package/dist/protocols/openai-responses.js +208 -0
- package/dist/protocols/shared.d.ts +194 -0
- package/dist/protocols/shared.js +238 -0
- package/dist/protocols/utils/bedrock-auth.d.ts +21 -0
- package/dist/protocols/utils/bedrock-auth.js +43 -0
- package/dist/protocols/utils/bedrock-cache.d.ts +15 -0
- package/dist/protocols/utils/bedrock-cache.js +29 -0
- package/dist/protocols/utils/bedrock-media.d.ts +49 -0
- package/dist/protocols/utils/bedrock-media.js +70 -0
- package/dist/protocols/utils/cache.d.ts +6 -0
- package/dist/protocols/utils/cache.js +8 -0
- package/dist/protocols/utils/gemini-tool-schema.d.ts +2 -0
- package/dist/protocols/utils/gemini-tool-schema.js +103 -0
- package/dist/protocols/utils/image-input.d.ts +21 -0
- package/dist/protocols/utils/image-input.js +22 -0
- package/dist/protocols/utils/lifecycle.d.ts +20 -0
- package/dist/protocols/utils/lifecycle.js +68 -0
- package/dist/protocols/utils/open-responses-options.d.ts +21 -0
- package/dist/protocols/utils/open-responses-options.js +41 -0
- package/dist/protocols/utils/openai-image.d.ts +5 -0
- package/dist/protocols/utils/openai-image.js +18 -0
- package/dist/protocols/utils/openai-options.d.ts +14 -0
- package/dist/protocols/utils/openai-options.js +14 -0
- package/dist/protocols/utils/tool-schema.d.ts +8 -0
- package/dist/protocols/utils/tool-schema.js +81 -0
- package/dist/protocols/utils/tool-stream.d.ts +639 -0
- package/dist/protocols/utils/tool-stream.js +141 -0
- package/dist/protocols/xai-images.d.ts +26 -0
- package/dist/protocols/xai-images.js +111 -0
- package/dist/protocols/zai-images.d.ts +22 -0
- package/dist/protocols/zai-images.js +84 -0
- package/dist/protocols.d.ts +1 -0
- package/dist/protocols.js +1 -0
- package/dist/provider-error.d.ts +14 -0
- package/dist/provider-error.js +130 -0
- package/dist/provider-package.d.ts +15 -0
- package/dist/provider-package.js +1 -0
- package/dist/provider.d.ts +23 -0
- package/dist/provider.js +2 -0
- package/dist/providers/amazon-bedrock/mantle/chat.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle/chat.js +1 -0
- package/dist/providers/amazon-bedrock/mantle/responses.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle/responses.js +1 -0
- package/dist/providers/amazon-bedrock/mantle.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle.js +1 -0
- package/dist/providers/amazon-bedrock-mantle.d.ts +247 -0
- package/dist/providers/amazon-bedrock-mantle.js +68 -0
- package/dist/providers/amazon-bedrock.d.ts +172 -0
- package/dist/providers/amazon-bedrock.js +41 -0
- package/dist/providers/anthropic-compatible.d.ts +218 -0
- package/dist/providers/anthropic-compatible.js +45 -0
- package/dist/providers/anthropic.d.ts +216 -0
- package/dist/providers/anthropic.js +40 -0
- package/dist/providers/azure/chat.d.ts +2 -0
- package/dist/providers/azure/chat.js +1 -0
- package/dist/providers/azure/responses.d.ts +2 -0
- package/dist/providers/azure/responses.js +1 -0
- package/dist/providers/azure.d.ts +248 -0
- package/dist/providers/azure.js +90 -0
- package/dist/providers/cloudflare.d.ts +338 -0
- package/dist/providers/cloudflare.js +87 -0
- package/dist/providers/google-vertex/chat.d.ts +2 -0
- package/dist/providers/google-vertex/chat.js +1 -0
- package/dist/providers/google-vertex/gemini.d.ts +2 -0
- package/dist/providers/google-vertex/gemini.js +1 -0
- package/dist/providers/google-vertex/messages.d.ts +2 -0
- package/dist/providers/google-vertex/messages.js +1 -0
- package/dist/providers/google-vertex/responses.d.ts +2 -0
- package/dist/providers/google-vertex/responses.js +1 -0
- package/dist/providers/google-vertex-chat.d.ts +131 -0
- package/dist/providers/google-vertex-chat.js +50 -0
- package/dist/providers/google-vertex-messages.d.ts +215 -0
- package/dist/providers/google-vertex-messages.js +75 -0
- package/dist/providers/google-vertex-responses.d.ts +122 -0
- package/dist/providers/google-vertex-responses.js +51 -0
- package/dist/providers/google-vertex-shared.d.ts +22 -0
- package/dist/providers/google-vertex-shared.js +61 -0
- package/dist/providers/google-vertex.d.ts +115 -0
- package/dist/providers/google-vertex.js +64 -0
- package/dist/providers/google.d.ts +112 -0
- package/dist/providers/google.js +43 -0
- package/dist/providers/index.d.ts +18 -0
- package/dist/providers/index.js +18 -0
- package/dist/providers/open-responses-options.d.ts +16 -0
- package/dist/providers/open-responses-options.js +1 -0
- package/dist/providers/openai/chat.d.ts +2 -0
- package/dist/providers/openai/chat.js +1 -0
- package/dist/providers/openai/responses.d.ts +2 -0
- package/dist/providers/openai/responses.js +1 -0
- package/dist/providers/openai-compatible/responses.d.ts +1 -0
- package/dist/providers/openai-compatible/responses.js +1 -0
- package/dist/providers/openai-compatible-profile.d.ts +43 -0
- package/dist/providers/openai-compatible-profile.js +12 -0
- package/dist/providers/openai-compatible-responses.d.ts +120 -0
- package/dist/providers/openai-compatible-responses.js +33 -0
- package/dist/providers/openai-compatible.d.ts +167 -0
- package/dist/providers/openai-compatible.js +55 -0
- package/dist/providers/openai-options.d.ts +21 -0
- package/dist/providers/openai-options.js +41 -0
- package/dist/providers/openai.d.ts +264 -0
- package/dist/providers/openai.js +83 -0
- package/dist/providers/openrouter.d.ts +537 -0
- package/dist/providers/openrouter.js +117 -0
- package/dist/providers/xai.d.ts +249 -0
- package/dist/providers/xai.js +79 -0
- package/dist/providers/zai.d.ts +24 -0
- package/dist/providers/zai.js +21 -0
- package/dist/providers.d.ts +1 -0
- package/dist/providers.js +1 -0
- package/dist/route/auth-options.d.ts +34 -0
- package/dist/route/auth-options.js +14 -0
- package/dist/route/auth.d.ts +51 -0
- package/dist/route/auth.js +89 -0
- package/dist/route/client.d.ts +348 -0
- package/dist/route/client.js +214 -0
- package/dist/route/endpoint.d.ts +28 -0
- package/dist/route/endpoint.js +21 -0
- package/dist/route/executor.d.ts +23 -0
- package/dist/route/executor.js +227 -0
- package/dist/route/framing.d.ts +23 -0
- package/dist/route/framing.js +4 -0
- package/dist/route/index.d.ts +16 -0
- package/dist/route/index.js +9 -0
- package/dist/route/protocol.d.ts +77 -0
- package/dist/route/protocol.js +17 -0
- package/dist/route/transport/http.d.ts +34 -0
- package/dist/route/transport/http.js +63 -0
- package/dist/route/transport/index.d.ts +39 -0
- package/dist/route/transport/index.js +4 -0
- package/dist/route/transport/websocket-channel.d.ts +56 -0
- package/dist/route/transport/websocket-channel.js +1 -0
- package/dist/route/transport/websocket.d.ts +55 -0
- package/dist/route/transport/websocket.js +326 -0
- package/dist/route.d.ts +1 -0
- package/dist/route.js +1 -0
- package/dist/schema/errors.d.ts +159 -0
- package/dist/schema/errors.js +155 -0
- package/dist/schema/events.d.ts +4587 -0
- package/dist/schema/events.js +492 -0
- package/dist/schema/ids.d.ts +30 -0
- package/dist/schema/ids.js +19 -0
- package/dist/schema/index.d.ts +5 -0
- package/dist/schema/index.js +5 -0
- package/dist/schema/messages.d.ts +402 -0
- package/dist/schema/messages.js +244 -0
- package/dist/schema/options.d.ts +159 -0
- package/dist/schema/options.js +202 -0
- package/dist/testing.d.ts +1493 -0
- package/dist/testing.js +88 -0
- package/dist/tool-runtime.d.ts +15 -0
- package/dist/tool-runtime.js +54 -0
- package/dist/tool.d.ts +135 -0
- package/dist/tool.js +66 -0
- package/dist/utils/record.d.ts +2 -0
- package/dist/utils/record.js +2 -0
- package/package.json +40 -9
- package/index.js +0 -1
package/dist/image.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { HttpOptions, InvalidRequestReason, AIError, ModelID, ProviderID, ProviderMetadata, Usage, } from "./schema/index.js";
|
|
3
|
+
import { ImageClient, Service } from "./image-client.js";
|
|
4
|
+
export class ImageModel {
|
|
5
|
+
id;
|
|
6
|
+
provider;
|
|
7
|
+
route;
|
|
8
|
+
http;
|
|
9
|
+
constructor(input) {
|
|
10
|
+
this.id = input.id;
|
|
11
|
+
this.provider = input.provider;
|
|
12
|
+
this.route = input.route;
|
|
13
|
+
this.http = input.http;
|
|
14
|
+
}
|
|
15
|
+
static make(input) {
|
|
16
|
+
return new ImageModel({
|
|
17
|
+
id: ModelID.make(input.id),
|
|
18
|
+
provider: ProviderID.make(input.provider),
|
|
19
|
+
route: input.route,
|
|
20
|
+
http: input.http,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export const ImageModelSchema = Schema.declare((value) => value instanceof ImageModel, {
|
|
25
|
+
expected: "Image.Model",
|
|
26
|
+
});
|
|
27
|
+
const ImageBytesInput = Schema.Struct({
|
|
28
|
+
type: Schema.Literal("bytes"),
|
|
29
|
+
data: Schema.Uint8Array,
|
|
30
|
+
mediaType: Schema.String,
|
|
31
|
+
});
|
|
32
|
+
const ImageUrlInput = Schema.Struct({
|
|
33
|
+
type: Schema.Literal("url"),
|
|
34
|
+
url: Schema.String,
|
|
35
|
+
});
|
|
36
|
+
const ImageFileIDInput = Schema.Struct({
|
|
37
|
+
type: Schema.Literal("file-id"),
|
|
38
|
+
id: Schema.String,
|
|
39
|
+
});
|
|
40
|
+
const ImageFileURIInput = Schema.Struct({
|
|
41
|
+
type: Schema.Literal("file-uri"),
|
|
42
|
+
uri: Schema.String,
|
|
43
|
+
mediaType: Schema.String,
|
|
44
|
+
});
|
|
45
|
+
export const ImageInputSchema = Schema.Union([
|
|
46
|
+
ImageBytesInput,
|
|
47
|
+
ImageUrlInput,
|
|
48
|
+
ImageFileIDInput,
|
|
49
|
+
ImageFileURIInput,
|
|
50
|
+
]).pipe(Schema.toTaggedUnion("type"));
|
|
51
|
+
export const ImageInput = {
|
|
52
|
+
bytes: (data, mediaType) => ({ type: "bytes", data, mediaType }),
|
|
53
|
+
url: (url) => ({ type: "url", url }),
|
|
54
|
+
file: (id) => ({ type: "file-id", id }),
|
|
55
|
+
fileUri: (uri, mediaType) => ({ type: "file-uri", uri, mediaType }),
|
|
56
|
+
};
|
|
57
|
+
export class ImageRequest extends Schema.Class("Image.Request")({
|
|
58
|
+
model: ImageModelSchema,
|
|
59
|
+
prompt: Schema.String,
|
|
60
|
+
images: Schema.optional(Schema.Array(ImageInputSchema)),
|
|
61
|
+
options: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
62
|
+
http: Schema.optional(HttpOptions),
|
|
63
|
+
}) {
|
|
64
|
+
}
|
|
65
|
+
export class GeneratedImage extends Schema.Class("Image.Generated")({
|
|
66
|
+
mediaType: Schema.String,
|
|
67
|
+
data: Schema.Union([Schema.String, Schema.Uint8Array]),
|
|
68
|
+
providerMetadata: Schema.optional(ProviderMetadata),
|
|
69
|
+
}) {
|
|
70
|
+
}
|
|
71
|
+
export class ImageResponse extends Schema.Class("Image.Response")({
|
|
72
|
+
images: Schema.Array(GeneratedImage),
|
|
73
|
+
usage: Schema.optional(Usage),
|
|
74
|
+
providerMetadata: Schema.optional(ProviderMetadata),
|
|
75
|
+
}) {
|
|
76
|
+
get image() {
|
|
77
|
+
return this.images[0];
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
export function request(input) {
|
|
81
|
+
if (input instanceof ImageRequest)
|
|
82
|
+
return input;
|
|
83
|
+
return new ImageRequest({
|
|
84
|
+
...input,
|
|
85
|
+
model: input.model,
|
|
86
|
+
http: input.http === undefined ? undefined : HttpOptions.make(input.http),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
export function generate(input) {
|
|
90
|
+
return Effect.try({
|
|
91
|
+
try: () => (input instanceof ImageRequest ? input : request(input)),
|
|
92
|
+
catch: (error) => new AIError({
|
|
93
|
+
module: "Image",
|
|
94
|
+
method: "generate",
|
|
95
|
+
reason: new InvalidRequestReason({ message: error instanceof Error ? error.message : String(error) }),
|
|
96
|
+
}),
|
|
97
|
+
}).pipe(Effect.flatMap((request) => ImageClient.generate(request)));
|
|
98
|
+
}
|
|
99
|
+
export const Image = {
|
|
100
|
+
request,
|
|
101
|
+
generate,
|
|
102
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { LLMClient } from "./route/client.js";
|
|
2
|
+
export { ImageClient } from "./image-client.js";
|
|
3
|
+
export { Auth } from "./route/auth.js";
|
|
4
|
+
export { Provider } from "./provider.js";
|
|
5
|
+
export { ProviderPackage } from "./provider-package.js";
|
|
6
|
+
export { isContextOverflow, isContextOverflowFailure } from "./provider-error.js";
|
|
7
|
+
export type { RouteLanguageModelInput, RouteRoutedLanguageModelInput, Interface as LLMClientShape, Service as LLMClientService, } from "./route/client.js";
|
|
8
|
+
export * from "./schema/index.js";
|
|
9
|
+
export { GeneratedImage, ImageInput, ImageInputSchema, ImageModel, ImageRequest, ImageResponse } from "./image.js";
|
|
10
|
+
export type { ImageModelOptions, ImageOptions, ImageRequestFor, ImageRequestInput, ImageRoute } from "./image.js";
|
|
11
|
+
export { Image } from "./image.js";
|
|
12
|
+
export { Tool, ToolFailure, toDefinitions } from "./tool.js";
|
|
13
|
+
export { ToolRuntime } from "./tool-runtime.js";
|
|
14
|
+
export type { DispatchResult as ToolDispatchResult, ToolSettlement } from "./tool-runtime.js";
|
|
15
|
+
export type { AnyExecutableTool, AnyTool, ExecutableTool, ExecutableTools, Definition as ToolShape, ToolExecute, ToolExecuteContext, ToolModelOutputInput, Tools, ToolSchema, ToolToModelOutput, } from "./tool.js";
|
|
16
|
+
export * as LLM from "./llm.js";
|
|
17
|
+
export type { Definition as ProviderDefinition, LanguageModelFactory as ProviderLanguageModelFactory, LanguageModelOptions as ProviderLanguageModelOptions, } from "./provider.js";
|
|
18
|
+
export type { Definition as ProviderPackageDefinition, Settings as ProviderPackageSettings, } from "./provider-package.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { LLMClient } from "./route/client.js";
|
|
2
|
+
export { ImageClient } from "./image-client.js";
|
|
3
|
+
export { Auth } from "./route/auth.js";
|
|
4
|
+
export { Provider } from "./provider.js";
|
|
5
|
+
export { ProviderPackage } from "./provider-package.js";
|
|
6
|
+
export { isContextOverflow, isContextOverflowFailure } from "./provider-error.js";
|
|
7
|
+
export * from "./schema/index.js";
|
|
8
|
+
export { GeneratedImage, ImageInput, ImageInputSchema, ImageModel, ImageRequest, ImageResponse } from "./image.js";
|
|
9
|
+
export { Image } from "./image.js";
|
|
10
|
+
export { Tool, ToolFailure, toDefinitions } from "./tool.js";
|
|
11
|
+
export { ToolRuntime } from "./tool-runtime.js";
|
|
12
|
+
export * as LLM from "./llm.js";
|
package/dist/llm.d.ts
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { Effect, JsonSchema, Schema } from "effect";
|
|
2
|
+
import { Service } from "./route/client.js";
|
|
3
|
+
import { GenerationOptions, HttpOptions, AIError, LLMRequest, LLMResponse, Message, LanguageModel, SystemPart, ToolChoice, ToolDefinition, type ContentPart, type LanguageModelProviderOptions } from "./schema/index.js";
|
|
4
|
+
import { type ToolSchema } from "./tool.js";
|
|
5
|
+
/** Input accepted by `LLM.request`, normalized into the canonical `LLMRequest` class. */
|
|
6
|
+
export type RequestInput<SelectedLanguageModel extends LanguageModel = LanguageModel> = Omit<ConstructorParameters<typeof LLMRequest>[0], "model" | "system" | "messages" | "tools" | "toolChoice" | "generation" | "http" | "providerOptions"> & {
|
|
7
|
+
readonly model: SelectedLanguageModel;
|
|
8
|
+
readonly system?: string | SystemPart | ReadonlyArray<SystemPart>;
|
|
9
|
+
readonly prompt?: string | ContentPart | ReadonlyArray<ContentPart>;
|
|
10
|
+
readonly messages?: ReadonlyArray<Message | Message.Input>;
|
|
11
|
+
readonly tools?: ReadonlyArray<ToolDefinition.Input>;
|
|
12
|
+
readonly toolChoice?: ToolChoice.Input;
|
|
13
|
+
readonly generation?: GenerationOptions.Input;
|
|
14
|
+
readonly providerOptions?: NoInfer<LanguageModelProviderOptions<SelectedLanguageModel>>;
|
|
15
|
+
readonly http?: HttpOptions.Input;
|
|
16
|
+
};
|
|
17
|
+
export declare const generate: typeof import("./route/client.js").generate;
|
|
18
|
+
export declare const stream: typeof import("./route/client.js").stream;
|
|
19
|
+
export declare const request: <const SelectedLanguageModel extends LanguageModel>(input: RequestInput<SelectedLanguageModel>) => LLMRequest;
|
|
20
|
+
type GenerateObjectBase<SelectedLanguageModel extends LanguageModel = LanguageModel> = Omit<RequestInput<SelectedLanguageModel>, "tools" | "toolChoice">;
|
|
21
|
+
export declare class GenerateObjectResponse<T> {
|
|
22
|
+
readonly object: T;
|
|
23
|
+
readonly response: LLMResponse;
|
|
24
|
+
constructor(object: T, response: LLMResponse);
|
|
25
|
+
get events(): readonly ({
|
|
26
|
+
readonly type: "step-start";
|
|
27
|
+
readonly index: number;
|
|
28
|
+
} | {
|
|
29
|
+
readonly id: string;
|
|
30
|
+
readonly type: "text-start";
|
|
31
|
+
readonly providerMetadata?: {
|
|
32
|
+
readonly [x: string]: {
|
|
33
|
+
readonly [x: string]: unknown;
|
|
34
|
+
};
|
|
35
|
+
} | undefined;
|
|
36
|
+
} | {
|
|
37
|
+
readonly id: string;
|
|
38
|
+
readonly type: "text-delta";
|
|
39
|
+
readonly text: string;
|
|
40
|
+
readonly providerMetadata?: {
|
|
41
|
+
readonly [x: string]: {
|
|
42
|
+
readonly [x: string]: unknown;
|
|
43
|
+
};
|
|
44
|
+
} | undefined;
|
|
45
|
+
} | {
|
|
46
|
+
readonly id: string;
|
|
47
|
+
readonly type: "text-end";
|
|
48
|
+
readonly providerMetadata?: {
|
|
49
|
+
readonly [x: string]: {
|
|
50
|
+
readonly [x: string]: unknown;
|
|
51
|
+
};
|
|
52
|
+
} | undefined;
|
|
53
|
+
} | {
|
|
54
|
+
readonly id: string;
|
|
55
|
+
readonly type: "reasoning-start";
|
|
56
|
+
readonly providerMetadata?: {
|
|
57
|
+
readonly [x: string]: {
|
|
58
|
+
readonly [x: string]: unknown;
|
|
59
|
+
};
|
|
60
|
+
} | undefined;
|
|
61
|
+
} | {
|
|
62
|
+
readonly id: string;
|
|
63
|
+
readonly type: "reasoning-delta";
|
|
64
|
+
readonly text: string;
|
|
65
|
+
readonly providerMetadata?: {
|
|
66
|
+
readonly [x: string]: {
|
|
67
|
+
readonly [x: string]: unknown;
|
|
68
|
+
};
|
|
69
|
+
} | undefined;
|
|
70
|
+
} | {
|
|
71
|
+
readonly id: string;
|
|
72
|
+
readonly type: "reasoning-end";
|
|
73
|
+
readonly providerMetadata?: {
|
|
74
|
+
readonly [x: string]: {
|
|
75
|
+
readonly [x: string]: unknown;
|
|
76
|
+
};
|
|
77
|
+
} | undefined;
|
|
78
|
+
} | {
|
|
79
|
+
readonly id: string;
|
|
80
|
+
readonly type: "tool-input-start";
|
|
81
|
+
readonly name: string;
|
|
82
|
+
readonly providerMetadata?: {
|
|
83
|
+
readonly [x: string]: {
|
|
84
|
+
readonly [x: string]: unknown;
|
|
85
|
+
};
|
|
86
|
+
} | undefined;
|
|
87
|
+
readonly providerExecuted?: boolean | undefined;
|
|
88
|
+
} | {
|
|
89
|
+
readonly type: "tool-input-delta";
|
|
90
|
+
readonly id: string;
|
|
91
|
+
readonly name: string;
|
|
92
|
+
readonly text: string;
|
|
93
|
+
} | {
|
|
94
|
+
readonly id: string;
|
|
95
|
+
readonly type: "tool-input-end";
|
|
96
|
+
readonly name: string;
|
|
97
|
+
readonly providerMetadata?: {
|
|
98
|
+
readonly [x: string]: {
|
|
99
|
+
readonly [x: string]: unknown;
|
|
100
|
+
};
|
|
101
|
+
} | undefined;
|
|
102
|
+
} | {
|
|
103
|
+
readonly type: "tool-input-error";
|
|
104
|
+
readonly id: string;
|
|
105
|
+
readonly name: string;
|
|
106
|
+
readonly raw: string;
|
|
107
|
+
} | {
|
|
108
|
+
readonly id: string;
|
|
109
|
+
readonly type: "tool-call";
|
|
110
|
+
readonly name: string;
|
|
111
|
+
readonly input: unknown;
|
|
112
|
+
readonly providerMetadata?: {
|
|
113
|
+
readonly [x: string]: {
|
|
114
|
+
readonly [x: string]: unknown;
|
|
115
|
+
};
|
|
116
|
+
} | undefined;
|
|
117
|
+
readonly providerExecuted?: boolean | undefined;
|
|
118
|
+
} | {
|
|
119
|
+
readonly id: string;
|
|
120
|
+
readonly type: "tool-result";
|
|
121
|
+
readonly name: string;
|
|
122
|
+
readonly result: {
|
|
123
|
+
readonly type: "json";
|
|
124
|
+
readonly value: unknown;
|
|
125
|
+
} | {
|
|
126
|
+
readonly type: "text";
|
|
127
|
+
readonly value: unknown;
|
|
128
|
+
} | {
|
|
129
|
+
readonly type: "error";
|
|
130
|
+
readonly value: unknown;
|
|
131
|
+
} | {
|
|
132
|
+
readonly type: "content";
|
|
133
|
+
readonly value: readonly ({
|
|
134
|
+
readonly type: "text";
|
|
135
|
+
readonly text: string;
|
|
136
|
+
} | {
|
|
137
|
+
readonly type: "file";
|
|
138
|
+
readonly uri: string;
|
|
139
|
+
readonly mime: string;
|
|
140
|
+
readonly name?: string | undefined;
|
|
141
|
+
})[];
|
|
142
|
+
};
|
|
143
|
+
readonly output?: {
|
|
144
|
+
readonly structured: unknown;
|
|
145
|
+
readonly content: readonly ({
|
|
146
|
+
readonly type: "text";
|
|
147
|
+
readonly text: string;
|
|
148
|
+
} | {
|
|
149
|
+
readonly type: "file";
|
|
150
|
+
readonly uri: string;
|
|
151
|
+
readonly mime: string;
|
|
152
|
+
readonly name?: string | undefined;
|
|
153
|
+
})[];
|
|
154
|
+
} | undefined;
|
|
155
|
+
readonly providerMetadata?: {
|
|
156
|
+
readonly [x: string]: {
|
|
157
|
+
readonly [x: string]: unknown;
|
|
158
|
+
};
|
|
159
|
+
} | undefined;
|
|
160
|
+
readonly providerExecuted?: boolean | undefined;
|
|
161
|
+
} | {
|
|
162
|
+
readonly id: string;
|
|
163
|
+
readonly type: "tool-error";
|
|
164
|
+
readonly name: string;
|
|
165
|
+
readonly message: string;
|
|
166
|
+
readonly error?: unknown;
|
|
167
|
+
readonly providerMetadata?: {
|
|
168
|
+
readonly [x: string]: {
|
|
169
|
+
readonly [x: string]: unknown;
|
|
170
|
+
};
|
|
171
|
+
} | undefined;
|
|
172
|
+
} | {
|
|
173
|
+
readonly type: "step-finish";
|
|
174
|
+
readonly reason: {
|
|
175
|
+
readonly normalized: "length" | "stop" | "tool-calls" | "content-filter" | "error" | "unknown";
|
|
176
|
+
readonly raw?: string | undefined;
|
|
177
|
+
};
|
|
178
|
+
readonly index: number;
|
|
179
|
+
readonly providerMetadata?: {
|
|
180
|
+
readonly [x: string]: {
|
|
181
|
+
readonly [x: string]: unknown;
|
|
182
|
+
};
|
|
183
|
+
} | undefined;
|
|
184
|
+
readonly usage?: import("./schema/events.js").Usage | undefined;
|
|
185
|
+
} | {
|
|
186
|
+
readonly type: "finish";
|
|
187
|
+
readonly reason: {
|
|
188
|
+
readonly normalized: "length" | "stop" | "tool-calls" | "content-filter" | "error" | "unknown";
|
|
189
|
+
readonly raw?: string | undefined;
|
|
190
|
+
};
|
|
191
|
+
readonly providerMetadata?: {
|
|
192
|
+
readonly [x: string]: {
|
|
193
|
+
readonly [x: string]: unknown;
|
|
194
|
+
};
|
|
195
|
+
} | undefined;
|
|
196
|
+
readonly usage?: import("./schema/events.js").Usage | undefined;
|
|
197
|
+
} | {
|
|
198
|
+
readonly type: "provider-error";
|
|
199
|
+
readonly message: string;
|
|
200
|
+
readonly providerMetadata?: {
|
|
201
|
+
readonly [x: string]: {
|
|
202
|
+
readonly [x: string]: unknown;
|
|
203
|
+
};
|
|
204
|
+
} | undefined;
|
|
205
|
+
readonly classification?: "context-overflow" | "payload-too-large" | undefined;
|
|
206
|
+
})[];
|
|
207
|
+
get usage(): import("./schema/events.js").Usage | undefined;
|
|
208
|
+
}
|
|
209
|
+
export interface GenerateObjectOptions<S extends ToolSchema<any>, SelectedLanguageModel extends LanguageModel = LanguageModel> extends GenerateObjectBase<SelectedLanguageModel> {
|
|
210
|
+
readonly schema: S;
|
|
211
|
+
}
|
|
212
|
+
export interface GenerateObjectDynamicOptions<SelectedLanguageModel extends LanguageModel = LanguageModel> extends GenerateObjectBase<SelectedLanguageModel> {
|
|
213
|
+
/** Raw JSON Schema object describing the expected output shape. */
|
|
214
|
+
readonly jsonSchema: JsonSchema.JsonSchema;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Run a model and decode its output against `schema`. Works on every protocol
|
|
218
|
+
* because it forces a synthetic tool call internally — provider-native JSON
|
|
219
|
+
* modes are intentionally avoided so behaviour is uniform.
|
|
220
|
+
*
|
|
221
|
+
* Two input modes:
|
|
222
|
+
*
|
|
223
|
+
* 1. `schema: EffectSchema<T>` — `.object` is decoded and typed as `T`.
|
|
224
|
+
* Decode failures surface as `AIError`.
|
|
225
|
+
* 2. `jsonSchema: JsonSchema.JsonSchema` — `.object` is `unknown`. Use when
|
|
226
|
+
* the schema is only available at runtime (MCP, plugin manifests). Caller validates.
|
|
227
|
+
*/
|
|
228
|
+
export declare function generateObject<const SelectedLanguageModel extends LanguageModel, S extends ToolSchema<any>>(options: GenerateObjectOptions<S, SelectedLanguageModel>): Effect.Effect<GenerateObjectResponse<Schema.Schema.Type<S>>, AIError, Service>;
|
|
229
|
+
export declare function generateObject<const SelectedLanguageModel extends LanguageModel>(options: GenerateObjectDynamicOptions<SelectedLanguageModel>): Effect.Effect<GenerateObjectResponse<unknown>, AIError, Service>;
|
|
230
|
+
export {};
|
package/dist/llm.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Effect, JsonSchema, Schema } from "effect";
|
|
2
|
+
import { LLMClient, Service } from "./route/client.js";
|
|
3
|
+
import { GenerationOptions, HttpOptions, InvalidProviderOutputReason, AIError, LLMEvent, LLMRequest, LLMResponse, Message, LanguageModel, SystemPart, ToolChoice, ToolDefinition, } from "./schema/index.js";
|
|
4
|
+
import { make as makeTool, toDefinitions } from "./tool.js";
|
|
5
|
+
export const generate = LLMClient.generate;
|
|
6
|
+
export const stream = LLMClient.stream;
|
|
7
|
+
export const request = (input) => {
|
|
8
|
+
const { system: requestSystem, prompt, messages, tools, toolChoice: requestToolChoice, generation: requestGeneration, providerOptions: requestProviderOptions, http: requestHttp, ...rest } = input;
|
|
9
|
+
return new LLMRequest({
|
|
10
|
+
...rest,
|
|
11
|
+
system: SystemPart.content(requestSystem),
|
|
12
|
+
messages: [...(messages?.map(Message.make) ?? []), ...(prompt === undefined ? [] : [Message.user(prompt)])],
|
|
13
|
+
tools: tools?.map(ToolDefinition.make) ?? [],
|
|
14
|
+
toolChoice: requestToolChoice ? ToolChoice.make(requestToolChoice) : undefined,
|
|
15
|
+
generation: requestGeneration === undefined ? undefined : GenerationOptions.make(requestGeneration),
|
|
16
|
+
providerOptions: requestProviderOptions,
|
|
17
|
+
http: requestHttp === undefined ? undefined : HttpOptions.make(requestHttp),
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
const GENERATE_OBJECT_TOOL_NAME = "generate_object";
|
|
21
|
+
const GENERATE_OBJECT_TOOL_DESCRIPTION = "Return the structured result by calling this tool.";
|
|
22
|
+
export class GenerateObjectResponse {
|
|
23
|
+
object;
|
|
24
|
+
response;
|
|
25
|
+
constructor(object, response) {
|
|
26
|
+
this.object = object;
|
|
27
|
+
this.response = response;
|
|
28
|
+
}
|
|
29
|
+
get events() {
|
|
30
|
+
return this.response.events;
|
|
31
|
+
}
|
|
32
|
+
get usage() {
|
|
33
|
+
return this.response.usage;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const runGenerateObject = Effect.fn("LLM.generateObject")(function* (options, tool) {
|
|
37
|
+
const baseRequest = request(options);
|
|
38
|
+
const generateRequest = LLMRequest.update(baseRequest, {
|
|
39
|
+
tools: toDefinitions({ [GENERATE_OBJECT_TOOL_NAME]: tool }),
|
|
40
|
+
toolChoice: ToolChoice.named(GENERATE_OBJECT_TOOL_NAME),
|
|
41
|
+
});
|
|
42
|
+
const response = yield* LLMClient.generate(generateRequest);
|
|
43
|
+
const call = response.toolCalls.find((event) => LLMEvent.is.toolCall(event) && event.name === GENERATE_OBJECT_TOOL_NAME);
|
|
44
|
+
if (!call || !LLMEvent.is.toolCall(call))
|
|
45
|
+
return yield* new AIError({
|
|
46
|
+
module: "LLM",
|
|
47
|
+
method: "generateObject",
|
|
48
|
+
reason: new InvalidProviderOutputReason({
|
|
49
|
+
message: `generateObject: model did not call the forced \`${GENERATE_OBJECT_TOOL_NAME}\` tool`,
|
|
50
|
+
}),
|
|
51
|
+
});
|
|
52
|
+
const object = yield* tool._decode(call.input).pipe(Effect.mapError((error) => new AIError({
|
|
53
|
+
module: "LLM",
|
|
54
|
+
method: "generateObject",
|
|
55
|
+
reason: new InvalidProviderOutputReason({
|
|
56
|
+
message: `generateObject: tool input failed schema decode: ${error.message}`,
|
|
57
|
+
}),
|
|
58
|
+
})));
|
|
59
|
+
return new GenerateObjectResponse(object, response);
|
|
60
|
+
});
|
|
61
|
+
export function generateObject(options) {
|
|
62
|
+
if ("schema" in options) {
|
|
63
|
+
const { schema, ...rest } = options;
|
|
64
|
+
return runGenerateObject(rest, makeTool({
|
|
65
|
+
description: GENERATE_OBJECT_TOOL_DESCRIPTION,
|
|
66
|
+
parameters: schema,
|
|
67
|
+
success: Schema.Unknown,
|
|
68
|
+
execute: () => Effect.void,
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
71
|
+
const { jsonSchema, ...rest } = options;
|
|
72
|
+
return runGenerateObject(rest, makeTool({
|
|
73
|
+
description: GENERATE_OBJECT_TOOL_DESCRIPTION,
|
|
74
|
+
jsonSchema,
|
|
75
|
+
execute: () => Effect.void,
|
|
76
|
+
}));
|
|
77
|
+
}
|