@opencode/ai 0.0.0-dev-457 → 0.0.0-dev-461

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 CHANGED
@@ -47,7 +47,8 @@ await Effect.runPromise(program.pipe(Effect.provide(AIClient.layer)))
47
47
  ```
48
48
 
49
49
  Run `ai.stream(...)` instead of `generate` when you want incremental `LLMEvent`s. Both accept input or a prebuilt
50
- `ai.request(...)`. The event stream is provider-neutral — same shape across OpenAI Chat, OpenAI Responses,
50
+ `ai.request(...)`. Input takes either `prompt`, a single user message, or `messages`, the whole conversation, never
51
+ both; `system` goes with either. The event stream is provider-neutral — same shape across OpenAI Chat, OpenAI Responses,
51
52
  Anthropic Messages, Gemini, Bedrock Converse, and any OpenAI-compatible deployment.
52
53
 
53
54
  ### Tool loop
package/dist/llm.d.ts CHANGED
@@ -3,11 +3,18 @@ import { Service, type StreamOptions } from "./route/client.js";
3
3
  import { GenerationOptions, HttpOptions, AIError, LLMEvent, LLMRequest, LLMResponse, Message, LanguageModel, SystemPart, type Finish, ToolChoice, type ContentPart, type LanguageModelProviderOptions, type ToolEntryInput } from "./schema/index.js";
4
4
  import { type ExecutableTool, type Tool, type ToolSchema } from "./tool.js";
5
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"> & {
6
+ export type RequestInput<SelectedLanguageModel extends LanguageModel = LanguageModel> = RequestFields<SelectedLanguageModel> & PromptOrMessages;
7
+ /** Exactly one of `prompt`, a single user message, or `messages`, the full conversation. */
8
+ export type PromptOrMessages = {
9
+ readonly prompt: string | ContentPart | ReadonlyArray<ContentPart>;
10
+ readonly messages?: never;
11
+ } | {
12
+ readonly messages: ReadonlyArray<Message | Message.Input>;
13
+ readonly prompt?: never;
14
+ };
15
+ type RequestFields<SelectedLanguageModel extends LanguageModel> = Omit<ConstructorParameters<typeof LLMRequest>[0], "model" | "system" | "messages" | "tools" | "toolChoice" | "generation" | "http" | "providerOptions"> & {
7
16
  readonly model: SelectedLanguageModel;
8
17
  readonly system?: string | SystemPart | ReadonlyArray<SystemPart>;
9
- readonly prompt?: string | ContentPart | ReadonlyArray<ContentPart>;
10
- readonly messages?: ReadonlyArray<Message | Message.Input>;
11
18
  /** Sent in array order. Pass `tool(...)` values or wire entries such as hosted provider tools. */
12
19
  readonly tools?: ReadonlyArray<Tool | ToolEntryInput>;
13
20
  readonly toolChoice?: ToolChoice.Input;
@@ -38,7 +45,7 @@ export declare function generate<const Model extends LanguageModel>(input: Reque
38
45
  /** Stream one model call. With `schema`, the `finish` event carries the decoded `object`. */
39
46
  export declare function stream<const Model extends LanguageModel, S extends OutputSchema>(input: ObjectInput<S, Model>, options?: StreamOptions): Stream.Stream<ObjectEvent<OutputType<S>>, AIError, Service>;
40
47
  export declare function stream<const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: StreamOptions): Stream.Stream<LLMEvent, AIError, Service>;
41
- export type LoopInput<SelectedLanguageModel extends LanguageModel = LanguageModel> = Omit<RequestInput<SelectedLanguageModel>, "tools"> & {
48
+ export type LoopInput<SelectedLanguageModel extends LanguageModel = LanguageModel> = Omit<RequestFields<SelectedLanguageModel>, "tools"> & PromptOrMessages & {
42
49
  readonly tools: ReadonlyArray<ExecutableTool>;
43
50
  /** Called after each step that has tool calls; returning `true` ends the loop without running them. */
44
51
  readonly until?: (state: {
package/dist/llm.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Effect, JsonSchema, Schema, Stream } from "effect";
2
2
  import { tryRequest } from "./media-model.js";
3
3
  import { LLMClient, Service } from "./route/client.js";
4
- import { GenerationOptions, HttpOptions, InvalidProviderOutputError, AIError, LLMEvent, LLMRequest, LLMResponse, Message, LanguageModel, SystemPart, ToolChoice, ToolEntry, withPartial, } from "./schema/index.js";
4
+ import { GenerationOptions, HttpOptions, InvalidProviderOutputError, InvalidRequestError, AIError, LLMEvent, LLMRequest, LLMResponse, Message, LanguageModel, SystemPart, ToolChoice, ToolEntry, withPartial, } from "./schema/index.js";
5
5
  import { tool } from "./tool.js";
6
6
  import { ToolRuntime } from "./tool-runtime.js";
7
7
  export function generate(input, options) {
@@ -91,13 +91,14 @@ const prepare = (input) => {
91
91
  const decodeObject = (output, response) => {
92
92
  const call = response.toolCalls.find((event) => event.name === OBJECT_TOOL);
93
93
  const invalid = (message, cause) => new AIError({ reason: new InvalidProviderOutputError({ message, cause }), partial: response });
94
+ // A cut-off call arrives with truncated or empty input, so its decode error would hide the real cause.
95
+ if (response.finishReason.normalized === "length")
96
+ return Effect.fail(invalid("output hit the token limit before the forced tool call completed"));
94
97
  if (call)
95
98
  return output
96
99
  ._decode(call.input)
97
100
  .pipe(Effect.mapError((error) => invalid(`schema decode failed: ${error.message}`, error)));
98
- return Effect.fail(invalid(response.finishReason.normalized === "length"
99
- ? "output hit the token limit before the forced tool call completed"
100
- : `model did not call the forced \`${OBJECT_TOOL}\` tool`));
101
+ return Effect.fail(invalid(`model did not call the forced \`${OBJECT_TOOL}\` tool`));
101
102
  };
102
103
  const withObject = (output, event, state) => {
103
104
  const response = LLMResponse.complete(state);
@@ -107,10 +108,14 @@ const withObject = (output, event, state) => {
107
108
  };
108
109
  export const request = (input) => {
109
110
  const { system: requestSystem, prompt, messages, tools, toolChoice: requestToolChoice, generation: requestGeneration, providerOptions: requestProviderOptions, http: requestHttp, ...rest } = input;
111
+ if ((prompt === undefined) === (messages === undefined))
112
+ throw new AIError({
113
+ reason: new InvalidRequestError({ message: "Pass exactly one of `prompt` or `messages`, not both or neither" }),
114
+ });
110
115
  return new LLMRequest({
111
116
  ...rest,
112
117
  system: SystemPart.content(requestSystem),
113
- messages: [...(messages?.map(Message.make) ?? []), ...(prompt === undefined ? [] : [Message.user(prompt)])],
118
+ messages: prompt === undefined ? (messages ?? []).map(Message.make) : [Message.user(prompt)],
114
119
  tools: tools?.map((item) => ("_definition" in item ? item._definition : ToolEntry.make(item))) ?? [],
115
120
  toolChoice: requestToolChoice ? ToolChoice.make(requestToolChoice) : undefined,
116
121
  generation: requestGeneration === undefined ? undefined : GenerationOptions.make(requestGeneration),
@@ -41,10 +41,12 @@ const isQueuedInput = (route) => route.protocol.kind === "queued";
41
41
  /** Lift a synchronous Schema-class constructor into a typed `InvalidRequest` failure. */
42
42
  export const tryRequest = (make) => Effect.try({
43
43
  try: make,
44
- catch: (error) => new AIError({
45
- reason: new InvalidRequestError({
46
- message: error instanceof Error ? error.message : String(error),
47
- cause: error,
44
+ catch: (error) => error instanceof AIError
45
+ ? error
46
+ : new AIError({
47
+ reason: new InvalidRequestError({
48
+ message: error instanceof Error ? error.message : String(error),
49
+ cause: error,
50
+ }),
48
51
  }),
49
- }),
50
52
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
- "version": "0.0.0-dev-457",
3
+ "version": "0.0.0-dev-461",
4
4
  "name": "@opencode/ai",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -520,7 +520,7 @@
520
520
  "devDependencies": {
521
521
  "@clack/prompts": "1.0.0-alpha.1",
522
522
  "@effect/platform-node": "4.1.0",
523
- "@opencode/http-recorder": "0.0.0-dev-457",
523
+ "@opencode/http-recorder": "0.0.0-dev-461",
524
524
  "@tsconfig/bun": "1.0.9",
525
525
  "@types/bun": "1.4.3",
526
526
  "@typescript/native": "npm:typescript@7.1.0-beta",
@@ -530,7 +530,7 @@
530
530
  "@aws-sdk/credential-providers": "3.1057.0",
531
531
  "@smithy/eventstream-codec": "4.2.14",
532
532
  "@smithy/util-utf8": "4.2.2",
533
- "@opencode/schema": "0.0.0-dev-457",
533
+ "@opencode/schema": "0.0.0-dev-461",
534
534
  "aws4fetch": "1.0.20",
535
535
  "effect": "4.1.0",
536
536
  "google-auth-library": "10.5.0"