@restatedev/agents 0.0.0 → 0.1.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/LICENSE +21 -0
- package/README.md +688 -2
- package/dist/calls.d.ts +29 -0
- package/dist/calls.d.ts.map +1 -0
- package/dist/calls.js +91 -0
- package/dist/calls.js.map +1 -0
- package/dist/claude/conversation.d.ts +33 -0
- package/dist/claude/conversation.d.ts.map +1 -0
- package/dist/claude/conversation.js +95 -0
- package/dist/claude/conversation.js.map +1 -0
- package/dist/claude/index.d.ts +78 -0
- package/dist/claude/index.d.ts.map +1 -0
- package/dist/claude/index.js +269 -0
- package/dist/claude/index.js.map +1 -0
- package/dist/client.d.ts +83 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +88 -0
- package/dist/client.js.map +1 -0
- package/dist/common.d.ts +284 -0
- package/dist/common.d.ts.map +1 -0
- package/dist/common.js +475 -0
- package/dist/common.js.map +1 -0
- package/dist/deepseek/index.d.ts +69 -0
- package/dist/deepseek/index.d.ts.map +1 -0
- package/dist/deepseek/index.js +381 -0
- package/dist/deepseek/index.js.map +1 -0
- package/dist/deepseek/persistence.d.ts +61 -0
- package/dist/deepseek/persistence.d.ts.map +1 -0
- package/dist/deepseek/persistence.js +224 -0
- package/dist/deepseek/persistence.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/dist/langgraph/checkpointer.d.ts +29 -0
- package/dist/langgraph/checkpointer.d.ts.map +1 -0
- package/dist/langgraph/checkpointer.js +275 -0
- package/dist/langgraph/checkpointer.js.map +1 -0
- package/dist/langgraph/index.d.ts +67 -0
- package/dist/langgraph/index.d.ts.map +1 -0
- package/dist/langgraph/index.js +265 -0
- package/dist/langgraph/index.js.map +1 -0
- package/dist/log.d.ts +29 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +145 -0
- package/dist/log.js.map +1 -0
- package/dist/object.d.ts +181 -0
- package/dist/object.d.ts.map +1 -0
- package/dist/object.js +232 -0
- package/dist/object.js.map +1 -0
- package/dist/opencode/db.d.ts +40 -0
- package/dist/opencode/db.d.ts.map +1 -0
- package/dist/opencode/db.js +161 -0
- package/dist/opencode/db.js.map +1 -0
- package/dist/opencode/index.d.ts +101 -0
- package/dist/opencode/index.d.ts.map +1 -0
- package/dist/opencode/index.js +464 -0
- package/dist/opencode/index.js.map +1 -0
- package/dist/output.d.ts +17 -0
- package/dist/output.d.ts.map +1 -0
- package/dist/output.js +81 -0
- package/dist/output.js.map +1 -0
- package/dist/pi/index.d.ts +90 -0
- package/dist/pi/index.d.ts.map +1 -0
- package/dist/pi/index.js +313 -0
- package/dist/pi/index.js.map +1 -0
- package/dist/pi/storage.d.ts +5 -0
- package/dist/pi/storage.d.ts.map +1 -0
- package/dist/pi/storage.js +41 -0
- package/dist/pi/storage.js.map +1 -0
- package/dist/signals.d.ts +195 -0
- package/dist/signals.d.ts.map +1 -0
- package/dist/signals.js +258 -0
- package/dist/signals.js.map +1 -0
- package/dist/tools.d.ts +158 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +211 -0
- package/dist/tools.js.map +1 -0
- package/dist/webhook.d.ts +73 -0
- package/dist/webhook.d.ts.map +1 -0
- package/dist/webhook.js +104 -0
- package/dist/webhook.js.map +1 -0
- package/package.json +218 -5
- package/src/calls.ts +138 -0
- package/src/claude/README.md +84 -0
- package/src/claude/conversation.ts +114 -0
- package/src/claude/index.ts +364 -0
- package/src/client.ts +179 -0
- package/src/common.ts +593 -0
- package/src/deepseek/README.md +110 -0
- package/src/deepseek/index.ts +487 -0
- package/src/deepseek/persistence.ts +293 -0
- package/src/index.ts +60 -0
- package/src/langgraph/README.md +87 -0
- package/src/langgraph/checkpointer.ts +364 -0
- package/src/langgraph/index.ts +349 -0
- package/src/log.ts +193 -0
- package/src/object.ts +343 -0
- package/src/opencode/README.md +125 -0
- package/src/opencode/db.ts +223 -0
- package/src/opencode/index.ts +595 -0
- package/src/output.ts +95 -0
- package/src/pi/README.md +105 -0
- package/src/pi/index.ts +402 -0
- package/src/pi/storage.ts +65 -0
- package/src/signals.ts +426 -0
- package/src/tools.ts +343 -0
- package/src/webhook.ts +163 -0
package/dist/object.d.ts
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import * as restate from '@restatedev/sessions';
|
|
2
|
+
import { type Messages, type MessagesRequest, type Turn } from './common.ts';
|
|
3
|
+
import { type Notifications, type Resolved, type Signal, type Steered, type Waiting, type Watch } from './signals.ts';
|
|
4
|
+
/** What an object is doing: the message it answers, if any, and what its tools wait for. */
|
|
5
|
+
export interface Status {
|
|
6
|
+
/** The message being answered (by a `chat` handler), or null. */
|
|
7
|
+
answering: Turn | null;
|
|
8
|
+
/** The waits of its tools, oldest first (see `pending`). */
|
|
9
|
+
waiting: Waiting[];
|
|
10
|
+
}
|
|
11
|
+
/** What `stop` did: the invocation of the message it stopped, or null if none was answered. */
|
|
12
|
+
export interface Stopped {
|
|
13
|
+
stopped: string | null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The views every object here has, beside its own: `signal`, which resolves
|
|
17
|
+
* a wait of the object's tools (see `signal` in tools.ts), `pending`, the
|
|
18
|
+
* waits, `status`, what the object is doing, `stop`, which stops the message
|
|
19
|
+
* it answers, `steer`, which adds to it, and `notifications` and `watch`,
|
|
20
|
+
* the revision of its conversation, and a wait for a newer one (an agent's
|
|
21
|
+
* has `clear` and `sync` too: {@link followed}). Each asks the object's
|
|
22
|
+
* `signals` companion, which knows what the object does: they run while the
|
|
23
|
+
* object's handler works.
|
|
24
|
+
*/
|
|
25
|
+
declare const BUILT_IN: {
|
|
26
|
+
signal: (ctx: restate.ViewContext, signal: Signal) => Promise<Resolved>;
|
|
27
|
+
pending: (ctx: restate.ViewContext) => Promise<Waiting[]>;
|
|
28
|
+
status: (ctx: restate.ViewContext) => Promise<Status>;
|
|
29
|
+
/** Cancel the invocation of the message being answered (`status`'s), as Restate's admin API cancels one. */
|
|
30
|
+
stop: (ctx: restate.ViewContext) => Promise<Stopped>;
|
|
31
|
+
/**
|
|
32
|
+
* Add `text` to the message being answered (`status`'s), such as "Only
|
|
33
|
+
* Monday, please": the agent takes it at its next tool call, as its
|
|
34
|
+
* framework steers a turn. A turn that ends first answers it as a message
|
|
35
|
+
* of its own, as `chat` does when no message is answered.
|
|
36
|
+
*/
|
|
37
|
+
steer: {
|
|
38
|
+
input: restate.Serde<string>;
|
|
39
|
+
accept: string;
|
|
40
|
+
handler: (ctx: restate.ViewContext, text: string) => Promise<Steered>;
|
|
41
|
+
};
|
|
42
|
+
/** The revision of the conversation: each change of what `messages` and `status` give increases it. */
|
|
43
|
+
notifications: (ctx: restate.ViewContext) => Promise<Notifications>;
|
|
44
|
+
/**
|
|
45
|
+
* The revision, once there is one newer than `revision`, or at the timeout (50 s by
|
|
46
|
+
* default, at most 60 s), whichever comes first: a client then reads what
|
|
47
|
+
* changed. It waits in Restate, holding no process; a client that retries
|
|
48
|
+
* it with the same idempotency key joins the same wait.
|
|
49
|
+
*/
|
|
50
|
+
watch: {
|
|
51
|
+
inactivityTimeout: {
|
|
52
|
+
minutes: number;
|
|
53
|
+
};
|
|
54
|
+
handler: (ctx: restate.ViewContext, watch: Watch) => Promise<Notifications>;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* The views that agents (an object with a `messages` view) have beside
|
|
59
|
+
* those of every object: `clear`, which forgets the conversation; and
|
|
60
|
+
* `sync`, the conversation since `cursor` (by default, all of it), what the
|
|
61
|
+
* agent is doing, and the revision, in one request; with `revision`, once
|
|
62
|
+
* there is a newer one, or at the timeout. A client follows the
|
|
63
|
+
* conversation by sending each answer's `revision` and `cursor` back, at
|
|
64
|
+
* once while it has `more`.
|
|
65
|
+
*/
|
|
66
|
+
declare const followed: (messages: (ctx: restate.ViewContext, request: MessagesRequest) => Promise<Messages>) => {
|
|
67
|
+
/**
|
|
68
|
+
* Forget the conversation: stop the message being answered (as `stop`),
|
|
69
|
+
* then empty the key's database, and what its `signals` hold (waits, kept
|
|
70
|
+
* signals, steered messages). The log's ids and cursors go on (see
|
|
71
|
+
* log.ts), so a follower reads on, and drops the messages before `first`.
|
|
72
|
+
* A message sent before it is answered first, then forgotten too; one sent
|
|
73
|
+
* after starts anew.
|
|
74
|
+
*/
|
|
75
|
+
clear: {
|
|
76
|
+
inactivityTimeout: {
|
|
77
|
+
minutes: number;
|
|
78
|
+
};
|
|
79
|
+
handler: (ctx: restate.ViewContext) => Promise<Stopped>;
|
|
80
|
+
};
|
|
81
|
+
sync: {
|
|
82
|
+
inactivityTimeout: {
|
|
83
|
+
minutes: number;
|
|
84
|
+
};
|
|
85
|
+
handler: (ctx: restate.ViewContext, { cursor, revision, timeoutSeconds, limit }?: SyncRequest) => Promise<Synced>;
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* The agents' handlers that only their own views call (Restate's ingress
|
|
90
|
+
* refuses them), and their types leave out: `erase`, `clear`'s work, once
|
|
91
|
+
* the handlers before it are done. It empties the key's database and its
|
|
92
|
+
* signals', and the log goes on where it got to (see log.ts).
|
|
93
|
+
*/
|
|
94
|
+
declare const INTERNAL: {
|
|
95
|
+
erase: {
|
|
96
|
+
ingressPrivate: boolean;
|
|
97
|
+
handler: (ctx: restate.SessionContext) => Promise<void>;
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* What `sync` reads: what is new or changed since `cursor` (default 0, all
|
|
102
|
+
* of it), at most `limit` messages (default 100); once the revision is past
|
|
103
|
+
* `revision`, if given, or at `timeoutSeconds` (default 50, at most 60).
|
|
104
|
+
*/
|
|
105
|
+
export interface SyncRequest {
|
|
106
|
+
cursor?: number;
|
|
107
|
+
revision?: number;
|
|
108
|
+
timeoutSeconds?: number;
|
|
109
|
+
limit?: number;
|
|
110
|
+
}
|
|
111
|
+
/** What `sync` answers: the messages since the cursor, as `messages` gives them, the status and the revision. */
|
|
112
|
+
export interface Synced extends Messages {
|
|
113
|
+
revision: number;
|
|
114
|
+
status: Status;
|
|
115
|
+
}
|
|
116
|
+
/** An object's (or agent's) name, its description, and the tables of your own in its database, if any (see `sessionObject`). */
|
|
117
|
+
export interface Definition<P extends string> {
|
|
118
|
+
name: P;
|
|
119
|
+
/** What it is: for the UI, and for the model of a parent that hands it tasks (`subagent`). */
|
|
120
|
+
description?: string;
|
|
121
|
+
schema?: readonly restate.Step[];
|
|
122
|
+
/**
|
|
123
|
+
* Whether it works unwatched, such as a subagent: its turn calls its
|
|
124
|
+
* `signals` only at its end, not at each step, so a client that follows it
|
|
125
|
+
* sees each turn once it ended, and a steered message waits for the turn's
|
|
126
|
+
* end. `messages` has every turn as before.
|
|
127
|
+
*/
|
|
128
|
+
background?: boolean;
|
|
129
|
+
}
|
|
130
|
+
/** Each entry's function, as clients see it; none for entries left out. */
|
|
131
|
+
type Functions<H> = string extends keyof H ? unknown : {
|
|
132
|
+
[K in keyof H]: H[K] extends {
|
|
133
|
+
handler: infer F;
|
|
134
|
+
} ? F : H[K];
|
|
135
|
+
};
|
|
136
|
+
/** The names of the handlers and views every object (or agent) has, which its own cannot take. */
|
|
137
|
+
type Reserved = {
|
|
138
|
+
[name in keyof typeof BUILT_IN | keyof ReturnType<typeof followed> | keyof typeof INTERNAL]?: 'a handler every object has: name yours otherwise';
|
|
139
|
+
};
|
|
140
|
+
/** What an object with `handlers` and `views` has: those of every object, an agent's `clear` and `sync`, and its own. */
|
|
141
|
+
type With<H, V> = Functions<typeof BUILT_IN> & (V extends {
|
|
142
|
+
messages: unknown;
|
|
143
|
+
} ? Functions<ReturnType<typeof followed>> : unknown) & Functions<H> & Functions<V>;
|
|
144
|
+
/**
|
|
145
|
+
* A Restate virtual object with a SQL database per key, `ctx.db`: a
|
|
146
|
+
* `restate.sessionObject` of @restatedev/sessions, set up for agents.
|
|
147
|
+
*
|
|
148
|
+
* const notes = sessionObject({
|
|
149
|
+
* name: 'notes',
|
|
150
|
+
* handlers: {
|
|
151
|
+
* add: (ctx, note: string) => ctx.db.execute('INSERT INTO notes VALUES (?)', [note]),
|
|
152
|
+
* },
|
|
153
|
+
* });
|
|
154
|
+
*
|
|
155
|
+
* Besides its own handlers and views, it has the views `status`, what it is
|
|
156
|
+
* doing (the message it answers, and what its tools wait for), `stop`,
|
|
157
|
+
* which stops that message, `steer`, which adds to it, `pending`, the waits
|
|
158
|
+
* of its tools (`signal()`), `signal({ id, value })`, which resolves one,
|
|
159
|
+
* and `notifications` and `watch({ revision })`, to follow its
|
|
160
|
+
* conversation. An agent, an object with a `messages` view (such as
|
|
161
|
+
* `claude.messages()`), has `clear` too, which forgets its conversation
|
|
162
|
+
* (its database too), and `sync`, which `follow()` of @restatedev/agents/client
|
|
163
|
+
* reads.
|
|
164
|
+
*
|
|
165
|
+
* `schema`, optional: tables of your own, beside the agent's (which are its
|
|
166
|
+
* own business), made in each key's database as a session object makes
|
|
167
|
+
* them. Any name is yours but those starting with `restate_`, the agent's
|
|
168
|
+
* (`restate_pi_tasks`, `restate_claude_messages`, `restate_agent_log`...);
|
|
169
|
+
* OpenCode's tables are in a database of their own.
|
|
170
|
+
*
|
|
171
|
+
* `options` and `metadata` add to (or replace) the ones set here, such as
|
|
172
|
+
* `options: { retryPolicy: ... }`.
|
|
173
|
+
*/
|
|
174
|
+
export declare const sessionObject: <P extends string, H extends Record<string, restate.Entry<restate.SessionContext>>, V extends Record<string, restate.Entry<restate.ViewContext>>>(definition: Definition<P> & {
|
|
175
|
+
handlers?: H & Reserved;
|
|
176
|
+
views?: V & Reserved;
|
|
177
|
+
metadata?: Record<string, string>;
|
|
178
|
+
options?: restate.ObjectOptions;
|
|
179
|
+
}) => restate.VirtualObjectDefinition<P, With<H, V>>;
|
|
180
|
+
export {};
|
|
181
|
+
//# sourceMappingURL=object.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceR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
import { reset } from '@restatedev/sessions/internal';
|
|
4
|
+
import { cleared, onClear, inBackground, messageSerde, refusal, } from './common.js';
|
|
5
|
+
import * as log from './log.js';
|
|
6
|
+
import { agentOf, signals, signalsOf, withCompanion, } from './signals.js';
|
|
7
|
+
/**
|
|
8
|
+
* The options of every object here, beside a session object's: retries for
|
|
9
|
+
* weeks, since each attempt resumes from the database: soon after a crash,
|
|
10
|
+
* then twice as long each time, up to 10 seconds apart (a provider's rate
|
|
11
|
+
* limit, say, is not asked again twice a second).
|
|
12
|
+
*/
|
|
13
|
+
const OPTIONS = {
|
|
14
|
+
retryPolicy: { initialInterval: 50, exponentiationFactor: 2, maxInterval: 10_000, maxAttempts: 100_000 },
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* A call of the companion's of `ctx`; with `step`, a change, keyed by the
|
|
18
|
+
* request and `step`: a restarted view joins the same call. A read has no
|
|
19
|
+
* key: asked again, it answers again, and Restate keeps nothing of it.
|
|
20
|
+
*/
|
|
21
|
+
const companion = (ctx, step) => ctx.rpc(signals, signalsOf(ctx), step === undefined ? {} : { idempotencyKey: `${ctx.request.id}:${step}` });
|
|
22
|
+
/**
|
|
23
|
+
* Cancel the invocation of the message being answered, if any, and say
|
|
24
|
+
* which. The companion's `status` says which, with a key of the request and
|
|
25
|
+
* `step`: a restarted view gets the same answer, and cancels the same
|
|
26
|
+
* invocation again (which does nothing), not the message that started once
|
|
27
|
+
* it was cancelled.
|
|
28
|
+
*/
|
|
29
|
+
async function stopped(ctx, step) {
|
|
30
|
+
const turn = (await companion(ctx, step).status()).answering;
|
|
31
|
+
if (turn)
|
|
32
|
+
ctx.cancel(turn.invocationId);
|
|
33
|
+
return { stopped: turn?.invocationId ?? null };
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Views that wait on the companion (`watch`, `sync`, up to a minute) or on
|
|
37
|
+
* the object (`clear`): Restate
|
|
38
|
+
* suspends one that waits past its inactivity timeout, which restarts it,
|
|
39
|
+
* so it is longer than the wait.
|
|
40
|
+
*/
|
|
41
|
+
const WAITS = { inactivityTimeout: { minutes: 5 } };
|
|
42
|
+
/**
|
|
43
|
+
* The views every object here has, beside its own: `signal`, which resolves
|
|
44
|
+
* a wait of the object's tools (see `signal` in tools.ts), `pending`, the
|
|
45
|
+
* waits, `status`, what the object is doing, `stop`, which stops the message
|
|
46
|
+
* it answers, `steer`, which adds to it, and `notifications` and `watch`,
|
|
47
|
+
* the revision of its conversation, and a wait for a newer one (an agent's
|
|
48
|
+
* has `clear` and `sync` too: {@link followed}). Each asks the object's
|
|
49
|
+
* `signals` companion, which knows what the object does: they run while the
|
|
50
|
+
* object's handler works.
|
|
51
|
+
*/
|
|
52
|
+
const BUILT_IN = {
|
|
53
|
+
signal: async (ctx, signal) => companion(ctx, 'signal').resolve(signal),
|
|
54
|
+
pending: async (ctx) => companion(ctx).pending(),
|
|
55
|
+
status: async (ctx) => companion(ctx).status(),
|
|
56
|
+
/** Cancel the invocation of the message being answered (`status`'s), as Restate's admin API cancels one. */
|
|
57
|
+
stop: async (ctx) => stopped(ctx, 'stop'),
|
|
58
|
+
/**
|
|
59
|
+
* Add `text` to the message being answered (`status`'s), such as "Only
|
|
60
|
+
* Monday, please": the agent takes it at its next tool call, as its
|
|
61
|
+
* framework steers a turn. A turn that ends first answers it as a message
|
|
62
|
+
* of its own, as `chat` does when no message is answered.
|
|
63
|
+
*/
|
|
64
|
+
steer: {
|
|
65
|
+
input: messageSerde,
|
|
66
|
+
accept: '*/*',
|
|
67
|
+
handler: async (ctx, text) => companion(ctx, 'steer').steer({ agent: agentOf(ctx), text }),
|
|
68
|
+
},
|
|
69
|
+
/** The revision of the conversation: each change of what `messages` and `status` give increases it. */
|
|
70
|
+
notifications: async (ctx) => companion(ctx).notifications(),
|
|
71
|
+
/**
|
|
72
|
+
* The revision, once there is one newer than `revision`, or at the timeout (50 s by
|
|
73
|
+
* default, at most 60 s), whichever comes first: a client then reads what
|
|
74
|
+
* changed. It waits in Restate, holding no process; a client that retries
|
|
75
|
+
* it with the same idempotency key joins the same wait.
|
|
76
|
+
*/
|
|
77
|
+
watch: {
|
|
78
|
+
...WAITS,
|
|
79
|
+
handler: async (ctx, watch) => companion(ctx, 'watch').watch(watch),
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* The views that agents (an object with a `messages` view) have beside
|
|
84
|
+
* those of every object: `clear`, which forgets the conversation; and
|
|
85
|
+
* `sync`, the conversation since `cursor` (by default, all of it), what the
|
|
86
|
+
* agent is doing, and the revision, in one request; with `revision`, once
|
|
87
|
+
* there is a newer one, or at the timeout. A client follows the
|
|
88
|
+
* conversation by sending each answer's `revision` and `cursor` back, at
|
|
89
|
+
* once while it has `more`.
|
|
90
|
+
*/
|
|
91
|
+
const followed = (messages) => ({
|
|
92
|
+
/**
|
|
93
|
+
* Forget the conversation: stop the message being answered (as `stop`),
|
|
94
|
+
* then empty the key's database, and what its `signals` hold (waits, kept
|
|
95
|
+
* signals, steered messages). The log's ids and cursors go on (see
|
|
96
|
+
* log.ts), so a follower reads on, and drops the messages before `first`.
|
|
97
|
+
* A message sent before it is answered first, then forgotten too; one sent
|
|
98
|
+
* after starts anew.
|
|
99
|
+
*/
|
|
100
|
+
clear: {
|
|
101
|
+
// `erase` waits for the messages sent before it.
|
|
102
|
+
...WAITS,
|
|
103
|
+
handler: async (ctx) => {
|
|
104
|
+
const stop = await stopped(ctx, 'clear');
|
|
105
|
+
await ctx.rpc({
|
|
106
|
+
service: ctx.request.target.service,
|
|
107
|
+
key: ctx.key,
|
|
108
|
+
handler: 'erase',
|
|
109
|
+
idempotencyKey: `${ctx.request.id}:erase`,
|
|
110
|
+
});
|
|
111
|
+
return stop;
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
sync: {
|
|
115
|
+
...WAITS,
|
|
116
|
+
handler: async (ctx, { cursor = 0, revision, timeoutSeconds = 50, limit = 100 } = {}) => {
|
|
117
|
+
if (revision !== undefined)
|
|
118
|
+
await companion(ctx, 'watch').watch({ revision, timeoutSeconds });
|
|
119
|
+
// The revision first: a change after it shows in the next sync. Then
|
|
120
|
+
// what the object does, and its conversation, as they are now.
|
|
121
|
+
const now = await companion(ctx).notifications();
|
|
122
|
+
const status = await companion(ctx).status();
|
|
123
|
+
return { revision: now.revision, status, ...(await messages(ctx, { cursor, limit })) };
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
/**
|
|
128
|
+
* The agents' handlers that only their own views call (Restate's ingress
|
|
129
|
+
* refuses them), and their types leave out: `erase`, `clear`'s work, once
|
|
130
|
+
* the handlers before it are done. It empties the key's database and its
|
|
131
|
+
* signals', and the log goes on where it got to (see log.ts).
|
|
132
|
+
*/
|
|
133
|
+
const INTERNAL = {
|
|
134
|
+
erase: {
|
|
135
|
+
ingressPrivate: true,
|
|
136
|
+
handler: async (ctx) => {
|
|
137
|
+
const start = startAfter(await log.page(ctx.db, { limit: 1 }));
|
|
138
|
+
await ctx.db.transaction(async (tx) => {
|
|
139
|
+
await reset(tx);
|
|
140
|
+
await log.restart(tx, start);
|
|
141
|
+
});
|
|
142
|
+
await cleared(ctx);
|
|
143
|
+
await ctx.rpc(signals, signalsOf(ctx), { idempotencyKey: `${ctx.request.id}:reset` }).reset();
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
/** Where the log starts after `latest`, its latest message: past its ids and cursor. */
|
|
148
|
+
const startAfter = ({ messages: [latest], cursor, first = 0 }) => ({
|
|
149
|
+
seq: cursor,
|
|
150
|
+
id: latest ? latest.id + 1 : first,
|
|
151
|
+
});
|
|
152
|
+
/**
|
|
153
|
+
* A Restate virtual object with a SQL database per key, `ctx.db`: a
|
|
154
|
+
* `restate.sessionObject` of @restatedev/sessions, set up for agents.
|
|
155
|
+
*
|
|
156
|
+
* const notes = sessionObject({
|
|
157
|
+
* name: 'notes',
|
|
158
|
+
* handlers: {
|
|
159
|
+
* add: (ctx, note: string) => ctx.db.execute('INSERT INTO notes VALUES (?)', [note]),
|
|
160
|
+
* },
|
|
161
|
+
* });
|
|
162
|
+
*
|
|
163
|
+
* Besides its own handlers and views, it has the views `status`, what it is
|
|
164
|
+
* doing (the message it answers, and what its tools wait for), `stop`,
|
|
165
|
+
* which stops that message, `steer`, which adds to it, `pending`, the waits
|
|
166
|
+
* of its tools (`signal()`), `signal({ id, value })`, which resolves one,
|
|
167
|
+
* and `notifications` and `watch({ revision })`, to follow its
|
|
168
|
+
* conversation. An agent, an object with a `messages` view (such as
|
|
169
|
+
* `claude.messages()`), has `clear` too, which forgets its conversation
|
|
170
|
+
* (its database too), and `sync`, which `follow()` of @restatedev/agents/client
|
|
171
|
+
* reads.
|
|
172
|
+
*
|
|
173
|
+
* `schema`, optional: tables of your own, beside the agent's (which are its
|
|
174
|
+
* own business), made in each key's database as a session object makes
|
|
175
|
+
* them. Any name is yours but those starting with `restate_`, the agent's
|
|
176
|
+
* (`restate_pi_tasks`, `restate_claude_messages`, `restate_agent_log`...);
|
|
177
|
+
* OpenCode's tables are in a database of their own.
|
|
178
|
+
*
|
|
179
|
+
* `options` and `metadata` add to (or replace) the ones set here, such as
|
|
180
|
+
* `options: { retryPolicy: ... }`.
|
|
181
|
+
*/
|
|
182
|
+
export const sessionObject = (definition) => {
|
|
183
|
+
const { name, description, schema, background, handlers = {}, views = {}, metadata, options } = definition;
|
|
184
|
+
if (background)
|
|
185
|
+
inBackground(name);
|
|
186
|
+
const own = [
|
|
187
|
+
...Object.keys(BUILT_IN),
|
|
188
|
+
...Object.keys(followed(async () => ({}))),
|
|
189
|
+
...Object.keys(INTERNAL),
|
|
190
|
+
];
|
|
191
|
+
if ('messages' in handlers) {
|
|
192
|
+
throw new Error(`${name}: \`messages\` is a view: put it in \`views\`, so that the object is an agent`);
|
|
193
|
+
}
|
|
194
|
+
for (const key of own) {
|
|
195
|
+
if (key in handlers || key in views)
|
|
196
|
+
throw new Error(`${name}: \`${key}\` is a handler of @restatedev/agents' own: name yours otherwise`);
|
|
197
|
+
}
|
|
198
|
+
// A model provider's refusal (a missing API key) ends a handler's invocation.
|
|
199
|
+
const yours = Object.fromEntries(Object.entries(handlers).map(([key, entry]) => [
|
|
200
|
+
key,
|
|
201
|
+
answering(entry),
|
|
202
|
+
]));
|
|
203
|
+
const messages = views.messages;
|
|
204
|
+
onClear(name, messages);
|
|
205
|
+
const page = (typeof messages === 'function' ? messages : messages?.handler);
|
|
206
|
+
const agent = page === undefined ? {} : followed(page);
|
|
207
|
+
return withCompanion(restate.sessionObject({
|
|
208
|
+
name,
|
|
209
|
+
description,
|
|
210
|
+
schema,
|
|
211
|
+
handlers: { ...(messages === undefined ? {} : INTERNAL), ...yours },
|
|
212
|
+
views: { ...BUILT_IN, ...agent, ...views },
|
|
213
|
+
metadata,
|
|
214
|
+
options: { ...OPTIONS, ...options },
|
|
215
|
+
}));
|
|
216
|
+
};
|
|
217
|
+
/** `entry`, but a model provider's refusal it fails with ends its invocation, as `chat`'s does. */
|
|
218
|
+
function answering(entry) {
|
|
219
|
+
const { handler, ...options } = typeof entry === 'function' ? { handler: entry } : entry;
|
|
220
|
+
return {
|
|
221
|
+
...options,
|
|
222
|
+
handler: async (ctx, input) => {
|
|
223
|
+
try {
|
|
224
|
+
return await handler(ctx, input);
|
|
225
|
+
}
|
|
226
|
+
catch (error) {
|
|
227
|
+
throw refusal(error) ?? error;
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
//# sourceMappingURL=object.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.js","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,+BAA+B,CAAC;AACtD,OAAO,EACL,OAAO,EACP,OAAO,EACP,YAAY,EAGZ,YAAY,EACZ,OAAO,GAER,MAAM,aAAa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AK,EAAE;QAC1C,QAAQ;QACR,OAAO,EAAE,EAAE,GAAG,OAAO,EAAE,GAAG,OAAO,EAAE;KACpC,CAAC,CACH,CAAC;AACJ,CAAC,CAAC;AAEF,mGAAmG;AACnG,SAAS,SAAS,CAAC,KAA4C;IAC7D,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,EAAE,GAAG,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;IACzF,OAAO;QACL,GAAG,OAAO;QACV,OAAO,EAAE,KAAK,EAAE,GAA2B,EAAE,KAAc,EAAE,EAAE;YAC7D,IAAI,CAAC;gBACH,OAAO,MAAM,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YACnC,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,MAAM,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC;YAChC,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Effect, Layer } from 'effect';
|
|
2
|
+
import { SqlClient } from 'effect/sql';
|
|
3
|
+
import { OpenCode } from '@opencode/sdk/effect';
|
|
4
|
+
import type { Plugin } from '@opencode/plugin';
|
|
5
|
+
import type { Database } from '@restatedev/sessions';
|
|
6
|
+
/** Closed: every later statement fails. */
|
|
7
|
+
export interface Fence {
|
|
8
|
+
closed: boolean;
|
|
9
|
+
}
|
|
10
|
+
/** An Effect SqlClient over `db`, with its statements refused once `fence` closes. */
|
|
11
|
+
export declare function sqlClient(db: Database, fence: Fence): Layer.Layer<SqlClient.SqlClient>;
|
|
12
|
+
export interface OpenOptions {
|
|
13
|
+
/** OpenCode plugins: models, tools, agents. */
|
|
14
|
+
plugins?: Plugin.Plugin[];
|
|
15
|
+
/** OpenCode's config (opencode.json), inline. */
|
|
16
|
+
config?: Record<string, unknown>;
|
|
17
|
+
/**
|
|
18
|
+
* OpenCode's own filesystem, search and shell, on the local machine, for
|
|
19
|
+
* its built-in tools (read, write, edit, bash, glob, grep...) in a
|
|
20
|
+
* session's directory. Without it, the workerd profile has none.
|
|
21
|
+
*/
|
|
22
|
+
local?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Resume the turns a previous attempt left unfinished, as OpenCode does
|
|
25
|
+
* when it boots (default true). A reader beside a running turn does not.
|
|
26
|
+
*/
|
|
27
|
+
resume?: boolean;
|
|
28
|
+
}
|
|
29
|
+
/** A booted OpenCode: `call` runs one of `client`'s calls (which return Effects); `close` shuts it down. */
|
|
30
|
+
export interface Host {
|
|
31
|
+
client: OpenCode.Interface;
|
|
32
|
+
call<A, E, R>(effect: Effect.Effect<A, E, R>): Promise<A>;
|
|
33
|
+
close(): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Boot OpenCode with its database in `db`. Booting resumes the turns a
|
|
37
|
+
* previous attempt left unfinished. Close the host when done.
|
|
38
|
+
*/
|
|
39
|
+
export declare function openOpenCode(db: Database, fence: Fence, { plugins, config, local, resume }: OpenOptions): Promise<Host>;
|
|
40
|
+
//# sourceMappingURL=db.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db.d.ts","sourceRoot":"","sources":["../../src/opencode/db.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAW,MAAM,EAAQ,KAAK,EAAiB,MAAM,QAAQ,CAAC;AAErE,OAAO,EAAE,SAAS,EAAa,MAAM,YAAY,CAAC;AAOlD,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,KAAK,EAAkB,QAAQ,EAAY,MAAM,sBAAsB,CAAC;AAE/E,2CAA2C;AAC3C,MAAM,WAAW,KAAK;IACpB,MAAM,EAAE,OAAO,CAAC;CACjB;AA+CD,sFAAsF;AACtF,wBAAgB,SAAS,CAAC,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,SAAS,CAAC,SAAS,CAAC,CAkEtF;AAED,MAAM,WAAW,WAAW;IAC1B,+CAA+C;IAC/C,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC;IAC1B,iDAAiD;IACjD,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC;;;;OAIG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,4GAA4G;AAC5G,MAAM,WAAW,IAAI;IACnB,MAAM,EAAE,QAAQ,CAAC,SAAS,CAAC;IAC3B,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IAC1D,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAYD;;;GAGG;AACH,wBAAsB,YAAY,CAChC,EAAE,EAAE,QAAQ,EACZ,KAAK,EAAE,KAAK,EACZ,EAAE,OAAY,EAAE,MAAW,EAAE,KAAa,EAAE,MAAa,EAAE,EAAE,WAAW,GACvE,OAAO,CAAC,IAAI,CAAC,CA+Bf"}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// OpenCode on the object's database: `openOpenCode(db, fence, options)` boots
|
|
2
|
+
// OpenCode 2.0's embedded host (@opencode/sdk) with its database in a
|
|
3
|
+
// SQL session, `ctx.db`.
|
|
4
|
+
//
|
|
5
|
+
// The host is OpenCode's workerd profile, the one it boots in a Cloudflare
|
|
6
|
+
// Durable Object: no local filesystem, shell, terminal or version control,
|
|
7
|
+
// config given inline, plugins given in code. Only its database differs. The
|
|
8
|
+
// workerd profile reads a Durable Object's SQLite, whose `exec` is
|
|
9
|
+
// synchronous; here OpenCode gets an Effect SqlClient instead, which is
|
|
10
|
+
// asynchronous, through `Database.configuredClient` (OpenCode's), the seam OpenCode keeps
|
|
11
|
+
// for an injected client. Each transaction is an SQL transaction, and the
|
|
12
|
+
// session runs statements one at a time, as OpenCode's own clients do.
|
|
13
|
+
//
|
|
14
|
+
// `fence.closed` refuses every statement from then on: an attempt that is
|
|
15
|
+
// giving up stores nothing more, as Restate fences an attempt that a newer
|
|
16
|
+
// one replaced.
|
|
17
|
+
import { Context, Effect, Exit, Layer, Option, Scope } from 'effect';
|
|
18
|
+
import { Reactivity } from 'effect/reactivity';
|
|
19
|
+
import { SqlClient, Statement } from 'effect/sql';
|
|
20
|
+
import { SqlError, UnknownError } from 'effect/sql/SqlError';
|
|
21
|
+
import { Database as OpenCodeDatabase } from '@opencode/core/database/database';
|
|
22
|
+
import { SessionRestart } from '@opencode/core/session/execution/restart';
|
|
23
|
+
import { ServerWorkerd } from '@opencode/server/workerd';
|
|
24
|
+
import { fromPromise } from '@opencode/core/plugin/promise';
|
|
25
|
+
import { OpenCode } from '@opencode/sdk/effect';
|
|
26
|
+
const error = (operation, cause) => new SqlError({
|
|
27
|
+
reason: new UnknownError({
|
|
28
|
+
cause,
|
|
29
|
+
message: cause instanceof Error ? cause.message : String(cause),
|
|
30
|
+
operation,
|
|
31
|
+
}),
|
|
32
|
+
});
|
|
33
|
+
/** The connection inside a transaction, for statements of that transaction. */
|
|
34
|
+
const InTransaction = Context.Service('@restatedev/agents/OpenCodeTransaction');
|
|
35
|
+
/**
|
|
36
|
+
* A connection whose statements go to `target`: the session, which runs one
|
|
37
|
+
* call at a time and holds them while a transaction runs, or that
|
|
38
|
+
* transaction's database. Each is refused once `fence` closes.
|
|
39
|
+
*/
|
|
40
|
+
function connection(target, fence) {
|
|
41
|
+
// Effect binds what the compiler made of OpenCode's values: SQL values.
|
|
42
|
+
const exec = (read, sql, params) => Effect.tryPromise({
|
|
43
|
+
try: async () => {
|
|
44
|
+
if (fence.closed)
|
|
45
|
+
throw new Error('the attempt is over');
|
|
46
|
+
return read(sql, params);
|
|
47
|
+
},
|
|
48
|
+
catch: (cause) => error('execute', cause),
|
|
49
|
+
});
|
|
50
|
+
const run = (sql, params = []) => exec((s, p) => target.query(s, p), sql, params);
|
|
51
|
+
const runValues = (sql, params = []) => exec((s, p) => target.values(s, p), sql, params);
|
|
52
|
+
const transformed = (sql, params, transformRows) => transformRows ? Effect.map(run(sql, params), transformRows) : run(sql, params);
|
|
53
|
+
return {
|
|
54
|
+
execute: transformed,
|
|
55
|
+
executeRaw: (sql, params) => run(sql, params),
|
|
56
|
+
executeValues: (sql, params) => runValues(sql, params),
|
|
57
|
+
executeValuesUnprepared: (sql, params) => runValues(sql, params),
|
|
58
|
+
executeUnprepared: transformed,
|
|
59
|
+
executeStream: () => {
|
|
60
|
+
throw new Error('executeStream is not supported');
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/** An Effect SqlClient over `db`, with its statements refused once `fence` closes. */
|
|
65
|
+
export function sqlClient(db, fence) {
|
|
66
|
+
// `effect` as one SQL transaction: it commits when `effect` succeeds
|
|
67
|
+
// and rolls back when it fails. Its statements go to the transaction's
|
|
68
|
+
// database.
|
|
69
|
+
const withTransaction = (effect) => Effect.withFiber((fiber) => {
|
|
70
|
+
if (Option.isSome(Context.getOption(fiber.context, InTransaction))) {
|
|
71
|
+
return Effect.fail(error('transaction', new Error('nested transactions are not supported')));
|
|
72
|
+
}
|
|
73
|
+
return Effect.callback((resume) => {
|
|
74
|
+
const rollback = new Error('rollback');
|
|
75
|
+
let interrupted = false;
|
|
76
|
+
let resumed = false;
|
|
77
|
+
// Settles when the transaction has committed, or rejects: with
|
|
78
|
+
// `rollback` once `effect` failed, or with the error of BEGIN or COMMIT.
|
|
79
|
+
const ended = db.transaction((tx) => new Promise((resolve, reject) => {
|
|
80
|
+
if (interrupted)
|
|
81
|
+
return reject(rollback);
|
|
82
|
+
const inner = Effect.provideContext(effect, Context.add(fiber.context, InTransaction, [connection(tx, fence), 0]));
|
|
83
|
+
resumed = true;
|
|
84
|
+
resume(
|
|
85
|
+
// A finalizer, so that the body settles however `effect`
|
|
86
|
+
// ends, an interruption included: otherwise the transaction
|
|
87
|
+
// would hold the session for good.
|
|
88
|
+
Effect.onExit(inner, (exit) => {
|
|
89
|
+
if (Exit.isSuccess(exit)) {
|
|
90
|
+
resolve();
|
|
91
|
+
return Effect.void;
|
|
92
|
+
}
|
|
93
|
+
reject(rollback);
|
|
94
|
+
return Effect.promise(() => settled);
|
|
95
|
+
}).pipe(
|
|
96
|
+
// The commit is part of the transaction: if it fails, so
|
|
97
|
+
// does the transaction.
|
|
98
|
+
Effect.flatMap((value) => Effect.as(Effect.tryPromise({ try: () => ended, catch: (e) => error('commit', e) }), value))));
|
|
99
|
+
}));
|
|
100
|
+
const settled = ended.catch((cause) => {
|
|
101
|
+
// BEGIN failed: `effect` never ran.
|
|
102
|
+
if (!resumed && cause !== rollback)
|
|
103
|
+
resume(Effect.fail(error('transaction', cause)));
|
|
104
|
+
});
|
|
105
|
+
return Effect.suspend(() => {
|
|
106
|
+
interrupted = true;
|
|
107
|
+
return Effect.promise(() => settled);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
const make = Effect.gen(function* () {
|
|
112
|
+
const client = yield* SqlClient.make({
|
|
113
|
+
acquirer: Effect.succeed(connection(db, fence)),
|
|
114
|
+
compiler: Statement.makeCompilerSqlite(),
|
|
115
|
+
spanAttributes: [['db.system.name', 'sqlite']],
|
|
116
|
+
transactionService: InTransaction,
|
|
117
|
+
});
|
|
118
|
+
return Object.assign(client, { withTransaction, transactionStatements: false });
|
|
119
|
+
});
|
|
120
|
+
return Layer.effect(SqlClient.SqlClient, make).pipe(Layer.provide(Reactivity.layer));
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* How OpenCode resumes the turns a previous attempt left unfinished when it
|
|
124
|
+
* boots: each, however often it crashed (Restate's retry policy decides how
|
|
125
|
+
* often an attempt is retried), or, without `resume`, none.
|
|
126
|
+
*/
|
|
127
|
+
const restarts = (resume) => resume
|
|
128
|
+
? SessionRestart.layer({ maxAttempts: Infinity })
|
|
129
|
+
: Layer.succeed(SessionRestart.Service, { resumeSuspendedSessions: Effect.void });
|
|
130
|
+
/**
|
|
131
|
+
* Boot OpenCode with its database in `db`. Booting resumes the turns a
|
|
132
|
+
* previous attempt left unfinished. Close the host when done.
|
|
133
|
+
*/
|
|
134
|
+
export async function openOpenCode(db, fence, { plugins = [], config = {}, local = false, resume = true }) {
|
|
135
|
+
// The workerd profile, but for its database, which comes first. (The
|
|
136
|
+
// Promise API's OpenCode.create takes no replacements; the Effect API's does.)
|
|
137
|
+
const profile = { storage: undefined, models: { fetch: false } };
|
|
138
|
+
// In 2.0.27's order: database, process spawner, snapshot, version control,
|
|
139
|
+
// filesystem, filesystem search, pty, plugin discovery. Local keeps the
|
|
140
|
+
// stubs of snapshots, the pty and plugin discovery only.
|
|
141
|
+
const [, spawner, snapshot, vcs, fs, search, pty, discovery] = ServerWorkerd.replacements(profile);
|
|
142
|
+
const rest = local ? [snapshot, pty, discovery] : [spawner, snapshot, vcs, fs, search, pty, discovery];
|
|
143
|
+
const scope = Scope.makeUnsafe();
|
|
144
|
+
const client = await Effect.runPromise(OpenCode.create({
|
|
145
|
+
...ServerWorkerd.serverOptions({ ...profile, config: { content: JSON.stringify(config) } }),
|
|
146
|
+
plugins: plugins.map(fromPromise),
|
|
147
|
+
}, {
|
|
148
|
+
overrides: [
|
|
149
|
+
OpenCodeDatabase.node.replace(OpenCodeDatabase.configuredClient(sqlClient(db, fence))),
|
|
150
|
+
...rest,
|
|
151
|
+
SessionRestart.node.replace(SessionRestart.node.mapLayer(() => restarts(resume))),
|
|
152
|
+
],
|
|
153
|
+
}).pipe(Scope.provide(scope)));
|
|
154
|
+
return {
|
|
155
|
+
client,
|
|
156
|
+
// The client's calls need nothing more: OpenCode provided it all.
|
|
157
|
+
call: (effect) => Effect.runPromise(effect),
|
|
158
|
+
close: () => Effect.runPromise(Scope.close(scope, Exit.void)),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
//# sourceMappingURL=db.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db.js","sourceRoot":"","sources":["../../src/opencode/db.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,sEAAsE;AACtE,yBAAyB;AACzB,EAAE;AACF,2EAA2E;AAC3E,2EAA2E;AAC3E,6EAA6E;AAC7E,mEAAmE;AACnE,wEAAwE;AACxE,0FAA0F;AAC1F,0EAA0E;AAC1E,uEAAuE;AACvE,EAAE;AACF,0EAA0E;AAC1E,2EAA2E;AAC3E,gBAAgB;AAChB,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAE7D,OAAO,EAAE,QAAQ,IAAI,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AAChF,OAAO,EAAE,cAAc,EAAE,MAAM,0CAA0C,CAAC;AAC1E,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAShD,MAAM,KAAK,GAAG,CAAC,SAAiB,EAAE,KAAc,EAAE,EAAE,CAClD,IAAI,QAAQ,CAAC;IACX,MAAM,EAAE,IAAI,YAAY,CAAC;QACvB,KAAK;QACL,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAA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