@restatedev/agents 0.11.1 → 0.12.0
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 +189 -808
- package/dist/pi/factory.d.ts +73 -0
- package/dist/pi/factory.d.ts.map +1 -0
- package/dist/pi/factory.js +145 -0
- package/dist/pi/factory.js.map +1 -0
- package/dist/pi/index.d.ts +5 -129
- package/dist/pi/index.d.ts.map +1 -1
- package/dist/pi/index.js +31 -571
- package/dist/pi/index.js.map +1 -1
- package/dist/pi/keepalive.d.ts +3 -16
- package/dist/pi/keepalive.d.ts.map +1 -1
- package/dist/pi/keepalive.js +38 -41
- package/dist/pi/keepalive.js.map +1 -1
- package/dist/pi/left.d.ts +16 -0
- package/dist/pi/left.d.ts.map +1 -0
- package/dist/pi/left.js +23 -0
- package/dist/pi/left.js.map +1 -0
- package/dist/pi/storage.d.ts +19 -76
- package/dist/pi/storage.d.ts.map +1 -1
- package/dist/pi/storage.js +50 -368
- package/dist/pi/storage.js.map +1 -1
- package/dist/pi/tools.d.ts +12 -0
- package/dist/pi/tools.d.ts.map +1 -0
- package/dist/pi/tools.js +70 -0
- package/dist/pi/tools.js.map +1 -0
- package/package.json +11 -254
- package/src/pi/factory.ts +231 -0
- package/src/pi/index.ts +31 -759
- package/src/pi/keepalive.ts +46 -56
- package/src/pi/left.ts +52 -0
- package/src/pi/storage.ts +75 -409
- package/src/pi/tools.ts +78 -0
- package/dist/agentlib/agent.d.ts +0 -32
- package/dist/agentlib/agent.d.ts.map +0 -1
- package/dist/agentlib/agent.js +0 -174
- package/dist/agentlib/agent.js.map +0 -1
- package/dist/agentlib/index.d.ts +0 -5
- package/dist/agentlib/index.d.ts.map +0 -1
- package/dist/agentlib/index.js +0 -15
- package/dist/agentlib/index.js.map +0 -1
- package/dist/agentlib/messages.d.ts +0 -39
- package/dist/agentlib/messages.d.ts.map +0 -1
- package/dist/agentlib/messages.js +0 -177
- package/dist/agentlib/messages.js.map +0 -1
- package/dist/agentlib/model.d.ts +0 -31
- package/dist/agentlib/model.d.ts.map +0 -1
- package/dist/agentlib/model.js +0 -140
- package/dist/agentlib/model.js.map +0 -1
- package/dist/agentlib/options.d.ts +0 -58
- package/dist/agentlib/options.d.ts.map +0 -1
- package/dist/agentlib/options.js +0 -2
- package/dist/agentlib/options.js.map +0 -1
- package/dist/agentlib/runtime.d.ts +0 -37
- package/dist/agentlib/runtime.d.ts.map +0 -1
- package/dist/agentlib/runtime.js +0 -216
- package/dist/agentlib/runtime.js.map +0 -1
- package/dist/agentlib/state.d.ts +0 -114
- package/dist/agentlib/state.d.ts.map +0 -1
- package/dist/agentlib/state.js +0 -52
- package/dist/agentlib/state.js.map +0 -1
- package/dist/agentlib/steering.d.ts +0 -23
- package/dist/agentlib/steering.d.ts.map +0 -1
- package/dist/agentlib/steering.js +0 -48
- package/dist/agentlib/steering.js.map +0 -1
- package/dist/agentlib/tools.d.ts +0 -33
- package/dist/agentlib/tools.d.ts.map +0 -1
- package/dist/agentlib/tools.js +0 -245
- package/dist/agentlib/tools.js.map +0 -1
- package/dist/agentlib/turn.d.ts +0 -82
- package/dist/agentlib/turn.d.ts.map +0 -1
- package/dist/agentlib/turn.js +0 -253
- package/dist/agentlib/turn.js.map +0 -1
- package/dist/calls.d.ts +0 -90
- package/dist/calls.d.ts.map +0 -1
- package/dist/calls.js +0 -235
- package/dist/calls.js.map +0 -1
- package/dist/claude/index.d.ts +0 -106
- package/dist/claude/index.d.ts.map +0 -1
- package/dist/claude/index.js +0 -386
- package/dist/claude/index.js.map +0 -1
- package/dist/client.d.ts +0 -90
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -88
- package/dist/client.js.map +0 -1
- package/dist/common.d.ts +0 -410
- package/dist/common.d.ts.map +0 -1
- package/dist/common.js +0 -563
- package/dist/common.js.map +0 -1
- package/dist/companion.d.ts +0 -286
- package/dist/companion.d.ts.map +0 -1
- package/dist/companion.js +0 -463
- package/dist/companion.js.map +0 -1
- package/dist/conversation.d.ts +0 -576
- package/dist/conversation.d.ts.map +0 -1
- package/dist/conversation.js +0 -1476
- package/dist/conversation.js.map +0 -1
- package/dist/deepseek/barriers.d.ts +0 -11
- package/dist/deepseek/barriers.d.ts.map +0 -1
- package/dist/deepseek/barriers.js +0 -36
- package/dist/deepseek/barriers.js.map +0 -1
- package/dist/deepseek/index.d.ts +0 -97
- package/dist/deepseek/index.d.ts.map +0 -1
- package/dist/deepseek/index.js +0 -603
- package/dist/deepseek/index.js.map +0 -1
- package/dist/deepseek/persistence.d.ts +0 -83
- package/dist/deepseek/persistence.d.ts.map +0 -1
- package/dist/deepseek/persistence.js +0 -262
- package/dist/deepseek/persistence.js.map +0 -1
- package/dist/index.d.ts +0 -14
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -17
- package/dist/index.js.map +0 -1
- package/dist/langgraph/checkpointer.d.ts +0 -52
- package/dist/langgraph/checkpointer.d.ts.map +0 -1
- package/dist/langgraph/checkpointer.js +0 -477
- package/dist/langgraph/checkpointer.js.map +0 -1
- package/dist/langgraph/context.d.ts +0 -10
- package/dist/langgraph/context.d.ts.map +0 -1
- package/dist/langgraph/context.js +0 -21
- package/dist/langgraph/context.js.map +0 -1
- package/dist/langgraph/errors.d.ts +0 -16
- package/dist/langgraph/errors.d.ts.map +0 -1
- package/dist/langgraph/errors.js +0 -30
- package/dist/langgraph/errors.js.map +0 -1
- package/dist/langgraph/graph.d.ts +0 -25
- package/dist/langgraph/graph.d.ts.map +0 -1
- package/dist/langgraph/graph.js +0 -378
- package/dist/langgraph/graph.js.map +0 -1
- package/dist/langgraph/index.d.ts +0 -119
- package/dist/langgraph/index.d.ts.map +0 -1
- package/dist/langgraph/index.js +0 -325
- package/dist/langgraph/index.js.map +0 -1
- package/dist/langgraph/keepalive.d.ts +0 -16
- package/dist/langgraph/keepalive.d.ts.map +0 -1
- package/dist/langgraph/keepalive.js +0 -51
- package/dist/langgraph/keepalive.js.map +0 -1
- package/dist/langgraph/nodes.d.ts +0 -71
- package/dist/langgraph/nodes.d.ts.map +0 -1
- package/dist/langgraph/nodes.js +0 -148
- package/dist/langgraph/nodes.js.map +0 -1
- package/dist/messages.d.ts +0 -18
- package/dist/messages.d.ts.map +0 -1
- package/dist/messages.js +0 -182
- package/dist/messages.js.map +0 -1
- package/dist/object.d.ts +0 -213
- package/dist/object.d.ts.map +0 -1
- package/dist/object.js +0 -309
- package/dist/object.js.map +0 -1
- package/dist/opencode/db.d.ts +0 -81
- package/dist/opencode/db.d.ts.map +0 -1
- package/dist/opencode/db.js +0 -249
- package/dist/opencode/db.js.map +0 -1
- package/dist/opencode/index.d.ts +0 -184
- package/dist/opencode/index.d.ts.map +0 -1
- package/dist/opencode/index.js +0 -1079
- package/dist/opencode/index.js.map +0 -1
- package/dist/output.d.ts +0 -25
- package/dist/output.d.ts.map +0 -1
- package/dist/output.js +0 -99
- package/dist/output.js.map +0 -1
- package/dist/pi/commits.d.ts +0 -6
- package/dist/pi/commits.d.ts.map +0 -1
- package/dist/pi/commits.js +0 -3
- package/dist/pi/commits.js.map +0 -1
- package/dist/steering.d.ts +0 -32
- package/dist/steering.d.ts.map +0 -1
- package/dist/steering.js +0 -45
- package/dist/steering.js.map +0 -1
- package/dist/tools.d.ts +0 -267
- package/dist/tools.d.ts.map +0 -1
- package/dist/tools.js +0 -305
- package/dist/tools.js.map +0 -1
- package/dist/webhook.d.ts +0 -71
- package/dist/webhook.d.ts.map +0 -1
- package/dist/webhook.js +0 -102
- package/dist/webhook.js.map +0 -1
- package/src/agentlib/README.md +0 -229
- package/src/agentlib/agent.ts +0 -216
- package/src/agentlib/index.ts +0 -16
- package/src/agentlib/messages.ts +0 -207
- package/src/agentlib/model.ts +0 -156
- package/src/agentlib/options.ts +0 -62
- package/src/agentlib/runtime.ts +0 -252
- package/src/agentlib/state.ts +0 -93
- package/src/agentlib/steering.ts +0 -53
- package/src/agentlib/tools.ts +0 -355
- package/src/agentlib/turn.ts +0 -302
- package/src/calls.ts +0 -330
- package/src/claude/README.md +0 -107
- package/src/claude/index.ts +0 -487
- package/src/client.ts +0 -187
- package/src/common.ts +0 -766
- package/src/companion.ts +0 -675
- package/src/conversation.ts +0 -1799
- package/src/deepseek/README.md +0 -202
- package/src/deepseek/barriers.ts +0 -63
- package/src/deepseek/index.ts +0 -740
- package/src/deepseek/persistence.ts +0 -337
- package/src/index.ts +0 -41
- package/src/langgraph/README.md +0 -199
- package/src/langgraph/checkpointer.ts +0 -614
- package/src/langgraph/context.ts +0 -27
- package/src/langgraph/errors.ts +0 -26
- package/src/langgraph/graph.ts +0 -422
- package/src/langgraph/index.ts +0 -500
- package/src/langgraph/keepalive.ts +0 -58
- package/src/langgraph/nodes.ts +0 -247
- package/src/messages.ts +0 -244
- package/src/object.ts +0 -454
- package/src/opencode/README.md +0 -249
- package/src/opencode/db.ts +0 -387
- package/src/opencode/index.ts +0 -1339
- package/src/output.ts +0 -116
- package/src/pi/README.md +0 -174
- package/src/pi/commits.ts +0 -7
- package/src/steering.ts +0 -68
- package/src/tools.ts +0 -515
- package/src/webhook.ts +0 -161
package/src/pi/keepalive.ts
CHANGED
|
@@ -1,18 +1,30 @@
|
|
|
1
|
-
// pi
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
1
|
+
// Wraps pi's model registry (`Harness.open({ models })`) so that each request
|
|
2
|
+
// runs in a `ctx.stoppable` scope and gets the stop signal beside pi's own.
|
|
3
|
+
// pi-durable makes every model request through this registry (`streamSimple`,
|
|
4
|
+
// `fetchDeferred`, `completeSimple`), so the wrapper covers all of them without
|
|
5
|
+
// patching pi-durable. A request the signal aborts ends as aborted. pi-durable
|
|
6
|
+
// retries only a response that ended in an error, and commits an aborted one
|
|
7
|
+
// as the run's failure.
|
|
7
8
|
import type { Models } from '@earendil-works/pi-ai';
|
|
8
9
|
import type { AssistantMessageEventStream } from '@earendil-works/pi-ai';
|
|
9
10
|
import type * as restate from '@restatedev/sessions';
|
|
10
|
-
import {
|
|
11
|
+
import { openScope } from '@restatedev/sessions/internal';
|
|
11
12
|
|
|
12
|
-
/** `Models`
|
|
13
|
+
/** Every `Models` method takes its options third. */
|
|
14
|
+
const OPTIONS = 2;
|
|
15
|
+
|
|
16
|
+
function withSignal(args: unknown[], signal: AbortSignal): unknown[] {
|
|
17
|
+
const options = args[OPTIONS] as { signal?: AbortSignal } | undefined;
|
|
18
|
+
const given = options?.signal;
|
|
19
|
+
const copy = [...args];
|
|
20
|
+
copy[OPTIONS] = { ...options, signal: given ? AbortSignal.any([given, signal]) : signal };
|
|
21
|
+
return copy;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The request is open from the call until the stream ends. */
|
|
13
25
|
const STREAMS = new Set<PropertyKey>(['stream', 'streamSimple', 'streamDeferred']);
|
|
14
26
|
|
|
15
|
-
/**
|
|
27
|
+
/** The request is open until the promise settles. */
|
|
16
28
|
const REQUESTS = new Set<PropertyKey>([
|
|
17
29
|
'complete',
|
|
18
30
|
'completeSimple',
|
|
@@ -23,40 +35,27 @@ const REQUESTS = new Set<PropertyKey>([
|
|
|
23
35
|
]);
|
|
24
36
|
|
|
25
37
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
38
|
+
* Requests pi-durable makes while it stops a run: `cancelDeferred` cancels the
|
|
39
|
+
* provider's job after a cancellation. The stop signal has already aborted by
|
|
40
|
+
* then, so these run in `ctx.working`, without it, with the caller's options.
|
|
29
41
|
*/
|
|
30
|
-
|
|
31
|
-
/** The request starts: commits are written at once until the returned function is called (once at most). */
|
|
32
|
-
started(): () => void;
|
|
33
|
-
/** Write what is batched. */
|
|
34
|
-
flush(): Promise<void>;
|
|
35
|
-
}
|
|
42
|
+
const STOPPING = new Set<PropertyKey>(['cancelDeferred']);
|
|
36
43
|
|
|
37
44
|
/**
|
|
38
|
-
* `stream
|
|
39
|
-
*
|
|
40
|
-
* event), its consumer leaves it early, or `signal` aborts. Then `ended`
|
|
41
|
-
* is called too: the same events, and no more, so a stream that none of
|
|
42
|
-
* them reaches never ends its effect, and the batching stays off.
|
|
45
|
+
* Keeps `stream` alive until `result()` settles (pi-ai ends an aborted stream
|
|
46
|
+
* with an error event), the consumer stops iterating, or `signal` aborts.
|
|
43
47
|
*/
|
|
44
48
|
function keptStream(
|
|
45
|
-
|
|
49
|
+
alive: { release(): void },
|
|
46
50
|
stream: AssistantMessageEventStream,
|
|
47
|
-
signal: AbortSignal
|
|
48
|
-
ended: () => void,
|
|
51
|
+
signal: AbortSignal,
|
|
49
52
|
): AssistantMessageEventStream {
|
|
50
|
-
const
|
|
51
|
-
const release = () => {
|
|
52
|
-
alive();
|
|
53
|
-
ended();
|
|
54
|
-
};
|
|
53
|
+
const release = () => alive.release();
|
|
55
54
|
stream.result().then(release, release);
|
|
56
|
-
signal
|
|
57
|
-
if (signal
|
|
55
|
+
signal.addEventListener('abort', release, { once: true });
|
|
56
|
+
if (signal.aborted) release();
|
|
58
57
|
const iterate = stream[Symbol.asyncIterator].bind(stream);
|
|
59
|
-
// A consumer that stops iterating (a `break`, a throw) abandons the stream
|
|
58
|
+
// A consumer that stops iterating (a `break`, a throw) abandons the stream.
|
|
60
59
|
stream[Symbol.asyncIterator] = () => {
|
|
61
60
|
const iterator = iterate();
|
|
62
61
|
return {
|
|
@@ -76,12 +75,10 @@ function keptStream(
|
|
|
76
75
|
}
|
|
77
76
|
|
|
78
77
|
/**
|
|
79
|
-
* `models
|
|
80
|
-
*
|
|
81
|
-
* is started as the stream is made and not waited for (the request has no
|
|
82
|
-
* effect that must not repeat), a promise-returning method's is waited for.
|
|
78
|
+
* Wraps `models` so that each request keeps the attempt of `ctx` alive while
|
|
79
|
+
* it is open and is aborted by the stop signal (except `STOPPING`).
|
|
83
80
|
*/
|
|
84
|
-
export function keptModels<M extends Models>(ctx: restate.SessionContext, models: M
|
|
81
|
+
export function keptModels<M extends Models>(ctx: restate.SessionContext, models: M): M {
|
|
85
82
|
return new Proxy(models, {
|
|
86
83
|
get(target, property) {
|
|
87
84
|
const member: unknown = Reflect.get(target, property, target);
|
|
@@ -89,31 +86,24 @@ export function keptModels<M extends Models>(ctx: restate.SessionContext, models
|
|
|
89
86
|
const method = member.bind(target) as (...args: unknown[]) => unknown;
|
|
90
87
|
if (STREAMS.has(property)) {
|
|
91
88
|
return (...args: unknown[]) => {
|
|
92
|
-
const
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
void effects?.flush().catch(() => {});
|
|
89
|
+
const alive = openScope(ctx);
|
|
90
|
+
const kept = withSignal(args, alive.signal);
|
|
91
|
+
const signal = (kept[OPTIONS] as { signal: AbortSignal }).signal;
|
|
96
92
|
let stream: AssistantMessageEventStream;
|
|
97
93
|
try {
|
|
98
|
-
stream = method(...
|
|
94
|
+
stream = method(...kept) as AssistantMessageEventStream;
|
|
99
95
|
} catch (error) {
|
|
100
|
-
|
|
96
|
+
alive.release();
|
|
101
97
|
throw error;
|
|
102
98
|
}
|
|
103
|
-
return keptStream(
|
|
99
|
+
return keptStream(alive, stream, signal);
|
|
104
100
|
};
|
|
105
101
|
}
|
|
106
102
|
if (REQUESTS.has(property)) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
await effects.flush();
|
|
112
|
-
return await ctx.working(() => method(...args) as Promise<unknown>);
|
|
113
|
-
} finally {
|
|
114
|
-
ended();
|
|
115
|
-
}
|
|
116
|
-
};
|
|
103
|
+
const request = STOPPING.has(property)
|
|
104
|
+
? (args: unknown[]) => ctx.working(() => method(...args) as Promise<unknown>)
|
|
105
|
+
: (args: unknown[]) => ctx.stoppable((signal) => method(...withSignal(args, signal)) as Promise<unknown>);
|
|
106
|
+
return (...args: unknown[]) => request(args);
|
|
117
107
|
}
|
|
118
108
|
return method;
|
|
119
109
|
},
|
package/src/pi/left.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Aborts runs that another invocation left in pi's storage, before this
|
|
2
|
+
// invocation's own work. A run is left when its invocation was cancelled while
|
|
3
|
+
// no work held the stop signal (the attempt had ended, or it was between
|
|
4
|
+
// requests), or crashed while stopping. Restate considers that turn over, but
|
|
5
|
+
// `Harness.open` would dispatch its tasks again and ask the model for a message
|
|
6
|
+
// whose invocation ended. `Conversation.abort()` commits abort marks before its
|
|
7
|
+
// scheduler dispatches, so the run's tasks run their abort handlers, not their
|
|
8
|
+
// progress, and its inputs settle unanswered. The run of an earlier attempt of this invocation is kept:
|
|
9
|
+
// the retry resumes it.
|
|
10
|
+
//
|
|
11
|
+
// Which runs are this invocation's own is not in the request ids, which the
|
|
12
|
+
// caller chooses. Restate runs one handler per key at a time, so every other
|
|
13
|
+
// invocation that wrote pi on the key has ended. Once this check is done, pi's
|
|
14
|
+
// next writing commit records this invocation as the writer (storage.ts), and
|
|
15
|
+
// every run live from then on is its own. So a later attempt that finds itself
|
|
16
|
+
// recorded keeps every live run, and has nothing to record; one that finds
|
|
17
|
+
// another invocation, or none, aborts them: none of them is its own, as it has
|
|
18
|
+
// not written since its check.
|
|
19
|
+
import type { Harness } from '@earendil-works/pi-durable';
|
|
20
|
+
import type * as restate from '@restatedev/sessions';
|
|
21
|
+
import { lastWriter, recordWriter } from './storage.ts';
|
|
22
|
+
|
|
23
|
+
/** The chord context the Harness takes. */
|
|
24
|
+
type Context = Parameters<Harness['inspect']>[0];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Aborts the runs of conversations that hold a live input with a request id,
|
|
28
|
+
* unless this invocation is the one that last wrote pi's storage after this
|
|
29
|
+
* check; otherwise records it as the writer of pi's next writing commit. Call it
|
|
30
|
+
* before this invocation's work (a prompt, a compaction, a wait). An input
|
|
31
|
+
* without a request id is not a prompt, and pi's internal writes are not runs:
|
|
32
|
+
* both are left alone. `db` is the database pi's storage was opened on
|
|
33
|
+
* (`openSqlKvStorage(db)`), by default `ctx.db`.
|
|
34
|
+
*/
|
|
35
|
+
export async function abortLeft(
|
|
36
|
+
ctx: restate.SessionContext,
|
|
37
|
+
harness: Harness,
|
|
38
|
+
context: Context,
|
|
39
|
+
db: restate.Database = ctx.db,
|
|
40
|
+
): Promise<void> {
|
|
41
|
+
if ((await lastWriter(db)) === ctx.request.id) return;
|
|
42
|
+
const { submissions } = await harness.inspect(context);
|
|
43
|
+
const live = new Set(
|
|
44
|
+
submissions.flatMap((s) => (s.type === 'input' && s.requestId !== undefined ? [s.conversationId] : [])),
|
|
45
|
+
);
|
|
46
|
+
const conversations = await Promise.all([...live].map((id) => harness.conversation(id, context)));
|
|
47
|
+
// `abort()` resumes pi's scheduler, which dispatches in a later microtask, and queues its marks' commit before it
|
|
48
|
+
// yields. So every abort is called before any is awaited: the scheduler's first dispatch comes after all the marks,
|
|
49
|
+
// where one conversation's abort awaited alone would let it run another's tasks.
|
|
50
|
+
await Promise.all(conversations.flatMap((conversation) => (conversation ? [conversation.abort(context)] : [])));
|
|
51
|
+
recordWriter(db, ctx.request.id);
|
|
52
|
+
}
|