omsago 0.0.0-stage → 1.0.4
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 +113 -2
- package/dist/client/discovery.d.ts +4 -0
- package/dist/client/discovery.js +133 -0
- package/dist/client/errors.d.ts +8 -0
- package/dist/client/errors.js +20 -0
- package/dist/client/events.d.ts +6 -0
- package/dist/client/events.js +192 -0
- package/dist/client/index.d.ts +15 -0
- package/dist/client/index.js +70 -0
- package/dist/client/messages.d.ts +4 -0
- package/dist/client/messages.js +186 -0
- package/dist/client/prompts.d.ts +3 -0
- package/dist/client/prompts.js +231 -0
- package/dist/client/requests.d.ts +100 -0
- package/dist/client/requests.js +81 -0
- package/dist/client/session-routes.d.ts +3 -0
- package/dist/client/session-routes.js +94 -0
- package/dist/client/sessions.d.ts +6 -0
- package/dist/client/sessions.js +29 -0
- package/dist/client/state.d.ts +26 -0
- package/dist/client/state.js +52 -0
- package/dist/client/tool-parts.d.ts +3 -0
- package/dist/client/tool-parts.js +51 -0
- package/dist/domains/agents.d.ts +3 -0
- package/dist/domains/agents.js +58 -0
- package/dist/domains/commands.d.ts +4 -0
- package/dist/domains/commands.js +65 -0
- package/dist/domains/index.d.ts +4 -0
- package/dist/domains/index.js +102 -0
- package/dist/domains/mcp.d.ts +3 -0
- package/dist/domains/mcp.js +79 -0
- package/dist/domains/permissions.d.ts +6 -0
- package/dist/domains/permissions.js +33 -0
- package/dist/domains/prompt.d.ts +5 -0
- package/dist/domains/prompt.js +32 -0
- package/dist/domains/tools.d.ts +5 -0
- package/dist/domains/tools.js +150 -0
- package/dist/domains/value.d.ts +11 -0
- package/dist/domains/value.js +49 -0
- package/dist/enums.d.ts +205 -0
- package/dist/enums.js +187 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +90 -0
- package/dist/session/index.d.ts +4 -0
- package/dist/session/index.js +133 -0
- package/dist/session/message-restore.d.ts +9 -0
- package/dist/session/message-restore.js +176 -0
- package/dist/session/messages.d.ts +3 -0
- package/dist/session/messages.js +222 -0
- package/dist/session/model.d.ts +6 -0
- package/dist/session/model.js +86 -0
- package/dist/session/prompt.d.ts +4 -0
- package/dist/session/prompt.js +115 -0
- package/dist/tui.d.ts +3 -0
- package/dist/tui.js +17 -0
- package/dist/user-discovery.d.ts +13 -0
- package/dist/user-discovery.js +117 -0
- package/package.json +77 -4
- package/scripts/postinstall.mjs +12 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { AssistantContentKind, ErrorName, MessageRole, PartKind, PromptFileSourceKind, RecordType, SessionOutcome, ToolStateStatus, } from "../enums.js";
|
|
2
|
+
import { assertNever } from "./errors.js";
|
|
3
|
+
import { legacyToolState } from "./tool-parts.js";
|
|
4
|
+
export function legacyError(error) {
|
|
5
|
+
if (error.type.toLowerCase().includes("interrupt") ||
|
|
6
|
+
error.type.toLowerCase().includes("abort")) {
|
|
7
|
+
return { name: ErrorName.Aborted, data: { message: error.message } };
|
|
8
|
+
}
|
|
9
|
+
return {
|
|
10
|
+
name: ErrorName.Api,
|
|
11
|
+
data: {
|
|
12
|
+
message: error.message,
|
|
13
|
+
isRetryable: error.retryAfterMs !== undefined,
|
|
14
|
+
...(error.status === undefined ? {} : { statusCode: error.status }),
|
|
15
|
+
...(error.response === undefined ? {} : { responseBody: error.response.body }),
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export function legacyMessages(session, records) {
|
|
20
|
+
const result = [];
|
|
21
|
+
let parentID = "";
|
|
22
|
+
let agent = session.agent ?? "";
|
|
23
|
+
let model = session.model;
|
|
24
|
+
for (const record of records) {
|
|
25
|
+
const base = { id: record.id, sessionID: session.id, time: { created: record.time.created } };
|
|
26
|
+
const partBase = { id: `${record.id}:text`, sessionID: session.id, messageID: record.id };
|
|
27
|
+
switch (record.type) {
|
|
28
|
+
case RecordType.AgentSwitched:
|
|
29
|
+
agent = record.agent;
|
|
30
|
+
break;
|
|
31
|
+
case RecordType.ModelSwitched:
|
|
32
|
+
model = record.model;
|
|
33
|
+
break;
|
|
34
|
+
case RecordType.User:
|
|
35
|
+
case RecordType.Synthetic:
|
|
36
|
+
case RecordType.System:
|
|
37
|
+
case RecordType.Skill: {
|
|
38
|
+
const info = {
|
|
39
|
+
...base,
|
|
40
|
+
role: MessageRole.User,
|
|
41
|
+
agent,
|
|
42
|
+
model: { providerID: model?.providerID ?? "", modelID: model?.id ?? "" },
|
|
43
|
+
};
|
|
44
|
+
const parts = [
|
|
45
|
+
{
|
|
46
|
+
...partBase,
|
|
47
|
+
type: PartKind.Text,
|
|
48
|
+
text: record.text,
|
|
49
|
+
...(record.type === RecordType.User ? {} : { synthetic: true }),
|
|
50
|
+
},
|
|
51
|
+
];
|
|
52
|
+
if (record.type === RecordType.User) {
|
|
53
|
+
parts.push(...(record.files ?? []).map((file, index) => ({
|
|
54
|
+
...partBase,
|
|
55
|
+
id: `${record.id}:file:${index}`,
|
|
56
|
+
type: PartKind.File,
|
|
57
|
+
mime: file.mime,
|
|
58
|
+
url: file.source.type === PromptFileSourceKind.Uri
|
|
59
|
+
? file.source.uri
|
|
60
|
+
: `data:${file.mime};base64,${file.data}`,
|
|
61
|
+
...(file.name === undefined ? {} : { filename: file.name }),
|
|
62
|
+
})));
|
|
63
|
+
parts.push(...(record.agents ?? []).map((mention, index) => ({
|
|
64
|
+
...partBase,
|
|
65
|
+
id: `${record.id}:agent:${index}`,
|
|
66
|
+
type: PartKind.Agent,
|
|
67
|
+
name: mention.name,
|
|
68
|
+
})));
|
|
69
|
+
}
|
|
70
|
+
result.push({ info, parts });
|
|
71
|
+
parentID = record.id;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
case RecordType.Assistant: {
|
|
75
|
+
const info = {
|
|
76
|
+
...base,
|
|
77
|
+
role: MessageRole.Assistant,
|
|
78
|
+
parentID,
|
|
79
|
+
time: { ...record.time },
|
|
80
|
+
providerID: record.model.providerID,
|
|
81
|
+
modelID: record.model.id,
|
|
82
|
+
mode: record.agent,
|
|
83
|
+
path: { cwd: session.location.directory, root: session.location.directory },
|
|
84
|
+
cost: record.cost ?? 0,
|
|
85
|
+
tokens: record.tokens ?? {
|
|
86
|
+
input: 0,
|
|
87
|
+
output: 0,
|
|
88
|
+
reasoning: 0,
|
|
89
|
+
cache: { read: 0, write: 0 },
|
|
90
|
+
},
|
|
91
|
+
...(record.error === undefined ? {} : { error: legacyError(record.error) }),
|
|
92
|
+
...(record.finish === undefined ? {} : { finish: record.finish }),
|
|
93
|
+
};
|
|
94
|
+
const parts = record.content.map((content, index) => {
|
|
95
|
+
const identity = { ...partBase, id: `${record.id}:${index}` };
|
|
96
|
+
switch (content.type) {
|
|
97
|
+
case AssistantContentKind.Text:
|
|
98
|
+
return { ...identity, type: PartKind.Text, text: content.text };
|
|
99
|
+
case AssistantContentKind.Reasoning:
|
|
100
|
+
return {
|
|
101
|
+
...identity,
|
|
102
|
+
type: PartKind.Reasoning,
|
|
103
|
+
text: content.text,
|
|
104
|
+
time: {
|
|
105
|
+
start: content.time?.created ?? record.time.created,
|
|
106
|
+
...(content.time?.completed === undefined ? {} : { end: content.time.completed }),
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
case AssistantContentKind.Tool: {
|
|
110
|
+
const state = legacyToolState(content);
|
|
111
|
+
if (state.status === ToolStateStatus.Completed && state.attachments) {
|
|
112
|
+
state.attachments = state.attachments.map((file) => ({
|
|
113
|
+
...file,
|
|
114
|
+
sessionID: session.id,
|
|
115
|
+
messageID: record.id,
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
...identity,
|
|
120
|
+
id: `${record.id}:tool:${content.id}`,
|
|
121
|
+
type: PartKind.Tool,
|
|
122
|
+
callID: content.id,
|
|
123
|
+
tool: content.name,
|
|
124
|
+
state,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
default:
|
|
128
|
+
return assertNever(content);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
result.push({ info, parts });
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case RecordType.Idle: {
|
|
135
|
+
const last = result.at(-1);
|
|
136
|
+
if (record.outcome !== SessionOutcome.Succeeded &&
|
|
137
|
+
last?.info.role !== MessageRole.Assistant) {
|
|
138
|
+
result.push({
|
|
139
|
+
info: {
|
|
140
|
+
...base,
|
|
141
|
+
role: MessageRole.Assistant,
|
|
142
|
+
parentID,
|
|
143
|
+
mode: agent,
|
|
144
|
+
modelID: model?.id ?? "",
|
|
145
|
+
providerID: model?.providerID ?? "",
|
|
146
|
+
time: { created: record.time.created, completed: record.time.created },
|
|
147
|
+
path: { cwd: session.location.directory, root: session.location.directory },
|
|
148
|
+
cost: 0,
|
|
149
|
+
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
|
|
150
|
+
error: record.error === undefined
|
|
151
|
+
? {
|
|
152
|
+
name: record.outcome === SessionOutcome.Interrupted
|
|
153
|
+
? ErrorName.Aborted
|
|
154
|
+
: ErrorName.Unknown,
|
|
155
|
+
data: { message: `Session ${record.outcome}` },
|
|
156
|
+
}
|
|
157
|
+
: legacyError(record.error),
|
|
158
|
+
},
|
|
159
|
+
parts: [],
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
if (last?.info.role === MessageRole.Assistant &&
|
|
163
|
+
record.outcome !== SessionOutcome.Succeeded &&
|
|
164
|
+
last.info.error === undefined) {
|
|
165
|
+
last.info.error =
|
|
166
|
+
record.error === undefined
|
|
167
|
+
? {
|
|
168
|
+
name: record.outcome === SessionOutcome.Interrupted
|
|
169
|
+
? ErrorName.Aborted
|
|
170
|
+
: ErrorName.Unknown,
|
|
171
|
+
data: { message: `Session ${record.outcome}` },
|
|
172
|
+
}
|
|
173
|
+
: legacyError(record.error);
|
|
174
|
+
}
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
case RecordType.LocationSwitched:
|
|
178
|
+
case RecordType.Shell:
|
|
179
|
+
case RecordType.Compaction:
|
|
180
|
+
break;
|
|
181
|
+
default:
|
|
182
|
+
assertNever(record);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return result;
|
|
186
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
2
|
+
if (value !== null && value !== void 0) {
|
|
3
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
4
|
+
var dispose, inner;
|
|
5
|
+
if (async) {
|
|
6
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
7
|
+
dispose = value[Symbol.asyncDispose];
|
|
8
|
+
}
|
|
9
|
+
if (dispose === void 0) {
|
|
10
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
11
|
+
dispose = value[Symbol.dispose];
|
|
12
|
+
if (async) inner = dispose;
|
|
13
|
+
}
|
|
14
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
15
|
+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
16
|
+
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
17
|
+
}
|
|
18
|
+
else if (async) {
|
|
19
|
+
env.stack.push({ async: true });
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
24
|
+
return function (env) {
|
|
25
|
+
function fail(e) {
|
|
26
|
+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
27
|
+
env.hasError = true;
|
|
28
|
+
}
|
|
29
|
+
var r, s = 0;
|
|
30
|
+
function next() {
|
|
31
|
+
while (r = env.stack.pop()) {
|
|
32
|
+
try {
|
|
33
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
34
|
+
if (r.dispose) {
|
|
35
|
+
var result = r.dispose.call(r.value);
|
|
36
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
37
|
+
}
|
|
38
|
+
else s |= 1;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
fail(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
45
|
+
if (env.hasError) throw env.error;
|
|
46
|
+
}
|
|
47
|
+
return next();
|
|
48
|
+
};
|
|
49
|
+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
50
|
+
var e = new Error(message);
|
|
51
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
52
|
+
});
|
|
53
|
+
import { BridgeError, assertNever, unsupported } from "./errors.js";
|
|
54
|
+
import { legacyMessages } from "./messages.js";
|
|
55
|
+
import { ErrorName, MessageRole, PartKind, PermissionEffect, RecordType, SessionEventType, SessionOperation, SessionOutcome, } from "../enums.js";
|
|
56
|
+
import { body, promptBody, summarizeBody } from "./requests.js";
|
|
57
|
+
import { promptOverrides } from "./state.js";
|
|
58
|
+
import { nativeAction } from "../domains/permissions.js";
|
|
59
|
+
import { metadata } from "../domains/value.js";
|
|
60
|
+
export function promptRoutes(context, state) {
|
|
61
|
+
return async (request, sessionID, operation) => {
|
|
62
|
+
const options = { signal: request.signal };
|
|
63
|
+
if (operation === SessionOperation.Summarize) {
|
|
64
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
65
|
+
try {
|
|
66
|
+
const input = await body(request, summarizeBody);
|
|
67
|
+
await context.session.switchModel({ sessionID, model: { providerID: input.providerID, id: input.modelID } }, options);
|
|
68
|
+
const controller = new AbortController();
|
|
69
|
+
const signal = AbortSignal.any([request.signal, controller.signal]);
|
|
70
|
+
const subscription = context.event.subscribe({
|
|
71
|
+
types: [
|
|
72
|
+
SessionEventType.CompactionEnded,
|
|
73
|
+
SessionEventType.CompactionFailed,
|
|
74
|
+
SessionEventType.ExecutionInterrupted,
|
|
75
|
+
SessionEventType.Deleted,
|
|
76
|
+
],
|
|
77
|
+
signal,
|
|
78
|
+
});
|
|
79
|
+
const events = subscription[Symbol.asyncIterator]();
|
|
80
|
+
let pending = events.next();
|
|
81
|
+
const cleanup = __addDisposableResource(env_1, new AsyncDisposableStack(), true);
|
|
82
|
+
cleanup.defer(async () => {
|
|
83
|
+
controller.abort();
|
|
84
|
+
try {
|
|
85
|
+
await pending;
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
if (error !== signal.reason &&
|
|
89
|
+
!(error instanceof Error && error.name === ErrorName.Abort))
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
await events.return?.();
|
|
93
|
+
});
|
|
94
|
+
const admitted = await context.session.compact({ sessionID }, options);
|
|
95
|
+
for (;;) {
|
|
96
|
+
const next = await pending;
|
|
97
|
+
if (next.done)
|
|
98
|
+
throw new BridgeError(500, "Session compaction subscription ended before completion");
|
|
99
|
+
const event = next.value;
|
|
100
|
+
if (event.data.sessionID === sessionID) {
|
|
101
|
+
switch (event.type) {
|
|
102
|
+
case SessionEventType.CompactionFailed:
|
|
103
|
+
if (event.data.inputID === admitted.id)
|
|
104
|
+
throw new BridgeError(500, event.data.error.message);
|
|
105
|
+
break;
|
|
106
|
+
case SessionEventType.CompactionEnded: {
|
|
107
|
+
const records = await context.session.context({ sessionID }, options);
|
|
108
|
+
if (records.some((record) => record.id === admitted.id && record.type === RecordType.Compaction)) {
|
|
109
|
+
await context.session.wait({ sessionID }, options);
|
|
110
|
+
return Response.json(true);
|
|
111
|
+
}
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case SessionEventType.ExecutionInterrupted:
|
|
115
|
+
throw new BridgeError(409, "Session compaction was interrupted");
|
|
116
|
+
case SessionEventType.Deleted:
|
|
117
|
+
throw new BridgeError(404, "Session was deleted during compaction");
|
|
118
|
+
default:
|
|
119
|
+
assertNever(event);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
pending = events.next();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
catch (e_1) {
|
|
126
|
+
env_1.error = e_1;
|
|
127
|
+
env_1.hasError = true;
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
const result_1 = __disposeResources(env_1);
|
|
131
|
+
if (result_1)
|
|
132
|
+
await result_1;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const input = await body(request, promptBody);
|
|
136
|
+
const overrides = promptOverrides.parse(input);
|
|
137
|
+
if (input.noReply === true && operation !== SessionOperation.PromptAsync)
|
|
138
|
+
unsupported("synchronous prompt.noReply response");
|
|
139
|
+
if (input.variant !== undefined && input.model === undefined)
|
|
140
|
+
unsupported("prompt.variant without model");
|
|
141
|
+
if (input.agent !== undefined)
|
|
142
|
+
await context.session.switchAgent({ sessionID, agent: input.agent }, options);
|
|
143
|
+
if (input.model !== undefined)
|
|
144
|
+
await context.session.switchModel({
|
|
145
|
+
sessionID,
|
|
146
|
+
model: {
|
|
147
|
+
providerID: input.model.providerID,
|
|
148
|
+
id: input.model.modelID,
|
|
149
|
+
...(input.variant === undefined ? {} : { variant: input.variant }),
|
|
150
|
+
},
|
|
151
|
+
}, options);
|
|
152
|
+
if (input.tools !== undefined) {
|
|
153
|
+
const session = await context.session.get({ sessionID }, options);
|
|
154
|
+
await context.session.update({
|
|
155
|
+
sessionID,
|
|
156
|
+
permissions: [
|
|
157
|
+
...(session.permissions ?? []),
|
|
158
|
+
...Object.entries(input.tools).map(([tool, enabled]) => ({
|
|
159
|
+
action: nativeAction(tool),
|
|
160
|
+
resource: "*",
|
|
161
|
+
effect: enabled ? PermissionEffect.Allow : PermissionEffect.Deny,
|
|
162
|
+
})),
|
|
163
|
+
],
|
|
164
|
+
}, options);
|
|
165
|
+
}
|
|
166
|
+
const files = [];
|
|
167
|
+
const agents = [];
|
|
168
|
+
const texts = [];
|
|
169
|
+
for (const part of input.parts) {
|
|
170
|
+
switch (part.type) {
|
|
171
|
+
case PartKind.Text:
|
|
172
|
+
if (part.ignored !== true)
|
|
173
|
+
texts.push(part.text);
|
|
174
|
+
break;
|
|
175
|
+
case PartKind.File:
|
|
176
|
+
files.push({
|
|
177
|
+
uri: part.url,
|
|
178
|
+
...(part.filename === undefined ? {} : { name: part.filename }),
|
|
179
|
+
});
|
|
180
|
+
break;
|
|
181
|
+
case PartKind.Agent:
|
|
182
|
+
agents.push({
|
|
183
|
+
name: part.name,
|
|
184
|
+
...(part.source === undefined
|
|
185
|
+
? {}
|
|
186
|
+
: {
|
|
187
|
+
mention: {
|
|
188
|
+
start: part.source.start,
|
|
189
|
+
end: part.source.end,
|
|
190
|
+
text: part.source.value ?? part.name,
|
|
191
|
+
},
|
|
192
|
+
}),
|
|
193
|
+
});
|
|
194
|
+
break;
|
|
195
|
+
default:
|
|
196
|
+
assertNever(part);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
state.set(sessionID, overrides);
|
|
200
|
+
const admission = await context.session.prompt({
|
|
201
|
+
sessionID,
|
|
202
|
+
text: texts.join("\n"),
|
|
203
|
+
files,
|
|
204
|
+
agents,
|
|
205
|
+
...(input.messageID === undefined ? {} : { id: input.messageID }),
|
|
206
|
+
metadata: { "omsago.client": metadata(overrides) },
|
|
207
|
+
resume: input.noReply !== true,
|
|
208
|
+
}, options);
|
|
209
|
+
if (operation === SessionOperation.PromptAsync)
|
|
210
|
+
return new Response(null, { status: 204 });
|
|
211
|
+
await context.session.wait({ sessionID }, options);
|
|
212
|
+
const session = await context.session.get({ sessionID }, options);
|
|
213
|
+
const records = await context.session.context({ sessionID }, options);
|
|
214
|
+
const messages = legacyMessages(session, records);
|
|
215
|
+
const userIndex = messages.findIndex((message) => message.info.id === admission.id);
|
|
216
|
+
const assistant = messages
|
|
217
|
+
.slice(userIndex + 1)
|
|
218
|
+
.findLast((message) => message.info.role === MessageRole.Assistant);
|
|
219
|
+
if (userIndex < 0 || assistant === undefined)
|
|
220
|
+
throw new BridgeError(500, `Prompt ${admission.id} produced no assistant message (${session.outcome ?? "unknown"})`);
|
|
221
|
+
if (assistant.info.role === MessageRole.Assistant &&
|
|
222
|
+
assistant.info.error === undefined &&
|
|
223
|
+
session.outcome !== SessionOutcome.Succeeded) {
|
|
224
|
+
assistant.info.error = {
|
|
225
|
+
name: session.outcome === SessionOutcome.Interrupted ? ErrorName.Aborted : ErrorName.Unknown,
|
|
226
|
+
data: { message: `Session ${session.outcome ?? "did not succeed"}` },
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
return Response.json(assistant);
|
|
230
|
+
};
|
|
231
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const model: z.ZodObject<{
|
|
3
|
+
providerID: z.ZodString;
|
|
4
|
+
modelID: z.ZodString;
|
|
5
|
+
}, z.core.$strict>;
|
|
6
|
+
export declare const nativeModel: z.ZodObject<{
|
|
7
|
+
providerID: z.ZodString;
|
|
8
|
+
id: z.ZodString;
|
|
9
|
+
variant: z.ZodOptional<z.ZodString>;
|
|
10
|
+
}, z.core.$strict>;
|
|
11
|
+
export declare const permission: z.ZodObject<{
|
|
12
|
+
permission: z.ZodString;
|
|
13
|
+
pattern: z.ZodString;
|
|
14
|
+
action: z.ZodEnum<{
|
|
15
|
+
allow: "allow";
|
|
16
|
+
ask: "ask";
|
|
17
|
+
deny: "deny";
|
|
18
|
+
}>;
|
|
19
|
+
}, z.core.$strict>;
|
|
20
|
+
export declare const createBody: z.ZodObject<{
|
|
21
|
+
parentID: z.ZodOptional<z.ZodString>;
|
|
22
|
+
title: z.ZodOptional<z.ZodString>;
|
|
23
|
+
agent: z.ZodOptional<z.ZodString>;
|
|
24
|
+
model: z.ZodOptional<z.ZodObject<{
|
|
25
|
+
providerID: z.ZodString;
|
|
26
|
+
id: z.ZodString;
|
|
27
|
+
variant: z.ZodOptional<z.ZodString>;
|
|
28
|
+
}, z.core.$strict>>;
|
|
29
|
+
permission: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
30
|
+
permission: z.ZodString;
|
|
31
|
+
pattern: z.ZodString;
|
|
32
|
+
action: z.ZodEnum<{
|
|
33
|
+
allow: "allow";
|
|
34
|
+
ask: "ask";
|
|
35
|
+
deny: "deny";
|
|
36
|
+
}>;
|
|
37
|
+
}, z.core.$strict>>>;
|
|
38
|
+
}, z.core.$strict>;
|
|
39
|
+
export declare const updateBody: z.ZodObject<{
|
|
40
|
+
title: z.ZodOptional<z.ZodString>;
|
|
41
|
+
permission: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
42
|
+
permission: z.ZodString;
|
|
43
|
+
pattern: z.ZodString;
|
|
44
|
+
action: z.ZodEnum<{
|
|
45
|
+
allow: "allow";
|
|
46
|
+
ask: "ask";
|
|
47
|
+
deny: "deny";
|
|
48
|
+
}>;
|
|
49
|
+
}, z.core.$strict>>>;
|
|
50
|
+
time: z.ZodOptional<z.ZodObject<{
|
|
51
|
+
archived: z.ZodOptional<z.ZodNumber>;
|
|
52
|
+
}, z.core.$strict>>;
|
|
53
|
+
}, z.core.$strict>;
|
|
54
|
+
export declare const promptBody: z.ZodObject<{
|
|
55
|
+
messageID: z.ZodOptional<z.ZodString>;
|
|
56
|
+
model: z.ZodOptional<z.ZodObject<{
|
|
57
|
+
providerID: z.ZodString;
|
|
58
|
+
modelID: z.ZodString;
|
|
59
|
+
}, z.core.$strict>>;
|
|
60
|
+
agent: z.ZodOptional<z.ZodString>;
|
|
61
|
+
variant: z.ZodOptional<z.ZodString>;
|
|
62
|
+
noReply: z.ZodOptional<z.ZodBoolean>;
|
|
63
|
+
system: z.ZodOptional<z.ZodString>;
|
|
64
|
+
tools: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodBoolean>>;
|
|
65
|
+
parts: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
66
|
+
type: z.ZodLiteral<"text">;
|
|
67
|
+
id: z.ZodOptional<z.ZodString>;
|
|
68
|
+
text: z.ZodString;
|
|
69
|
+
synthetic: z.ZodOptional<z.ZodBoolean>;
|
|
70
|
+
ignored: z.ZodOptional<z.ZodBoolean>;
|
|
71
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
72
|
+
type: z.ZodLiteral<"file">;
|
|
73
|
+
id: z.ZodOptional<z.ZodString>;
|
|
74
|
+
mime: z.ZodString;
|
|
75
|
+
url: z.ZodString;
|
|
76
|
+
filename: z.ZodOptional<z.ZodString>;
|
|
77
|
+
source: z.ZodOptional<z.ZodUnknown>;
|
|
78
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
79
|
+
type: z.ZodLiteral<"agent">;
|
|
80
|
+
id: z.ZodOptional<z.ZodString>;
|
|
81
|
+
name: z.ZodString;
|
|
82
|
+
source: z.ZodOptional<z.ZodObject<{
|
|
83
|
+
start: z.ZodNumber;
|
|
84
|
+
end: z.ZodNumber;
|
|
85
|
+
value: z.ZodOptional<z.ZodString>;
|
|
86
|
+
}, z.core.$strip>>;
|
|
87
|
+
}, z.core.$strict>], "type">>;
|
|
88
|
+
temperature: z.ZodOptional<z.ZodNumber>;
|
|
89
|
+
topP: z.ZodOptional<z.ZodNumber>;
|
|
90
|
+
topK: z.ZodOptional<z.ZodNumber>;
|
|
91
|
+
maxOutputTokens: z.ZodOptional<z.ZodNumber>;
|
|
92
|
+
options: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodJSONSchema>>;
|
|
93
|
+
params: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodJSONSchema>>;
|
|
94
|
+
}, z.core.$strict>;
|
|
95
|
+
export declare const summarizeBody: z.ZodObject<{
|
|
96
|
+
providerID: z.ZodString;
|
|
97
|
+
modelID: z.ZodString;
|
|
98
|
+
auto: z.ZodOptional<z.ZodBoolean>;
|
|
99
|
+
}, z.core.$strict>;
|
|
100
|
+
export declare function body<S extends z.ZodType>(request: Request, schema: S): Promise<z.output<S>>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { PartKind, permissionEffects } from "../enums.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
export const model = z.object({ providerID: z.string(), modelID: z.string() }).strict();
|
|
4
|
+
export const nativeModel = z
|
|
5
|
+
.object({ providerID: z.string(), id: z.string(), variant: z.string().optional() })
|
|
6
|
+
.strict();
|
|
7
|
+
export const permission = z
|
|
8
|
+
.object({ permission: z.string(), pattern: z.string(), action: z.enum(permissionEffects) })
|
|
9
|
+
.strict();
|
|
10
|
+
export const createBody = z
|
|
11
|
+
.object({
|
|
12
|
+
parentID: z.string().optional(),
|
|
13
|
+
title: z.string().optional(),
|
|
14
|
+
agent: z.string().optional(),
|
|
15
|
+
model: nativeModel.optional(),
|
|
16
|
+
permission: z.array(permission).optional(),
|
|
17
|
+
})
|
|
18
|
+
.strict();
|
|
19
|
+
export const updateBody = z
|
|
20
|
+
.object({
|
|
21
|
+
title: z.string().optional(),
|
|
22
|
+
permission: z.array(permission).optional(),
|
|
23
|
+
time: z.object({ archived: z.number().optional() }).strict().optional(),
|
|
24
|
+
})
|
|
25
|
+
.strict();
|
|
26
|
+
const mention = z.object({ start: z.number(), end: z.number(), value: z.string().optional() });
|
|
27
|
+
const part = z.discriminatedUnion("type", [
|
|
28
|
+
z
|
|
29
|
+
.object({
|
|
30
|
+
type: z.literal(PartKind.Text),
|
|
31
|
+
id: z.string().optional(),
|
|
32
|
+
text: z.string(),
|
|
33
|
+
synthetic: z.boolean().optional(),
|
|
34
|
+
ignored: z.boolean().optional(),
|
|
35
|
+
})
|
|
36
|
+
.strict(),
|
|
37
|
+
z
|
|
38
|
+
.object({
|
|
39
|
+
type: z.literal(PartKind.File),
|
|
40
|
+
id: z.string().optional(),
|
|
41
|
+
mime: z.string(),
|
|
42
|
+
url: z.string(),
|
|
43
|
+
filename: z.string().optional(),
|
|
44
|
+
source: z.unknown().optional(),
|
|
45
|
+
})
|
|
46
|
+
.strict(),
|
|
47
|
+
z
|
|
48
|
+
.object({
|
|
49
|
+
type: z.literal(PartKind.Agent),
|
|
50
|
+
id: z.string().optional(),
|
|
51
|
+
name: z.string(),
|
|
52
|
+
source: mention.optional(),
|
|
53
|
+
})
|
|
54
|
+
.strict(),
|
|
55
|
+
]);
|
|
56
|
+
export const promptBody = z
|
|
57
|
+
.object({
|
|
58
|
+
messageID: z.string().optional(),
|
|
59
|
+
model: model.optional(),
|
|
60
|
+
agent: z.string().optional(),
|
|
61
|
+
variant: z.string().optional(),
|
|
62
|
+
noReply: z.boolean().optional(),
|
|
63
|
+
system: z.string().optional(),
|
|
64
|
+
tools: z.record(z.string(), z.boolean()).optional(),
|
|
65
|
+
parts: z.array(part),
|
|
66
|
+
temperature: z.number().optional(),
|
|
67
|
+
topP: z.number().optional(),
|
|
68
|
+
topK: z.number().optional(),
|
|
69
|
+
maxOutputTokens: z.number().int().positive().optional(),
|
|
70
|
+
options: z.record(z.string(), z.json()).optional(),
|
|
71
|
+
params: z.record(z.string(), z.json()).optional(),
|
|
72
|
+
})
|
|
73
|
+
.strict();
|
|
74
|
+
export const summarizeBody = z
|
|
75
|
+
.object({ providerID: z.string(), modelID: z.string(), auto: z.boolean().optional() })
|
|
76
|
+
.strict();
|
|
77
|
+
export async function body(request, schema) {
|
|
78
|
+
const text = await request.text();
|
|
79
|
+
const value = text.length === 0 ? {} : JSON.parse(text);
|
|
80
|
+
return schema.parse(value);
|
|
81
|
+
}
|