@opencode/ai 0.0.0-dev-521 → 0.0.0-dev-522
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/llm.js +5 -5
- package/dist/loop.js +4 -4
- package/dist/route/client.js +5 -5
- package/dist/schema/events.d.ts +17 -11
- package/dist/schema/events.js +19 -14
- package/package.json +3 -3
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, canCompact, compactRequest, } from "./route/client.js";
|
|
4
|
-
import { GenerationOptions, HttpOptions, InvalidProviderOutputError, InvalidRequestError, AIError, LLMEvent, LLMResponse, Message, LanguageModel, SystemPart, ToolChoice, ToolEntry, Usage, withPartial, } from "./schema/index.js";
|
|
4
|
+
import { GenerationOptions, HttpOptions, InvalidProviderOutputError, InvalidRequestError, AIError, LLMEvent, LLMResponse, ResponseFold, Message, LanguageModel, SystemPart, ToolChoice, ToolEntry, Usage, withPartial, } from "./schema/index.js";
|
|
5
5
|
import { tool } from "./tool.js";
|
|
6
6
|
import { runTools } from "./tool-runtime.js";
|
|
7
7
|
import { addUsage, nextRequest, pendingCalls } from "./loop.js";
|
|
@@ -23,16 +23,16 @@ export { collect } from "./loop.js";
|
|
|
23
23
|
* With `index`, the step's `step-start` and `step-finish` carry it.
|
|
24
24
|
*/
|
|
25
25
|
const stepStream = (request, index, options, after) => Stream.suspend(() => {
|
|
26
|
-
let state =
|
|
26
|
+
let state = ResponseFold.empty();
|
|
27
27
|
let finish;
|
|
28
28
|
return LLMClient.stream(request, options).pipe(Stream.map((event) => index !== undefined && (event.type === "step-start" || event.type === "step-finish")
|
|
29
29
|
? { ...event, index }
|
|
30
30
|
: event), Stream.tap((event) => Effect.sync(() => {
|
|
31
|
-
state =
|
|
31
|
+
state = ResponseFold.reduce(state, event);
|
|
32
32
|
if (event.type === "finish")
|
|
33
33
|
finish = event;
|
|
34
|
-
})), Stream.filter((event) => event.type !== "finish"), Stream.mapError((error) => withPartial(error,
|
|
35
|
-
const response =
|
|
34
|
+
})), Stream.filter((event) => event.type !== "finish"), Stream.mapError((error) => withPartial(error, ResponseFold.partial(state))), Stream.concat(Stream.suspend(() => {
|
|
35
|
+
const response = ResponseFold.complete(state);
|
|
36
36
|
return response && finish ? after({ response, finish }) : Stream.empty;
|
|
37
37
|
})));
|
|
38
38
|
});
|
package/dist/loop.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect, Stream } from "effect";
|
|
2
|
-
import { AIError, InvalidProviderOutputError, LLMResponse, Message, Usage, LLMEvent, ToolCallPart, ToolChoice, } from "./schema/index.js";
|
|
2
|
+
import { AIError, InvalidProviderOutputError, LLMResponse, ResponseFold, Message, Usage, LLMEvent, ToolCallPart, ToolChoice, } from "./schema/index.js";
|
|
3
3
|
/**
|
|
4
4
|
* Client calls to answer before the next step. A call whose input was not valid JSON carries the error the model
|
|
5
5
|
* gets back, so it can retry. A step is final when it calls `answerTool`, even alongside other tools, or when it hits
|
|
@@ -66,7 +66,7 @@ export function collect(events) {
|
|
|
66
66
|
return Effect.suspend(() => {
|
|
67
67
|
const steps = [];
|
|
68
68
|
const messages = [];
|
|
69
|
-
let step =
|
|
69
|
+
let step = ResponseFold.empty();
|
|
70
70
|
// Between a `step-finish` and the next `step-start`, tool results come from the loop running tools.
|
|
71
71
|
let betweenSteps = false;
|
|
72
72
|
// The last step's assistant message, and its calls with invalid input that the loop may answer.
|
|
@@ -85,7 +85,7 @@ export function collect(events) {
|
|
|
85
85
|
steps.push(response);
|
|
86
86
|
assistant = messages.push(response.message) - 1;
|
|
87
87
|
invalid = step.events.filter(LLMEvent.is.toolInputError).map(invalidCall);
|
|
88
|
-
step =
|
|
88
|
+
step = ResponseFold.empty();
|
|
89
89
|
};
|
|
90
90
|
return Stream.runForEach(events, (event) => Effect.sync(() => {
|
|
91
91
|
if (event.type === "finish") {
|
|
@@ -108,7 +108,7 @@ export function collect(events) {
|
|
|
108
108
|
}
|
|
109
109
|
if (event.type === "step-start")
|
|
110
110
|
betweenSteps = false;
|
|
111
|
-
step =
|
|
111
|
+
step = ResponseFold.reduce(step, event);
|
|
112
112
|
if (event.type !== "step-finish")
|
|
113
113
|
return;
|
|
114
114
|
close(event.reason, event.usage);
|
package/dist/route/client.js
CHANGED
|
@@ -10,7 +10,7 @@ import { normalizeToolHistory } from "../tool-history.js";
|
|
|
10
10
|
import { sanitizeSurrogates } from "../utils/sanitize.js";
|
|
11
11
|
import * as ProviderShared from "../protocols/shared.js";
|
|
12
12
|
import { ToolSchemaProjection } from "../protocols/utils/tool-schema.js";
|
|
13
|
-
import { AIError, CompactionResponse, CompactionCheckpointResponse, AIErrorReason, GenerationOptions, HttpOptions, LLMResponse, LanguageModel, Message, LLMEvent, InvalidProviderOutputError, ProviderConfigurationError, ProviderID, ToolChoice, ToolEntry, mergeGenerationOptions, mergeHttpOptions, mergeProviderOptions, withPartial, } from "../schema/index.js";
|
|
13
|
+
import { AIError, CompactionResponse, CompactionCheckpointResponse, AIErrorReason, GenerationOptions, HttpOptions, LLMResponse, ResponseFold, LanguageModel, Message, LLMEvent, InvalidProviderOutputError, ProviderConfigurationError, ProviderID, ToolChoice, ToolEntry, mergeGenerationOptions, mergeHttpOptions, mergeProviderOptions, withPartial, } from "../schema/index.js";
|
|
14
14
|
const makeRouteLanguageModel = (route, mapped) => {
|
|
15
15
|
const provider = route.provider ?? ("provider" in mapped ? mapped.provider : undefined);
|
|
16
16
|
if (!provider)
|
|
@@ -275,12 +275,12 @@ const streamRequestWith = (runtime) => (request, options) => Stream.unwrap(Effec
|
|
|
275
275
|
return compiled.route.streamPrepared(compiled.prepared, compiled.request, runtime, options);
|
|
276
276
|
}));
|
|
277
277
|
export const generateWith = (stream) => Effect.fn("LLM.generate")(function* (request, options) {
|
|
278
|
-
let state =
|
|
279
|
-
yield* stream(request, options).pipe(Stream.runForEach((event) => Effect.sync(() => (state =
|
|
280
|
-
const response =
|
|
278
|
+
let state = ResponseFold.empty();
|
|
279
|
+
yield* stream(request, options).pipe(Stream.runForEach((event) => Effect.sync(() => (state = ResponseFold.reduce(state, event)))), Effect.mapError((error) => withPartial(error, ResponseFold.partial(state))));
|
|
280
|
+
const response = ResponseFold.complete(state);
|
|
281
281
|
if (response)
|
|
282
282
|
return response;
|
|
283
|
-
return yield* withPartial(incompleteStreamError(`${request.model.provider}/${request.model.route.id}`),
|
|
283
|
+
return yield* withPartial(incompleteStreamError(`${request.model.provider}/${request.model.route.id}`), ResponseFold.partial(state));
|
|
284
284
|
});
|
|
285
285
|
export function stream(request, options) {
|
|
286
286
|
return Stream.unwrap(Effect.gen(function* () {
|
package/dist/schema/events.d.ts
CHANGED
|
@@ -4462,7 +4462,7 @@ interface ToolInputAssembly {
|
|
|
4462
4462
|
readonly providerMetadata?: ProviderMetadata;
|
|
4463
4463
|
}
|
|
4464
4464
|
interface ResponseState {
|
|
4465
|
-
readonly events:
|
|
4465
|
+
readonly events: LLMEvent[];
|
|
4466
4466
|
readonly message: Message;
|
|
4467
4467
|
readonly usage?: Usage;
|
|
4468
4468
|
readonly finishReason?: FinishReasonDetails;
|
|
@@ -8304,22 +8304,28 @@ export declare class LLMResponse extends LLMResponse_base {
|
|
|
8304
8304
|
} | undefined;
|
|
8305
8305
|
}[];
|
|
8306
8306
|
}
|
|
8307
|
-
|
|
8307
|
+
/**
|
|
8308
|
+
* Internal fold that assembles one provider attempt. Not exported from the package.
|
|
8309
|
+
* A state has a single owner: `reduce` appends to the events buffer in place, so earlier states must not be reused.
|
|
8310
|
+
*/
|
|
8311
|
+
export declare namespace ResponseFold {
|
|
8308
8312
|
type State = ResponseState;
|
|
8309
|
-
|
|
8310
|
-
readonly events: ReadonlyArray<LLMEvent>;
|
|
8311
|
-
readonly usage?: Usage;
|
|
8312
|
-
};
|
|
8313
|
-
/** Initial reducer state for assembling one provider attempt. */
|
|
8313
|
+
/** Initial state with a fresh events buffer. */
|
|
8314
8314
|
const empty: typeof emptyResponseState;
|
|
8315
|
-
/**
|
|
8315
|
+
/** Fold one provider-neutral event into the state. */
|
|
8316
8316
|
const reduce: typeof reduceResponseState;
|
|
8317
|
-
/** Return a completed response only after the terminal finish. */
|
|
8317
|
+
/** Return a completed response only after the terminal finish. The response gets its own copy of the events. */
|
|
8318
8318
|
const complete: (state: State) => LLMResponse | undefined;
|
|
8319
|
-
/** Convenience reducer for callers that already have a collected event list. */
|
|
8320
|
-
const fromEvents: (events: ReadonlyArray<LLMEvent>) => LLMResponse | undefined;
|
|
8321
8319
|
/** Output that arrived before a failure, or `undefined` if only `step-start` did; an unfinished response reports `error`. */
|
|
8322
8320
|
const partial: (state: State) => LLMResponse | undefined;
|
|
8321
|
+
}
|
|
8322
|
+
export declare namespace LLMResponse {
|
|
8323
|
+
type Output = LLMResponse | {
|
|
8324
|
+
readonly events: ReadonlyArray<LLMEvent>;
|
|
8325
|
+
readonly usage?: Usage;
|
|
8326
|
+
};
|
|
8327
|
+
/** Assemble a response from a collected event list, or `undefined` if it has no terminal finish. */
|
|
8328
|
+
const fromEvents: (events: ReadonlyArray<LLMEvent>) => LLMResponse | undefined;
|
|
8323
8329
|
/** Concatenate assistant text from a response or collected event list. */
|
|
8324
8330
|
const text: (response: Output) => string;
|
|
8325
8331
|
/** Return response usage, falling back to the latest usage-bearing event. */
|
package/dist/schema/events.js
CHANGED
|
@@ -348,18 +348,16 @@ const emptyResponseState = () => ({
|
|
|
348
348
|
toolInputs: {},
|
|
349
349
|
});
|
|
350
350
|
const appendEvent = (state, event) => {
|
|
351
|
-
|
|
351
|
+
state.events.push(event);
|
|
352
352
|
if (LLMEvent.is.finish(event)) {
|
|
353
353
|
return {
|
|
354
354
|
...state,
|
|
355
|
-
events,
|
|
356
355
|
usage: event.usage ?? state.usage,
|
|
357
356
|
finishReason: event.reason,
|
|
358
357
|
};
|
|
359
358
|
}
|
|
360
359
|
return {
|
|
361
360
|
...state,
|
|
362
|
-
events,
|
|
363
361
|
usage: "usage" in event && event.usage !== undefined ? event.usage : state.usage,
|
|
364
362
|
};
|
|
365
363
|
};
|
|
@@ -520,13 +518,18 @@ export class LLMResponse extends Schema.Class("LLM.Response")({
|
|
|
520
518
|
return this.events.filter(LLMEvent.is.toolCall);
|
|
521
519
|
}
|
|
522
520
|
}
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
521
|
+
/**
|
|
522
|
+
* Internal fold that assembles one provider attempt. Not exported from the package.
|
|
523
|
+
* A state has a single owner: `reduce` appends to the events buffer in place, so earlier states must not be reused.
|
|
524
|
+
*/
|
|
525
|
+
export var ResponseFold;
|
|
526
|
+
(function (ResponseFold) {
|
|
527
|
+
/** Initial state with a fresh events buffer. */
|
|
528
|
+
ResponseFold.empty = emptyResponseState;
|
|
529
|
+
/** Fold one provider-neutral event into the state. */
|
|
530
|
+
ResponseFold.reduce = reduceResponseState;
|
|
531
|
+
/** Return a completed response only after the terminal finish. The response gets its own copy of the events. */
|
|
532
|
+
ResponseFold.complete = (state) => state.finishReason === undefined
|
|
530
533
|
? undefined
|
|
531
534
|
: new LLMResponse({
|
|
532
535
|
message: state.message,
|
|
@@ -534,12 +537,14 @@ export class LLMResponse extends Schema.Class("LLM.Response")({
|
|
|
534
537
|
usage: state.usage,
|
|
535
538
|
finishReason: state.finishReason,
|
|
536
539
|
});
|
|
537
|
-
/** Convenience reducer for callers that already have a collected event list. */
|
|
538
|
-
LLMResponse.fromEvents = (events) => LLMResponse.complete(events.reduce(LLMResponse.reduce, LLMResponse.empty()));
|
|
539
540
|
/** Output that arrived before a failure, or `undefined` if only `step-start` did; an unfinished response reports `error`. */
|
|
540
|
-
|
|
541
|
+
ResponseFold.partial = (state) => state.events.every(LLMEvent.is.stepStart)
|
|
541
542
|
? undefined
|
|
542
|
-
:
|
|
543
|
+
: ResponseFold.complete({ ...state, finishReason: state.finishReason ?? { normalized: "error" } });
|
|
544
|
+
})(ResponseFold || (ResponseFold = {}));
|
|
545
|
+
(function (LLMResponse) {
|
|
546
|
+
/** Assemble a response from a collected event list, or `undefined` if it has no terminal finish. */
|
|
547
|
+
LLMResponse.fromEvents = (events) => ResponseFold.complete(events.reduce(ResponseFold.reduce, ResponseFold.empty()));
|
|
543
548
|
/** Concatenate assistant text from a response or collected event list. */
|
|
544
549
|
LLMResponse.text = (response) => responseText(response.events);
|
|
545
550
|
/** Return response usage, falling back to the latest usage-bearing event. */
|
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-
|
|
3
|
+
"version": "0.0.0-dev-522",
|
|
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-
|
|
523
|
+
"@opencode/http-recorder": "0.0.0-dev-522",
|
|
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-
|
|
533
|
+
"@opencode/schema": "0.0.0-dev-522",
|
|
534
534
|
"aws4fetch": "1.0.20",
|
|
535
535
|
"effect": "4.1.0",
|
|
536
536
|
"google-auth-library": "10.5.0"
|