copilotz 1.0.0-rc.20 → 1.0.0-rc.21
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/CHANGELOG.md +417 -0
- package/README.md +295 -19
- package/hosts/orgs/mod.d.ts +4 -1
- package/hosts/orgs/mod.js +2 -2
- package/hosts/web/files.d.ts +6 -2
- package/hosts/web/files.js +36 -3
- package/hosts/web/node/mod.d.ts +1 -1
- package/hosts/web/node/mod.js +1 -1
- package/hosts/web/node/serve.d.ts +26 -0
- package/hosts/web/node/serve.js +23 -1
- package/hosts/web/routes.d.ts +28 -1
- package/hosts/web/routes.js +65 -10
- package/hosts/web/server.d.ts +5 -4
- package/hosts/web/server.js +1 -1
- package/package.json +2 -2
- package/plugins/access/accounts/collections/account.d.ts +2 -2
- package/plugins/access/contacts/actions/chat.d.ts +1 -0
- package/plugins/access/contacts/actions/chat.js +45 -0
- package/plugins/access/contacts/actions/forget.d.ts +1 -0
- package/plugins/access/contacts/actions/forget.js +56 -0
- package/plugins/access/contacts/actions/start.d.ts +1 -0
- package/plugins/access/contacts/actions/start.js +50 -0
- package/plugins/access/contacts/collections/contact.d.ts +81 -0
- package/plugins/access/contacts/collections/contact.js +58 -0
- package/plugins/access/contacts/contract.d.ts +10 -0
- package/plugins/access/contacts/contract.js +10 -0
- package/plugins/access/contacts/mod.d.ts +27 -0
- package/plugins/access/contacts/mod.js +49 -0
- package/plugins/access/contacts/views/contact.d.ts +8 -0
- package/plugins/access/contacts/views/contact.js +25 -0
- package/plugins/access/contacts/web.d.ts +7 -0
- package/plugins/access/contacts/web.js +11 -0
- package/plugins/access/directory/collections/credit-code.d.ts +1 -1
- package/plugins/access/directory/collections/invite.d.ts +1 -1
- package/plugins/access/identity/mod.js +8 -6
- package/plugins/access/mod.d.ts +7 -1
- package/plugins/access/mod.js +7 -2
- package/plugins/access/spaces/actions/agents.js +12 -6
- package/plugins/access/spaces/actions/create.js +10 -1
- package/plugins/access/spaces/actions/invitation.js +5 -2
- package/plugins/access/spaces/actions/keep.d.ts +1 -0
- package/plugins/access/spaces/actions/keep.js +55 -0
- package/plugins/access/spaces/actions/member.js +17 -4
- package/plugins/access/spaces/actions/rename.js +4 -0
- package/plugins/access/spaces/collections/member.d.ts +1 -1
- package/plugins/access/spaces/collections/request.d.ts +1 -1
- package/plugins/access/spaces/collections/space.d.ts +9 -0
- package/plugins/access/spaces/collections/space.js +2 -0
- package/plugins/access/spaces/contract.d.ts +4 -3
- package/plugins/access/spaces/contract.js +1 -1
- package/plugins/access/spaces/mod.d.ts +7 -1
- package/plugins/access/spaces/mod.js +8 -1
- package/plugins/access/spaces/resources/tools.d.ts +0 -5
- package/plugins/access/spaces/resources/tools.js +11 -1
- package/plugins/access/spaces/shared/agents.js +2 -2
- package/plugins/access/spaces/shared/can.d.ts +18 -3
- package/plugins/access/spaces/shared/can.js +32 -5
- package/plugins/access/spaces/shared/declared.d.ts +21 -0
- package/plugins/access/spaces/shared/declared.js +8 -0
- package/plugins/access/spaces/shared/labels.d.ts +7 -1
- package/plugins/access/spaces/shared/labels.js +56 -8
- package/plugins/access/spaces/shared/managers.js +2 -2
- package/plugins/access/spaces/shared/names.d.ts +9 -5
- package/plugins/access/spaces/shared/names.js +36 -19
- package/plugins/access/spaces/shared/reach.d.ts +20 -6
- package/plugins/access/spaces/shared/reach.js +62 -20
- package/plugins/access/spaces/shared/rules.d.ts +10 -5
- package/plugins/access/spaces/shared/rules.js +35 -10
- package/plugins/access/spaces/shared/seat.d.ts +3 -2
- package/plugins/access/spaces/shared/seat.js +9 -2
- package/plugins/access/spaces/views/where.d.ts +4 -2
- package/plugins/access/spaces/views/where.js +9 -0
- package/plugins/access/spaces/web.js +1 -1
- package/plugins/access/web.d.ts +1 -0
- package/plugins/access/web.js +1 -0
- package/plugins/agents/chat/actions/erase.d.ts +1 -0
- package/plugins/agents/chat/actions/erase.js +51 -0
- package/plugins/agents/chat/actions/open.d.ts +1 -0
- package/plugins/agents/chat/actions/open.js +49 -0
- package/plugins/agents/chat/actions/quiet.d.ts +1 -0
- package/plugins/agents/chat/actions/quiet.js +33 -0
- package/plugins/agents/chat/actions/say.d.ts +1 -0
- package/plugins/agents/chat/actions/say.js +82 -0
- package/plugins/agents/chat/actions/send.js +57 -19
- package/plugins/agents/chat/actions/shown.d.ts +1 -0
- package/plugins/agents/chat/actions/shown.js +35 -0
- package/plugins/agents/chat/collections/intent.d.ts +124 -0
- package/plugins/agents/chat/collections/intent.js +52 -0
- package/plugins/agents/chat/collections/message.d.ts +74 -1
- package/plugins/agents/chat/collections/message.js +23 -1
- package/plugins/agents/chat/collections/shown.d.ts +105 -0
- package/plugins/agents/chat/collections/shown.js +34 -0
- package/plugins/agents/chat/contract.d.ts +15 -7
- package/plugins/agents/chat/contract.js +7 -1
- package/plugins/agents/chat/mod.d.ts +20 -2
- package/plugins/agents/chat/mod.js +43 -4
- package/plugins/agents/chat/processors/failed.js +15 -5
- package/plugins/agents/chat/processors/turn.d.ts +1 -1
- package/plugins/agents/chat/processors/turn.js +95 -35
- package/plugins/agents/chat/shared/delivery.d.ts +16 -2
- package/plugins/agents/chat/shared/delivery.js +62 -7
- package/plugins/agents/chat/shared/send.d.ts +1 -3
- package/plugins/agents/chat/shared/send.js +2 -9
- package/plugins/agents/chat/shared/tools.d.ts +33 -0
- package/plugins/agents/chat/shared/tools.js +28 -0
- package/plugins/agents/chat/shared/types.d.ts +59 -1
- package/plugins/agents/chat/views/message.d.ts +4 -2
- package/plugins/agents/chat/views/message.js +22 -4
- package/plugins/agents/chat/views/prompt.js +38 -4
- package/plugins/agents/history/collections/outline.d.ts +1 -1
- package/plugins/agents/history/collections/summary.d.ts +1 -1
- package/plugins/agents/history/mod.js +2 -1
- package/plugins/agents/history/processors/erase.d.ts +3 -0
- package/plugins/agents/history/processors/erase.js +11 -0
- package/plugins/agents/memory/mod.js +2 -1
- package/plugins/agents/memory/processors/extract.d.ts +1 -1
- package/plugins/agents/memory/processors/extract.js +5 -0
- package/plugins/agents/memory/resources/tools.js +1 -1
- package/plugins/agents/memory/shared/settings.d.ts +7 -2
- package/plugins/agents/memory/shared/settings.js +9 -1
- package/plugins/agents/routines/collections/routine.d.ts +1 -1
- package/plugins/agents/routines/collections/run.d.ts +1 -1
- package/plugins/compute/apps/collections/app.d.ts +2 -2
- package/plugins/compute/cloud/resources/tools.js +3 -3
- package/plugins/compute/computers/collections/approval.d.ts +2 -2
- package/plugins/compute/computers/collections/grant.d.ts +1 -1
- package/plugins/compute/computers/collections/preview.d.ts +2 -2
- package/plugins/compute/computers/program/version.d.ts +1 -1
- package/plugins/compute/computers/program/version.js +1 -1
- package/plugins/compute/computers/resources/downloads.js +1 -1
- package/plugins/compute/computers/shared/wake.d.ts +1 -1
- package/plugins/compute/computers/shared/wake.js +17 -0
- package/plugins/interfaces/api/collections/key.d.ts +1 -1
- package/plugins/interfaces/api/shared/events.js +5 -4
- package/plugins/interfaces/maintenance/actions/read.d.ts +4 -0
- package/plugins/interfaces/maintenance/actions/read.js +76 -0
- package/plugins/interfaces/maintenance/actions/write.d.ts +4 -0
- package/plugins/interfaces/maintenance/actions/write.js +64 -0
- package/plugins/interfaces/maintenance/contract.d.ts +60 -0
- package/plugins/interfaces/maintenance/contract.js +2 -0
- package/plugins/interfaces/maintenance/mod.d.ts +52 -0
- package/plugins/interfaces/maintenance/mod.js +66 -0
- package/plugins/interfaces/maintenance/resources/routes.d.ts +71 -0
- package/plugins/interfaces/maintenance/resources/routes.js +457 -0
- package/plugins/interfaces/maintenance/shared/ops.d.ts +4 -0
- package/plugins/interfaces/maintenance/shared/ops.js +19 -0
- package/plugins/interfaces/mcp/actions/call.d.ts +11 -0
- package/plugins/interfaces/mcp/actions/call.js +304 -0
- package/plugins/interfaces/mcp/actions/prune.d.ts +1 -0
- package/plugins/interfaces/mcp/actions/prune.js +18 -0
- package/plugins/interfaces/mcp/collections/closed.d.ts +23 -0
- package/plugins/interfaces/mcp/collections/closed.js +17 -0
- package/plugins/interfaces/mcp/collections/rate.d.ts +27 -0
- package/plugins/interfaces/mcp/collections/rate.js +18 -0
- package/plugins/interfaces/mcp/collections/receipt.d.ts +46 -0
- package/plugins/interfaces/mcp/collections/receipt.js +46 -0
- package/plugins/interfaces/mcp/contract.d.ts +9 -0
- package/plugins/interfaces/mcp/contract.js +9 -0
- package/plugins/interfaces/mcp/mod.d.ts +84 -0
- package/plugins/interfaces/mcp/mod.js +122 -0
- package/plugins/interfaces/mcp/processors/closed.d.ts +13 -0
- package/plugins/interfaces/mcp/processors/closed.js +16 -0
- package/plugins/interfaces/mcp/resources/running.d.ts +21 -0
- package/plugins/interfaces/mcp/resources/running.js +36 -0
- package/plugins/interfaces/mcp/resources/server.d.ts +54 -0
- package/plugins/interfaces/mcp/resources/server.js +423 -0
- package/plugins/interfaces/mcp/resources/tools.d.ts +114 -0
- package/plugins/interfaces/mcp/resources/tools.js +324 -0
- package/plugins/interfaces/mcp/shared/caller.d.ts +45 -0
- package/plugins/interfaces/mcp/shared/caller.js +99 -0
- package/plugins/interfaces/mcp/shared/keys.d.ts +50 -0
- package/plugins/interfaces/mcp/shared/keys.js +53 -0
- package/plugins/interfaces/mcp/shared/settings.d.ts +187 -0
- package/plugins/interfaces/mcp/shared/settings.js +176 -0
- package/plugins/interfaces/mcp/views/offered.d.ts +20 -0
- package/plugins/interfaces/mcp/views/offered.js +20 -0
- package/plugins/interfaces/mod.d.ts +5 -1
- package/plugins/interfaces/mod.js +5 -1
- package/plugins/interfaces/terminal/actions/print.js +1 -1
- package/plugins/interfaces/terminal/actions/set.js +1 -1
- package/plugins/interfaces/terminal/collections/output.d.ts +1 -1
- package/plugins/interfaces/terminal/collections/shell.js +1 -1
- package/plugins/interfaces/voice/actions/block.d.ts +2 -0
- package/plugins/interfaces/voice/actions/block.js +74 -0
- package/plugins/interfaces/voice/actions/call.d.ts +19 -0
- package/plugins/interfaces/voice/actions/call.js +253 -0
- package/plugins/interfaces/voice/actions/hook.d.ts +5 -0
- package/plugins/interfaces/voice/actions/hook.js +225 -0
- package/plugins/interfaces/voice/actions/prune.d.ts +1 -0
- package/plugins/interfaces/voice/actions/prune.js +35 -0
- package/plugins/interfaces/voice/collections/blocked.d.ts +87 -0
- package/plugins/interfaces/voice/collections/blocked.js +34 -0
- package/plugins/interfaces/voice/collections/call.d.ts +279 -0
- package/plugins/interfaces/voice/collections/call.js +77 -0
- package/plugins/interfaces/voice/collections/phone.d.ts +74 -0
- package/plugins/interfaces/voice/collections/phone.js +26 -0
- package/plugins/interfaces/voice/contract.d.ts +14 -0
- package/plugins/interfaces/voice/contract.js +2 -0
- package/plugins/interfaces/voice/mod.d.ts +57 -0
- package/plugins/interfaces/voice/mod.js +118 -0
- package/plugins/interfaces/voice/processors/forget.d.ts +13 -0
- package/plugins/interfaces/voice/processors/forget.js +27 -0
- package/plugins/interfaces/voice/resources/relay.d.ts +77 -0
- package/plugins/interfaces/voice/resources/relay.js +428 -0
- package/plugins/interfaces/voice/resources/tools.d.ts +1 -0
- package/plugins/interfaces/voice/resources/tools.js +25 -0
- package/plugins/interfaces/voice/shared/caller.d.ts +27 -0
- package/plugins/interfaces/voice/shared/caller.js +38 -0
- package/plugins/interfaces/voice/shared/settings.d.ts +136 -0
- package/plugins/interfaces/voice/shared/settings.js +121 -0
- package/plugins/interfaces/voice/shared/signature.d.ts +11 -0
- package/plugins/interfaces/voice/shared/signature.js +34 -0
- package/plugins/interfaces/voice/views/call.d.ts +41 -0
- package/plugins/interfaces/voice/views/call.js +30 -0
- package/plugins/interfaces/voice/views/context.d.ts +9 -0
- package/plugins/interfaces/voice/views/context.js +14 -0
- package/plugins/interfaces/voice/views/message.d.ts +17 -0
- package/plugins/interfaces/voice/views/message.js +41 -0
- package/plugins/interfaces/whatsapp/actions/connect.js +3 -0
- package/plugins/interfaces/whatsapp/actions/disconnect.js +4 -1
- package/plugins/interfaces/whatsapp/actions/hook.js +27 -11
- package/plugins/interfaces/whatsapp/actions/set.d.ts +8 -0
- package/plugins/interfaces/whatsapp/actions/set.js +121 -0
- package/plugins/interfaces/whatsapp/collections/blocked.d.ts +29 -0
- package/plugins/interfaces/whatsapp/collections/blocked.js +22 -0
- package/plugins/interfaces/whatsapp/collections/code.d.ts +1 -1
- package/plugins/interfaces/whatsapp/collections/number.d.ts +90 -4
- package/plugins/interfaces/whatsapp/collections/number.js +36 -4
- package/plugins/interfaces/whatsapp/collections/phone.d.ts +13 -3
- package/plugins/interfaces/whatsapp/collections/phone.js +9 -3
- package/plugins/interfaces/whatsapp/collections/rate.d.ts +27 -0
- package/plugins/interfaces/whatsapp/collections/rate.js +13 -0
- package/plugins/interfaces/whatsapp/collections/sent.d.ts +24 -12
- package/plugins/interfaces/whatsapp/collections/sent.js +11 -7
- package/plugins/interfaces/whatsapp/collections/thread.d.ts +14 -6
- package/plugins/interfaces/whatsapp/collections/thread.js +7 -5
- package/plugins/interfaces/whatsapp/collections/typing.d.ts +70 -0
- package/plugins/interfaces/whatsapp/collections/typing.js +21 -0
- package/plugins/interfaces/whatsapp/contract.d.ts +27 -4
- package/plugins/interfaces/whatsapp/contract.js +34 -1
- package/plugins/interfaces/whatsapp/events/undelivered.d.ts +8 -0
- package/plugins/interfaces/whatsapp/events/undelivered.js +17 -0
- package/plugins/interfaces/whatsapp/mod.d.ts +28 -2
- package/plugins/interfaces/whatsapp/mod.js +65 -6
- package/plugins/interfaces/whatsapp/processors/forget.d.ts +6 -0
- package/plugins/interfaces/whatsapp/processors/forget.js +11 -0
- package/plugins/interfaces/whatsapp/processors/receive.d.ts +17 -3
- package/plugins/interfaces/whatsapp/processors/receive.js +264 -81
- package/plugins/interfaces/whatsapp/processors/reply.d.ts +25 -5
- package/plugins/interfaces/whatsapp/processors/reply.js +216 -20
- package/plugins/interfaces/whatsapp/processors/typing.d.ts +8 -0
- package/plugins/interfaces/whatsapp/processors/typing.js +25 -0
- package/plugins/interfaces/whatsapp/shared/customers.d.ts +13 -0
- package/plugins/interfaces/whatsapp/shared/customers.js +16 -0
- package/plugins/interfaces/whatsapp/shared/graph.d.ts +31 -6
- package/plugins/interfaces/whatsapp/shared/graph.js +117 -10
- package/plugins/interfaces/whatsapp/shared/media.d.ts +23 -0
- package/plugins/interfaces/whatsapp/shared/media.js +46 -0
- package/plugins/interfaces/whatsapp/shared/number.d.ts +19 -8
- package/plugins/interfaces/whatsapp/shared/number.js +64 -7
- package/plugins/interfaces/whatsapp/shared/parts.d.ts +94 -0
- package/plugins/interfaces/whatsapp/shared/parts.js +184 -0
- package/plugins/interfaces/whatsapp/shared/payload.d.ts +27 -1
- package/plugins/interfaces/whatsapp/shared/payload.js +36 -1
- package/plugins/interfaces/whatsapp/shared/settings.d.ts +49 -1
- package/plugins/interfaces/whatsapp/shared/settings.js +11 -0
- package/plugins/interfaces/whatsapp/views/message.d.ts +13 -0
- package/plugins/interfaces/whatsapp/views/message.js +13 -0
- package/plugins/interfaces/whatsapp/views/web/line.d.ts +24 -0
- package/plugins/interfaces/whatsapp/views/web/line.js +44 -0
- package/plugins/interfaces/whatsapp/views/web/space.d.ts +7 -5
- package/plugins/interfaces/whatsapp/views/web/space.js +17 -10
- package/plugins/interfaces/whatsapp/views/web/style.d.ts +1 -1
- package/plugins/interfaces/whatsapp/views/web/style.js +8 -0
- package/plugins/models/adapters/llm/anthropic.d.ts +1 -1
- package/plugins/models/adapters/llm/anthropic.js +28 -15
- package/plugins/models/adapters/llm/gemini.d.ts +1 -1
- package/plugins/models/adapters/llm/gemini.js +2 -1
- package/plugins/models/adapters/llm/openai-compatible.d.ts +1 -1
- package/plugins/models/adapters/llm/openai-compatible.js +2 -1
- package/plugins/models/adapters/llm/openai.d.ts +1 -1
- package/plugins/models/adapters/llm/openai.js +16 -4
- package/plugins/models/shared/types.d.ts +8 -0
- package/plugins/models/shared/types.js +10 -0
- package/runtime/actor.d.ts +6 -0
- package/runtime/actor.js +9 -4
- package/runtime/aggregate.js +8 -2
- package/runtime/blobs.d.ts +15 -12
- package/runtime/blobs.js +58 -11
- package/runtime/cache.d.ts +6 -0
- package/runtime/cache.js +19 -1
- package/runtime/define.d.ts +10 -0
- package/runtime/errors.d.ts +20 -2
- package/runtime/errors.js +7 -1
- package/runtime/executor.js +27 -11
- package/runtime/links.d.ts +31 -0
- package/runtime/links.js +60 -0
- package/runtime/maintain.d.ts +88 -0
- package/runtime/maintain.js +120 -0
- package/runtime/memory.js +7 -4
- package/runtime/mod.d.ts +6 -4
- package/runtime/mod.js +5 -2
- package/runtime/mongo.d.ts +13 -0
- package/runtime/mongo.js +39 -8
- package/runtime/observe.d.ts +2 -0
- package/runtime/observe.js +14 -10
- package/runtime/ops.d.ts +5 -0
- package/runtime/ops.js +10 -1
- package/runtime/owner.d.ts +18 -1
- package/runtime/owner.js +127 -61
- package/runtime/presence.d.ts +18 -0
- package/runtime/presence.js +25 -0
- package/runtime/registry.d.ts +5 -0
- package/runtime/registry.js +18 -0
- package/runtime/runtime.js +9 -3
- package/runtime/store.d.ts +48 -1
- package/runtime/wire.d.ts +1 -0
- package/runtime/wire.js +3 -1
- package/stores/blobs/files/mod.js +4 -2
- package/stores/blobs/gcs/mod.d.ts +3 -0
- package/stores/blobs/gcs/mod.js +6 -5
- package/stores/blobs/http/mod.d.ts +1 -1
- package/stores/blobs/http/mod.js +2 -1
- package/stores/latency/mod.js +5 -1
- package/stores/sql/postgres/maintenance.d.ts +24 -0
- package/stores/sql/postgres/maintenance.js +258 -0
- package/stores/sql/postgres/mod.d.ts +20 -4
- package/stores/sql/postgres/mod.js +363 -96
- package/stores/sql/postgres/update.js +3 -1
- package/stores/sql/shared/capped.d.ts +2 -0
- package/stores/sql/shared/capped.js +22 -0
- package/stores/sql/shared/rows.d.ts +14 -0
- package/stores/sql/shared/rows.js +29 -0
- package/stores/sql/shared/statement.d.ts +12 -0
- package/stores/sql/shared/statement.js +244 -0
- package/stores/sql/sqlite/apart.d.ts +11 -0
- package/stores/sql/sqlite/apart.js +219 -0
- package/stores/sql/sqlite/mod.d.ts +16 -2
- package/stores/sql/sqlite/mod.js +246 -51
- package/testing/mod.js +1 -1
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
/** An agent's turn: model calls and tool steps until it answers, or waits for replies. */
|
|
2
|
+
import { principalOf, } from "../../../../runtime/mod.js";
|
|
3
|
+
import { agentsNamed, modelOf, NO_ANSWER, now, peopleIn, SEND, teamOf, turnOf, } from "../shared/agents.js";
|
|
2
4
|
import { answer, delivery } from "../shared/delivery.js";
|
|
3
5
|
import { liveWriter } from "../shared/live.js";
|
|
4
|
-
import { prepare
|
|
6
|
+
import { prepare } from "../shared/send.js";
|
|
5
7
|
import { noted, startedWith } from "../shared/summary.js";
|
|
8
|
+
import { runTool, toolText } from "../shared/tools.js";
|
|
6
9
|
import { settingsOf } from "../shared/types.js";
|
|
7
10
|
/** Runs a turn: started by `turn.requested`, or resumed when its wait is over. */
|
|
8
11
|
/**
|
|
9
|
-
* A turn someone started
|
|
10
|
-
* plugin, it reaches only what both reach
|
|
11
|
-
* like a person). Without anyone, it runs
|
|
12
|
+
* A turn someone started (a person, or a contact in its chat) acts as its
|
|
13
|
+
* agent, for them: with the access plugin, it reaches only what both reach
|
|
14
|
+
* (an agent is a member of spaces, like a person). Without anyone, it runs
|
|
15
|
+
* as the system.
|
|
12
16
|
*/
|
|
13
17
|
const asAgent = (ctx, agent, at) => {
|
|
14
18
|
const principal = principalOf(ctx.actor);
|
|
@@ -53,27 +57,38 @@ export const turnProcessor = {
|
|
|
53
57
|
const turn = async (ctx, event) => {
|
|
54
58
|
const t = turnOf(event);
|
|
55
59
|
const run = event.id;
|
|
56
|
-
const tools = (ctx.resources.tools ?? {});
|
|
57
60
|
// What it is: the app's, or one people made that works where this thread is.
|
|
58
61
|
const agent = (await agentsNamed(ctx, [t.agent]))[t.agent];
|
|
59
62
|
if (!agent)
|
|
60
63
|
throw new Error(`No agent "${t.agent}" works here`);
|
|
61
64
|
const messages = ctx.db.collection("message");
|
|
62
|
-
|
|
65
|
+
const intents = ctx.db.collection("intent");
|
|
66
|
+
// A retried run continues after the steps it committed, if it didn't finish,
|
|
67
|
+
// and goes on with the step it was taking if that step's calls were written.
|
|
63
68
|
let step = 0;
|
|
69
|
+
let open = null;
|
|
64
70
|
if (ctx.retry) {
|
|
65
|
-
const done = await
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
71
|
+
const [done, waiting, taking] = await Promise.all([
|
|
72
|
+
messages.find({ turn: run }),
|
|
73
|
+
ctx.db.collection("wait").findOne({ _id: `${run}:wait` }),
|
|
74
|
+
intents.findOne({ _id: run }),
|
|
75
|
+
]);
|
|
76
|
+
if (done.some((m) => m._id === `${run}:reply`) || waiting)
|
|
69
77
|
return;
|
|
70
78
|
step = done.length;
|
|
79
|
+
if (taking?.step === step)
|
|
80
|
+
open = taking;
|
|
71
81
|
}
|
|
72
82
|
// What it is sent is the thread's llm View: its instructions, the
|
|
73
83
|
// conversation, and what contexts add.
|
|
74
84
|
const viewer = { kind: "agent", name: t.agent };
|
|
75
85
|
const current = await ctx.db.collection("thread").findOne({ _id: t.thread }) ??
|
|
76
86
|
{ _id: t.thread, at: "" };
|
|
87
|
+
// A person's messages in a row are answered together, by the turn that read
|
|
88
|
+
// them: a later one's own turn, with nothing of it written yet, has nothing to add.
|
|
89
|
+
if (event.type === "turn.requested" && t.hops === 0 && step === 0 && !open &&
|
|
90
|
+
current.latest !== t.trigger && await answered(ctx, t))
|
|
91
|
+
return;
|
|
77
92
|
const composed = await ctx.render("llm", "thread", current, { viewer });
|
|
78
93
|
const instructions = composed.system.join("\n\n");
|
|
79
94
|
// The conversation up to here repeats in the next calls: a cache point, before what's new each turn.
|
|
@@ -107,7 +122,11 @@ const turn = async (ctx, event) => {
|
|
|
107
122
|
let text = "";
|
|
108
123
|
let state;
|
|
109
124
|
const calls = [];
|
|
110
|
-
|
|
125
|
+
// A step whose calls were written goes on with them: its model isn't asked again.
|
|
126
|
+
const resumed = open;
|
|
127
|
+
open = null;
|
|
128
|
+
const ask = (input) => resumed ? replay(resumed) : ctx.call("llm.stream", input);
|
|
129
|
+
for await (const chunk of await ask({
|
|
111
130
|
model,
|
|
112
131
|
instructions,
|
|
113
132
|
messages: conversation,
|
|
@@ -139,14 +158,34 @@ const turn = async (ctx, event) => {
|
|
|
139
158
|
if (!calls.length) {
|
|
140
159
|
await live.settle();
|
|
141
160
|
// Its answer keeps what it was told, and its state: what it thought goes back with it.
|
|
161
|
+
// And how far it had read the thread: what came after, it reads after this answer.
|
|
142
162
|
return await answer(ctx, t, run, text, {
|
|
143
163
|
...(step === first ? { told } : {}),
|
|
144
164
|
...(state ? { state } : {}),
|
|
165
|
+
...(current.at ? { knew: current.at } : {}),
|
|
145
166
|
}, current);
|
|
146
167
|
}
|
|
147
168
|
// Ids derive from the run, so a retry rewrites nothing it already committed.
|
|
148
169
|
const id = `${run}:${step}`;
|
|
149
|
-
const at = now(); // The step comes before what it sends.
|
|
170
|
+
const at = resumed?.at ?? now(); // The step comes before what it sends.
|
|
171
|
+
// The turn's first message keeps what it was told.
|
|
172
|
+
const kept = told.length && step === first ? { told } : {};
|
|
173
|
+
// Before any of its calls runs, the step is written, so a retry runs these
|
|
174
|
+
// same calls rather than ask again. Messages need none: they go with the step.
|
|
175
|
+
const intended = !!resumed || calls.some((call) => !(messaging && call.name === SEND));
|
|
176
|
+
if (intended && !resumed) {
|
|
177
|
+
await intents.replaceOne({ _id: run }, {
|
|
178
|
+
thread: t.thread,
|
|
179
|
+
agent: t.agent,
|
|
180
|
+
step,
|
|
181
|
+
...(text ? { text } : {}),
|
|
182
|
+
...(state ? { state } : {}),
|
|
183
|
+
...kept,
|
|
184
|
+
calls,
|
|
185
|
+
outputs: {},
|
|
186
|
+
at,
|
|
187
|
+
}, { upsert: true });
|
|
188
|
+
}
|
|
150
189
|
// The step's calls run together, each in a branch of its own, so a retry
|
|
151
190
|
// finds what each one called whatever order they started in. Their
|
|
152
191
|
// results keep the model's order.
|
|
@@ -154,27 +193,31 @@ const turn = async (ctx, event) => {
|
|
|
154
193
|
live.call({ id: call.id, name: call.name, input: call.input });
|
|
155
194
|
let output;
|
|
156
195
|
let send;
|
|
196
|
+
const returned = resumed?.outputs[i];
|
|
157
197
|
if (messaging && call.name === SEND) {
|
|
158
198
|
send = await prepare(ctx, t, id, `${id}:${i}`, call.input, here, known, current.in);
|
|
159
199
|
output = send.output;
|
|
160
200
|
}
|
|
201
|
+
else if (returned !== undefined) {
|
|
202
|
+
// It returned before the retry: it doesn't run again.
|
|
203
|
+
output = returned;
|
|
204
|
+
}
|
|
161
205
|
else {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
check(call.name, tools[call.name])(call.input);
|
|
167
|
-
const result = await tools[call.name].run(call.input, {
|
|
168
|
-
...ctx.branch(`${step}.${i}`),
|
|
206
|
+
// Only a tool offered to it here; each in a branch of its own, as chat's runner runs them.
|
|
207
|
+
output = offered.some((tool) => tool.name === call.name)
|
|
208
|
+
? (await runTool(ctx.branch(`${step}.${i}`), call.name, call.input, {
|
|
169
209
|
agent: t.agent,
|
|
170
210
|
thread: t.thread,
|
|
171
211
|
trigger: t.trigger,
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
212
|
+
callId: `${id}:${i}`,
|
|
213
|
+
// Only a call that was written and never returned may have run already.
|
|
214
|
+
retry: !!resumed,
|
|
215
|
+
})).output
|
|
216
|
+
: `Error: Unknown tool "${call.name}"`;
|
|
217
|
+
// What it returned is on record at once: a retry won't run it again. If
|
|
218
|
+
// that write fails, a retry runs it again, as itself.
|
|
219
|
+
await intents.updateOne({ _id: run }, { $set: { [`outputs.${i}`]: output } })
|
|
220
|
+
.catch(() => { });
|
|
178
221
|
}
|
|
179
222
|
live.result(call.id, output);
|
|
180
223
|
const made = { ...call, output, ...(send?.message ? { sent: send.message._id } : {}) };
|
|
@@ -190,12 +233,7 @@ const turn = async (ctx, event) => {
|
|
|
190
233
|
});
|
|
191
234
|
for (const c of done) {
|
|
192
235
|
// As later turns will see it, through its tool's View.
|
|
193
|
-
|
|
194
|
-
conversation.push({
|
|
195
|
-
role: "tool",
|
|
196
|
-
toolCallId: c.id,
|
|
197
|
-
text: typeof shown === "string" ? shown : c.output,
|
|
198
|
-
});
|
|
236
|
+
conversation.push({ role: "tool", toolCallId: c.id, text: await toolText(ctx, c, viewer) });
|
|
199
237
|
}
|
|
200
238
|
// The step, what it sent and what it waits for commit together, so no
|
|
201
239
|
// reply can arrive before the wait it checks off.
|
|
@@ -204,12 +242,12 @@ const turn = async (ctx, event) => {
|
|
|
204
242
|
// Handing work to another space is trusted code's: the agent doesn't work there.
|
|
205
243
|
const writer = sends.some((s) => s.across) ? ctx.as() : ctx;
|
|
206
244
|
const deliveries = await Promise.all(sends.map((s) => delivery(writer, s.message, s.message.subthread)));
|
|
207
|
-
// The turn's first message keeps what it was told.
|
|
208
|
-
const kept = told.length && step === first ? { told } : {};
|
|
209
245
|
await live.settle();
|
|
210
246
|
await writer.transaction(async (tx) => {
|
|
211
|
-
// What showed live becomes the step.
|
|
247
|
+
// What showed live becomes the step, and so does what was written of it.
|
|
212
248
|
await live.done(tx);
|
|
249
|
+
if (intended)
|
|
250
|
+
await tx.db.collection("intent").deleteOne({ _id: run }, NO_ANSWER);
|
|
213
251
|
const step = {
|
|
214
252
|
_id: id,
|
|
215
253
|
thread: t.thread,
|
|
@@ -256,3 +294,25 @@ const turn = async (ctx, event) => {
|
|
|
256
294
|
}
|
|
257
295
|
throw new Error(`Agent "${t.agent}" did not finish within ${maxSteps} steps`);
|
|
258
296
|
};
|
|
297
|
+
/**
|
|
298
|
+
* Whether the person's message that started a turn is answered already: its
|
|
299
|
+
* agent's latest answer here had read it (`knew`), and went to them.
|
|
300
|
+
*/
|
|
301
|
+
async function answered(ctx, t) {
|
|
302
|
+
const messages = ctx.db.collection("message");
|
|
303
|
+
const [trigger, [latest]] = await Promise.all([
|
|
304
|
+
messages.findOne({ _id: t.trigger }),
|
|
305
|
+
messages.find({ thread: t.thread, from: t.agent, knew: { $exists: true } }, { sort: { at: -1 }, limit: 1 }),
|
|
306
|
+
]);
|
|
307
|
+
return !!trigger && !!latest?.knew && latest.knew >= trigger.at &&
|
|
308
|
+
latest.to.includes(trigger.from);
|
|
309
|
+
}
|
|
310
|
+
/** What a written step's model said, said again: what it wrote, its state and its calls. */
|
|
311
|
+
async function* replay(intent) {
|
|
312
|
+
if (intent.text)
|
|
313
|
+
yield { type: "text", text: intent.text };
|
|
314
|
+
if (intent.state)
|
|
315
|
+
yield { type: "state", state: intent.state };
|
|
316
|
+
for (const call of intent.calls)
|
|
317
|
+
yield { type: "tool_call", call };
|
|
318
|
+
}
|
|
@@ -3,6 +3,11 @@ import type { Context, Resources, TransactionScope } from "../../../../runtime/m
|
|
|
3
3
|
import type { LlmState } from "../../../models/contract.js";
|
|
4
4
|
import type { Thread } from "../collections/thread.js";
|
|
5
5
|
import { type Told, type Turn } from "./types.js";
|
|
6
|
+
/**
|
|
7
|
+
* The timer a sender's quiet window is (see `delivery`'s `quiet`): each of
|
|
8
|
+
* their messages sets it again, and nobody else's does.
|
|
9
|
+
*/
|
|
10
|
+
export declare const quietId: (thread: string, sender: string, agent: string) => string;
|
|
6
11
|
/** Whether a message after `hops` agent messages in a row may still start a turn. */
|
|
7
12
|
export declare const starts: (ctx: {
|
|
8
13
|
resources: Resources;
|
|
@@ -26,17 +31,26 @@ export declare const delivery: (ctx: Context, m: {
|
|
|
26
31
|
/** Where the turns it starts run: its thread, or the subthread hanging from it. */
|
|
27
32
|
where?: string,
|
|
28
33
|
/** False: it checks off waits but starts no turn (an answer to a stop). */
|
|
29
|
-
start?: boolean
|
|
34
|
+
start?: boolean,
|
|
35
|
+
/**
|
|
36
|
+
* Ms its sender may write on before agents answer (a customer who writes in
|
|
37
|
+
* bursts): each turn it starts waits that long, and a message sent meanwhile
|
|
38
|
+
* waits again in its place, so the agent answers them all, once.
|
|
39
|
+
*/
|
|
40
|
+
quiet?: number) => Promise<(tx: TransactionScope) => Promise<void>>;
|
|
30
41
|
/**
|
|
31
42
|
* An agent's answer to what started its turn, addressed back to its sender.
|
|
32
43
|
* A turn that failed or was stopped answers too, so whoever waits on it
|
|
33
|
-
* resumes; a stopped one starts no new turn, and ends its own wait.
|
|
44
|
+
* resumes; a stopped one starts no new turn, and ends its own wait. It keeps
|
|
45
|
+
* the step it was taking, if its calls were written: what each returned, or
|
|
46
|
+
* that it never did, for people and for its later turns to see.
|
|
34
47
|
*/
|
|
35
48
|
export declare const answer: (ctx: Context, t: Turn, run: string, text: string, how?: {
|
|
36
49
|
failed?: boolean;
|
|
37
50
|
stopped?: boolean;
|
|
38
51
|
told?: Told[];
|
|
39
52
|
state?: LlmState;
|
|
53
|
+
knew?: string;
|
|
40
54
|
},
|
|
41
55
|
/** The turn's thread, when it has it already. */
|
|
42
56
|
known?: Pick<Thread, "in">) => Promise<void>;
|
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CUT_SHORT, NOT_SENT } from "../collections/intent.js";
|
|
2
|
+
import { agentsNamed, NO_ANSWER, now, SEND } from "./agents.js";
|
|
2
3
|
import { noted } from "./summary.js";
|
|
4
|
+
import { sharedWith } from "../../../access/spaces/contract.js";
|
|
3
5
|
import { DELEGATED, settingsOf } from "./types.js";
|
|
6
|
+
/**
|
|
7
|
+
* The timer a sender's quiet window is (see `delivery`'s `quiet`): each of
|
|
8
|
+
* their messages sets it again, and nobody else's does.
|
|
9
|
+
*/
|
|
10
|
+
export const quietId = (thread, sender, agent) => `quiet:${thread}:${sender}:${agent}`;
|
|
4
11
|
/** Whether a message after `hops` agent messages in a row may still start a turn. */
|
|
5
12
|
export const starts = (ctx, hops) => {
|
|
6
13
|
const { maxHops } = settingsOf(ctx);
|
|
@@ -15,7 +22,13 @@ export const delivery = async (ctx, m,
|
|
|
15
22
|
/** Where the turns it starts run: its thread, or the subthread hanging from it. */
|
|
16
23
|
where = m.thread,
|
|
17
24
|
/** False: it checks off waits but starts no turn (an answer to a stop). */
|
|
18
|
-
start = true
|
|
25
|
+
start = true,
|
|
26
|
+
/**
|
|
27
|
+
* Ms its sender may write on before agents answer (a customer who writes in
|
|
28
|
+
* bursts): each turn it starts waits that long, and a message sent meanwhile
|
|
29
|
+
* waits again in its place, so the agent answers them all, once.
|
|
30
|
+
*/
|
|
31
|
+
quiet = 0) => {
|
|
19
32
|
const agents = await agentsNamed(ctx, m.to);
|
|
20
33
|
const waits = ctx.db.collection("wait");
|
|
21
34
|
const steps = [];
|
|
@@ -32,7 +45,13 @@ start = true) => {
|
|
|
32
45
|
steps.push((tx) => tx.db.collection("wait").updateOne({ _id: waiting._id, waitingFor: m.replyTo }, { $pull: { waitingFor: m.replyTo }, $push: { received: m._id } }, NO_ANSWER));
|
|
33
46
|
}
|
|
34
47
|
else if (start && starts(ctx, m.hops)) {
|
|
35
|
-
|
|
48
|
+
const turn = { thread: where, agent: name, trigger: m._id, hops: m.hops };
|
|
49
|
+
steps.push((tx) => quiet > 0
|
|
50
|
+
? void tx.schedule("chat.quiet", turn, {
|
|
51
|
+
at: Date.now() + quiet,
|
|
52
|
+
id: quietId(where, m.from, name),
|
|
53
|
+
})
|
|
54
|
+
: tx.emit("turn.requested", turn, { id: `${m._id}:${name}` }));
|
|
36
55
|
}
|
|
37
56
|
}
|
|
38
57
|
return async (tx) => {
|
|
@@ -43,15 +62,19 @@ start = true) => {
|
|
|
43
62
|
/**
|
|
44
63
|
* An agent's answer to what started its turn, addressed back to its sender.
|
|
45
64
|
* A turn that failed or was stopped answers too, so whoever waits on it
|
|
46
|
-
* resumes; a stopped one starts no new turn, and ends its own wait.
|
|
65
|
+
* resumes; a stopped one starts no new turn, and ends its own wait. It keeps
|
|
66
|
+
* the step it was taking, if its calls were written: what each returned, or
|
|
67
|
+
* that it never did, for people and for its later turns to see.
|
|
47
68
|
*/
|
|
48
69
|
export const answer = async (ctx, t, run, text, how = {},
|
|
49
70
|
/** The turn's thread, when it has it already. */
|
|
50
71
|
known) => {
|
|
51
72
|
const messages = ctx.db.collection("message");
|
|
52
|
-
const [trigger, thread] = await Promise.all([
|
|
73
|
+
const [trigger, thread, open] = await Promise.all([
|
|
53
74
|
messages.findOne({ _id: t.trigger }),
|
|
54
75
|
known ?? ctx.db.collection("thread").findOne({ _id: t.thread }),
|
|
76
|
+
// A turn ending this way may have been taking a step: it's kept.
|
|
77
|
+
how.failed || how.stopped ? ctx.db.collection("intent").findOne({ _id: run }) : null,
|
|
55
78
|
]);
|
|
56
79
|
const back = trigger?.back && trigger.expectReply !== false ? trigger.back : undefined;
|
|
57
80
|
const reply = {
|
|
@@ -72,9 +95,14 @@ known) => {
|
|
|
72
95
|
...(how.told?.length ? { told: how.told } : {}),
|
|
73
96
|
// Its model's state (its thinking), for that model's next calls: private, like `told`.
|
|
74
97
|
...(how.state ? { state: how.state } : {}),
|
|
98
|
+
// How far its turn had read this thread.
|
|
99
|
+
...(how.knew ? { knew: how.knew } : {}),
|
|
75
100
|
at: now(),
|
|
76
101
|
};
|
|
77
|
-
// Handed over from another space: the answer goes back there too, where its sender waits.
|
|
102
|
+
// Handed over from another space: the answer goes back there too, where its sender waits. Into
|
|
103
|
+
// a chat shared with someone outside its space (a contact), it says where it came from: only
|
|
104
|
+
// those who reach that space read it there.
|
|
105
|
+
const outside = back && thread?.in ? (await sharedWith(ctx, "thread", back.thread)).length : 0;
|
|
78
106
|
const returned = back
|
|
79
107
|
? {
|
|
80
108
|
...reply,
|
|
@@ -89,6 +117,8 @@ known) => {
|
|
|
89
117
|
},
|
|
90
118
|
told: undefined,
|
|
91
119
|
state: undefined,
|
|
120
|
+
knew: undefined,
|
|
121
|
+
...(outside ? { sources: [thread.in] } : {}),
|
|
92
122
|
}
|
|
93
123
|
: undefined;
|
|
94
124
|
const writer = returned ? ctx.as() : ctx;
|
|
@@ -98,13 +128,38 @@ known) => {
|
|
|
98
128
|
await tx.db.collection("live").deleteOne({ _id: run }, NO_ANSWER);
|
|
99
129
|
if (how.stopped)
|
|
100
130
|
await tx.db.collection("wait").deleteOne({ _id: `${run}:wait` }, NO_ANSWER);
|
|
131
|
+
if (open) {
|
|
132
|
+
await tx.db.collection("intent").deleteOne({ _id: run }, NO_ANSWER);
|
|
133
|
+
await tx.db.collection("message").insertOne(stepOf(open, run, thread?.in));
|
|
134
|
+
}
|
|
101
135
|
await tx.db.collection("message").insertOne(reply);
|
|
102
136
|
await noted(tx, reply);
|
|
103
137
|
if (returned) {
|
|
104
|
-
const { told: _, state: __, ...copy } = returned;
|
|
138
|
+
const { told: _, state: __, knew: ___, ...copy } = returned;
|
|
105
139
|
await tx.db.collection("message").insertOne(copy);
|
|
106
140
|
await noted(tx, copy);
|
|
107
141
|
}
|
|
108
142
|
await deliver(tx);
|
|
109
143
|
});
|
|
110
144
|
};
|
|
145
|
+
/**
|
|
146
|
+
* The step a turn was taking when it ended without committing it: each call
|
|
147
|
+
* with what it returned, or that it never returned (its messages unsent).
|
|
148
|
+
*/
|
|
149
|
+
const stepOf = (open, run, space) => ({
|
|
150
|
+
_id: `${run}:${open.step}`,
|
|
151
|
+
thread: open.thread,
|
|
152
|
+
...(space ? { in: space } : {}),
|
|
153
|
+
from: open.agent,
|
|
154
|
+
to: [],
|
|
155
|
+
private: true,
|
|
156
|
+
...(open.text ? { text: open.text } : {}),
|
|
157
|
+
toolCalls: open.calls.map((call, i) => ({
|
|
158
|
+
...call,
|
|
159
|
+
output: open.outputs[i] ?? (call.name === SEND ? NOT_SENT : CUT_SHORT),
|
|
160
|
+
})),
|
|
161
|
+
...(open.state ? { state: open.state } : {}),
|
|
162
|
+
...(open.told ? { told: open.told } : {}),
|
|
163
|
+
turn: run,
|
|
164
|
+
at: open.at,
|
|
165
|
+
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** `send_message`: checking a call, building what it writes, and the tool as agents see it. */
|
|
2
|
-
import
|
|
2
|
+
import { type Context } from "../../../../runtime/mod.js";
|
|
3
3
|
import type { LlmTool } from "../../../models/contract.js";
|
|
4
4
|
import type { Message } from "../collections/message.js";
|
|
5
5
|
import type { Thread } from "../collections/thread.js";
|
|
@@ -14,8 +14,6 @@ export type Send = {
|
|
|
14
14
|
/** It goes to another space: trusted code writes it, once its person may. */
|
|
15
15
|
across?: true;
|
|
16
16
|
};
|
|
17
|
-
/** Whom work is for: the person found along `for` and `via`, carrying any limit or origin of theirs. */
|
|
18
|
-
export declare function principalOf(actor: Actor | undefined): Actor | undefined;
|
|
19
17
|
/** Checks a `send_message` call and builds what it writes; nothing is written yet. */
|
|
20
18
|
export declare const prepare: (ctx: Context, t: Turn, step: string, id: string, input: Record<string, unknown>,
|
|
21
19
|
/** Who answers where the thread is: only they are messaged as agents (any, when it's open). */
|
|
@@ -1,15 +1,8 @@
|
|
|
1
|
+
/** `send_message`: checking a call, building what it writes, and the tool as agents see it. */
|
|
2
|
+
import { principalOf } from "../../../../runtime/mod.js";
|
|
1
3
|
import { agentsNamed, now, rosterOf, SEND } from "./agents.js";
|
|
2
4
|
import { starts } from "./delivery.js";
|
|
3
5
|
import { DELEGATED, settingsOf } from "./types.js";
|
|
4
|
-
/** Whom work is for: the person found along `for` and `via`, carrying any limit or origin of theirs. */
|
|
5
|
-
export function principalOf(actor) {
|
|
6
|
-
for (let a = actor, hops = 0; a && hops < 4; hops++) {
|
|
7
|
-
if (a.kind === "person")
|
|
8
|
-
return a;
|
|
9
|
-
a = (a.for ?? a.via);
|
|
10
|
-
}
|
|
11
|
-
return undefined;
|
|
12
|
-
}
|
|
13
6
|
/**
|
|
14
7
|
* Hands work to an agent of another space, in a new chat there that its members
|
|
15
8
|
* see, for the same person, who must be able to write there. The sender need
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Running one of the app's tools, as an agent's turn does: chat's tool
|
|
3
|
+
* runner, for whoever else runs them (the MCP interface: a model outside
|
|
4
|
+
* calling them), so a tool runs and reads the same however it's called.
|
|
5
|
+
*
|
|
6
|
+
* - `runTool`: its input checked against its schema, then run; what it
|
|
7
|
+
* returns as text (JSON unless it's text already); a throw, or input that
|
|
8
|
+
* doesn't fit, as `Error: <why>`, with the error itself beside it (and
|
|
9
|
+
* `unchecked` when the input didn't fit: it isn't the tool's input). What
|
|
10
|
+
* input doesn't fit says is about the caller's own input: public (see the
|
|
11
|
+
* runtime's `failure`).
|
|
12
|
+
* - `toolText`: what a call's result reads as to a model: its tool's
|
|
13
|
+
* `tool:<name>` View (`llm`), else the result's own text.
|
|
14
|
+
*/
|
|
15
|
+
import type { Context, Viewer } from "../../../../runtime/mod.js";
|
|
16
|
+
import type { Step, ToolContext } from "./types.js";
|
|
17
|
+
/**
|
|
18
|
+
* What a call ran into: its output as the agent's step keeps it, what was
|
|
19
|
+
* thrown, if anything, and whether its input didn't fit the tool's schema.
|
|
20
|
+
*/
|
|
21
|
+
export type ToolRun = {
|
|
22
|
+
output: string;
|
|
23
|
+
error?: unknown;
|
|
24
|
+
unchecked?: true;
|
|
25
|
+
};
|
|
26
|
+
/** Where a call runs: who calls it, in which thread, for what message, and the call's own id. */
|
|
27
|
+
export type ToolAt = Omit<ToolContext, keyof Context> & {
|
|
28
|
+
retry: boolean;
|
|
29
|
+
};
|
|
30
|
+
/** Runs the tool `name` with `input`, as `ctx` (whose reach it has), where `at` says. */
|
|
31
|
+
export declare function runTool(ctx: Context, name: string, input: unknown, at: ToolAt): Promise<ToolRun>;
|
|
32
|
+
/** What a call's result reads as to a model (`viewer`): its tool's View, else its output. */
|
|
33
|
+
export declare function toolText(ctx: Pick<Context, "render">, call: Step, viewer: Viewer): Promise<string>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { check } from "./agents.js";
|
|
2
|
+
/** Runs the tool `name` with `input`, as `ctx` (whose reach it has), where `at` says. */
|
|
3
|
+
export async function runTool(ctx, name, input, at) {
|
|
4
|
+
const tools = (ctx.resources.tools ?? {});
|
|
5
|
+
const tool = Object.hasOwn(tools, name) ? tools[name] : undefined;
|
|
6
|
+
const failed = (error) => `Error: ${error instanceof Error ? error.message : String(error)}`;
|
|
7
|
+
try {
|
|
8
|
+
if (!tool)
|
|
9
|
+
throw new Error(`Unknown tool "${name}"`);
|
|
10
|
+
check(name, tool)(input);
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
const told = Object.assign(error, { public: true });
|
|
14
|
+
return { output: failed(told), error: told, unchecked: true };
|
|
15
|
+
}
|
|
16
|
+
try {
|
|
17
|
+
const result = await tool.run(input, { ...ctx, ...at });
|
|
18
|
+
return { output: typeof result === "string" ? result : JSON.stringify(result) };
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
return { output: failed(error), error };
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** What a call's result reads as to a model (`viewer`): its tool's View, else its output. */
|
|
25
|
+
export async function toolText(ctx, call, viewer) {
|
|
26
|
+
const shown = await ctx.render("llm", `tool:${call.name}`, call, { viewer });
|
|
27
|
+
return typeof shown === "string" ? shown : call.output;
|
|
28
|
+
}
|
|
@@ -43,9 +43,31 @@ export type Tool<I = Record<string, unknown>> = {
|
|
|
43
43
|
run(input: I, ctx: ToolContext): unknown;
|
|
44
44
|
/**
|
|
45
45
|
* Offered to every agent (`true`), to these, or where this says so (say,
|
|
46
|
-
* once a thread is summarized), without being listed in their `tools`.
|
|
46
|
+
* once a thread is summarized), without being listed in their `tools`. A
|
|
47
|
+
* function decides for agents that list it too: where it says no (working
|
|
48
|
+
* for a contact, say), it isn't offered.
|
|
47
49
|
*/
|
|
48
50
|
offer?: Offer;
|
|
51
|
+
/** What people see it called ("Search trips"), where they're shown it: its name otherwise. */
|
|
52
|
+
title?: string;
|
|
53
|
+
/** What it does to the world, for whoever decides how to call it (see `ToolAnnotations`). */
|
|
54
|
+
annotations?: ToolAnnotations;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* What a tool does to the world, as hints for whoever calls it: a model
|
|
58
|
+
* outside (MCP's annotations), or an app asking before a destructive one.
|
|
59
|
+
* Hints, not rules: what it may do is still what its caller may.
|
|
60
|
+
*
|
|
61
|
+
* - `readOnly`: it changes nothing (a search).
|
|
62
|
+
* - `destructive`: it may change or remove what's there, not only add.
|
|
63
|
+
* - `idempotent`: calling it again with the same input changes nothing more.
|
|
64
|
+
* - `openWorld`: it reaches outside the app (another company's system, the web).
|
|
65
|
+
*/
|
|
66
|
+
export type ToolAnnotations = {
|
|
67
|
+
readOnly?: boolean;
|
|
68
|
+
destructive?: boolean;
|
|
69
|
+
idempotent?: boolean;
|
|
70
|
+
openWorld?: boolean;
|
|
49
71
|
};
|
|
50
72
|
/** Whom a tool is offered to: everyone, these agents, or decided per agent and thread. */
|
|
51
73
|
export type Offer = true | string[] | ((agent: string, thread: Thread, ctx: ViewContext) => boolean);
|
|
@@ -57,6 +79,15 @@ export type ToolContext = Context & {
|
|
|
57
79
|
agent: string;
|
|
58
80
|
thread: string;
|
|
59
81
|
trigger: string;
|
|
82
|
+
/**
|
|
83
|
+
* This call's id, the same however many times it runs: the idempotency key
|
|
84
|
+
* for what it does outside (a payment, an email). A call returns once: a
|
|
85
|
+
* retried turn reuses what it returned. One cut short (its executor lost,
|
|
86
|
+
* the runtime closed) runs again as itself, with `retry` true, and the
|
|
87
|
+
* recorded Actions it called answer from the log; `retry` is false when the
|
|
88
|
+
* call never ran before.
|
|
89
|
+
*/
|
|
90
|
+
callId: string;
|
|
60
91
|
};
|
|
61
92
|
/** A tool whose `run` input type comes from its JSON Schema. */
|
|
62
93
|
export declare const tool: <const S extends JSONSchema>(def: {
|
|
@@ -64,6 +95,8 @@ export declare const tool: <const S extends JSONSchema>(def: {
|
|
|
64
95
|
input?: S;
|
|
65
96
|
run(input: Infer<S>, ctx: ToolContext): unknown;
|
|
66
97
|
offer?: Offer;
|
|
98
|
+
title?: string;
|
|
99
|
+
annotations?: ToolAnnotations;
|
|
67
100
|
}) => Tool;
|
|
68
101
|
/** Chat's settings: the `chat` resource, which an app patches (`resources: { chat: { maxHops: 3 } }`). */
|
|
69
102
|
export type ChatSettings = {
|
|
@@ -196,6 +229,31 @@ export type ContextEntry = {
|
|
|
196
229
|
agents?: string[];
|
|
197
230
|
instructions?: boolean;
|
|
198
231
|
};
|
|
232
|
+
/**
|
|
233
|
+
* An `additions` resource: what a feature shows after an agent's final
|
|
234
|
+
* answer, in a medium that can (WhatsApp: a carousel of trips after the
|
|
235
|
+
* answer that found them). Its content is a View, `addition:<name>`, in that
|
|
236
|
+
* medium, given `{ thread, answer }`: it returns an `Addition`, several, or
|
|
237
|
+
* nothing. Lowest `order` first; only after the answers of `agents`, when it
|
|
238
|
+
* names them.
|
|
239
|
+
*/
|
|
240
|
+
export type AdditionEntry = {
|
|
241
|
+
order?: number;
|
|
242
|
+
agents?: string[];
|
|
243
|
+
};
|
|
244
|
+
/**
|
|
245
|
+
* What an `addition:<name>` View returns: the parts to show (each in the
|
|
246
|
+
* medium's own terms), once per chat per `key` (at most 120 of `[\w:.-]`:
|
|
247
|
+
* `trips:9f2a`), what goes instead when the medium refuses a part
|
|
248
|
+
* (`fallback`: the trips as a text list), and a line the model is told after
|
|
249
|
+
* that answer (`note`: "6 trips as a carousel").
|
|
250
|
+
*/
|
|
251
|
+
export type Addition<Part = unknown> = {
|
|
252
|
+
key: string;
|
|
253
|
+
parts: Part[];
|
|
254
|
+
fallback?: Part[];
|
|
255
|
+
note?: string;
|
|
256
|
+
};
|
|
199
257
|
/** Chat's settings, as configured. */
|
|
200
258
|
export declare const settingsOf: (ctx: {
|
|
201
259
|
resources: Resources;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { ViewContext } from "../../../../runtime/mod.js";
|
|
2
2
|
import type { LlmMessage } from "../../../models/contract.js";
|
|
3
|
-
import type
|
|
3
|
+
import { type Message } from "../collections/message.js";
|
|
4
4
|
/**
|
|
5
5
|
* A message as the viewing agent sees it: its own as its turns (after what it
|
|
6
6
|
* was told then), others' as labelled input. So is what it handed between
|
|
7
|
-
* spaces itself: that was other work, in another chat, not this turn's.
|
|
7
|
+
* spaces itself: that was other work, in another chat, not this turn's. A
|
|
8
|
+
* choice tapped says what it was (`[tapped: trip:123]`); an answer, what went
|
|
9
|
+
* out after it.
|
|
8
10
|
*/
|
|
9
11
|
export declare function messageView(m: Message, ctx: ViewContext): LlmMessage[];
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { FILE_CHARS, text, textual } from "../collections/file.js";
|
|
2
|
+
import { isAnswer } from "../collections/message.js";
|
|
2
3
|
import { DELEGATED } from "../shared/types.js";
|
|
3
4
|
import { nameOf, namesIn } from "../../../access/spaces/contract.js";
|
|
4
5
|
/** Files a model may be shown as they are: images and PDFs (the models plugin decides, by model). */
|
|
@@ -34,14 +35,29 @@ function attached(m, ctx) {
|
|
|
34
35
|
});
|
|
35
36
|
return { text: lines.join(""), files };
|
|
36
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* What went out after an answer beyond its text (an addition: a carousel of
|
|
40
|
+
* trips), as the model is told: `[then shown: <note>]`. One read per pass,
|
|
41
|
+
* for the whole chat.
|
|
42
|
+
*/
|
|
43
|
+
function shownAfter(m, ctx) {
|
|
44
|
+
if (!isAnswer(m))
|
|
45
|
+
return "";
|
|
46
|
+
const shown = ctx.db.collection("shown").find({ thread: m.thread }).value ?? [];
|
|
47
|
+
return shown.filter((s) => s.after === m._id && s.note)
|
|
48
|
+
.map((s) => `\n[then shown: ${s.note}${s.fallback ? " (as text)" : ""}]`).join("");
|
|
49
|
+
}
|
|
37
50
|
/**
|
|
38
51
|
* A message as the viewing agent sees it: its own as its turns (after what it
|
|
39
52
|
* was told then), others' as labelled input. So is what it handed between
|
|
40
|
-
* spaces itself: that was other work, in another chat, not this turn's.
|
|
53
|
+
* spaces itself: that was other work, in another chat, not this turn's. A
|
|
54
|
+
* choice tapped says what it was (`[tapped: trip:123]`); an answer, what went
|
|
55
|
+
* out after it.
|
|
41
56
|
*/
|
|
42
57
|
export function messageView(m, ctx) {
|
|
43
58
|
const me = ctx.viewer?.name;
|
|
44
59
|
const handed = m.via?.kind === DELEGATED;
|
|
60
|
+
const then = shownAfter(m, ctx);
|
|
45
61
|
if (m.from !== me || handed) {
|
|
46
62
|
// People by name (an agent's own name is as it is).
|
|
47
63
|
const names = namesIn(ctx, [m.from, ...m.to]);
|
|
@@ -51,9 +67,10 @@ export function messageView(m, ctx) {
|
|
|
51
67
|
const { text: notes, files } = attached(m, ctx);
|
|
52
68
|
// Sent for them, not typed now: they may not be there to answer.
|
|
53
69
|
const via = m.via ? ` (via ${m.via.kind} "${m.via.name}")` : "";
|
|
70
|
+
const tapped = m.tap ? ` [tapped: ${m.tap.id}]` : "";
|
|
54
71
|
return [{
|
|
55
72
|
role: "user",
|
|
56
|
-
text: `${m.from === me ? "You" : nameOf(m.from, names)}${via}${to}: ${m.text ?? ""}${notes}`,
|
|
73
|
+
text: `${m.from === me ? "You" : nameOf(m.from, names)}${via}${to}: ${m.text ?? ""}${tapped}${notes}${then}`,
|
|
57
74
|
...(files.length ? { files } : {}),
|
|
58
75
|
}];
|
|
59
76
|
}
|
|
@@ -63,8 +80,9 @@ export function messageView(m, ctx) {
|
|
|
63
80
|
: [];
|
|
64
81
|
// Its model's state goes back with it: thinking, signatures (only to the model that wrote them).
|
|
65
82
|
const state = m.state ? { state: m.state } : {};
|
|
66
|
-
if (!m.toolCalls?.length)
|
|
67
|
-
return [...told, { role: "assistant", text: m.text ?? ""
|
|
83
|
+
if (!m.toolCalls?.length) {
|
|
84
|
+
return [...told, { role: "assistant", text: `${m.text ?? ""}${then}`, ...state }];
|
|
85
|
+
}
|
|
68
86
|
const calls = m.toolCalls;
|
|
69
87
|
return [
|
|
70
88
|
...told,
|