cueme 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 +169 -0
- package/README.md +54 -0
- package/bin/cue-command.js +9 -0
- package/package.json +25 -0
- package/src/cli.js +62 -0
- package/src/db.js +80 -0
- package/src/handler.js +347 -0
- package/src/io.js +17 -0
- package/src/naming.js +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
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
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
124
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
125
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
126
|
+
this License, without any additional terms or conditions.
|
|
127
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
128
|
+
the terms of any separate license agreement you may have executed
|
|
129
|
+
with Licensor regarding such Contributions.
|
|
130
|
+
|
|
131
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
132
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
133
|
+
except as required for reasonable and customary use in describing the
|
|
134
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
135
|
+
|
|
136
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
137
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
138
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
139
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
140
|
+
implied, including, without limitation, any warranties or conditions
|
|
141
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
142
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
143
|
+
appropriateness of using or redistributing the Work and assume any
|
|
144
|
+
risks associated with Your exercise of permissions under this License.
|
|
145
|
+
|
|
146
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
147
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
148
|
+
unless required by applicable law (such as deliberate and grossly
|
|
149
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
150
|
+
liable to You for damages, including any direct, indirect, special,
|
|
151
|
+
incidental, or consequential damages of any character arising as a
|
|
152
|
+
result of this License or out of the use or inability to use the
|
|
153
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
154
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
155
|
+
other commercial damages or losses), even if such Contributor
|
|
156
|
+
has been advised of the possibility of such damages.
|
|
157
|
+
|
|
158
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
159
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
160
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
161
|
+
or other liability obligations and/or rights consistent with this
|
|
162
|
+
License. However, in accepting such obligations, You may act only
|
|
163
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
164
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
165
|
+
defend, and hold each Contributor harmless for any liability
|
|
166
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
167
|
+
of your accepting any such warranty or additional liability.
|
|
168
|
+
|
|
169
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# cueme
|
|
2
|
+
|
|
3
|
+
A command protocol adapter for Cue (stdin/stdout JSON), compatible with the existing SQLite mailbox (`~/.cue/cue.db`).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g cueme
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### join
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
cueme join
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### recall
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
cueme recall --hints "refactored login"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### cue
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
cueme cue --agent_id "tavilron" --prompt "What should I do next?" --timeout 600
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### pause
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
cueme pause --agent_id "tavilron" --prompt "Waiting..."
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### rpc mode (stdin)
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
echo '{"cmd":"join"}' | cueme rpc
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
All commands output one JSON object to stdout.
|
|
44
|
+
|
|
45
|
+
## Release
|
|
46
|
+
|
|
47
|
+
Publishing is tag-driven (GitHub Actions). Create a tag `v<version>` that matches `package.json` `version`.
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
git tag v0.1.1
|
|
51
|
+
git push origin v0.1.1
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The workflow publishes to npm using `NPM_TOKEN` from GitHub repo secrets.
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cueme",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Cue command protocol adapter (stdin/stdout JSON)",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"files": [
|
|
7
|
+
"bin/",
|
|
8
|
+
"src/",
|
|
9
|
+
"README.md",
|
|
10
|
+
"LICENSE"
|
|
11
|
+
],
|
|
12
|
+
"bin": {
|
|
13
|
+
"cueme": "bin/cue-command.js"
|
|
14
|
+
},
|
|
15
|
+
"type": "commonjs",
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=18"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"prepare": "node -c src/cli.js && node -c src/handler.js"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"sqlite3": "^5.1.7"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const { readAllStdin } = require('./io');
|
|
2
|
+
const { handleCommand } = require('./handler');
|
|
3
|
+
|
|
4
|
+
function parseArgs(argv) {
|
|
5
|
+
const args = argv.slice(2);
|
|
6
|
+
const out = { _: [] };
|
|
7
|
+
let i = 0;
|
|
8
|
+
while (i < args.length) {
|
|
9
|
+
const a = args[i];
|
|
10
|
+
if (a.startsWith('--')) {
|
|
11
|
+
const key = a.slice(2);
|
|
12
|
+
const next = args[i + 1];
|
|
13
|
+
if (next == null || next.startsWith('--')) {
|
|
14
|
+
out[key] = true;
|
|
15
|
+
i += 1;
|
|
16
|
+
} else {
|
|
17
|
+
out[key] = next;
|
|
18
|
+
i += 2;
|
|
19
|
+
}
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
out._.push(a);
|
|
23
|
+
i += 1;
|
|
24
|
+
}
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function main() {
|
|
29
|
+
const parsed = parseArgs(process.argv);
|
|
30
|
+
const sub = parsed._[0];
|
|
31
|
+
|
|
32
|
+
if (!sub || sub === 'help' || sub === '-h' || sub === '--help') {
|
|
33
|
+
process.stdout.write(
|
|
34
|
+
[
|
|
35
|
+
'cueme (stdin/stdout JSON)',
|
|
36
|
+
'',
|
|
37
|
+
'Usage:',
|
|
38
|
+
' cueme join',
|
|
39
|
+
' cueme recall --hints "..."',
|
|
40
|
+
' cueme cue --agent_id "..." --prompt "..." [--payload "{...}"] [--timeout 600]',
|
|
41
|
+
' cueme pause --agent_id "..." [--prompt "..."]',
|
|
42
|
+
' cueme rpc # read one JSON object from stdin: {"cmd":...}',
|
|
43
|
+
'',
|
|
44
|
+
'All commands output a single JSON object to stdout.',
|
|
45
|
+
].join('\n') + '\n'
|
|
46
|
+
);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let input = null;
|
|
51
|
+
if (sub === 'rpc' || parsed.json) {
|
|
52
|
+
const raw = await readAllStdin();
|
|
53
|
+
if (raw.trim().length > 0) {
|
|
54
|
+
input = JSON.parse(raw);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const result = await handleCommand({ subcommand: sub, args: parsed, input });
|
|
59
|
+
process.stdout.write(JSON.stringify(result) + '\n');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
module.exports = { main };
|
package/src/db.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const os = require('os');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const sqlite3 = require('sqlite3');
|
|
5
|
+
|
|
6
|
+
function getDbPath() {
|
|
7
|
+
return path.join(os.homedir(), '.cue', 'cue.db');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function ensureDbDir() {
|
|
11
|
+
const p = getDbPath();
|
|
12
|
+
fs.mkdirSync(path.dirname(p), { recursive: true });
|
|
13
|
+
return p;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function openDb() {
|
|
17
|
+
const dbPath = ensureDbDir();
|
|
18
|
+
const db = new sqlite3.Database(dbPath);
|
|
19
|
+
return { db, dbPath };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function run(db, sql, params = []) {
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
db.run(sql, params, function onRun(err) {
|
|
25
|
+
if (err) return reject(err);
|
|
26
|
+
resolve({ lastID: this.lastID, changes: this.changes });
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function get(db, sql, params = []) {
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
db.get(sql, params, (err, row) => {
|
|
34
|
+
if (err) return reject(err);
|
|
35
|
+
resolve(row || null);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function initSchema(db) {
|
|
41
|
+
await run(
|
|
42
|
+
db,
|
|
43
|
+
[
|
|
44
|
+
'CREATE TABLE IF NOT EXISTS cue_requests (',
|
|
45
|
+
' id INTEGER PRIMARY KEY AUTOINCREMENT,',
|
|
46
|
+
' request_id TEXT UNIQUE,',
|
|
47
|
+
' agent_id TEXT DEFAULT "",',
|
|
48
|
+
' prompt TEXT NOT NULL,',
|
|
49
|
+
' payload TEXT,',
|
|
50
|
+
' status TEXT DEFAULT "PENDING",',
|
|
51
|
+
' created_at TEXT,',
|
|
52
|
+
' updated_at TEXT',
|
|
53
|
+
')',
|
|
54
|
+
].join('\n')
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
await run(db, 'CREATE INDEX IF NOT EXISTS ix_cue_requests_request_id ON cue_requests(request_id)');
|
|
58
|
+
await run(db, 'CREATE INDEX IF NOT EXISTS ix_cue_requests_agent_id ON cue_requests(agent_id)');
|
|
59
|
+
|
|
60
|
+
await run(
|
|
61
|
+
db,
|
|
62
|
+
[
|
|
63
|
+
'CREATE TABLE IF NOT EXISTS cue_responses (',
|
|
64
|
+
' id INTEGER PRIMARY KEY AUTOINCREMENT,',
|
|
65
|
+
' request_id TEXT UNIQUE,',
|
|
66
|
+
' response_json TEXT NOT NULL,',
|
|
67
|
+
' cancelled INTEGER DEFAULT 0,',
|
|
68
|
+
' created_at TEXT',
|
|
69
|
+
')',
|
|
70
|
+
].join('\n')
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
await run(db, 'CREATE INDEX IF NOT EXISTS ix_cue_responses_request_id ON cue_responses(request_id)');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function nowIso() {
|
|
77
|
+
return new Date().toISOString();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
module.exports = { openDb, initSchema, run, get, nowIso, getDbPath };
|
package/src/handler.js
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
const crypto = require('crypto');
|
|
2
|
+
const { sleep } = require('./io');
|
|
3
|
+
const { generateName } = require('./naming');
|
|
4
|
+
const { openDb, initSchema, run, get, nowIso, getDbPath } = require('./db');
|
|
5
|
+
|
|
6
|
+
const CUE_TODO_CONSTRAINT_TEXT =
|
|
7
|
+
'重要:【Human Agent Protocol 强制遵循】你必须遵循Human Agent Protocol协议:在每次完成我的指令之前或回答问题时,必须向我 cue(即调用 cue(prompt, agent_id, payload?) 让我继续输入)。未实际调用 cue之前,不得结束回复;\n\n' +
|
|
8
|
+
"重要:【todo_list 强制检查】开始执行此条用户指令前,请检查当前 todo_list,并确保最后一条任务为'用 cue发送{{用户这条指令所对应的可能的响应}}'。 若不存在,必须立即补充添加。";
|
|
9
|
+
|
|
10
|
+
function newRequestId() {
|
|
11
|
+
return 'req_' + crypto.randomBytes(6).toString('hex');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function waitForResponse(db, requestId, timeoutSeconds) {
|
|
15
|
+
const start = Date.now();
|
|
16
|
+
while (true) {
|
|
17
|
+
const row = await get(db, 'SELECT * FROM cue_responses WHERE request_id = ?', [requestId]);
|
|
18
|
+
if (row) return row;
|
|
19
|
+
|
|
20
|
+
if (timeoutSeconds != null) {
|
|
21
|
+
const elapsed = (Date.now() - start) / 1000;
|
|
22
|
+
if (elapsed > timeoutSeconds) {
|
|
23
|
+
const err = new Error(`Timed out waiting for response: ${requestId}`);
|
|
24
|
+
err.code = 'TIMEOUT';
|
|
25
|
+
throw err;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
await sleep(500);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function parseUserResponseJson(responseJson) {
|
|
34
|
+
try {
|
|
35
|
+
const obj = JSON.parse(responseJson);
|
|
36
|
+
if (!obj || typeof obj !== 'object') return { text: '', images: [] };
|
|
37
|
+
return {
|
|
38
|
+
text: typeof obj.text === 'string' ? obj.text : '',
|
|
39
|
+
images: Array.isArray(obj.images) ? obj.images : [],
|
|
40
|
+
};
|
|
41
|
+
} catch {
|
|
42
|
+
return { text: '', images: [] };
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function buildToolContentsFromUserResponse(userResp) {
|
|
47
|
+
const contents = [];
|
|
48
|
+
|
|
49
|
+
const text = (userResp.text || '').trim();
|
|
50
|
+
const images = Array.isArray(userResp.images) ? userResp.images : [];
|
|
51
|
+
|
|
52
|
+
if (text) {
|
|
53
|
+
contents.push({ type: 'text', text: `用户希望继续,并提供了以下指令:\n\n${text}` });
|
|
54
|
+
} else if (images.length > 0) {
|
|
55
|
+
contents.push({ type: 'text', text: '用户希望继续,并附加了图片:' });
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
for (const img of images) {
|
|
59
|
+
if (!img) continue;
|
|
60
|
+
contents.push({
|
|
61
|
+
type: 'image',
|
|
62
|
+
data: img.base64_data,
|
|
63
|
+
mimeType: img.mime_type,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
contents.push({ type: 'text', text: `\n\n${CUE_TODO_CONSTRAINT_TEXT}` });
|
|
68
|
+
return contents;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function handleJoin(db) {
|
|
72
|
+
const agent_id = generateName();
|
|
73
|
+
return {
|
|
74
|
+
ok: true,
|
|
75
|
+
data: {
|
|
76
|
+
agent_id,
|
|
77
|
+
message:
|
|
78
|
+
`agent_id=${agent_id}\n\n` +
|
|
79
|
+
'Use this agent_id when calling cue(prompt, agent_id).' +
|
|
80
|
+
' You must remember this agent_id: when calling cue(), pass it as agent_id so the system knows who you are.' +
|
|
81
|
+
' Before ending this session, call cue. Please use cue to provide a final summary, ask a question, or make a request.',
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function handleRecall(db, hints) {
|
|
87
|
+
const row = await get(
|
|
88
|
+
db,
|
|
89
|
+
'SELECT agent_id FROM cue_requests WHERE agent_id != "" AND prompt LIKE ? ORDER BY created_at DESC LIMIT 1',
|
|
90
|
+
[`%${hints}%`]
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
if (row && row.agent_id) {
|
|
94
|
+
return {
|
|
95
|
+
ok: true,
|
|
96
|
+
data: {
|
|
97
|
+
agent_id: row.agent_id,
|
|
98
|
+
message: `agent_id=${row.agent_id}\n\nUse this agent_id when calling cue(prompt, agent_id).`,
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const agent_id = generateName();
|
|
104
|
+
return {
|
|
105
|
+
ok: true,
|
|
106
|
+
data: {
|
|
107
|
+
agent_id,
|
|
108
|
+
message:
|
|
109
|
+
'No matching record found; generated a new agent_id.\n\n' +
|
|
110
|
+
`agent_id=${agent_id}\n\n` +
|
|
111
|
+
'Use this agent_id when calling cue(prompt, agent_id).',
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function cancelledContentsForMode(mode) {
|
|
117
|
+
if (mode === 'pause') {
|
|
118
|
+
return [
|
|
119
|
+
{
|
|
120
|
+
type: 'text',
|
|
121
|
+
text:
|
|
122
|
+
'The user did not continue. Call pause(agent_id) to suspend and wait for resume.\n\n' +
|
|
123
|
+
CUE_TODO_CONSTRAINT_TEXT,
|
|
124
|
+
},
|
|
125
|
+
];
|
|
126
|
+
}
|
|
127
|
+
return [
|
|
128
|
+
{
|
|
129
|
+
type: 'text',
|
|
130
|
+
text: 'The user did not continue. Call pause(agent_id) to suspend and wait for resume.\n\n',
|
|
131
|
+
},
|
|
132
|
+
];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function emptyContentsForMode(mode) {
|
|
136
|
+
if (mode === 'pause') {
|
|
137
|
+
return [
|
|
138
|
+
{
|
|
139
|
+
type: 'text',
|
|
140
|
+
text: 'The user resumed the conversation.\n\n' + CUE_TODO_CONSTRAINT_TEXT,
|
|
141
|
+
},
|
|
142
|
+
];
|
|
143
|
+
}
|
|
144
|
+
return [
|
|
145
|
+
{
|
|
146
|
+
type: 'text',
|
|
147
|
+
text:
|
|
148
|
+
'No user input received. Call pause(agent_id) to suspend and wait for resume.\n\n' +
|
|
149
|
+
CUE_TODO_CONSTRAINT_TEXT,
|
|
150
|
+
},
|
|
151
|
+
];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function timeoutContentsForMode(mode) {
|
|
155
|
+
if (mode === 'pause') {
|
|
156
|
+
return [
|
|
157
|
+
{
|
|
158
|
+
type: 'text',
|
|
159
|
+
text:
|
|
160
|
+
'Tool call was cancelled. Call pause(agent_id) to suspend and wait for resume.\n\n',
|
|
161
|
+
},
|
|
162
|
+
];
|
|
163
|
+
}
|
|
164
|
+
return [
|
|
165
|
+
{
|
|
166
|
+
type: 'text',
|
|
167
|
+
text:
|
|
168
|
+
'Timed out waiting for user response. Call pause(agent_id) to suspend and wait for resume.\n\n',
|
|
169
|
+
},
|
|
170
|
+
];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async function handleCueLike(db, { mode, agent_id, prompt, payload, timeoutSeconds }) {
|
|
174
|
+
const request_id = newRequestId();
|
|
175
|
+
const created_at = nowIso();
|
|
176
|
+
|
|
177
|
+
await run(
|
|
178
|
+
db,
|
|
179
|
+
[
|
|
180
|
+
'INSERT INTO cue_requests (request_id, agent_id, prompt, payload, status, created_at, updated_at)',
|
|
181
|
+
'VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
182
|
+
].join('\n'),
|
|
183
|
+
[request_id, agent_id, prompt, payload ?? null, 'PENDING', created_at, created_at]
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
try {
|
|
187
|
+
const respRow = await waitForResponse(db, request_id, timeoutSeconds);
|
|
188
|
+
|
|
189
|
+
if (respRow.cancelled) {
|
|
190
|
+
return {
|
|
191
|
+
ok: true,
|
|
192
|
+
data: {
|
|
193
|
+
request_id,
|
|
194
|
+
cancelled: true,
|
|
195
|
+
response: { text: '', images: [] },
|
|
196
|
+
contents: cancelledContentsForMode(mode),
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const userResp = parseUserResponseJson(respRow.response_json);
|
|
202
|
+
|
|
203
|
+
if (!userResp.text.trim() && (!userResp.images || userResp.images.length === 0)) {
|
|
204
|
+
if (mode === 'cue') {
|
|
205
|
+
const updated_at = nowIso();
|
|
206
|
+
await run(
|
|
207
|
+
db,
|
|
208
|
+
'UPDATE cue_requests SET status = ?, updated_at = ? WHERE request_id = ?',
|
|
209
|
+
['COMPLETED', updated_at, request_id]
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
return {
|
|
213
|
+
ok: true,
|
|
214
|
+
data: {
|
|
215
|
+
request_id,
|
|
216
|
+
cancelled: false,
|
|
217
|
+
response: userResp,
|
|
218
|
+
contents: emptyContentsForMode(mode),
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (mode === 'cue') {
|
|
224
|
+
const updated_at = nowIso();
|
|
225
|
+
await run(
|
|
226
|
+
db,
|
|
227
|
+
'UPDATE cue_requests SET status = ?, updated_at = ? WHERE request_id = ?',
|
|
228
|
+
['COMPLETED', updated_at, request_id]
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return {
|
|
233
|
+
ok: true,
|
|
234
|
+
data: {
|
|
235
|
+
request_id,
|
|
236
|
+
cancelled: false,
|
|
237
|
+
response: userResp,
|
|
238
|
+
contents: buildToolContentsFromUserResponse(userResp),
|
|
239
|
+
constraint_text: CUE_TODO_CONSTRAINT_TEXT,
|
|
240
|
+
},
|
|
241
|
+
};
|
|
242
|
+
} catch (err) {
|
|
243
|
+
if (err && err.code === 'TIMEOUT') {
|
|
244
|
+
const updated_at = nowIso();
|
|
245
|
+
await run(
|
|
246
|
+
db,
|
|
247
|
+
'UPDATE cue_requests SET status = ?, updated_at = ? WHERE request_id = ?',
|
|
248
|
+
['CANCELLED', updated_at, request_id]
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
const existing = await get(db, 'SELECT id FROM cue_responses WHERE request_id = ?', [request_id]);
|
|
252
|
+
if (!existing) {
|
|
253
|
+
const cancelledResponse = JSON.stringify({ text: '', images: [] });
|
|
254
|
+
await run(
|
|
255
|
+
db,
|
|
256
|
+
'INSERT INTO cue_responses (request_id, response_json, cancelled, created_at) VALUES (?, ?, ?, ?)',
|
|
257
|
+
[request_id, cancelledResponse, 1, updated_at]
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return {
|
|
262
|
+
ok: true,
|
|
263
|
+
data: {
|
|
264
|
+
request_id,
|
|
265
|
+
cancelled: true,
|
|
266
|
+
response: { text: '', images: [] },
|
|
267
|
+
contents: timeoutContentsForMode(mode),
|
|
268
|
+
},
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
throw err;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
async function handlePause(db, { agent_id, prompt }) {
|
|
277
|
+
const pausePrompt = prompt || 'Waiting for your confirmation. Click Continue when you are ready.';
|
|
278
|
+
const payload =
|
|
279
|
+
'{"type":"confirm","variant":"pause","text":"Paused. Click Continue when you are ready.","confirm_label":"Continue","cancel_label":""}';
|
|
280
|
+
|
|
281
|
+
return handleCueLike(db, {
|
|
282
|
+
mode: 'pause',
|
|
283
|
+
agent_id,
|
|
284
|
+
prompt: pausePrompt,
|
|
285
|
+
payload,
|
|
286
|
+
timeoutSeconds: null,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async function handleCommand({ subcommand, args, input }) {
|
|
291
|
+
const { db, dbPath } = openDb();
|
|
292
|
+
try {
|
|
293
|
+
await initSchema(db);
|
|
294
|
+
|
|
295
|
+
if (subcommand === 'rpc') {
|
|
296
|
+
if (!input || typeof input !== 'object') {
|
|
297
|
+
return { ok: false, error: 'rpc expects a JSON object on stdin' };
|
|
298
|
+
}
|
|
299
|
+
const cmd = input.cmd;
|
|
300
|
+
if (cmd === 'join') return await handleJoin(db);
|
|
301
|
+
if (cmd === 'recall') return await handleRecall(db, String(input.hints || ''));
|
|
302
|
+
if (cmd === 'cue') {
|
|
303
|
+
return await handleCueLike(db, {
|
|
304
|
+
mode: 'cue',
|
|
305
|
+
agent_id: String(input.agent_id || ''),
|
|
306
|
+
prompt: String(input.prompt || ''),
|
|
307
|
+
payload: input.payload == null ? null : String(input.payload),
|
|
308
|
+
timeoutSeconds: input.timeout == null ? 600 : Number(input.timeout),
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
if (cmd === 'pause') {
|
|
312
|
+
return await handlePause(db, {
|
|
313
|
+
agent_id: String(input.agent_id || ''),
|
|
314
|
+
prompt: input.prompt == null ? null : String(input.prompt),
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
return { ok: false, error: `unknown cmd: ${cmd}` };
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (subcommand === 'join') return await handleJoin(db);
|
|
321
|
+
|
|
322
|
+
if (subcommand === 'recall') {
|
|
323
|
+
const hints = (args.hints ?? '').toString();
|
|
324
|
+
return await handleRecall(db, hints);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (subcommand === 'cue') {
|
|
328
|
+
const agent_id = (args.agent_id ?? '').toString();
|
|
329
|
+
const prompt = (args.prompt ?? '').toString();
|
|
330
|
+
const payload = args.payload == null ? null : args.payload.toString();
|
|
331
|
+
const timeoutSeconds = args.timeout == null ? 600 : Number(args.timeout);
|
|
332
|
+
return await handleCueLike(db, { mode: 'cue', agent_id, prompt, payload, timeoutSeconds });
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (subcommand === 'pause') {
|
|
336
|
+
const agent_id = (args.agent_id ?? '').toString();
|
|
337
|
+
const prompt = args.prompt == null ? null : args.prompt.toString();
|
|
338
|
+
return await handlePause(db, { agent_id, prompt });
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return { ok: false, error: `unknown subcommand: ${subcommand}`, data: { db_path: dbPath } };
|
|
342
|
+
} finally {
|
|
343
|
+
db.close();
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
module.exports = { handleCommand, getDbPath };
|
package/src/io.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function readAllStdin() {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
let data = '';
|
|
4
|
+
process.stdin.setEncoding('utf8');
|
|
5
|
+
process.stdin.on('data', (chunk) => {
|
|
6
|
+
data += chunk;
|
|
7
|
+
});
|
|
8
|
+
process.stdin.on('end', () => resolve(data));
|
|
9
|
+
process.stdin.on('error', reject);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function sleep(ms) {
|
|
14
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = { readAllStdin, sleep };
|
package/src/naming.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const crypto = require('crypto');
|
|
2
|
+
|
|
3
|
+
const C = 'bcdfghjklmnpqrstvwxz';
|
|
4
|
+
const V = 'aeiou';
|
|
5
|
+
const CODA = ['', 'n', 'r', 'l', 's', 'm', 'nd', 'st', 'rk', 'ld'];
|
|
6
|
+
|
|
7
|
+
function choice(strOrArr) {
|
|
8
|
+
const n = strOrArr.length;
|
|
9
|
+
const i = crypto.randomInt(0, n);
|
|
10
|
+
return strOrArr[i];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function syllable() {
|
|
14
|
+
return choice(C) + choice(V) + choice(CODA);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function pureName(minLen = 8, maxLen = 12) {
|
|
18
|
+
for (let tries = 0; tries < 100; tries += 1) {
|
|
19
|
+
const nSyl = choice([3, 4, 5]);
|
|
20
|
+
let s = '';
|
|
21
|
+
for (let i = 0; i < nSyl; i += 1) s += syllable();
|
|
22
|
+
s = s.slice(0, maxLen);
|
|
23
|
+
if (s.length >= minLen && s.length <= maxLen && /^[a-zA-Z]+$/.test(s)) {
|
|
24
|
+
return s;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
throw new Error('Name generation failed; adjust syllable/length parameters');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function generateName() {
|
|
31
|
+
return pureName().toLowerCase();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
module.exports = { generateName };
|