@cogitator-ai/openai-compat 19.0.16 → 20.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -50
- package/dist/client/openai-adapter.d.ts +74 -37
- package/dist/client/openai-adapter.d.ts.map +1 -1
- package/dist/client/openai-adapter.js +416 -137
- package/dist/client/openai-adapter.js.map +1 -1
- package/dist/client/storage.d.ts +4 -0
- package/dist/client/storage.d.ts.map +1 -1
- package/dist/client/storage.js +80 -60
- package/dist/client/storage.js.map +1 -1
- package/dist/client/thread-manager.d.ts +45 -31
- package/dist/client/thread-manager.d.ts.map +1 -1
- package/dist/client/thread-manager.js +157 -115
- package/dist/client/thread-manager.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/server/api-server.d.ts +23 -1
- package/dist/server/api-server.d.ts.map +1 -1
- package/dist/server/api-server.js +42 -23
- package/dist/server/api-server.js.map +1 -1
- package/dist/server/middleware/auth.d.ts +2 -0
- package/dist/server/middleware/auth.d.ts.map +1 -1
- package/dist/server/middleware/auth.js +25 -2
- package/dist/server/middleware/auth.js.map +1 -1
- package/dist/server/routes/assistants.d.ts.map +1 -1
- package/dist/server/routes/assistants.js +16 -26
- package/dist/server/routes/assistants.js.map +1 -1
- package/dist/server/routes/files.d.ts.map +1 -1
- package/dist/server/routes/files.js +60 -56
- package/dist/server/routes/files.js.map +1 -1
- package/dist/server/routes/runs.d.ts.map +1 -1
- package/dist/server/routes/runs.js +102 -105
- package/dist/server/routes/runs.js.map +1 -1
- package/dist/server/routes/shared.d.ts +27 -0
- package/dist/server/routes/shared.d.ts.map +1 -0
- package/dist/server/routes/shared.js +54 -0
- package/dist/server/routes/shared.js.map +1 -0
- package/dist/server/routes/threads.d.ts.map +1 -1
- package/dist/server/routes/threads.js +29 -16
- package/dist/server/routes/threads.js.map +1 -1
- package/package.json +18 -6
package/README.md
CHANGED
|
@@ -17,7 +17,8 @@ pnpm add @cogitator-ai/openai-compat
|
|
|
17
17
|
- **SSE Streaming** - Real-time token streaming with OpenAI-compatible events
|
|
18
18
|
- **File Operations** - Upload and manage files for assistants
|
|
19
19
|
- **Full Assistants API** - Create, update, delete assistants
|
|
20
|
-
- **Run Management** - Execute
|
|
20
|
+
- **Run Management** - Execute, stream, cancel and list runs
|
|
21
|
+
- **Function Calling** - Assistant `function` tools pause the run with `requires_action` until the client submits outputs
|
|
21
22
|
- **Authentication** - Optional API key authentication
|
|
22
23
|
- **CORS Support** - Configurable cross-origin requests
|
|
23
24
|
|
|
@@ -32,27 +33,26 @@ import { createOpenAIServer } from '@cogitator-ai/openai-compat';
|
|
|
32
33
|
import { Cogitator, tool } from '@cogitator-ai/core';
|
|
33
34
|
import { z } from 'zod';
|
|
34
35
|
|
|
35
|
-
const
|
|
36
|
-
name: '
|
|
37
|
-
description: '
|
|
38
|
-
parameters: z.object({
|
|
39
|
-
|
|
40
|
-
}),
|
|
41
|
-
execute: async ({ expression }) => eval(expression).toString(),
|
|
36
|
+
const add = tool({
|
|
37
|
+
name: 'add',
|
|
38
|
+
description: 'Add two numbers',
|
|
39
|
+
parameters: z.object({ a: z.number(), b: z.number() }),
|
|
40
|
+
execute: async ({ a, b }) => ({ sum: a + b }),
|
|
42
41
|
});
|
|
43
42
|
|
|
44
43
|
const cogitator = new Cogitator({
|
|
45
|
-
defaultModel: 'openai/gpt-4o-mini',
|
|
44
|
+
llm: { defaultModel: 'openai/gpt-4o-mini' },
|
|
46
45
|
});
|
|
47
46
|
|
|
48
47
|
const server = createOpenAIServer(cogitator, {
|
|
49
48
|
port: 8080,
|
|
50
|
-
tools: [
|
|
49
|
+
tools: [add],
|
|
51
50
|
apiKeys: ['sk-my-secret-key'],
|
|
51
|
+
defaultModel: 'openai/gpt-4o-mini', // used for the advertised `cogitator` model id
|
|
52
52
|
});
|
|
53
53
|
|
|
54
54
|
await server.start();
|
|
55
|
-
//
|
|
55
|
+
console.log(server.getBaseUrl()); // http://localhost:8080/v1
|
|
56
56
|
```
|
|
57
57
|
|
|
58
58
|
### Client Mode
|
|
@@ -78,22 +78,22 @@ await openai.beta.threads.messages.create(thread.id, {
|
|
|
78
78
|
content: 'What is 2 + 2?',
|
|
79
79
|
});
|
|
80
80
|
|
|
81
|
-
const run = await openai.beta.threads.runs.
|
|
81
|
+
const run = await openai.beta.threads.runs.createAndPoll(thread.id, {
|
|
82
82
|
assistant_id: assistant.id,
|
|
83
83
|
});
|
|
84
84
|
|
|
85
|
-
// Poll for completion
|
|
86
|
-
let status = run.status;
|
|
87
|
-
while (status === 'queued' || status === 'in_progress') {
|
|
88
|
-
await new Promise((r) => setTimeout(r, 1000));
|
|
89
|
-
const updated = await openai.beta.threads.runs.retrieve(thread.id, run.id);
|
|
90
|
-
status = updated.status;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
85
|
const messages = await openai.beta.threads.messages.list(thread.id);
|
|
94
|
-
console.log(messages.data[0].content);
|
|
86
|
+
console.log(run.status, messages.data[0].content); // newest message first
|
|
87
|
+
|
|
88
|
+
// Streaming
|
|
89
|
+
const stream = openai.beta.threads.runs
|
|
90
|
+
.stream(thread.id, { assistant_id: assistant.id })
|
|
91
|
+
.on('textDelta', (delta) => process.stdout.write(delta.value ?? ''));
|
|
92
|
+
await stream.finalRun();
|
|
95
93
|
```
|
|
96
94
|
|
|
95
|
+
Assistant `model` values are Cogitator model strings (`openai/gpt-4o`, `ollama/llama3.2:latest`, ...). The id `cogitator` listed by `GET /v1/models` maps to the server's `defaultModel`.
|
|
96
|
+
|
|
97
97
|
---
|
|
98
98
|
|
|
99
99
|
## OpenAI Server
|
|
@@ -129,8 +129,11 @@ const server = new OpenAIServer(cogitator, {
|
|
|
129
129
|
| `port` | `number` | `8080` | Port to listen on |
|
|
130
130
|
| `host` | `string` | `'0.0.0.0'` | Host to bind to |
|
|
131
131
|
| `apiKeys` | `string[]` | `[]` | API keys for authentication. Empty disables auth |
|
|
132
|
-
| `tools` | `Tool[]` | `[]` |
|
|
133
|
-
| `
|
|
132
|
+
| `tools` | `Tool[]` | `[]` | Server-side tools available to every run |
|
|
133
|
+
| `defaultModel` | `string` | — | Model used for the `cogitator` model id |
|
|
134
|
+
| `storage` | `ThreadStorage` | in-memory | Persistence backend (connect before passing) |
|
|
135
|
+
| `maxFileSize` | `number` | `512 MB` | Upload limit for `POST /v1/files` |
|
|
136
|
+
| `logging` | `boolean` | `false` | Enable Fastify request logging (JSON logs) |
|
|
134
137
|
| `cors.origin` | `string \| string[] \| boolean` | `true` | CORS origin configuration |
|
|
135
138
|
| `cors.methods` | `string[]` | `['GET', 'POST', 'DELETE', 'OPTIONS']` | Allowed HTTP methods |
|
|
136
139
|
|
|
@@ -139,7 +142,7 @@ const server = new OpenAIServer(cogitator, {
|
|
|
139
142
|
```typescript
|
|
140
143
|
await server.start();
|
|
141
144
|
|
|
142
|
-
console.log(server.getUrl());
|
|
145
|
+
console.log(server.getUrl()); // uses the bound port, so `port: 0` works
|
|
143
146
|
console.log(server.getBaseUrl());
|
|
144
147
|
|
|
145
148
|
console.log(server.isRunning());
|
|
@@ -151,7 +154,7 @@ await server.stop();
|
|
|
151
154
|
|
|
152
155
|
### Health Check
|
|
153
156
|
|
|
154
|
-
The server provides a health endpoint:
|
|
157
|
+
The server provides a health endpoint (public even when `apiKeys` are configured):
|
|
155
158
|
|
|
156
159
|
```bash
|
|
157
160
|
curl http://localhost:8080/health
|
|
@@ -236,11 +239,21 @@ const run = await adapter.createRun(thread.id, {
|
|
|
236
239
|
|
|
237
240
|
const status = adapter.getRun(thread.id, run.id);
|
|
238
241
|
|
|
239
|
-
const
|
|
242
|
+
const runs = adapter.listRuns(thread.id);
|
|
243
|
+
|
|
244
|
+
const cancelled = adapter.cancelRun(thread.id, run.id); // throws if the run already finished
|
|
245
|
+
|
|
246
|
+
for await (const { event, data } of adapter.streamRunEvents(run.id)) {
|
|
247
|
+
console.log(event, data);
|
|
248
|
+
}
|
|
240
249
|
```
|
|
241
250
|
|
|
251
|
+
Runs use the whole thread as context (earlier messages are replayed to the agent), receive an abort signal on cancel, and honour `additional_instructions`, `response_format` (`json_object` / `json_schema`), `max_completion_tokens`, `top_p`, `tool_choice` (`none` or a specific function), `parallel_tool_calls` and `truncation_strategy` (`last_messages`). A thread can have only one active run at a time. Image parts (`image_url`, and `image_file` uploads with an image extension) are passed to the model.
|
|
252
|
+
|
|
242
253
|
### Tool Outputs
|
|
243
254
|
|
|
255
|
+
Server-side `tools` run inside Cogitator. Assistant tools of type `function` are executed by the API client: when the model calls one, the run moves to `requires_action` with the pending calls, and continues after all outputs are submitted (runs waiting longer than 10 minutes expire). With the OpenAI SDK use `submitToolOutputsAndPoll` / `submitToolOutputsStream`.
|
|
256
|
+
|
|
244
257
|
```typescript
|
|
245
258
|
const run = adapter.getRun(thread.id, runId);
|
|
246
259
|
|
|
@@ -452,14 +465,17 @@ import { Cogitator } from '@cogitator-ai/core';
|
|
|
452
465
|
const storage = new RedisThreadStorage({ host: 'localhost' });
|
|
453
466
|
await storage.connect();
|
|
454
467
|
|
|
455
|
-
|
|
456
|
-
|
|
468
|
+
const cogitator = new Cogitator({ llm: { defaultModel: 'openai/gpt-4o' } });
|
|
469
|
+
|
|
470
|
+
// In-process adapter backed by Redis
|
|
471
|
+
const adapter = new OpenAIAdapter(cogitator, { tools: [], storage });
|
|
457
472
|
|
|
458
|
-
//
|
|
459
|
-
const
|
|
460
|
-
const adapter = new OpenAIAdapter(cogitator, { tools: [] });
|
|
473
|
+
// Or the REST server
|
|
474
|
+
const server = createOpenAIServer(cogitator, { storage });
|
|
461
475
|
```
|
|
462
476
|
|
|
477
|
+
Storage is the single source of truth (no in-process cache), so several server instances can share one Redis or PostgreSQL backend. Redis listings use `SCAN` + `MGET`; `ttl: 0` disables expiry. `ioredis` and `pg` are optional peer dependencies loaded on `connect()`.
|
|
478
|
+
|
|
463
479
|
### ThreadStorage Interface
|
|
464
480
|
|
|
465
481
|
All storage backends implement this interface:
|
|
@@ -527,11 +543,12 @@ const manager = new ThreadManager(new MyCustomStorage());
|
|
|
527
543
|
|
|
528
544
|
### Threads
|
|
529
545
|
|
|
530
|
-
| Method | Endpoint | Description
|
|
531
|
-
| ------ | ----------------- |
|
|
532
|
-
| POST | `/v1/threads` | Create thread
|
|
533
|
-
| GET | `/v1/threads/:id` | Get thread
|
|
534
|
-
|
|
|
546
|
+
| Method | Endpoint | Description |
|
|
547
|
+
| ------ | ----------------- | ---------------------- |
|
|
548
|
+
| POST | `/v1/threads` | Create thread |
|
|
549
|
+
| GET | `/v1/threads/:id` | Get thread |
|
|
550
|
+
| POST | `/v1/threads/:id` | Update thread metadata |
|
|
551
|
+
| DELETE | `/v1/threads/:id` | Delete thread |
|
|
535
552
|
|
|
536
553
|
### Messages
|
|
537
554
|
|
|
@@ -545,7 +562,9 @@ const manager = new ThreadManager(new MyCustomStorage());
|
|
|
545
562
|
|
|
546
563
|
| Method | Endpoint | Description |
|
|
547
564
|
| ------ | -------------------------------------------------- | ------------------- |
|
|
565
|
+
| POST | `/v1/threads/runs` | Create thread + run |
|
|
548
566
|
| POST | `/v1/threads/:id/runs` | Create run |
|
|
567
|
+
| GET | `/v1/threads/:id/runs` | List runs |
|
|
549
568
|
| GET | `/v1/threads/:id/runs/:run_id` | Get run status |
|
|
550
569
|
| POST | `/v1/threads/:id/runs/:run_id/cancel` | Cancel run |
|
|
551
570
|
| POST | `/v1/threads/:id/runs/:run_id/submit_tool_outputs` | Submit tool outputs |
|
|
@@ -627,10 +646,13 @@ type RunStatus =
|
|
|
627
646
|
queued → in_progress → completed
|
|
628
647
|
→ failed
|
|
629
648
|
→ requires_action → in_progress → ...
|
|
649
|
+
→ expired (no outputs within 10 minutes)
|
|
630
650
|
|
|
631
|
-
in_progress → cancelling → cancelled
|
|
651
|
+
queued / in_progress / requires_action → cancelling → cancelled
|
|
632
652
|
```
|
|
633
653
|
|
|
654
|
+
Streaming runs (`stream: true`) emit `thread.run.created`, `thread.run.queued`, `thread.run.in_progress`, `thread.message.created`, `thread.message.delta`, `thread.message.completed`, `thread.run.completed` (or `failed` / `cancelled` / `expired` / `requires_action`) and a final `done` event.
|
|
655
|
+
|
|
634
656
|
### Polling for Completion
|
|
635
657
|
|
|
636
658
|
```typescript
|
|
@@ -638,7 +660,7 @@ async function waitForRun(openai: OpenAI, threadId: string, runId: string): Prom
|
|
|
638
660
|
const terminalStates = ['completed', 'failed', 'cancelled', 'expired'];
|
|
639
661
|
|
|
640
662
|
while (true) {
|
|
641
|
-
const run = await openai.beta.threads.runs.retrieve(
|
|
663
|
+
const run = await openai.beta.threads.runs.retrieve(runId, { thread_id: threadId });
|
|
642
664
|
|
|
643
665
|
if (terminalStates.includes(run.status)) {
|
|
644
666
|
return run;
|
|
@@ -709,7 +731,9 @@ type StreamEvent =
|
|
|
709
731
|
| { event: 'thread.run.requires_action'; data: Run }
|
|
710
732
|
| { event: 'thread.run.completed'; data: Run }
|
|
711
733
|
| { event: 'thread.run.failed'; data: Run }
|
|
734
|
+
| { event: 'thread.run.cancelling'; data: Run }
|
|
712
735
|
| { event: 'thread.run.cancelled'; data: Run }
|
|
736
|
+
| { event: 'thread.run.expired'; data: Run }
|
|
713
737
|
| { event: 'thread.message.created'; data: Message }
|
|
714
738
|
| { event: 'thread.message.in_progress'; data: Message }
|
|
715
739
|
| { event: 'thread.message.delta'; data: MessageDelta }
|
|
@@ -717,8 +741,12 @@ type StreamEvent =
|
|
|
717
741
|
| { event: 'done'; data: '[DONE]' };
|
|
718
742
|
```
|
|
719
743
|
|
|
744
|
+
A stream ends with `done` after the run finishes or pauses on `requires_action`; continue a paused run with `submit_tool_outputs` and `stream: true`.
|
|
745
|
+
|
|
720
746
|
### Message Delta Format
|
|
721
747
|
|
|
748
|
+
All text deltas target content index `0`, so SDK helpers accumulate them into a single text part whose message id matches the stored message.
|
|
749
|
+
|
|
722
750
|
```typescript
|
|
723
751
|
interface MessageDelta {
|
|
724
752
|
id: string;
|
|
@@ -882,7 +910,7 @@ import { Cogitator } from '@cogitator-ai/core';
|
|
|
882
910
|
import OpenAI from 'openai';
|
|
883
911
|
|
|
884
912
|
const cogitator = new Cogitator({
|
|
885
|
-
defaultModel: 'ollama/llama3.2:latest',
|
|
913
|
+
llm: { defaultModel: 'ollama/llama3.2:latest' },
|
|
886
914
|
});
|
|
887
915
|
|
|
888
916
|
const server = createOpenAIServer(cogitator, { port: 8080 });
|
|
@@ -907,16 +935,10 @@ async function chat(message: string): Promise<string> {
|
|
|
907
935
|
content: message,
|
|
908
936
|
});
|
|
909
937
|
|
|
910
|
-
const run = await openai.beta.threads.runs.
|
|
938
|
+
const run = await openai.beta.threads.runs.createAndPoll(thread.id, {
|
|
911
939
|
assistant_id: assistant.id,
|
|
912
940
|
});
|
|
913
|
-
|
|
914
|
-
while (true) {
|
|
915
|
-
const status = await openai.beta.threads.runs.retrieve(thread.id, run.id);
|
|
916
|
-
if (status.status === 'completed') break;
|
|
917
|
-
if (status.status === 'failed') throw new Error(status.last_error?.message);
|
|
918
|
-
await new Promise((r) => setTimeout(r, 500));
|
|
919
|
-
}
|
|
941
|
+
if (run.status !== 'completed') throw new Error(run.last_error?.message ?? run.status);
|
|
920
942
|
|
|
921
943
|
const messages = await openai.beta.threads.messages.list(thread.id, {
|
|
922
944
|
limit: 1,
|
|
@@ -951,7 +973,7 @@ const runCode = tool({
|
|
|
951
973
|
});
|
|
952
974
|
|
|
953
975
|
const cogitator = new Cogitator({
|
|
954
|
-
defaultModel: 'openai/gpt-4o',
|
|
976
|
+
llm: { defaultModel: 'openai/gpt-4o' },
|
|
955
977
|
});
|
|
956
978
|
|
|
957
979
|
const server = createOpenAIServer(cogitator, {
|
|
@@ -970,8 +992,10 @@ const assistant = await openai.beta.assistants.create({
|
|
|
970
992
|
name: 'Code Runner',
|
|
971
993
|
instructions: 'You can run Python code using the run_code tool.',
|
|
972
994
|
model: 'openai/gpt-4o',
|
|
973
|
-
tools: [{ type: 'function', function: { name: 'run_code' } }],
|
|
974
995
|
});
|
|
996
|
+
// Server-side tools (`tools` on the server) are available to every run without
|
|
997
|
+
// being declared on the assistant. Declare `function` tools only for functions
|
|
998
|
+
// your client executes (they surface as `requires_action`).
|
|
975
999
|
```
|
|
976
1000
|
|
|
977
1001
|
### File Upload
|
|
@@ -1001,7 +1025,7 @@ await openai.files.del(file.id);
|
|
|
1001
1025
|
|
|
1002
1026
|
```typescript
|
|
1003
1027
|
const cogitator = new Cogitator({
|
|
1004
|
-
defaultModel: 'ollama/llama3.2:latest',
|
|
1028
|
+
llm: { defaultModel: 'ollama/llama3.2:latest' },
|
|
1005
1029
|
});
|
|
1006
1030
|
|
|
1007
1031
|
const server = createOpenAIServer(cogitator, { port: 8080 });
|
|
@@ -1,52 +1,57 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* OpenAI SDK Adapter
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* Creates an in-process adapter that can be used with the SDK's baseURL.
|
|
4
|
+
* Implements the OpenAI Assistants API semantics on top of Cogitator.
|
|
6
5
|
*/
|
|
7
6
|
import { type Cogitator } from '@cogitator-ai/core';
|
|
8
7
|
import type { Tool } from '@cogitator-ai/types';
|
|
9
8
|
import { EventEmitter } from 'events';
|
|
10
9
|
import { ThreadManager } from './thread-manager.js';
|
|
11
|
-
import type {
|
|
12
|
-
|
|
10
|
+
import type { ThreadStorage } from './storage.js';
|
|
11
|
+
import type { Assistant, AssistantTool, CreateMessageRequest, Run, RunStep, Message, MessageDelta, CreateRunRequest, ResponseFormat, SubmitToolOutputsRequest } from '../types/openai-types.js';
|
|
12
|
+
export type StreamEventType = 'thread.run.created' | 'thread.run.queued' | 'thread.run.in_progress' | 'thread.run.requires_action' | 'thread.run.completed' | 'thread.run.failed' | 'thread.run.cancelling' | 'thread.run.cancelled' | 'thread.run.expired' | 'thread.run.step.created' | 'thread.run.step.in_progress' | 'thread.run.step.completed' | 'thread.message.created' | 'thread.message.in_progress' | 'thread.message.delta' | 'thread.message.completed' | 'done' | 'error';
|
|
13
|
+
export type StreamEventData = Run | RunStep | Message | MessageDelta | string;
|
|
13
14
|
export interface StreamEmitterEvents {
|
|
14
|
-
event: [type: StreamEventType, data:
|
|
15
|
+
event: [type: StreamEventType, data: StreamEventData];
|
|
15
16
|
error: [error: Error];
|
|
16
17
|
end: [];
|
|
17
18
|
}
|
|
19
|
+
export interface RunStreamEvent {
|
|
20
|
+
event: StreamEventType;
|
|
21
|
+
data: StreamEventData;
|
|
22
|
+
}
|
|
23
|
+
export interface OpenAIAdapterOptions {
|
|
24
|
+
/** Server-side Cogitator tools available to every run */
|
|
25
|
+
tools?: Tool[];
|
|
26
|
+
/**
|
|
27
|
+
* Model used when an assistant/run asks for the advertised `cogitator`
|
|
28
|
+
* model id (the one listed by `GET /v1/models`).
|
|
29
|
+
*/
|
|
30
|
+
defaultModel?: string;
|
|
31
|
+
/** Maximum number of finished runs kept in memory (default: 10 000) */
|
|
32
|
+
maxStoredRuns?: number;
|
|
33
|
+
/** Persistence for assistants, threads, messages and files (default: in-memory) */
|
|
34
|
+
storage?: ThreadStorage;
|
|
35
|
+
}
|
|
36
|
+
export declare const COGITATOR_MODEL_ID = "cogitator";
|
|
18
37
|
/**
|
|
19
38
|
* OpenAI SDK Adapter
|
|
20
39
|
*
|
|
21
40
|
* @example
|
|
22
41
|
* ```typescript
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* const adapter = createOpenAIAdapter(cogitator, {
|
|
27
|
-
* tools: [calculator, datetime],
|
|
28
|
-
* });
|
|
29
|
-
*
|
|
30
|
-
* const openai = new OpenAI({
|
|
31
|
-
* apiKey: 'not-needed',
|
|
32
|
-
* baseURL: adapter.baseURL,
|
|
33
|
-
* });
|
|
34
|
-
*
|
|
35
|
-
* // Use OpenAI SDK as normal
|
|
36
|
-
* const assistant = await openai.beta.assistants.create({
|
|
37
|
-
* model: 'gpt-4o',
|
|
38
|
-
* name: 'My Assistant',
|
|
39
|
-
* });
|
|
42
|
+
* const adapter = createOpenAIAdapter(cogitator, { tools: [calculator] });
|
|
43
|
+
* const assistant = await adapter.createAssistant({ model: 'openai/gpt-4o' });
|
|
40
44
|
* ```
|
|
41
45
|
*/
|
|
42
46
|
export declare class OpenAIAdapter {
|
|
43
47
|
private cogitator;
|
|
44
48
|
private threadManager;
|
|
45
49
|
private runs;
|
|
50
|
+
private activeRunByThread;
|
|
46
51
|
private tools;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
52
|
+
private defaultModel?;
|
|
53
|
+
private maxStoredRuns;
|
|
54
|
+
constructor(cogitator: Cogitator, options?: OpenAIAdapterOptions);
|
|
50
55
|
/**
|
|
51
56
|
* Get the thread manager for direct access
|
|
52
57
|
*/
|
|
@@ -54,19 +59,25 @@ export declare class OpenAIAdapter {
|
|
|
54
59
|
createAssistant(params: {
|
|
55
60
|
model: string;
|
|
56
61
|
name?: string;
|
|
62
|
+
description?: string;
|
|
57
63
|
instructions?: string;
|
|
58
64
|
tools?: AssistantTool[];
|
|
59
65
|
metadata?: Record<string, string>;
|
|
60
66
|
temperature?: number;
|
|
67
|
+
top_p?: number;
|
|
68
|
+
response_format?: ResponseFormat;
|
|
61
69
|
}): Promise<Assistant>;
|
|
62
70
|
getAssistant(id: string): Promise<Assistant | undefined>;
|
|
63
71
|
updateAssistant(id: string, updates: Partial<{
|
|
64
72
|
model: string;
|
|
65
73
|
name: string;
|
|
74
|
+
description: string;
|
|
66
75
|
instructions: string;
|
|
67
76
|
tools: AssistantTool[];
|
|
68
77
|
metadata: Record<string, string>;
|
|
69
78
|
temperature: number;
|
|
79
|
+
top_p: number;
|
|
80
|
+
response_format: ResponseFormat;
|
|
70
81
|
}>): Promise<Assistant | undefined>;
|
|
71
82
|
deleteAssistant(id: string): Promise<boolean>;
|
|
72
83
|
listAssistants(): Promise<Assistant[]>;
|
|
@@ -77,11 +88,7 @@ export declare class OpenAIAdapter {
|
|
|
77
88
|
metadata?: Record<string, string>;
|
|
78
89
|
}): Promise<import("..").Thread | undefined>;
|
|
79
90
|
deleteThread(id: string): Promise<boolean>;
|
|
80
|
-
addMessage(threadId: string, params:
|
|
81
|
-
role: 'user' | 'assistant';
|
|
82
|
-
content: string;
|
|
83
|
-
metadata?: Record<string, string>;
|
|
84
|
-
}): Promise<Message | undefined>;
|
|
91
|
+
addMessage(threadId: string, params: CreateMessageRequest): Promise<Message | undefined>;
|
|
85
92
|
getMessage(threadId: string, messageId: string): Promise<Message | undefined>;
|
|
86
93
|
listMessages(threadId: string, options?: {
|
|
87
94
|
limit?: number;
|
|
@@ -91,7 +98,10 @@ export declare class OpenAIAdapter {
|
|
|
91
98
|
run_id?: string;
|
|
92
99
|
}): Promise<Message[]>;
|
|
93
100
|
/**
|
|
94
|
-
* Create and
|
|
101
|
+
* Create a run and start executing it in the background.
|
|
102
|
+
*
|
|
103
|
+
* Rejects when the assistant or thread does not exist, or when the thread
|
|
104
|
+
* already has an active run.
|
|
95
105
|
*/
|
|
96
106
|
createRun(threadId: string, request: CreateRunRequest): Promise<Run>;
|
|
97
107
|
/**
|
|
@@ -99,23 +109,50 @@ export declare class OpenAIAdapter {
|
|
|
99
109
|
*/
|
|
100
110
|
getRun(threadId: string, runId: string): Run | undefined;
|
|
101
111
|
/**
|
|
102
|
-
*
|
|
112
|
+
* List runs of a thread, newest first
|
|
113
|
+
*/
|
|
114
|
+
listRuns(threadId: string): Run[];
|
|
115
|
+
/**
|
|
116
|
+
* Cancel a run. Throws when the run already finished.
|
|
103
117
|
*/
|
|
104
118
|
cancelRun(threadId: string, runId: string): Run | undefined;
|
|
105
119
|
/**
|
|
106
|
-
* Submit
|
|
120
|
+
* Submit outputs for the tool calls of a run that requires action.
|
|
121
|
+
* Outputs for every pending tool call must be provided.
|
|
107
122
|
*/
|
|
108
123
|
submitToolOutputs(threadId: string, runId: string, request: SubmitToolOutputsRequest): Promise<Run | undefined>;
|
|
109
124
|
/**
|
|
110
|
-
* Get the
|
|
125
|
+
* Get the event emitter of a run (all runs have one)
|
|
111
126
|
*/
|
|
112
127
|
getStreamEmitter(runId: string): EventEmitter<StreamEmitterEvents> | undefined;
|
|
128
|
+
/**
|
|
129
|
+
* Position in the run's event log; pass it to {@link streamRunEvents} to
|
|
130
|
+
* stream only events emitted after this point.
|
|
131
|
+
*/
|
|
132
|
+
getRunEventCursor(runId: string): number;
|
|
133
|
+
/**
|
|
134
|
+
* Stream run events as server-sent-event payloads, starting at `fromIndex`
|
|
135
|
+
* of the run's event log (0 replays the whole run). The stream ends after the
|
|
136
|
+
* next `done` event, which follows a terminal status or `requires_action`.
|
|
137
|
+
*/
|
|
138
|
+
streamRunEvents(runId: string, fromIndex?: number): AsyncGenerator<RunStreamEvent>;
|
|
139
|
+
private emit;
|
|
140
|
+
private end;
|
|
141
|
+
private clearExpiry;
|
|
142
|
+
private terminate;
|
|
143
|
+
private evictFinishedRuns;
|
|
144
|
+
private resolveModel;
|
|
145
|
+
/**
|
|
146
|
+
* Turn assistant `function` tools into Cogitator tools whose execution is
|
|
147
|
+
* delegated to the API client through `requires_action`.
|
|
148
|
+
*/
|
|
149
|
+
private createClientTools;
|
|
150
|
+
private scheduleRequiresAction;
|
|
151
|
+
private buildRunInput;
|
|
113
152
|
private executeRun;
|
|
114
153
|
}
|
|
115
154
|
/**
|
|
116
155
|
* Create an OpenAI adapter for Cogitator
|
|
117
156
|
*/
|
|
118
|
-
export declare function createOpenAIAdapter(cogitator: Cogitator, options?:
|
|
119
|
-
tools?: Tool[];
|
|
120
|
-
}): OpenAIAdapter;
|
|
157
|
+
export declare function createOpenAIAdapter(cogitator: Cogitator, options?: OpenAIAdapterOptions): OpenAIAdapter;
|
|
121
158
|
//# sourceMappingURL=openai-adapter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-adapter.d.ts","sourceRoot":"","sources":["../../src/client/openai-adapter.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"openai-adapter.d.ts","sourceRoot":"","sources":["../../src/client/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