@godspeedai/cognate-profile-copilot 0.0.0-stage → 0.1.1
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 +201 -0
- package/README.md +31 -2
- package/package.json +50 -4
- package/src/index.tsx +249 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @godspeedai/cognate-profile-copilot
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Reference Copilot profile (spec §29.1, §40.J, CG-PRF-002): a pure composition of existing packages. No
|
|
4
|
+
kernel or runtime primitive is added; every profile component name resolves to an existing package.
|
|
5
|
+
|
|
6
|
+
| Profile component | Backing package | Primitive used |
|
|
7
|
+
| ----------------------- | --------------------------------- | ---------------------------------------------------------------------------------- |
|
|
8
|
+
| `runtime` | `@godspeedai/cognate-runtime-bun` | `createRuntime` (kernel, store, agent execution loop) |
|
|
9
|
+
| `runs` | `@godspeedai/cognate-runtime-api` | `AgentDefinition.run(input, RunContext)` |
|
|
10
|
+
| `continuations` | `@godspeedai/cognate-continuations` | `RunContext.waitFor` (approval gate + frontend tool waits) |
|
|
11
|
+
| `frontend-capabilities` | `@godspeedai/cognate-runtime-api` | `invokeFrontend` (a `frontend_capability` continuation, no bidi transport) |
|
|
12
|
+
| `state-projections` | `@godspeedai/cognate-client` | `state.changed {snapshot|patch}` folded into `RunState.state` (RFC 6902) |
|
|
13
|
+
| `artifact-projections` | `@godspeedai/cognate-artifacts` | `RunContext.artifact` → `run.artifact` → `RunState.artifacts` / `getArtifact` |
|
|
14
|
+
| `ag-ui` | `@godspeedai/cognate-ag-ui` | `createAgUiHandler({ service, defaultAgent, authenticate })` |
|
|
15
|
+
| `react` | `@godspeedai/cognate-react` | `useCognate`, `CognateProvider` (headless client bindings) |
|
|
16
|
+
| `astryx` | `@godspeedai/cognate-ui-astryx` | `ArtifactViewer` (used directly by consumers; not reimplemented here) |
|
|
17
|
+
|
|
18
|
+
`createCopilotApp` mounts `modelComponent(options.model)` (`@godspeedai/cognate-provider-openai-compatible`) plus one
|
|
19
|
+
kernel component per backend tool, each publishing exactly `tool.contract` (its own capability id/version) —
|
|
20
|
+
no new contract shapes. `COPILOT_AGENT` is an ordinary `AgentDefinition`: it builds a `ModelRequest`, prefers
|
|
21
|
+
`provider.stream()` when available (recorded once via `ctx.step`, replayed as ordered `ctx.emit("message.delta")`
|
|
22
|
+
calls — `ctx.emit` is idempotent per call site, so replay never duplicates), and per tool call either
|
|
23
|
+
`ctx.waitFor` an approval continuation (`type` containing `"approval"`, `condition.kind: "human_approval"`) or
|
|
24
|
+
`invokeFrontend` a client-served tool. Approved backend calls go through `ctx.invoke(capability, args)`; their
|
|
25
|
+
`statePatch`/`render`/`artifact` projections emit `state.changed`, `ui.render`, and `ctx.artifact` respectively.
|
|
26
|
+
|
|
27
|
+
`GenerativeSurface` renders a run's `ui.render` events with an app-supplied component registry, synchronously
|
|
28
|
+
draining `useCognate().subscribe({ runId })`'s replayed buffer (so it also works under `renderToString`) and
|
|
29
|
+
keeping live via `useEffect` in a mounted client; an unknown `component` name renders a placeholder, never throws.
|
|
30
|
+
|
|
31
|
+
No file in this package touches `@godspeedai/cognate-kernel-cordis`, `@godspeedai/cognate-kernel-api`'s scope/fiber machinery, or any
|
|
32
|
+
runtime-bun internal — only its public `createRuntime` and `RunContext` surface.
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@godspeedai/cognate-profile-copilot",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Copilot reference profile: a composition of runtime, runs, continuations, frontend capabilities, state and artifact projections, AG-UI, React, and Astryx (spec §29.1). No kernel additions.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/GodSpeedAI/Cognate.git",
|
|
10
|
+
"directory": "profiles/copilot"
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"bun": ">=1.4.0"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"src"
|
|
20
|
+
],
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./src/index.tsx"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@godspeedai/cognate-ag-ui": "^0.1.1",
|
|
26
|
+
"@godspeedai/cognate-capabilities": "^0.1.1",
|
|
27
|
+
"@godspeedai/cognate-client": "^0.1.1",
|
|
28
|
+
"@godspeedai/cognate-events": "^0.1.1",
|
|
29
|
+
"@godspeedai/cognate-kernel-api": "^0.1.1",
|
|
30
|
+
"@godspeedai/cognate-profile": "^0.1.1",
|
|
31
|
+
"@godspeedai/cognate-protocol-connect-bun": "^0.1.1",
|
|
32
|
+
"@godspeedai/cognate-provider-openai-compatible": "^0.1.1",
|
|
33
|
+
"@godspeedai/cognate-react": "^0.1.1",
|
|
34
|
+
"@godspeedai/cognate-runtime-api": "^0.1.1",
|
|
35
|
+
"@godspeedai/cognate-runtime-bun": "^0.1.1",
|
|
36
|
+
"@godspeedai/cognate-ui-astryx": "^0.1.1"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@ag-ui/core": "1.0.0",
|
|
40
|
+
"@godspeedai/cognate-provider-local": "workspace:*",
|
|
41
|
+
"@happy-dom/global-registrator": "20.14.5",
|
|
42
|
+
"@testing-library/react": "16.3.3",
|
|
43
|
+
"react": "19.3.0",
|
|
44
|
+
"react-dom": "19.3.0",
|
|
45
|
+
"@types/react": "19.3.0",
|
|
46
|
+
"@types/react-dom": "19.2.2"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"react": "19.3.0",
|
|
50
|
+
"react-dom": "19.3.0"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
// CG-PRF-002 (spec §29.1, §40.J): the copilot profile as a pure composition of registered components.
|
|
2
|
+
// No kernel additions: the model provider and each backend tool are mounted as ordinary kernel components
|
|
3
|
+
// (spec §8.3); the agent loop is authored entirely against `RunContext` (spec §12); AG-UI, React, and Astryx
|
|
4
|
+
// are the existing packages, unmodified.
|
|
5
|
+
import { allowCapabilities } from "@godspeedai/cognate-kernel-api";
|
|
6
|
+
import type { Component, Policy } from "@godspeedai/cognate-kernel-api";
|
|
7
|
+
import type { ModelMessage, ModelProvider, ModelRequest, ModelResponse, ToolCall, ToolDefinition } from "@godspeedai/cognate-capabilities";
|
|
8
|
+
import type { Json } from "@godspeedai/cognate-events";
|
|
9
|
+
import type { CapabilityContract } from "@godspeedai/cognate-kernel-api";
|
|
10
|
+
import { createAgUiHandler } from "@godspeedai/cognate-ag-ui";
|
|
11
|
+
import { type CognateClient } from "@godspeedai/cognate-client";
|
|
12
|
+
import { useCognate } from "@godspeedai/cognate-react";
|
|
13
|
+
import { type ComponentEntry, type ProfileDefinition, defineProfile } from "@godspeedai/cognate-profile";
|
|
14
|
+
import { modelComponent } from "@godspeedai/cognate-provider-openai-compatible";
|
|
15
|
+
import { devTokenAuthenticator } from "@godspeedai/cognate-protocol-connect-bun";
|
|
16
|
+
import { type AgentDefinition, type Caller, ContinuationClosedError, invokeFrontend, type RunContext } from "@godspeedai/cognate-runtime-api";
|
|
17
|
+
import { createRuntime } from "@godspeedai/cognate-runtime-bun";
|
|
18
|
+
import type { Runtime } from "@godspeedai/cognate-runtime-bun";
|
|
19
|
+
import { type ReactNode, useEffect, useState } from "react";
|
|
20
|
+
|
|
21
|
+
export const COPILOT_AGENT = "agent.copilot";
|
|
22
|
+
|
|
23
|
+
const COPILOT_COMPONENTS = ["runtime", "runs", "continuations", "frontend-capabilities", "state-projections", "artifact-projections", "ag-ui", "react", "astryx"] as const;
|
|
24
|
+
|
|
25
|
+
/** The copilot profile as a composition of registered component names (spec §29.1). */
|
|
26
|
+
export const copilotProfile: ProfileDefinition = defineProfile({
|
|
27
|
+
name: "copilot",
|
|
28
|
+
description: "Copilot reference profile: model + tool loop over the runtime, continuations for approvals and frontend capabilities, state/artifact projections, AG-UI, React, and Astryx (spec §29.1).",
|
|
29
|
+
components: COPILOT_COMPONENTS,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
/** Registry entries backing the profile's component names; every one is backed by an existing package (no DEBT). */
|
|
33
|
+
export const copilotRegistry: readonly ComponentEntry<unknown>[] = [
|
|
34
|
+
{ name: "runtime", package: "@godspeedai/cognate-runtime-bun", supported: true, contribute: () => undefined },
|
|
35
|
+
{ name: "runs", package: "@godspeedai/cognate-runtime-api", supported: true, contribute: () => undefined },
|
|
36
|
+
{ name: "continuations", package: "@godspeedai/cognate-continuations", supported: true, contribute: () => undefined },
|
|
37
|
+
{ name: "frontend-capabilities", package: "@godspeedai/cognate-runtime-api", supported: true, contribute: () => undefined },
|
|
38
|
+
{ name: "state-projections", package: "@godspeedai/cognate-client", supported: true, contribute: () => undefined },
|
|
39
|
+
{ name: "artifact-projections", package: "@godspeedai/cognate-artifacts", supported: true, contribute: () => undefined },
|
|
40
|
+
{ name: "ag-ui", package: "@godspeedai/cognate-ag-ui", supported: true, contribute: () => undefined },
|
|
41
|
+
{ name: "react", package: "@godspeedai/cognate-react", supported: true, contribute: () => undefined },
|
|
42
|
+
{ name: "astryx", package: "@godspeedai/cognate-ui-astryx", supported: true, contribute: () => undefined },
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
export interface BackendTool {
|
|
46
|
+
readonly contract: CapabilityContract;
|
|
47
|
+
readonly description: string;
|
|
48
|
+
readonly inputSchema: { readonly [key: string]: Json };
|
|
49
|
+
readonly handler: (input: Json) => Json | Promise<Json>;
|
|
50
|
+
/** Pause for human approval (a continuation) before invoking. */
|
|
51
|
+
readonly requiresApproval?: boolean;
|
|
52
|
+
/** Shared-state projection of a completed call: RFC 6902 patch emitted as `state.changed`. */
|
|
53
|
+
readonly statePatch?: (input: Json, output: Json) => readonly { readonly op: string; readonly path: string; readonly value?: Json }[];
|
|
54
|
+
/** Generative UI projection of a completed call: emitted as `ui.render`. */
|
|
55
|
+
readonly render?: (input: Json, output: Json) => { readonly component: string; readonly props: Json };
|
|
56
|
+
/** Artifact produced from a completed call. */
|
|
57
|
+
readonly artifact?: (input: Json, output: Json) => { readonly name: string; readonly mediaType: string; readonly content: string };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface CopilotAppOptions {
|
|
61
|
+
/** SQLite path (or ":memory:"). */
|
|
62
|
+
readonly store: string;
|
|
63
|
+
readonly model: ModelProvider;
|
|
64
|
+
readonly instructions?: string;
|
|
65
|
+
readonly backendTools?: readonly BackendTool[];
|
|
66
|
+
/** Capabilities served by the connected UI (frontend tools), offered to the model as tools. */
|
|
67
|
+
readonly frontendTools?: readonly { readonly name: string; readonly description: string; readonly inputSchema: { readonly [key: string]: Json } }[];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface CopilotApp {
|
|
71
|
+
readonly runtime: Runtime;
|
|
72
|
+
/** AG-UI endpoint for the copilot agent (authenticates `Bearer <tenant>:<user>` in dev). */
|
|
73
|
+
readonly agUi: (request: Request) => Promise<Response>;
|
|
74
|
+
close(): Promise<void>;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A kernel component publishing one backend tool at its own capability contract. */
|
|
78
|
+
function backendToolComponent(tool: BackendTool): Component {
|
|
79
|
+
return {
|
|
80
|
+
id: `copilot-tool:${tool.contract.id}`,
|
|
81
|
+
provides: [tool.contract],
|
|
82
|
+
activate(fiber) {
|
|
83
|
+
fiber.provide(tool.contract, async (input) => tool.handler(input as Json));
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Runs the provider's stream (when supported) to completion, recording text deltas alongside the final response. */
|
|
89
|
+
async function collectResponse(model: ModelProvider, request: ModelRequest): Promise<{ readonly response: ModelResponse; readonly deltas: readonly string[] }> {
|
|
90
|
+
if (!model.features.stream) {
|
|
91
|
+
const response = await model.generate(request);
|
|
92
|
+
return { response, deltas: response.message.content.length > 0 ? [response.message.content] : [] };
|
|
93
|
+
}
|
|
94
|
+
const deltas: string[] = [];
|
|
95
|
+
const toolCalls: ToolCall[] = [];
|
|
96
|
+
let done: ModelResponse | undefined;
|
|
97
|
+
for await (const delta of model.stream(request)) {
|
|
98
|
+
if (delta.type === "text") deltas.push(delta.text);
|
|
99
|
+
else if (delta.type === "tool_call") toolCalls.push(delta.call);
|
|
100
|
+
else if (delta.type === "done") done = delta.response;
|
|
101
|
+
}
|
|
102
|
+
if (!done) throw new Error("model provider stream ended without a done delta");
|
|
103
|
+
return { response: done, deltas };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function toolDefinition(name: string, description: string, inputSchema: { readonly [key: string]: Json }): ToolDefinition {
|
|
107
|
+
return { name, description, inputSchema };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Build a copilot application: the copilot agent loops model → tool calls → backend tools (optionally approved) or
|
|
112
|
+
* frontend tools (client-served continuations) → model, streaming text as `message.delta`, publishing shared state as
|
|
113
|
+
* `state.changed`, generative UI as `ui.render {component, props}`, and artifacts via the run context.
|
|
114
|
+
*/
|
|
115
|
+
export async function createCopilotApp(options: CopilotAppOptions): Promise<CopilotApp> {
|
|
116
|
+
const backendTools = options.backendTools ?? [];
|
|
117
|
+
const frontendTools = options.frontendTools ?? [];
|
|
118
|
+
const backendByName = new Map(backendTools.map((tool) => [tool.contract.id, tool]));
|
|
119
|
+
const frontendByName = new Map(frontendTools.map((tool) => [tool.name, tool]));
|
|
120
|
+
|
|
121
|
+
const toolDefs: ToolDefinition[] = [
|
|
122
|
+
...backendTools.map((tool) => toolDefinition(tool.contract.id, tool.description, tool.inputSchema)),
|
|
123
|
+
...frontendTools.map((tool) => toolDefinition(tool.name, tool.description, tool.inputSchema)),
|
|
124
|
+
];
|
|
125
|
+
|
|
126
|
+
const policy: Policy = allowCapabilities("copilot-tools", ["model.generate", ...backendTools.map((tool) => tool.contract.id)]);
|
|
127
|
+
|
|
128
|
+
const copilotAgent: AgentDefinition = {
|
|
129
|
+
id: COPILOT_AGENT,
|
|
130
|
+
version: "1.0.0",
|
|
131
|
+
async run(input, ctx: RunContext): Promise<Json> {
|
|
132
|
+
const { message } = input as { readonly message: string };
|
|
133
|
+
// Shared application state starts empty so `add`-style patches (e.g. `add /refund`) apply cleanly.
|
|
134
|
+
await ctx.emit("state.changed", { snapshot: {} });
|
|
135
|
+
|
|
136
|
+
const messages: ModelMessage[] = [];
|
|
137
|
+
if (options.instructions) messages.push({ role: "system", content: options.instructions });
|
|
138
|
+
messages.push({ role: "user", content: message });
|
|
139
|
+
|
|
140
|
+
for (let turn = 0; ; turn++) {
|
|
141
|
+
const request: ModelRequest = { messages, ...(toolDefs.length > 0 ? { tools: toolDefs } : {}) };
|
|
142
|
+
const stepResult = await ctx.step(`model:${turn}`, async () => (await collectResponse(options.model, request)) as unknown as Json);
|
|
143
|
+
const { response, deltas } = stepResult as unknown as { readonly response: ModelResponse; readonly deltas: readonly string[] };
|
|
144
|
+
for (const text of deltas) await ctx.emit("message.delta", { text });
|
|
145
|
+
|
|
146
|
+
if (response.toolCalls.length === 0) return { answer: response.message.content };
|
|
147
|
+
|
|
148
|
+
messages.push({ role: "assistant", content: response.message.content, toolCalls: response.toolCalls });
|
|
149
|
+
|
|
150
|
+
for (const call of response.toolCalls) {
|
|
151
|
+
const backend = backendByName.get(call.name);
|
|
152
|
+
if (backend) {
|
|
153
|
+
if (backend.requiresApproval) {
|
|
154
|
+
const decision = (await ctx.waitFor(`approval:${call.id}`, {
|
|
155
|
+
type: `${call.name}.approval`,
|
|
156
|
+
condition: { kind: "human_approval" },
|
|
157
|
+
payload: { capability: call.name, input: call.arguments },
|
|
158
|
+
}).catch((error: unknown) => {
|
|
159
|
+
if (error instanceof ContinuationClosedError) return { approved: false };
|
|
160
|
+
throw error;
|
|
161
|
+
})) as { readonly approved?: boolean } | null;
|
|
162
|
+
if (!decision?.approved) {
|
|
163
|
+
messages.push({ role: "tool", toolCallId: call.id, content: `refused: ${call.name} was not approved` });
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const output = await ctx.invoke(call.name, call.arguments);
|
|
168
|
+
if (backend.statePatch) await ctx.emit("state.changed", { patch: backend.statePatch(call.arguments, output) } as unknown as Json);
|
|
169
|
+
if (backend.render) await ctx.emit("ui.render", backend.render(call.arguments, output));
|
|
170
|
+
if (backend.artifact) {
|
|
171
|
+
const built = backend.artifact(call.arguments, output);
|
|
172
|
+
await ctx.artifact({ name: built.name, mediaType: built.mediaType, bytes: new TextEncoder().encode(built.content) });
|
|
173
|
+
}
|
|
174
|
+
messages.push({ role: "tool", toolCallId: call.id, content: JSON.stringify(output) });
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const frontend = frontendByName.get(call.name);
|
|
179
|
+
if (frontend) {
|
|
180
|
+
const value = await invokeFrontend(ctx, `frontend:${call.id}`, call.name, call.arguments);
|
|
181
|
+
messages.push({ role: "tool", toolCallId: call.id, content: JSON.stringify(value) });
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
messages.push({ role: "tool", toolCallId: call.id, content: `unknown tool: ${call.name}` });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const components: Component[] = [modelComponent(options.model), ...backendTools.map((tool) => backendToolComponent(tool))];
|
|
192
|
+
|
|
193
|
+
const runtime = await createRuntime({
|
|
194
|
+
store: options.store,
|
|
195
|
+
policy,
|
|
196
|
+
actions: { authorize: () => ({ allow: true, reason: "copilot profile allows its own callers (spec §29.1)" }) },
|
|
197
|
+
agents: [copilotAgent],
|
|
198
|
+
components,
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
const agUi = createAgUiHandler({ service: runtime.service, defaultAgent: COPILOT_AGENT, authenticate: devTokenAuthenticator as (headers: Headers) => Caller | undefined });
|
|
202
|
+
|
|
203
|
+
return {
|
|
204
|
+
runtime,
|
|
205
|
+
agUi,
|
|
206
|
+
async close() {
|
|
207
|
+
await runtime.close();
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
interface RenderEvent {
|
|
213
|
+
readonly component: string;
|
|
214
|
+
readonly props: Json;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Synchronously drains buffered `ui.render` events for `runId` (the client replays its buffer into `subscribe`). */
|
|
218
|
+
function bufferedRenders(client: CognateClient, runId: string): RenderEvent[] {
|
|
219
|
+
const renders: RenderEvent[] = [];
|
|
220
|
+
const stop = client.subscribe({ runId }, (event) => {
|
|
221
|
+
if (event.eventType === "ui.render") renders.push(event.payload as unknown as RenderEvent);
|
|
222
|
+
});
|
|
223
|
+
stop();
|
|
224
|
+
return renders;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Renders a run's `ui.render` events with an application-supplied component registry (generative UI projection). */
|
|
228
|
+
export function GenerativeSurface({ runId, components }: { readonly runId: string; readonly components: Readonly<Record<string, (props: Json) => ReactNode>> }): ReactNode {
|
|
229
|
+
const client = useCognate();
|
|
230
|
+
// The lazy initializer re-runs on every render pass with no prior state (as in `renderToString`), so it always
|
|
231
|
+
// reflects the run's currently committed events; a live client additionally keeps this fresh via `useEffect` below.
|
|
232
|
+
const [renders, setRenders] = useState<readonly RenderEvent[]>(() => bufferedRenders(client, runId));
|
|
233
|
+
useEffect(() => {
|
|
234
|
+
setRenders(bufferedRenders(client, runId));
|
|
235
|
+
return client.subscribe({ runId }, (event) => {
|
|
236
|
+
if (event.eventType !== "ui.render") return;
|
|
237
|
+
setRenders((prev) => [...prev, event.payload as unknown as RenderEvent]);
|
|
238
|
+
});
|
|
239
|
+
}, [client, runId]);
|
|
240
|
+
|
|
241
|
+
return (
|
|
242
|
+
<>
|
|
243
|
+
{renders.map((render, index) => {
|
|
244
|
+
const Render = components[render.component];
|
|
245
|
+
return Render ? <Render key={index} {...(render.props as Record<string, unknown>)} /> : <div key={index}>Unknown component: {render.component}</div>;
|
|
246
|
+
})}
|
|
247
|
+
</>
|
|
248
|
+
);
|
|
249
|
+
}
|